builtin-stat.c 18 KB

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