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