builtin-stat.c 34 KB

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