builtin-stat.c 34 KB

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