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