builtin-stat.c 19 KB

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