builtin-stat.c 41 KB

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