builtin-stat.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. char msg[512];
  211. unsigned long long t0, t1;
  212. struct perf_evsel *counter;
  213. int status = 0;
  214. int child_ready_pipe[2], go_pipe[2];
  215. const bool forks = (argc > 0);
  216. char buf;
  217. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  218. perror("failed to create pipes");
  219. return -1;
  220. }
  221. if (forks) {
  222. if ((child_pid = fork()) < 0)
  223. perror("failed to fork");
  224. if (!child_pid) {
  225. close(child_ready_pipe[0]);
  226. close(go_pipe[1]);
  227. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  228. /*
  229. * Do a dummy execvp to get the PLT entry resolved,
  230. * so we avoid the resolver overhead on the real
  231. * execvp call.
  232. */
  233. execvp("", (char **)argv);
  234. /*
  235. * Tell the parent we're ready to go
  236. */
  237. close(child_ready_pipe[1]);
  238. /*
  239. * Wait until the parent tells us to go.
  240. */
  241. if (read(go_pipe[0], &buf, 1) == -1)
  242. perror("unable to read pipe");
  243. execvp(argv[0], (char **)argv);
  244. perror(argv[0]);
  245. exit(-1);
  246. }
  247. if (perf_target__none(&target))
  248. evsel_list->threads->map[0] = child_pid;
  249. /*
  250. * Wait for the child to be ready to exec.
  251. */
  252. close(child_ready_pipe[1]);
  253. close(go_pipe[0]);
  254. if (read(child_ready_pipe[0], &buf, 1) == -1)
  255. perror("unable to read pipe");
  256. close(child_ready_pipe[0]);
  257. }
  258. if (group)
  259. perf_evlist__set_leader(evsel_list);
  260. list_for_each_entry(counter, &evsel_list->entries, node) {
  261. if (create_perf_stat_counter(counter) < 0) {
  262. /*
  263. * PPC returns ENXIO for HW counters until 2.6.37
  264. * (behavior changed with commit b0a873e).
  265. */
  266. if (errno == EINVAL || errno == ENOSYS ||
  267. errno == ENOENT || errno == EOPNOTSUPP ||
  268. errno == ENXIO) {
  269. if (verbose)
  270. ui__warning("%s event is not supported by the kernel.\n",
  271. perf_evsel__name(counter));
  272. counter->supported = false;
  273. continue;
  274. }
  275. perf_evsel__open_strerror(counter, &target,
  276. errno, msg, sizeof(msg));
  277. ui__error("%s\n", msg);
  278. if (child_pid != -1)
  279. kill(child_pid, SIGTERM);
  280. return -1;
  281. }
  282. counter->supported = true;
  283. }
  284. if (perf_evlist__apply_filters(evsel_list)) {
  285. error("failed to set filter with %d (%s)\n", errno,
  286. strerror(errno));
  287. return -1;
  288. }
  289. /*
  290. * Enable counters and exec the command:
  291. */
  292. t0 = rdclock();
  293. if (forks) {
  294. close(go_pipe[1]);
  295. wait(&status);
  296. if (WIFSIGNALED(status))
  297. psignal(WTERMSIG(status), argv[0]);
  298. } else {
  299. while(!done) sleep(1);
  300. }
  301. t1 = rdclock();
  302. update_stats(&walltime_nsecs_stats, t1 - t0);
  303. if (no_aggr) {
  304. list_for_each_entry(counter, &evsel_list->entries, node) {
  305. read_counter(counter);
  306. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
  307. }
  308. } else {
  309. list_for_each_entry(counter, &evsel_list->entries, node) {
  310. read_counter_aggr(counter);
  311. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
  312. evsel_list->threads->nr);
  313. }
  314. }
  315. return WEXITSTATUS(status);
  316. }
  317. static int run_perf_stat(int argc __maybe_unused, const char **argv)
  318. {
  319. int ret;
  320. if (pre_cmd) {
  321. ret = system(pre_cmd);
  322. if (ret)
  323. return ret;
  324. }
  325. if (sync_run)
  326. sync();
  327. ret = __run_perf_stat(argc, argv);
  328. if (ret)
  329. return ret;
  330. if (post_cmd) {
  331. ret = system(post_cmd);
  332. if (ret)
  333. return ret;
  334. }
  335. return ret;
  336. }
  337. static void print_noise_pct(double total, double avg)
  338. {
  339. double pct = rel_stddev_stats(total, avg);
  340. if (csv_output)
  341. fprintf(output, "%s%.2f%%", csv_sep, pct);
  342. else if (pct)
  343. fprintf(output, " ( +-%6.2f%% )", pct);
  344. }
  345. static void print_noise(struct perf_evsel *evsel, double avg)
  346. {
  347. struct perf_stat *ps;
  348. if (run_count == 1)
  349. return;
  350. ps = evsel->priv;
  351. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  352. }
  353. static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
  354. {
  355. double msecs = avg / 1e6;
  356. char cpustr[16] = { '\0', };
  357. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
  358. if (no_aggr)
  359. sprintf(cpustr, "CPU%*d%s",
  360. csv_output ? 0 : -4,
  361. perf_evsel__cpus(evsel)->map[cpu], csv_sep);
  362. fprintf(output, fmt, cpustr, msecs, csv_sep, perf_evsel__name(evsel));
  363. if (evsel->cgrp)
  364. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  365. if (csv_output)
  366. return;
  367. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  368. fprintf(output, " # %8.3f CPUs utilized ",
  369. avg / avg_stats(&walltime_nsecs_stats));
  370. else
  371. fprintf(output, " ");
  372. }
  373. /* used for get_ratio_color() */
  374. enum grc_type {
  375. GRC_STALLED_CYCLES_FE,
  376. GRC_STALLED_CYCLES_BE,
  377. GRC_CACHE_MISSES,
  378. GRC_MAX_NR
  379. };
  380. static const char *get_ratio_color(enum grc_type type, double ratio)
  381. {
  382. static const double grc_table[GRC_MAX_NR][3] = {
  383. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  384. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  385. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  386. };
  387. const char *color = PERF_COLOR_NORMAL;
  388. if (ratio > grc_table[type][0])
  389. color = PERF_COLOR_RED;
  390. else if (ratio > grc_table[type][1])
  391. color = PERF_COLOR_MAGENTA;
  392. else if (ratio > grc_table[type][2])
  393. color = PERF_COLOR_YELLOW;
  394. return color;
  395. }
  396. static void print_stalled_cycles_frontend(int cpu,
  397. struct perf_evsel *evsel
  398. __maybe_unused, double avg)
  399. {
  400. double total, ratio = 0.0;
  401. const char *color;
  402. total = avg_stats(&runtime_cycles_stats[cpu]);
  403. if (total)
  404. ratio = avg / total * 100.0;
  405. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  406. fprintf(output, " # ");
  407. color_fprintf(output, color, "%6.2f%%", ratio);
  408. fprintf(output, " frontend cycles idle ");
  409. }
  410. static void print_stalled_cycles_backend(int cpu,
  411. struct perf_evsel *evsel
  412. __maybe_unused, double avg)
  413. {
  414. double total, ratio = 0.0;
  415. const char *color;
  416. total = avg_stats(&runtime_cycles_stats[cpu]);
  417. if (total)
  418. ratio = avg / total * 100.0;
  419. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  420. fprintf(output, " # ");
  421. color_fprintf(output, color, "%6.2f%%", ratio);
  422. fprintf(output, " backend cycles idle ");
  423. }
  424. static void print_branch_misses(int cpu,
  425. struct perf_evsel *evsel __maybe_unused,
  426. double avg)
  427. {
  428. double total, ratio = 0.0;
  429. const char *color;
  430. total = avg_stats(&runtime_branches_stats[cpu]);
  431. if (total)
  432. ratio = avg / total * 100.0;
  433. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  434. fprintf(output, " # ");
  435. color_fprintf(output, color, "%6.2f%%", ratio);
  436. fprintf(output, " of all branches ");
  437. }
  438. static void print_l1_dcache_misses(int cpu,
  439. struct perf_evsel *evsel __maybe_unused,
  440. double avg)
  441. {
  442. double total, ratio = 0.0;
  443. const char *color;
  444. total = avg_stats(&runtime_l1_dcache_stats[cpu]);
  445. if (total)
  446. ratio = avg / total * 100.0;
  447. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  448. fprintf(output, " # ");
  449. color_fprintf(output, color, "%6.2f%%", ratio);
  450. fprintf(output, " of all L1-dcache hits ");
  451. }
  452. static void print_l1_icache_misses(int cpu,
  453. struct perf_evsel *evsel __maybe_unused,
  454. double avg)
  455. {
  456. double total, ratio = 0.0;
  457. const char *color;
  458. total = avg_stats(&runtime_l1_icache_stats[cpu]);
  459. if (total)
  460. ratio = avg / total * 100.0;
  461. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  462. fprintf(output, " # ");
  463. color_fprintf(output, color, "%6.2f%%", ratio);
  464. fprintf(output, " of all L1-icache hits ");
  465. }
  466. static void print_dtlb_cache_misses(int cpu,
  467. struct perf_evsel *evsel __maybe_unused,
  468. double avg)
  469. {
  470. double total, ratio = 0.0;
  471. const char *color;
  472. total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
  473. if (total)
  474. ratio = avg / total * 100.0;
  475. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  476. fprintf(output, " # ");
  477. color_fprintf(output, color, "%6.2f%%", ratio);
  478. fprintf(output, " of all dTLB cache hits ");
  479. }
  480. static void print_itlb_cache_misses(int cpu,
  481. struct perf_evsel *evsel __maybe_unused,
  482. double avg)
  483. {
  484. double total, ratio = 0.0;
  485. const char *color;
  486. total = avg_stats(&runtime_itlb_cache_stats[cpu]);
  487. if (total)
  488. ratio = avg / total * 100.0;
  489. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  490. fprintf(output, " # ");
  491. color_fprintf(output, color, "%6.2f%%", ratio);
  492. fprintf(output, " of all iTLB cache hits ");
  493. }
  494. static void print_ll_cache_misses(int cpu,
  495. struct perf_evsel *evsel __maybe_unused,
  496. double avg)
  497. {
  498. double total, ratio = 0.0;
  499. const char *color;
  500. total = avg_stats(&runtime_ll_cache_stats[cpu]);
  501. if (total)
  502. ratio = avg / total * 100.0;
  503. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  504. fprintf(output, " # ");
  505. color_fprintf(output, color, "%6.2f%%", ratio);
  506. fprintf(output, " of all LL-cache hits ");
  507. }
  508. static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
  509. {
  510. double total, ratio = 0.0;
  511. char cpustr[16] = { '\0', };
  512. const char *fmt;
  513. if (csv_output)
  514. fmt = "%s%.0f%s%s";
  515. else if (big_num)
  516. fmt = "%s%'18.0f%s%-25s";
  517. else
  518. fmt = "%s%18.0f%s%-25s";
  519. if (no_aggr)
  520. sprintf(cpustr, "CPU%*d%s",
  521. csv_output ? 0 : -4,
  522. perf_evsel__cpus(evsel)->map[cpu], csv_sep);
  523. else
  524. cpu = 0;
  525. fprintf(output, fmt, cpustr, avg, csv_sep, perf_evsel__name(evsel));
  526. if (evsel->cgrp)
  527. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  528. if (csv_output)
  529. return;
  530. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  531. total = avg_stats(&runtime_cycles_stats[cpu]);
  532. if (total)
  533. ratio = avg / total;
  534. fprintf(output, " # %5.2f insns per cycle ", ratio);
  535. total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
  536. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
  537. if (total && avg) {
  538. ratio = total / avg;
  539. fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
  540. }
  541. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  542. runtime_branches_stats[cpu].n != 0) {
  543. print_branch_misses(cpu, evsel, avg);
  544. } else if (
  545. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  546. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  547. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  548. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  549. runtime_l1_dcache_stats[cpu].n != 0) {
  550. print_l1_dcache_misses(cpu, evsel, avg);
  551. } else if (
  552. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  553. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  554. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  555. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  556. runtime_l1_icache_stats[cpu].n != 0) {
  557. print_l1_icache_misses(cpu, evsel, avg);
  558. } else if (
  559. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  560. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  561. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  562. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  563. runtime_dtlb_cache_stats[cpu].n != 0) {
  564. print_dtlb_cache_misses(cpu, evsel, avg);
  565. } else if (
  566. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  567. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  568. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  569. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  570. runtime_itlb_cache_stats[cpu].n != 0) {
  571. print_itlb_cache_misses(cpu, evsel, avg);
  572. } else if (
  573. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  574. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  575. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  576. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  577. runtime_ll_cache_stats[cpu].n != 0) {
  578. print_ll_cache_misses(cpu, evsel, avg);
  579. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
  580. runtime_cacherefs_stats[cpu].n != 0) {
  581. total = avg_stats(&runtime_cacherefs_stats[cpu]);
  582. if (total)
  583. ratio = avg * 100 / total;
  584. fprintf(output, " # %8.3f %% of all cache refs ", ratio);
  585. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  586. print_stalled_cycles_frontend(cpu, evsel, avg);
  587. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  588. print_stalled_cycles_backend(cpu, evsel, avg);
  589. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  590. total = avg_stats(&runtime_nsecs_stats[cpu]);
  591. if (total)
  592. ratio = 1.0 * avg / total;
  593. fprintf(output, " # %8.3f GHz ", ratio);
  594. } else if (runtime_nsecs_stats[cpu].n != 0) {
  595. char unit = 'M';
  596. total = avg_stats(&runtime_nsecs_stats[cpu]);
  597. if (total)
  598. ratio = 1000.0 * avg / total;
  599. if (ratio < 0.001) {
  600. ratio *= 1000;
  601. unit = 'K';
  602. }
  603. fprintf(output, " # %8.3f %c/sec ", ratio, unit);
  604. } else {
  605. fprintf(output, " ");
  606. }
  607. }
  608. /*
  609. * Print out the results of a single counter:
  610. * aggregated counts in system-wide mode
  611. */
  612. static void print_counter_aggr(struct perf_evsel *counter)
  613. {
  614. struct perf_stat *ps = counter->priv;
  615. double avg = avg_stats(&ps->res_stats[0]);
  616. int scaled = counter->counts->scaled;
  617. if (scaled == -1) {
  618. fprintf(output, "%*s%s%*s",
  619. csv_output ? 0 : 18,
  620. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  621. csv_sep,
  622. csv_output ? 0 : -24,
  623. perf_evsel__name(counter));
  624. if (counter->cgrp)
  625. fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
  626. fputc('\n', output);
  627. return;
  628. }
  629. if (nsec_counter(counter))
  630. nsec_printout(-1, counter, avg);
  631. else
  632. abs_printout(-1, counter, avg);
  633. print_noise(counter, avg);
  634. if (csv_output) {
  635. fputc('\n', output);
  636. return;
  637. }
  638. if (scaled) {
  639. double avg_enabled, avg_running;
  640. avg_enabled = avg_stats(&ps->res_stats[1]);
  641. avg_running = avg_stats(&ps->res_stats[2]);
  642. fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
  643. }
  644. fprintf(output, "\n");
  645. }
  646. /*
  647. * Print out the results of a single counter:
  648. * does not use aggregated count in system-wide
  649. */
  650. static void print_counter(struct perf_evsel *counter)
  651. {
  652. u64 ena, run, val;
  653. int cpu;
  654. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  655. val = counter->counts->cpu[cpu].val;
  656. ena = counter->counts->cpu[cpu].ena;
  657. run = counter->counts->cpu[cpu].run;
  658. if (run == 0 || ena == 0) {
  659. fprintf(output, "CPU%*d%s%*s%s%*s",
  660. csv_output ? 0 : -4,
  661. perf_evsel__cpus(counter)->map[cpu], csv_sep,
  662. csv_output ? 0 : 18,
  663. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  664. csv_sep,
  665. csv_output ? 0 : -24,
  666. perf_evsel__name(counter));
  667. if (counter->cgrp)
  668. fprintf(output, "%s%s",
  669. csv_sep, counter->cgrp->name);
  670. fputc('\n', output);
  671. continue;
  672. }
  673. if (nsec_counter(counter))
  674. nsec_printout(cpu, counter, val);
  675. else
  676. abs_printout(cpu, counter, val);
  677. if (!csv_output) {
  678. print_noise(counter, 1.0);
  679. if (run != ena)
  680. fprintf(output, " (%.2f%%)",
  681. 100.0 * run / ena);
  682. }
  683. fputc('\n', output);
  684. }
  685. }
  686. static void print_stat(int argc, const char **argv)
  687. {
  688. struct perf_evsel *counter;
  689. int i;
  690. fflush(stdout);
  691. if (!csv_output) {
  692. fprintf(output, "\n");
  693. fprintf(output, " Performance counter stats for ");
  694. if (!perf_target__has_task(&target)) {
  695. fprintf(output, "\'%s", argv[0]);
  696. for (i = 1; i < argc; i++)
  697. fprintf(output, " %s", argv[i]);
  698. } else if (target.pid)
  699. fprintf(output, "process id \'%s", target.pid);
  700. else
  701. fprintf(output, "thread id \'%s", target.tid);
  702. fprintf(output, "\'");
  703. if (run_count > 1)
  704. fprintf(output, " (%d runs)", run_count);
  705. fprintf(output, ":\n\n");
  706. }
  707. if (no_aggr) {
  708. list_for_each_entry(counter, &evsel_list->entries, node)
  709. print_counter(counter);
  710. } else {
  711. list_for_each_entry(counter, &evsel_list->entries, node)
  712. print_counter_aggr(counter);
  713. }
  714. if (!csv_output) {
  715. if (!null_run)
  716. fprintf(output, "\n");
  717. fprintf(output, " %17.9f seconds time elapsed",
  718. avg_stats(&walltime_nsecs_stats)/1e9);
  719. if (run_count > 1) {
  720. fprintf(output, " ");
  721. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  722. avg_stats(&walltime_nsecs_stats));
  723. }
  724. fprintf(output, "\n\n");
  725. }
  726. }
  727. static volatile int signr = -1;
  728. static void skip_signal(int signo)
  729. {
  730. if(child_pid == -1)
  731. done = 1;
  732. signr = signo;
  733. }
  734. static void sig_atexit(void)
  735. {
  736. if (child_pid != -1)
  737. kill(child_pid, SIGTERM);
  738. if (signr == -1)
  739. return;
  740. signal(signr, SIG_DFL);
  741. kill(getpid(), signr);
  742. }
  743. static int stat__set_big_num(const struct option *opt __maybe_unused,
  744. const char *s __maybe_unused, int unset)
  745. {
  746. big_num_opt = unset ? 0 : 1;
  747. return 0;
  748. }
  749. /*
  750. * Add default attributes, if there were no attributes specified or
  751. * if -d/--detailed, -d -d or -d -d -d is used:
  752. */
  753. static int add_default_attributes(void)
  754. {
  755. struct perf_event_attr default_attrs[] = {
  756. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  757. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  758. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  759. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  760. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  761. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  762. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  763. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  764. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  765. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  766. };
  767. /*
  768. * Detailed stats (-d), covering the L1 and last level data caches:
  769. */
  770. struct perf_event_attr detailed_attrs[] = {
  771. { .type = PERF_TYPE_HW_CACHE,
  772. .config =
  773. PERF_COUNT_HW_CACHE_L1D << 0 |
  774. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  775. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  776. { .type = PERF_TYPE_HW_CACHE,
  777. .config =
  778. PERF_COUNT_HW_CACHE_L1D << 0 |
  779. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  780. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  781. { .type = PERF_TYPE_HW_CACHE,
  782. .config =
  783. PERF_COUNT_HW_CACHE_LL << 0 |
  784. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  785. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  786. { .type = PERF_TYPE_HW_CACHE,
  787. .config =
  788. PERF_COUNT_HW_CACHE_LL << 0 |
  789. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  790. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  791. };
  792. /*
  793. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  794. */
  795. struct perf_event_attr very_detailed_attrs[] = {
  796. { .type = PERF_TYPE_HW_CACHE,
  797. .config =
  798. PERF_COUNT_HW_CACHE_L1I << 0 |
  799. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  800. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  801. { .type = PERF_TYPE_HW_CACHE,
  802. .config =
  803. PERF_COUNT_HW_CACHE_L1I << 0 |
  804. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  805. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  806. { .type = PERF_TYPE_HW_CACHE,
  807. .config =
  808. PERF_COUNT_HW_CACHE_DTLB << 0 |
  809. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  810. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  811. { .type = PERF_TYPE_HW_CACHE,
  812. .config =
  813. PERF_COUNT_HW_CACHE_DTLB << 0 |
  814. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  815. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  816. { .type = PERF_TYPE_HW_CACHE,
  817. .config =
  818. PERF_COUNT_HW_CACHE_ITLB << 0 |
  819. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  820. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  821. { .type = PERF_TYPE_HW_CACHE,
  822. .config =
  823. PERF_COUNT_HW_CACHE_ITLB << 0 |
  824. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  825. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  826. };
  827. /*
  828. * Very, very detailed stats (-d -d -d), adding prefetch events:
  829. */
  830. struct perf_event_attr very_very_detailed_attrs[] = {
  831. { .type = PERF_TYPE_HW_CACHE,
  832. .config =
  833. PERF_COUNT_HW_CACHE_L1D << 0 |
  834. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  835. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  836. { .type = PERF_TYPE_HW_CACHE,
  837. .config =
  838. PERF_COUNT_HW_CACHE_L1D << 0 |
  839. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  840. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  841. };
  842. /* Set attrs if no event is selected and !null_run: */
  843. if (null_run)
  844. return 0;
  845. if (!evsel_list->nr_entries) {
  846. if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
  847. return -1;
  848. }
  849. /* Detailed events get appended to the event list: */
  850. if (detailed_run < 1)
  851. return 0;
  852. /* Append detailed run extra attributes: */
  853. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  854. return -1;
  855. if (detailed_run < 2)
  856. return 0;
  857. /* Append very detailed run extra attributes: */
  858. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  859. return -1;
  860. if (detailed_run < 3)
  861. return 0;
  862. /* Append very, very detailed run extra attributes: */
  863. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  864. }
  865. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  866. {
  867. bool append_file = false;
  868. int output_fd = 0;
  869. const char *output_name = NULL;
  870. const struct option options[] = {
  871. OPT_CALLBACK('e', "event", &evsel_list, "event",
  872. "event selector. use 'perf list' to list available events",
  873. parse_events_option),
  874. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  875. "event filter", parse_filter),
  876. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  877. "child tasks do not inherit counters"),
  878. OPT_STRING('p', "pid", &target.pid, "pid",
  879. "stat events on existing process id"),
  880. OPT_STRING('t', "tid", &target.tid, "tid",
  881. "stat events on existing thread id"),
  882. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  883. "system-wide collection from all CPUs"),
  884. OPT_BOOLEAN('g', "group", &group,
  885. "put the counters into a counter group"),
  886. OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
  887. OPT_INCR('v', "verbose", &verbose,
  888. "be more verbose (show counter open errors, etc)"),
  889. OPT_INTEGER('r', "repeat", &run_count,
  890. "repeat command and print average + stddev (max: 100)"),
  891. OPT_BOOLEAN('n', "null", &null_run,
  892. "null run - dont start any counters"),
  893. OPT_INCR('d', "detailed", &detailed_run,
  894. "detailed run - start a lot of events"),
  895. OPT_BOOLEAN('S', "sync", &sync_run,
  896. "call sync() before starting a run"),
  897. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  898. "print large numbers with thousands\' separators",
  899. stat__set_big_num),
  900. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  901. "list of cpus to monitor in system-wide"),
  902. OPT_BOOLEAN('A', "no-aggr", &no_aggr, "disable CPU count aggregation"),
  903. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  904. "print counts with custom separator"),
  905. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  906. "monitor event in cgroup name only", parse_cgroups),
  907. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  908. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  909. OPT_INTEGER(0, "log-fd", &output_fd,
  910. "log output to fd, instead of stderr"),
  911. OPT_STRING(0, "pre", &pre_cmd, "command",
  912. "command to run prior to the measured command"),
  913. OPT_STRING(0, "post", &post_cmd, "command",
  914. "command to run after to the measured command"),
  915. OPT_END()
  916. };
  917. const char * const stat_usage[] = {
  918. "perf stat [<options>] [<command>]",
  919. NULL
  920. };
  921. struct perf_evsel *pos;
  922. int status = -ENOMEM, run_idx;
  923. const char *mode;
  924. setlocale(LC_ALL, "");
  925. evsel_list = perf_evlist__new(NULL, NULL);
  926. if (evsel_list == NULL)
  927. return -ENOMEM;
  928. argc = parse_options(argc, argv, options, stat_usage,
  929. PARSE_OPT_STOP_AT_NON_OPTION);
  930. output = stderr;
  931. if (output_name && strcmp(output_name, "-"))
  932. output = NULL;
  933. if (output_name && output_fd) {
  934. fprintf(stderr, "cannot use both --output and --log-fd\n");
  935. usage_with_options(stat_usage, options);
  936. }
  937. if (output_fd < 0) {
  938. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  939. usage_with_options(stat_usage, options);
  940. }
  941. if (!output) {
  942. struct timespec tm;
  943. mode = append_file ? "a" : "w";
  944. output = fopen(output_name, mode);
  945. if (!output) {
  946. perror("failed to create output file");
  947. return -1;
  948. }
  949. clock_gettime(CLOCK_REALTIME, &tm);
  950. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  951. } else if (output_fd > 0) {
  952. mode = append_file ? "a" : "w";
  953. output = fdopen(output_fd, mode);
  954. if (!output) {
  955. perror("Failed opening logfd");
  956. return -errno;
  957. }
  958. }
  959. if (csv_sep) {
  960. csv_output = true;
  961. if (!strcmp(csv_sep, "\\t"))
  962. csv_sep = "\t";
  963. } else
  964. csv_sep = DEFAULT_SEPARATOR;
  965. /*
  966. * let the spreadsheet do the pretty-printing
  967. */
  968. if (csv_output) {
  969. /* User explicitly passed -B? */
  970. if (big_num_opt == 1) {
  971. fprintf(stderr, "-B option not supported with -x\n");
  972. usage_with_options(stat_usage, options);
  973. } else /* Nope, so disable big number formatting */
  974. big_num = false;
  975. } else if (big_num_opt == 0) /* User passed --no-big-num */
  976. big_num = false;
  977. if (!argc && !perf_target__has_task(&target))
  978. usage_with_options(stat_usage, options);
  979. if (run_count <= 0)
  980. usage_with_options(stat_usage, options);
  981. /* no_aggr, cgroup are for system-wide only */
  982. if ((no_aggr || nr_cgroups) && !perf_target__has_cpu(&target)) {
  983. fprintf(stderr, "both cgroup and no-aggregation "
  984. "modes only available in system-wide mode\n");
  985. usage_with_options(stat_usage, options);
  986. }
  987. if (add_default_attributes())
  988. goto out;
  989. perf_target__validate(&target);
  990. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  991. if (perf_target__has_task(&target))
  992. pr_err("Problems finding threads of monitor\n");
  993. if (perf_target__has_cpu(&target))
  994. perror("failed to parse CPUs map");
  995. usage_with_options(stat_usage, options);
  996. return -1;
  997. }
  998. list_for_each_entry(pos, &evsel_list->entries, node) {
  999. if (perf_evsel__alloc_stat_priv(pos) < 0 ||
  1000. perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
  1001. goto out_free_fd;
  1002. }
  1003. /*
  1004. * We dont want to block the signals - that would cause
  1005. * child tasks to inherit that and Ctrl-C would not work.
  1006. * What we want is for Ctrl-C to work in the exec()-ed
  1007. * task, but being ignored by perf stat itself:
  1008. */
  1009. atexit(sig_atexit);
  1010. signal(SIGINT, skip_signal);
  1011. signal(SIGALRM, skip_signal);
  1012. signal(SIGABRT, skip_signal);
  1013. status = 0;
  1014. for (run_idx = 0; run_idx < run_count; run_idx++) {
  1015. if (run_count != 1 && verbose)
  1016. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1017. run_idx + 1);
  1018. status = run_perf_stat(argc, argv);
  1019. }
  1020. if (status != -1)
  1021. print_stat(argc, argv);
  1022. out_free_fd:
  1023. list_for_each_entry(pos, &evsel_list->entries, node)
  1024. perf_evsel__free_stat_priv(pos);
  1025. perf_evlist__delete_maps(evsel_list);
  1026. out:
  1027. perf_evlist__delete(evsel_list);
  1028. return status;
  1029. }