builtin-stat.c 33 KB

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