builtin-stat.c 34 KB

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