builtin-stat.c 34 KB

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