builtin-stat.c 40 KB

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