builtin-stat.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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/pmu.h"
  43. #include "util/event.h"
  44. #include "util/evlist.h"
  45. #include "util/evsel.h"
  46. #include "util/debug.h"
  47. #include "util/color.h"
  48. #include "util/stat.h"
  49. #include "util/header.h"
  50. #include "util/cpumap.h"
  51. #include "util/thread.h"
  52. #include "util/thread_map.h"
  53. #include <stdlib.h>
  54. #include <sys/prctl.h>
  55. #include <locale.h>
  56. #define DEFAULT_SEPARATOR " "
  57. #define CNTR_NOT_SUPPORTED "<not supported>"
  58. #define CNTR_NOT_COUNTED "<not counted>"
  59. static void print_stat(int argc, const char **argv);
  60. static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
  61. static void print_counter(struct perf_evsel *counter, char *prefix);
  62. static void print_aggr(char *prefix);
  63. /* Default events used for perf stat -T */
  64. static const char * const transaction_attrs[] = {
  65. "task-clock",
  66. "{"
  67. "instructions,"
  68. "cycles,"
  69. "cpu/cycles-t/,"
  70. "cpu/tx-start/,"
  71. "cpu/el-start/,"
  72. "cpu/cycles-ct/"
  73. "}"
  74. };
  75. /* More limited version when the CPU does not have all events. */
  76. static const char * const transaction_limited_attrs[] = {
  77. "task-clock",
  78. "{"
  79. "instructions,"
  80. "cycles,"
  81. "cpu/cycles-t/,"
  82. "cpu/tx-start/"
  83. "}"
  84. };
  85. /* must match transaction_attrs and the beginning limited_attrs */
  86. enum {
  87. T_TASK_CLOCK,
  88. T_INSTRUCTIONS,
  89. T_CYCLES,
  90. T_CYCLES_IN_TX,
  91. T_TRANSACTION_START,
  92. T_ELISION_START,
  93. T_CYCLES_IN_TX_CP,
  94. };
  95. static struct perf_evlist *evsel_list;
  96. static struct target target = {
  97. .uid = UINT_MAX,
  98. };
  99. enum aggr_mode {
  100. AGGR_NONE,
  101. AGGR_GLOBAL,
  102. AGGR_SOCKET,
  103. AGGR_CORE,
  104. };
  105. static int run_count = 1;
  106. static bool no_inherit = false;
  107. static bool scale = true;
  108. static enum aggr_mode aggr_mode = AGGR_GLOBAL;
  109. static volatile pid_t child_pid = -1;
  110. static bool null_run = false;
  111. static int detailed_run = 0;
  112. static bool transaction_run;
  113. static bool big_num = true;
  114. static int big_num_opt = -1;
  115. static const char *csv_sep = NULL;
  116. static bool csv_output = false;
  117. static bool group = false;
  118. static FILE *output = NULL;
  119. static const char *pre_cmd = NULL;
  120. static const char *post_cmd = NULL;
  121. static bool sync_run = false;
  122. static unsigned int interval = 0;
  123. static unsigned int initial_delay = 0;
  124. static unsigned int unit_width = 4; /* strlen("unit") */
  125. static bool forever = false;
  126. static struct timespec ref_time;
  127. static struct cpu_map *aggr_map;
  128. static int (*aggr_get_id)(struct cpu_map *m, int cpu);
  129. static volatile int done = 0;
  130. struct perf_stat {
  131. struct stats res_stats[3];
  132. };
  133. static inline void diff_timespec(struct timespec *r, struct timespec *a,
  134. struct timespec *b)
  135. {
  136. r->tv_sec = a->tv_sec - b->tv_sec;
  137. if (a->tv_nsec < b->tv_nsec) {
  138. r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
  139. r->tv_sec--;
  140. } else {
  141. r->tv_nsec = a->tv_nsec - b->tv_nsec ;
  142. }
  143. }
  144. static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
  145. {
  146. return (evsel->cpus && !target.cpu_list) ? evsel->cpus : evsel_list->cpus;
  147. }
  148. static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
  149. {
  150. return perf_evsel__cpus(evsel)->nr;
  151. }
  152. static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
  153. {
  154. int i;
  155. struct perf_stat *ps = evsel->priv;
  156. for (i = 0; i < 3; i++)
  157. init_stats(&ps->res_stats[i]);
  158. }
  159. static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
  160. {
  161. evsel->priv = zalloc(sizeof(struct perf_stat));
  162. if (evsel == NULL)
  163. return -ENOMEM;
  164. perf_evsel__reset_stat_priv(evsel);
  165. return 0;
  166. }
  167. static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
  168. {
  169. zfree(&evsel->priv);
  170. }
  171. static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
  172. {
  173. void *addr;
  174. size_t sz;
  175. sz = sizeof(*evsel->counts) +
  176. (perf_evsel__nr_cpus(evsel) * sizeof(struct perf_counts_values));
  177. addr = zalloc(sz);
  178. if (!addr)
  179. return -ENOMEM;
  180. evsel->prev_raw_counts = addr;
  181. return 0;
  182. }
  183. static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
  184. {
  185. zfree(&evsel->prev_raw_counts);
  186. }
  187. static void perf_evlist__free_stats(struct perf_evlist *evlist)
  188. {
  189. struct perf_evsel *evsel;
  190. evlist__for_each(evlist, evsel) {
  191. perf_evsel__free_stat_priv(evsel);
  192. perf_evsel__free_counts(evsel);
  193. perf_evsel__free_prev_raw_counts(evsel);
  194. }
  195. }
  196. static int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
  197. {
  198. struct perf_evsel *evsel;
  199. evlist__for_each(evlist, evsel) {
  200. if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
  201. perf_evsel__alloc_counts(evsel, perf_evsel__nr_cpus(evsel)) < 0 ||
  202. (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel) < 0))
  203. goto out_free;
  204. }
  205. return 0;
  206. out_free:
  207. perf_evlist__free_stats(evlist);
  208. return -1;
  209. }
  210. static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  211. static struct stats runtime_cycles_stats[MAX_NR_CPUS];
  212. static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
  213. static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
  214. static struct stats runtime_branches_stats[MAX_NR_CPUS];
  215. static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
  216. static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
  217. static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
  218. static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
  219. static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
  220. static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
  221. static struct stats runtime_cycles_in_tx_stats[MAX_NR_CPUS];
  222. static struct stats walltime_nsecs_stats;
  223. static struct stats runtime_transaction_stats[MAX_NR_CPUS];
  224. static struct stats runtime_elision_stats[MAX_NR_CPUS];
  225. static void perf_stat__reset_stats(struct perf_evlist *evlist)
  226. {
  227. struct perf_evsel *evsel;
  228. evlist__for_each(evlist, evsel) {
  229. perf_evsel__reset_stat_priv(evsel);
  230. perf_evsel__reset_counts(evsel, perf_evsel__nr_cpus(evsel));
  231. }
  232. memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
  233. memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
  234. memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
  235. memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
  236. memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
  237. memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
  238. memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
  239. memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
  240. memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
  241. memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
  242. memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
  243. memset(runtime_cycles_in_tx_stats, 0,
  244. sizeof(runtime_cycles_in_tx_stats));
  245. memset(runtime_transaction_stats, 0,
  246. sizeof(runtime_transaction_stats));
  247. memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
  248. memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
  249. }
  250. static int create_perf_stat_counter(struct perf_evsel *evsel)
  251. {
  252. struct perf_event_attr *attr = &evsel->attr;
  253. if (scale)
  254. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  255. PERF_FORMAT_TOTAL_TIME_RUNNING;
  256. attr->inherit = !no_inherit;
  257. if (target__has_cpu(&target))
  258. return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
  259. if (!target__has_task(&target) && perf_evsel__is_group_leader(evsel)) {
  260. attr->disabled = 1;
  261. if (!initial_delay)
  262. attr->enable_on_exec = 1;
  263. }
  264. return perf_evsel__open_per_thread(evsel, evsel_list->threads);
  265. }
  266. /*
  267. * Does the counter have nsecs as a unit?
  268. */
  269. static inline int nsec_counter(struct perf_evsel *evsel)
  270. {
  271. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  272. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  273. return 1;
  274. return 0;
  275. }
  276. static struct perf_evsel *nth_evsel(int n)
  277. {
  278. static struct perf_evsel **array;
  279. static int array_len;
  280. struct perf_evsel *ev;
  281. int j;
  282. /* Assumes this only called when evsel_list does not change anymore. */
  283. if (!array) {
  284. evlist__for_each(evsel_list, ev)
  285. array_len++;
  286. array = malloc(array_len * sizeof(void *));
  287. if (!array)
  288. exit(ENOMEM);
  289. j = 0;
  290. evlist__for_each(evsel_list, ev)
  291. array[j++] = ev;
  292. }
  293. if (n < array_len)
  294. return array[n];
  295. return NULL;
  296. }
  297. /*
  298. * Update various tracking values we maintain to print
  299. * more semantic information such as miss/hit ratios,
  300. * instruction rates, etc:
  301. */
  302. static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
  303. {
  304. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
  305. update_stats(&runtime_nsecs_stats[0], count[0]);
  306. else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
  307. update_stats(&runtime_cycles_stats[0], count[0]);
  308. else if (transaction_run &&
  309. perf_evsel__cmp(counter, nth_evsel(T_CYCLES_IN_TX)))
  310. update_stats(&runtime_cycles_in_tx_stats[0], count[0]);
  311. else if (transaction_run &&
  312. perf_evsel__cmp(counter, nth_evsel(T_TRANSACTION_START)))
  313. update_stats(&runtime_transaction_stats[0], count[0]);
  314. else if (transaction_run &&
  315. perf_evsel__cmp(counter, nth_evsel(T_ELISION_START)))
  316. update_stats(&runtime_elision_stats[0], count[0]);
  317. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
  318. update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
  319. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
  320. update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
  321. else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
  322. update_stats(&runtime_branches_stats[0], count[0]);
  323. else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
  324. update_stats(&runtime_cacherefs_stats[0], count[0]);
  325. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
  326. update_stats(&runtime_l1_dcache_stats[0], count[0]);
  327. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
  328. update_stats(&runtime_l1_icache_stats[0], count[0]);
  329. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
  330. update_stats(&runtime_ll_cache_stats[0], count[0]);
  331. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
  332. update_stats(&runtime_dtlb_cache_stats[0], count[0]);
  333. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
  334. update_stats(&runtime_itlb_cache_stats[0], count[0]);
  335. }
  336. /*
  337. * Read out the results of a single counter:
  338. * aggregate counts across CPUs in system-wide mode
  339. */
  340. static int read_counter_aggr(struct perf_evsel *counter)
  341. {
  342. struct perf_stat *ps = counter->priv;
  343. u64 *count = counter->counts->aggr.values;
  344. int i;
  345. if (__perf_evsel__read(counter, perf_evsel__nr_cpus(counter),
  346. thread_map__nr(evsel_list->threads), scale) < 0)
  347. return -1;
  348. for (i = 0; i < 3; i++)
  349. update_stats(&ps->res_stats[i], count[i]);
  350. if (verbose) {
  351. fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  352. perf_evsel__name(counter), count[0], count[1], count[2]);
  353. }
  354. /*
  355. * Save the full runtime - to allow normalization during printout:
  356. */
  357. update_shadow_stats(counter, count);
  358. return 0;
  359. }
  360. /*
  361. * Read out the results of a single counter:
  362. * do not aggregate counts across CPUs in system-wide mode
  363. */
  364. static int read_counter(struct perf_evsel *counter)
  365. {
  366. u64 *count;
  367. int cpu;
  368. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  369. if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
  370. return -1;
  371. count = counter->counts->cpu[cpu].values;
  372. update_shadow_stats(counter, count);
  373. }
  374. return 0;
  375. }
  376. static void print_interval(void)
  377. {
  378. static int num_print_interval;
  379. struct perf_evsel *counter;
  380. struct perf_stat *ps;
  381. struct timespec ts, rs;
  382. char prefix[64];
  383. if (aggr_mode == AGGR_GLOBAL) {
  384. evlist__for_each(evsel_list, counter) {
  385. ps = counter->priv;
  386. memset(ps->res_stats, 0, sizeof(ps->res_stats));
  387. read_counter_aggr(counter);
  388. }
  389. } else {
  390. evlist__for_each(evsel_list, counter) {
  391. ps = counter->priv;
  392. memset(ps->res_stats, 0, sizeof(ps->res_stats));
  393. read_counter(counter);
  394. }
  395. }
  396. clock_gettime(CLOCK_MONOTONIC, &ts);
  397. diff_timespec(&rs, &ts, &ref_time);
  398. sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
  399. if (num_print_interval == 0 && !csv_output) {
  400. switch (aggr_mode) {
  401. case AGGR_SOCKET:
  402. fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
  403. break;
  404. case AGGR_CORE:
  405. fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
  406. break;
  407. case AGGR_NONE:
  408. fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
  409. break;
  410. case AGGR_GLOBAL:
  411. default:
  412. fprintf(output, "# time counts %*s events\n", unit_width, "unit");
  413. }
  414. }
  415. if (++num_print_interval == 25)
  416. num_print_interval = 0;
  417. switch (aggr_mode) {
  418. case AGGR_CORE:
  419. case AGGR_SOCKET:
  420. print_aggr(prefix);
  421. break;
  422. case AGGR_NONE:
  423. evlist__for_each(evsel_list, counter)
  424. print_counter(counter, prefix);
  425. break;
  426. case AGGR_GLOBAL:
  427. default:
  428. evlist__for_each(evsel_list, counter)
  429. print_counter_aggr(counter, prefix);
  430. }
  431. fflush(output);
  432. }
  433. static void handle_initial_delay(void)
  434. {
  435. struct perf_evsel *counter;
  436. if (initial_delay) {
  437. const int ncpus = cpu_map__nr(evsel_list->cpus),
  438. nthreads = thread_map__nr(evsel_list->threads);
  439. usleep(initial_delay * 1000);
  440. evlist__for_each(evsel_list, counter)
  441. perf_evsel__enable(counter, ncpus, nthreads);
  442. }
  443. }
  444. static volatile int workload_exec_errno;
  445. /*
  446. * perf_evlist__prepare_workload will send a SIGUSR1
  447. * if the fork fails, since we asked by setting its
  448. * want_signal to true.
  449. */
  450. static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
  451. void *ucontext __maybe_unused)
  452. {
  453. workload_exec_errno = info->si_value.sival_int;
  454. }
  455. static int __run_perf_stat(int argc, const char **argv)
  456. {
  457. char msg[512];
  458. unsigned long long t0, t1;
  459. struct perf_evsel *counter;
  460. struct timespec ts;
  461. size_t l;
  462. int status = 0;
  463. const bool forks = (argc > 0);
  464. if (interval) {
  465. ts.tv_sec = interval / 1000;
  466. ts.tv_nsec = (interval % 1000) * 1000000;
  467. } else {
  468. ts.tv_sec = 1;
  469. ts.tv_nsec = 0;
  470. }
  471. if (forks) {
  472. if (perf_evlist__prepare_workload(evsel_list, &target, argv, false,
  473. workload_exec_failed_signal) < 0) {
  474. perror("failed to prepare workload");
  475. return -1;
  476. }
  477. child_pid = evsel_list->workload.pid;
  478. }
  479. if (group)
  480. perf_evlist__set_leader(evsel_list);
  481. evlist__for_each(evsel_list, counter) {
  482. if (create_perf_stat_counter(counter) < 0) {
  483. /*
  484. * PPC returns ENXIO for HW counters until 2.6.37
  485. * (behavior changed with commit b0a873e).
  486. */
  487. if (errno == EINVAL || errno == ENOSYS ||
  488. errno == ENOENT || errno == EOPNOTSUPP ||
  489. errno == ENXIO) {
  490. if (verbose)
  491. ui__warning("%s event is not supported by the kernel.\n",
  492. perf_evsel__name(counter));
  493. counter->supported = false;
  494. continue;
  495. }
  496. perf_evsel__open_strerror(counter, &target,
  497. errno, msg, sizeof(msg));
  498. ui__error("%s\n", msg);
  499. if (child_pid != -1)
  500. kill(child_pid, SIGTERM);
  501. return -1;
  502. }
  503. counter->supported = true;
  504. l = strlen(counter->unit);
  505. if (l > unit_width)
  506. unit_width = l;
  507. }
  508. if (perf_evlist__apply_filters(evsel_list)) {
  509. error("failed to set filter with %d (%s)\n", errno,
  510. strerror(errno));
  511. return -1;
  512. }
  513. /*
  514. * Enable counters and exec the command:
  515. */
  516. t0 = rdclock();
  517. clock_gettime(CLOCK_MONOTONIC, &ref_time);
  518. if (forks) {
  519. perf_evlist__start_workload(evsel_list);
  520. handle_initial_delay();
  521. if (interval) {
  522. while (!waitpid(child_pid, &status, WNOHANG)) {
  523. nanosleep(&ts, NULL);
  524. print_interval();
  525. }
  526. }
  527. wait(&status);
  528. if (workload_exec_errno) {
  529. const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
  530. pr_err("Workload failed: %s\n", emsg);
  531. return -1;
  532. }
  533. if (WIFSIGNALED(status))
  534. psignal(WTERMSIG(status), argv[0]);
  535. } else {
  536. handle_initial_delay();
  537. while (!done) {
  538. nanosleep(&ts, NULL);
  539. if (interval)
  540. print_interval();
  541. }
  542. }
  543. t1 = rdclock();
  544. update_stats(&walltime_nsecs_stats, t1 - t0);
  545. if (aggr_mode == AGGR_GLOBAL) {
  546. evlist__for_each(evsel_list, counter) {
  547. read_counter_aggr(counter);
  548. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
  549. thread_map__nr(evsel_list->threads));
  550. }
  551. } else {
  552. evlist__for_each(evsel_list, counter) {
  553. read_counter(counter);
  554. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
  555. }
  556. }
  557. return WEXITSTATUS(status);
  558. }
  559. static int run_perf_stat(int argc, const char **argv)
  560. {
  561. int ret;
  562. if (pre_cmd) {
  563. ret = system(pre_cmd);
  564. if (ret)
  565. return ret;
  566. }
  567. if (sync_run)
  568. sync();
  569. ret = __run_perf_stat(argc, argv);
  570. if (ret)
  571. return ret;
  572. if (post_cmd) {
  573. ret = system(post_cmd);
  574. if (ret)
  575. return ret;
  576. }
  577. return ret;
  578. }
  579. static void print_noise_pct(double total, double avg)
  580. {
  581. double pct = rel_stddev_stats(total, avg);
  582. if (csv_output)
  583. fprintf(output, "%s%.2f%%", csv_sep, pct);
  584. else if (pct)
  585. fprintf(output, " ( +-%6.2f%% )", pct);
  586. }
  587. static void print_noise(struct perf_evsel *evsel, double avg)
  588. {
  589. struct perf_stat *ps;
  590. if (run_count == 1)
  591. return;
  592. ps = evsel->priv;
  593. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  594. }
  595. static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
  596. {
  597. switch (aggr_mode) {
  598. case AGGR_CORE:
  599. fprintf(output, "S%d-C%*d%s%*d%s",
  600. cpu_map__id_to_socket(id),
  601. csv_output ? 0 : -8,
  602. cpu_map__id_to_cpu(id),
  603. csv_sep,
  604. csv_output ? 0 : 4,
  605. nr,
  606. csv_sep);
  607. break;
  608. case AGGR_SOCKET:
  609. fprintf(output, "S%*d%s%*d%s",
  610. csv_output ? 0 : -5,
  611. id,
  612. csv_sep,
  613. csv_output ? 0 : 4,
  614. nr,
  615. csv_sep);
  616. break;
  617. case AGGR_NONE:
  618. fprintf(output, "CPU%*d%s",
  619. csv_output ? 0 : -4,
  620. perf_evsel__cpus(evsel)->map[id], csv_sep);
  621. break;
  622. case AGGR_GLOBAL:
  623. default:
  624. break;
  625. }
  626. }
  627. static void nsec_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
  628. {
  629. double msecs = avg / 1e6;
  630. const char *fmt_v, *fmt_n;
  631. char name[25];
  632. fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
  633. fmt_n = csv_output ? "%s" : "%-25s";
  634. aggr_printout(evsel, cpu, nr);
  635. scnprintf(name, sizeof(name), "%s%s",
  636. perf_evsel__name(evsel), csv_output ? "" : " (msec)");
  637. fprintf(output, fmt_v, msecs, csv_sep);
  638. if (csv_output)
  639. fprintf(output, "%s%s", evsel->unit, csv_sep);
  640. else
  641. fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
  642. fprintf(output, fmt_n, name);
  643. if (evsel->cgrp)
  644. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  645. if (csv_output || interval)
  646. return;
  647. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  648. fprintf(output, " # %8.3f CPUs utilized ",
  649. avg / avg_stats(&walltime_nsecs_stats));
  650. else
  651. fprintf(output, " ");
  652. }
  653. /* used for get_ratio_color() */
  654. enum grc_type {
  655. GRC_STALLED_CYCLES_FE,
  656. GRC_STALLED_CYCLES_BE,
  657. GRC_CACHE_MISSES,
  658. GRC_MAX_NR
  659. };
  660. static const char *get_ratio_color(enum grc_type type, double ratio)
  661. {
  662. static const double grc_table[GRC_MAX_NR][3] = {
  663. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  664. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  665. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  666. };
  667. const char *color = PERF_COLOR_NORMAL;
  668. if (ratio > grc_table[type][0])
  669. color = PERF_COLOR_RED;
  670. else if (ratio > grc_table[type][1])
  671. color = PERF_COLOR_MAGENTA;
  672. else if (ratio > grc_table[type][2])
  673. color = PERF_COLOR_YELLOW;
  674. return color;
  675. }
  676. static void print_stalled_cycles_frontend(int cpu,
  677. struct perf_evsel *evsel
  678. __maybe_unused, double avg)
  679. {
  680. double total, ratio = 0.0;
  681. const char *color;
  682. total = avg_stats(&runtime_cycles_stats[cpu]);
  683. if (total)
  684. ratio = avg / total * 100.0;
  685. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  686. fprintf(output, " # ");
  687. color_fprintf(output, color, "%6.2f%%", ratio);
  688. fprintf(output, " frontend cycles idle ");
  689. }
  690. static void print_stalled_cycles_backend(int cpu,
  691. struct perf_evsel *evsel
  692. __maybe_unused, double avg)
  693. {
  694. double total, ratio = 0.0;
  695. const char *color;
  696. total = avg_stats(&runtime_cycles_stats[cpu]);
  697. if (total)
  698. ratio = avg / total * 100.0;
  699. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  700. fprintf(output, " # ");
  701. color_fprintf(output, color, "%6.2f%%", ratio);
  702. fprintf(output, " backend cycles idle ");
  703. }
  704. static void print_branch_misses(int cpu,
  705. struct perf_evsel *evsel __maybe_unused,
  706. double avg)
  707. {
  708. double total, ratio = 0.0;
  709. const char *color;
  710. total = avg_stats(&runtime_branches_stats[cpu]);
  711. if (total)
  712. ratio = avg / total * 100.0;
  713. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  714. fprintf(output, " # ");
  715. color_fprintf(output, color, "%6.2f%%", ratio);
  716. fprintf(output, " of all branches ");
  717. }
  718. static void print_l1_dcache_misses(int cpu,
  719. struct perf_evsel *evsel __maybe_unused,
  720. double avg)
  721. {
  722. double total, ratio = 0.0;
  723. const char *color;
  724. total = avg_stats(&runtime_l1_dcache_stats[cpu]);
  725. if (total)
  726. ratio = avg / total * 100.0;
  727. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  728. fprintf(output, " # ");
  729. color_fprintf(output, color, "%6.2f%%", ratio);
  730. fprintf(output, " of all L1-dcache hits ");
  731. }
  732. static void print_l1_icache_misses(int cpu,
  733. struct perf_evsel *evsel __maybe_unused,
  734. double avg)
  735. {
  736. double total, ratio = 0.0;
  737. const char *color;
  738. total = avg_stats(&runtime_l1_icache_stats[cpu]);
  739. if (total)
  740. ratio = avg / total * 100.0;
  741. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  742. fprintf(output, " # ");
  743. color_fprintf(output, color, "%6.2f%%", ratio);
  744. fprintf(output, " of all L1-icache hits ");
  745. }
  746. static void print_dtlb_cache_misses(int cpu,
  747. struct perf_evsel *evsel __maybe_unused,
  748. double avg)
  749. {
  750. double total, ratio = 0.0;
  751. const char *color;
  752. total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
  753. if (total)
  754. ratio = avg / total * 100.0;
  755. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  756. fprintf(output, " # ");
  757. color_fprintf(output, color, "%6.2f%%", ratio);
  758. fprintf(output, " of all dTLB cache hits ");
  759. }
  760. static void print_itlb_cache_misses(int cpu,
  761. struct perf_evsel *evsel __maybe_unused,
  762. double avg)
  763. {
  764. double total, ratio = 0.0;
  765. const char *color;
  766. total = avg_stats(&runtime_itlb_cache_stats[cpu]);
  767. if (total)
  768. ratio = avg / total * 100.0;
  769. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  770. fprintf(output, " # ");
  771. color_fprintf(output, color, "%6.2f%%", ratio);
  772. fprintf(output, " of all iTLB cache hits ");
  773. }
  774. static void print_ll_cache_misses(int cpu,
  775. struct perf_evsel *evsel __maybe_unused,
  776. double avg)
  777. {
  778. double total, ratio = 0.0;
  779. const char *color;
  780. total = avg_stats(&runtime_ll_cache_stats[cpu]);
  781. if (total)
  782. ratio = avg / total * 100.0;
  783. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  784. fprintf(output, " # ");
  785. color_fprintf(output, color, "%6.2f%%", ratio);
  786. fprintf(output, " of all LL-cache hits ");
  787. }
  788. static void abs_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
  789. {
  790. double total, ratio = 0.0, total2;
  791. double sc = evsel->scale;
  792. const char *fmt;
  793. if (csv_output) {
  794. fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
  795. } else {
  796. if (big_num)
  797. fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
  798. else
  799. fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
  800. }
  801. aggr_printout(evsel, cpu, nr);
  802. if (aggr_mode == AGGR_GLOBAL)
  803. cpu = 0;
  804. fprintf(output, fmt, avg, csv_sep);
  805. if (evsel->unit)
  806. fprintf(output, "%-*s%s",
  807. csv_output ? 0 : unit_width,
  808. evsel->unit, csv_sep);
  809. fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
  810. if (evsel->cgrp)
  811. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  812. if (csv_output || interval)
  813. return;
  814. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  815. total = avg_stats(&runtime_cycles_stats[cpu]);
  816. if (total) {
  817. ratio = avg / total;
  818. fprintf(output, " # %5.2f insns per cycle ", ratio);
  819. }
  820. total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
  821. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
  822. if (total && avg) {
  823. ratio = total / avg;
  824. fprintf(output, "\n");
  825. if (aggr_mode == AGGR_NONE)
  826. fprintf(output, " ");
  827. fprintf(output, " # %5.2f stalled cycles per insn", ratio);
  828. }
  829. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  830. runtime_branches_stats[cpu].n != 0) {
  831. print_branch_misses(cpu, evsel, avg);
  832. } else if (
  833. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  834. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  835. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  836. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  837. runtime_l1_dcache_stats[cpu].n != 0) {
  838. print_l1_dcache_misses(cpu, evsel, avg);
  839. } else if (
  840. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  841. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  842. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  843. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  844. runtime_l1_icache_stats[cpu].n != 0) {
  845. print_l1_icache_misses(cpu, evsel, avg);
  846. } else if (
  847. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  848. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  849. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  850. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  851. runtime_dtlb_cache_stats[cpu].n != 0) {
  852. print_dtlb_cache_misses(cpu, evsel, avg);
  853. } else if (
  854. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  855. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  856. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  857. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  858. runtime_itlb_cache_stats[cpu].n != 0) {
  859. print_itlb_cache_misses(cpu, evsel, avg);
  860. } else if (
  861. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  862. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  863. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  864. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  865. runtime_ll_cache_stats[cpu].n != 0) {
  866. print_ll_cache_misses(cpu, evsel, avg);
  867. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
  868. runtime_cacherefs_stats[cpu].n != 0) {
  869. total = avg_stats(&runtime_cacherefs_stats[cpu]);
  870. if (total)
  871. ratio = avg * 100 / total;
  872. fprintf(output, " # %8.3f %% of all cache refs ", ratio);
  873. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  874. print_stalled_cycles_frontend(cpu, evsel, avg);
  875. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  876. print_stalled_cycles_backend(cpu, evsel, avg);
  877. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  878. total = avg_stats(&runtime_nsecs_stats[cpu]);
  879. if (total) {
  880. ratio = avg / total;
  881. fprintf(output, " # %8.3f GHz ", ratio);
  882. }
  883. } else if (transaction_run &&
  884. perf_evsel__cmp(evsel, nth_evsel(T_CYCLES_IN_TX))) {
  885. total = avg_stats(&runtime_cycles_stats[cpu]);
  886. if (total)
  887. fprintf(output,
  888. " # %5.2f%% transactional cycles ",
  889. 100.0 * (avg / total));
  890. } else if (transaction_run &&
  891. perf_evsel__cmp(evsel, nth_evsel(T_CYCLES_IN_TX_CP))) {
  892. total = avg_stats(&runtime_cycles_stats[cpu]);
  893. total2 = avg_stats(&runtime_cycles_in_tx_stats[cpu]);
  894. if (total2 < avg)
  895. total2 = avg;
  896. if (total)
  897. fprintf(output,
  898. " # %5.2f%% aborted cycles ",
  899. 100.0 * ((total2-avg) / total));
  900. } else if (transaction_run &&
  901. perf_evsel__cmp(evsel, nth_evsel(T_TRANSACTION_START)) &&
  902. avg > 0 &&
  903. runtime_cycles_in_tx_stats[cpu].n != 0) {
  904. total = avg_stats(&runtime_cycles_in_tx_stats[cpu]);
  905. if (total)
  906. ratio = total / avg;
  907. fprintf(output, " # %8.0f cycles / transaction ", ratio);
  908. } else if (transaction_run &&
  909. perf_evsel__cmp(evsel, nth_evsel(T_ELISION_START)) &&
  910. avg > 0 &&
  911. runtime_cycles_in_tx_stats[cpu].n != 0) {
  912. total = avg_stats(&runtime_cycles_in_tx_stats[cpu]);
  913. if (total)
  914. ratio = total / avg;
  915. fprintf(output, " # %8.0f cycles / elision ", ratio);
  916. } else if (runtime_nsecs_stats[cpu].n != 0) {
  917. char unit = 'M';
  918. total = avg_stats(&runtime_nsecs_stats[cpu]);
  919. if (total)
  920. ratio = 1000.0 * avg / total;
  921. if (ratio < 0.001) {
  922. ratio *= 1000;
  923. unit = 'K';
  924. }
  925. fprintf(output, " # %8.3f %c/sec ", ratio, unit);
  926. } else {
  927. fprintf(output, " ");
  928. }
  929. }
  930. static void print_aggr(char *prefix)
  931. {
  932. struct perf_evsel *counter;
  933. int cpu, cpu2, s, s2, id, nr;
  934. double uval;
  935. u64 ena, run, val;
  936. if (!(aggr_map || aggr_get_id))
  937. return;
  938. for (s = 0; s < aggr_map->nr; s++) {
  939. id = aggr_map->map[s];
  940. evlist__for_each(evsel_list, counter) {
  941. val = ena = run = 0;
  942. nr = 0;
  943. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  944. cpu2 = perf_evsel__cpus(counter)->map[cpu];
  945. s2 = aggr_get_id(evsel_list->cpus, cpu2);
  946. if (s2 != id)
  947. continue;
  948. val += counter->counts->cpu[cpu].val;
  949. ena += counter->counts->cpu[cpu].ena;
  950. run += counter->counts->cpu[cpu].run;
  951. nr++;
  952. }
  953. if (prefix)
  954. fprintf(output, "%s", prefix);
  955. if (run == 0 || ena == 0) {
  956. aggr_printout(counter, id, nr);
  957. fprintf(output, "%*s%s",
  958. csv_output ? 0 : 18,
  959. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  960. csv_sep);
  961. fprintf(output, "%-*s%s",
  962. csv_output ? 0 : unit_width,
  963. counter->unit, csv_sep);
  964. fprintf(output, "%*s",
  965. csv_output ? 0 : -25,
  966. perf_evsel__name(counter));
  967. if (counter->cgrp)
  968. fprintf(output, "%s%s",
  969. csv_sep, counter->cgrp->name);
  970. fputc('\n', output);
  971. continue;
  972. }
  973. uval = val * counter->scale;
  974. if (nsec_counter(counter))
  975. nsec_printout(id, nr, counter, uval);
  976. else
  977. abs_printout(id, nr, counter, uval);
  978. if (!csv_output) {
  979. print_noise(counter, 1.0);
  980. if (run != ena)
  981. fprintf(output, " (%.2f%%)",
  982. 100.0 * run / ena);
  983. }
  984. fputc('\n', output);
  985. }
  986. }
  987. }
  988. /*
  989. * Print out the results of a single counter:
  990. * aggregated counts in system-wide mode
  991. */
  992. static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
  993. {
  994. struct perf_stat *ps = counter->priv;
  995. double avg = avg_stats(&ps->res_stats[0]);
  996. int scaled = counter->counts->scaled;
  997. double uval;
  998. if (prefix)
  999. fprintf(output, "%s", prefix);
  1000. if (scaled == -1) {
  1001. fprintf(output, "%*s%s",
  1002. csv_output ? 0 : 18,
  1003. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  1004. csv_sep);
  1005. fprintf(output, "%-*s%s",
  1006. csv_output ? 0 : unit_width,
  1007. counter->unit, csv_sep);
  1008. fprintf(output, "%*s",
  1009. csv_output ? 0 : -25,
  1010. perf_evsel__name(counter));
  1011. if (counter->cgrp)
  1012. fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
  1013. fputc('\n', output);
  1014. return;
  1015. }
  1016. uval = avg * counter->scale;
  1017. if (nsec_counter(counter))
  1018. nsec_printout(-1, 0, counter, uval);
  1019. else
  1020. abs_printout(-1, 0, counter, uval);
  1021. print_noise(counter, avg);
  1022. if (csv_output) {
  1023. fputc('\n', output);
  1024. return;
  1025. }
  1026. if (scaled) {
  1027. double avg_enabled, avg_running;
  1028. avg_enabled = avg_stats(&ps->res_stats[1]);
  1029. avg_running = avg_stats(&ps->res_stats[2]);
  1030. fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
  1031. }
  1032. fprintf(output, "\n");
  1033. }
  1034. /*
  1035. * Print out the results of a single counter:
  1036. * does not use aggregated count in system-wide
  1037. */
  1038. static void print_counter(struct perf_evsel *counter, char *prefix)
  1039. {
  1040. u64 ena, run, val;
  1041. double uval;
  1042. int cpu;
  1043. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  1044. val = counter->counts->cpu[cpu].val;
  1045. ena = counter->counts->cpu[cpu].ena;
  1046. run = counter->counts->cpu[cpu].run;
  1047. if (prefix)
  1048. fprintf(output, "%s", prefix);
  1049. if (run == 0 || ena == 0) {
  1050. fprintf(output, "CPU%*d%s%*s%s",
  1051. csv_output ? 0 : -4,
  1052. perf_evsel__cpus(counter)->map[cpu], csv_sep,
  1053. csv_output ? 0 : 18,
  1054. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  1055. csv_sep);
  1056. fprintf(output, "%-*s%s",
  1057. csv_output ? 0 : unit_width,
  1058. counter->unit, csv_sep);
  1059. fprintf(output, "%*s",
  1060. csv_output ? 0 : -25,
  1061. perf_evsel__name(counter));
  1062. if (counter->cgrp)
  1063. fprintf(output, "%s%s",
  1064. csv_sep, counter->cgrp->name);
  1065. fputc('\n', output);
  1066. continue;
  1067. }
  1068. uval = val * counter->scale;
  1069. if (nsec_counter(counter))
  1070. nsec_printout(cpu, 0, counter, uval);
  1071. else
  1072. abs_printout(cpu, 0, counter, uval);
  1073. if (!csv_output) {
  1074. print_noise(counter, 1.0);
  1075. if (run != ena)
  1076. fprintf(output, " (%.2f%%)",
  1077. 100.0 * run / ena);
  1078. }
  1079. fputc('\n', output);
  1080. }
  1081. }
  1082. static void print_stat(int argc, const char **argv)
  1083. {
  1084. struct perf_evsel *counter;
  1085. int i;
  1086. fflush(stdout);
  1087. if (!csv_output) {
  1088. fprintf(output, "\n");
  1089. fprintf(output, " Performance counter stats for ");
  1090. if (target.system_wide)
  1091. fprintf(output, "\'system wide");
  1092. else if (target.cpu_list)
  1093. fprintf(output, "\'CPU(s) %s", target.cpu_list);
  1094. else if (!target__has_task(&target)) {
  1095. fprintf(output, "\'%s", argv[0]);
  1096. for (i = 1; i < argc; i++)
  1097. fprintf(output, " %s", argv[i]);
  1098. } else if (target.pid)
  1099. fprintf(output, "process id \'%s", target.pid);
  1100. else
  1101. fprintf(output, "thread id \'%s", target.tid);
  1102. fprintf(output, "\'");
  1103. if (run_count > 1)
  1104. fprintf(output, " (%d runs)", run_count);
  1105. fprintf(output, ":\n\n");
  1106. }
  1107. switch (aggr_mode) {
  1108. case AGGR_CORE:
  1109. case AGGR_SOCKET:
  1110. print_aggr(NULL);
  1111. break;
  1112. case AGGR_GLOBAL:
  1113. evlist__for_each(evsel_list, counter)
  1114. print_counter_aggr(counter, NULL);
  1115. break;
  1116. case AGGR_NONE:
  1117. evlist__for_each(evsel_list, counter)
  1118. print_counter(counter, NULL);
  1119. break;
  1120. default:
  1121. break;
  1122. }
  1123. if (!csv_output) {
  1124. if (!null_run)
  1125. fprintf(output, "\n");
  1126. fprintf(output, " %17.9f seconds time elapsed",
  1127. avg_stats(&walltime_nsecs_stats)/1e9);
  1128. if (run_count > 1) {
  1129. fprintf(output, " ");
  1130. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  1131. avg_stats(&walltime_nsecs_stats));
  1132. }
  1133. fprintf(output, "\n\n");
  1134. }
  1135. }
  1136. static volatile int signr = -1;
  1137. static void skip_signal(int signo)
  1138. {
  1139. if ((child_pid == -1) || interval)
  1140. done = 1;
  1141. signr = signo;
  1142. /*
  1143. * render child_pid harmless
  1144. * won't send SIGTERM to a random
  1145. * process in case of race condition
  1146. * and fast PID recycling
  1147. */
  1148. child_pid = -1;
  1149. }
  1150. static void sig_atexit(void)
  1151. {
  1152. sigset_t set, oset;
  1153. /*
  1154. * avoid race condition with SIGCHLD handler
  1155. * in skip_signal() which is modifying child_pid
  1156. * goal is to avoid send SIGTERM to a random
  1157. * process
  1158. */
  1159. sigemptyset(&set);
  1160. sigaddset(&set, SIGCHLD);
  1161. sigprocmask(SIG_BLOCK, &set, &oset);
  1162. if (child_pid != -1)
  1163. kill(child_pid, SIGTERM);
  1164. sigprocmask(SIG_SETMASK, &oset, NULL);
  1165. if (signr == -1)
  1166. return;
  1167. signal(signr, SIG_DFL);
  1168. kill(getpid(), signr);
  1169. }
  1170. static int stat__set_big_num(const struct option *opt __maybe_unused,
  1171. const char *s __maybe_unused, int unset)
  1172. {
  1173. big_num_opt = unset ? 0 : 1;
  1174. return 0;
  1175. }
  1176. static int perf_stat_init_aggr_mode(void)
  1177. {
  1178. switch (aggr_mode) {
  1179. case AGGR_SOCKET:
  1180. if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
  1181. perror("cannot build socket map");
  1182. return -1;
  1183. }
  1184. aggr_get_id = cpu_map__get_socket;
  1185. break;
  1186. case AGGR_CORE:
  1187. if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
  1188. perror("cannot build core map");
  1189. return -1;
  1190. }
  1191. aggr_get_id = cpu_map__get_core;
  1192. break;
  1193. case AGGR_NONE:
  1194. case AGGR_GLOBAL:
  1195. default:
  1196. break;
  1197. }
  1198. return 0;
  1199. }
  1200. static int setup_events(const char * const *attrs, unsigned len)
  1201. {
  1202. unsigned i;
  1203. for (i = 0; i < len; i++) {
  1204. if (parse_events(evsel_list, attrs[i]))
  1205. return -1;
  1206. }
  1207. return 0;
  1208. }
  1209. /*
  1210. * Add default attributes, if there were no attributes specified or
  1211. * if -d/--detailed, -d -d or -d -d -d is used:
  1212. */
  1213. static int add_default_attributes(void)
  1214. {
  1215. struct perf_event_attr default_attrs[] = {
  1216. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  1217. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  1218. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  1219. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  1220. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  1221. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  1222. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  1223. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  1224. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  1225. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  1226. };
  1227. /*
  1228. * Detailed stats (-d), covering the L1 and last level data caches:
  1229. */
  1230. struct perf_event_attr detailed_attrs[] = {
  1231. { .type = PERF_TYPE_HW_CACHE,
  1232. .config =
  1233. PERF_COUNT_HW_CACHE_L1D << 0 |
  1234. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1235. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1236. { .type = PERF_TYPE_HW_CACHE,
  1237. .config =
  1238. PERF_COUNT_HW_CACHE_L1D << 0 |
  1239. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1240. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1241. { .type = PERF_TYPE_HW_CACHE,
  1242. .config =
  1243. PERF_COUNT_HW_CACHE_LL << 0 |
  1244. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1245. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1246. { .type = PERF_TYPE_HW_CACHE,
  1247. .config =
  1248. PERF_COUNT_HW_CACHE_LL << 0 |
  1249. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1250. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1251. };
  1252. /*
  1253. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  1254. */
  1255. struct perf_event_attr very_detailed_attrs[] = {
  1256. { .type = PERF_TYPE_HW_CACHE,
  1257. .config =
  1258. PERF_COUNT_HW_CACHE_L1I << 0 |
  1259. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1260. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1261. { .type = PERF_TYPE_HW_CACHE,
  1262. .config =
  1263. PERF_COUNT_HW_CACHE_L1I << 0 |
  1264. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1265. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1266. { .type = PERF_TYPE_HW_CACHE,
  1267. .config =
  1268. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1269. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1270. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1271. { .type = PERF_TYPE_HW_CACHE,
  1272. .config =
  1273. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1274. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1275. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1276. { .type = PERF_TYPE_HW_CACHE,
  1277. .config =
  1278. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1279. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1280. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1281. { .type = PERF_TYPE_HW_CACHE,
  1282. .config =
  1283. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1284. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1285. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1286. };
  1287. /*
  1288. * Very, very detailed stats (-d -d -d), adding prefetch events:
  1289. */
  1290. struct perf_event_attr very_very_detailed_attrs[] = {
  1291. { .type = PERF_TYPE_HW_CACHE,
  1292. .config =
  1293. PERF_COUNT_HW_CACHE_L1D << 0 |
  1294. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1295. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1296. { .type = PERF_TYPE_HW_CACHE,
  1297. .config =
  1298. PERF_COUNT_HW_CACHE_L1D << 0 |
  1299. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1300. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1301. };
  1302. /* Set attrs if no event is selected and !null_run: */
  1303. if (null_run)
  1304. return 0;
  1305. if (transaction_run) {
  1306. int err;
  1307. if (pmu_have_event("cpu", "cycles-ct") &&
  1308. pmu_have_event("cpu", "el-start"))
  1309. err = setup_events(transaction_attrs,
  1310. ARRAY_SIZE(transaction_attrs));
  1311. else
  1312. err = setup_events(transaction_limited_attrs,
  1313. ARRAY_SIZE(transaction_limited_attrs));
  1314. if (err < 0) {
  1315. fprintf(stderr, "Cannot set up transaction events\n");
  1316. return -1;
  1317. }
  1318. return 0;
  1319. }
  1320. if (!evsel_list->nr_entries) {
  1321. if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
  1322. return -1;
  1323. }
  1324. /* Detailed events get appended to the event list: */
  1325. if (detailed_run < 1)
  1326. return 0;
  1327. /* Append detailed run extra attributes: */
  1328. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  1329. return -1;
  1330. if (detailed_run < 2)
  1331. return 0;
  1332. /* Append very detailed run extra attributes: */
  1333. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  1334. return -1;
  1335. if (detailed_run < 3)
  1336. return 0;
  1337. /* Append very, very detailed run extra attributes: */
  1338. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  1339. }
  1340. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  1341. {
  1342. bool append_file = false;
  1343. int output_fd = 0;
  1344. const char *output_name = NULL;
  1345. const struct option options[] = {
  1346. OPT_BOOLEAN('T', "transaction", &transaction_run,
  1347. "hardware transaction statistics"),
  1348. OPT_CALLBACK('e', "event", &evsel_list, "event",
  1349. "event selector. use 'perf list' to list available events",
  1350. parse_events_option),
  1351. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  1352. "event filter", parse_filter),
  1353. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  1354. "child tasks do not inherit counters"),
  1355. OPT_STRING('p', "pid", &target.pid, "pid",
  1356. "stat events on existing process id"),
  1357. OPT_STRING('t', "tid", &target.tid, "tid",
  1358. "stat events on existing thread id"),
  1359. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  1360. "system-wide collection from all CPUs"),
  1361. OPT_BOOLEAN('g', "group", &group,
  1362. "put the counters into a counter group"),
  1363. OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
  1364. OPT_INCR('v', "verbose", &verbose,
  1365. "be more verbose (show counter open errors, etc)"),
  1366. OPT_INTEGER('r', "repeat", &run_count,
  1367. "repeat command and print average + stddev (max: 100, forever: 0)"),
  1368. OPT_BOOLEAN('n', "null", &null_run,
  1369. "null run - dont start any counters"),
  1370. OPT_INCR('d', "detailed", &detailed_run,
  1371. "detailed run - start a lot of events"),
  1372. OPT_BOOLEAN('S', "sync", &sync_run,
  1373. "call sync() before starting a run"),
  1374. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  1375. "print large numbers with thousands\' separators",
  1376. stat__set_big_num),
  1377. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  1378. "list of cpus to monitor in system-wide"),
  1379. OPT_SET_UINT('A', "no-aggr", &aggr_mode,
  1380. "disable CPU count aggregation", AGGR_NONE),
  1381. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  1382. "print counts with custom separator"),
  1383. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  1384. "monitor event in cgroup name only", parse_cgroups),
  1385. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1386. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  1387. OPT_INTEGER(0, "log-fd", &output_fd,
  1388. "log output to fd, instead of stderr"),
  1389. OPT_STRING(0, "pre", &pre_cmd, "command",
  1390. "command to run prior to the measured command"),
  1391. OPT_STRING(0, "post", &post_cmd, "command",
  1392. "command to run after to the measured command"),
  1393. OPT_UINTEGER('I', "interval-print", &interval,
  1394. "print counts at regular interval in ms (>= 100)"),
  1395. OPT_SET_UINT(0, "per-socket", &aggr_mode,
  1396. "aggregate counts per processor socket", AGGR_SOCKET),
  1397. OPT_SET_UINT(0, "per-core", &aggr_mode,
  1398. "aggregate counts per physical processor core", AGGR_CORE),
  1399. OPT_UINTEGER('D', "delay", &initial_delay,
  1400. "ms to wait before starting measurement after program start"),
  1401. OPT_END()
  1402. };
  1403. const char * const stat_usage[] = {
  1404. "perf stat [<options>] [<command>]",
  1405. NULL
  1406. };
  1407. int status = -EINVAL, run_idx;
  1408. const char *mode;
  1409. setlocale(LC_ALL, "");
  1410. evsel_list = perf_evlist__new();
  1411. if (evsel_list == NULL)
  1412. return -ENOMEM;
  1413. argc = parse_options(argc, argv, options, stat_usage,
  1414. PARSE_OPT_STOP_AT_NON_OPTION);
  1415. output = stderr;
  1416. if (output_name && strcmp(output_name, "-"))
  1417. output = NULL;
  1418. if (output_name && output_fd) {
  1419. fprintf(stderr, "cannot use both --output and --log-fd\n");
  1420. parse_options_usage(stat_usage, options, "o", 1);
  1421. parse_options_usage(NULL, options, "log-fd", 0);
  1422. goto out;
  1423. }
  1424. if (output_fd < 0) {
  1425. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  1426. parse_options_usage(stat_usage, options, "log-fd", 0);
  1427. goto out;
  1428. }
  1429. if (!output) {
  1430. struct timespec tm;
  1431. mode = append_file ? "a" : "w";
  1432. output = fopen(output_name, mode);
  1433. if (!output) {
  1434. perror("failed to create output file");
  1435. return -1;
  1436. }
  1437. clock_gettime(CLOCK_REALTIME, &tm);
  1438. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  1439. } else if (output_fd > 0) {
  1440. mode = append_file ? "a" : "w";
  1441. output = fdopen(output_fd, mode);
  1442. if (!output) {
  1443. perror("Failed opening logfd");
  1444. return -errno;
  1445. }
  1446. }
  1447. if (csv_sep) {
  1448. csv_output = true;
  1449. if (!strcmp(csv_sep, "\\t"))
  1450. csv_sep = "\t";
  1451. } else
  1452. csv_sep = DEFAULT_SEPARATOR;
  1453. /*
  1454. * let the spreadsheet do the pretty-printing
  1455. */
  1456. if (csv_output) {
  1457. /* User explicitly passed -B? */
  1458. if (big_num_opt == 1) {
  1459. fprintf(stderr, "-B option not supported with -x\n");
  1460. parse_options_usage(stat_usage, options, "B", 1);
  1461. parse_options_usage(NULL, options, "x", 1);
  1462. goto out;
  1463. } else /* Nope, so disable big number formatting */
  1464. big_num = false;
  1465. } else if (big_num_opt == 0) /* User passed --no-big-num */
  1466. big_num = false;
  1467. if (!argc && target__none(&target))
  1468. usage_with_options(stat_usage, options);
  1469. if (run_count < 0) {
  1470. pr_err("Run count must be a positive number\n");
  1471. parse_options_usage(stat_usage, options, "r", 1);
  1472. goto out;
  1473. } else if (run_count == 0) {
  1474. forever = true;
  1475. run_count = 1;
  1476. }
  1477. /* no_aggr, cgroup are for system-wide only */
  1478. if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) &&
  1479. !target__has_cpu(&target)) {
  1480. fprintf(stderr, "both cgroup and no-aggregation "
  1481. "modes only available in system-wide mode\n");
  1482. parse_options_usage(stat_usage, options, "G", 1);
  1483. parse_options_usage(NULL, options, "A", 1);
  1484. parse_options_usage(NULL, options, "a", 1);
  1485. goto out;
  1486. }
  1487. if (add_default_attributes())
  1488. goto out;
  1489. target__validate(&target);
  1490. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  1491. if (target__has_task(&target)) {
  1492. pr_err("Problems finding threads of monitor\n");
  1493. parse_options_usage(stat_usage, options, "p", 1);
  1494. parse_options_usage(NULL, options, "t", 1);
  1495. } else if (target__has_cpu(&target)) {
  1496. perror("failed to parse CPUs map");
  1497. parse_options_usage(stat_usage, options, "C", 1);
  1498. parse_options_usage(NULL, options, "a", 1);
  1499. }
  1500. goto out;
  1501. }
  1502. if (interval && interval < 100) {
  1503. pr_err("print interval must be >= 100ms\n");
  1504. parse_options_usage(stat_usage, options, "I", 1);
  1505. goto out;
  1506. }
  1507. if (perf_evlist__alloc_stats(evsel_list, interval))
  1508. goto out;
  1509. if (perf_stat_init_aggr_mode())
  1510. goto out;
  1511. /*
  1512. * We dont want to block the signals - that would cause
  1513. * child tasks to inherit that and Ctrl-C would not work.
  1514. * What we want is for Ctrl-C to work in the exec()-ed
  1515. * task, but being ignored by perf stat itself:
  1516. */
  1517. atexit(sig_atexit);
  1518. if (!forever)
  1519. signal(SIGINT, skip_signal);
  1520. signal(SIGCHLD, skip_signal);
  1521. signal(SIGALRM, skip_signal);
  1522. signal(SIGABRT, skip_signal);
  1523. status = 0;
  1524. for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
  1525. if (run_count != 1 && verbose)
  1526. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1527. run_idx + 1);
  1528. status = run_perf_stat(argc, argv);
  1529. if (forever && status != -1) {
  1530. print_stat(argc, argv);
  1531. perf_stat__reset_stats(evsel_list);
  1532. }
  1533. }
  1534. if (!forever && status != -1 && !interval)
  1535. print_stat(argc, argv);
  1536. perf_evlist__free_stats(evsel_list);
  1537. out:
  1538. perf_evlist__delete(evsel_list);
  1539. return status;
  1540. }