stat-shadow.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include <stdio.h>
  2. #include "evsel.h"
  3. #include "stat.h"
  4. #include "color.h"
  5. #include "pmu.h"
  6. enum {
  7. CTX_BIT_USER = 1 << 0,
  8. CTX_BIT_KERNEL = 1 << 1,
  9. CTX_BIT_HV = 1 << 2,
  10. CTX_BIT_HOST = 1 << 3,
  11. CTX_BIT_IDLE = 1 << 4,
  12. CTX_BIT_MAX = 1 << 5,
  13. };
  14. #define NUM_CTX CTX_BIT_MAX
  15. /*
  16. * AGGR_GLOBAL: Use CPU 0
  17. * AGGR_SOCKET: Use first CPU of socket
  18. * AGGR_CORE: Use first CPU of core
  19. * AGGR_NONE: Use matching CPU
  20. * AGGR_THREAD: Not supported?
  21. */
  22. static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  23. static struct stats runtime_cycles_stats[NUM_CTX][MAX_NR_CPUS];
  24. static struct stats runtime_stalled_cycles_front_stats[NUM_CTX][MAX_NR_CPUS];
  25. static struct stats runtime_stalled_cycles_back_stats[NUM_CTX][MAX_NR_CPUS];
  26. static struct stats runtime_branches_stats[NUM_CTX][MAX_NR_CPUS];
  27. static struct stats runtime_cacherefs_stats[NUM_CTX][MAX_NR_CPUS];
  28. static struct stats runtime_l1_dcache_stats[NUM_CTX][MAX_NR_CPUS];
  29. static struct stats runtime_l1_icache_stats[NUM_CTX][MAX_NR_CPUS];
  30. static struct stats runtime_ll_cache_stats[NUM_CTX][MAX_NR_CPUS];
  31. static struct stats runtime_itlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
  32. static struct stats runtime_dtlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
  33. static struct stats runtime_cycles_in_tx_stats[NUM_CTX][MAX_NR_CPUS];
  34. static struct stats runtime_transaction_stats[NUM_CTX][MAX_NR_CPUS];
  35. static struct stats runtime_elision_stats[NUM_CTX][MAX_NR_CPUS];
  36. static bool have_frontend_stalled;
  37. struct stats walltime_nsecs_stats;
  38. void perf_stat__init_shadow_stats(void)
  39. {
  40. have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
  41. }
  42. static int evsel_context(struct perf_evsel *evsel)
  43. {
  44. int ctx = 0;
  45. if (evsel->attr.exclude_kernel)
  46. ctx |= CTX_BIT_KERNEL;
  47. if (evsel->attr.exclude_user)
  48. ctx |= CTX_BIT_USER;
  49. if (evsel->attr.exclude_hv)
  50. ctx |= CTX_BIT_HV;
  51. if (evsel->attr.exclude_host)
  52. ctx |= CTX_BIT_HOST;
  53. if (evsel->attr.exclude_idle)
  54. ctx |= CTX_BIT_IDLE;
  55. return ctx;
  56. }
  57. void perf_stat__reset_shadow_stats(void)
  58. {
  59. memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
  60. memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
  61. memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
  62. memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
  63. memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
  64. memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
  65. memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
  66. memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
  67. memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
  68. memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
  69. memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
  70. memset(runtime_cycles_in_tx_stats, 0,
  71. sizeof(runtime_cycles_in_tx_stats));
  72. memset(runtime_transaction_stats, 0,
  73. sizeof(runtime_transaction_stats));
  74. memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
  75. memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
  76. }
  77. /*
  78. * Update various tracking values we maintain to print
  79. * more semantic information such as miss/hit ratios,
  80. * instruction rates, etc:
  81. */
  82. void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
  83. int cpu)
  84. {
  85. int ctx = evsel_context(counter);
  86. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
  87. perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
  88. update_stats(&runtime_nsecs_stats[cpu], count[0]);
  89. else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
  90. update_stats(&runtime_cycles_stats[ctx][cpu], count[0]);
  91. else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
  92. update_stats(&runtime_cycles_in_tx_stats[ctx][cpu], count[0]);
  93. else if (perf_stat_evsel__is(counter, TRANSACTION_START))
  94. update_stats(&runtime_transaction_stats[ctx][cpu], count[0]);
  95. else if (perf_stat_evsel__is(counter, ELISION_START))
  96. update_stats(&runtime_elision_stats[ctx][cpu], count[0]);
  97. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
  98. update_stats(&runtime_stalled_cycles_front_stats[ctx][cpu], count[0]);
  99. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
  100. update_stats(&runtime_stalled_cycles_back_stats[ctx][cpu], count[0]);
  101. else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
  102. update_stats(&runtime_branches_stats[ctx][cpu], count[0]);
  103. else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
  104. update_stats(&runtime_cacherefs_stats[ctx][cpu], count[0]);
  105. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
  106. update_stats(&runtime_l1_dcache_stats[ctx][cpu], count[0]);
  107. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
  108. update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
  109. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
  110. update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
  111. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
  112. update_stats(&runtime_dtlb_cache_stats[ctx][cpu], count[0]);
  113. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
  114. update_stats(&runtime_itlb_cache_stats[ctx][cpu], count[0]);
  115. }
  116. /* used for get_ratio_color() */
  117. enum grc_type {
  118. GRC_STALLED_CYCLES_FE,
  119. GRC_STALLED_CYCLES_BE,
  120. GRC_CACHE_MISSES,
  121. GRC_MAX_NR
  122. };
  123. static const char *get_ratio_color(enum grc_type type, double ratio)
  124. {
  125. static const double grc_table[GRC_MAX_NR][3] = {
  126. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  127. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  128. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  129. };
  130. const char *color = PERF_COLOR_NORMAL;
  131. if (ratio > grc_table[type][0])
  132. color = PERF_COLOR_RED;
  133. else if (ratio > grc_table[type][1])
  134. color = PERF_COLOR_MAGENTA;
  135. else if (ratio > grc_table[type][2])
  136. color = PERF_COLOR_YELLOW;
  137. return color;
  138. }
  139. static void print_stalled_cycles_frontend(int cpu,
  140. struct perf_evsel *evsel, double avg,
  141. struct perf_stat_output_ctx *out)
  142. {
  143. double total, ratio = 0.0;
  144. const char *color;
  145. int ctx = evsel_context(evsel);
  146. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  147. if (total)
  148. ratio = avg / total * 100.0;
  149. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  150. if (ratio)
  151. out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
  152. ratio);
  153. else
  154. out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
  155. }
  156. static void print_stalled_cycles_backend(int cpu,
  157. struct perf_evsel *evsel, double avg,
  158. struct perf_stat_output_ctx *out)
  159. {
  160. double total, ratio = 0.0;
  161. const char *color;
  162. int ctx = evsel_context(evsel);
  163. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  164. if (total)
  165. ratio = avg / total * 100.0;
  166. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  167. out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
  168. }
  169. static void print_branch_misses(int cpu,
  170. struct perf_evsel *evsel,
  171. double avg,
  172. struct perf_stat_output_ctx *out)
  173. {
  174. double total, ratio = 0.0;
  175. const char *color;
  176. int ctx = evsel_context(evsel);
  177. total = avg_stats(&runtime_branches_stats[ctx][cpu]);
  178. if (total)
  179. ratio = avg / total * 100.0;
  180. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  181. out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
  182. }
  183. static void print_l1_dcache_misses(int cpu,
  184. struct perf_evsel *evsel,
  185. double avg,
  186. struct perf_stat_output_ctx *out)
  187. {
  188. double total, ratio = 0.0;
  189. const char *color;
  190. int ctx = evsel_context(evsel);
  191. total = avg_stats(&runtime_l1_dcache_stats[ctx][cpu]);
  192. if (total)
  193. ratio = avg / total * 100.0;
  194. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  195. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
  196. }
  197. static void print_l1_icache_misses(int cpu,
  198. struct perf_evsel *evsel,
  199. double avg,
  200. struct perf_stat_output_ctx *out)
  201. {
  202. double total, ratio = 0.0;
  203. const char *color;
  204. int ctx = evsel_context(evsel);
  205. total = avg_stats(&runtime_l1_icache_stats[ctx][cpu]);
  206. if (total)
  207. ratio = avg / total * 100.0;
  208. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  209. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
  210. }
  211. static void print_dtlb_cache_misses(int cpu,
  212. struct perf_evsel *evsel,
  213. double avg,
  214. struct perf_stat_output_ctx *out)
  215. {
  216. double total, ratio = 0.0;
  217. const char *color;
  218. int ctx = evsel_context(evsel);
  219. total = avg_stats(&runtime_dtlb_cache_stats[ctx][cpu]);
  220. if (total)
  221. ratio = avg / total * 100.0;
  222. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  223. out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
  224. }
  225. static void print_itlb_cache_misses(int cpu,
  226. struct perf_evsel *evsel,
  227. double avg,
  228. struct perf_stat_output_ctx *out)
  229. {
  230. double total, ratio = 0.0;
  231. const char *color;
  232. int ctx = evsel_context(evsel);
  233. total = avg_stats(&runtime_itlb_cache_stats[ctx][cpu]);
  234. if (total)
  235. ratio = avg / total * 100.0;
  236. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  237. out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
  238. }
  239. static void print_ll_cache_misses(int cpu,
  240. struct perf_evsel *evsel,
  241. double avg,
  242. struct perf_stat_output_ctx *out)
  243. {
  244. double total, ratio = 0.0;
  245. const char *color;
  246. int ctx = evsel_context(evsel);
  247. total = avg_stats(&runtime_ll_cache_stats[ctx][cpu]);
  248. if (total)
  249. ratio = avg / total * 100.0;
  250. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  251. out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
  252. }
  253. void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
  254. double avg, int cpu,
  255. struct perf_stat_output_ctx *out)
  256. {
  257. void *ctxp = out->ctx;
  258. print_metric_t print_metric = out->print_metric;
  259. double total, ratio = 0.0, total2;
  260. int ctx = evsel_context(evsel);
  261. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  262. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  263. if (total) {
  264. ratio = avg / total;
  265. print_metric(ctxp, NULL, "%7.2f ",
  266. "insn per cycle", ratio);
  267. } else {
  268. print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
  269. }
  270. total = avg_stats(&runtime_stalled_cycles_front_stats[ctx][cpu]);
  271. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[ctx][cpu]));
  272. if (total && avg) {
  273. out->new_line(ctxp);
  274. ratio = total / avg;
  275. print_metric(ctxp, NULL, "%7.2f ",
  276. "stalled cycles per insn",
  277. ratio);
  278. } else if (have_frontend_stalled) {
  279. print_metric(ctxp, NULL, NULL,
  280. "stalled cycles per insn", 0);
  281. }
  282. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
  283. if (runtime_branches_stats[ctx][cpu].n != 0)
  284. print_branch_misses(cpu, evsel, avg, out);
  285. else
  286. print_metric(ctxp, NULL, NULL, "of all branches", 0);
  287. } else if (
  288. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  289. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  290. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  291. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  292. if (runtime_l1_dcache_stats[ctx][cpu].n != 0)
  293. print_l1_dcache_misses(cpu, evsel, avg, out);
  294. else
  295. print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
  296. } else if (
  297. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  298. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  299. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  300. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  301. if (runtime_l1_icache_stats[ctx][cpu].n != 0)
  302. print_l1_icache_misses(cpu, evsel, avg, out);
  303. else
  304. print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
  305. } else if (
  306. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  307. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  308. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  309. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  310. if (runtime_dtlb_cache_stats[ctx][cpu].n != 0)
  311. print_dtlb_cache_misses(cpu, evsel, avg, out);
  312. else
  313. print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
  314. } else if (
  315. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  316. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  317. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  318. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  319. if (runtime_itlb_cache_stats[ctx][cpu].n != 0)
  320. print_itlb_cache_misses(cpu, evsel, avg, out);
  321. else
  322. print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
  323. } else if (
  324. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  325. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  326. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  327. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  328. if (runtime_ll_cache_stats[ctx][cpu].n != 0)
  329. print_ll_cache_misses(cpu, evsel, avg, out);
  330. else
  331. print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
  332. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
  333. total = avg_stats(&runtime_cacherefs_stats[ctx][cpu]);
  334. if (total)
  335. ratio = avg * 100 / total;
  336. if (runtime_cacherefs_stats[ctx][cpu].n != 0)
  337. print_metric(ctxp, NULL, "%8.3f %%",
  338. "of all cache refs", ratio);
  339. else
  340. print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
  341. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  342. print_stalled_cycles_frontend(cpu, evsel, avg, out);
  343. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  344. print_stalled_cycles_backend(cpu, evsel, avg, out);
  345. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  346. total = avg_stats(&runtime_nsecs_stats[cpu]);
  347. if (total) {
  348. ratio = avg / total;
  349. print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
  350. } else {
  351. print_metric(ctxp, NULL, NULL, "Ghz", 0);
  352. }
  353. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
  354. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  355. if (total)
  356. print_metric(ctxp, NULL,
  357. "%7.2f%%", "transactional cycles",
  358. 100.0 * (avg / total));
  359. else
  360. print_metric(ctxp, NULL, NULL, "transactional cycles",
  361. 0);
  362. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
  363. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  364. total2 = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  365. if (total2 < avg)
  366. total2 = avg;
  367. if (total)
  368. print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
  369. 100.0 * ((total2-avg) / total));
  370. else
  371. print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
  372. } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
  373. total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  374. if (avg)
  375. ratio = total / avg;
  376. if (runtime_cycles_in_tx_stats[ctx][cpu].n != 0)
  377. print_metric(ctxp, NULL, "%8.0f",
  378. "cycles / transaction", ratio);
  379. else
  380. print_metric(ctxp, NULL, NULL, "cycles / transaction",
  381. 0);
  382. } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
  383. total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  384. if (avg)
  385. ratio = total / avg;
  386. print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
  387. } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) ||
  388. perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK)) {
  389. if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
  390. print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
  391. avg / ratio);
  392. else
  393. print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
  394. } else if (runtime_nsecs_stats[cpu].n != 0) {
  395. char unit = 'M';
  396. char unit_buf[10];
  397. total = avg_stats(&runtime_nsecs_stats[cpu]);
  398. if (total)
  399. ratio = 1000.0 * avg / total;
  400. if (ratio < 0.001) {
  401. ratio *= 1000;
  402. unit = 'K';
  403. }
  404. snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
  405. print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
  406. } else {
  407. print_metric(ctxp, NULL, NULL, NULL, 0);
  408. }
  409. }