builtin-stat.c 12 KB

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