builtin-stat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. #include <locale.h>
  47. #define DEFAULT_SEPARATOR " "
  48. static struct perf_event_attr default_attrs[] = {
  49. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  50. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  51. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  52. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  53. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  54. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  55. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  56. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  57. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES },
  58. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
  59. };
  60. static bool system_wide = false;
  61. static int nr_cpus = 0;
  62. static int run_idx = 0;
  63. static int run_count = 1;
  64. static bool no_inherit = false;
  65. static bool scale = true;
  66. static bool no_aggr = false;
  67. static pid_t target_pid = -1;
  68. static pid_t target_tid = -1;
  69. static pid_t *all_tids = NULL;
  70. static int thread_num = 0;
  71. static pid_t child_pid = -1;
  72. static bool null_run = false;
  73. static bool big_num = true;
  74. static int big_num_opt = -1;
  75. static const char *cpu_list;
  76. static const char *csv_sep = NULL;
  77. static bool csv_output = false;
  78. static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
  79. static int event_scaled[MAX_COUNTERS];
  80. static struct {
  81. u64 val;
  82. u64 ena;
  83. u64 run;
  84. } cpu_counts[MAX_NR_CPUS][MAX_COUNTERS];
  85. static volatile int done = 0;
  86. struct stats
  87. {
  88. double n, mean, M2;
  89. };
  90. static void update_stats(struct stats *stats, u64 val)
  91. {
  92. double delta;
  93. stats->n++;
  94. delta = val - stats->mean;
  95. stats->mean += delta / stats->n;
  96. stats->M2 += delta*(val - stats->mean);
  97. }
  98. static double avg_stats(struct stats *stats)
  99. {
  100. return stats->mean;
  101. }
  102. /*
  103. * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  104. *
  105. * (\Sum n_i^2) - ((\Sum n_i)^2)/n
  106. * s^2 = -------------------------------
  107. * n - 1
  108. *
  109. * http://en.wikipedia.org/wiki/Stddev
  110. *
  111. * The std dev of the mean is related to the std dev by:
  112. *
  113. * s
  114. * s_mean = -------
  115. * sqrt(n)
  116. *
  117. */
  118. static double stddev_stats(struct stats *stats)
  119. {
  120. double variance = stats->M2 / (stats->n - 1);
  121. double variance_mean = variance / stats->n;
  122. return sqrt(variance_mean);
  123. }
  124. struct stats event_res_stats[MAX_COUNTERS][3];
  125. struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  126. struct stats runtime_cycles_stats[MAX_NR_CPUS];
  127. struct stats runtime_branches_stats[MAX_NR_CPUS];
  128. struct stats walltime_nsecs_stats;
  129. #define MATCH_EVENT(t, c, counter) \
  130. (attrs[counter].type == PERF_TYPE_##t && \
  131. attrs[counter].config == PERF_COUNT_##c)
  132. #define ERR_PERF_OPEN \
  133. "counter %d, sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information."
  134. static int create_perf_stat_counter(int counter, bool *perm_err)
  135. {
  136. struct perf_event_attr *attr = attrs + counter;
  137. int thread;
  138. int ncreated = 0;
  139. if (scale)
  140. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  141. PERF_FORMAT_TOTAL_TIME_RUNNING;
  142. if (system_wide) {
  143. int cpu;
  144. for (cpu = 0; cpu < nr_cpus; cpu++) {
  145. fd[cpu][counter][0] = sys_perf_event_open(attr,
  146. -1, cpumap[cpu], -1, 0);
  147. if (fd[cpu][counter][0] < 0) {
  148. if (errno == EPERM || errno == EACCES)
  149. *perm_err = true;
  150. error(ERR_PERF_OPEN, counter,
  151. fd[cpu][counter][0], strerror(errno));
  152. } else {
  153. ++ncreated;
  154. }
  155. }
  156. } else {
  157. attr->inherit = !no_inherit;
  158. if (target_pid == -1 && target_tid == -1) {
  159. attr->disabled = 1;
  160. attr->enable_on_exec = 1;
  161. }
  162. for (thread = 0; thread < thread_num; thread++) {
  163. fd[0][counter][thread] = sys_perf_event_open(attr,
  164. all_tids[thread], -1, -1, 0);
  165. if (fd[0][counter][thread] < 0) {
  166. if (errno == EPERM || errno == EACCES)
  167. *perm_err = true;
  168. error(ERR_PERF_OPEN, counter,
  169. fd[0][counter][thread],
  170. strerror(errno));
  171. } else {
  172. ++ncreated;
  173. }
  174. }
  175. }
  176. return ncreated;
  177. }
  178. /*
  179. * Does the counter have nsecs as a unit?
  180. */
  181. static inline int nsec_counter(int counter)
  182. {
  183. if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) ||
  184. MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
  185. return 1;
  186. return 0;
  187. }
  188. /*
  189. * Read out the results of a single counter:
  190. * aggregate counts across CPUs in system-wide mode
  191. */
  192. static void read_counter_aggr(int counter)
  193. {
  194. u64 count[3], single_count[3];
  195. int cpu;
  196. size_t res, nv;
  197. int scaled;
  198. int i, thread;
  199. count[0] = count[1] = count[2] = 0;
  200. nv = scale ? 3 : 1;
  201. for (cpu = 0; cpu < nr_cpus; cpu++) {
  202. for (thread = 0; thread < thread_num; thread++) {
  203. if (fd[cpu][counter][thread] < 0)
  204. continue;
  205. res = read(fd[cpu][counter][thread],
  206. single_count, nv * sizeof(u64));
  207. assert(res == nv * sizeof(u64));
  208. close(fd[cpu][counter][thread]);
  209. fd[cpu][counter][thread] = -1;
  210. count[0] += single_count[0];
  211. if (scale) {
  212. count[1] += single_count[1];
  213. count[2] += single_count[2];
  214. }
  215. }
  216. }
  217. scaled = 0;
  218. if (scale) {
  219. if (count[2] == 0) {
  220. event_scaled[counter] = -1;
  221. count[0] = 0;
  222. return;
  223. }
  224. if (count[2] < count[1]) {
  225. event_scaled[counter] = 1;
  226. count[0] = (unsigned long long)
  227. ((double)count[0] * count[1] / count[2] + 0.5);
  228. }
  229. }
  230. for (i = 0; i < 3; i++)
  231. update_stats(&event_res_stats[counter][i], count[i]);
  232. if (verbose) {
  233. fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
  234. count[0], count[1], count[2]);
  235. }
  236. /*
  237. * Save the full runtime - to allow normalization during printout:
  238. */
  239. if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
  240. update_stats(&runtime_nsecs_stats[0], count[0]);
  241. if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
  242. update_stats(&runtime_cycles_stats[0], count[0]);
  243. if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
  244. update_stats(&runtime_branches_stats[0], count[0]);
  245. }
  246. /*
  247. * Read out the results of a single counter:
  248. * do not aggregate counts across CPUs in system-wide mode
  249. */
  250. static void read_counter(int counter)
  251. {
  252. u64 count[3];
  253. int cpu;
  254. size_t res, nv;
  255. count[0] = count[1] = count[2] = 0;
  256. nv = scale ? 3 : 1;
  257. for (cpu = 0; cpu < nr_cpus; cpu++) {
  258. if (fd[cpu][counter][0] < 0)
  259. continue;
  260. res = read(fd[cpu][counter][0], count, nv * sizeof(u64));
  261. assert(res == nv * sizeof(u64));
  262. close(fd[cpu][counter][0]);
  263. fd[cpu][counter][0] = -1;
  264. if (scale) {
  265. if (count[2] == 0) {
  266. count[0] = 0;
  267. } else if (count[2] < count[1]) {
  268. count[0] = (unsigned long long)
  269. ((double)count[0] * count[1] / count[2] + 0.5);
  270. }
  271. }
  272. cpu_counts[cpu][counter].val = count[0]; /* scaled count */
  273. cpu_counts[cpu][counter].ena = count[1];
  274. cpu_counts[cpu][counter].run = count[2];
  275. if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter))
  276. update_stats(&runtime_nsecs_stats[cpu], count[0]);
  277. if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter))
  278. update_stats(&runtime_cycles_stats[cpu], count[0]);
  279. if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter))
  280. update_stats(&runtime_branches_stats[cpu], count[0]);
  281. }
  282. }
  283. static int run_perf_stat(int argc __used, const char **argv)
  284. {
  285. unsigned long long t0, t1;
  286. int status = 0;
  287. int counter, ncreated = 0;
  288. int child_ready_pipe[2], go_pipe[2];
  289. bool perm_err = false;
  290. const bool forks = (argc > 0);
  291. char buf;
  292. if (!system_wide)
  293. nr_cpus = 1;
  294. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  295. perror("failed to create pipes");
  296. exit(1);
  297. }
  298. if (forks) {
  299. if ((child_pid = fork()) < 0)
  300. perror("failed to fork");
  301. if (!child_pid) {
  302. close(child_ready_pipe[0]);
  303. close(go_pipe[1]);
  304. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  305. /*
  306. * Do a dummy execvp to get the PLT entry resolved,
  307. * so we avoid the resolver overhead on the real
  308. * execvp call.
  309. */
  310. execvp("", (char **)argv);
  311. /*
  312. * Tell the parent we're ready to go
  313. */
  314. close(child_ready_pipe[1]);
  315. /*
  316. * Wait until the parent tells us to go.
  317. */
  318. if (read(go_pipe[0], &buf, 1) == -1)
  319. perror("unable to read pipe");
  320. execvp(argv[0], (char **)argv);
  321. perror(argv[0]);
  322. exit(-1);
  323. }
  324. if (target_tid == -1 && target_pid == -1 && !system_wide)
  325. all_tids[0] = child_pid;
  326. /*
  327. * Wait for the child to be ready to exec.
  328. */
  329. close(child_ready_pipe[1]);
  330. close(go_pipe[0]);
  331. if (read(child_ready_pipe[0], &buf, 1) == -1)
  332. perror("unable to read pipe");
  333. close(child_ready_pipe[0]);
  334. }
  335. for (counter = 0; counter < nr_counters; counter++)
  336. ncreated += create_perf_stat_counter(counter, &perm_err);
  337. if (ncreated < nr_counters) {
  338. if (perm_err)
  339. error("You may not have permission to collect %sstats.\n"
  340. "\t Consider tweaking"
  341. " /proc/sys/kernel/perf_event_paranoid or running as root.",
  342. system_wide ? "system-wide " : "");
  343. die("Not all events could be opened.\n");
  344. if (child_pid != -1)
  345. kill(child_pid, SIGTERM);
  346. return -1;
  347. }
  348. /*
  349. * Enable counters and exec the command:
  350. */
  351. t0 = rdclock();
  352. if (forks) {
  353. close(go_pipe[1]);
  354. wait(&status);
  355. } else {
  356. while(!done) sleep(1);
  357. }
  358. t1 = rdclock();
  359. update_stats(&walltime_nsecs_stats, t1 - t0);
  360. if (no_aggr) {
  361. for (counter = 0; counter < nr_counters; counter++)
  362. read_counter(counter);
  363. } else {
  364. for (counter = 0; counter < nr_counters; counter++)
  365. read_counter_aggr(counter);
  366. }
  367. return WEXITSTATUS(status);
  368. }
  369. static void print_noise(int counter, double avg)
  370. {
  371. if (run_count == 1)
  372. return;
  373. fprintf(stderr, " ( +- %7.3f%% )",
  374. 100 * stddev_stats(&event_res_stats[counter][0]) / avg);
  375. }
  376. static void nsec_printout(int cpu, int counter, double avg)
  377. {
  378. double msecs = avg / 1e6;
  379. char cpustr[16] = { '\0', };
  380. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
  381. if (no_aggr)
  382. sprintf(cpustr, "CPU%*d%s",
  383. csv_output ? 0 : -4,
  384. cpumap[cpu], csv_sep);
  385. fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(counter));
  386. if (csv_output)
  387. return;
  388. if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
  389. fprintf(stderr, " # %10.3f CPUs ",
  390. avg / avg_stats(&walltime_nsecs_stats));
  391. }
  392. }
  393. static void abs_printout(int cpu, int counter, double avg)
  394. {
  395. double total, ratio = 0.0;
  396. char cpustr[16] = { '\0', };
  397. const char *fmt;
  398. if (csv_output)
  399. fmt = "%s%.0f%s%s";
  400. else if (big_num)
  401. fmt = "%s%'18.0f%s%-24s";
  402. else
  403. fmt = "%s%18.0f%s%-24s";
  404. if (no_aggr)
  405. sprintf(cpustr, "CPU%*d%s",
  406. csv_output ? 0 : -4,
  407. cpumap[cpu], csv_sep);
  408. else
  409. cpu = 0;
  410. fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(counter));
  411. if (csv_output)
  412. return;
  413. if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
  414. total = avg_stats(&runtime_cycles_stats[cpu]);
  415. if (total)
  416. ratio = avg / total;
  417. fprintf(stderr, " # %10.3f IPC ", ratio);
  418. } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) &&
  419. runtime_branches_stats[cpu].n != 0) {
  420. total = avg_stats(&runtime_branches_stats[cpu]);
  421. if (total)
  422. ratio = avg * 100 / total;
  423. fprintf(stderr, " # %10.3f %% ", ratio);
  424. } else if (runtime_nsecs_stats[cpu].n != 0) {
  425. total = avg_stats(&runtime_nsecs_stats[cpu]);
  426. if (total)
  427. ratio = 1000.0 * avg / total;
  428. fprintf(stderr, " # %10.3f M/sec", ratio);
  429. }
  430. }
  431. /*
  432. * Print out the results of a single counter:
  433. * aggregated counts in system-wide mode
  434. */
  435. static void print_counter_aggr(int counter)
  436. {
  437. double avg = avg_stats(&event_res_stats[counter][0]);
  438. int scaled = event_scaled[counter];
  439. if (scaled == -1) {
  440. fprintf(stderr, "%*s%s%-24s\n",
  441. csv_output ? 0 : 18,
  442. "<not counted>", csv_sep, event_name(counter));
  443. return;
  444. }
  445. if (nsec_counter(counter))
  446. nsec_printout(-1, counter, avg);
  447. else
  448. abs_printout(-1, counter, avg);
  449. if (csv_output) {
  450. fputc('\n', stderr);
  451. return;
  452. }
  453. print_noise(counter, avg);
  454. if (scaled) {
  455. double avg_enabled, avg_running;
  456. avg_enabled = avg_stats(&event_res_stats[counter][1]);
  457. avg_running = avg_stats(&event_res_stats[counter][2]);
  458. fprintf(stderr, " (scaled from %.2f%%)",
  459. 100 * avg_running / avg_enabled);
  460. }
  461. fprintf(stderr, "\n");
  462. }
  463. /*
  464. * Print out the results of a single counter:
  465. * does not use aggregated count in system-wide
  466. */
  467. static void print_counter(int counter)
  468. {
  469. u64 ena, run, val;
  470. int cpu;
  471. for (cpu = 0; cpu < nr_cpus; cpu++) {
  472. val = cpu_counts[cpu][counter].val;
  473. ena = cpu_counts[cpu][counter].ena;
  474. run = cpu_counts[cpu][counter].run;
  475. if (run == 0 || ena == 0) {
  476. fprintf(stderr, "CPU%*d%s%*s%s%-24s",
  477. csv_output ? 0 : -4,
  478. cpumap[cpu], csv_sep,
  479. csv_output ? 0 : 18,
  480. "<not counted>", csv_sep,
  481. event_name(counter));
  482. fprintf(stderr, "\n");
  483. continue;
  484. }
  485. if (nsec_counter(counter))
  486. nsec_printout(cpu, counter, val);
  487. else
  488. abs_printout(cpu, counter, val);
  489. if (!csv_output) {
  490. print_noise(counter, 1.0);
  491. if (run != ena) {
  492. fprintf(stderr, " (scaled from %.2f%%)",
  493. 100.0 * run / ena);
  494. }
  495. }
  496. fprintf(stderr, "\n");
  497. }
  498. }
  499. static void print_stat(int argc, const char **argv)
  500. {
  501. int i, counter;
  502. fflush(stdout);
  503. if (!csv_output) {
  504. fprintf(stderr, "\n");
  505. fprintf(stderr, " Performance counter stats for ");
  506. if(target_pid == -1 && target_tid == -1) {
  507. fprintf(stderr, "\'%s", argv[0]);
  508. for (i = 1; i < argc; i++)
  509. fprintf(stderr, " %s", argv[i]);
  510. } else if (target_pid != -1)
  511. fprintf(stderr, "process id \'%d", target_pid);
  512. else
  513. fprintf(stderr, "thread id \'%d", target_tid);
  514. fprintf(stderr, "\'");
  515. if (run_count > 1)
  516. fprintf(stderr, " (%d runs)", run_count);
  517. fprintf(stderr, ":\n\n");
  518. }
  519. if (no_aggr) {
  520. for (counter = 0; counter < nr_counters; counter++)
  521. print_counter(counter);
  522. } else {
  523. for (counter = 0; counter < nr_counters; counter++)
  524. print_counter_aggr(counter);
  525. }
  526. if (!csv_output) {
  527. fprintf(stderr, "\n");
  528. fprintf(stderr, " %18.9f seconds time elapsed",
  529. avg_stats(&walltime_nsecs_stats)/1e9);
  530. if (run_count > 1) {
  531. fprintf(stderr, " ( +- %7.3f%% )",
  532. 100*stddev_stats(&walltime_nsecs_stats) /
  533. avg_stats(&walltime_nsecs_stats));
  534. }
  535. fprintf(stderr, "\n\n");
  536. }
  537. }
  538. static volatile int signr = -1;
  539. static void skip_signal(int signo)
  540. {
  541. if(child_pid == -1)
  542. done = 1;
  543. signr = signo;
  544. }
  545. static void sig_atexit(void)
  546. {
  547. if (child_pid != -1)
  548. kill(child_pid, SIGTERM);
  549. if (signr == -1)
  550. return;
  551. signal(signr, SIG_DFL);
  552. kill(getpid(), signr);
  553. }
  554. static const char * const stat_usage[] = {
  555. "perf stat [<options>] [<command>]",
  556. NULL
  557. };
  558. static int stat__set_big_num(const struct option *opt __used,
  559. const char *s __used, int unset)
  560. {
  561. big_num_opt = unset ? 0 : 1;
  562. return 0;
  563. }
  564. static const struct option options[] = {
  565. OPT_CALLBACK('e', "event", NULL, "event",
  566. "event selector. use 'perf list' to list available events",
  567. parse_events),
  568. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  569. "child tasks do not inherit counters"),
  570. OPT_INTEGER('p', "pid", &target_pid,
  571. "stat events on existing process id"),
  572. OPT_INTEGER('t', "tid", &target_tid,
  573. "stat events on existing thread id"),
  574. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  575. "system-wide collection from all CPUs"),
  576. OPT_BOOLEAN('c', "scale", &scale,
  577. "scale/normalize counters"),
  578. OPT_INCR('v', "verbose", &verbose,
  579. "be more verbose (show counter open errors, etc)"),
  580. OPT_INTEGER('r', "repeat", &run_count,
  581. "repeat command and print average + stddev (max: 100)"),
  582. OPT_BOOLEAN('n', "null", &null_run,
  583. "null run - dont start any counters"),
  584. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  585. "print large numbers with thousands\' separators",
  586. stat__set_big_num),
  587. OPT_STRING('C', "cpu", &cpu_list, "cpu",
  588. "list of cpus to monitor in system-wide"),
  589. OPT_BOOLEAN('A', "no-aggr", &no_aggr,
  590. "disable CPU count aggregation"),
  591. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  592. "print counts with custom separator"),
  593. OPT_END()
  594. };
  595. int cmd_stat(int argc, const char **argv, const char *prefix __used)
  596. {
  597. int status;
  598. int i,j;
  599. setlocale(LC_ALL, "");
  600. argc = parse_options(argc, argv, options, stat_usage,
  601. PARSE_OPT_STOP_AT_NON_OPTION);
  602. if (csv_sep)
  603. csv_output = true;
  604. else
  605. csv_sep = DEFAULT_SEPARATOR;
  606. /*
  607. * let the spreadsheet do the pretty-printing
  608. */
  609. if (csv_output) {
  610. /* User explicitely passed -B? */
  611. if (big_num_opt == 1) {
  612. fprintf(stderr, "-B option not supported with -x\n");
  613. usage_with_options(stat_usage, options);
  614. } else /* Nope, so disable big number formatting */
  615. big_num = false;
  616. } else if (big_num_opt == 0) /* User passed --no-big-num */
  617. big_num = false;
  618. if (!argc && target_pid == -1 && target_tid == -1)
  619. usage_with_options(stat_usage, options);
  620. if (run_count <= 0)
  621. usage_with_options(stat_usage, options);
  622. /* no_aggr is for system-wide only */
  623. if (no_aggr && !system_wide)
  624. usage_with_options(stat_usage, options);
  625. /* Set attrs and nr_counters if no event is selected and !null_run */
  626. if (!null_run && !nr_counters) {
  627. memcpy(attrs, default_attrs, sizeof(default_attrs));
  628. nr_counters = ARRAY_SIZE(default_attrs);
  629. }
  630. if (system_wide)
  631. nr_cpus = read_cpu_map(cpu_list);
  632. else
  633. nr_cpus = 1;
  634. if (nr_cpus < 1)
  635. usage_with_options(stat_usage, options);
  636. if (target_pid != -1) {
  637. target_tid = target_pid;
  638. thread_num = find_all_tid(target_pid, &all_tids);
  639. if (thread_num <= 0) {
  640. fprintf(stderr, "Can't find all threads of pid %d\n",
  641. target_pid);
  642. usage_with_options(stat_usage, options);
  643. }
  644. } else {
  645. all_tids=malloc(sizeof(pid_t));
  646. if (!all_tids)
  647. return -ENOMEM;
  648. all_tids[0] = target_tid;
  649. thread_num = 1;
  650. }
  651. for (i = 0; i < MAX_NR_CPUS; i++) {
  652. for (j = 0; j < MAX_COUNTERS; j++) {
  653. fd[i][j] = malloc(sizeof(int)*thread_num);
  654. if (!fd[i][j])
  655. return -ENOMEM;
  656. }
  657. }
  658. /*
  659. * We dont want to block the signals - that would cause
  660. * child tasks to inherit that and Ctrl-C would not work.
  661. * What we want is for Ctrl-C to work in the exec()-ed
  662. * task, but being ignored by perf stat itself:
  663. */
  664. atexit(sig_atexit);
  665. signal(SIGINT, skip_signal);
  666. signal(SIGALRM, skip_signal);
  667. signal(SIGABRT, skip_signal);
  668. status = 0;
  669. for (run_idx = 0; run_idx < run_count; run_idx++) {
  670. if (run_count != 1 && verbose)
  671. fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
  672. status = run_perf_stat(argc, argv);
  673. }
  674. if (status != -1)
  675. print_stat(argc, argv);
  676. return status;
  677. }