builtin-stat.c 33 KB

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