stat-shadow.c 28 KB

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