stat-shadow.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include "evsel.h"
  4. #include "stat.h"
  5. #include "color.h"
  6. #include "pmu.h"
  7. #include "rblist.h"
  8. #include "evlist.h"
  9. #include "expr.h"
  10. enum {
  11. CTX_BIT_USER = 1 << 0,
  12. CTX_BIT_KERNEL = 1 << 1,
  13. CTX_BIT_HV = 1 << 2,
  14. CTX_BIT_HOST = 1 << 3,
  15. CTX_BIT_IDLE = 1 << 4,
  16. CTX_BIT_MAX = 1 << 5,
  17. };
  18. #define NUM_CTX CTX_BIT_MAX
  19. /*
  20. * AGGR_GLOBAL: Use CPU 0
  21. * AGGR_SOCKET: Use first CPU of socket
  22. * AGGR_CORE: Use first CPU of core
  23. * AGGR_NONE: Use matching CPU
  24. * AGGR_THREAD: Not supported?
  25. */
  26. static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
  27. static struct stats runtime_cycles_stats[NUM_CTX][MAX_NR_CPUS];
  28. static struct stats runtime_stalled_cycles_front_stats[NUM_CTX][MAX_NR_CPUS];
  29. static struct stats runtime_stalled_cycles_back_stats[NUM_CTX][MAX_NR_CPUS];
  30. static struct stats runtime_branches_stats[NUM_CTX][MAX_NR_CPUS];
  31. static struct stats runtime_cacherefs_stats[NUM_CTX][MAX_NR_CPUS];
  32. static struct stats runtime_l1_dcache_stats[NUM_CTX][MAX_NR_CPUS];
  33. static struct stats runtime_l1_icache_stats[NUM_CTX][MAX_NR_CPUS];
  34. static struct stats runtime_ll_cache_stats[NUM_CTX][MAX_NR_CPUS];
  35. static struct stats runtime_itlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
  36. static struct stats runtime_dtlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
  37. static struct stats runtime_cycles_in_tx_stats[NUM_CTX][MAX_NR_CPUS];
  38. static struct stats runtime_transaction_stats[NUM_CTX][MAX_NR_CPUS];
  39. static struct stats runtime_elision_stats[NUM_CTX][MAX_NR_CPUS];
  40. static struct stats runtime_topdown_total_slots[NUM_CTX][MAX_NR_CPUS];
  41. static struct stats runtime_topdown_slots_issued[NUM_CTX][MAX_NR_CPUS];
  42. static struct stats runtime_topdown_slots_retired[NUM_CTX][MAX_NR_CPUS];
  43. static struct stats runtime_topdown_fetch_bubbles[NUM_CTX][MAX_NR_CPUS];
  44. static struct stats runtime_topdown_recovery_bubbles[NUM_CTX][MAX_NR_CPUS];
  45. static struct stats runtime_smi_num_stats[NUM_CTX][MAX_NR_CPUS];
  46. static struct stats runtime_aperf_stats[NUM_CTX][MAX_NR_CPUS];
  47. static struct rblist runtime_saved_values;
  48. static bool have_frontend_stalled;
  49. struct stats walltime_nsecs_stats;
  50. struct saved_value {
  51. struct rb_node rb_node;
  52. struct perf_evsel *evsel;
  53. int cpu;
  54. int ctx;
  55. struct stats stats;
  56. };
  57. static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
  58. {
  59. struct saved_value *a = container_of(rb_node,
  60. struct saved_value,
  61. rb_node);
  62. const struct saved_value *b = entry;
  63. if (a->ctx != b->ctx)
  64. return a->ctx - b->ctx;
  65. if (a->cpu != b->cpu)
  66. return a->cpu - b->cpu;
  67. if (a->evsel == b->evsel)
  68. return 0;
  69. if ((char *)a->evsel < (char *)b->evsel)
  70. return -1;
  71. return +1;
  72. }
  73. static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
  74. const void *entry)
  75. {
  76. struct saved_value *nd = malloc(sizeof(struct saved_value));
  77. if (!nd)
  78. return NULL;
  79. memcpy(nd, entry, sizeof(struct saved_value));
  80. return &nd->rb_node;
  81. }
  82. static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
  83. int cpu, int ctx,
  84. bool create)
  85. {
  86. struct rb_node *nd;
  87. struct saved_value dm = {
  88. .cpu = cpu,
  89. .ctx = ctx,
  90. .evsel = evsel,
  91. };
  92. nd = rblist__find(&runtime_saved_values, &dm);
  93. if (nd)
  94. return container_of(nd, struct saved_value, rb_node);
  95. if (create) {
  96. rblist__add_node(&runtime_saved_values, &dm);
  97. nd = rblist__find(&runtime_saved_values, &dm);
  98. if (nd)
  99. return container_of(nd, struct saved_value, rb_node);
  100. }
  101. return NULL;
  102. }
  103. void perf_stat__init_shadow_stats(void)
  104. {
  105. have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
  106. rblist__init(&runtime_saved_values);
  107. runtime_saved_values.node_cmp = saved_value_cmp;
  108. runtime_saved_values.node_new = saved_value_new;
  109. /* No delete for now */
  110. }
  111. static int evsel_context(struct perf_evsel *evsel)
  112. {
  113. int ctx = 0;
  114. if (evsel->attr.exclude_kernel)
  115. ctx |= CTX_BIT_KERNEL;
  116. if (evsel->attr.exclude_user)
  117. ctx |= CTX_BIT_USER;
  118. if (evsel->attr.exclude_hv)
  119. ctx |= CTX_BIT_HV;
  120. if (evsel->attr.exclude_host)
  121. ctx |= CTX_BIT_HOST;
  122. if (evsel->attr.exclude_idle)
  123. ctx |= CTX_BIT_IDLE;
  124. return ctx;
  125. }
  126. void perf_stat__reset_shadow_stats(void)
  127. {
  128. struct rb_node *pos, *next;
  129. memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
  130. memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
  131. memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
  132. memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
  133. memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
  134. memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
  135. memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
  136. memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
  137. memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
  138. memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
  139. memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
  140. memset(runtime_cycles_in_tx_stats, 0,
  141. sizeof(runtime_cycles_in_tx_stats));
  142. memset(runtime_transaction_stats, 0,
  143. sizeof(runtime_transaction_stats));
  144. memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
  145. memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
  146. memset(runtime_topdown_total_slots, 0, sizeof(runtime_topdown_total_slots));
  147. memset(runtime_topdown_slots_retired, 0, sizeof(runtime_topdown_slots_retired));
  148. memset(runtime_topdown_slots_issued, 0, sizeof(runtime_topdown_slots_issued));
  149. memset(runtime_topdown_fetch_bubbles, 0, sizeof(runtime_topdown_fetch_bubbles));
  150. memset(runtime_topdown_recovery_bubbles, 0, sizeof(runtime_topdown_recovery_bubbles));
  151. memset(runtime_smi_num_stats, 0, sizeof(runtime_smi_num_stats));
  152. memset(runtime_aperf_stats, 0, sizeof(runtime_aperf_stats));
  153. next = rb_first(&runtime_saved_values.entries);
  154. while (next) {
  155. pos = next;
  156. next = rb_next(pos);
  157. memset(&container_of(pos, struct saved_value, rb_node)->stats,
  158. 0,
  159. sizeof(struct stats));
  160. }
  161. }
  162. /*
  163. * Update various tracking values we maintain to print
  164. * more semantic information such as miss/hit ratios,
  165. * instruction rates, etc:
  166. */
  167. void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
  168. int cpu)
  169. {
  170. int ctx = evsel_context(counter);
  171. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
  172. perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
  173. update_stats(&runtime_nsecs_stats[cpu], count[0]);
  174. else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
  175. update_stats(&runtime_cycles_stats[ctx][cpu], count[0]);
  176. else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
  177. update_stats(&runtime_cycles_in_tx_stats[ctx][cpu], count[0]);
  178. else if (perf_stat_evsel__is(counter, TRANSACTION_START))
  179. update_stats(&runtime_transaction_stats[ctx][cpu], count[0]);
  180. else if (perf_stat_evsel__is(counter, ELISION_START))
  181. update_stats(&runtime_elision_stats[ctx][cpu], count[0]);
  182. else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
  183. update_stats(&runtime_topdown_total_slots[ctx][cpu], count[0]);
  184. else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
  185. update_stats(&runtime_topdown_slots_issued[ctx][cpu], count[0]);
  186. else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
  187. update_stats(&runtime_topdown_slots_retired[ctx][cpu], count[0]);
  188. else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
  189. update_stats(&runtime_topdown_fetch_bubbles[ctx][cpu],count[0]);
  190. else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
  191. update_stats(&runtime_topdown_recovery_bubbles[ctx][cpu], count[0]);
  192. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
  193. update_stats(&runtime_stalled_cycles_front_stats[ctx][cpu], count[0]);
  194. else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
  195. update_stats(&runtime_stalled_cycles_back_stats[ctx][cpu], count[0]);
  196. else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
  197. update_stats(&runtime_branches_stats[ctx][cpu], count[0]);
  198. else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
  199. update_stats(&runtime_cacherefs_stats[ctx][cpu], count[0]);
  200. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
  201. update_stats(&runtime_l1_dcache_stats[ctx][cpu], count[0]);
  202. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
  203. update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
  204. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
  205. update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
  206. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
  207. update_stats(&runtime_dtlb_cache_stats[ctx][cpu], count[0]);
  208. else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
  209. update_stats(&runtime_itlb_cache_stats[ctx][cpu], count[0]);
  210. else if (perf_stat_evsel__is(counter, SMI_NUM))
  211. update_stats(&runtime_smi_num_stats[ctx][cpu], count[0]);
  212. else if (perf_stat_evsel__is(counter, APERF))
  213. update_stats(&runtime_aperf_stats[ctx][cpu], count[0]);
  214. if (counter->collect_stat) {
  215. struct saved_value *v = saved_value_lookup(counter, cpu, ctx,
  216. true);
  217. update_stats(&v->stats, count[0]);
  218. }
  219. }
  220. /* used for get_ratio_color() */
  221. enum grc_type {
  222. GRC_STALLED_CYCLES_FE,
  223. GRC_STALLED_CYCLES_BE,
  224. GRC_CACHE_MISSES,
  225. GRC_MAX_NR
  226. };
  227. static const char *get_ratio_color(enum grc_type type, double ratio)
  228. {
  229. static const double grc_table[GRC_MAX_NR][3] = {
  230. [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
  231. [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
  232. [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
  233. };
  234. const char *color = PERF_COLOR_NORMAL;
  235. if (ratio > grc_table[type][0])
  236. color = PERF_COLOR_RED;
  237. else if (ratio > grc_table[type][1])
  238. color = PERF_COLOR_MAGENTA;
  239. else if (ratio > grc_table[type][2])
  240. color = PERF_COLOR_YELLOW;
  241. return color;
  242. }
  243. static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,
  244. const char *name)
  245. {
  246. struct perf_evsel *c2;
  247. evlist__for_each_entry (evsel_list, c2) {
  248. if (!strcasecmp(c2->name, name) && !c2->collect_stat)
  249. return c2;
  250. }
  251. return NULL;
  252. }
  253. /* Mark MetricExpr target events and link events using them to them. */
  254. void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)
  255. {
  256. struct perf_evsel *counter, *leader, **metric_events, *oc;
  257. bool found;
  258. const char **metric_names;
  259. int i;
  260. int num_metric_names;
  261. evlist__for_each_entry(evsel_list, counter) {
  262. bool invalid = false;
  263. leader = counter->leader;
  264. if (!counter->metric_expr)
  265. continue;
  266. metric_events = counter->metric_events;
  267. if (!metric_events) {
  268. if (expr__find_other(counter->metric_expr, counter->name,
  269. &metric_names, &num_metric_names) < 0)
  270. continue;
  271. metric_events = calloc(sizeof(struct perf_evsel *),
  272. num_metric_names + 1);
  273. if (!metric_events)
  274. return;
  275. counter->metric_events = metric_events;
  276. }
  277. for (i = 0; i < num_metric_names; i++) {
  278. found = false;
  279. if (leader) {
  280. /* Search in group */
  281. for_each_group_member (oc, leader) {
  282. if (!strcasecmp(oc->name, metric_names[i]) &&
  283. !oc->collect_stat) {
  284. found = true;
  285. break;
  286. }
  287. }
  288. }
  289. if (!found) {
  290. /* Search ignoring groups */
  291. oc = perf_stat__find_event(evsel_list, metric_names[i]);
  292. }
  293. if (!oc) {
  294. /* Deduping one is good enough to handle duplicated PMUs. */
  295. static char *printed;
  296. /*
  297. * Adding events automatically would be difficult, because
  298. * it would risk creating groups that are not schedulable.
  299. * perf stat doesn't understand all the scheduling constraints
  300. * of events. So we ask the user instead to add the missing
  301. * events.
  302. */
  303. if (!printed || strcasecmp(printed, metric_names[i])) {
  304. fprintf(stderr,
  305. "Add %s event to groups to get metric expression for %s\n",
  306. metric_names[i],
  307. counter->name);
  308. printed = strdup(metric_names[i]);
  309. }
  310. invalid = true;
  311. continue;
  312. }
  313. metric_events[i] = oc;
  314. oc->collect_stat = true;
  315. }
  316. metric_events[i] = NULL;
  317. free(metric_names);
  318. if (invalid) {
  319. free(metric_events);
  320. counter->metric_events = NULL;
  321. counter->metric_expr = NULL;
  322. }
  323. }
  324. }
  325. static void print_stalled_cycles_frontend(int cpu,
  326. struct perf_evsel *evsel, double avg,
  327. struct perf_stat_output_ctx *out)
  328. {
  329. double total, ratio = 0.0;
  330. const char *color;
  331. int ctx = evsel_context(evsel);
  332. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  333. if (total)
  334. ratio = avg / total * 100.0;
  335. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  336. if (ratio)
  337. out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
  338. ratio);
  339. else
  340. out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
  341. }
  342. static void print_stalled_cycles_backend(int cpu,
  343. struct perf_evsel *evsel, double avg,
  344. struct perf_stat_output_ctx *out)
  345. {
  346. double total, ratio = 0.0;
  347. const char *color;
  348. int ctx = evsel_context(evsel);
  349. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  350. if (total)
  351. ratio = avg / total * 100.0;
  352. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  353. out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
  354. }
  355. static void print_branch_misses(int cpu,
  356. struct perf_evsel *evsel,
  357. double avg,
  358. struct perf_stat_output_ctx *out)
  359. {
  360. double total, ratio = 0.0;
  361. const char *color;
  362. int ctx = evsel_context(evsel);
  363. total = avg_stats(&runtime_branches_stats[ctx][cpu]);
  364. if (total)
  365. ratio = avg / total * 100.0;
  366. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  367. out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
  368. }
  369. static void print_l1_dcache_misses(int cpu,
  370. struct perf_evsel *evsel,
  371. double avg,
  372. struct perf_stat_output_ctx *out)
  373. {
  374. double total, ratio = 0.0;
  375. const char *color;
  376. int ctx = evsel_context(evsel);
  377. total = avg_stats(&runtime_l1_dcache_stats[ctx][cpu]);
  378. if (total)
  379. ratio = avg / total * 100.0;
  380. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  381. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
  382. }
  383. static void print_l1_icache_misses(int cpu,
  384. struct perf_evsel *evsel,
  385. double avg,
  386. struct perf_stat_output_ctx *out)
  387. {
  388. double total, ratio = 0.0;
  389. const char *color;
  390. int ctx = evsel_context(evsel);
  391. total = avg_stats(&runtime_l1_icache_stats[ctx][cpu]);
  392. if (total)
  393. ratio = avg / total * 100.0;
  394. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  395. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
  396. }
  397. static void print_dtlb_cache_misses(int cpu,
  398. struct perf_evsel *evsel,
  399. double avg,
  400. struct perf_stat_output_ctx *out)
  401. {
  402. double total, ratio = 0.0;
  403. const char *color;
  404. int ctx = evsel_context(evsel);
  405. total = avg_stats(&runtime_dtlb_cache_stats[ctx][cpu]);
  406. if (total)
  407. ratio = avg / total * 100.0;
  408. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  409. out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
  410. }
  411. static void print_itlb_cache_misses(int cpu,
  412. struct perf_evsel *evsel,
  413. double avg,
  414. struct perf_stat_output_ctx *out)
  415. {
  416. double total, ratio = 0.0;
  417. const char *color;
  418. int ctx = evsel_context(evsel);
  419. total = avg_stats(&runtime_itlb_cache_stats[ctx][cpu]);
  420. if (total)
  421. ratio = avg / total * 100.0;
  422. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  423. out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
  424. }
  425. static void print_ll_cache_misses(int cpu,
  426. struct perf_evsel *evsel,
  427. double avg,
  428. struct perf_stat_output_ctx *out)
  429. {
  430. double total, ratio = 0.0;
  431. const char *color;
  432. int ctx = evsel_context(evsel);
  433. total = avg_stats(&runtime_ll_cache_stats[ctx][cpu]);
  434. if (total)
  435. ratio = avg / total * 100.0;
  436. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  437. out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
  438. }
  439. /*
  440. * High level "TopDown" CPU core pipe line bottleneck break down.
  441. *
  442. * Basic concept following
  443. * Yasin, A Top Down Method for Performance analysis and Counter architecture
  444. * ISPASS14
  445. *
  446. * The CPU pipeline is divided into 4 areas that can be bottlenecks:
  447. *
  448. * Frontend -> Backend -> Retiring
  449. * BadSpeculation in addition means out of order execution that is thrown away
  450. * (for example branch mispredictions)
  451. * Frontend is instruction decoding.
  452. * Backend is execution, like computation and accessing data in memory
  453. * Retiring is good execution that is not directly bottlenecked
  454. *
  455. * The formulas are computed in slots.
  456. * A slot is an entry in the pipeline each for the pipeline width
  457. * (for example a 4-wide pipeline has 4 slots for each cycle)
  458. *
  459. * Formulas:
  460. * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
  461. * TotalSlots
  462. * Retiring = SlotsRetired / TotalSlots
  463. * FrontendBound = FetchBubbles / TotalSlots
  464. * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
  465. *
  466. * The kernel provides the mapping to the low level CPU events and any scaling
  467. * needed for the CPU pipeline width, for example:
  468. *
  469. * TotalSlots = Cycles * 4
  470. *
  471. * The scaling factor is communicated in the sysfs unit.
  472. *
  473. * In some cases the CPU may not be able to measure all the formulas due to
  474. * missing events. In this case multiple formulas are combined, as possible.
  475. *
  476. * Full TopDown supports more levels to sub-divide each area: for example
  477. * BackendBound into computing bound and memory bound. For now we only
  478. * support Level 1 TopDown.
  479. */
  480. static double sanitize_val(double x)
  481. {
  482. if (x < 0 && x >= -0.02)
  483. return 0.0;
  484. return x;
  485. }
  486. static double td_total_slots(int ctx, int cpu)
  487. {
  488. return avg_stats(&runtime_topdown_total_slots[ctx][cpu]);
  489. }
  490. static double td_bad_spec(int ctx, int cpu)
  491. {
  492. double bad_spec = 0;
  493. double total_slots;
  494. double total;
  495. total = avg_stats(&runtime_topdown_slots_issued[ctx][cpu]) -
  496. avg_stats(&runtime_topdown_slots_retired[ctx][cpu]) +
  497. avg_stats(&runtime_topdown_recovery_bubbles[ctx][cpu]);
  498. total_slots = td_total_slots(ctx, cpu);
  499. if (total_slots)
  500. bad_spec = total / total_slots;
  501. return sanitize_val(bad_spec);
  502. }
  503. static double td_retiring(int ctx, int cpu)
  504. {
  505. double retiring = 0;
  506. double total_slots = td_total_slots(ctx, cpu);
  507. double ret_slots = avg_stats(&runtime_topdown_slots_retired[ctx][cpu]);
  508. if (total_slots)
  509. retiring = ret_slots / total_slots;
  510. return retiring;
  511. }
  512. static double td_fe_bound(int ctx, int cpu)
  513. {
  514. double fe_bound = 0;
  515. double total_slots = td_total_slots(ctx, cpu);
  516. double fetch_bub = avg_stats(&runtime_topdown_fetch_bubbles[ctx][cpu]);
  517. if (total_slots)
  518. fe_bound = fetch_bub / total_slots;
  519. return fe_bound;
  520. }
  521. static double td_be_bound(int ctx, int cpu)
  522. {
  523. double sum = (td_fe_bound(ctx, cpu) +
  524. td_bad_spec(ctx, cpu) +
  525. td_retiring(ctx, cpu));
  526. if (sum == 0)
  527. return 0;
  528. return sanitize_val(1.0 - sum);
  529. }
  530. static void print_smi_cost(int cpu, struct perf_evsel *evsel,
  531. struct perf_stat_output_ctx *out)
  532. {
  533. double smi_num, aperf, cycles, cost = 0.0;
  534. int ctx = evsel_context(evsel);
  535. const char *color = NULL;
  536. smi_num = avg_stats(&runtime_smi_num_stats[ctx][cpu]);
  537. aperf = avg_stats(&runtime_aperf_stats[ctx][cpu]);
  538. cycles = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  539. if ((cycles == 0) || (aperf == 0))
  540. return;
  541. if (smi_num)
  542. cost = (aperf - cycles) / aperf * 100.00;
  543. if (cost > 10)
  544. color = PERF_COLOR_RED;
  545. out->print_metric(out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
  546. out->print_metric(out->ctx, NULL, "%4.0f", "SMI#", smi_num);
  547. }
  548. void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
  549. double avg, int cpu,
  550. struct perf_stat_output_ctx *out)
  551. {
  552. void *ctxp = out->ctx;
  553. print_metric_t print_metric = out->print_metric;
  554. double total, ratio = 0.0, total2;
  555. const char *color = NULL;
  556. int ctx = evsel_context(evsel);
  557. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  558. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  559. if (total) {
  560. ratio = avg / total;
  561. print_metric(ctxp, NULL, "%7.2f ",
  562. "insn per cycle", ratio);
  563. } else {
  564. print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
  565. }
  566. total = avg_stats(&runtime_stalled_cycles_front_stats[ctx][cpu]);
  567. total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[ctx][cpu]));
  568. if (total && avg) {
  569. out->new_line(ctxp);
  570. ratio = total / avg;
  571. print_metric(ctxp, NULL, "%7.2f ",
  572. "stalled cycles per insn",
  573. ratio);
  574. } else if (have_frontend_stalled) {
  575. print_metric(ctxp, NULL, NULL,
  576. "stalled cycles per insn", 0);
  577. }
  578. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
  579. if (runtime_branches_stats[ctx][cpu].n != 0)
  580. print_branch_misses(cpu, evsel, avg, out);
  581. else
  582. print_metric(ctxp, NULL, NULL, "of all branches", 0);
  583. } else if (
  584. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  585. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  586. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  587. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  588. if (runtime_l1_dcache_stats[ctx][cpu].n != 0)
  589. print_l1_dcache_misses(cpu, evsel, avg, out);
  590. else
  591. print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
  592. } else if (
  593. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  594. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  595. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  596. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  597. if (runtime_l1_icache_stats[ctx][cpu].n != 0)
  598. print_l1_icache_misses(cpu, evsel, avg, out);
  599. else
  600. print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
  601. } else if (
  602. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  603. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  604. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  605. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  606. if (runtime_dtlb_cache_stats[ctx][cpu].n != 0)
  607. print_dtlb_cache_misses(cpu, evsel, avg, out);
  608. else
  609. print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
  610. } else if (
  611. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  612. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  613. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  614. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  615. if (runtime_itlb_cache_stats[ctx][cpu].n != 0)
  616. print_itlb_cache_misses(cpu, evsel, avg, out);
  617. else
  618. print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
  619. } else if (
  620. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  621. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  622. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  623. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  624. if (runtime_ll_cache_stats[ctx][cpu].n != 0)
  625. print_ll_cache_misses(cpu, evsel, avg, out);
  626. else
  627. print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
  628. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
  629. total = avg_stats(&runtime_cacherefs_stats[ctx][cpu]);
  630. if (total)
  631. ratio = avg * 100 / total;
  632. if (runtime_cacherefs_stats[ctx][cpu].n != 0)
  633. print_metric(ctxp, NULL, "%8.3f %%",
  634. "of all cache refs", ratio);
  635. else
  636. print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
  637. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  638. print_stalled_cycles_frontend(cpu, evsel, avg, out);
  639. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  640. print_stalled_cycles_backend(cpu, evsel, avg, out);
  641. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  642. total = avg_stats(&runtime_nsecs_stats[cpu]);
  643. if (total) {
  644. ratio = avg / total;
  645. print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
  646. } else {
  647. print_metric(ctxp, NULL, NULL, "Ghz", 0);
  648. }
  649. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
  650. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  651. if (total)
  652. print_metric(ctxp, NULL,
  653. "%7.2f%%", "transactional cycles",
  654. 100.0 * (avg / total));
  655. else
  656. print_metric(ctxp, NULL, NULL, "transactional cycles",
  657. 0);
  658. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
  659. total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
  660. total2 = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  661. if (total2 < avg)
  662. total2 = avg;
  663. if (total)
  664. print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
  665. 100.0 * ((total2-avg) / total));
  666. else
  667. print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
  668. } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
  669. total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  670. if (avg)
  671. ratio = total / avg;
  672. if (runtime_cycles_in_tx_stats[ctx][cpu].n != 0)
  673. print_metric(ctxp, NULL, "%8.0f",
  674. "cycles / transaction", ratio);
  675. else
  676. print_metric(ctxp, NULL, NULL, "cycles / transaction",
  677. 0);
  678. } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
  679. total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
  680. if (avg)
  681. ratio = total / avg;
  682. print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
  683. } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) ||
  684. perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK)) {
  685. if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
  686. print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
  687. avg / ratio);
  688. else
  689. print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
  690. } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
  691. double fe_bound = td_fe_bound(ctx, cpu);
  692. if (fe_bound > 0.2)
  693. color = PERF_COLOR_RED;
  694. print_metric(ctxp, color, "%8.1f%%", "frontend bound",
  695. fe_bound * 100.);
  696. } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
  697. double retiring = td_retiring(ctx, cpu);
  698. if (retiring > 0.7)
  699. color = PERF_COLOR_GREEN;
  700. print_metric(ctxp, color, "%8.1f%%", "retiring",
  701. retiring * 100.);
  702. } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
  703. double bad_spec = td_bad_spec(ctx, cpu);
  704. if (bad_spec > 0.1)
  705. color = PERF_COLOR_RED;
  706. print_metric(ctxp, color, "%8.1f%%", "bad speculation",
  707. bad_spec * 100.);
  708. } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
  709. double be_bound = td_be_bound(ctx, cpu);
  710. const char *name = "backend bound";
  711. static int have_recovery_bubbles = -1;
  712. /* In case the CPU does not support topdown-recovery-bubbles */
  713. if (have_recovery_bubbles < 0)
  714. have_recovery_bubbles = pmu_have_event("cpu",
  715. "topdown-recovery-bubbles");
  716. if (!have_recovery_bubbles)
  717. name = "backend bound/bad spec";
  718. if (be_bound > 0.2)
  719. color = PERF_COLOR_RED;
  720. if (td_total_slots(ctx, cpu) > 0)
  721. print_metric(ctxp, color, "%8.1f%%", name,
  722. be_bound * 100.);
  723. else
  724. print_metric(ctxp, NULL, NULL, name, 0);
  725. } else if (evsel->metric_expr) {
  726. struct parse_ctx pctx;
  727. int i;
  728. expr__ctx_init(&pctx);
  729. expr__add_id(&pctx, evsel->name, avg);
  730. for (i = 0; evsel->metric_events[i]; i++) {
  731. struct saved_value *v;
  732. v = saved_value_lookup(evsel->metric_events[i], cpu, ctx, false);
  733. if (!v)
  734. break;
  735. expr__add_id(&pctx, evsel->metric_events[i]->name,
  736. avg_stats(&v->stats));
  737. }
  738. if (!evsel->metric_events[i]) {
  739. const char *p = evsel->metric_expr;
  740. if (expr__parse(&ratio, &pctx, &p) == 0)
  741. print_metric(ctxp, NULL, "%8.1f",
  742. evsel->metric_name ?
  743. evsel->metric_name :
  744. out->force_header ? evsel->name : "",
  745. ratio);
  746. else
  747. print_metric(ctxp, NULL, NULL, "", 0);
  748. } else
  749. print_metric(ctxp, NULL, NULL, "", 0);
  750. } else if (runtime_nsecs_stats[cpu].n != 0) {
  751. char unit = 'M';
  752. char unit_buf[10];
  753. total = avg_stats(&runtime_nsecs_stats[cpu]);
  754. if (total)
  755. ratio = 1000.0 * avg / total;
  756. if (ratio < 0.001) {
  757. ratio *= 1000;
  758. unit = 'K';
  759. }
  760. snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
  761. print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
  762. } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
  763. print_smi_cost(cpu, evsel, out);
  764. } else {
  765. print_metric(ctxp, NULL, NULL, NULL, 0);
  766. }
  767. }