builtin-stat.c 47 KB

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