top.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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;
  24. float ksamples_per_sec;
  25. float esamples_percent;
  26. struct record_opts *opts = &top->record_opts;
  27. struct target *target = &opts->target;
  28. size_t ret = 0;
  29. if (top->samples) {
  30. samples_per_sec = top->samples / top->delay_secs;
  31. ksamples_per_sec = top->kernel_samples / top->delay_secs;
  32. esamples_percent = (100.0 * top->exact_samples) / top->samples;
  33. } else {
  34. samples_per_sec = ksamples_per_sec = esamples_percent = 0.0;
  35. }
  36. if (!perf_guest) {
  37. float ksamples_percent = 0.0;
  38. if (samples_per_sec)
  39. ksamples_percent = (100.0 * ksamples_per_sec) /
  40. samples_per_sec;
  41. ret = SNPRINTF(bf, size,
  42. " PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
  43. " exact: %4.1f%% [", samples_per_sec,
  44. ksamples_percent, esamples_percent);
  45. } else {
  46. float us_samples_per_sec = top->us_samples / top->delay_secs;
  47. float guest_kernel_samples_per_sec = top->guest_kernel_samples / top->delay_secs;
  48. float guest_us_samples_per_sec = top->guest_us_samples / top->delay_secs;
  49. ret = SNPRINTF(bf, size,
  50. " PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
  51. " guest kernel:%4.1f%% guest us:%4.1f%%"
  52. " exact: %4.1f%% [", samples_per_sec,
  53. 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
  54. samples_per_sec)),
  55. 100.0 - (100.0 * ((samples_per_sec - us_samples_per_sec) /
  56. samples_per_sec)),
  57. 100.0 - (100.0 * ((samples_per_sec -
  58. guest_kernel_samples_per_sec) /
  59. samples_per_sec)),
  60. 100.0 - (100.0 * ((samples_per_sec -
  61. guest_us_samples_per_sec) /
  62. samples_per_sec)),
  63. esamples_percent);
  64. }
  65. if (top->evlist->nr_entries == 1) {
  66. struct perf_evsel *first = perf_evlist__first(top->evlist);
  67. ret += SNPRINTF(bf + ret, size - ret, "%" PRIu64 "%s ",
  68. (uint64_t)first->attr.sample_period,
  69. opts->freq ? "Hz" : "");
  70. }
  71. ret += SNPRINTF(bf + ret, size - ret, "%s", perf_evsel__name(top->sym_evsel));
  72. ret += SNPRINTF(bf + ret, size - ret, "], ");
  73. if (target->pid)
  74. ret += SNPRINTF(bf + ret, size - ret, " (target_pid: %s",
  75. target->pid);
  76. else if (target->tid)
  77. ret += SNPRINTF(bf + ret, size - ret, " (target_tid: %s",
  78. target->tid);
  79. else if (target->uid_str != NULL)
  80. ret += SNPRINTF(bf + ret, size - ret, " (uid: %s",
  81. target->uid_str);
  82. else
  83. ret += SNPRINTF(bf + ret, size - ret, " (all");
  84. if (target->cpu_list)
  85. ret += SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)",
  86. top->evlist->cpus->nr > 1 ? "s" : "",
  87. target->cpu_list);
  88. else {
  89. if (target->tid)
  90. ret += SNPRINTF(bf + ret, size - ret, ")");
  91. else
  92. ret += SNPRINTF(bf + ret, size - ret, ", %d CPU%s)",
  93. top->evlist->cpus->nr,
  94. top->evlist->cpus->nr > 1 ? "s" : "");
  95. }
  96. return ret;
  97. }
  98. void perf_top__reset_sample_counters(struct perf_top *top)
  99. {
  100. top->samples = top->us_samples = top->kernel_samples =
  101. top->exact_samples = top->guest_kernel_samples =
  102. top->guest_us_samples = 0;
  103. }