builtin-stat.c 33 KB

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