projects/linux·blob master · refs

359 lines · 9.6 KiB

1// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <math.h>
4#include <stdio.h>
5
6#include <linux/zalloc.h>
7
8#include "cgroup.h"
9#include "color.h"
10#include "debug.h"
11#include "evlist.h"
12#include "evsel.h"
13#include "expr.h"
14#include "hashmap.h"
15#include "iostat.h"
16#include "metricgroup.h"
17#include "pmu.h"
18#include "pmus.h"
19#include "rblist.h"
20#include "stat.h"
21#include "tool_pmu.h"
22#include "units.h"
23
24static bool tool_pmu__is_time_event(const struct perf_stat_config *config,
25 const struct evsel *evsel, int *tool_aggr_idx)
26{
27 enum tool_pmu_event event = evsel__tool_event(evsel);
28 int aggr_idx;
29
30 if (event != TOOL_PMU__EVENT_DURATION_TIME &&
31 event != TOOL_PMU__EVENT_USER_TIME &&
32 event != TOOL_PMU__EVENT_SYSTEM_TIME)
33 return false;
34
35 if (config) {
36 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
37 if (config->aggr_map->map[aggr_idx].cpu.cpu == 0) {
38 *tool_aggr_idx = aggr_idx;
39 return true;
40 }
41 }
42 pr_debug("Unexpected CPU0 missing in aggregation for tool event.\n");
43 }
44 *tool_aggr_idx = 0; /* Assume the first aggregation index works. */
45 return true;
46}
47
48static int prepare_metric(struct perf_stat_config *config,
49 const struct metric_expr *mexp,
50 const struct evsel *evsel,
51 struct expr_parse_ctx *pctx,
52 int aggr_idx)
53{
54 struct evsel * const *metric_events = mexp->metric_events;
55 struct metric_ref *metric_refs = mexp->metric_refs;
56 int i;
57
58 for (i = 0; metric_events[i]; i++) {
59 int source_count = 0, tool_aggr_idx;
60 int aggr_nr = 1;
61 bool is_tool_time =
62 tool_pmu__is_time_event(config, metric_events[i], &tool_aggr_idx);
63 struct perf_stat_evsel *ps = metric_events[i]->stats;
64 char *n;
65 double val;
66
67 /*
68 * If there are multiple uncore PMUs and we're not reading the
69 * leader's stats, determine the stats for the appropriate
70 * uncore PMU.
71 */
72 if (evsel && evsel->metric_leader &&
73 evsel->pmu != evsel->metric_leader->pmu &&
74 mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) {
75 struct evsel *pos;
76
77 evlist__for_each_entry(evsel->evlist, pos) {
78 if (pos->pmu != evsel->pmu)
79 continue;
80 if (pos->metric_leader != mexp->metric_events[i])
81 continue;
82 ps = pos->stats;
83 source_count = 1;
84 break;
85 }
86 }
87 /* Time events are always on CPU0, the first aggregation index. */
88 if (!ps || !metric_events[i]->supported) {
89 /*
90 * Not supported events will have a count of 0, which
91 * can be confusing in a metric. Explicitly set the
92 * value to NAN. Not counted events (enable time of 0)
93 * are read as 0.
94 */
95 val = NAN;
96 source_count = 0;
97 aggr_nr = 0;
98 } else {
99 struct perf_stat_aggr *aggr =
100 &ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];
101
102 if (aggr->counts.run == 0) {
103 val = NAN;
104 source_count = 0;
105 aggr_nr = 0;
106 } else {
107 val = aggr->counts.val;
108 if (is_tool_time) {
109 /* Convert time event nanoseconds to seconds. */
110 val *= 1e-9;
111 }
112 if (!source_count)
113 source_count = evsel__source_count(metric_events[i]);
114 aggr_nr = aggr->nr ?: 1;
115 }
116 }
117 n = strdup(evsel__metric_id(metric_events[i]));
118 if (!n)
119 return -ENOMEM;
120
121 expr__add_id_val_source_count_aggr_nr(pctx, n, val, source_count, aggr_nr);
122 }
123
124 for (int j = 0; metric_refs && metric_refs[j].metric_name; j++) {
125 int ret = expr__add_ref(pctx, &metric_refs[j]);
126
127 if (ret)
128 return ret;
129 }
130
131 return i;
132}
133
134static void generic_metric(struct perf_stat_config *config,
135 struct metric_expr *mexp,
136 struct evsel *evsel,
137 int aggr_idx,
138 struct perf_stat_output_ctx *out)
139{
140 print_metric_t print_metric = out->print_metric;
141 const char *metric_name = mexp->metric_name;
142 const char *metric_expr = mexp->metric_expr;
143 const char *metric_threshold = mexp->metric_threshold;
144 const char *metric_unit = mexp->metric_unit;
145 struct evsel * const *metric_events = mexp->metric_events;
146 int runtime = mexp->runtime;
147 struct expr_parse_ctx *pctx;
148 double ratio, scale, threshold;
149 int i;
150 void *ctxp = out->ctx;
151 enum metric_threshold_classify thresh = METRIC_THRESHOLD_UNKNOWN;
152
153 pctx = expr__ctx_new();
154 if (!pctx)
155 return;
156
157 if (config->user_requested_cpu_list)
158 pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
159 pctx->sctx.runtime = runtime;
160 pctx->sctx.system_wide = config->system_wide;
161 i = prepare_metric(config, mexp, evsel, pctx, aggr_idx);
162 if (i < 0) {
163 expr__ctx_free(pctx);
164 return;
165 }
166 if (!metric_events[i]) {
167 if (expr__parse(&ratio, pctx, metric_expr) == 0) {
168 char *unit;
169 char metric_bf[128];
170
171 if (metric_threshold &&
172 expr__parse(&threshold, pctx, metric_threshold) == 0 &&
173 !isnan(threshold)) {
174 thresh = fpclassify(threshold) == FP_ZERO
175 ? METRIC_THRESHOLD_GOOD : METRIC_THRESHOLD_BAD;
176 }
177
178 if (metric_unit && metric_name) {
179 if (perf_pmu__convert_scale(metric_unit,
180 &unit, &scale) >= 0) {
181 ratio *= scale;
182 }
183 if (strstr(metric_expr, "?"))
184 scnprintf(metric_bf, sizeof(metric_bf),
185 "%s %s_%d", unit, metric_name, runtime);
186 else
187 scnprintf(metric_bf, sizeof(metric_bf),
188 "%s %s", unit, metric_name);
189
190 print_metric(config, ctxp, thresh, "%8.1f",
191 metric_bf, ratio);
192 } else {
193 print_metric(config, ctxp, thresh, "%8.2f",
194 metric_name ?
195 metric_name :
196 out->force_header ? evsel->name : "",
197 ratio);
198 }
199 } else {
200 print_metric(config, ctxp, thresh, /*fmt=*/NULL,
201 out->force_header ?
202 (metric_name ?: evsel->name) : "", 0);
203 }
204 } else {
205 print_metric(config, ctxp, thresh, /*fmt=*/NULL,
206 out->force_header ?
207 (metric_name ?: evsel->name) : "", 0);
208 }
209
210 expr__ctx_free(pctx);
211}
212
213double test_generic_metric(struct metric_expr *mexp, int aggr_idx)
214{
215 struct expr_parse_ctx *pctx;
216 double ratio = 0.0;
217
218 pctx = expr__ctx_new();
219 if (!pctx)
220 return NAN;
221
222 if (prepare_metric(/*config=*/NULL, mexp, /*evsel=*/NULL, pctx, aggr_idx) < 0)
223 goto out;
224
225 if (expr__parse(&ratio, pctx, mexp->metric_expr))
226 ratio = 0.0;
227
228out:
229 expr__ctx_free(pctx);
230 return ratio;
231}
232
233static void perf_stat__print_metricgroup_header(struct perf_stat_config *config,
234 struct evsel *evsel,
235 void *ctxp,
236 const char *name,
237 struct perf_stat_output_ctx *out)
238{
239 bool need_full_name = perf_pmus__num_core_pmus() > 1;
240 static const char *last_name;
241 static const struct perf_pmu *last_pmu;
242 char full_name[64];
243
244 /*
245 * A metricgroup may have several metric events,
246 * e.g.,TopdownL1 on e-core of ADL.
247 * The name has been output by the first metric
248 * event. Only align with other metics from
249 * different metric events.
250 */
251 if (last_name && !strcmp(last_name, name) && last_pmu == evsel->pmu) {
252 out->print_metricgroup_header(config, ctxp, NULL);
253 return;
254 }
255
256 if (need_full_name && evsel->pmu)
257 scnprintf(full_name, sizeof(full_name), "%s (%s)", name, evsel->pmu->name);
258 else
259 scnprintf(full_name, sizeof(full_name), "%s", name);
260
261 out->print_metricgroup_header(config, ctxp, full_name);
262
263 last_name = name;
264 last_pmu = evsel->pmu;
265}
266
267/**
268 * perf_stat__print_shadow_stats_metricgroup - Print out metrics associated with the evsel
269 * For the non-default, all metrics associated
270 * with the evsel are printed.
271 * For the default mode, only the metrics from
272 * the same metricgroup and the name of the
273 * metricgroup are printed. To print the metrics
274 * from the next metricgroup (if available),
275 * invoke the function with correspoinding
276 * metric_expr.
277 */
278void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
279 struct evsel *evsel,
280 int aggr_idx,
281 int *num,
282 void *from,
283 struct perf_stat_output_ctx *out)
284{
285 struct metric_event *me;
286 struct metric_expr *mexp = from;
287 void *ctxp = out->ctx;
288 bool header_printed = false;
289 const char *name = NULL;
290 struct rblist *metric_events = evlist__metric_events(evsel->evlist);
291
292 me = metricgroup__lookup(metric_events, evsel, false);
293 if (me == NULL)
294 return NULL;
295
296 if (!mexp)
297 mexp = list_first_entry(&me->head, typeof(*mexp), nd);
298
299 list_for_each_entry_from(mexp, &me->head, nd) {
300 /* Print the display name of the Default metricgroup */
301 if (!config->metric_only && me->is_default) {
302 if (!name)
303 name = mexp->default_metricgroup_name;
304 /*
305 * Two or more metricgroup may share the same metric
306 * event, e.g., TopdownL1 and TopdownL2 on SPR.
307 * Return and print the prefix, e.g., noise, running
308 * for the next metricgroup.
309 */
310 if (strcmp(name, mexp->default_metricgroup_name))
311 return (void *)mexp;
312 /* Only print the name of the metricgroup once */
313 if (!header_printed && !evsel->default_show_events) {
314 header_printed = true;
315 perf_stat__print_metricgroup_header(config, evsel, ctxp,
316 name, out);
317 }
318 }
319
320 if ((*num)++ > 0 && out->new_line)
321 out->new_line(config, ctxp);
322 generic_metric(config, mexp, evsel, aggr_idx, out);
323 }
324
325 return NULL;
326}
327
328void perf_stat__print_shadow_stats(struct perf_stat_config *config,
329 struct evsel *evsel,
330 int aggr_idx,
331 struct perf_stat_output_ctx *out)
332{
333 print_metric_t print_metric = out->print_metric;
334 void *ctxp = out->ctx;
335 int num = 0;
336
337 if (config->iostat_run)
338 iostat_print_metric(config, evsel, out);
339
340 perf_stat__print_shadow_stats_metricgroup(config, evsel, aggr_idx,
341 &num, NULL, out);
342
343 if (num == 0) {
344 print_metric(config, ctxp, METRIC_THRESHOLD_UNKNOWN,
345 /*fmt=*/NULL, /*unit=*/NULL, 0);
346 }
347}
348
349/**
350 * perf_stat__skip_metric_event - Skip the evsel in the Default metricgroup,
351 * if it's not running or not the metric event.
352 */
353bool perf_stat__skip_metric_event(struct evsel *evsel)
354{
355 if (!evsel->default_metricgroup)
356 return false;
357
358 return !metricgroup__lookup(evlist__metric_events(evsel->evlist), evsel, false);
359}