builtin-stat.c 39 KB

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