1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/compiler.h> 3 #include <errno.h> 4 #include <string.h> 5 #include <perf/cpumap.h> 6 #include <perf/evlist.h> 7 #include "metricgroup.h" 8 #include "tests.h" 9 #include "pmu-events/pmu-events.h" 10 #include "evlist.h" 11 #include "rblist.h" 12 #include "debug.h" 13 #include "expr.h" 14 #include "stat.h" 15 #include "pmus.h" 16 17 struct value { 18 const char *event; 19 u64 val; 20 }; 21 22 static u64 find_value(const char *name, struct value *values) 23 { 24 struct value *v = values; 25 26 while (v->event) { 27 if (!strcmp(name, v->event)) 28 return v->val; 29 v++; 30 } 31 return 0; 32 } 33 34 static void load_runtime_stat(struct evlist *evlist, struct value *vals) 35 { 36 struct evsel *evsel; 37 u64 count; 38 39 evlist__alloc_aggr_stats(evlist, 1); 40 evlist__for_each_entry(evlist, evsel) { 41 count = find_value(evsel->name, vals); 42 evsel->supported = true; 43 evsel->stats->aggr->counts.val = count; 44 } 45 } 46 47 static double compute_single(struct evlist *evlist, const char *name) 48 { 49 struct metric_expr *mexp; 50 struct metric_event *me; 51 struct evsel *evsel; 52 53 evlist__for_each_entry(evlist, evsel) { 54 me = metricgroup__lookup(&evlist->metric_events, evsel, false); 55 if (me != NULL) { 56 list_for_each_entry (mexp, &me->head, nd) { 57 if (strcmp(mexp->metric_name, name)) 58 continue; 59 return test_generic_metric(mexp, 0); 60 } 61 } 62 } 63 return 0.; 64 } 65 66 static int __compute_metric(const char *name, struct value *vals, 67 const char *name1, double *ratio1, 68 const char *name2, double *ratio2) 69 { 70 const struct pmu_metrics_table *pme_test; 71 struct perf_cpu_map *cpus; 72 struct evlist *evlist; 73 int err; 74 75 /* 76 * We need to prepare evlist for stat mode running on CPU 0 77 * because that's where all the stats are going to be created. 78 */ 79 evlist = evlist__new(); 80 if (!evlist) 81 return -ENOMEM; 82 83 cpus = perf_cpu_map__new("0"); 84 if (!cpus) { 85 evlist__delete(evlist); 86 return -ENOMEM; 87 } 88 89 perf_evlist__set_maps(&evlist->core, cpus, NULL); 90 91 /* Parse the metric into metric_events list. */ 92 pme_test = find_core_metrics_table("testarch", "testcpu"); 93 err = metricgroup__parse_groups_test(evlist, pme_test, name); 94 if (err) 95 goto out; 96 97 err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false); 98 if (err) 99 goto out; 100 101 /* Load the runtime stats with given numbers for events. */ 102 load_runtime_stat(evlist, vals); 103 104 /* And execute the metric */ 105 if (name1 && ratio1) 106 *ratio1 = compute_single(evlist, name1); 107 if (name2 && ratio2) 108 *ratio2 = compute_single(evlist, name2); 109 110 out: 111 /* ... cleanup. */ 112 evlist__free_stats(evlist); 113 perf_cpu_map__put(cpus); 114 evlist__delete(evlist); 115 return err; 116 } 117 118 static int compute_metric(const char *name, struct value *vals, double *ratio) 119 { 120 return __compute_metric(name, vals, name, ratio, NULL, NULL); 121 } 122 123 static int compute_metric_group(const char *name, struct value *vals, 124 const char *name1, double *ratio1, 125 const char *name2, double *ratio2) 126 { 127 return __compute_metric(name, vals, name1, ratio1, name2, ratio2); 128 } 129 130 static int test_ipc(void) 131 { 132 double ratio; 133 struct value vals[] = { 134 { .event = "inst_retired.any", .val = 300 }, 135 { .event = "cpu_clk_unhalted.thread", .val = 200 }, 136 { .event = NULL, }, 137 }; 138 139 TEST_ASSERT_VAL("failed to compute metric", 140 compute_metric("IPC", vals, &ratio) == 0); 141 142 TEST_ASSERT_VAL("IPC failed, wrong ratio", 143 ratio == 1.5); 144 return 0; 145 } 146 147 static int test_frontend(void) 148 { 149 double ratio; 150 struct value vals[] = { 151 { .event = "idq_uops_not_delivered.core", .val = 300 }, 152 { .event = "cpu_clk_unhalted.thread", .val = 200 }, 153 { .event = "cpu_clk_unhalted.one_thread_active", .val = 400 }, 154 { .event = "cpu_clk_unhalted.ref_xclk", .val = 600 }, 155 { .event = NULL, }, 156 }; 157 158 TEST_ASSERT_VAL("failed to compute metric", 159 compute_metric("Frontend_Bound_SMT", vals, &ratio) == 0); 160 161 TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio", 162 ratio == 0.45); 163 return 0; 164 } 165 166 static int test_cache_miss_cycles(void) 167 { 168 double ratio; 169 struct value vals[] = { 170 { .event = "l1d-loads-misses", .val = 300 }, 171 { .event = "l1i-loads-misses", .val = 200 }, 172 { .event = "inst_retired.any", .val = 400 }, 173 { .event = NULL, }, 174 }; 175 176 TEST_ASSERT_VAL("failed to compute metric", 177 compute_metric("cache_miss_cycles", vals, &ratio) == 0); 178 179 TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio", 180 ratio == 1.25); 181 return 0; 182 } 183 184 185 /* 186 * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi 187 * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) + 188 * l2_rqsts.pf_miss + l2_rqsts.rfo_miss 189 * DCache_L2_All = dcache_l2_all_hits + dcache_l2_all_miss 190 * DCache_L2_Hits = d_ratio(dcache_l2_all_hits, dcache_l2_all) 191 * DCache_L2_Misses = d_ratio(dcache_l2_all_miss, dcache_l2_all) 192 * 193 * l2_rqsts.demand_data_rd_hit = 100 194 * l2_rqsts.pf_hit = 200 195 * l2_rqsts.rfo_hi = 300 196 * l2_rqsts.all_demand_data_rd = 400 197 * l2_rqsts.pf_miss = 500 198 * l2_rqsts.rfo_miss = 600 199 * 200 * DCache_L2_All_Hits = 600 201 * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400 202 * DCache_L2_All = 600 + 1400 = 2000 203 * DCache_L2_Hits = 600 / 2000 = 0.3 204 * DCache_L2_Misses = 1400 / 2000 = 0.7 205 */ 206 static int test_dcache_l2(void) 207 { 208 double ratio; 209 struct value vals[] = { 210 { .event = "l2_rqsts.demand_data_rd_hit", .val = 100 }, 211 { .event = "l2_rqsts.pf_hit", .val = 200 }, 212 { .event = "l2_rqsts.rfo_hit", .val = 300 }, 213 { .event = "l2_rqsts.all_demand_data_rd", .val = 400 }, 214 { .event = "l2_rqsts.pf_miss", .val = 500 }, 215 { .event = "l2_rqsts.rfo_miss", .val = 600 }, 216 { .event = NULL, }, 217 }; 218 219 TEST_ASSERT_VAL("failed to compute metric", 220 compute_metric("DCache_L2_Hits", vals, &ratio) == 0); 221 222 TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio", 223 ratio == 0.3); 224 225 TEST_ASSERT_VAL("failed to compute metric", 226 compute_metric("DCache_L2_Misses", vals, &ratio) == 0); 227 228 TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio", 229 ratio == 0.7); 230 return 0; 231 } 232 233 static int test_recursion_fail(void) 234 { 235 double ratio; 236 struct value vals[] = { 237 { .event = "inst_retired.any", .val = 300 }, 238 { .event = "cpu_clk_unhalted.thread", .val = 200 }, 239 { .event = NULL, }, 240 }; 241 242 TEST_ASSERT_VAL("failed to find recursion", 243 compute_metric("M1", vals, &ratio) == -1); 244 245 TEST_ASSERT_VAL("failed to find recursion", 246 compute_metric("M3", vals, &ratio) == -1); 247 return 0; 248 } 249 250 static int test_memory_bandwidth(void) 251 { 252 double ratio; 253 struct value vals[] = { 254 { .event = "l1d.replacement", .val = 4000000 }, 255 { .event = "duration_time", .val = 200000000 }, 256 { .event = NULL, }, 257 }; 258 259 TEST_ASSERT_VAL("failed to compute metric", 260 compute_metric("L1D_Cache_Fill_BW", vals, &ratio) == 0); 261 TEST_ASSERT_VAL("L1D_Cache_Fill_BW, wrong ratio", 262 1.28 == ratio); 263 264 return 0; 265 } 266 267 static int test_metric_group(void) 268 { 269 double ratio1, ratio2; 270 struct value vals[] = { 271 { .event = "cpu_clk_unhalted.thread", .val = 200 }, 272 { .event = "l1d-loads-misses", .val = 300 }, 273 { .event = "l1i-loads-misses", .val = 200 }, 274 { .event = "inst_retired.any", .val = 400 }, 275 { .event = NULL, }, 276 }; 277 278 TEST_ASSERT_VAL("failed to find recursion", 279 compute_metric_group("group1", vals, 280 "IPC", &ratio1, 281 "cache_miss_cycles", &ratio2) == 0); 282 283 TEST_ASSERT_VAL("group IPC failed, wrong ratio", 284 ratio1 == 2.0); 285 286 TEST_ASSERT_VAL("group cache_miss_cycles failed, wrong ratio", 287 ratio2 == 1.25); 288 return 0; 289 } 290 291 static int test__parse_metric(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 292 { 293 TEST_ASSERT_VAL("IPC failed", test_ipc() == 0); 294 TEST_ASSERT_VAL("frontend failed", test_frontend() == 0); 295 TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0); 296 TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0); 297 TEST_ASSERT_VAL("Memory bandwidth", test_memory_bandwidth() == 0); 298 TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0); 299 TEST_ASSERT_VAL("test metric group", test_metric_group() == 0); 300 return 0; 301 } 302 303 DEFINE_SUITE("Parse and process metrics", parse_metric); 304