builtin-stat.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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.118
  10. Performance counter stats for './hackbench 10':
  11. 1708.761321 task-clock # 11.037 CPUs utilized
  12. 41,190 context-switches # 0.024 M/sec
  13. 6,735 CPU-migrations # 0.004 M/sec
  14. 17,318 page-faults # 0.010 M/sec
  15. 5,205,202,243 cycles # 3.046 GHz
  16. 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
  17. 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
  18. 2,603,501,247 instructions # 0.50 insns per cycle
  19. # 1.48 stalled cycles per insn
  20. 484,357,498 branches # 283.455 M/sec
  21. 6,388,934 branch-misses # 1.32% of all branches
  22. 0.154822978 seconds time elapsed
  23. *
  24. * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  25. *
  26. * Improvements and fixes by:
  27. *
  28. * Arjan van de Ven <arjan@linux.intel.com>
  29. * Yanmin Zhang <yanmin.zhang@intel.com>
  30. * Wu Fengguang <fengguang.wu@intel.com>
  31. * Mike Galbraith <efault@gmx.de>
  32. * Paul Mackerras <paulus@samba.org>
  33. * Jaswinder Singh Rajput <jaswinder@kernel.org>
  34. *
  35. * Released under the GPL v2. (and only v2, not any later version)
  36. */
  37. #include "perf.h"
  38. #include "builtin.h"
  39. #include "util/util.h"
  40. #include "util/parse-options.h"
  41. #include "util/parse-events.h"
  42. #include "util/event.h"
  43. #include "util/evlist.h"
  44. #include "util/evsel.h"
  45. #include "util/debug.h"
  46. #include "util/color.h"
  47. #include "util/stat.h"
  48. #include "util/header.h"
  49. #include "util/cpumap.h"
  50. #include "util/thread.h"
  51. #include "util/thread_map.h"
  52. #include <stdlib.h>
  53. #include <sys/prctl.h>
  54. #include <locale.h>
  55. #define DEFAULT_SEPARATOR " "
  56. #define CNTR_NOT_SUPPORTED "<not supported>"
  57. #define CNTR_NOT_COUNTED "<not counted>"
  58. static struct perf_evlist *evsel_list;
  59. static struct perf_target target = {
  60. .uid = UINT_MAX,
  61. };
  62. static int run_count = 1;
  63. static bool no_inherit = false;
  64. static bool scale = true;
  65. static bool no_aggr = false;
  66. static pid_t child_pid = -1;
  67. static bool null_run = false;
  68. static int detailed_run = 0;
  69. static bool big_num = true;
  70. static int big_num_opt = -1;
  71. static const char *csv_sep = NULL;
  72. static bool csv_output = false;
  73. static bool group = false;
  74. static FILE *output = NULL;
  75. static const char *pre_cmd = NULL;
  76. static const char *post_cmd = NULL;
  77. static bool sync_run = false;
  78. static volatile int done = 0;
  79. struct perf_stat {
  80. struct stats res_stats[3];
  81. };
  82. static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  83. {
  84. evsel->priv = zalloc(sizeof(struct perf_stat));
  85. return evsel->priv == NULL ? -ENOMEM : 0;
  86. }
  87. static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  88. {
  89. free(evsel->priv);
  90. evsel->priv = NULL;
  91. }
  92. static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
  93. {
  94. return (evsel->cpus && !target.cpu_list) ? evsel->cpus : evsel_list->cpus;
  95. }
  96. static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
  97. {
  98. return perf_evsel__cpus(evsel)->nr;
  99. }
  100. static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  101. static struct stats runtime_cycles_stats[MAX_NR_CPUS];
  102. static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
  103. static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
  104. static struct stats runtime_branches_stats[MAX_NR_CPUS];
  105. static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
  106. static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
  107. static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
  108. static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
  109. static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
  110. static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
  111. static struct stats walltime_nsecs_stats;
  112. static int create_perf_stat_counter(struct perf_evsel *evsel)
  113. {
  114. struct perf_event_attr *attr = &evsel->attr;
  115. if (scale)
  116. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  117. PERF_FORMAT_TOTAL_TIME_RUNNING;
  118. attr->inherit = !no_inherit;
  119. if (perf_target__has_cpu(&target))
  120. return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
  121. if (!perf_target__has_task(&target) &&
  122. perf_evsel__is_group_leader(evsel)) {
  123. attr->disabled = 1;
  124. attr->enable_on_exec = 1;
  125. }
  126. return perf_evsel__open_per_thread(evsel, evsel_list->threads);
  127. }
  128. /*
  129. * Does the counter have nsecs as a unit?
  130. */
  131. static inline int nsec_counter(struct perf_evsel *evsel)
  132. {
  133. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  134. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  135. return 1;
  136. return 0;
  137. }
  138. /*
  139. * Update various tracking values we maintain to print
  140. * more semantic information such as miss/hit ratios,
  141. * instruction rates, etc:
  142. */
  143. static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
  144. {
  145. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
  146. update_stats(&runtime_nsecs_stats[0], count[0]);
  147. else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
  148. update_stats(&runtime_cycles_stats[0], count[0]);
  149. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
  150. update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
  151. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
  152. update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
  153. else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
  154. update_stats(&runtime_branches_stats[0], count[0]);
  155. else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
  156. update_stats(&runtime_cacherefs_stats[0], count[0]);
  157. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
  158. update_stats(&runtime_l1_dcache_stats[0], count[0]);
  159. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
  160. update_stats(&runtime_l1_icache_stats[0], count[0]);
  161. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
  162. update_stats(&runtime_ll_cache_stats[0], count[0]);
  163. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
  164. update_stats(&runtime_dtlb_cache_stats[0], count[0]);
  165. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
  166. update_stats(&runtime_itlb_cache_stats[0], count[0]);
  167. }
  168. /*
  169. * Read out the results of a single counter:
  170. * aggregate counts across CPUs in system-wide mode
  171. */
  172. static int read_counter_aggr(struct perf_evsel *counter)
  173. {
  174. struct perf_stat *ps = counter->priv;
  175. u64 *count = counter->counts->aggr.values;
  176. int i;
  177. if (__perf_evsel__read(counter, perf_evsel__nr_cpus(counter),
  178. evsel_list->threads->nr, scale) < 0)
  179. return -1;
  180. for (i = 0; i < 3; i++)
  181. update_stats(&ps->res_stats[i], count[i]);
  182. if (verbose) {
  183. fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  184. perf_evsel__name(counter), count[0], count[1], count[2]);
  185. }
  186. /*
  187. * Save the full runtime - to allow normalization during printout:
  188. */
  189. update_shadow_stats(counter, count);
  190. return 0;
  191. }
  192. /*
  193. * Read out the results of a single counter:
  194. * do not aggregate counts across CPUs in system-wide mode
  195. */
  196. static int read_counter(struct perf_evsel *counter)
  197. {
  198. u64 *count;
  199. int cpu;
  200. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  201. if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
  202. return -1;
  203. count = counter->counts->cpu[cpu].values;
  204. update_shadow_stats(counter, count);
  205. }
  206. return 0;
  207. }
  208. static int __run_perf_stat(int argc __maybe_unused, const char **argv)
  209. {
  210. unsigned long long t0, t1;
  211. struct perf_evsel *counter;
  212. int status = 0;
  213. int child_ready_pipe[2], go_pipe[2];
  214. const bool forks = (argc > 0);
  215. char buf;
  216. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  217. perror("failed to create pipes");
  218. return -1;
  219. }
  220. if (forks) {
  221. if ((child_pid = fork()) < 0)
  222. perror("failed to fork");
  223. if (!child_pid) {
  224. close(child_ready_pipe[0]);
  225. close(go_pipe[1]);
  226. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  227. /*
  228. * Do a dummy execvp to get the PLT entry resolved,
  229. * so we avoid the resolver overhead on the real
  230. * execvp call.
  231. */
  232. execvp("", (char **)argv);
  233. /*
  234. * Tell the parent we're ready to go
  235. */
  236. close(child_ready_pipe[1]);
  237. /*
  238. * Wait until the parent tells us to go.
  239. */
  240. if (read(go_pipe[0], &buf, 1) == -1)
  241. perror("unable to read pipe");
  242. execvp(argv[0], (char **)argv);
  243. perror(argv[0]);
  244. exit(-1);
  245. }
  246. if (perf_target__none(&target))
  247. evsel_list->threads->map[0] = child_pid;
  248. /*
  249. * Wait for the child to be ready to exec.
  250. */
  251. close(child_ready_pipe[1]);
  252. close(go_pipe[0]);
  253. if (read(child_ready_pipe[0], &buf, 1) == -1)
  254. perror("unable to read pipe");
  255. close(child_ready_pipe[0]);
  256. }
  257. if (group)
  258. perf_evlist__set_leader(evsel_list);
  259. list_for_each_entry(counter, &evsel_list->entries, node) {
  260. if (create_perf_stat_counter(counter) < 0) {
  261. /*
  262. * PPC returns ENXIO for HW counters until 2.6.37
  263. * (behavior changed with commit b0a873e).
  264. */
  265. if (errno == EINVAL || errno == ENOSYS ||
  266. errno == ENOENT || errno == EOPNOTSUPP ||
  267. errno == ENXIO) {
  268. if (verbose)
  269. ui__warning("%s event is not supported by the kernel.\n",
  270. perf_evsel__name(counter));
  271. counter->supported = false;
  272. continue;
  273. }
  274. if (errno == EPERM || errno == EACCES) {
  275. error("You may not have permission to collect %sstats.\n"
  276. "\t Consider tweaking"
  277. " /proc/sys/kernel/perf_event_paranoid or running as root.",
  278. target.system_wide ? "system-wide " : "");
  279. } else {
  280. error("open_counter returned with %d (%s). "
  281. "/bin/dmesg may provide additional information.\n",
  282. errno, strerror(errno));
  283. }
  284. if (child_pid != -1)
  285. kill(child_pid, SIGTERM);
  286. pr_err("Not all events could be opened.\n");
  287. return -1;
  288. }
  289. counter->supported = true;
  290. }
  291. if (perf_evlist__apply_filters(evsel_list)) {
  292. error("failed to set filter with %d (%s)\n", errno,
  293. strerror(errno));
  294. return -1;
  295. }
  296. /*
  297. * Enable counters and exec the command:
  298. */
  299. t0 = rdclock();
  300. if (forks) {
  301. close(go_pipe[1]);
  302. wait(&status);
  303. if (WIFSIGNALED(status))
  304. psignal(WTERMSIG(status), argv[0]);
  305. } else {
  306. while(!done) sleep(1);
  307. }
  308. t1 = rdclock();
  309. update_stats(&walltime_nsecs_stats, t1 - t0);
  310. if (no_aggr) {
  311. list_for_each_entry(counter, &evsel_list->entries, node) {
  312. read_counter(counter);
  313. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
  314. }
  315. } else {
  316. list_for_each_entry(counter, &evsel_list->entries, node) {
  317. read_counter_aggr(counter);
  318. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
  319. evsel_list->threads->nr);
  320. }
  321. }
  322. return WEXITSTATUS(status);
  323. }
  324. static int run_perf_stat(int argc __maybe_unused, const char **argv)
  325. {
  326. int ret;
  327. if (pre_cmd) {
  328. ret = system(pre_cmd);
  329. if (ret)
  330. return ret;
  331. }
  332. if (sync_run)
  333. sync();
  334. ret = __run_perf_stat(argc, argv);
  335. if (ret)
  336. return ret;
  337. if (post_cmd) {
  338. ret = system(post_cmd);
  339. if (ret)
  340. return ret;
  341. }
  342. return ret;
  343. }
  344. static void print_noise_pct(double total, double avg)
  345. {
  346. double pct = rel_stddev_stats(total, avg);
  347. if (csv_output)
  348. fprintf(output, "%s%.2f%%", csv_sep, pct);
  349. else if (pct)
  350. fprintf(output, " ( +-%6.2f%% )", pct);
  351. }
  352. static void print_noise(struct perf_evsel *evsel, double avg)
  353. {
  354. struct perf_stat *ps;
  355. if (run_count == 1)
  356. return;
  357. ps = evsel->priv;
  358. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  359. }
  360. static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
  361. {
  362. double msecs = avg / 1e6;
  363. char cpustr[16] = { '\0', };
  364. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
  365. if (no_aggr)
  366. sprintf(cpustr, "CPU%*d%s",
  367. csv_output ? 0 : -4,
  368. perf_evsel__cpus(evsel)->map[cpu], csv_sep);
  369. fprintf(output, fmt, cpustr, msecs, csv_sep, perf_evsel__name(evsel));
  370. if (evsel->cgrp)
  371. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  372. if (csv_output)
  373. return;
  374. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  375. fprintf(output, " # %8.3f CPUs utilized ",
  376. avg / avg_stats(&walltime_nsecs_stats));
  377. else
  378. fprintf(output, " ");
  379. }
  380. /* used for get_ratio_color() */
  381. enum grc_type {
  382. GRC_STALLED_CYCLES_FE,
  383. GRC_STALLED_CYCLES_BE,
  384. GRC_CACHE_MISSES,
  385. GRC_MAX_NR
  386. };
  387. static const char *get_ratio_color(enum grc_type type, double ratio)
  388. {
  389. static const double grc_table[GRC_MAX_NR][3] = {
  390. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  391. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  392. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  393. };
  394. const char *color = PERF_COLOR_NORMAL;
  395. if (ratio > grc_table[type][0])
  396. color = PERF_COLOR_RED;
  397. else if (ratio > grc_table[type][1])
  398. color = PERF_COLOR_MAGENTA;
  399. else if (ratio > grc_table[type][2])
  400. color = PERF_COLOR_YELLOW;
  401. return color;
  402. }
  403. static void print_stalled_cycles_frontend(int cpu,
  404. struct perf_evsel *evsel
  405. __maybe_unused, double avg)
  406. {
  407. double total, ratio = 0.0;
  408. const char *color;
  409. total = avg_stats(&runtime_cycles_stats[cpu]);
  410. if (total)
  411. ratio = avg / total * 100.0;
  412. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  413. fprintf(output, " # ");
  414. color_fprintf(output, color, "%6.2f%%", ratio);
  415. fprintf(output, " frontend cycles idle ");
  416. }
  417. static void print_stalled_cycles_backend(int cpu,
  418. struct perf_evsel *evsel
  419. __maybe_unused, double avg)
  420. {
  421. double total, ratio = 0.0;
  422. const char *color;
  423. total = avg_stats(&runtime_cycles_stats[cpu]);
  424. if (total)
  425. ratio = avg / total * 100.0;
  426. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  427. fprintf(output, " # ");
  428. color_fprintf(output, color, "%6.2f%%", ratio);
  429. fprintf(output, " backend cycles idle ");
  430. }
  431. static void print_branch_misses(int cpu,
  432. struct perf_evsel *evsel __maybe_unused,
  433. double avg)
  434. {
  435. double total, ratio = 0.0;
  436. const char *color;
  437. total = avg_stats(&runtime_branches_stats[cpu]);
  438. if (total)
  439. ratio = avg / total * 100.0;
  440. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  441. fprintf(output, " # ");
  442. color_fprintf(output, color, "%6.2f%%", ratio);
  443. fprintf(output, " of all branches ");
  444. }
  445. static void print_l1_dcache_misses(int cpu,
  446. struct perf_evsel *evsel __maybe_unused,
  447. double avg)
  448. {
  449. double total, ratio = 0.0;
  450. const char *color;
  451. total = avg_stats(&runtime_l1_dcache_stats[cpu]);
  452. if (total)
  453. ratio = avg / total * 100.0;
  454. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  455. fprintf(output, " # ");
  456. color_fprintf(output, color, "%6.2f%%", ratio);
  457. fprintf(output, " of all L1-dcache hits ");
  458. }
  459. static void print_l1_icache_misses(int cpu,
  460. struct perf_evsel *evsel __maybe_unused,
  461. double avg)
  462. {
  463. double total, ratio = 0.0;
  464. const char *color;
  465. total = avg_stats(&runtime_l1_icache_stats[cpu]);
  466. if (total)
  467. ratio = avg / total * 100.0;
  468. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  469. fprintf(output, " # ");
  470. color_fprintf(output, color, "%6.2f%%", ratio);
  471. fprintf(output, " of all L1-icache hits ");
  472. }
  473. static void print_dtlb_cache_misses(int cpu,
  474. struct perf_evsel *evsel __maybe_unused,
  475. double avg)
  476. {
  477. double total, ratio = 0.0;
  478. const char *color;
  479. total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
  480. if (total)
  481. ratio = avg / total * 100.0;
  482. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  483. fprintf(output, " # ");
  484. color_fprintf(output, color, "%6.2f%%", ratio);
  485. fprintf(output, " of all dTLB cache hits ");
  486. }
  487. static void print_itlb_cache_misses(int cpu,
  488. struct perf_evsel *evsel __maybe_unused,
  489. double avg)
  490. {
  491. double total, ratio = 0.0;
  492. const char *color;
  493. total = avg_stats(&runtime_itlb_cache_stats[cpu]);
  494. if (total)
  495. ratio = avg / total * 100.0;
  496. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  497. fprintf(output, " # ");
  498. color_fprintf(output, color, "%6.2f%%", ratio);
  499. fprintf(output, " of all iTLB cache hits ");
  500. }
  501. static void print_ll_cache_misses(int cpu,
  502. struct perf_evsel *evsel __maybe_unused,
  503. double avg)
  504. {
  505. double total, ratio = 0.0;
  506. const char *color;
  507. total = avg_stats(&runtime_ll_cache_stats[cpu]);
  508. if (total)
  509. ratio = avg / total * 100.0;
  510. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  511. fprintf(output, " # ");
  512. color_fprintf(output, color, "%6.2f%%", ratio);
  513. fprintf(output, " of all LL-cache hits ");
  514. }
  515. static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
  516. {
  517. double total, ratio = 0.0;
  518. char cpustr[16] = { '\0', };
  519. const char *fmt;
  520. if (csv_output)
  521. fmt = "%s%.0f%s%s";
  522. else if (big_num)
  523. fmt = "%s%'18.0f%s%-25s";
  524. else
  525. fmt = "%s%18.0f%s%-25s";
  526. if (no_aggr)
  527. sprintf(cpustr, "CPU%*d%s",
  528. csv_output ? 0 : -4,
  529. perf_evsel__cpus(evsel)->map[cpu], csv_sep);
  530. else
  531. cpu = 0;
  532. fprintf(output, fmt, cpustr, avg, csv_sep, perf_evsel__name(evsel));
  533. if (evsel->cgrp)
  534. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  535. if (csv_output)
  536. return;
  537. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  538. total = avg_stats(&runtime_cycles_stats[cpu]);
  539. if (total)
  540. ratio = avg / total;
  541. fprintf(output, " # %5.2f insns per cycle ", ratio);
  542. total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
  543. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
  544. if (total && avg) {
  545. ratio = total / avg;
  546. fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
  547. }
  548. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  549. runtime_branches_stats[cpu].n != 0) {
  550. print_branch_misses(cpu, evsel, avg);
  551. } else if (
  552. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  553. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  554. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  555. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  556. runtime_l1_dcache_stats[cpu].n != 0) {
  557. print_l1_dcache_misses(cpu, evsel, avg);
  558. } else if (
  559. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  560. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  561. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  562. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  563. runtime_l1_icache_stats[cpu].n != 0) {
  564. print_l1_icache_misses(cpu, evsel, avg);
  565. } else if (
  566. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  567. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  568. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  569. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  570. runtime_dtlb_cache_stats[cpu].n != 0) {
  571. print_dtlb_cache_misses(cpu, evsel, avg);
  572. } else if (
  573. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  574. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  575. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  576. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  577. runtime_itlb_cache_stats[cpu].n != 0) {
  578. print_itlb_cache_misses(cpu, evsel, avg);
  579. } else if (
  580. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  581. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  582. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  583. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  584. runtime_ll_cache_stats[cpu].n != 0) {
  585. print_ll_cache_misses(cpu, evsel, avg);
  586. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
  587. runtime_cacherefs_stats[cpu].n != 0) {
  588. total = avg_stats(&runtime_cacherefs_stats[cpu]);
  589. if (total)
  590. ratio = avg * 100 / total;
  591. fprintf(output, " # %8.3f %% of all cache refs ", ratio);
  592. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  593. print_stalled_cycles_frontend(cpu, evsel, avg);
  594. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  595. print_stalled_cycles_backend(cpu, evsel, avg);
  596. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  597. total = avg_stats(&runtime_nsecs_stats[cpu]);
  598. if (total)
  599. ratio = 1.0 * avg / total;
  600. fprintf(output, " # %8.3f GHz ", ratio);
  601. } else if (runtime_nsecs_stats[cpu].n != 0) {
  602. char unit = 'M';
  603. total = avg_stats(&runtime_nsecs_stats[cpu]);
  604. if (total)
  605. ratio = 1000.0 * avg / total;
  606. if (ratio < 0.001) {
  607. ratio *= 1000;
  608. unit = 'K';
  609. }
  610. fprintf(output, " # %8.3f %c/sec ", ratio, unit);
  611. } else {
  612. fprintf(output, " ");
  613. }
  614. }
  615. /*
  616. * Print out the results of a single counter:
  617. * aggregated counts in system-wide mode
  618. */
  619. static void print_counter_aggr(struct perf_evsel *counter)
  620. {
  621. struct perf_stat *ps = counter->priv;
  622. double avg = avg_stats(&ps->res_stats[0]);
  623. int scaled = counter->counts->scaled;
  624. if (scaled == -1) {
  625. fprintf(output, "%*s%s%*s",
  626. csv_output ? 0 : 18,
  627. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  628. csv_sep,
  629. csv_output ? 0 : -24,
  630. perf_evsel__name(counter));
  631. if (counter->cgrp)
  632. fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
  633. fputc('\n', output);
  634. return;
  635. }
  636. if (nsec_counter(counter))
  637. nsec_printout(-1, counter, avg);
  638. else
  639. abs_printout(-1, counter, avg);
  640. print_noise(counter, avg);
  641. if (csv_output) {
  642. fputc('\n', output);
  643. return;
  644. }
  645. if (scaled) {
  646. double avg_enabled, avg_running;
  647. avg_enabled = avg_stats(&ps->res_stats[1]);
  648. avg_running = avg_stats(&ps->res_stats[2]);
  649. fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
  650. }
  651. fprintf(output, "\n");
  652. }
  653. /*
  654. * Print out the results of a single counter:
  655. * does not use aggregated count in system-wide
  656. */
  657. static void print_counter(struct perf_evsel *counter)
  658. {
  659. u64 ena, run, val;
  660. int cpu;
  661. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  662. val = counter->counts->cpu[cpu].val;
  663. ena = counter->counts->cpu[cpu].ena;
  664. run = counter->counts->cpu[cpu].run;
  665. if (run == 0 || ena == 0) {
  666. fprintf(output, "CPU%*d%s%*s%s%*s",
  667. csv_output ? 0 : -4,
  668. perf_evsel__cpus(counter)->map[cpu], csv_sep,
  669. csv_output ? 0 : 18,
  670. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  671. csv_sep,
  672. csv_output ? 0 : -24,
  673. perf_evsel__name(counter));
  674. if (counter->cgrp)
  675. fprintf(output, "%s%s",
  676. csv_sep, counter->cgrp->name);
  677. fputc('\n', output);
  678. continue;
  679. }
  680. if (nsec_counter(counter))
  681. nsec_printout(cpu, counter, val);
  682. else
  683. abs_printout(cpu, counter, val);
  684. if (!csv_output) {
  685. print_noise(counter, 1.0);
  686. if (run != ena)
  687. fprintf(output, " (%.2f%%)",
  688. 100.0 * run / ena);
  689. }
  690. fputc('\n', output);
  691. }
  692. }
  693. static void print_stat(int argc, const char **argv)
  694. {
  695. struct perf_evsel *counter;
  696. int i;
  697. fflush(stdout);
  698. if (!csv_output) {
  699. fprintf(output, "\n");
  700. fprintf(output, " Performance counter stats for ");
  701. if (!perf_target__has_task(&target)) {
  702. fprintf(output, "\'%s", argv[0]);
  703. for (i = 1; i < argc; i++)
  704. fprintf(output, " %s", argv[i]);
  705. } else if (target.pid)
  706. fprintf(output, "process id \'%s", target.pid);
  707. else
  708. fprintf(output, "thread id \'%s", target.tid);
  709. fprintf(output, "\'");
  710. if (run_count > 1)
  711. fprintf(output, " (%d runs)", run_count);
  712. fprintf(output, ":\n\n");
  713. }
  714. if (no_aggr) {
  715. list_for_each_entry(counter, &evsel_list->entries, node)
  716. print_counter(counter);
  717. } else {
  718. list_for_each_entry(counter, &evsel_list->entries, node)
  719. print_counter_aggr(counter);
  720. }
  721. if (!csv_output) {
  722. if (!null_run)
  723. fprintf(output, "\n");
  724. fprintf(output, " %17.9f seconds time elapsed",
  725. avg_stats(&walltime_nsecs_stats)/1e9);
  726. if (run_count > 1) {
  727. fprintf(output, " ");
  728. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  729. avg_stats(&walltime_nsecs_stats));
  730. }
  731. fprintf(output, "\n\n");
  732. }
  733. }
  734. static volatile int signr = -1;
  735. static void skip_signal(int signo)
  736. {
  737. if(child_pid == -1)
  738. done = 1;
  739. signr = signo;
  740. }
  741. static void sig_atexit(void)
  742. {
  743. if (child_pid != -1)
  744. kill(child_pid, SIGTERM);
  745. if (signr == -1)
  746. return;
  747. signal(signr, SIG_DFL);
  748. kill(getpid(), signr);
  749. }
  750. static int stat__set_big_num(const struct option *opt __maybe_unused,
  751. const char *s __maybe_unused, int unset)
  752. {
  753. big_num_opt = unset ? 0 : 1;
  754. return 0;
  755. }
  756. /*
  757. * Add default attributes, if there were no attributes specified or
  758. * if -d/--detailed, -d -d or -d -d -d is used:
  759. */
  760. static int add_default_attributes(void)
  761. {
  762. struct perf_event_attr default_attrs[] = {
  763. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  764. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  765. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  766. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  767. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  768. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  769. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  770. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  771. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  772. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  773. };
  774. /*
  775. * Detailed stats (-d), covering the L1 and last level data caches:
  776. */
  777. struct perf_event_attr detailed_attrs[] = {
  778. { .type = PERF_TYPE_HW_CACHE,
  779. .config =
  780. PERF_COUNT_HW_CACHE_L1D << 0 |
  781. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  782. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  783. { .type = PERF_TYPE_HW_CACHE,
  784. .config =
  785. PERF_COUNT_HW_CACHE_L1D << 0 |
  786. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  787. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  788. { .type = PERF_TYPE_HW_CACHE,
  789. .config =
  790. PERF_COUNT_HW_CACHE_LL << 0 |
  791. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  792. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  793. { .type = PERF_TYPE_HW_CACHE,
  794. .config =
  795. PERF_COUNT_HW_CACHE_LL << 0 |
  796. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  797. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  798. };
  799. /*
  800. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  801. */
  802. struct perf_event_attr very_detailed_attrs[] = {
  803. { .type = PERF_TYPE_HW_CACHE,
  804. .config =
  805. PERF_COUNT_HW_CACHE_L1I << 0 |
  806. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  807. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  808. { .type = PERF_TYPE_HW_CACHE,
  809. .config =
  810. PERF_COUNT_HW_CACHE_L1I << 0 |
  811. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  812. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  813. { .type = PERF_TYPE_HW_CACHE,
  814. .config =
  815. PERF_COUNT_HW_CACHE_DTLB << 0 |
  816. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  817. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  818. { .type = PERF_TYPE_HW_CACHE,
  819. .config =
  820. PERF_COUNT_HW_CACHE_DTLB << 0 |
  821. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  822. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  823. { .type = PERF_TYPE_HW_CACHE,
  824. .config =
  825. PERF_COUNT_HW_CACHE_ITLB << 0 |
  826. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  827. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  828. { .type = PERF_TYPE_HW_CACHE,
  829. .config =
  830. PERF_COUNT_HW_CACHE_ITLB << 0 |
  831. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  832. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  833. };
  834. /*
  835. * Very, very detailed stats (-d -d -d), adding prefetch events:
  836. */
  837. struct perf_event_attr very_very_detailed_attrs[] = {
  838. { .type = PERF_TYPE_HW_CACHE,
  839. .config =
  840. PERF_COUNT_HW_CACHE_L1D << 0 |
  841. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  842. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  843. { .type = PERF_TYPE_HW_CACHE,
  844. .config =
  845. PERF_COUNT_HW_CACHE_L1D << 0 |
  846. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  847. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  848. };
  849. /* Set attrs if no event is selected and !null_run: */
  850. if (null_run)
  851. return 0;
  852. if (!evsel_list->nr_entries) {
  853. if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
  854. return -1;
  855. }
  856. /* Detailed events get appended to the event list: */
  857. if (detailed_run < 1)
  858. return 0;
  859. /* Append detailed run extra attributes: */
  860. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  861. return -1;
  862. if (detailed_run < 2)
  863. return 0;
  864. /* Append very detailed run extra attributes: */
  865. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  866. return -1;
  867. if (detailed_run < 3)
  868. return 0;
  869. /* Append very, very detailed run extra attributes: */
  870. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  871. }
  872. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  873. {
  874. bool append_file = false;
  875. int output_fd = 0;
  876. const char *output_name = NULL;
  877. const struct option options[] = {
  878. OPT_CALLBACK('e', "event", &evsel_list, "event",
  879. "event selector. use 'perf list' to list available events",
  880. parse_events_option),
  881. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  882. "event filter", parse_filter),
  883. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  884. "child tasks do not inherit counters"),
  885. OPT_STRING('p', "pid", &target.pid, "pid",
  886. "stat events on existing process id"),
  887. OPT_STRING('t', "tid", &target.tid, "tid",
  888. "stat events on existing thread id"),
  889. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  890. "system-wide collection from all CPUs"),
  891. OPT_BOOLEAN('g', "group", &group,
  892. "put the counters into a counter group"),
  893. OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
  894. OPT_INCR('v', "verbose", &verbose,
  895. "be more verbose (show counter open errors, etc)"),
  896. OPT_INTEGER('r', "repeat", &run_count,
  897. "repeat command and print average + stddev (max: 100)"),
  898. OPT_BOOLEAN('n', "null", &null_run,
  899. "null run - dont start any counters"),
  900. OPT_INCR('d', "detailed", &detailed_run,
  901. "detailed run - start a lot of events"),
  902. OPT_BOOLEAN('S', "sync", &sync_run,
  903. "call sync() before starting a run"),
  904. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  905. "print large numbers with thousands\' separators",
  906. stat__set_big_num),
  907. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  908. "list of cpus to monitor in system-wide"),
  909. OPT_BOOLEAN('A', "no-aggr", &no_aggr, "disable CPU count aggregation"),
  910. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  911. "print counts with custom separator"),
  912. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  913. "monitor event in cgroup name only", parse_cgroups),
  914. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  915. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  916. OPT_INTEGER(0, "log-fd", &output_fd,
  917. "log output to fd, instead of stderr"),
  918. OPT_STRING(0, "pre", &pre_cmd, "command",
  919. "command to run prior to the measured command"),
  920. OPT_STRING(0, "post", &post_cmd, "command",
  921. "command to run after to the measured command"),
  922. OPT_END()
  923. };
  924. const char * const stat_usage[] = {
  925. "perf stat [<options>] [<command>]",
  926. NULL
  927. };
  928. struct perf_evsel *pos;
  929. int status = -ENOMEM, run_idx;
  930. const char *mode;
  931. setlocale(LC_ALL, "");
  932. evsel_list = perf_evlist__new(NULL, NULL);
  933. if (evsel_list == NULL)
  934. return -ENOMEM;
  935. argc = parse_options(argc, argv, options, stat_usage,
  936. PARSE_OPT_STOP_AT_NON_OPTION);
  937. output = stderr;
  938. if (output_name && strcmp(output_name, "-"))
  939. output = NULL;
  940. if (output_name && output_fd) {
  941. fprintf(stderr, "cannot use both --output and --log-fd\n");
  942. usage_with_options(stat_usage, options);
  943. }
  944. if (output_fd < 0) {
  945. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  946. usage_with_options(stat_usage, options);
  947. }
  948. if (!output) {
  949. struct timespec tm;
  950. mode = append_file ? "a" : "w";
  951. output = fopen(output_name, mode);
  952. if (!output) {
  953. perror("failed to create output file");
  954. return -1;
  955. }
  956. clock_gettime(CLOCK_REALTIME, &tm);
  957. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  958. } else if (output_fd > 0) {
  959. mode = append_file ? "a" : "w";
  960. output = fdopen(output_fd, mode);
  961. if (!output) {
  962. perror("Failed opening logfd");
  963. return -errno;
  964. }
  965. }
  966. if (csv_sep) {
  967. csv_output = true;
  968. if (!strcmp(csv_sep, "\\t"))
  969. csv_sep = "\t";
  970. } else
  971. csv_sep = DEFAULT_SEPARATOR;
  972. /*
  973. * let the spreadsheet do the pretty-printing
  974. */
  975. if (csv_output) {
  976. /* User explicitly passed -B? */
  977. if (big_num_opt == 1) {
  978. fprintf(stderr, "-B option not supported with -x\n");
  979. usage_with_options(stat_usage, options);
  980. } else /* Nope, so disable big number formatting */
  981. big_num = false;
  982. } else if (big_num_opt == 0) /* User passed --no-big-num */
  983. big_num = false;
  984. if (!argc && !perf_target__has_task(&target))
  985. usage_with_options(stat_usage, options);
  986. if (run_count <= 0)
  987. usage_with_options(stat_usage, options);
  988. /* no_aggr, cgroup are for system-wide only */
  989. if ((no_aggr || nr_cgroups) && !perf_target__has_cpu(&target)) {
  990. fprintf(stderr, "both cgroup and no-aggregation "
  991. "modes only available in system-wide mode\n");
  992. usage_with_options(stat_usage, options);
  993. }
  994. if (add_default_attributes())
  995. goto out;
  996. perf_target__validate(&target);
  997. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  998. if (perf_target__has_task(&target))
  999. pr_err("Problems finding threads of monitor\n");
  1000. if (perf_target__has_cpu(&target))
  1001. perror("failed to parse CPUs map");
  1002. usage_with_options(stat_usage, options);
  1003. return -1;
  1004. }
  1005. list_for_each_entry(pos, &evsel_list->entries, node) {
  1006. if (perf_evsel__alloc_stat_priv(pos) < 0 ||
  1007. perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
  1008. goto out_free_fd;
  1009. }
  1010. /*
  1011. * We dont want to block the signals - that would cause
  1012. * child tasks to inherit that and Ctrl-C would not work.
  1013. * What we want is for Ctrl-C to work in the exec()-ed
  1014. * task, but being ignored by perf stat itself:
  1015. */
  1016. atexit(sig_atexit);
  1017. signal(SIGINT, skip_signal);
  1018. signal(SIGALRM, skip_signal);
  1019. signal(SIGABRT, skip_signal);
  1020. status = 0;
  1021. for (run_idx = 0; run_idx < run_count; run_idx++) {
  1022. if (run_count != 1 && verbose)
  1023. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1024. run_idx + 1);
  1025. status = run_perf_stat(argc, argv);
  1026. }
  1027. if (status != -1)
  1028. print_stat(argc, argv);
  1029. out_free_fd:
  1030. list_for_each_entry(pos, &evsel_list->entries, node)
  1031. perf_evsel__free_stat_priv(pos);
  1032. perf_evlist__delete_maps(evsel_list);
  1033. out:
  1034. perf_evlist__delete(evsel_list);
  1035. return status;
  1036. }