1 // SPDX-License-Identifier: GPL-2.0
2 #include <math.h>
3 #include <stdio.h>
4 #include "evsel.h"
5 #include "stat.h"
6 #include "color.h"
7 #include "debug.h"
8 #include "pmu.h"
9 #include "rblist.h"
10 #include "evlist.h"
11 #include "expr.h"
12 #include "metricgroup.h"
13 #include "cgroup.h"
14 #include "units.h"
15 #include <linux/zalloc.h>
16 #include "iostat.h"
17 #include "util/hashmap.h"
18
19 struct stats walltime_nsecs_stats;
20 struct rusage_stats ru_stats;
21
22 enum {
23 CTX_BIT_USER = 1 << 0,
24 CTX_BIT_KERNEL = 1 << 1,
25 CTX_BIT_HV = 1 << 2,
26 CTX_BIT_HOST = 1 << 3,
27 CTX_BIT_IDLE = 1 << 4,
28 CTX_BIT_MAX = 1 << 5,
29 };
30
31 enum stat_type {
32 STAT_NONE = 0,
33 STAT_NSECS,
34 STAT_CYCLES,
35 STAT_INSTRUCTIONS,
36 STAT_STALLED_CYCLES_FRONT,
37 STAT_STALLED_CYCLES_BACK,
38 STAT_BRANCHES,
39 STAT_BRANCH_MISS,
40 STAT_CACHE_REFS,
41 STAT_CACHE_MISSES,
42 STAT_L1_DCACHE,
43 STAT_L1_ICACHE,
44 STAT_LL_CACHE,
45 STAT_ITLB_CACHE,
46 STAT_DTLB_CACHE,
47 STAT_L1D_MISS,
48 STAT_L1I_MISS,
49 STAT_LL_MISS,
50 STAT_DTLB_MISS,
51 STAT_ITLB_MISS,
52 STAT_MAX
53 };
54
evsel_context(const struct evsel * evsel)55 static int evsel_context(const struct evsel *evsel)
56 {
57 int ctx = 0;
58
59 if (evsel->core.attr.exclude_kernel)
60 ctx |= CTX_BIT_KERNEL;
61 if (evsel->core.attr.exclude_user)
62 ctx |= CTX_BIT_USER;
63 if (evsel->core.attr.exclude_hv)
64 ctx |= CTX_BIT_HV;
65 if (evsel->core.attr.exclude_host)
66 ctx |= CTX_BIT_HOST;
67 if (evsel->core.attr.exclude_idle)
68 ctx |= CTX_BIT_IDLE;
69
70 return ctx;
71 }
72
perf_stat__reset_shadow_stats(void)73 void perf_stat__reset_shadow_stats(void)
74 {
75 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
76 memset(&ru_stats, 0, sizeof(ru_stats));
77 }
78
evsel__stat_type(const struct evsel * evsel)79 static enum stat_type evsel__stat_type(const struct evsel *evsel)
80 {
81 /* Fake perf_hw_cache_op_id values for use with evsel__match. */
82 u64 PERF_COUNT_hw_cache_l1d_miss = PERF_COUNT_HW_CACHE_L1D |
83 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
84 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16);
85 u64 PERF_COUNT_hw_cache_l1i_miss = PERF_COUNT_HW_CACHE_L1I |
86 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
87 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16);
88 u64 PERF_COUNT_hw_cache_ll_miss = PERF_COUNT_HW_CACHE_LL |
89 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
90 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16);
91 u64 PERF_COUNT_hw_cache_dtlb_miss = PERF_COUNT_HW_CACHE_DTLB |
92 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
93 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16);
94 u64 PERF_COUNT_hw_cache_itlb_miss = PERF_COUNT_HW_CACHE_ITLB |
95 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
96 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16);
97
98 if (evsel__is_clock(evsel))
99 return STAT_NSECS;
100 else if (evsel__match(evsel, HARDWARE, HW_CPU_CYCLES))
101 return STAT_CYCLES;
102 else if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS))
103 return STAT_INSTRUCTIONS;
104 else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
105 return STAT_STALLED_CYCLES_FRONT;
106 else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND))
107 return STAT_STALLED_CYCLES_BACK;
108 else if (evsel__match(evsel, HARDWARE, HW_BRANCH_INSTRUCTIONS))
109 return STAT_BRANCHES;
110 else if (evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES))
111 return STAT_BRANCH_MISS;
112 else if (evsel__match(evsel, HARDWARE, HW_CACHE_REFERENCES))
113 return STAT_CACHE_REFS;
114 else if (evsel__match(evsel, HARDWARE, HW_CACHE_MISSES))
115 return STAT_CACHE_MISSES;
116 else if (evsel__match(evsel, HW_CACHE, HW_CACHE_L1D))
117 return STAT_L1_DCACHE;
118 else if (evsel__match(evsel, HW_CACHE, HW_CACHE_L1I))
119 return STAT_L1_ICACHE;
120 else if (evsel__match(evsel, HW_CACHE, HW_CACHE_LL))
121 return STAT_LL_CACHE;
122 else if (evsel__match(evsel, HW_CACHE, HW_CACHE_DTLB))
123 return STAT_DTLB_CACHE;
124 else if (evsel__match(evsel, HW_CACHE, HW_CACHE_ITLB))
125 return STAT_ITLB_CACHE;
126 else if (evsel__match(evsel, HW_CACHE, hw_cache_l1d_miss))
127 return STAT_L1D_MISS;
128 else if (evsel__match(evsel, HW_CACHE, hw_cache_l1i_miss))
129 return STAT_L1I_MISS;
130 else if (evsel__match(evsel, HW_CACHE, hw_cache_ll_miss))
131 return STAT_LL_MISS;
132 else if (evsel__match(evsel, HW_CACHE, hw_cache_dtlb_miss))
133 return STAT_DTLB_MISS;
134 else if (evsel__match(evsel, HW_CACHE, hw_cache_itlb_miss))
135 return STAT_ITLB_MISS;
136 return STAT_NONE;
137 }
138
get_ratio_color(const double ratios[3],double val)139 static const char *get_ratio_color(const double ratios[3], double val)
140 {
141 const char *color = PERF_COLOR_NORMAL;
142
143 if (val > ratios[0])
144 color = PERF_COLOR_RED;
145 else if (val > ratios[1])
146 color = PERF_COLOR_MAGENTA;
147 else if (val > ratios[2])
148 color = PERF_COLOR_YELLOW;
149
150 return color;
151 }
152
find_stat(const struct evsel * evsel,int aggr_idx,enum stat_type type)153 static double find_stat(const struct evsel *evsel, int aggr_idx, enum stat_type type)
154 {
155 const struct evsel *cur;
156 int evsel_ctx = evsel_context(evsel);
157 struct perf_pmu *evsel_pmu = evsel__find_pmu(evsel);
158
159 evlist__for_each_entry(evsel->evlist, cur) {
160 struct perf_stat_aggr *aggr;
161
162 /* Ignore the evsel that is being searched from. */
163 if (evsel == cur)
164 continue;
165
166 /* Ignore evsels that are part of different groups. */
167 if (evsel->core.leader->nr_members > 1 &&
168 evsel->core.leader != cur->core.leader)
169 continue;
170 /* Ignore evsels with mismatched modifiers. */
171 if (evsel_ctx != evsel_context(cur))
172 continue;
173 /* Ignore if not the cgroup we're looking for. */
174 if (evsel->cgrp != cur->cgrp)
175 continue;
176 /* Ignore if not the stat we're looking for. */
177 if (type != evsel__stat_type(cur))
178 continue;
179
180 /*
181 * Except the SW CLOCK events,
182 * ignore if not the PMU we're looking for.
183 */
184 if ((type != STAT_NSECS) && (evsel_pmu != evsel__find_pmu(cur)))
185 continue;
186
187 aggr = &cur->stats->aggr[aggr_idx];
188 if (type == STAT_NSECS)
189 return aggr->counts.val;
190 return aggr->counts.val * cur->scale;
191 }
192 return 0.0;
193 }
194
print_ratio(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double numerator,struct perf_stat_output_ctx * out,enum stat_type denominator_type,const double color_ratios[3],const char * unit)195 static void print_ratio(struct perf_stat_config *config,
196 const struct evsel *evsel, int aggr_idx,
197 double numerator, struct perf_stat_output_ctx *out,
198 enum stat_type denominator_type,
199 const double color_ratios[3], const char *unit)
200 {
201 double denominator = find_stat(evsel, aggr_idx, denominator_type);
202
203 if (numerator && denominator) {
204 double ratio = numerator / denominator * 100.0;
205 const char *color = get_ratio_color(color_ratios, ratio);
206
207 out->print_metric(config, out->ctx, color, "%7.2f%%", unit, ratio);
208 } else
209 out->print_metric(config, out->ctx, NULL, NULL, unit, 0);
210 }
211
print_stalled_cycles_front(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double stalled,struct perf_stat_output_ctx * out)212 static void print_stalled_cycles_front(struct perf_stat_config *config,
213 const struct evsel *evsel,
214 int aggr_idx, double stalled,
215 struct perf_stat_output_ctx *out)
216 {
217 static const double color_ratios[3] = {50.0, 30.0, 10.0};
218
219 print_ratio(config, evsel, aggr_idx, stalled, out, STAT_CYCLES, color_ratios,
220 "frontend cycles idle");
221 }
222
print_stalled_cycles_back(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double stalled,struct perf_stat_output_ctx * out)223 static void print_stalled_cycles_back(struct perf_stat_config *config,
224 const struct evsel *evsel,
225 int aggr_idx, double stalled,
226 struct perf_stat_output_ctx *out)
227 {
228 static const double color_ratios[3] = {75.0, 50.0, 20.0};
229
230 print_ratio(config, evsel, aggr_idx, stalled, out, STAT_CYCLES, color_ratios,
231 "backend cycles idle");
232 }
233
print_branch_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)234 static void print_branch_miss(struct perf_stat_config *config,
235 const struct evsel *evsel,
236 int aggr_idx, double misses,
237 struct perf_stat_output_ctx *out)
238 {
239 static const double color_ratios[3] = {20.0, 10.0, 5.0};
240
241 print_ratio(config, evsel, aggr_idx, misses, out, STAT_BRANCHES, color_ratios,
242 "of all branches");
243 }
244
print_l1d_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)245 static void print_l1d_miss(struct perf_stat_config *config,
246 const struct evsel *evsel,
247 int aggr_idx, double misses,
248 struct perf_stat_output_ctx *out)
249 {
250 static const double color_ratios[3] = {20.0, 10.0, 5.0};
251
252 print_ratio(config, evsel, aggr_idx, misses, out, STAT_L1_DCACHE, color_ratios,
253 "of all L1-dcache accesses");
254 }
255
print_l1i_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)256 static void print_l1i_miss(struct perf_stat_config *config,
257 const struct evsel *evsel,
258 int aggr_idx, double misses,
259 struct perf_stat_output_ctx *out)
260 {
261 static const double color_ratios[3] = {20.0, 10.0, 5.0};
262
263 print_ratio(config, evsel, aggr_idx, misses, out, STAT_L1_ICACHE, color_ratios,
264 "of all L1-icache accesses");
265 }
266
print_ll_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)267 static void print_ll_miss(struct perf_stat_config *config,
268 const struct evsel *evsel,
269 int aggr_idx, double misses,
270 struct perf_stat_output_ctx *out)
271 {
272 static const double color_ratios[3] = {20.0, 10.0, 5.0};
273
274 print_ratio(config, evsel, aggr_idx, misses, out, STAT_LL_CACHE, color_ratios,
275 "of all LL-cache accesses");
276 }
277
print_dtlb_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)278 static void print_dtlb_miss(struct perf_stat_config *config,
279 const struct evsel *evsel,
280 int aggr_idx, double misses,
281 struct perf_stat_output_ctx *out)
282 {
283 static const double color_ratios[3] = {20.0, 10.0, 5.0};
284
285 print_ratio(config, evsel, aggr_idx, misses, out, STAT_DTLB_CACHE, color_ratios,
286 "of all dTLB cache accesses");
287 }
288
print_itlb_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)289 static void print_itlb_miss(struct perf_stat_config *config,
290 const struct evsel *evsel,
291 int aggr_idx, double misses,
292 struct perf_stat_output_ctx *out)
293 {
294 static const double color_ratios[3] = {20.0, 10.0, 5.0};
295
296 print_ratio(config, evsel, aggr_idx, misses, out, STAT_ITLB_CACHE, color_ratios,
297 "of all iTLB cache accesses");
298 }
299
print_cache_miss(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double misses,struct perf_stat_output_ctx * out)300 static void print_cache_miss(struct perf_stat_config *config,
301 const struct evsel *evsel,
302 int aggr_idx, double misses,
303 struct perf_stat_output_ctx *out)
304 {
305 static const double color_ratios[3] = {20.0, 10.0, 5.0};
306
307 print_ratio(config, evsel, aggr_idx, misses, out, STAT_CACHE_REFS, color_ratios,
308 "of all cache refs");
309 }
310
print_instructions(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double instructions,struct perf_stat_output_ctx * out)311 static void print_instructions(struct perf_stat_config *config,
312 const struct evsel *evsel,
313 int aggr_idx, double instructions,
314 struct perf_stat_output_ctx *out)
315 {
316 print_metric_t print_metric = out->print_metric;
317 void *ctxp = out->ctx;
318 double cycles = find_stat(evsel, aggr_idx, STAT_CYCLES);
319 double max_stalled = max(find_stat(evsel, aggr_idx, STAT_STALLED_CYCLES_FRONT),
320 find_stat(evsel, aggr_idx, STAT_STALLED_CYCLES_BACK));
321
322 if (cycles) {
323 print_metric(config, ctxp, NULL, "%7.2f ", "insn per cycle",
324 instructions / cycles);
325 } else
326 print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0);
327
328 if (max_stalled && instructions) {
329 out->new_line(config, ctxp);
330 print_metric(config, ctxp, NULL, "%7.2f ", "stalled cycles per insn",
331 max_stalled / instructions);
332 }
333 }
334
print_cycles(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx,double cycles,struct perf_stat_output_ctx * out)335 static void print_cycles(struct perf_stat_config *config,
336 const struct evsel *evsel,
337 int aggr_idx, double cycles,
338 struct perf_stat_output_ctx *out)
339 {
340 double nsecs = find_stat(evsel, aggr_idx, STAT_NSECS);
341
342 if (cycles && nsecs) {
343 double ratio = cycles / nsecs;
344
345 out->print_metric(config, out->ctx, NULL, "%8.3f", "GHz", ratio);
346 } else
347 out->print_metric(config, out->ctx, NULL, NULL, "GHz", 0);
348 }
349
print_nsecs(struct perf_stat_config * config,const struct evsel * evsel,int aggr_idx __maybe_unused,double nsecs,struct perf_stat_output_ctx * out)350 static void print_nsecs(struct perf_stat_config *config,
351 const struct evsel *evsel,
352 int aggr_idx __maybe_unused, double nsecs,
353 struct perf_stat_output_ctx *out)
354 {
355 print_metric_t print_metric = out->print_metric;
356 void *ctxp = out->ctx;
357 double wall_time = avg_stats(&walltime_nsecs_stats);
358
359 if (wall_time) {
360 print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized",
361 nsecs / (wall_time * evsel->scale));
362 } else
363 print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
364 }
365
prepare_metric(struct evsel ** metric_events,struct metric_ref * metric_refs,struct expr_parse_ctx * pctx,int aggr_idx)366 static int prepare_metric(struct evsel **metric_events,
367 struct metric_ref *metric_refs,
368 struct expr_parse_ctx *pctx,
369 int aggr_idx)
370 {
371 int i;
372
373 for (i = 0; metric_events[i]; i++) {
374 char *n;
375 double val;
376 int source_count = 0;
377
378 if (evsel__is_tool(metric_events[i])) {
379 struct stats *stats;
380 double scale;
381
382 switch (metric_events[i]->tool_event) {
383 case PERF_TOOL_DURATION_TIME:
384 stats = &walltime_nsecs_stats;
385 scale = 1e-9;
386 break;
387 case PERF_TOOL_USER_TIME:
388 stats = &ru_stats.ru_utime_usec_stat;
389 scale = 1e-6;
390 break;
391 case PERF_TOOL_SYSTEM_TIME:
392 stats = &ru_stats.ru_stime_usec_stat;
393 scale = 1e-6;
394 break;
395 case PERF_TOOL_NONE:
396 pr_err("Invalid tool event 'none'");
397 abort();
398 case PERF_TOOL_MAX:
399 pr_err("Invalid tool event 'max'");
400 abort();
401 default:
402 pr_err("Unknown tool event '%s'", evsel__name(metric_events[i]));
403 abort();
404 }
405 val = avg_stats(stats) * scale;
406 source_count = 1;
407 } else {
408 struct perf_stat_evsel *ps = metric_events[i]->stats;
409 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
410
411 if (!aggr)
412 break;
413
414 if (!metric_events[i]->supported) {
415 /*
416 * Not supported events will have a count of 0,
417 * which can be confusing in a
418 * metric. Explicitly set the value to NAN. Not
419 * counted events (enable time of 0) are read as
420 * 0.
421 */
422 val = NAN;
423 source_count = 0;
424 } else {
425 val = aggr->counts.val;
426 source_count = evsel__source_count(metric_events[i]);
427 }
428 }
429 n = strdup(evsel__metric_id(metric_events[i]));
430 if (!n)
431 return -ENOMEM;
432
433 expr__add_id_val_source_count(pctx, n, val, source_count);
434 }
435
436 for (int j = 0; metric_refs && metric_refs[j].metric_name; j++) {
437 int ret = expr__add_ref(pctx, &metric_refs[j]);
438
439 if (ret)
440 return ret;
441 }
442
443 return i;
444 }
445
generic_metric(struct perf_stat_config * config,const char * metric_expr,const char * metric_threshold,struct evsel ** metric_events,struct metric_ref * metric_refs,char * name,const char * metric_name,const char * metric_unit,int runtime,int aggr_idx,struct perf_stat_output_ctx * out)446 static void generic_metric(struct perf_stat_config *config,
447 const char *metric_expr,
448 const char *metric_threshold,
449 struct evsel **metric_events,
450 struct metric_ref *metric_refs,
451 char *name,
452 const char *metric_name,
453 const char *metric_unit,
454 int runtime,
455 int aggr_idx,
456 struct perf_stat_output_ctx *out)
457 {
458 print_metric_t print_metric = out->print_metric;
459 struct expr_parse_ctx *pctx;
460 double ratio, scale, threshold;
461 int i;
462 void *ctxp = out->ctx;
463 const char *color = NULL;
464
465 pctx = expr__ctx_new();
466 if (!pctx)
467 return;
468
469 if (config->user_requested_cpu_list)
470 pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
471 pctx->sctx.runtime = runtime;
472 pctx->sctx.system_wide = config->system_wide;
473 i = prepare_metric(metric_events, metric_refs, pctx, aggr_idx);
474 if (i < 0) {
475 expr__ctx_free(pctx);
476 return;
477 }
478 if (!metric_events[i]) {
479 if (expr__parse(&ratio, pctx, metric_expr) == 0) {
480 char *unit;
481 char metric_bf[64];
482
483 if (metric_threshold &&
484 expr__parse(&threshold, pctx, metric_threshold) == 0 &&
485 !isnan(threshold)) {
486 color = fpclassify(threshold) == FP_ZERO
487 ? PERF_COLOR_GREEN : PERF_COLOR_RED;
488 }
489
490 if (metric_unit && metric_name) {
491 if (perf_pmu__convert_scale(metric_unit,
492 &unit, &scale) >= 0) {
493 ratio *= scale;
494 }
495 if (strstr(metric_expr, "?"))
496 scnprintf(metric_bf, sizeof(metric_bf),
497 "%s %s_%d", unit, metric_name, runtime);
498 else
499 scnprintf(metric_bf, sizeof(metric_bf),
500 "%s %s", unit, metric_name);
501
502 print_metric(config, ctxp, color, "%8.1f",
503 metric_bf, ratio);
504 } else {
505 print_metric(config, ctxp, color, "%8.2f",
506 metric_name ?
507 metric_name :
508 out->force_header ? name : "",
509 ratio);
510 }
511 } else {
512 print_metric(config, ctxp, color, /*unit=*/NULL,
513 out->force_header ?
514 (metric_name ? metric_name : name) : "", 0);
515 }
516 } else {
517 print_metric(config, ctxp, color, /*unit=*/NULL,
518 out->force_header ?
519 (metric_name ? metric_name : name) : "", 0);
520 }
521
522 expr__ctx_free(pctx);
523 }
524
test_generic_metric(struct metric_expr * mexp,int aggr_idx)525 double test_generic_metric(struct metric_expr *mexp, int aggr_idx)
526 {
527 struct expr_parse_ctx *pctx;
528 double ratio = 0.0;
529
530 pctx = expr__ctx_new();
531 if (!pctx)
532 return NAN;
533
534 if (prepare_metric(mexp->metric_events, mexp->metric_refs, pctx, aggr_idx) < 0)
535 goto out;
536
537 if (expr__parse(&ratio, pctx, mexp->metric_expr))
538 ratio = 0.0;
539
540 out:
541 expr__ctx_free(pctx);
542 return ratio;
543 }
544
perf_stat__print_metricgroup_header(struct perf_stat_config * config,struct evsel * evsel,void * ctxp,const char * name,struct perf_stat_output_ctx * out)545 static void perf_stat__print_metricgroup_header(struct perf_stat_config *config,
546 struct evsel *evsel,
547 void *ctxp,
548 const char *name,
549 struct perf_stat_output_ctx *out)
550 {
551 bool need_full_name = perf_pmus__num_core_pmus() > 1;
552 static const char *last_name;
553 static const char *last_pmu;
554 char full_name[64];
555
556 /*
557 * A metricgroup may have several metric events,
558 * e.g.,TopdownL1 on e-core of ADL.
559 * The name has been output by the first metric
560 * event. Only align with other metics from
561 * different metric events.
562 */
563 if (last_name && !strcmp(last_name, name)) {
564 if (!need_full_name || !strcmp(last_pmu, evsel->pmu_name)) {
565 out->print_metricgroup_header(config, ctxp, NULL);
566 return;
567 }
568 }
569
570 if (need_full_name)
571 scnprintf(full_name, sizeof(full_name), "%s (%s)", name, evsel->pmu_name);
572 else
573 scnprintf(full_name, sizeof(full_name), "%s", name);
574
575 out->print_metricgroup_header(config, ctxp, full_name);
576
577 last_name = name;
578 last_pmu = evsel->pmu_name;
579 }
580
581 /**
582 * perf_stat__print_shadow_stats_metricgroup - Print out metrics associated with the evsel
583 * For the non-default, all metrics associated
584 * with the evsel are printed.
585 * For the default mode, only the metrics from
586 * the same metricgroup and the name of the
587 * metricgroup are printed. To print the metrics
588 * from the next metricgroup (if available),
589 * invoke the function with correspoinding
590 * metric_expr.
591 */
perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config * config,struct evsel * evsel,int aggr_idx,int * num,void * from,struct perf_stat_output_ctx * out,struct rblist * metric_events)592 void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
593 struct evsel *evsel,
594 int aggr_idx,
595 int *num,
596 void *from,
597 struct perf_stat_output_ctx *out,
598 struct rblist *metric_events)
599 {
600 struct metric_event *me;
601 struct metric_expr *mexp = from;
602 void *ctxp = out->ctx;
603 bool header_printed = false;
604 const char *name = NULL;
605
606 me = metricgroup__lookup(metric_events, evsel, false);
607 if (me == NULL)
608 return NULL;
609
610 if (!mexp)
611 mexp = list_first_entry(&me->head, typeof(*mexp), nd);
612
613 list_for_each_entry_from(mexp, &me->head, nd) {
614 /* Print the display name of the Default metricgroup */
615 if (!config->metric_only && me->is_default) {
616 if (!name)
617 name = mexp->default_metricgroup_name;
618 /*
619 * Two or more metricgroup may share the same metric
620 * event, e.g., TopdownL1 and TopdownL2 on SPR.
621 * Return and print the prefix, e.g., noise, running
622 * for the next metricgroup.
623 */
624 if (strcmp(name, mexp->default_metricgroup_name))
625 return (void *)mexp;
626 /* Only print the name of the metricgroup once */
627 if (!header_printed) {
628 header_printed = true;
629 perf_stat__print_metricgroup_header(config, evsel, ctxp,
630 name, out);
631 }
632 }
633
634 if ((*num)++ > 0)
635 out->new_line(config, ctxp);
636 generic_metric(config, mexp->metric_expr, mexp->metric_threshold,
637 mexp->metric_events, mexp->metric_refs, evsel->name,
638 mexp->metric_name, mexp->metric_unit, mexp->runtime,
639 aggr_idx, out);
640 }
641
642 return NULL;
643 }
644
perf_stat__print_shadow_stats(struct perf_stat_config * config,struct evsel * evsel,double avg,int aggr_idx,struct perf_stat_output_ctx * out,struct rblist * metric_events)645 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
646 struct evsel *evsel,
647 double avg, int aggr_idx,
648 struct perf_stat_output_ctx *out,
649 struct rblist *metric_events)
650 {
651 typedef void (*stat_print_function_t)(struct perf_stat_config *config,
652 const struct evsel *evsel,
653 int aggr_idx, double misses,
654 struct perf_stat_output_ctx *out);
655 static const stat_print_function_t stat_print_function[STAT_MAX] = {
656 [STAT_INSTRUCTIONS] = print_instructions,
657 [STAT_BRANCH_MISS] = print_branch_miss,
658 [STAT_L1D_MISS] = print_l1d_miss,
659 [STAT_L1I_MISS] = print_l1i_miss,
660 [STAT_DTLB_MISS] = print_dtlb_miss,
661 [STAT_ITLB_MISS] = print_itlb_miss,
662 [STAT_LL_MISS] = print_ll_miss,
663 [STAT_CACHE_MISSES] = print_cache_miss,
664 [STAT_STALLED_CYCLES_FRONT] = print_stalled_cycles_front,
665 [STAT_STALLED_CYCLES_BACK] = print_stalled_cycles_back,
666 [STAT_CYCLES] = print_cycles,
667 [STAT_NSECS] = print_nsecs,
668 };
669 print_metric_t print_metric = out->print_metric;
670 void *ctxp = out->ctx;
671 int num = 1;
672
673 if (config->iostat_run) {
674 iostat_print_metric(config, evsel, out);
675 } else {
676 stat_print_function_t fn = stat_print_function[evsel__stat_type(evsel)];
677
678 if (fn)
679 fn(config, evsel, aggr_idx, avg, out);
680 else {
681 double nsecs = find_stat(evsel, aggr_idx, STAT_NSECS);
682
683 if (nsecs) {
684 char unit = ' ';
685 char unit_buf[10] = "/sec";
686 double ratio = convert_unit_double(1000000000.0 * avg / nsecs,
687 &unit);
688
689 if (unit != ' ')
690 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
691 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio);
692 } else
693 num = 0;
694 }
695 }
696
697 perf_stat__print_shadow_stats_metricgroup(config, evsel, aggr_idx,
698 &num, NULL, out, metric_events);
699
700 if (num == 0)
701 print_metric(config, ctxp, NULL, NULL, NULL, 0);
702 }
703
704 /**
705 * perf_stat__skip_metric_event - Skip the evsel in the Default metricgroup,
706 * if it's not running or not the metric event.
707 */
perf_stat__skip_metric_event(struct evsel * evsel,struct rblist * metric_events,u64 ena,u64 run)708 bool perf_stat__skip_metric_event(struct evsel *evsel,
709 struct rblist *metric_events,
710 u64 ena, u64 run)
711 {
712 if (!evsel->default_metricgroup)
713 return false;
714
715 if (!ena || !run)
716 return true;
717
718 return !metricgroup__lookup(metric_events, evsel, false);
719 }
720