builtin-stat.c 45 KB

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