stat-shadow.c 26 KB

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