builtin-stat.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 <sys/prctl.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 perf_stat {
  167. struct stats res_stats[3];
  168. };
  169. static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  170. {
  171. evsel->priv = zalloc(sizeof(struct perf_stat));
  172. return evsel->priv == NULL ? -ENOMEM : 0;
  173. }
  174. static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  175. {
  176. free(evsel->priv);
  177. evsel->priv = NULL;
  178. }
  179. static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  180. static struct stats runtime_cycles_stats[MAX_NR_CPUS];
  181. static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
  182. static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
  183. static struct stats runtime_branches_stats[MAX_NR_CPUS];
  184. static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
  185. static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
  186. static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
  187. static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
  188. static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
  189. static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
  190. static struct stats walltime_nsecs_stats;
  191. static int create_perf_stat_counter(struct perf_evsel *evsel,
  192. struct perf_evsel *first)
  193. {
  194. struct perf_event_attr *attr = &evsel->attr;
  195. bool exclude_guest_missing = false;
  196. int ret;
  197. if (scale)
  198. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  199. PERF_FORMAT_TOTAL_TIME_RUNNING;
  200. attr->inherit = !no_inherit;
  201. retry:
  202. if (exclude_guest_missing)
  203. evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
  204. if (perf_target__has_cpu(&target)) {
  205. ret = perf_evsel__open_per_cpu(evsel, evsel_list->cpus);
  206. if (ret)
  207. goto check_ret;
  208. return 0;
  209. }
  210. if (!perf_target__has_task(&target) && (!group || evsel == first)) {
  211. attr->disabled = 1;
  212. attr->enable_on_exec = 1;
  213. }
  214. ret = perf_evsel__open_per_thread(evsel, evsel_list->threads);
  215. if (!ret)
  216. return 0;
  217. /* fall through */
  218. check_ret:
  219. if (ret && errno == EINVAL) {
  220. if (!exclude_guest_missing &&
  221. (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
  222. pr_debug("Old kernel, cannot exclude "
  223. "guest or host samples.\n");
  224. exclude_guest_missing = true;
  225. goto retry;
  226. }
  227. }
  228. return ret;
  229. }
  230. /*
  231. * Does the counter have nsecs as a unit?
  232. */
  233. static inline int nsec_counter(struct perf_evsel *evsel)
  234. {
  235. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  236. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  237. return 1;
  238. return 0;
  239. }
  240. /*
  241. * Update various tracking values we maintain to print
  242. * more semantic information such as miss/hit ratios,
  243. * instruction rates, etc:
  244. */
  245. static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
  246. {
  247. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
  248. update_stats(&runtime_nsecs_stats[0], count[0]);
  249. else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
  250. update_stats(&runtime_cycles_stats[0], count[0]);
  251. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
  252. update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
  253. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
  254. update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
  255. else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
  256. update_stats(&runtime_branches_stats[0], count[0]);
  257. else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
  258. update_stats(&runtime_cacherefs_stats[0], count[0]);
  259. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
  260. update_stats(&runtime_l1_dcache_stats[0], count[0]);
  261. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
  262. update_stats(&runtime_l1_icache_stats[0], count[0]);
  263. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
  264. update_stats(&runtime_ll_cache_stats[0], count[0]);
  265. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
  266. update_stats(&runtime_dtlb_cache_stats[0], count[0]);
  267. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
  268. update_stats(&runtime_itlb_cache_stats[0], count[0]);
  269. }
  270. /*
  271. * Read out the results of a single counter:
  272. * aggregate counts across CPUs in system-wide mode
  273. */
  274. static int read_counter_aggr(struct perf_evsel *counter)
  275. {
  276. struct perf_stat *ps = counter->priv;
  277. u64 *count = counter->counts->aggr.values;
  278. int i;
  279. if (__perf_evsel__read(counter, evsel_list->cpus->nr,
  280. evsel_list->threads->nr, scale) < 0)
  281. return -1;
  282. for (i = 0; i < 3; i++)
  283. update_stats(&ps->res_stats[i], count[i]);
  284. if (verbose) {
  285. fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  286. perf_evsel__name(counter), count[0], count[1], count[2]);
  287. }
  288. /*
  289. * Save the full runtime - to allow normalization during printout:
  290. */
  291. update_shadow_stats(counter, count);
  292. return 0;
  293. }
  294. /*
  295. * Read out the results of a single counter:
  296. * do not aggregate counts across CPUs in system-wide mode
  297. */
  298. static int read_counter(struct perf_evsel *counter)
  299. {
  300. u64 *count;
  301. int cpu;
  302. for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
  303. if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
  304. return -1;
  305. count = counter->counts->cpu[cpu].values;
  306. update_shadow_stats(counter, count);
  307. }
  308. return 0;
  309. }
  310. static int run_perf_stat(int argc __maybe_unused, const char **argv)
  311. {
  312. unsigned long long t0, t1;
  313. struct perf_evsel *counter, *first;
  314. int status = 0;
  315. int child_ready_pipe[2], go_pipe[2];
  316. const bool forks = (argc > 0);
  317. char buf;
  318. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  319. perror("failed to create pipes");
  320. return -1;
  321. }
  322. if (forks) {
  323. if ((child_pid = fork()) < 0)
  324. perror("failed to fork");
  325. if (!child_pid) {
  326. close(child_ready_pipe[0]);
  327. close(go_pipe[1]);
  328. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  329. /*
  330. * Do a dummy execvp to get the PLT entry resolved,
  331. * so we avoid the resolver overhead on the real
  332. * execvp call.
  333. */
  334. execvp("", (char **)argv);
  335. /*
  336. * Tell the parent we're ready to go
  337. */
  338. close(child_ready_pipe[1]);
  339. /*
  340. * Wait until the parent tells us to go.
  341. */
  342. if (read(go_pipe[0], &buf, 1) == -1)
  343. perror("unable to read pipe");
  344. execvp(argv[0], (char **)argv);
  345. perror(argv[0]);
  346. exit(-1);
  347. }
  348. if (perf_target__none(&target))
  349. evsel_list->threads->map[0] = child_pid;
  350. /*
  351. * Wait for the child to be ready to exec.
  352. */
  353. close(child_ready_pipe[1]);
  354. close(go_pipe[0]);
  355. if (read(child_ready_pipe[0], &buf, 1) == -1)
  356. perror("unable to read pipe");
  357. close(child_ready_pipe[0]);
  358. }
  359. if (group)
  360. perf_evlist__set_leader(evsel_list);
  361. first = perf_evlist__first(evsel_list);
  362. list_for_each_entry(counter, &evsel_list->entries, node) {
  363. if (create_perf_stat_counter(counter, first) < 0) {
  364. /*
  365. * PPC returns ENXIO for HW counters until 2.6.37
  366. * (behavior changed with commit b0a873e).
  367. */
  368. if (errno == EINVAL || errno == ENOSYS ||
  369. errno == ENOENT || errno == EOPNOTSUPP ||
  370. errno == ENXIO) {
  371. if (verbose)
  372. ui__warning("%s event is not supported by the kernel.\n",
  373. perf_evsel__name(counter));
  374. counter->supported = false;
  375. continue;
  376. }
  377. if (errno == EPERM || errno == EACCES) {
  378. error("You may not have permission to collect %sstats.\n"
  379. "\t Consider tweaking"
  380. " /proc/sys/kernel/perf_event_paranoid or running as root.",
  381. target.system_wide ? "system-wide " : "");
  382. } else {
  383. error("open_counter returned with %d (%s). "
  384. "/bin/dmesg may provide additional information.\n",
  385. errno, strerror(errno));
  386. }
  387. if (child_pid != -1)
  388. kill(child_pid, SIGTERM);
  389. pr_err("Not all events could be opened.\n");
  390. return -1;
  391. }
  392. counter->supported = true;
  393. }
  394. if (perf_evlist__set_filters(evsel_list)) {
  395. error("failed to set filter with %d (%s)\n", errno,
  396. strerror(errno));
  397. return -1;
  398. }
  399. /*
  400. * Enable counters and exec the command:
  401. */
  402. t0 = rdclock();
  403. if (forks) {
  404. close(go_pipe[1]);
  405. wait(&status);
  406. if (WIFSIGNALED(status))
  407. psignal(WTERMSIG(status), argv[0]);
  408. } else {
  409. while(!done) sleep(1);
  410. }
  411. t1 = rdclock();
  412. update_stats(&walltime_nsecs_stats, t1 - t0);
  413. if (no_aggr) {
  414. list_for_each_entry(counter, &evsel_list->entries, node) {
  415. read_counter(counter);
  416. perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
  417. }
  418. } else {
  419. list_for_each_entry(counter, &evsel_list->entries, node) {
  420. read_counter_aggr(counter);
  421. perf_evsel__close_fd(counter, evsel_list->cpus->nr,
  422. evsel_list->threads->nr);
  423. }
  424. }
  425. return WEXITSTATUS(status);
  426. }
  427. static void print_noise_pct(double total, double avg)
  428. {
  429. double pct = rel_stddev_stats(total, avg);
  430. if (csv_output)
  431. fprintf(output, "%s%.2f%%", csv_sep, pct);
  432. else if (pct)
  433. fprintf(output, " ( +-%6.2f%% )", pct);
  434. }
  435. static void print_noise(struct perf_evsel *evsel, double avg)
  436. {
  437. struct perf_stat *ps;
  438. if (run_count == 1)
  439. return;
  440. ps = evsel->priv;
  441. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  442. }
  443. static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
  444. {
  445. double msecs = avg / 1e6;
  446. char cpustr[16] = { '\0', };
  447. const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
  448. if (no_aggr)
  449. sprintf(cpustr, "CPU%*d%s",
  450. csv_output ? 0 : -4,
  451. evsel_list->cpus->map[cpu], csv_sep);
  452. fprintf(output, fmt, cpustr, msecs, csv_sep, perf_evsel__name(evsel));
  453. if (evsel->cgrp)
  454. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  455. if (csv_output)
  456. return;
  457. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  458. fprintf(output, " # %8.3f CPUs utilized ",
  459. avg / avg_stats(&walltime_nsecs_stats));
  460. else
  461. fprintf(output, " ");
  462. }
  463. /* used for get_ratio_color() */
  464. enum grc_type {
  465. GRC_STALLED_CYCLES_FE,
  466. GRC_STALLED_CYCLES_BE,
  467. GRC_CACHE_MISSES,
  468. GRC_MAX_NR
  469. };
  470. static const char *get_ratio_color(enum grc_type type, double ratio)
  471. {
  472. static const double grc_table[GRC_MAX_NR][3] = {
  473. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  474. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  475. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  476. };
  477. const char *color = PERF_COLOR_NORMAL;
  478. if (ratio > grc_table[type][0])
  479. color = PERF_COLOR_RED;
  480. else if (ratio > grc_table[type][1])
  481. color = PERF_COLOR_MAGENTA;
  482. else if (ratio > grc_table[type][2])
  483. color = PERF_COLOR_YELLOW;
  484. return color;
  485. }
  486. static void print_stalled_cycles_frontend(int cpu,
  487. struct perf_evsel *evsel
  488. __maybe_unused, double avg)
  489. {
  490. double total, ratio = 0.0;
  491. const char *color;
  492. total = avg_stats(&runtime_cycles_stats[cpu]);
  493. if (total)
  494. ratio = avg / total * 100.0;
  495. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  496. fprintf(output, " # ");
  497. color_fprintf(output, color, "%6.2f%%", ratio);
  498. fprintf(output, " frontend cycles idle ");
  499. }
  500. static void print_stalled_cycles_backend(int cpu,
  501. struct perf_evsel *evsel
  502. __maybe_unused, 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_BE, ratio);
  510. fprintf(output, " # ");
  511. color_fprintf(output, color, "%6.2f%%", ratio);
  512. fprintf(output, " backend cycles idle ");
  513. }
  514. static void print_branch_misses(int cpu,
  515. struct perf_evsel *evsel __maybe_unused,
  516. double avg)
  517. {
  518. double total, ratio = 0.0;
  519. const char *color;
  520. total = avg_stats(&runtime_branches_stats[cpu]);
  521. if (total)
  522. ratio = avg / total * 100.0;
  523. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  524. fprintf(output, " # ");
  525. color_fprintf(output, color, "%6.2f%%", ratio);
  526. fprintf(output, " of all branches ");
  527. }
  528. static void print_l1_dcache_misses(int cpu,
  529. struct perf_evsel *evsel __maybe_unused,
  530. double avg)
  531. {
  532. double total, ratio = 0.0;
  533. const char *color;
  534. total = avg_stats(&runtime_l1_dcache_stats[cpu]);
  535. if (total)
  536. ratio = avg / total * 100.0;
  537. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  538. fprintf(output, " # ");
  539. color_fprintf(output, color, "%6.2f%%", ratio);
  540. fprintf(output, " of all L1-dcache hits ");
  541. }
  542. static void print_l1_icache_misses(int cpu,
  543. struct perf_evsel *evsel __maybe_unused,
  544. double avg)
  545. {
  546. double total, ratio = 0.0;
  547. const char *color;
  548. total = avg_stats(&runtime_l1_icache_stats[cpu]);
  549. if (total)
  550. ratio = avg / total * 100.0;
  551. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  552. fprintf(output, " # ");
  553. color_fprintf(output, color, "%6.2f%%", ratio);
  554. fprintf(output, " of all L1-icache hits ");
  555. }
  556. static void print_dtlb_cache_misses(int cpu,
  557. struct perf_evsel *evsel __maybe_unused,
  558. double avg)
  559. {
  560. double total, ratio = 0.0;
  561. const char *color;
  562. total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
  563. if (total)
  564. ratio = avg / total * 100.0;
  565. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  566. fprintf(output, " # ");
  567. color_fprintf(output, color, "%6.2f%%", ratio);
  568. fprintf(output, " of all dTLB cache hits ");
  569. }
  570. static void print_itlb_cache_misses(int cpu,
  571. struct perf_evsel *evsel __maybe_unused,
  572. double avg)
  573. {
  574. double total, ratio = 0.0;
  575. const char *color;
  576. total = avg_stats(&runtime_itlb_cache_stats[cpu]);
  577. if (total)
  578. ratio = avg / total * 100.0;
  579. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  580. fprintf(output, " # ");
  581. color_fprintf(output, color, "%6.2f%%", ratio);
  582. fprintf(output, " of all iTLB cache hits ");
  583. }
  584. static void print_ll_cache_misses(int cpu,
  585. struct perf_evsel *evsel __maybe_unused,
  586. 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, perf_evsel__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. perf_evsel__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. perf_evsel__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 (!perf_target__has_task(&target)) {
  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 __maybe_unused,
  838. const char *s __maybe_unused, 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_default_attrs(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_default_attrs(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_default_attrs(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_default_attrs(evsel_list, very_very_detailed_attrs);
  919. }
  920. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  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_fd < 0) {
  939. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  940. usage_with_options(stat_usage, options);
  941. }
  942. if (!output) {
  943. struct timespec tm;
  944. mode = append_file ? "a" : "w";
  945. output = fopen(output_name, mode);
  946. if (!output) {
  947. perror("failed to create output file");
  948. return -1;
  949. }
  950. clock_gettime(CLOCK_REALTIME, &tm);
  951. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  952. } else if (output_fd > 0) {
  953. mode = append_file ? "a" : "w";
  954. output = fdopen(output_fd, mode);
  955. if (!output) {
  956. perror("Failed opening logfd");
  957. return -errno;
  958. }
  959. }
  960. if (csv_sep) {
  961. csv_output = true;
  962. if (!strcmp(csv_sep, "\\t"))
  963. csv_sep = "\t";
  964. } else
  965. csv_sep = DEFAULT_SEPARATOR;
  966. /*
  967. * let the spreadsheet do the pretty-printing
  968. */
  969. if (csv_output) {
  970. /* User explicitly passed -B? */
  971. if (big_num_opt == 1) {
  972. fprintf(stderr, "-B option not supported with -x\n");
  973. usage_with_options(stat_usage, options);
  974. } else /* Nope, so disable big number formatting */
  975. big_num = false;
  976. } else if (big_num_opt == 0) /* User passed --no-big-num */
  977. big_num = false;
  978. if (!argc && !perf_target__has_task(&target))
  979. usage_with_options(stat_usage, options);
  980. if (run_count <= 0)
  981. usage_with_options(stat_usage, options);
  982. /* no_aggr, cgroup are for system-wide only */
  983. if ((no_aggr || nr_cgroups) && !perf_target__has_cpu(&target)) {
  984. fprintf(stderr, "both cgroup and no-aggregation "
  985. "modes only available in system-wide mode\n");
  986. usage_with_options(stat_usage, options);
  987. }
  988. if (add_default_attributes())
  989. goto out;
  990. perf_target__validate(&target);
  991. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  992. if (perf_target__has_task(&target))
  993. pr_err("Problems finding threads of monitor\n");
  994. if (perf_target__has_cpu(&target))
  995. perror("failed to parse CPUs map");
  996. usage_with_options(stat_usage, options);
  997. return -1;
  998. }
  999. list_for_each_entry(pos, &evsel_list->entries, node) {
  1000. if (perf_evsel__alloc_stat_priv(pos) < 0 ||
  1001. perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0)
  1002. goto out_free_fd;
  1003. }
  1004. /*
  1005. * We dont want to block the signals - that would cause
  1006. * child tasks to inherit that and Ctrl-C would not work.
  1007. * What we want is for Ctrl-C to work in the exec()-ed
  1008. * task, but being ignored by perf stat itself:
  1009. */
  1010. atexit(sig_atexit);
  1011. signal(SIGINT, skip_signal);
  1012. signal(SIGALRM, skip_signal);
  1013. signal(SIGABRT, skip_signal);
  1014. status = 0;
  1015. for (run_idx = 0; run_idx < run_count; run_idx++) {
  1016. if (run_count != 1 && verbose)
  1017. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1018. run_idx + 1);
  1019. if (sync_run)
  1020. sync();
  1021. status = run_perf_stat(argc, argv);
  1022. }
  1023. if (status != -1)
  1024. print_stat(argc, argv);
  1025. out_free_fd:
  1026. list_for_each_entry(pos, &evsel_list->entries, node)
  1027. perf_evsel__free_stat_priv(pos);
  1028. perf_evlist__delete_maps(evsel_list);
  1029. out:
  1030. perf_evlist__delete(evsel_list);
  1031. return status;
  1032. }