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