builtin-stat.c 33 KB

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