builtin-stat.c 41 KB

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