stat-shadow.c 29 KB

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