builtin-stat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 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 pid_t child_pid = -1;
  73. static bool null_run = false;
  74. static bool big_num = true;
  75. static int big_num_opt = -1;
  76. static const char *cpu_list;
  77. static const char *csv_sep = NULL;
  78. static bool csv_output = false;
  79. static volatile int done = 0;
  80. struct stats
  81. {
  82. double n, mean, M2;
  83. };
  84. struct perf_stat {
  85. struct stats res_stats[3];
  86. };
  87. static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  88. {
  89. evsel->priv = zalloc(sizeof(struct perf_stat));
  90. return evsel->priv == NULL ? -ENOMEM : 0;
  91. }
  92. static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  93. {
  94. free(evsel->priv);
  95. evsel->priv = NULL;
  96. }
  97. static void update_stats(struct stats *stats, u64 val)
  98. {
  99. double delta;
  100. stats->n++;
  101. delta = val - stats->mean;
  102. stats->mean += delta / stats->n;
  103. stats->M2 += delta*(val - stats->mean);
  104. }
  105. static double avg_stats(struct stats *stats)
  106. {
  107. return stats->mean;
  108. }
  109. /*
  110. * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  111. *
  112. * (\Sum n_i^2) - ((\Sum n_i)^2)/n
  113. * s^2 = -------------------------------
  114. * n - 1
  115. *
  116. * http://en.wikipedia.org/wiki/Stddev
  117. *
  118. * The std dev of the mean is related to the std dev by:
  119. *
  120. * s
  121. * s_mean = -------
  122. * sqrt(n)
  123. *
  124. */
  125. static double stddev_stats(struct stats *stats)
  126. {
  127. double variance = stats->M2 / (stats->n - 1);
  128. double variance_mean = variance / stats->n;
  129. return sqrt(variance_mean);
  130. }
  131. struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  132. struct stats runtime_cycles_stats[MAX_NR_CPUS];
  133. struct stats runtime_branches_stats[MAX_NR_CPUS];
  134. struct stats walltime_nsecs_stats;
  135. static int create_perf_stat_counter(struct perf_evsel *evsel)
  136. {
  137. struct perf_event_attr *attr = &evsel->attr;
  138. if (scale)
  139. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  140. PERF_FORMAT_TOTAL_TIME_RUNNING;
  141. if (system_wide)
  142. return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, false, false);
  143. attr->inherit = !no_inherit;
  144. if (target_pid == -1 && target_tid == -1) {
  145. attr->disabled = 1;
  146. attr->enable_on_exec = 1;
  147. }
  148. return perf_evsel__open_per_thread(evsel, evsel_list->threads, false, false);
  149. }
  150. /*
  151. * Does the counter have nsecs as a unit?
  152. */
  153. static inline int nsec_counter(struct perf_evsel *evsel)
  154. {
  155. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  156. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  157. return 1;
  158. return 0;
  159. }
  160. /*
  161. * Read out the results of a single counter:
  162. * aggregate counts across CPUs in system-wide mode
  163. */
  164. static int read_counter_aggr(struct perf_evsel *counter)
  165. {
  166. struct perf_stat *ps = counter->priv;
  167. u64 *count = counter->counts->aggr.values;
  168. int i;
  169. if (__perf_evsel__read(counter, evsel_list->cpus->nr,
  170. evsel_list->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 < evsel_list->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. evsel_list->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. if (perf_evlist__set_filters(evsel_list)) {
  280. error("failed to set filter with %d (%s)\n", errno,
  281. strerror(errno));
  282. return -1;
  283. }
  284. /*
  285. * Enable counters and exec the command:
  286. */
  287. t0 = rdclock();
  288. if (forks) {
  289. close(go_pipe[1]);
  290. wait(&status);
  291. } else {
  292. while(!done) sleep(1);
  293. }
  294. t1 = rdclock();
  295. update_stats(&walltime_nsecs_stats, t1 - t0);
  296. if (no_aggr) {
  297. list_for_each_entry(counter, &evsel_list->entries, node) {
  298. read_counter(counter);
  299. perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
  300. }
  301. } else {
  302. list_for_each_entry(counter, &evsel_list->entries, node) {
  303. read_counter_aggr(counter);
  304. perf_evsel__close_fd(counter, evsel_list->cpus->nr,
  305. evsel_list->threads->nr);
  306. }
  307. }
  308. return WEXITSTATUS(status);
  309. }
  310. static void print_noise(struct perf_evsel *evsel, double avg)
  311. {
  312. struct perf_stat *ps;
  313. if (run_count == 1)
  314. return;
  315. ps = evsel->priv;
  316. fprintf(stderr, " ( +- %7.3f%% )",
  317. 100 * stddev_stats(&ps->res_stats[0]) / avg);
  318. }
  319. static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
  320. {
  321. double msecs = avg / 1e6;
  322. char cpustr[16] = { '\0', };
  323. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
  324. if (no_aggr)
  325. sprintf(cpustr, "CPU%*d%s",
  326. csv_output ? 0 : -4,
  327. evsel_list->cpus->map[cpu], csv_sep);
  328. fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
  329. if (evsel->cgrp)
  330. fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
  331. if (csv_output)
  332. return;
  333. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  334. fprintf(stderr, " # %10.3f CPUs ",
  335. avg / avg_stats(&walltime_nsecs_stats));
  336. }
  337. static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
  338. {
  339. double total, ratio = 0.0;
  340. char cpustr[16] = { '\0', };
  341. const char *fmt;
  342. if (csv_output)
  343. fmt = "%s%.0f%s%s";
  344. else if (big_num)
  345. fmt = "%s%'18.0f%s%-24s";
  346. else
  347. fmt = "%s%18.0f%s%-24s";
  348. if (no_aggr)
  349. sprintf(cpustr, "CPU%*d%s",
  350. csv_output ? 0 : -4,
  351. evsel_list->cpus->map[cpu], csv_sep);
  352. else
  353. cpu = 0;
  354. fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
  355. if (evsel->cgrp)
  356. fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
  357. if (csv_output)
  358. return;
  359. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  360. total = avg_stats(&runtime_cycles_stats[cpu]);
  361. if (total)
  362. ratio = avg / total;
  363. fprintf(stderr, " # %10.3f IPC ", ratio);
  364. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  365. runtime_branches_stats[cpu].n != 0) {
  366. total = avg_stats(&runtime_branches_stats[cpu]);
  367. if (total)
  368. ratio = avg * 100 / total;
  369. fprintf(stderr, " # %10.3f %% ", ratio);
  370. } else if (runtime_nsecs_stats[cpu].n != 0) {
  371. total = avg_stats(&runtime_nsecs_stats[cpu]);
  372. if (total)
  373. ratio = 1000.0 * avg / total;
  374. fprintf(stderr, " # %10.3f M/sec", ratio);
  375. }
  376. }
  377. /*
  378. * Print out the results of a single counter:
  379. * aggregated counts in system-wide mode
  380. */
  381. static void print_counter_aggr(struct perf_evsel *counter)
  382. {
  383. struct perf_stat *ps = counter->priv;
  384. double avg = avg_stats(&ps->res_stats[0]);
  385. int scaled = counter->counts->scaled;
  386. if (scaled == -1) {
  387. fprintf(stderr, "%*s%s%*s",
  388. csv_output ? 0 : 18,
  389. "<not counted>",
  390. csv_sep,
  391. csv_output ? 0 : -24,
  392. event_name(counter));
  393. if (counter->cgrp)
  394. fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
  395. fputc('\n', stderr);
  396. return;
  397. }
  398. if (nsec_counter(counter))
  399. nsec_printout(-1, counter, avg);
  400. else
  401. abs_printout(-1, counter, avg);
  402. if (csv_output) {
  403. fputc('\n', stderr);
  404. return;
  405. }
  406. print_noise(counter, avg);
  407. if (scaled) {
  408. double avg_enabled, avg_running;
  409. avg_enabled = avg_stats(&ps->res_stats[1]);
  410. avg_running = avg_stats(&ps->res_stats[2]);
  411. fprintf(stderr, " (scaled from %.2f%%)",
  412. 100 * avg_running / avg_enabled);
  413. }
  414. fprintf(stderr, "\n");
  415. }
  416. /*
  417. * Print out the results of a single counter:
  418. * does not use aggregated count in system-wide
  419. */
  420. static void print_counter(struct perf_evsel *counter)
  421. {
  422. u64 ena, run, val;
  423. int cpu;
  424. for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
  425. val = counter->counts->cpu[cpu].val;
  426. ena = counter->counts->cpu[cpu].ena;
  427. run = counter->counts->cpu[cpu].run;
  428. if (run == 0 || ena == 0) {
  429. fprintf(stderr, "CPU%*d%s%*s%s%*s",
  430. csv_output ? 0 : -4,
  431. evsel_list->cpus->map[cpu], csv_sep,
  432. csv_output ? 0 : 18,
  433. "<not counted>", csv_sep,
  434. csv_output ? 0 : -24,
  435. event_name(counter));
  436. if (counter->cgrp)
  437. fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
  438. fputc('\n', stderr);
  439. continue;
  440. }
  441. if (nsec_counter(counter))
  442. nsec_printout(cpu, counter, val);
  443. else
  444. abs_printout(cpu, counter, val);
  445. if (!csv_output) {
  446. print_noise(counter, 1.0);
  447. if (run != ena) {
  448. fprintf(stderr, " (scaled from %.2f%%)",
  449. 100.0 * run / ena);
  450. }
  451. }
  452. fputc('\n', stderr);
  453. }
  454. }
  455. static void print_stat(int argc, const char **argv)
  456. {
  457. struct perf_evsel *counter;
  458. int i;
  459. fflush(stdout);
  460. if (!csv_output) {
  461. fprintf(stderr, "\n");
  462. fprintf(stderr, " Performance counter stats for ");
  463. if(target_pid == -1 && target_tid == -1) {
  464. fprintf(stderr, "\'%s", argv[0]);
  465. for (i = 1; i < argc; i++)
  466. fprintf(stderr, " %s", argv[i]);
  467. } else if (target_pid != -1)
  468. fprintf(stderr, "process id \'%d", target_pid);
  469. else
  470. fprintf(stderr, "thread id \'%d", target_tid);
  471. fprintf(stderr, "\'");
  472. if (run_count > 1)
  473. fprintf(stderr, " (%d runs)", run_count);
  474. fprintf(stderr, ":\n\n");
  475. }
  476. if (no_aggr) {
  477. list_for_each_entry(counter, &evsel_list->entries, node)
  478. print_counter(counter);
  479. } else {
  480. list_for_each_entry(counter, &evsel_list->entries, node)
  481. print_counter_aggr(counter);
  482. }
  483. if (!csv_output) {
  484. fprintf(stderr, "\n");
  485. fprintf(stderr, " %18.9f seconds time elapsed",
  486. avg_stats(&walltime_nsecs_stats)/1e9);
  487. if (run_count > 1) {
  488. fprintf(stderr, " ( +- %7.3f%% )",
  489. 100*stddev_stats(&walltime_nsecs_stats) /
  490. avg_stats(&walltime_nsecs_stats));
  491. }
  492. fprintf(stderr, "\n\n");
  493. }
  494. }
  495. static volatile int signr = -1;
  496. static void skip_signal(int signo)
  497. {
  498. if(child_pid == -1)
  499. done = 1;
  500. signr = signo;
  501. }
  502. static void sig_atexit(void)
  503. {
  504. if (child_pid != -1)
  505. kill(child_pid, SIGTERM);
  506. if (signr == -1)
  507. return;
  508. signal(signr, SIG_DFL);
  509. kill(getpid(), signr);
  510. }
  511. static const char * const stat_usage[] = {
  512. "perf stat [<options>] [<command>]",
  513. NULL
  514. };
  515. static int stat__set_big_num(const struct option *opt __used,
  516. const char *s __used, int unset)
  517. {
  518. big_num_opt = unset ? 0 : 1;
  519. return 0;
  520. }
  521. static const struct option options[] = {
  522. OPT_CALLBACK('e', "event", &evsel_list, "event",
  523. "event selector. use 'perf list' to list available events",
  524. parse_events),
  525. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  526. "event filter", parse_filter),
  527. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  528. "child tasks do not inherit counters"),
  529. OPT_INTEGER('p', "pid", &target_pid,
  530. "stat events on existing process id"),
  531. OPT_INTEGER('t', "tid", &target_tid,
  532. "stat events on existing thread id"),
  533. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  534. "system-wide collection from all CPUs"),
  535. OPT_BOOLEAN('c', "scale", &scale,
  536. "scale/normalize counters"),
  537. OPT_INCR('v', "verbose", &verbose,
  538. "be more verbose (show counter open errors, etc)"),
  539. OPT_INTEGER('r', "repeat", &run_count,
  540. "repeat command and print average + stddev (max: 100)"),
  541. OPT_BOOLEAN('n', "null", &null_run,
  542. "null run - dont start any counters"),
  543. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  544. "print large numbers with thousands\' separators",
  545. stat__set_big_num),
  546. OPT_STRING('C', "cpu", &cpu_list, "cpu",
  547. "list of cpus to monitor in system-wide"),
  548. OPT_BOOLEAN('A', "no-aggr", &no_aggr,
  549. "disable CPU count aggregation"),
  550. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  551. "print counts with custom separator"),
  552. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  553. "monitor event in cgroup name only",
  554. parse_cgroups),
  555. OPT_END()
  556. };
  557. int cmd_stat(int argc, const char **argv, const char *prefix __used)
  558. {
  559. struct perf_evsel *pos;
  560. int status = -ENOMEM;
  561. setlocale(LC_ALL, "");
  562. evsel_list = perf_evlist__new(NULL, NULL);
  563. if (evsel_list == NULL)
  564. return -ENOMEM;
  565. argc = parse_options(argc, argv, options, stat_usage,
  566. PARSE_OPT_STOP_AT_NON_OPTION);
  567. if (csv_sep)
  568. csv_output = true;
  569. else
  570. csv_sep = DEFAULT_SEPARATOR;
  571. /*
  572. * let the spreadsheet do the pretty-printing
  573. */
  574. if (csv_output) {
  575. /* User explicitely passed -B? */
  576. if (big_num_opt == 1) {
  577. fprintf(stderr, "-B option not supported with -x\n");
  578. usage_with_options(stat_usage, options);
  579. } else /* Nope, so disable big number formatting */
  580. big_num = false;
  581. } else if (big_num_opt == 0) /* User passed --no-big-num */
  582. big_num = false;
  583. if (!argc && target_pid == -1 && target_tid == -1)
  584. usage_with_options(stat_usage, options);
  585. if (run_count <= 0)
  586. usage_with_options(stat_usage, options);
  587. /* no_aggr, cgroup are for system-wide only */
  588. if ((no_aggr || nr_cgroups) && !system_wide) {
  589. fprintf(stderr, "both cgroup and no-aggregation "
  590. "modes only available in system-wide mode\n");
  591. usage_with_options(stat_usage, options);
  592. }
  593. /* Set attrs and nr_counters if no event is selected and !null_run */
  594. if (!null_run && !evsel_list->nr_entries) {
  595. size_t c;
  596. for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
  597. pos = perf_evsel__new(&default_attrs[c], c);
  598. if (pos == NULL)
  599. goto out;
  600. perf_evlist__add(evsel_list, pos);
  601. }
  602. }
  603. if (target_pid != -1)
  604. target_tid = target_pid;
  605. evsel_list->threads = thread_map__new(target_pid, target_tid);
  606. if (evsel_list->threads == NULL) {
  607. pr_err("Problems finding threads of monitor\n");
  608. usage_with_options(stat_usage, options);
  609. }
  610. if (system_wide)
  611. evsel_list->cpus = cpu_map__new(cpu_list);
  612. else
  613. evsel_list->cpus = cpu_map__dummy_new();
  614. if (evsel_list->cpus == NULL) {
  615. perror("failed to parse CPUs map");
  616. usage_with_options(stat_usage, options);
  617. return -1;
  618. }
  619. list_for_each_entry(pos, &evsel_list->entries, node) {
  620. if (perf_evsel__alloc_stat_priv(pos) < 0 ||
  621. perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0 ||
  622. perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, evsel_list->threads->nr) < 0)
  623. goto out_free_fd;
  624. }
  625. /*
  626. * We dont want to block the signals - that would cause
  627. * child tasks to inherit that and Ctrl-C would not work.
  628. * What we want is for Ctrl-C to work in the exec()-ed
  629. * task, but being ignored by perf stat itself:
  630. */
  631. atexit(sig_atexit);
  632. signal(SIGINT, skip_signal);
  633. signal(SIGALRM, skip_signal);
  634. signal(SIGABRT, skip_signal);
  635. status = 0;
  636. for (run_idx = 0; run_idx < run_count; run_idx++) {
  637. if (run_count != 1 && verbose)
  638. fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
  639. status = run_perf_stat(argc, argv);
  640. }
  641. if (status != -1)
  642. print_stat(argc, argv);
  643. out_free_fd:
  644. list_for_each_entry(pos, &evsel_list->entries, node)
  645. perf_evsel__free_stat_priv(pos);
  646. perf_evlist__delete_maps(evsel_list);
  647. out:
  648. perf_evlist__delete(evsel_list);
  649. return status;
  650. }