builtin-stat.c 50 KB

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