builtin-stat.c 36 KB

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