builtin-stat.c 41 KB

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