builtin-stat.c 34 KB

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