top.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Refactored from builtin-top.c, see that files for further copyright notes.
  5. *
  6. * Released under the GPL v2. (and only v2, not any later version)
  7. */
  8. #include "cpumap.h"
  9. #include "event.h"
  10. #include "evlist.h"
  11. #include "evsel.h"
  12. #include "parse-events.h"
  13. #include "symbol.h"
  14. #include "top.h"
  15. #include <inttypes.h>
  16. #define SNPRINTF(buf, size, fmt, args...) \
  17. ({ \
  18. size_t r = snprintf(buf, size, fmt, ## args); \
  19. r > size ? size : r; \
  20. })
  21. size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size)
  22. {
  23. float samples_per_sec = top->samples / top->delay_secs;
  24. float ksamples_per_sec = top->kernel_samples / top->delay_secs;
  25. float esamples_percent = (100.0 * top->exact_samples) / top->samples;
  26. size_t ret = 0;
  27. if (!perf_guest) {
  28. ret = SNPRINTF(bf, size,
  29. " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
  30. " exact: %4.1f%% [", samples_per_sec,
  31. 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
  32. samples_per_sec)),
  33. esamples_percent);
  34. } else {
  35. float us_samples_per_sec = top->us_samples / top->delay_secs;
  36. float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs;
  37. float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs;
  38. ret = SNPRINTF(bf, size,
  39. " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
  40. " guest kernel:%4.1f%% guest us:%4.1f%%"
  41. " exact: %4.1f%% [", samples_per_sec,
  42. 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
  43. samples_per_sec)),
  44. 100.0 - (100.0 * ((samples_per_sec - us_samples_per_sec) /
  45. samples_per_sec)),
  46. 100.0 - (100.0 * ((samples_per_sec -
  47. guest_kernel_samples_per_sec) /
  48. samples_per_sec)),
  49. 100.0 - (100.0 * ((samples_per_sec -
  50. guest_us_samples_per_sec) /
  51. samples_per_sec)),
  52. esamples_percent);
  53. }
  54. if (top->evlist->nr_entries == 1) {
  55. struct perf_evsel *first;
  56. first = list_entry(top->evlist->entries.next, struct perf_evsel, node);
  57. ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
  58. (uint64_t)first->attr.sample_period,
  59. top->freq ? "Hz" : "");
  60. }
  61. ret += SNPRINTF(bf + ret, size - ret, "%s", event_name(top->sym_evsel));
  62. ret += SNPRINTF(bf + ret, size - ret, "], ");
  63. if (top->target_pid)
  64. ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s",
  65. top->target_pid);
  66. else if (top->target_tid)
  67. ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s",
  68. top->target_tid);
  69. else if (top->uid_str != NULL)
  70. ret += SNPRINTF(bf + ret, size - ret, " (uid: %s",
  71. top->uid_str);
  72. else
  73. ret += SNPRINTF(bf + ret, size - ret, " (all");
  74. if (top->cpu_list)
  75. ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
  76. top->evlist->cpus->nr > 1 ? "s" : "", top->cpu_list);
  77. else {
  78. if (top->target_tid)
  79. ret += SNPRINTF(bf + ret, size - ret, ")");
  80. else
  81. ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
  82. top->evlist->cpus->nr,
  83. top->evlist->cpus->nr > 1 ? "s" : "");
  84. }
  85. return ret;
  86. }
  87. void perf_top__reset_sample_counters(struct perf_top *top)
  88. {
  89. top->samples = top->us_samples = top->kernel_samples =
  90. top->exact_samples = top->guest_kernel_samples =
  91. top->guest_us_samples = 0;
  92. }