stat-shadow.c 27 KB

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