builtin-stat.c 18 KB

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