builtin-stat.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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. fflush(output);
  356. }
  357. static void handle_initial_delay(void)
  358. {
  359. struct perf_evsel *counter;
  360. if (initial_delay) {
  361. const int ncpus = cpu_map__nr(evsel_list->cpus),
  362. nthreads = thread_map__nr(evsel_list->threads);
  363. usleep(initial_delay * 1000);
  364. list_for_each_entry(counter, &evsel_list->entries, node)
  365. perf_evsel__enable(counter, ncpus, nthreads);
  366. }
  367. }
  368. static int __run_perf_stat(int argc, const char **argv)
  369. {
  370. char msg[512];
  371. unsigned long long t0, t1;
  372. struct perf_evsel *counter;
  373. struct timespec ts;
  374. int status = 0;
  375. const bool forks = (argc > 0);
  376. if (interval) {
  377. ts.tv_sec = interval / 1000;
  378. ts.tv_nsec = (interval % 1000) * 1000000;
  379. } else {
  380. ts.tv_sec = 1;
  381. ts.tv_nsec = 0;
  382. }
  383. if (forks) {
  384. if (perf_evlist__prepare_workload(evsel_list, &target, argv,
  385. false, false) < 0) {
  386. perror("failed to prepare workload");
  387. return -1;
  388. }
  389. child_pid = evsel_list->workload.pid;
  390. }
  391. if (group)
  392. perf_evlist__set_leader(evsel_list);
  393. list_for_each_entry(counter, &evsel_list->entries, node) {
  394. if (create_perf_stat_counter(counter) < 0) {
  395. /*
  396. * PPC returns ENXIO for HW counters until 2.6.37
  397. * (behavior changed with commit b0a873e).
  398. */
  399. if (errno == EINVAL || errno == ENOSYS ||
  400. errno == ENOENT || errno == EOPNOTSUPP ||
  401. errno == ENXIO) {
  402. if (verbose)
  403. ui__warning("%s event is not supported by the kernel.\n",
  404. perf_evsel__name(counter));
  405. counter->supported = false;
  406. continue;
  407. }
  408. perf_evsel__open_strerror(counter, &target,
  409. errno, msg, sizeof(msg));
  410. ui__error("%s\n", msg);
  411. if (child_pid != -1)
  412. kill(child_pid, SIGTERM);
  413. return -1;
  414. }
  415. counter->supported = true;
  416. }
  417. if (perf_evlist__apply_filters(evsel_list)) {
  418. error("failed to set filter with %d (%s)\n", errno,
  419. strerror(errno));
  420. return -1;
  421. }
  422. /*
  423. * Enable counters and exec the command:
  424. */
  425. t0 = rdclock();
  426. clock_gettime(CLOCK_MONOTONIC, &ref_time);
  427. if (forks) {
  428. perf_evlist__start_workload(evsel_list);
  429. handle_initial_delay();
  430. if (interval) {
  431. while (!waitpid(child_pid, &status, WNOHANG)) {
  432. nanosleep(&ts, NULL);
  433. print_interval();
  434. }
  435. }
  436. wait(&status);
  437. if (WIFSIGNALED(status))
  438. psignal(WTERMSIG(status), argv[0]);
  439. } else {
  440. handle_initial_delay();
  441. while (!done) {
  442. nanosleep(&ts, NULL);
  443. if (interval)
  444. print_interval();
  445. }
  446. }
  447. t1 = rdclock();
  448. update_stats(&walltime_nsecs_stats, t1 - t0);
  449. if (aggr_mode == AGGR_GLOBAL) {
  450. list_for_each_entry(counter, &evsel_list->entries, node) {
  451. read_counter_aggr(counter);
  452. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
  453. thread_map__nr(evsel_list->threads));
  454. }
  455. } else {
  456. list_for_each_entry(counter, &evsel_list->entries, node) {
  457. read_counter(counter);
  458. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
  459. }
  460. }
  461. return WEXITSTATUS(status);
  462. }
  463. static int run_perf_stat(int argc __maybe_unused, const char **argv)
  464. {
  465. int ret;
  466. if (pre_cmd) {
  467. ret = system(pre_cmd);
  468. if (ret)
  469. return ret;
  470. }
  471. if (sync_run)
  472. sync();
  473. ret = __run_perf_stat(argc, argv);
  474. if (ret)
  475. return ret;
  476. if (post_cmd) {
  477. ret = system(post_cmd);
  478. if (ret)
  479. return ret;
  480. }
  481. return ret;
  482. }
  483. static void print_noise_pct(double total, double avg)
  484. {
  485. double pct = rel_stddev_stats(total, avg);
  486. if (csv_output)
  487. fprintf(output, "%s%.2f%%", csv_sep, pct);
  488. else if (pct)
  489. fprintf(output, " ( +-%6.2f%% )", pct);
  490. }
  491. static void print_noise(struct perf_evsel *evsel, double avg)
  492. {
  493. struct perf_stat *ps;
  494. if (run_count == 1)
  495. return;
  496. ps = evsel->priv;
  497. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  498. }
  499. static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
  500. {
  501. switch (aggr_mode) {
  502. case AGGR_CORE:
  503. fprintf(output, "S%d-C%*d%s%*d%s",
  504. cpu_map__id_to_socket(id),
  505. csv_output ? 0 : -8,
  506. cpu_map__id_to_cpu(id),
  507. csv_sep,
  508. csv_output ? 0 : 4,
  509. nr,
  510. csv_sep);
  511. break;
  512. case AGGR_SOCKET:
  513. fprintf(output, "S%*d%s%*d%s",
  514. csv_output ? 0 : -5,
  515. id,
  516. csv_sep,
  517. csv_output ? 0 : 4,
  518. nr,
  519. csv_sep);
  520. break;
  521. case AGGR_NONE:
  522. fprintf(output, "CPU%*d%s",
  523. csv_output ? 0 : -4,
  524. perf_evsel__cpus(evsel)->map[id], csv_sep);
  525. break;
  526. case AGGR_GLOBAL:
  527. default:
  528. break;
  529. }
  530. }
  531. static void nsec_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
  532. {
  533. double msecs = avg / 1e6;
  534. const char *fmt = csv_output ? "%.6f%s%s" : "%18.6f%s%-25s";
  535. aggr_printout(evsel, cpu, nr);
  536. fprintf(output, fmt, msecs, csv_sep, perf_evsel__name(evsel));
  537. if (evsel->cgrp)
  538. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  539. if (csv_output || interval)
  540. return;
  541. if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  542. fprintf(output, " # %8.3f CPUs utilized ",
  543. avg / avg_stats(&walltime_nsecs_stats));
  544. else
  545. fprintf(output, " ");
  546. }
  547. /* used for get_ratio_color() */
  548. enum grc_type {
  549. GRC_STALLED_CYCLES_FE,
  550. GRC_STALLED_CYCLES_BE,
  551. GRC_CACHE_MISSES,
  552. GRC_MAX_NR
  553. };
  554. static const char *get_ratio_color(enum grc_type type, double ratio)
  555. {
  556. static const double grc_table[GRC_MAX_NR][3] = {
  557. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  558. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  559. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  560. };
  561. const char *color = PERF_COLOR_NORMAL;
  562. if (ratio > grc_table[type][0])
  563. color = PERF_COLOR_RED;
  564. else if (ratio > grc_table[type][1])
  565. color = PERF_COLOR_MAGENTA;
  566. else if (ratio > grc_table[type][2])
  567. color = PERF_COLOR_YELLOW;
  568. return color;
  569. }
  570. static void print_stalled_cycles_frontend(int cpu,
  571. struct perf_evsel *evsel
  572. __maybe_unused, double avg)
  573. {
  574. double total, ratio = 0.0;
  575. const char *color;
  576. total = avg_stats(&runtime_cycles_stats[cpu]);
  577. if (total)
  578. ratio = avg / total * 100.0;
  579. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  580. fprintf(output, " # ");
  581. color_fprintf(output, color, "%6.2f%%", ratio);
  582. fprintf(output, " frontend cycles idle ");
  583. }
  584. static void print_stalled_cycles_backend(int cpu,
  585. struct perf_evsel *evsel
  586. __maybe_unused, double avg)
  587. {
  588. double total, ratio = 0.0;
  589. const char *color;
  590. total = avg_stats(&runtime_cycles_stats[cpu]);
  591. if (total)
  592. ratio = avg / total * 100.0;
  593. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  594. fprintf(output, " # ");
  595. color_fprintf(output, color, "%6.2f%%", ratio);
  596. fprintf(output, " backend cycles idle ");
  597. }
  598. static void print_branch_misses(int cpu,
  599. struct perf_evsel *evsel __maybe_unused,
  600. double avg)
  601. {
  602. double total, ratio = 0.0;
  603. const char *color;
  604. total = avg_stats(&runtime_branches_stats[cpu]);
  605. if (total)
  606. ratio = avg / total * 100.0;
  607. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  608. fprintf(output, " # ");
  609. color_fprintf(output, color, "%6.2f%%", ratio);
  610. fprintf(output, " of all branches ");
  611. }
  612. static void print_l1_dcache_misses(int cpu,
  613. struct perf_evsel *evsel __maybe_unused,
  614. double avg)
  615. {
  616. double total, ratio = 0.0;
  617. const char *color;
  618. total = avg_stats(&runtime_l1_dcache_stats[cpu]);
  619. if (total)
  620. ratio = avg / total * 100.0;
  621. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  622. fprintf(output, " # ");
  623. color_fprintf(output, color, "%6.2f%%", ratio);
  624. fprintf(output, " of all L1-dcache hits ");
  625. }
  626. static void print_l1_icache_misses(int cpu,
  627. struct perf_evsel *evsel __maybe_unused,
  628. double avg)
  629. {
  630. double total, ratio = 0.0;
  631. const char *color;
  632. total = avg_stats(&runtime_l1_icache_stats[cpu]);
  633. if (total)
  634. ratio = avg / total * 100.0;
  635. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  636. fprintf(output, " # ");
  637. color_fprintf(output, color, "%6.2f%%", ratio);
  638. fprintf(output, " of all L1-icache hits ");
  639. }
  640. static void print_dtlb_cache_misses(int cpu,
  641. struct perf_evsel *evsel __maybe_unused,
  642. double avg)
  643. {
  644. double total, ratio = 0.0;
  645. const char *color;
  646. total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
  647. if (total)
  648. ratio = avg / total * 100.0;
  649. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  650. fprintf(output, " # ");
  651. color_fprintf(output, color, "%6.2f%%", ratio);
  652. fprintf(output, " of all dTLB cache hits ");
  653. }
  654. static void print_itlb_cache_misses(int cpu,
  655. struct perf_evsel *evsel __maybe_unused,
  656. double avg)
  657. {
  658. double total, ratio = 0.0;
  659. const char *color;
  660. total = avg_stats(&runtime_itlb_cache_stats[cpu]);
  661. if (total)
  662. ratio = avg / total * 100.0;
  663. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  664. fprintf(output, " # ");
  665. color_fprintf(output, color, "%6.2f%%", ratio);
  666. fprintf(output, " of all iTLB cache hits ");
  667. }
  668. static void print_ll_cache_misses(int cpu,
  669. struct perf_evsel *evsel __maybe_unused,
  670. double avg)
  671. {
  672. double total, ratio = 0.0;
  673. const char *color;
  674. total = avg_stats(&runtime_ll_cache_stats[cpu]);
  675. if (total)
  676. ratio = avg / total * 100.0;
  677. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  678. fprintf(output, " # ");
  679. color_fprintf(output, color, "%6.2f%%", ratio);
  680. fprintf(output, " of all LL-cache hits ");
  681. }
  682. static void abs_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
  683. {
  684. double total, ratio = 0.0;
  685. const char *fmt;
  686. if (csv_output)
  687. fmt = "%.0f%s%s";
  688. else if (big_num)
  689. fmt = "%'18.0f%s%-25s";
  690. else
  691. fmt = "%18.0f%s%-25s";
  692. aggr_printout(evsel, cpu, nr);
  693. if (aggr_mode == AGGR_GLOBAL)
  694. cpu = 0;
  695. fprintf(output, fmt, avg, csv_sep, perf_evsel__name(evsel));
  696. if (evsel->cgrp)
  697. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  698. if (csv_output || interval)
  699. return;
  700. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  701. total = avg_stats(&runtime_cycles_stats[cpu]);
  702. if (total)
  703. ratio = avg / total;
  704. fprintf(output, " # %5.2f insns per cycle ", ratio);
  705. total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
  706. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
  707. if (total && avg) {
  708. ratio = total / avg;
  709. fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
  710. }
  711. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
  712. runtime_branches_stats[cpu].n != 0) {
  713. print_branch_misses(cpu, evsel, avg);
  714. } else if (
  715. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  716. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  717. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  718. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  719. runtime_l1_dcache_stats[cpu].n != 0) {
  720. print_l1_dcache_misses(cpu, evsel, avg);
  721. } else if (
  722. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  723. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  724. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  725. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  726. runtime_l1_icache_stats[cpu].n != 0) {
  727. print_l1_icache_misses(cpu, evsel, avg);
  728. } else if (
  729. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  730. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  731. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  732. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  733. runtime_dtlb_cache_stats[cpu].n != 0) {
  734. print_dtlb_cache_misses(cpu, evsel, avg);
  735. } else if (
  736. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  737. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  738. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  739. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  740. runtime_itlb_cache_stats[cpu].n != 0) {
  741. print_itlb_cache_misses(cpu, evsel, avg);
  742. } else if (
  743. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  744. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  745. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  746. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
  747. runtime_ll_cache_stats[cpu].n != 0) {
  748. print_ll_cache_misses(cpu, evsel, avg);
  749. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
  750. runtime_cacherefs_stats[cpu].n != 0) {
  751. total = avg_stats(&runtime_cacherefs_stats[cpu]);
  752. if (total)
  753. ratio = avg * 100 / total;
  754. fprintf(output, " # %8.3f %% of all cache refs ", ratio);
  755. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  756. print_stalled_cycles_frontend(cpu, evsel, avg);
  757. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  758. print_stalled_cycles_backend(cpu, evsel, avg);
  759. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  760. total = avg_stats(&runtime_nsecs_stats[cpu]);
  761. if (total)
  762. ratio = 1.0 * avg / total;
  763. fprintf(output, " # %8.3f GHz ", ratio);
  764. } else if (runtime_nsecs_stats[cpu].n != 0) {
  765. char unit = 'M';
  766. total = avg_stats(&runtime_nsecs_stats[cpu]);
  767. if (total)
  768. ratio = 1000.0 * avg / total;
  769. if (ratio < 0.001) {
  770. ratio *= 1000;
  771. unit = 'K';
  772. }
  773. fprintf(output, " # %8.3f %c/sec ", ratio, unit);
  774. } else {
  775. fprintf(output, " ");
  776. }
  777. }
  778. static void print_aggr(char *prefix)
  779. {
  780. struct perf_evsel *counter;
  781. int cpu, cpu2, s, s2, id, nr;
  782. u64 ena, run, val;
  783. if (!(aggr_map || aggr_get_id))
  784. return;
  785. for (s = 0; s < aggr_map->nr; s++) {
  786. id = aggr_map->map[s];
  787. list_for_each_entry(counter, &evsel_list->entries, node) {
  788. val = ena = run = 0;
  789. nr = 0;
  790. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  791. cpu2 = perf_evsel__cpus(counter)->map[cpu];
  792. s2 = aggr_get_id(evsel_list->cpus, cpu2);
  793. if (s2 != id)
  794. continue;
  795. val += counter->counts->cpu[cpu].val;
  796. ena += counter->counts->cpu[cpu].ena;
  797. run += counter->counts->cpu[cpu].run;
  798. nr++;
  799. }
  800. if (prefix)
  801. fprintf(output, "%s", prefix);
  802. if (run == 0 || ena == 0) {
  803. aggr_printout(counter, id, nr);
  804. fprintf(output, "%*s%s%*s",
  805. csv_output ? 0 : 18,
  806. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  807. csv_sep,
  808. csv_output ? 0 : -24,
  809. perf_evsel__name(counter));
  810. if (counter->cgrp)
  811. fprintf(output, "%s%s",
  812. csv_sep, counter->cgrp->name);
  813. fputc('\n', output);
  814. continue;
  815. }
  816. if (nsec_counter(counter))
  817. nsec_printout(id, nr, counter, val);
  818. else
  819. abs_printout(id, nr, counter, val);
  820. if (!csv_output) {
  821. print_noise(counter, 1.0);
  822. if (run != ena)
  823. fprintf(output, " (%.2f%%)",
  824. 100.0 * run / ena);
  825. }
  826. fputc('\n', output);
  827. }
  828. }
  829. }
  830. /*
  831. * Print out the results of a single counter:
  832. * aggregated counts in system-wide mode
  833. */
  834. static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
  835. {
  836. struct perf_stat *ps = counter->priv;
  837. double avg = avg_stats(&ps->res_stats[0]);
  838. int scaled = counter->counts->scaled;
  839. if (prefix)
  840. fprintf(output, "%s", prefix);
  841. if (scaled == -1) {
  842. fprintf(output, "%*s%s%*s",
  843. csv_output ? 0 : 18,
  844. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  845. csv_sep,
  846. csv_output ? 0 : -24,
  847. perf_evsel__name(counter));
  848. if (counter->cgrp)
  849. fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
  850. fputc('\n', output);
  851. return;
  852. }
  853. if (nsec_counter(counter))
  854. nsec_printout(-1, 0, counter, avg);
  855. else
  856. abs_printout(-1, 0, counter, avg);
  857. print_noise(counter, avg);
  858. if (csv_output) {
  859. fputc('\n', output);
  860. return;
  861. }
  862. if (scaled) {
  863. double avg_enabled, avg_running;
  864. avg_enabled = avg_stats(&ps->res_stats[1]);
  865. avg_running = avg_stats(&ps->res_stats[2]);
  866. fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
  867. }
  868. fprintf(output, "\n");
  869. }
  870. /*
  871. * Print out the results of a single counter:
  872. * does not use aggregated count in system-wide
  873. */
  874. static void print_counter(struct perf_evsel *counter, char *prefix)
  875. {
  876. u64 ena, run, val;
  877. int cpu;
  878. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  879. val = counter->counts->cpu[cpu].val;
  880. ena = counter->counts->cpu[cpu].ena;
  881. run = counter->counts->cpu[cpu].run;
  882. if (prefix)
  883. fprintf(output, "%s", prefix);
  884. if (run == 0 || ena == 0) {
  885. fprintf(output, "CPU%*d%s%*s%s%*s",
  886. csv_output ? 0 : -4,
  887. perf_evsel__cpus(counter)->map[cpu], csv_sep,
  888. csv_output ? 0 : 18,
  889. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  890. csv_sep,
  891. csv_output ? 0 : -24,
  892. perf_evsel__name(counter));
  893. if (counter->cgrp)
  894. fprintf(output, "%s%s",
  895. csv_sep, counter->cgrp->name);
  896. fputc('\n', output);
  897. continue;
  898. }
  899. if (nsec_counter(counter))
  900. nsec_printout(cpu, 0, counter, val);
  901. else
  902. abs_printout(cpu, 0, counter, val);
  903. if (!csv_output) {
  904. print_noise(counter, 1.0);
  905. if (run != ena)
  906. fprintf(output, " (%.2f%%)",
  907. 100.0 * run / ena);
  908. }
  909. fputc('\n', output);
  910. }
  911. }
  912. static void print_stat(int argc, const char **argv)
  913. {
  914. struct perf_evsel *counter;
  915. int i;
  916. fflush(stdout);
  917. if (!csv_output) {
  918. fprintf(output, "\n");
  919. fprintf(output, " Performance counter stats for ");
  920. if (!perf_target__has_task(&target)) {
  921. fprintf(output, "\'%s", argv[0]);
  922. for (i = 1; i < argc; i++)
  923. fprintf(output, " %s", argv[i]);
  924. } else if (target.pid)
  925. fprintf(output, "process id \'%s", target.pid);
  926. else
  927. fprintf(output, "thread id \'%s", target.tid);
  928. fprintf(output, "\'");
  929. if (run_count > 1)
  930. fprintf(output, " (%d runs)", run_count);
  931. fprintf(output, ":\n\n");
  932. }
  933. switch (aggr_mode) {
  934. case AGGR_CORE:
  935. case AGGR_SOCKET:
  936. print_aggr(NULL);
  937. break;
  938. case AGGR_GLOBAL:
  939. list_for_each_entry(counter, &evsel_list->entries, node)
  940. print_counter_aggr(counter, NULL);
  941. break;
  942. case AGGR_NONE:
  943. list_for_each_entry(counter, &evsel_list->entries, node)
  944. print_counter(counter, NULL);
  945. break;
  946. default:
  947. break;
  948. }
  949. if (!csv_output) {
  950. if (!null_run)
  951. fprintf(output, "\n");
  952. fprintf(output, " %17.9f seconds time elapsed",
  953. avg_stats(&walltime_nsecs_stats)/1e9);
  954. if (run_count > 1) {
  955. fprintf(output, " ");
  956. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  957. avg_stats(&walltime_nsecs_stats));
  958. }
  959. fprintf(output, "\n\n");
  960. }
  961. }
  962. static volatile int signr = -1;
  963. static void skip_signal(int signo)
  964. {
  965. if ((child_pid == -1) || interval)
  966. done = 1;
  967. signr = signo;
  968. /*
  969. * render child_pid harmless
  970. * won't send SIGTERM to a random
  971. * process in case of race condition
  972. * and fast PID recycling
  973. */
  974. child_pid = -1;
  975. }
  976. static void sig_atexit(void)
  977. {
  978. sigset_t set, oset;
  979. /*
  980. * avoid race condition with SIGCHLD handler
  981. * in skip_signal() which is modifying child_pid
  982. * goal is to avoid send SIGTERM to a random
  983. * process
  984. */
  985. sigemptyset(&set);
  986. sigaddset(&set, SIGCHLD);
  987. sigprocmask(SIG_BLOCK, &set, &oset);
  988. if (child_pid != -1)
  989. kill(child_pid, SIGTERM);
  990. sigprocmask(SIG_SETMASK, &oset, NULL);
  991. if (signr == -1)
  992. return;
  993. signal(signr, SIG_DFL);
  994. kill(getpid(), signr);
  995. }
  996. static int stat__set_big_num(const struct option *opt __maybe_unused,
  997. const char *s __maybe_unused, int unset)
  998. {
  999. big_num_opt = unset ? 0 : 1;
  1000. return 0;
  1001. }
  1002. static int perf_stat_init_aggr_mode(void)
  1003. {
  1004. switch (aggr_mode) {
  1005. case AGGR_SOCKET:
  1006. if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
  1007. perror("cannot build socket map");
  1008. return -1;
  1009. }
  1010. aggr_get_id = cpu_map__get_socket;
  1011. break;
  1012. case AGGR_CORE:
  1013. if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
  1014. perror("cannot build core map");
  1015. return -1;
  1016. }
  1017. aggr_get_id = cpu_map__get_core;
  1018. break;
  1019. case AGGR_NONE:
  1020. case AGGR_GLOBAL:
  1021. default:
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. /*
  1027. * Add default attributes, if there were no attributes specified or
  1028. * if -d/--detailed, -d -d or -d -d -d is used:
  1029. */
  1030. static int add_default_attributes(void)
  1031. {
  1032. struct perf_event_attr default_attrs[] = {
  1033. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  1034. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  1035. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  1036. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  1037. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  1038. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  1039. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  1040. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  1041. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  1042. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  1043. };
  1044. /*
  1045. * Detailed stats (-d), covering the L1 and last level data caches:
  1046. */
  1047. struct perf_event_attr detailed_attrs[] = {
  1048. { .type = PERF_TYPE_HW_CACHE,
  1049. .config =
  1050. PERF_COUNT_HW_CACHE_L1D << 0 |
  1051. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1052. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1053. { .type = PERF_TYPE_HW_CACHE,
  1054. .config =
  1055. PERF_COUNT_HW_CACHE_L1D << 0 |
  1056. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1057. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1058. { .type = PERF_TYPE_HW_CACHE,
  1059. .config =
  1060. PERF_COUNT_HW_CACHE_LL << 0 |
  1061. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1062. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1063. { .type = PERF_TYPE_HW_CACHE,
  1064. .config =
  1065. PERF_COUNT_HW_CACHE_LL << 0 |
  1066. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1067. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1068. };
  1069. /*
  1070. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  1071. */
  1072. struct perf_event_attr very_detailed_attrs[] = {
  1073. { .type = PERF_TYPE_HW_CACHE,
  1074. .config =
  1075. PERF_COUNT_HW_CACHE_L1I << 0 |
  1076. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1077. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1078. { .type = PERF_TYPE_HW_CACHE,
  1079. .config =
  1080. PERF_COUNT_HW_CACHE_L1I << 0 |
  1081. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1082. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1083. { .type = PERF_TYPE_HW_CACHE,
  1084. .config =
  1085. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1086. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1087. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1088. { .type = PERF_TYPE_HW_CACHE,
  1089. .config =
  1090. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1091. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1092. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1093. { .type = PERF_TYPE_HW_CACHE,
  1094. .config =
  1095. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1096. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1097. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1098. { .type = PERF_TYPE_HW_CACHE,
  1099. .config =
  1100. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1101. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1102. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1103. };
  1104. /*
  1105. * Very, very detailed stats (-d -d -d), adding prefetch events:
  1106. */
  1107. struct perf_event_attr very_very_detailed_attrs[] = {
  1108. { .type = PERF_TYPE_HW_CACHE,
  1109. .config =
  1110. PERF_COUNT_HW_CACHE_L1D << 0 |
  1111. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1112. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1113. { .type = PERF_TYPE_HW_CACHE,
  1114. .config =
  1115. PERF_COUNT_HW_CACHE_L1D << 0 |
  1116. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1117. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1118. };
  1119. /* Set attrs if no event is selected and !null_run: */
  1120. if (null_run)
  1121. return 0;
  1122. if (!evsel_list->nr_entries) {
  1123. if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
  1124. return -1;
  1125. }
  1126. /* Detailed events get appended to the event list: */
  1127. if (detailed_run < 1)
  1128. return 0;
  1129. /* Append detailed run extra attributes: */
  1130. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  1131. return -1;
  1132. if (detailed_run < 2)
  1133. return 0;
  1134. /* Append very detailed run extra attributes: */
  1135. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  1136. return -1;
  1137. if (detailed_run < 3)
  1138. return 0;
  1139. /* Append very, very detailed run extra attributes: */
  1140. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  1141. }
  1142. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  1143. {
  1144. bool append_file = false;
  1145. int output_fd = 0;
  1146. const char *output_name = NULL;
  1147. const struct option options[] = {
  1148. OPT_CALLBACK('e', "event", &evsel_list, "event",
  1149. "event selector. use 'perf list' to list available events",
  1150. parse_events_option),
  1151. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  1152. "event filter", parse_filter),
  1153. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  1154. "child tasks do not inherit counters"),
  1155. OPT_STRING('p', "pid", &target.pid, "pid",
  1156. "stat events on existing process id"),
  1157. OPT_STRING('t', "tid", &target.tid, "tid",
  1158. "stat events on existing thread id"),
  1159. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  1160. "system-wide collection from all CPUs"),
  1161. OPT_BOOLEAN('g', "group", &group,
  1162. "put the counters into a counter group"),
  1163. OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
  1164. OPT_INCR('v', "verbose", &verbose,
  1165. "be more verbose (show counter open errors, etc)"),
  1166. OPT_INTEGER('r', "repeat", &run_count,
  1167. "repeat command and print average + stddev (max: 100, forever: 0)"),
  1168. OPT_BOOLEAN('n', "null", &null_run,
  1169. "null run - dont start any counters"),
  1170. OPT_INCR('d', "detailed", &detailed_run,
  1171. "detailed run - start a lot of events"),
  1172. OPT_BOOLEAN('S', "sync", &sync_run,
  1173. "call sync() before starting a run"),
  1174. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  1175. "print large numbers with thousands\' separators",
  1176. stat__set_big_num),
  1177. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  1178. "list of cpus to monitor in system-wide"),
  1179. OPT_SET_UINT('A', "no-aggr", &aggr_mode,
  1180. "disable CPU count aggregation", AGGR_NONE),
  1181. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  1182. "print counts with custom separator"),
  1183. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  1184. "monitor event in cgroup name only", parse_cgroups),
  1185. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1186. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  1187. OPT_INTEGER(0, "log-fd", &output_fd,
  1188. "log output to fd, instead of stderr"),
  1189. OPT_STRING(0, "pre", &pre_cmd, "command",
  1190. "command to run prior to the measured command"),
  1191. OPT_STRING(0, "post", &post_cmd, "command",
  1192. "command to run after to the measured command"),
  1193. OPT_UINTEGER('I', "interval-print", &interval,
  1194. "print counts at regular interval in ms (>= 100)"),
  1195. OPT_SET_UINT(0, "per-socket", &aggr_mode,
  1196. "aggregate counts per processor socket", AGGR_SOCKET),
  1197. OPT_SET_UINT(0, "per-core", &aggr_mode,
  1198. "aggregate counts per physical processor core", AGGR_CORE),
  1199. OPT_UINTEGER('D', "delay", &initial_delay,
  1200. "ms to wait before starting measurement after program start"),
  1201. OPT_END()
  1202. };
  1203. const char * const stat_usage[] = {
  1204. "perf stat [<options>] [<command>]",
  1205. NULL
  1206. };
  1207. int status = -ENOMEM, run_idx;
  1208. const char *mode;
  1209. setlocale(LC_ALL, "");
  1210. evsel_list = perf_evlist__new();
  1211. if (evsel_list == NULL)
  1212. return -ENOMEM;
  1213. argc = parse_options(argc, argv, options, stat_usage,
  1214. PARSE_OPT_STOP_AT_NON_OPTION);
  1215. output = stderr;
  1216. if (output_name && strcmp(output_name, "-"))
  1217. output = NULL;
  1218. if (output_name && output_fd) {
  1219. fprintf(stderr, "cannot use both --output and --log-fd\n");
  1220. usage_with_options(stat_usage, options);
  1221. }
  1222. if (output_fd < 0) {
  1223. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  1224. usage_with_options(stat_usage, options);
  1225. }
  1226. if (!output) {
  1227. struct timespec tm;
  1228. mode = append_file ? "a" : "w";
  1229. output = fopen(output_name, mode);
  1230. if (!output) {
  1231. perror("failed to create output file");
  1232. return -1;
  1233. }
  1234. clock_gettime(CLOCK_REALTIME, &tm);
  1235. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  1236. } else if (output_fd > 0) {
  1237. mode = append_file ? "a" : "w";
  1238. output = fdopen(output_fd, mode);
  1239. if (!output) {
  1240. perror("Failed opening logfd");
  1241. return -errno;
  1242. }
  1243. }
  1244. if (csv_sep) {
  1245. csv_output = true;
  1246. if (!strcmp(csv_sep, "\\t"))
  1247. csv_sep = "\t";
  1248. } else
  1249. csv_sep = DEFAULT_SEPARATOR;
  1250. /*
  1251. * let the spreadsheet do the pretty-printing
  1252. */
  1253. if (csv_output) {
  1254. /* User explicitly passed -B? */
  1255. if (big_num_opt == 1) {
  1256. fprintf(stderr, "-B option not supported with -x\n");
  1257. usage_with_options(stat_usage, options);
  1258. } else /* Nope, so disable big number formatting */
  1259. big_num = false;
  1260. } else if (big_num_opt == 0) /* User passed --no-big-num */
  1261. big_num = false;
  1262. if (!argc && !perf_target__has_task(&target))
  1263. usage_with_options(stat_usage, options);
  1264. if (run_count < 0) {
  1265. usage_with_options(stat_usage, options);
  1266. } else if (run_count == 0) {
  1267. forever = true;
  1268. run_count = 1;
  1269. }
  1270. /* no_aggr, cgroup are for system-wide only */
  1271. if ((aggr_mode != AGGR_GLOBAL || nr_cgroups)
  1272. && !perf_target__has_cpu(&target)) {
  1273. fprintf(stderr, "both cgroup and no-aggregation "
  1274. "modes only available in system-wide mode\n");
  1275. usage_with_options(stat_usage, options);
  1276. return -1;
  1277. }
  1278. if (add_default_attributes())
  1279. goto out;
  1280. perf_target__validate(&target);
  1281. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  1282. if (perf_target__has_task(&target))
  1283. pr_err("Problems finding threads of monitor\n");
  1284. if (perf_target__has_cpu(&target))
  1285. perror("failed to parse CPUs map");
  1286. usage_with_options(stat_usage, options);
  1287. return -1;
  1288. }
  1289. if (interval && interval < 100) {
  1290. pr_err("print interval must be >= 100ms\n");
  1291. usage_with_options(stat_usage, options);
  1292. return -1;
  1293. }
  1294. if (perf_evlist__alloc_stats(evsel_list, interval))
  1295. goto out_free_maps;
  1296. if (perf_stat_init_aggr_mode())
  1297. goto out;
  1298. /*
  1299. * We dont want to block the signals - that would cause
  1300. * child tasks to inherit that and Ctrl-C would not work.
  1301. * What we want is for Ctrl-C to work in the exec()-ed
  1302. * task, but being ignored by perf stat itself:
  1303. */
  1304. atexit(sig_atexit);
  1305. if (!forever)
  1306. signal(SIGINT, skip_signal);
  1307. signal(SIGCHLD, skip_signal);
  1308. signal(SIGALRM, skip_signal);
  1309. signal(SIGABRT, skip_signal);
  1310. status = 0;
  1311. for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
  1312. if (run_count != 1 && verbose)
  1313. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1314. run_idx + 1);
  1315. status = run_perf_stat(argc, argv);
  1316. if (forever && status != -1) {
  1317. print_stat(argc, argv);
  1318. perf_stat__reset_stats(evsel_list);
  1319. }
  1320. }
  1321. if (!forever && status != -1 && !interval)
  1322. print_stat(argc, argv);
  1323. perf_evlist__free_stats(evsel_list);
  1324. out_free_maps:
  1325. perf_evlist__delete_maps(evsel_list);
  1326. out:
  1327. perf_evlist__delete(evsel_list);
  1328. return status;
  1329. }