builtin-stat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include <math.h>
  40. static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
  41. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  42. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
  43. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  44. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  45. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  46. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  47. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES},
  48. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
  49. };
  50. static int system_wide = 0;
  51. static int inherit = 1;
  52. static int verbose = 0;
  53. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  54. static int target_pid = -1;
  55. static int nr_cpus = 0;
  56. static unsigned int page_size;
  57. static int scale = 1;
  58. static const unsigned int default_count[] = {
  59. 1000000,
  60. 1000000,
  61. 10000,
  62. 10000,
  63. 1000000,
  64. 10000,
  65. };
  66. #define MAX_RUN 100
  67. static int run_count = 1;
  68. static int run_idx = 0;
  69. static u64 event_res[MAX_RUN][MAX_COUNTERS][3];
  70. static u64 event_scaled[MAX_RUN][MAX_COUNTERS];
  71. //static u64 event_hist[MAX_RUN][MAX_COUNTERS][3];
  72. static u64 runtime_nsecs[MAX_RUN];
  73. static u64 walltime_nsecs[MAX_RUN];
  74. static u64 runtime_cycles[MAX_RUN];
  75. static u64 event_res_avg[MAX_COUNTERS][3];
  76. static u64 event_res_noise[MAX_COUNTERS][3];
  77. static u64 event_scaled_avg[MAX_COUNTERS];
  78. static u64 runtime_nsecs_avg;
  79. static u64 runtime_nsecs_noise;
  80. static u64 walltime_nsecs_avg;
  81. static u64 walltime_nsecs_noise;
  82. static u64 runtime_cycles_avg;
  83. static u64 runtime_cycles_noise;
  84. static void create_perf_stat_counter(int counter)
  85. {
  86. struct perf_counter_attr *attr = attrs + counter;
  87. if (scale)
  88. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  89. PERF_FORMAT_TOTAL_TIME_RUNNING;
  90. if (system_wide) {
  91. int cpu;
  92. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  93. fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
  94. if (fd[cpu][counter] < 0 && verbose) {
  95. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[cpu][counter], strerror(errno));
  96. }
  97. }
  98. } else {
  99. attr->inherit = inherit;
  100. attr->disabled = 1;
  101. fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
  102. if (fd[0][counter] < 0 && verbose) {
  103. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[0][counter], strerror(errno));
  104. }
  105. }
  106. }
  107. /*
  108. * Does the counter have nsecs as a unit?
  109. */
  110. static inline int nsec_counter(int counter)
  111. {
  112. if (attrs[counter].type != PERF_TYPE_SOFTWARE)
  113. return 0;
  114. if (attrs[counter].config == PERF_COUNT_SW_CPU_CLOCK)
  115. return 1;
  116. if (attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
  117. return 1;
  118. return 0;
  119. }
  120. /*
  121. * Read out the results of a single counter:
  122. */
  123. static void read_counter(int counter)
  124. {
  125. u64 *count, single_count[3];
  126. ssize_t res;
  127. int cpu, nv;
  128. int scaled;
  129. count = event_res[run_idx][counter];
  130. count[0] = count[1] = count[2] = 0;
  131. nv = scale ? 3 : 1;
  132. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  133. if (fd[cpu][counter] < 0)
  134. continue;
  135. res = read(fd[cpu][counter], single_count, nv * sizeof(u64));
  136. assert(res == nv * sizeof(u64));
  137. close(fd[cpu][counter]);
  138. fd[cpu][counter] = -1;
  139. count[0] += single_count[0];
  140. if (scale) {
  141. count[1] += single_count[1];
  142. count[2] += single_count[2];
  143. }
  144. }
  145. scaled = 0;
  146. if (scale) {
  147. if (count[2] == 0) {
  148. event_scaled[run_idx][counter] = -1;
  149. count[0] = 0;
  150. return;
  151. }
  152. if (count[2] < count[1]) {
  153. event_scaled[run_idx][counter] = 1;
  154. count[0] = (unsigned long long)
  155. ((double)count[0] * count[1] / count[2] + 0.5);
  156. }
  157. }
  158. /*
  159. * Save the full runtime - to allow normalization during printout:
  160. */
  161. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  162. attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
  163. runtime_nsecs[run_idx] = count[0];
  164. if (attrs[counter].type == PERF_TYPE_HARDWARE &&
  165. attrs[counter].config == PERF_COUNT_HW_CPU_CYCLES)
  166. runtime_cycles[run_idx] = count[0];
  167. }
  168. static int run_perf_stat(int argc, const char **argv)
  169. {
  170. unsigned long long t0, t1;
  171. int status = 0;
  172. int counter;
  173. int pid;
  174. if (!system_wide)
  175. nr_cpus = 1;
  176. for (counter = 0; counter < nr_counters; counter++)
  177. create_perf_stat_counter(counter);
  178. /*
  179. * Enable counters and exec the command:
  180. */
  181. t0 = rdclock();
  182. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  183. if ((pid = fork()) < 0)
  184. perror("failed to fork");
  185. if (!pid) {
  186. if (execvp(argv[0], (char **)argv)) {
  187. perror(argv[0]);
  188. exit(-1);
  189. }
  190. }
  191. wait(&status);
  192. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  193. t1 = rdclock();
  194. walltime_nsecs[run_idx] = t1 - t0;
  195. for (counter = 0; counter < nr_counters; counter++)
  196. read_counter(counter);
  197. return WEXITSTATUS(status);
  198. }
  199. static void print_noise(u64 *count, u64 *noise)
  200. {
  201. if (run_count > 1)
  202. fprintf(stderr, " ( +- %7.3f%% )",
  203. (double)noise[0]/(count[0]+1)*100.0);
  204. }
  205. static void nsec_printout(int counter, u64 *count, u64 *noise)
  206. {
  207. double msecs = (double)count[0] / 1000000;
  208. fprintf(stderr, " %14.6f %-20s", msecs, event_name(counter));
  209. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  210. attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) {
  211. if (walltime_nsecs_avg)
  212. fprintf(stderr, " # %10.3f CPUs ",
  213. (double)count[0] / (double)walltime_nsecs_avg);
  214. }
  215. print_noise(count, noise);
  216. }
  217. static void abs_printout(int counter, u64 *count, u64 *noise)
  218. {
  219. fprintf(stderr, " %14Ld %-20s", count[0], event_name(counter));
  220. if (runtime_cycles_avg &&
  221. attrs[counter].type == PERF_TYPE_HARDWARE &&
  222. attrs[counter].config == PERF_COUNT_HW_INSTRUCTIONS) {
  223. fprintf(stderr, " # %10.3f IPC ",
  224. (double)count[0] / (double)runtime_cycles_avg);
  225. } else {
  226. if (runtime_nsecs_avg) {
  227. fprintf(stderr, " # %10.3f M/sec",
  228. (double)count[0]/runtime_nsecs_avg*1000.0);
  229. }
  230. }
  231. print_noise(count, noise);
  232. }
  233. /*
  234. * Print out the results of a single counter:
  235. */
  236. static void print_counter(int counter)
  237. {
  238. u64 *count, *noise;
  239. int scaled;
  240. count = event_res_avg[counter];
  241. noise = event_res_noise[counter];
  242. scaled = event_scaled_avg[counter];
  243. if (scaled == -1) {
  244. fprintf(stderr, " %14s %-20s\n",
  245. "<not counted>", event_name(counter));
  246. return;
  247. }
  248. if (nsec_counter(counter))
  249. nsec_printout(counter, count, noise);
  250. else
  251. abs_printout(counter, count, noise);
  252. if (scaled)
  253. fprintf(stderr, " (scaled from %.2f%%)",
  254. (double) count[2] / count[1] * 100);
  255. fprintf(stderr, "\n");
  256. }
  257. /*
  258. * normalize_noise noise values down to stddev:
  259. */
  260. static void normalize_noise(u64 *val)
  261. {
  262. double res;
  263. res = (double)*val / (run_count * sqrt((double)run_count));
  264. *val = (u64)res;
  265. }
  266. static void update_avg(const char *name, int idx, u64 *avg, u64 *val)
  267. {
  268. *avg += *val;
  269. if (verbose > 1)
  270. fprintf(stderr, "debug: %20s[%d]: %Ld\n", name, idx, *val);
  271. }
  272. /*
  273. * Calculate the averages and noises:
  274. */
  275. static void calc_avg(void)
  276. {
  277. int i, j;
  278. if (verbose > 1)
  279. fprintf(stderr, "\n");
  280. for (i = 0; i < run_count; i++) {
  281. update_avg("runtime", 0, &runtime_nsecs_avg, runtime_nsecs + i);
  282. update_avg("walltime", 0, &walltime_nsecs_avg, walltime_nsecs + i);
  283. update_avg("runtime_cycles", 0, &runtime_cycles_avg, runtime_cycles + i);
  284. for (j = 0; j < nr_counters; j++) {
  285. update_avg("counter/0", j,
  286. event_res_avg[j]+0, event_res[i][j]+0);
  287. update_avg("counter/1", j,
  288. event_res_avg[j]+1, event_res[i][j]+1);
  289. update_avg("counter/2", j,
  290. event_res_avg[j]+2, event_res[i][j]+2);
  291. update_avg("scaled", j,
  292. event_scaled_avg + j, event_scaled[i]+j);
  293. }
  294. }
  295. runtime_nsecs_avg /= run_count;
  296. walltime_nsecs_avg /= run_count;
  297. runtime_cycles_avg /= run_count;
  298. for (j = 0; j < nr_counters; j++) {
  299. event_res_avg[j][0] /= run_count;
  300. event_res_avg[j][1] /= run_count;
  301. event_res_avg[j][2] /= run_count;
  302. }
  303. for (i = 0; i < run_count; i++) {
  304. runtime_nsecs_noise +=
  305. abs((s64)(runtime_nsecs[i] - runtime_nsecs_avg));
  306. walltime_nsecs_noise +=
  307. abs((s64)(walltime_nsecs[i] - walltime_nsecs_avg));
  308. runtime_cycles_noise +=
  309. abs((s64)(runtime_cycles[i] - runtime_cycles_avg));
  310. for (j = 0; j < nr_counters; j++) {
  311. event_res_noise[j][0] +=
  312. abs((s64)(event_res[i][j][0] - event_res_avg[j][0]));
  313. event_res_noise[j][1] +=
  314. abs((s64)(event_res[i][j][1] - event_res_avg[j][1]));
  315. event_res_noise[j][2] +=
  316. abs((s64)(event_res[i][j][2] - event_res_avg[j][2]));
  317. }
  318. }
  319. normalize_noise(&runtime_nsecs_noise);
  320. normalize_noise(&walltime_nsecs_noise);
  321. normalize_noise(&runtime_cycles_noise);
  322. for (j = 0; j < nr_counters; j++) {
  323. normalize_noise(&event_res_noise[j][0]);
  324. normalize_noise(&event_res_noise[j][1]);
  325. normalize_noise(&event_res_noise[j][2]);
  326. }
  327. }
  328. static void print_stat(int argc, const char **argv)
  329. {
  330. int i, counter;
  331. calc_avg();
  332. fflush(stdout);
  333. fprintf(stderr, "\n");
  334. fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
  335. for (i = 1; i < argc; i++)
  336. fprintf(stderr, " %s", argv[i]);
  337. fprintf(stderr, "\'");
  338. if (run_count > 1)
  339. fprintf(stderr, " (%d runs)", run_count);
  340. fprintf(stderr, ":\n\n");
  341. for (counter = 0; counter < nr_counters; counter++)
  342. print_counter(counter);
  343. fprintf(stderr, "\n");
  344. fprintf(stderr, " %14.9f seconds time elapsed.\n",
  345. (double)walltime_nsecs_avg/1e9);
  346. fprintf(stderr, "\n");
  347. }
  348. static volatile int signr = -1;
  349. static void skip_signal(int signo)
  350. {
  351. signr = signo;
  352. }
  353. static void sig_atexit(void)
  354. {
  355. if (signr == -1)
  356. return;
  357. signal(signr, SIG_DFL);
  358. kill(getpid(), signr);
  359. }
  360. static const char * const stat_usage[] = {
  361. "perf stat [<options>] <command>",
  362. NULL
  363. };
  364. static const struct option options[] = {
  365. OPT_CALLBACK('e', "event", NULL, "event",
  366. "event selector. use 'perf list' to list available events",
  367. parse_events),
  368. OPT_BOOLEAN('i', "inherit", &inherit,
  369. "child tasks inherit counters"),
  370. OPT_INTEGER('p', "pid", &target_pid,
  371. "stat events on existing pid"),
  372. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  373. "system-wide collection from all CPUs"),
  374. OPT_BOOLEAN('S', "scale", &scale,
  375. "scale/normalize counters"),
  376. OPT_BOOLEAN('v', "verbose", &verbose,
  377. "be more verbose (show counter open errors, etc)"),
  378. OPT_INTEGER('r', "repeat", &run_count,
  379. "repeat command and print average + stddev (max: 100)"),
  380. OPT_END()
  381. };
  382. int cmd_stat(int argc, const char **argv, const char *prefix)
  383. {
  384. int status;
  385. page_size = sysconf(_SC_PAGE_SIZE);
  386. memcpy(attrs, default_attrs, sizeof(attrs));
  387. argc = parse_options(argc, argv, options, stat_usage, 0);
  388. if (!argc)
  389. usage_with_options(stat_usage, options);
  390. if (run_count <= 0 || run_count > MAX_RUN)
  391. usage_with_options(stat_usage, options);
  392. if (!nr_counters)
  393. nr_counters = 8;
  394. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  395. assert(nr_cpus <= MAX_NR_CPUS);
  396. assert(nr_cpus >= 0);
  397. /*
  398. * We dont want to block the signals - that would cause
  399. * child tasks to inherit that and Ctrl-C would not work.
  400. * What we want is for Ctrl-C to work in the exec()-ed
  401. * task, but being ignored by perf stat itself:
  402. */
  403. atexit(sig_atexit);
  404. signal(SIGINT, skip_signal);
  405. signal(SIGALRM, skip_signal);
  406. signal(SIGABRT, skip_signal);
  407. status = 0;
  408. for (run_idx = 0; run_idx < run_count; run_idx++) {
  409. if (run_count != 1 && verbose)
  410. fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx+1);
  411. status = run_perf_stat(argc, argv);
  412. }
  413. print_stat(argc, argv);
  414. return status;
  415. }