builtin-stat.c 34 KB

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