builtin-stat.c 36 KB

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