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