builtin-stat.c 34 KB

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