builtin-stat.c 46 KB

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