builtin-stat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. * Jaswinder Singh Rajput <jaswinder@kernel.org>
  31. *
  32. * Released under the GPL v2. (and only v2, not any later version)
  33. */
  34. #include "perf.h"
  35. #include "builtin.h"
  36. #include "util/util.h"
  37. #include "util/parse-options.h"
  38. #include "util/parse-events.h"
  39. #include "util/event.h"
  40. #include "util/debug.h"
  41. #include "util/header.h"
  42. #include "util/cpumap.h"
  43. #include "util/thread.h"
  44. #include <sys/prctl.h>
  45. #include <math.h>
  46. static struct perf_event_attr default_attrs[] = {
  47. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  48. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  49. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  50. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  51. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  52. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  53. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  54. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  55. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES },
  56. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
  57. };
  58. static bool system_wide = false;
  59. static unsigned int nr_cpus = 0;
  60. static int run_idx = 0;
  61. static int run_count = 1;
  62. static bool no_inherit = false;
  63. static bool scale = true;
  64. static pid_t target_pid = -1;
  65. static pid_t target_tid = -1;
  66. static pid_t *all_tids = NULL;
  67. static int thread_num = 0;
  68. static pid_t child_pid = -1;
  69. static bool null_run = false;
  70. static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
  71. static int event_scaled[MAX_COUNTERS];
  72. static volatile int done = 0;
  73. struct stats
  74. {
  75. double n, mean, M2;
  76. };
  77. static void update_stats(struct stats *stats, u64 val)
  78. {
  79. double delta;
  80. stats->n++;
  81. delta = val - stats->mean;
  82. stats->mean += delta / stats->n;
  83. stats->M2 += delta*(val - stats->mean);
  84. }
  85. static double avg_stats(struct stats *stats)
  86. {
  87. return stats->mean;
  88. }
  89. /*
  90. * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  91. *
  92. * (\Sum n_i^2) - ((\Sum n_i)^2)/n
  93. * s^2 = -------------------------------
  94. * n - 1
  95. *
  96. * http://en.wikipedia.org/wiki/Stddev
  97. *
  98. * The std dev of the mean is related to the std dev by:
  99. *
  100. * s
  101. * s_mean = -------
  102. * sqrt(n)
  103. *
  104. */
  105. static double stddev_stats(struct stats *stats)
  106. {
  107. double variance = stats->M2 / (stats->n - 1);
  108. double variance_mean = variance / stats->n;
  109. return sqrt(variance_mean);
  110. }
  111. struct stats event_res_stats[MAX_COUNTERS][3];
  112. struct stats runtime_nsecs_stats;
  113. struct stats walltime_nsecs_stats;
  114. struct stats runtime_cycles_stats;
  115. struct stats runtime_branches_stats;
  116. #define MATCH_EVENT(t, c, counter) \
  117. (attrs[counter].type == PERF_TYPE_##t && \
  118. attrs[counter].config == PERF_COUNT_##c)
  119. #define ERR_PERF_OPEN \
  120. "Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n"
  121. static int create_perf_stat_counter(int counter)
  122. {
  123. struct perf_event_attr *attr = attrs + counter;
  124. int thread;
  125. int ncreated = 0;
  126. if (scale)
  127. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  128. PERF_FORMAT_TOTAL_TIME_RUNNING;
  129. if (system_wide) {
  130. unsigned int cpu;
  131. for (cpu = 0; cpu < nr_cpus; cpu++) {
  132. fd[cpu][counter][0] = sys_perf_event_open(attr,
  133. -1, cpumap[cpu], -1, 0);
  134. if (fd[cpu][counter][0] < 0)
  135. pr_debug(ERR_PERF_OPEN, counter,
  136. fd[cpu][counter][0], strerror(errno));
  137. else
  138. ++ncreated;
  139. }
  140. } else {
  141. attr->inherit = !no_inherit;
  142. if (target_pid == -1 && target_tid == -1) {
  143. attr->disabled = 1;
  144. attr->enable_on_exec = 1;
  145. }
  146. for (thread = 0; thread < thread_num; thread++) {
  147. fd[0][counter][thread] = sys_perf_event_open(attr,
  148. all_tids[thread], -1, -1, 0);
  149. if (fd[0][counter][thread] < 0)
  150. pr_debug(ERR_PERF_OPEN, counter,
  151. fd[0][counter][thread],
  152. strerror(errno));
  153. else
  154. ++ncreated;
  155. }
  156. }
  157. return ncreated;
  158. }
  159. /*
  160. * Does the counter have nsecs as a unit?
  161. */
  162. static inline int nsec_counter(int counter)
  163. {
  164. if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
  165. MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
  166. return 1;
  167. return 0;
  168. }
  169. /*
  170. * Read out the results of a single counter:
  171. */
  172. static void read_counter(int counter)
  173. {
  174. u64 count[3], single_count[3];
  175. unsigned int cpu;
  176. size_t res, nv;
  177. int scaled;
  178. int i, thread;
  179. count[0] = count[1] = count[2] = 0;
  180. nv = scale ? 3 : 1;
  181. for (cpu = 0; cpu < nr_cpus; cpu++) {
  182. for (thread = 0; thread < thread_num; thread++) {
  183. if (fd[cpu][counter][thread] < 0)
  184. continue;
  185. res = read(fd[cpu][counter][thread],
  186. single_count, nv * sizeof(u64));
  187. assert(res == nv * sizeof(u64));
  188. close(fd[cpu][counter][thread]);
  189. fd[cpu][counter][thread] = -1;
  190. count[0] += single_count[0];
  191. if (scale) {
  192. count[1] += single_count[1];
  193. count[2] += single_count[2];
  194. }
  195. }
  196. }
  197. scaled = 0;
  198. if (scale) {
  199. if (count[2] == 0) {
  200. event_scaled[counter] = -1;
  201. count[0] = 0;
  202. return;
  203. }
  204. if (count[2] < count[1]) {
  205. event_scaled[counter] = 1;
  206. count[0] = (unsigned long long)
  207. ((double)count[0] * count[1] / count[2] + 0.5);
  208. }
  209. }
  210. for (i = 0; i < 3; i++)
  211. update_stats(&event_res_stats[counter][i], count[i]);
  212. if (verbose) {
  213. fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
  214. count[0], count[1], count[2]);
  215. }
  216. /*
  217. * Save the full runtime - to allow normalization during printout:
  218. */
  219. if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
  220. update_stats(&runtime_nsecs_stats, count[0]);
  221. if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
  222. update_stats(&runtime_cycles_stats, count[0]);
  223. if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
  224. update_stats(&runtime_branches_stats, count[0]);
  225. }
  226. static int run_perf_stat(int argc __used, const char **argv)
  227. {
  228. unsigned long long t0, t1;
  229. int status = 0;
  230. int counter, ncreated = 0;
  231. int child_ready_pipe[2], go_pipe[2];
  232. const bool forks = (argc > 0);
  233. char buf;
  234. if (!system_wide)
  235. nr_cpus = 1;
  236. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  237. perror("failed to create pipes");
  238. exit(1);
  239. }
  240. if (forks) {
  241. if ((child_pid = fork()) < 0)
  242. perror("failed to fork");
  243. if (!child_pid) {
  244. close(child_ready_pipe[0]);
  245. close(go_pipe[1]);
  246. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  247. /*
  248. * Do a dummy execvp to get the PLT entry resolved,
  249. * so we avoid the resolver overhead on the real
  250. * execvp call.
  251. */
  252. execvp("", (char **)argv);
  253. /*
  254. * Tell the parent we're ready to go
  255. */
  256. close(child_ready_pipe[1]);
  257. /*
  258. * Wait until the parent tells us to go.
  259. */
  260. if (read(go_pipe[0], &buf, 1) == -1)
  261. perror("unable to read pipe");
  262. execvp(argv[0], (char **)argv);
  263. perror(argv[0]);
  264. exit(-1);
  265. }
  266. if (target_tid == -1 && target_pid == -1 && !system_wide)
  267. all_tids[0] = child_pid;
  268. /*
  269. * Wait for the child to be ready to exec.
  270. */
  271. close(child_ready_pipe[1]);
  272. close(go_pipe[0]);
  273. if (read(child_ready_pipe[0], &buf, 1) == -1)
  274. perror("unable to read pipe");
  275. close(child_ready_pipe[0]);
  276. }
  277. for (counter = 0; counter < nr_counters; counter++)
  278. ncreated += create_perf_stat_counter(counter);
  279. if (ncreated == 0) {
  280. pr_err("No permission to collect %sstats.\n"
  281. "Consider tweaking /proc/sys/kernel/perf_event_paranoid.\n",
  282. system_wide ? "system-wide " : "");
  283. if (child_pid != -1)
  284. kill(child_pid, SIGTERM);
  285. return -1;
  286. }
  287. /*
  288. * Enable counters and exec the command:
  289. */
  290. t0 = rdclock();
  291. if (forks) {
  292. close(go_pipe[1]);
  293. wait(&status);
  294. } else {
  295. while(!done) sleep(1);
  296. }
  297. t1 = rdclock();
  298. update_stats(&walltime_nsecs_stats, t1 - t0);
  299. for (counter = 0; counter < nr_counters; counter++)
  300. read_counter(counter);
  301. return WEXITSTATUS(status);
  302. }
  303. static void print_noise(int counter, double avg)
  304. {
  305. if (run_count == 1)
  306. return;
  307. fprintf(stderr, " ( +- %7.3f%% )",
  308. 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
  309. }
  310. static void nsec_printout(int counter, double avg)
  311. {
  312. double msecs = avg / 1e6;
  313. fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter));
  314. if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
  315. fprintf(stderr, " # %10.3f CPUs ",
  316. avg / avg_stats(&walltime_nsecs_stats));
  317. }
  318. }
  319. static void abs_printout(int counter, double avg)
  320. {
  321. double total, ratio = 0.0;
  322. fprintf(stderr, " %14.0f %-24s", avg, event_name(counter));
  323. if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
  324. total = avg_stats(&runtime_cycles_stats);
  325. if (total)
  326. ratio = avg / total;
  327. fprintf(stderr, " # %10.3f IPC ", ratio);
  328. } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
  329. runtime_branches_stats.n != 0) {
  330. total = avg_stats(&runtime_branches_stats);
  331. if (total)
  332. ratio = avg * 100 / total;
  333. fprintf(stderr, " # %10.3f %% ", ratio);
  334. } else if (runtime_nsecs_stats.n != 0) {
  335. total = avg_stats(&runtime_nsecs_stats);
  336. if (total)
  337. ratio = 1000.0 * avg / total;
  338. fprintf(stderr, " # %10.3f M/sec", ratio);
  339. }
  340. }
  341. /*
  342. * Print out the results of a single counter:
  343. */
  344. static void print_counter(int counter)
  345. {
  346. double avg = avg_stats(&event_res_stats[counter][0]);
  347. int scaled = event_scaled[counter];
  348. if (scaled == -1) {
  349. fprintf(stderr, " %14s %-24s\n",
  350. "<not counted>", event_name(counter));
  351. return;
  352. }
  353. if (nsec_counter(counter))
  354. nsec_printout(counter, avg);
  355. else
  356. abs_printout(counter, avg);
  357. print_noise(counter, avg);
  358. if (scaled) {
  359. double avg_enabled, avg_running;
  360. avg_enabled = avg_stats(&event_res_stats[counter][1]);
  361. avg_running = avg_stats(&event_res_stats[counter][2]);
  362. fprintf(stderr, " (scaled from %.2f%%)",
  363. 100 * avg_running / avg_enabled);
  364. }
  365. fprintf(stderr, "\n");
  366. }
  367. static void print_stat(int argc, const char **argv)
  368. {
  369. int i, counter;
  370. fflush(stdout);
  371. fprintf(stderr, "\n");
  372. fprintf(stderr, " Performance counter stats for ");
  373. if(target_pid == -1 && target_tid == -1) {
  374. fprintf(stderr, "\'%s", argv[0]);
  375. for (i = 1; i < argc; i++)
  376. fprintf(stderr, " %s", argv[i]);
  377. } else if (target_pid != -1)
  378. fprintf(stderr, "process id \'%d", target_pid);
  379. else
  380. fprintf(stderr, "thread id \'%d", target_tid);
  381. fprintf(stderr, "\'");
  382. if (run_count > 1)
  383. fprintf(stderr, " (%d runs)", run_count);
  384. fprintf(stderr, ":\n\n");
  385. for (counter = 0; counter < nr_counters; counter++)
  386. print_counter(counter);
  387. fprintf(stderr, "\n");
  388. fprintf(stderr, " %14.9f seconds time elapsed",
  389. avg_stats(&walltime_nsecs_stats)/1e9);
  390. if (run_count > 1) {
  391. fprintf(stderr, " ( +- %7.3f%% )",
  392. 100*stddev_stats(&walltime_nsecs_stats) /
  393. avg_stats(&walltime_nsecs_stats));
  394. }
  395. fprintf(stderr, "\n\n");
  396. }
  397. static volatile int signr = -1;
  398. static void skip_signal(int signo)
  399. {
  400. if(child_pid == -1)
  401. done = 1;
  402. signr = signo;
  403. }
  404. static void sig_atexit(void)
  405. {
  406. if (child_pid != -1)
  407. kill(child_pid, SIGTERM);
  408. if (signr == -1)
  409. return;
  410. signal(signr, SIG_DFL);
  411. kill(getpid(), signr);
  412. }
  413. static const char * const stat_usage[] = {
  414. "perf stat [<options>] [<command>]",
  415. NULL
  416. };
  417. static const struct option options[] = {
  418. OPT_CALLBACK('e', "event", NULL, "event",
  419. "event selector. use 'perf list' to list available events",
  420. parse_events),
  421. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  422. "child tasks do not inherit counters"),
  423. OPT_INTEGER('p', "pid", &target_pid,
  424. "stat events on existing process id"),
  425. OPT_INTEGER('t', "tid", &target_tid,
  426. "stat events on existing thread id"),
  427. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  428. "system-wide collection from all CPUs"),
  429. OPT_BOOLEAN('c', "scale", &scale,
  430. "scale/normalize counters"),
  431. OPT_INCR('v', "verbose", &verbose,
  432. "be more verbose (show counter open errors, etc)"),
  433. OPT_INTEGER('r', "repeat", &run_count,
  434. "repeat command and print average + stddev (max: 100)"),
  435. OPT_BOOLEAN('n', "null", &null_run,
  436. "null run - dont start any counters"),
  437. OPT_END()
  438. };
  439. int cmd_stat(int argc, const char **argv, const char *prefix __used)
  440. {
  441. int status;
  442. int i,j;
  443. argc = parse_options(argc, argv, options, stat_usage,
  444. PARSE_OPT_STOP_AT_NON_OPTION);
  445. if (!argc && target_pid == -1 && target_tid == -1)
  446. usage_with_options(stat_usage, options);
  447. if (run_count <= 0)
  448. usage_with_options(stat_usage, options);
  449. /* Set attrs and nr_counters if no event is selected and !null_run */
  450. if (!null_run && !nr_counters) {
  451. memcpy(attrs, default_attrs, sizeof(default_attrs));
  452. nr_counters = ARRAY_SIZE(default_attrs);
  453. }
  454. if (system_wide)
  455. nr_cpus = read_cpu_map();
  456. else
  457. nr_cpus = 1;
  458. if (target_pid != -1) {
  459. target_tid = target_pid;
  460. thread_num = find_all_tid(target_pid, &all_tids);
  461. if (thread_num <= 0) {
  462. fprintf(stderr, "Can't find all threads of pid %d\n",
  463. target_pid);
  464. usage_with_options(stat_usage, options);
  465. }
  466. } else {
  467. all_tids=malloc(sizeof(pid_t));
  468. if (!all_tids)
  469. return -ENOMEM;
  470. all_tids[0] = target_tid;
  471. thread_num = 1;
  472. }
  473. for (i = 0; i < MAX_NR_CPUS; i++) {
  474. for (j = 0; j < MAX_COUNTERS; j++) {
  475. fd[i][j] = malloc(sizeof(int)*thread_num);
  476. if (!fd[i][j])
  477. return -ENOMEM;
  478. }
  479. }
  480. /*
  481. * We dont want to block the signals - that would cause
  482. * child tasks to inherit that and Ctrl-C would not work.
  483. * What we want is for Ctrl-C to work in the exec()-ed
  484. * task, but being ignored by perf stat itself:
  485. */
  486. atexit(sig_atexit);
  487. signal(SIGINT, skip_signal);
  488. signal(SIGALRM, skip_signal);
  489. signal(SIGABRT, skip_signal);
  490. status = 0;
  491. for (run_idx = 0; run_idx < run_count; run_idx++) {
  492. if (run_count != 1 && verbose)
  493. fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
  494. status = run_perf_stat(argc, argv);
  495. }
  496. if (status != -1)
  497. print_stat(argc, argv);
  498. return status;
  499. }