builtin-stat.c 45 KB

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