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(const struct metric_expr * mexp,const struct evsel * evsel,struct expr_parse_ctx * pctx,int aggr_idx)366 static int prepare_metric(const struct metric_expr *mexp,
367 const struct evsel *evsel,
368 struct expr_parse_ctx *pctx,
369 int aggr_idx)
370 {
371 struct evsel * const *metric_events = mexp->metric_events;
372 struct metric_ref *metric_refs = mexp->metric_refs;
373 int i;
374
375 for (i = 0; metric_events[i]; i++) {
376 char *n;
377 double val;
378 int source_count = 0;
379
380 if (evsel__is_tool(metric_events[i])) {
381 struct stats *stats;
382 double scale;
383
384 switch (evsel__tool_event(metric_events[i])) {
385 case PERF_TOOL_DURATION_TIME:
386 stats = &walltime_nsecs_stats;
387 scale = 1e-9;
388 break;
389 case PERF_TOOL_USER_TIME:
390 stats = &ru_stats.ru_utime_usec_stat;
391 scale = 1e-6;
392 break;
393 case PERF_TOOL_SYSTEM_TIME:
394 stats = &ru_stats.ru_stime_usec_stat;
395 scale = 1e-6;
396 break;
397 case PERF_TOOL_NONE:
398 pr_err("Invalid tool event 'none'");
399 abort();
400 case PERF_TOOL_MAX:
401 pr_err("Invalid tool event 'max'");
402 abort();
403 default:
404 pr_err("Unknown tool event '%s'", evsel__name(metric_events[i]));
405 abort();
406 }
407 val = avg_stats(stats) * scale;
408 source_count = 1;
409 } else {
410 struct perf_stat_evsel *ps = metric_events[i]->stats;
411 struct perf_stat_aggr *aggr;
412
413 /*
414 * If there are multiple uncore PMUs and we're not
415 * reading the leader's stats, determine the stats for
416 * the appropriate uncore PMU.
417 */
418 if (evsel && evsel->metric_leader &&
419 evsel->pmu != evsel->metric_leader->pmu &&
420 mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) {
421 struct evsel *pos;
422
423 evlist__for_each_entry(evsel->evlist, pos) {
424 if (pos->pmu != evsel->pmu)
425 continue;
426 if (pos->metric_leader != mexp->metric_events[i])
427 continue;
428 ps = pos->stats;
429 source_count = 1;
430 break;
431 }
432 }
433 aggr = &ps->aggr[aggr_idx];
434 if (!aggr)
435 break;
436
437 if (!metric_events[i]->supported) {
438 /*
439 * Not supported events will have a count of 0,
440 * which can be confusing in a
441 * metric. Explicitly set the value to NAN. Not
442 * counted events (enable time of 0) are read as
443 * 0.
444 */
445 val = NAN;
446 source_count = 0;
447 } else {
448 val = aggr->counts.val;
449 if (!source_count)
450 source_count = evsel__source_count(metric_events[i]);
451 }
452 }
453 n = strdup(evsel__metric_id(metric_events[i]));
454 if (!n)
455 return -ENOMEM;
456
457 expr__add_id_val_source_count(pctx, n, val, source_count);
458 }
459
460 for (int j = 0; metric_refs && metric_refs[j].metric_name; j++) {
461 int ret = expr__add_ref(pctx, &metric_refs[j]);
462
463 if (ret)
464 return ret;
465 }
466
467 return i;
468 }
469
generic_metric(struct perf_stat_config * config,struct metric_expr * mexp,struct evsel * evsel,int aggr_idx,struct perf_stat_output_ctx * out)470 static void generic_metric(struct perf_stat_config *config,
471 struct metric_expr *mexp,
472 struct evsel *evsel,
473 int aggr_idx,
474 struct perf_stat_output_ctx *out)
475 {
476 print_metric_t print_metric = out->print_metric;
477 const char *metric_name = mexp->metric_name;
478 const char *metric_expr = mexp->metric_expr;
479 const char *metric_threshold = mexp->metric_threshold;
480 const char *metric_unit = mexp->metric_unit;
481 struct evsel * const *metric_events = mexp->metric_events;
482 int runtime = mexp->runtime;
483 struct expr_parse_ctx *pctx;
484 double ratio, scale, threshold;
485 int i;
486 void *ctxp = out->ctx;
487 const char *color = NULL;
488
489 pctx = expr__ctx_new();
490 if (!pctx)
491 return;
492
493 if (config->user_requested_cpu_list)
494 pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
495 pctx->sctx.runtime = runtime;
496 pctx->sctx.system_wide = config->system_wide;
497 i = prepare_metric(mexp, evsel, pctx, aggr_idx);
498 if (i < 0) {
499 expr__ctx_free(pctx);
500 return;
501 }
502 if (!metric_events[i]) {
503 if (expr__parse(&ratio, pctx, metric_expr) == 0) {
504 char *unit;
505 char metric_bf[64];
506
507 if (metric_threshold &&
508 expr__parse(&threshold, pctx, metric_threshold) == 0 &&
509 !isnan(threshold)) {
510 color = fpclassify(threshold) == FP_ZERO
511 ? PERF_COLOR_GREEN : PERF_COLOR_RED;
512 }
513
514 if (metric_unit && metric_name) {
515 if (perf_pmu__convert_scale(metric_unit,
516 &unit, &scale) >= 0) {
517 ratio *= scale;
518 }
519 if (strstr(metric_expr, "?"))
520 scnprintf(metric_bf, sizeof(metric_bf),
521 "%s %s_%d", unit, metric_name, runtime);
522 else
523 scnprintf(metric_bf, sizeof(metric_bf),
524 "%s %s", unit, metric_name);
525
526 print_metric(config, ctxp, color, "%8.1f",
527 metric_bf, ratio);
528 } else {
529 print_metric(config, ctxp, color, "%8.2f",
530 metric_name ?
531 metric_name :
532 out->force_header ? evsel->name : "",
533 ratio);
534 }
535 } else {
536 print_metric(config, ctxp, color, /*unit=*/NULL,
537 out->force_header ?
538 (metric_name ?: evsel->name) : "", 0);
539 }
540 } else {
541 print_metric(config, ctxp, color, /*unit=*/NULL,
542 out->force_header ?
543 (metric_name ?: evsel->name) : "", 0);
544 }
545
546 expr__ctx_free(pctx);
547 }
548
test_generic_metric(struct metric_expr * mexp,int aggr_idx)549 double test_generic_metric(struct metric_expr *mexp, int aggr_idx)
550 {
551 struct expr_parse_ctx *pctx;
552 double ratio = 0.0;
553
554 pctx = expr__ctx_new();
555 if (!pctx)
556 return NAN;
557
558 if (prepare_metric(mexp, /*evsel=*/NULL, pctx, aggr_idx) < 0)
559 goto out;
560
561 if (expr__parse(&ratio, pctx, mexp->metric_expr))
562 ratio = 0.0;
563
564 out:
565 expr__ctx_free(pctx);
566 return ratio;
567 }
568
perf_stat__print_metricgroup_header(struct perf_stat_config * config,struct evsel * evsel,void * ctxp,const char * name,struct perf_stat_output_ctx * out)569 static void perf_stat__print_metricgroup_header(struct perf_stat_config *config,
570 struct evsel *evsel,
571 void *ctxp,
572 const char *name,
573 struct perf_stat_output_ctx *out)
574 {
575 bool need_full_name = perf_pmus__num_core_pmus() > 1;
576 static const char *last_name;
577 static const char *last_pmu;
578 char full_name[64];
579
580 /*
581 * A metricgroup may have several metric events,
582 * e.g.,TopdownL1 on e-core of ADL.
583 * The name has been output by the first metric
584 * event. Only align with other metics from
585 * different metric events.
586 */
587 if (last_name && !strcmp(last_name, name)) {
588 if (!need_full_name || !strcmp(last_pmu, evsel->pmu_name)) {
589 out->print_metricgroup_header(config, ctxp, NULL);
590 return;
591 }
592 }
593
594 if (need_full_name)
595 scnprintf(full_name, sizeof(full_name), "%s (%s)", name, evsel->pmu_name);
596 else
597 scnprintf(full_name, sizeof(full_name), "%s", name);
598
599 out->print_metricgroup_header(config, ctxp, full_name);
600
601 last_name = name;
602 last_pmu = evsel->pmu_name;
603 }
604
605 /**
606 * perf_stat__print_shadow_stats_metricgroup - Print out metrics associated with the evsel
607 * For the non-default, all metrics associated
608 * with the evsel are printed.
609 * For the default mode, only the metrics from
610 * the same metricgroup and the name of the
611 * metricgroup are printed. To print the metrics
612 * from the next metricgroup (if available),
613 * invoke the function with correspoinding
614 * metric_expr.
615 */
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)616 void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
617 struct evsel *evsel,
618 int aggr_idx,
619 int *num,
620 void *from,
621 struct perf_stat_output_ctx *out,
622 struct rblist *metric_events)
623 {
624 struct metric_event *me;
625 struct metric_expr *mexp = from;
626 void *ctxp = out->ctx;
627 bool header_printed = false;
628 const char *name = NULL;
629
630 me = metricgroup__lookup(metric_events, evsel, false);
631 if (me == NULL)
632 return NULL;
633
634 if (!mexp)
635 mexp = list_first_entry(&me->head, typeof(*mexp), nd);
636
637 list_for_each_entry_from(mexp, &me->head, nd) {
638 /* Print the display name of the Default metricgroup */
639 if (!config->metric_only && me->is_default) {
640 if (!name)
641 name = mexp->default_metricgroup_name;
642 /*
643 * Two or more metricgroup may share the same metric
644 * event, e.g., TopdownL1 and TopdownL2 on SPR.
645 * Return and print the prefix, e.g., noise, running
646 * for the next metricgroup.
647 */
648 if (strcmp(name, mexp->default_metricgroup_name))
649 return (void *)mexp;
650 /* Only print the name of the metricgroup once */
651 if (!header_printed) {
652 header_printed = true;
653 perf_stat__print_metricgroup_header(config, evsel, ctxp,
654 name, out);
655 }
656 }
657
658 if ((*num)++ > 0)
659 out->new_line(config, ctxp);
660 generic_metric(config, mexp, evsel, aggr_idx, out);
661 }
662
663 return NULL;
664 }
665
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)666 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
667 struct evsel *evsel,
668 double avg, int aggr_idx,
669 struct perf_stat_output_ctx *out,
670 struct rblist *metric_events)
671 {
672 typedef void (*stat_print_function_t)(struct perf_stat_config *config,
673 const struct evsel *evsel,
674 int aggr_idx, double misses,
675 struct perf_stat_output_ctx *out);
676 static const stat_print_function_t stat_print_function[STAT_MAX] = {
677 [STAT_INSTRUCTIONS] = print_instructions,
678 [STAT_BRANCH_MISS] = print_branch_miss,
679 [STAT_L1D_MISS] = print_l1d_miss,
680 [STAT_L1I_MISS] = print_l1i_miss,
681 [STAT_DTLB_MISS] = print_dtlb_miss,
682 [STAT_ITLB_MISS] = print_itlb_miss,
683 [STAT_LL_MISS] = print_ll_miss,
684 [STAT_CACHE_MISSES] = print_cache_miss,
685 [STAT_STALLED_CYCLES_FRONT] = print_stalled_cycles_front,
686 [STAT_STALLED_CYCLES_BACK] = print_stalled_cycles_back,
687 [STAT_CYCLES] = print_cycles,
688 [STAT_NSECS] = print_nsecs,
689 };
690 print_metric_t print_metric = out->print_metric;
691 void *ctxp = out->ctx;
692 int num = 1;
693
694 if (config->iostat_run) {
695 iostat_print_metric(config, evsel, out);
696 } else {
697 stat_print_function_t fn = stat_print_function[evsel__stat_type(evsel)];
698
699 if (fn)
700 fn(config, evsel, aggr_idx, avg, out);
701 else {
702 double nsecs = find_stat(evsel, aggr_idx, STAT_NSECS);
703
704 if (nsecs) {
705 char unit = ' ';
706 char unit_buf[10] = "/sec";
707 double ratio = convert_unit_double(1000000000.0 * avg / nsecs,
708 &unit);
709
710 if (unit != ' ')
711 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
712 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio);
713 } else
714 num = 0;
715 }
716 }
717
718 perf_stat__print_shadow_stats_metricgroup(config, evsel, aggr_idx,
719 &num, NULL, out, metric_events);
720
721 if (num == 0)
722 print_metric(config, ctxp, NULL, NULL, NULL, 0);
723 }
724
725 /**
726 * perf_stat__skip_metric_event - Skip the evsel in the Default metricgroup,
727 * if it's not running or not the metric event.
728 */
perf_stat__skip_metric_event(struct evsel * evsel,struct rblist * metric_events,u64 ena,u64 run)729 bool perf_stat__skip_metric_event(struct evsel *evsel,
730 struct rblist *metric_events,
731 u64 ena, u64 run)
732 {
733 if (!evsel->default_metricgroup)
734 return false;
735
736 if (!ena || !run)
737 return true;
738
739 return !metricgroup__lookup(metric_events, evsel, false);
740 }
741