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