builtin-stat.c 49 KB

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