builtin-stat.c 47 KB

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