builtin-stat.c 39 KB

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