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