builtin-stat.c 18 KB

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