builtin-stat.c 38 KB

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