builtin-stat.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * builtin-stat.c
  3. *
  4. * Builtin stat command: Give a precise performance counters summary
  5. * overview about any workload, CPU or specific PID.
  6. *
  7. * Sample output:
  8. $ perf stat ~/hackbench 10
  9. Time: 0.104
  10. Performance counter stats for '/home/mingo/hackbench':
  11. 1255.538611 task clock ticks # 10.143 CPU utilization factor
  12. 54011 context switches # 0.043 M/sec
  13. 385 CPU migrations # 0.000 M/sec
  14. 17755 pagefaults # 0.014 M/sec
  15. 3808323185 CPU cycles # 3033.219 M/sec
  16. 1575111190 instructions # 1254.530 M/sec
  17. 17367895 cache references # 13.833 M/sec
  18. 7674421 cache misses # 6.112 M/sec
  19. Wall-clock time elapsed: 123.786620 msecs
  20. *
  21. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  22. *
  23. * Improvements and fixes by:
  24. *
  25. * Arjan van de Ven <arjan@linux.intel.com>
  26. * Yanmin Zhang <yanmin.zhang@intel.com>
  27. * Wu Fengguang <fengguang.wu@intel.com>
  28. * Mike Galbraith <efault@gmx.de>
  29. * Paul Mackerras <paulus@samba.org>
  30. *
  31. * Released under the GPL v2. (and only v2, not any later version)
  32. */
  33. #include "perf.h"
  34. #include "builtin.h"
  35. #include "util/util.h"
  36. #include "util/parse-options.h"
  37. #include "util/parse-events.h"
  38. #include <sys/prctl.h>
  39. static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
  40. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_TASK_CLOCK },
  41. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CONTEXT_SWITCHES },
  42. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CPU_MIGRATIONS },
  43. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_PAGE_FAULTS },
  44. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CPU_CYCLES },
  45. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_INSTRUCTIONS },
  46. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_REFERENCES },
  47. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_MISSES },
  48. };
  49. static int system_wide = 0;
  50. static int inherit = 1;
  51. static int verbose = 0;
  52. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  53. static int target_pid = -1;
  54. static int nr_cpus = 0;
  55. static unsigned int page_size;
  56. static int scale = 1;
  57. static const unsigned int default_count[] = {
  58. 1000000,
  59. 1000000,
  60. 10000,
  61. 10000,
  62. 1000000,
  63. 10000,
  64. };
  65. static __u64 event_res[MAX_COUNTERS][3];
  66. static __u64 event_scaled[MAX_COUNTERS];
  67. static __u64 runtime_nsecs;
  68. static __u64 walltime_nsecs;
  69. static void create_perf_stat_counter(int counter)
  70. {
  71. struct perf_counter_attr *attr = attrs + counter;
  72. if (scale)
  73. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  74. PERF_FORMAT_TOTAL_TIME_RUNNING;
  75. if (system_wide) {
  76. int cpu;
  77. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  78. fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
  79. if (fd[cpu][counter] < 0 && verbose) {
  80. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[cpu][counter], strerror(errno));
  81. }
  82. }
  83. } else {
  84. attr->inherit = inherit;
  85. attr->disabled = 1;
  86. fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
  87. if (fd[0][counter] < 0 && verbose) {
  88. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[0][counter], strerror(errno));
  89. }
  90. }
  91. }
  92. /*
  93. * Does the counter have nsecs as a unit?
  94. */
  95. static inline int nsec_counter(int counter)
  96. {
  97. if (attrs[counter].type != PERF_TYPE_SOFTWARE)
  98. return 0;
  99. if (attrs[counter].config == PERF_COUNT_CPU_CLOCK)
  100. return 1;
  101. if (attrs[counter].config == PERF_COUNT_TASK_CLOCK)
  102. return 1;
  103. return 0;
  104. }
  105. /*
  106. * Read out the results of a single counter:
  107. */
  108. static void read_counter(int counter)
  109. {
  110. __u64 *count, single_count[3];
  111. ssize_t res;
  112. int cpu, nv;
  113. int scaled;
  114. count = event_res[counter];
  115. count[0] = count[1] = count[2] = 0;
  116. nv = scale ? 3 : 1;
  117. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  118. if (fd[cpu][counter] < 0)
  119. continue;
  120. res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
  121. assert(res == nv * sizeof(__u64));
  122. count[0] += single_count[0];
  123. if (scale) {
  124. count[1] += single_count[1];
  125. count[2] += single_count[2];
  126. }
  127. }
  128. scaled = 0;
  129. if (scale) {
  130. if (count[2] == 0) {
  131. event_scaled[counter] = -1;
  132. count[0] = 0;
  133. return;
  134. }
  135. if (count[2] < count[1]) {
  136. event_scaled[counter] = 1;
  137. count[0] = (unsigned long long)
  138. ((double)count[0] * count[1] / count[2] + 0.5);
  139. }
  140. }
  141. /*
  142. * Save the full runtime - to allow normalization during printout:
  143. */
  144. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  145. attrs[counter].config == PERF_COUNT_TASK_CLOCK)
  146. runtime_nsecs = count[0];
  147. }
  148. /*
  149. * Print out the results of a single counter:
  150. */
  151. static void print_counter(int counter)
  152. {
  153. __u64 *count;
  154. int scaled;
  155. count = event_res[counter];
  156. scaled = event_scaled[counter];
  157. if (scaled == -1) {
  158. fprintf(stderr, " %14s %-20s\n",
  159. "<not counted>", event_name(counter));
  160. return;
  161. }
  162. if (nsec_counter(counter)) {
  163. double msecs = (double)count[0] / 1000000;
  164. fprintf(stderr, " %14.6f %-20s",
  165. msecs, event_name(counter));
  166. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  167. attrs[counter].config == PERF_COUNT_TASK_CLOCK) {
  168. if (walltime_nsecs)
  169. fprintf(stderr, " # %11.3f CPU utilization factor",
  170. (double)count[0] / (double)walltime_nsecs);
  171. }
  172. } else {
  173. fprintf(stderr, " %14Ld %-20s",
  174. count[0], event_name(counter));
  175. if (runtime_nsecs)
  176. fprintf(stderr, " # %11.3f M/sec",
  177. (double)count[0]/runtime_nsecs*1000.0);
  178. }
  179. if (scaled)
  180. fprintf(stderr, " (scaled from %.2f%%)",
  181. (double) count[2] / count[1] * 100);
  182. fprintf(stderr, "\n");
  183. }
  184. static int do_perf_stat(int argc, const char **argv)
  185. {
  186. unsigned long long t0, t1;
  187. int counter;
  188. int status;
  189. int pid;
  190. int i;
  191. if (!system_wide)
  192. nr_cpus = 1;
  193. for (counter = 0; counter < nr_counters; counter++)
  194. create_perf_stat_counter(counter);
  195. /*
  196. * Enable counters and exec the command:
  197. */
  198. t0 = rdclock();
  199. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  200. if ((pid = fork()) < 0)
  201. perror("failed to fork");
  202. if (!pid) {
  203. if (execvp(argv[0], (char **)argv)) {
  204. perror(argv[0]);
  205. exit(-1);
  206. }
  207. }
  208. while (wait(&status) >= 0)
  209. ;
  210. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  211. t1 = rdclock();
  212. walltime_nsecs = t1 - t0;
  213. fflush(stdout);
  214. fprintf(stderr, "\n");
  215. fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
  216. for (i = 1; i < argc; i++)
  217. fprintf(stderr, " %s", argv[i]);
  218. fprintf(stderr, "\':\n");
  219. fprintf(stderr, "\n");
  220. for (counter = 0; counter < nr_counters; counter++)
  221. read_counter(counter);
  222. for (counter = 0; counter < nr_counters; counter++)
  223. print_counter(counter);
  224. fprintf(stderr, "\n");
  225. fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
  226. (double)(t1-t0)/1e6);
  227. fprintf(stderr, "\n");
  228. return 0;
  229. }
  230. static void skip_signal(int signo)
  231. {
  232. }
  233. static const char * const stat_usage[] = {
  234. "perf stat [<options>] <command>",
  235. NULL
  236. };
  237. static const struct option options[] = {
  238. OPT_CALLBACK('e', "event", NULL, "event",
  239. "event selector. use 'perf list' to list available events",
  240. parse_events),
  241. OPT_BOOLEAN('i', "inherit", &inherit,
  242. "child tasks inherit counters"),
  243. OPT_INTEGER('p', "pid", &target_pid,
  244. "stat events on existing pid"),
  245. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  246. "system-wide collection from all CPUs"),
  247. OPT_BOOLEAN('S', "scale", &scale,
  248. "scale/normalize counters"),
  249. OPT_BOOLEAN('v', "verbose", &verbose,
  250. "be more verbose (show counter open errors, etc)"),
  251. OPT_END()
  252. };
  253. int cmd_stat(int argc, const char **argv, const char *prefix)
  254. {
  255. page_size = sysconf(_SC_PAGE_SIZE);
  256. memcpy(attrs, default_attrs, sizeof(attrs));
  257. argc = parse_options(argc, argv, options, stat_usage, 0);
  258. if (!argc)
  259. usage_with_options(stat_usage, options);
  260. if (!nr_counters)
  261. nr_counters = 8;
  262. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  263. assert(nr_cpus <= MAX_NR_CPUS);
  264. assert(nr_cpus >= 0);
  265. /*
  266. * We dont want to block the signals - that would cause
  267. * child tasks to inherit that and Ctrl-C would not work.
  268. * What we want is for Ctrl-C to work in the exec()-ed
  269. * task, but being ignored by perf stat itself:
  270. */
  271. signal(SIGINT, skip_signal);
  272. signal(SIGALRM, skip_signal);
  273. signal(SIGABRT, skip_signal);
  274. return do_perf_stat(argc, argv);
  275. }