builtin-stat.c 34 KB

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