builtin-stat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 {
  265. error("open_counter returned with %d (%s). "
  266. "/bin/dmesg may provide additional information.\n",
  267. errno, strerror(errno));
  268. }
  269. if (child_pid != -1)
  270. kill(child_pid, SIGTERM);
  271. die("Not all events could be opened.\n");
  272. return -1;
  273. }
  274. }
  275. /*
  276. * Enable counters and exec the command:
  277. */
  278. t0 = rdclock();
  279. if (forks) {
  280. close(go_pipe[1]);
  281. wait(&status);
  282. } else {
  283. while(!done) sleep(1);
  284. }
  285. t1 = rdclock();
  286. update_stats(&walltime_nsecs_stats, t1 - t0);
  287. if (no_aggr) {
  288. list_for_each_entry(counter, &evsel_list, node) {
  289. read_counter(counter);
  290. perf_evsel__close_fd(counter, cpus->nr, 1);
  291. }
  292. } else {
  293. list_for_each_entry(counter, &evsel_list, node) {
  294. read_counter_aggr(counter);
  295. perf_evsel__close_fd(counter, cpus->nr, threads->nr);
  296. }
  297. }
  298. return WEXITSTATUS(status);
  299. }
  300. static void print_noise(struct perf_evsel *evsel, double avg)
  301. {
  302. struct perf_stat *ps;
  303. if (run_count == 1)
  304. return;
  305. ps = evsel->priv;
  306. fprintf(stderr, " ( +- %7.3f%% )",
  307. 100 * stddev_stats(&ps->res_stats[0]) / avg);
  308. }
  309. static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
  310. {
  311. double msecs = avg / 1e6;
  312. char cpustr[16] = { '\0', };
  313. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
  314. if (no_aggr)
  315. sprintf(cpustr, "CPU%*d%s",
  316. csv_output ? 0 : -4,
  317. cpus->map[cpu], csv_sep);
  318. fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
  319. if (csv_output)
  320. return;
  321. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  322. fprintf(stderr, " # %10.3f CPUs ",
  323. avg / avg_stats(&walltime_nsecs_stats));
  324. }
  325. static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
  326. {
  327. double total, ratio = 0.0;
  328. char cpustr[16] = { '\0', };
  329. const char *fmt;
  330. if (csv_output)
  331. fmt = "%s%.0f%s%s";
  332. else if (big_num)
  333. fmt = "%s%'18.0f%s%-24s";
  334. else
  335. fmt = "%s%18.0f%s%-24s";
  336. if (no_aggr)
  337. sprintf(cpustr, "CPU%*d%s",
  338. csv_output ? 0 : -4,
  339. cpus->map[cpu], csv_sep);
  340. else
  341. cpu = 0;
  342. fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
  343. if (csv_output)
  344. return;
  345. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  346. total = avg_stats(&runtime_cycles_stats[cpu]);
  347. if (total)
  348. ratio = avg / total;
  349. fprintf(stderr, " # %10.3f IPC ", ratio);
  350. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  351. runtime_branches_stats[cpu].n != 0) {
  352. total = avg_stats(&runtime_branches_stats[cpu]);
  353. if (total)
  354. ratio = avg * 100 / total;
  355. fprintf(stderr, " # %10.3f %% ", ratio);
  356. } else if (runtime_nsecs_stats[cpu].n != 0) {
  357. total = avg_stats(&runtime_nsecs_stats[cpu]);
  358. if (total)
  359. ratio = 1000.0 * avg / total;
  360. fprintf(stderr, " # %10.3f M/sec", ratio);
  361. }
  362. }
  363. /*
  364. * Print out the results of a single counter:
  365. * aggregated counts in system-wide mode
  366. */
  367. static void print_counter_aggr(struct perf_evsel *counter)
  368. {
  369. struct perf_stat *ps = counter->priv;
  370. double avg = avg_stats(&ps->res_stats[0]);
  371. int scaled = counter->counts->scaled;
  372. if (scaled == -1) {
  373. fprintf(stderr, "%*s%s%-24s\n",
  374. csv_output ? 0 : 18,
  375. "<not counted>", csv_sep, event_name(counter));
  376. return;
  377. }
  378. if (nsec_counter(counter))
  379. nsec_printout(-1, counter, avg);
  380. else
  381. abs_printout(-1, counter, avg);
  382. if (csv_output) {
  383. fputc('\n', stderr);
  384. return;
  385. }
  386. print_noise(counter, avg);
  387. if (scaled) {
  388. double avg_enabled, avg_running;
  389. avg_enabled = avg_stats(&ps->res_stats[1]);
  390. avg_running = avg_stats(&ps->res_stats[2]);
  391. fprintf(stderr, " (scaled from %.2f%%)",
  392. 100 * avg_running / avg_enabled);
  393. }
  394. fprintf(stderr, "\n");
  395. }
  396. /*
  397. * Print out the results of a single counter:
  398. * does not use aggregated count in system-wide
  399. */
  400. static void print_counter(struct perf_evsel *counter)
  401. {
  402. u64 ena, run, val;
  403. int cpu;
  404. for (cpu = 0; cpu < cpus->nr; cpu++) {
  405. val = counter->counts->cpu[cpu].val;
  406. ena = counter->counts->cpu[cpu].ena;
  407. run = counter->counts->cpu[cpu].run;
  408. if (run == 0 || ena == 0) {
  409. fprintf(stderr, "CPU%*d%s%*s%s%-24s",
  410. csv_output ? 0 : -4,
  411. cpus->map[cpu], csv_sep,
  412. csv_output ? 0 : 18,
  413. "<not counted>", csv_sep,
  414. event_name(counter));
  415. fprintf(stderr, "\n");
  416. continue;
  417. }
  418. if (nsec_counter(counter))
  419. nsec_printout(cpu, counter, val);
  420. else
  421. abs_printout(cpu, counter, val);
  422. if (!csv_output) {
  423. print_noise(counter, 1.0);
  424. if (run != ena) {
  425. fprintf(stderr, " (scaled from %.2f%%)",
  426. 100.0 * run / ena);
  427. }
  428. }
  429. fprintf(stderr, "\n");
  430. }
  431. }
  432. static void print_stat(int argc, const char **argv)
  433. {
  434. struct perf_evsel *counter;
  435. int i;
  436. fflush(stdout);
  437. if (!csv_output) {
  438. fprintf(stderr, "\n");
  439. fprintf(stderr, " Performance counter stats for ");
  440. if(target_pid == -1 && target_tid == -1) {
  441. fprintf(stderr, "\'%s", argv[0]);
  442. for (i = 1; i < argc; i++)
  443. fprintf(stderr, " %s", argv[i]);
  444. } else if (target_pid != -1)
  445. fprintf(stderr, "process id \'%d", target_pid);
  446. else
  447. fprintf(stderr, "thread id \'%d", target_tid);
  448. fprintf(stderr, "\'");
  449. if (run_count > 1)
  450. fprintf(stderr, " (%d runs)", run_count);
  451. fprintf(stderr, ":\n\n");
  452. }
  453. if (no_aggr) {
  454. list_for_each_entry(counter, &evsel_list, node)
  455. print_counter(counter);
  456. } else {
  457. list_for_each_entry(counter, &evsel_list, node)
  458. print_counter_aggr(counter);
  459. }
  460. if (!csv_output) {
  461. fprintf(stderr, "\n");
  462. fprintf(stderr, " %18.9f seconds time elapsed",
  463. avg_stats(&walltime_nsecs_stats)/1e9);
  464. if (run_count > 1) {
  465. fprintf(stderr, " ( +- %7.3f%% )",
  466. 100*stddev_stats(&walltime_nsecs_stats) /
  467. avg_stats(&walltime_nsecs_stats));
  468. }
  469. fprintf(stderr, "\n\n");
  470. }
  471. }
  472. static volatile int signr = -1;
  473. static void skip_signal(int signo)
  474. {
  475. if(child_pid == -1)
  476. done = 1;
  477. signr = signo;
  478. }
  479. static void sig_atexit(void)
  480. {
  481. if (child_pid != -1)
  482. kill(child_pid, SIGTERM);
  483. if (signr == -1)
  484. return;
  485. signal(signr, SIG_DFL);
  486. kill(getpid(), signr);
  487. }
  488. static const char * const stat_usage[] = {
  489. "perf stat [<options>] [<command>]",
  490. NULL
  491. };
  492. static int stat__set_big_num(const struct option *opt __used,
  493. const char *s __used, int unset)
  494. {
  495. big_num_opt = unset ? 0 : 1;
  496. return 0;
  497. }
  498. static const struct option options[] = {
  499. OPT_CALLBACK('e', "event", NULL, "event",
  500. "event selector. use 'perf list' to list available events",
  501. parse_events),
  502. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  503. "child tasks do not inherit counters"),
  504. OPT_INTEGER('p', "pid", &target_pid,
  505. "stat events on existing process id"),
  506. OPT_INTEGER('t', "tid", &target_tid,
  507. "stat events on existing thread id"),
  508. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  509. "system-wide collection from all CPUs"),
  510. OPT_BOOLEAN('c', "scale", &scale,
  511. "scale/normalize counters"),
  512. OPT_INCR('v', "verbose", &verbose,
  513. "be more verbose (show counter open errors, etc)"),
  514. OPT_INTEGER('r', "repeat", &run_count,
  515. "repeat command and print average + stddev (max: 100)"),
  516. OPT_BOOLEAN('n', "null", &null_run,
  517. "null run - dont start any counters"),
  518. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  519. "print large numbers with thousands\' separators",
  520. stat__set_big_num),
  521. OPT_STRING('C', "cpu", &cpu_list, "cpu",
  522. "list of cpus to monitor in system-wide"),
  523. OPT_BOOLEAN('A', "no-aggr", &no_aggr,
  524. "disable CPU count aggregation"),
  525. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  526. "print counts with custom separator"),
  527. OPT_END()
  528. };
  529. int cmd_stat(int argc, const char **argv, const char *prefix __used)
  530. {
  531. struct perf_evsel *pos;
  532. int status = -ENOMEM;
  533. setlocale(LC_ALL, "");
  534. argc = parse_options(argc, argv, options, stat_usage,
  535. PARSE_OPT_STOP_AT_NON_OPTION);
  536. if (csv_sep)
  537. csv_output = true;
  538. else
  539. csv_sep = DEFAULT_SEPARATOR;
  540. /*
  541. * let the spreadsheet do the pretty-printing
  542. */
  543. if (csv_output) {
  544. /* User explicitely passed -B? */
  545. if (big_num_opt == 1) {
  546. fprintf(stderr, "-B option not supported with -x\n");
  547. usage_with_options(stat_usage, options);
  548. } else /* Nope, so disable big number formatting */
  549. big_num = false;
  550. } else if (big_num_opt == 0) /* User passed --no-big-num */
  551. big_num = false;
  552. if (!argc && target_pid == -1 && target_tid == -1)
  553. usage_with_options(stat_usage, options);
  554. if (run_count <= 0)
  555. usage_with_options(stat_usage, options);
  556. /* no_aggr is for system-wide only */
  557. if (no_aggr && !system_wide)
  558. usage_with_options(stat_usage, options);
  559. /* Set attrs and nr_counters if no event is selected and !null_run */
  560. if (!null_run && !nr_counters) {
  561. size_t c;
  562. nr_counters = ARRAY_SIZE(default_attrs);
  563. for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
  564. pos = perf_evsel__new(default_attrs[c].type,
  565. default_attrs[c].config,
  566. nr_counters);
  567. if (pos == NULL)
  568. goto out;
  569. list_add(&pos->node, &evsel_list);
  570. }
  571. }
  572. if (target_pid != -1)
  573. target_tid = target_pid;
  574. threads = thread_map__new(target_pid, target_tid);
  575. if (threads == NULL) {
  576. pr_err("Problems finding threads of monitor\n");
  577. usage_with_options(stat_usage, options);
  578. }
  579. if (system_wide)
  580. cpus = cpu_map__new(cpu_list);
  581. else
  582. cpus = cpu_map__dummy_new();
  583. if (cpus == NULL) {
  584. perror("failed to parse CPUs map");
  585. usage_with_options(stat_usage, options);
  586. return -1;
  587. }
  588. list_for_each_entry(pos, &evsel_list, node) {
  589. if (perf_evsel__alloc_stat_priv(pos) < 0 ||
  590. perf_evsel__alloc_counts(pos, cpus->nr) < 0 ||
  591. perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
  592. goto out_free_fd;
  593. }
  594. /*
  595. * We dont want to block the signals - that would cause
  596. * child tasks to inherit that and Ctrl-C would not work.
  597. * What we want is for Ctrl-C to work in the exec()-ed
  598. * task, but being ignored by perf stat itself:
  599. */
  600. atexit(sig_atexit);
  601. signal(SIGINT, skip_signal);
  602. signal(SIGALRM, skip_signal);
  603. signal(SIGABRT, skip_signal);
  604. status = 0;
  605. for (run_idx = 0; run_idx < run_count; run_idx++) {
  606. if (run_count != 1 && verbose)
  607. fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
  608. status = run_perf_stat(argc, argv);
  609. }
  610. if (status != -1)
  611. print_stat(argc, argv);
  612. out_free_fd:
  613. list_for_each_entry(pos, &evsel_list, node)
  614. perf_evsel__free_stat_priv(pos);
  615. out:
  616. thread_map__delete(threads);
  617. threads = NULL;
  618. return status;
  619. }