builtin-stat.c 49 KB

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