builtin-stat.c 34 KB

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