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