stat-shadow.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. count *= counter->scale;
  181. if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
  182. perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
  183. update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
  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))
  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. found = true;
  301. break;
  302. }
  303. }
  304. }
  305. if (!found) {
  306. /* Search ignoring groups */
  307. oc = perf_stat__find_event(evsel_list, metric_names[i]);
  308. }
  309. if (!oc) {
  310. /* Deduping one is good enough to handle duplicated PMUs. */
  311. static char *printed;
  312. /*
  313. * Adding events automatically would be difficult, because
  314. * it would risk creating groups that are not schedulable.
  315. * perf stat doesn't understand all the scheduling constraints
  316. * of events. So we ask the user instead to add the missing
  317. * events.
  318. */
  319. if (!printed || strcasecmp(printed, metric_names[i])) {
  320. fprintf(stderr,
  321. "Add %s event to groups to get metric expression for %s\n",
  322. metric_names[i],
  323. counter->name);
  324. printed = strdup(metric_names[i]);
  325. }
  326. invalid = true;
  327. continue;
  328. }
  329. metric_events[i] = oc;
  330. oc->collect_stat = true;
  331. }
  332. metric_events[i] = NULL;
  333. free(metric_names);
  334. if (invalid) {
  335. free(metric_events);
  336. counter->metric_events = NULL;
  337. counter->metric_expr = NULL;
  338. }
  339. }
  340. }
  341. static double runtime_stat_avg(struct runtime_stat *st,
  342. enum stat_type type, int ctx, int cpu)
  343. {
  344. struct saved_value *v;
  345. v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
  346. if (!v)
  347. return 0.0;
  348. return avg_stats(&v->stats);
  349. }
  350. static double runtime_stat_n(struct runtime_stat *st,
  351. enum stat_type type, int ctx, int cpu)
  352. {
  353. struct saved_value *v;
  354. v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
  355. if (!v)
  356. return 0.0;
  357. return v->stats.n;
  358. }
  359. static void print_stalled_cycles_frontend(int cpu,
  360. struct perf_evsel *evsel, double avg,
  361. struct perf_stat_output_ctx *out,
  362. struct runtime_stat *st)
  363. {
  364. double total, ratio = 0.0;
  365. const char *color;
  366. int ctx = evsel_context(evsel);
  367. total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  368. if (total)
  369. ratio = avg / total * 100.0;
  370. color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
  371. if (ratio)
  372. out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
  373. ratio);
  374. else
  375. out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
  376. }
  377. static void print_stalled_cycles_backend(int cpu,
  378. struct perf_evsel *evsel, double avg,
  379. struct perf_stat_output_ctx *out,
  380. struct runtime_stat *st)
  381. {
  382. double total, ratio = 0.0;
  383. const char *color;
  384. int ctx = evsel_context(evsel);
  385. total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  386. if (total)
  387. ratio = avg / total * 100.0;
  388. color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
  389. out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
  390. }
  391. static void print_branch_misses(int cpu,
  392. struct perf_evsel *evsel,
  393. double avg,
  394. struct perf_stat_output_ctx *out,
  395. struct runtime_stat *st)
  396. {
  397. double total, ratio = 0.0;
  398. const char *color;
  399. int ctx = evsel_context(evsel);
  400. total = runtime_stat_avg(st, STAT_BRANCHES, ctx, cpu);
  401. if (total)
  402. ratio = avg / total * 100.0;
  403. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  404. out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
  405. }
  406. static void print_l1_dcache_misses(int cpu,
  407. struct perf_evsel *evsel,
  408. double avg,
  409. struct perf_stat_output_ctx *out,
  410. struct runtime_stat *st)
  411. {
  412. double total, ratio = 0.0;
  413. const char *color;
  414. int ctx = evsel_context(evsel);
  415. total = runtime_stat_avg(st, STAT_L1_DCACHE, ctx, cpu);
  416. if (total)
  417. ratio = avg / total * 100.0;
  418. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  419. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
  420. }
  421. static void print_l1_icache_misses(int cpu,
  422. struct perf_evsel *evsel,
  423. double avg,
  424. struct perf_stat_output_ctx *out,
  425. struct runtime_stat *st)
  426. {
  427. double total, ratio = 0.0;
  428. const char *color;
  429. int ctx = evsel_context(evsel);
  430. total = runtime_stat_avg(st, STAT_L1_ICACHE, ctx, cpu);
  431. if (total)
  432. ratio = avg / total * 100.0;
  433. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  434. out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
  435. }
  436. static void print_dtlb_cache_misses(int cpu,
  437. struct perf_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_DTLB_CACHE, ctx, cpu);
  446. if (total)
  447. ratio = avg / total * 100.0;
  448. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  449. out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
  450. }
  451. static void print_itlb_cache_misses(int cpu,
  452. struct perf_evsel *evsel,
  453. double avg,
  454. struct perf_stat_output_ctx *out,
  455. struct runtime_stat *st)
  456. {
  457. double total, ratio = 0.0;
  458. const char *color;
  459. int ctx = evsel_context(evsel);
  460. total = runtime_stat_avg(st, STAT_ITLB_CACHE, ctx, cpu);
  461. if (total)
  462. ratio = avg / total * 100.0;
  463. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  464. out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
  465. }
  466. static void print_ll_cache_misses(int cpu,
  467. struct perf_evsel *evsel,
  468. double avg,
  469. struct perf_stat_output_ctx *out,
  470. struct runtime_stat *st)
  471. {
  472. double total, ratio = 0.0;
  473. const char *color;
  474. int ctx = evsel_context(evsel);
  475. total = runtime_stat_avg(st, STAT_LL_CACHE, ctx, cpu);
  476. if (total)
  477. ratio = avg / total * 100.0;
  478. color = get_ratio_color(GRC_CACHE_MISSES, ratio);
  479. out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
  480. }
  481. /*
  482. * High level "TopDown" CPU core pipe line bottleneck break down.
  483. *
  484. * Basic concept following
  485. * Yasin, A Top Down Method for Performance analysis and Counter architecture
  486. * ISPASS14
  487. *
  488. * The CPU pipeline is divided into 4 areas that can be bottlenecks:
  489. *
  490. * Frontend -> Backend -> Retiring
  491. * BadSpeculation in addition means out of order execution that is thrown away
  492. * (for example branch mispredictions)
  493. * Frontend is instruction decoding.
  494. * Backend is execution, like computation and accessing data in memory
  495. * Retiring is good execution that is not directly bottlenecked
  496. *
  497. * The formulas are computed in slots.
  498. * A slot is an entry in the pipeline each for the pipeline width
  499. * (for example a 4-wide pipeline has 4 slots for each cycle)
  500. *
  501. * Formulas:
  502. * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
  503. * TotalSlots
  504. * Retiring = SlotsRetired / TotalSlots
  505. * FrontendBound = FetchBubbles / TotalSlots
  506. * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
  507. *
  508. * The kernel provides the mapping to the low level CPU events and any scaling
  509. * needed for the CPU pipeline width, for example:
  510. *
  511. * TotalSlots = Cycles * 4
  512. *
  513. * The scaling factor is communicated in the sysfs unit.
  514. *
  515. * In some cases the CPU may not be able to measure all the formulas due to
  516. * missing events. In this case multiple formulas are combined, as possible.
  517. *
  518. * Full TopDown supports more levels to sub-divide each area: for example
  519. * BackendBound into computing bound and memory bound. For now we only
  520. * support Level 1 TopDown.
  521. */
  522. static double sanitize_val(double x)
  523. {
  524. if (x < 0 && x >= -0.02)
  525. return 0.0;
  526. return x;
  527. }
  528. static double td_total_slots(int ctx, int cpu, struct runtime_stat *st)
  529. {
  530. return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, ctx, cpu);
  531. }
  532. static double td_bad_spec(int ctx, int cpu, struct runtime_stat *st)
  533. {
  534. double bad_spec = 0;
  535. double total_slots;
  536. double total;
  537. total = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_ISSUED, ctx, cpu) -
  538. runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, ctx, cpu) +
  539. runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, ctx, cpu);
  540. total_slots = td_total_slots(ctx, cpu, st);
  541. if (total_slots)
  542. bad_spec = total / total_slots;
  543. return sanitize_val(bad_spec);
  544. }
  545. static double td_retiring(int ctx, int cpu, struct runtime_stat *st)
  546. {
  547. double retiring = 0;
  548. double total_slots = td_total_slots(ctx, cpu, st);
  549. double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED,
  550. ctx, cpu);
  551. if (total_slots)
  552. retiring = ret_slots / total_slots;
  553. return retiring;
  554. }
  555. static double td_fe_bound(int ctx, int cpu, struct runtime_stat *st)
  556. {
  557. double fe_bound = 0;
  558. double total_slots = td_total_slots(ctx, cpu, st);
  559. double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES,
  560. ctx, cpu);
  561. if (total_slots)
  562. fe_bound = fetch_bub / total_slots;
  563. return fe_bound;
  564. }
  565. static double td_be_bound(int ctx, int cpu, struct runtime_stat *st)
  566. {
  567. double sum = (td_fe_bound(ctx, cpu, st) +
  568. td_bad_spec(ctx, cpu, st) +
  569. td_retiring(ctx, cpu, st));
  570. if (sum == 0)
  571. return 0;
  572. return sanitize_val(1.0 - sum);
  573. }
  574. static void print_smi_cost(int cpu, struct perf_evsel *evsel,
  575. struct perf_stat_output_ctx *out,
  576. struct runtime_stat *st)
  577. {
  578. double smi_num, aperf, cycles, cost = 0.0;
  579. int ctx = evsel_context(evsel);
  580. const char *color = NULL;
  581. smi_num = runtime_stat_avg(st, STAT_SMI_NUM, ctx, cpu);
  582. aperf = runtime_stat_avg(st, STAT_APERF, ctx, cpu);
  583. cycles = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  584. if ((cycles == 0) || (aperf == 0))
  585. return;
  586. if (smi_num)
  587. cost = (aperf - cycles) / aperf * 100.00;
  588. if (cost > 10)
  589. color = PERF_COLOR_RED;
  590. out->print_metric(out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
  591. out->print_metric(out->ctx, NULL, "%4.0f", "SMI#", smi_num);
  592. }
  593. static void generic_metric(const char *metric_expr,
  594. struct perf_evsel **metric_events,
  595. char *name,
  596. const char *metric_name,
  597. double avg,
  598. int cpu,
  599. struct perf_stat_output_ctx *out,
  600. struct runtime_stat *st)
  601. {
  602. print_metric_t print_metric = out->print_metric;
  603. struct parse_ctx pctx;
  604. double ratio;
  605. int i;
  606. void *ctxp = out->ctx;
  607. expr__ctx_init(&pctx);
  608. expr__add_id(&pctx, name, avg);
  609. for (i = 0; metric_events[i]; i++) {
  610. struct saved_value *v;
  611. struct stats *stats;
  612. double scale;
  613. if (!strcmp(metric_events[i]->name, "duration_time")) {
  614. stats = &walltime_nsecs_stats;
  615. scale = 1e-9;
  616. } else {
  617. v = saved_value_lookup(metric_events[i], cpu, false,
  618. STAT_NONE, 0, st);
  619. if (!v)
  620. break;
  621. stats = &v->stats;
  622. scale = 1.0;
  623. }
  624. expr__add_id(&pctx, metric_events[i]->name, avg_stats(stats)*scale);
  625. }
  626. if (!metric_events[i]) {
  627. const char *p = metric_expr;
  628. if (expr__parse(&ratio, &pctx, &p) == 0)
  629. print_metric(ctxp, NULL, "%8.1f",
  630. metric_name ?
  631. metric_name :
  632. out->force_header ? name : "",
  633. ratio);
  634. else
  635. print_metric(ctxp, NULL, NULL,
  636. out->force_header ?
  637. (metric_name ? metric_name : name) : "", 0);
  638. } else
  639. print_metric(ctxp, NULL, NULL, "", 0);
  640. }
  641. void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
  642. double avg, int cpu,
  643. struct perf_stat_output_ctx *out,
  644. struct rblist *metric_events,
  645. struct runtime_stat *st)
  646. {
  647. void *ctxp = out->ctx;
  648. print_metric_t print_metric = out->print_metric;
  649. double total, ratio = 0.0, total2;
  650. const char *color = NULL;
  651. int ctx = evsel_context(evsel);
  652. struct metric_event *me;
  653. int num = 1;
  654. if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
  655. total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  656. if (total) {
  657. ratio = avg / total;
  658. print_metric(ctxp, NULL, "%7.2f ",
  659. "insn per cycle", ratio);
  660. } else {
  661. print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
  662. }
  663. total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT,
  664. ctx, cpu);
  665. total = max(total, runtime_stat_avg(st,
  666. STAT_STALLED_CYCLES_BACK,
  667. ctx, cpu));
  668. if (total && avg) {
  669. out->new_line(ctxp);
  670. ratio = total / avg;
  671. print_metric(ctxp, NULL, "%7.2f ",
  672. "stalled cycles per insn",
  673. ratio);
  674. } else if (have_frontend_stalled) {
  675. print_metric(ctxp, NULL, NULL,
  676. "stalled cycles per insn", 0);
  677. }
  678. } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
  679. if (runtime_stat_n(st, STAT_BRANCHES, ctx, cpu) != 0)
  680. print_branch_misses(cpu, evsel, avg, out, st);
  681. else
  682. print_metric(ctxp, NULL, NULL, "of all branches", 0);
  683. } else if (
  684. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  685. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
  686. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  687. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  688. if (runtime_stat_n(st, STAT_L1_DCACHE, ctx, cpu) != 0)
  689. print_l1_dcache_misses(cpu, evsel, avg, out, st);
  690. else
  691. print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
  692. } else if (
  693. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  694. evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
  695. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  696. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  697. if (runtime_stat_n(st, STAT_L1_ICACHE, ctx, cpu) != 0)
  698. print_l1_icache_misses(cpu, evsel, avg, out, st);
  699. else
  700. print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
  701. } else if (
  702. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  703. evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
  704. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  705. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  706. if (runtime_stat_n(st, STAT_DTLB_CACHE, ctx, cpu) != 0)
  707. print_dtlb_cache_misses(cpu, evsel, avg, out, st);
  708. else
  709. print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
  710. } else if (
  711. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  712. evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
  713. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  714. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  715. if (runtime_stat_n(st, STAT_ITLB_CACHE, ctx, cpu) != 0)
  716. print_itlb_cache_misses(cpu, evsel, avg, out, st);
  717. else
  718. print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
  719. } else if (
  720. evsel->attr.type == PERF_TYPE_HW_CACHE &&
  721. evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
  722. ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
  723. ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
  724. if (runtime_stat_n(st, STAT_LL_CACHE, ctx, cpu) != 0)
  725. print_ll_cache_misses(cpu, evsel, avg, out, st);
  726. else
  727. print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
  728. } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
  729. total = runtime_stat_avg(st, STAT_CACHEREFS, ctx, cpu);
  730. if (total)
  731. ratio = avg * 100 / total;
  732. if (runtime_stat_n(st, STAT_CACHEREFS, ctx, cpu) != 0)
  733. print_metric(ctxp, NULL, "%8.3f %%",
  734. "of all cache refs", ratio);
  735. else
  736. print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
  737. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
  738. print_stalled_cycles_frontend(cpu, evsel, avg, out, st);
  739. } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
  740. print_stalled_cycles_backend(cpu, evsel, avg, out, st);
  741. } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
  742. total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
  743. if (total) {
  744. ratio = avg / total;
  745. print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
  746. } else {
  747. print_metric(ctxp, NULL, NULL, "Ghz", 0);
  748. }
  749. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
  750. total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  751. if (total)
  752. print_metric(ctxp, NULL,
  753. "%7.2f%%", "transactional cycles",
  754. 100.0 * (avg / total));
  755. else
  756. print_metric(ctxp, NULL, NULL, "transactional cycles",
  757. 0);
  758. } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
  759. total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
  760. total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, ctx, cpu);
  761. if (total2 < avg)
  762. total2 = avg;
  763. if (total)
  764. print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
  765. 100.0 * ((total2-avg) / total));
  766. else
  767. print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
  768. } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
  769. total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
  770. ctx, cpu);
  771. if (avg)
  772. ratio = total / avg;
  773. if (runtime_stat_n(st, STAT_CYCLES_IN_TX, ctx, cpu) != 0)
  774. print_metric(ctxp, NULL, "%8.0f",
  775. "cycles / transaction", ratio);
  776. else
  777. print_metric(ctxp, NULL, NULL, "cycles / transaction",
  778. 0);
  779. } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
  780. total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
  781. ctx, cpu);
  782. if (avg)
  783. ratio = total / avg;
  784. print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
  785. } else if (perf_evsel__is_clock(evsel)) {
  786. if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
  787. print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
  788. avg / (ratio * evsel->scale));
  789. else
  790. print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
  791. } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
  792. double fe_bound = td_fe_bound(ctx, cpu, st);
  793. if (fe_bound > 0.2)
  794. color = PERF_COLOR_RED;
  795. print_metric(ctxp, color, "%8.1f%%", "frontend bound",
  796. fe_bound * 100.);
  797. } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
  798. double retiring = td_retiring(ctx, cpu, st);
  799. if (retiring > 0.7)
  800. color = PERF_COLOR_GREEN;
  801. print_metric(ctxp, color, "%8.1f%%", "retiring",
  802. retiring * 100.);
  803. } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
  804. double bad_spec = td_bad_spec(ctx, cpu, st);
  805. if (bad_spec > 0.1)
  806. color = PERF_COLOR_RED;
  807. print_metric(ctxp, color, "%8.1f%%", "bad speculation",
  808. bad_spec * 100.);
  809. } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
  810. double be_bound = td_be_bound(ctx, cpu, st);
  811. const char *name = "backend bound";
  812. static int have_recovery_bubbles = -1;
  813. /* In case the CPU does not support topdown-recovery-bubbles */
  814. if (have_recovery_bubbles < 0)
  815. have_recovery_bubbles = pmu_have_event("cpu",
  816. "topdown-recovery-bubbles");
  817. if (!have_recovery_bubbles)
  818. name = "backend bound/bad spec";
  819. if (be_bound > 0.2)
  820. color = PERF_COLOR_RED;
  821. if (td_total_slots(ctx, cpu, st) > 0)
  822. print_metric(ctxp, color, "%8.1f%%", name,
  823. be_bound * 100.);
  824. else
  825. print_metric(ctxp, NULL, NULL, name, 0);
  826. } else if (evsel->metric_expr) {
  827. generic_metric(evsel->metric_expr, evsel->metric_events, evsel->name,
  828. evsel->metric_name, avg, cpu, out, st);
  829. } else if (runtime_stat_n(st, STAT_NSECS, 0, cpu) != 0) {
  830. char unit = 'M';
  831. char unit_buf[10];
  832. total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
  833. if (total)
  834. ratio = 1000.0 * avg / total;
  835. if (ratio < 0.001) {
  836. ratio *= 1000;
  837. unit = 'K';
  838. }
  839. snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
  840. print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
  841. } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
  842. print_smi_cost(cpu, evsel, out, st);
  843. } else {
  844. num = 0;
  845. }
  846. if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
  847. struct metric_expr *mexp;
  848. list_for_each_entry (mexp, &me->head, nd) {
  849. if (num++ > 0)
  850. out->new_line(ctxp);
  851. generic_metric(mexp->metric_expr, mexp->metric_events,
  852. evsel->name, mexp->metric_name,
  853. avg, cpu, out, st);
  854. }
  855. }
  856. if (num == 0)
  857. print_metric(ctxp, NULL, NULL, NULL, 0);
  858. }