1 // SPDX-License-Identifier: GPL-2.0 2 #include "util/cputopo.h" 3 #include "util/debug.h" 4 #include "util/expr.h" 5 #include "util/hashmap.h" 6 #include "util/header.h" 7 #include "util/smt.h" 8 #include "tests.h" 9 #include <perf/cpumap.h> 10 #include <math.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <string2.h> 14 #include <linux/zalloc.h> 15 16 static int test_ids_union(void) 17 { 18 struct hashmap *ids1, *ids2; 19 20 /* Empty union. */ 21 ids1 = ids__new(); 22 TEST_ASSERT_VAL("ids__new", ids1); 23 ids2 = ids__new(); 24 TEST_ASSERT_VAL("ids__new", ids2); 25 26 ids1 = ids__union(ids1, ids2); 27 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0); 28 29 /* Union {foo, bar} against {}. */ 30 ids2 = ids__new(); 31 TEST_ASSERT_VAL("ids__new", ids2); 32 33 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0); 34 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0); 35 36 ids1 = ids__union(ids1, ids2); 37 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2); 38 39 /* Union {foo, bar} against {foo}. */ 40 ids2 = ids__new(); 41 TEST_ASSERT_VAL("ids__new", ids2); 42 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0); 43 44 ids1 = ids__union(ids1, ids2); 45 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2); 46 47 /* Union {foo, bar} against {bar,baz}. */ 48 ids2 = ids__new(); 49 TEST_ASSERT_VAL("ids__new", ids2); 50 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0); 51 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0); 52 53 ids1 = ids__union(ids1, ids2); 54 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3); 55 56 ids__free(ids1); 57 58 return 0; 59 } 60 61 static int test(struct expr_parse_ctx *ctx, const char *e, double val2) 62 { 63 double val; 64 65 if (expr__parse(&val, ctx, e)) 66 TEST_ASSERT_VAL("parse test failed", 0); 67 TEST_ASSERT_VAL("unexpected value", val == val2); 68 return 0; 69 } 70 71 static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused) 72 { 73 struct expr_id_data *val_ptr; 74 const char *p; 75 double val, num_cpus_online, num_cpus, num_cores, num_dies, num_packages; 76 int ret; 77 struct expr_parse_ctx *ctx; 78 char strcmp_cpuid_buf[256]; 79 struct perf_cpu cpu = {-1}; 80 char *cpuid = get_cpuid_allow_env_override(cpu); 81 char *escaped_cpuid1, *escaped_cpuid2; 82 83 TEST_ASSERT_VAL("get_cpuid", cpuid); 84 85 TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0); 86 87 ctx = expr__ctx_new(); 88 TEST_ASSERT_VAL("expr__ctx_new", ctx); 89 expr__add_id_val(ctx, strdup("FOO"), 1); 90 expr__add_id_val(ctx, strdup("BAR"), 2); 91 92 ret = test(ctx, "1+1", 2); 93 ret |= test(ctx, "FOO+BAR", 3); 94 ret |= test(ctx, "(BAR/2)%2", 1); 95 ret |= test(ctx, "1 - -4", 5); 96 ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5); 97 ret |= test(ctx, "1-1 | 1", 1); 98 ret |= test(ctx, "1-1 & 1", 0); 99 ret |= test(ctx, "min(1,2) + 1", 2); 100 ret |= test(ctx, "max(1,2) + 1", 3); 101 ret |= test(ctx, "1+1 if 3*4 else 0", 2); 102 ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100); 103 ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200); 104 ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100); 105 ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300); 106 ret |= test(ctx, "1.1 + 2.1", 3.2); 107 ret |= test(ctx, ".1 + 2.", 2.1); 108 ret |= test(ctx, "d_ratio(1, 2)", 0.5); 109 ret |= test(ctx, "d_ratio(2.5, 0)", 0); 110 ret |= test(ctx, "1.1 < 2.2", 1); 111 ret |= test(ctx, "2.2 > 1.1", 1); 112 ret |= test(ctx, "1.1 < 1.1", 0); 113 ret |= test(ctx, "2.2 > 2.2", 0); 114 ret |= test(ctx, "2.2 < 1.1", 0); 115 ret |= test(ctx, "1.1 > 2.2", 0); 116 ret |= test(ctx, "1.1e10 < 1.1e100", 1); 117 ret |= test(ctx, "1.1e2 > 1.1e-2", 1); 118 119 if (ret) { 120 expr__ctx_free(ctx); 121 return ret; 122 } 123 124 p = "FOO/0"; 125 ret = expr__parse(&val, ctx, p); 126 TEST_ASSERT_VAL("division by zero", ret == 0); 127 TEST_ASSERT_VAL("division by zero", isnan(val)); 128 129 p = "BAR/"; 130 ret = expr__parse(&val, ctx, p); 131 TEST_ASSERT_VAL("missing operand", ret == -1); 132 133 expr__ctx_clear(ctx); 134 TEST_ASSERT_VAL("find ids", 135 expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO", 136 ctx) == 0); 137 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3); 138 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr)); 139 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr)); 140 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr)); 141 142 expr__ctx_clear(ctx); 143 ctx->sctx.runtime = 3; 144 TEST_ASSERT_VAL("find ids", 145 expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@", 146 NULL, ctx) == 0); 147 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2); 148 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr)); 149 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr)); 150 151 expr__ctx_clear(ctx); 152 TEST_ASSERT_VAL("find ids", 153 expr__find_ids("dash\\-event1 - dash\\-event2", 154 NULL, ctx) == 0); 155 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2); 156 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr)); 157 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr)); 158 159 /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */ 160 { 161 bool smton = smt_on(); 162 bool corewide = core_wide(/*system_wide=*/false, 163 /*user_requested_cpus=*/false); 164 165 expr__ctx_clear(ctx); 166 TEST_ASSERT_VAL("find ids", 167 expr__find_ids("EVENT1 if #smt_on else EVENT2", 168 NULL, ctx) == 0); 169 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 170 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, 171 smton ? "EVENT1" : "EVENT2", 172 &val_ptr)); 173 174 expr__ctx_clear(ctx); 175 TEST_ASSERT_VAL("find ids", 176 expr__find_ids("EVENT1 if #core_wide else EVENT2", 177 NULL, ctx) == 0); 178 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 179 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, 180 corewide ? "EVENT1" : "EVENT2", 181 &val_ptr)); 182 183 } 184 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */ 185 expr__ctx_clear(ctx); 186 TEST_ASSERT_VAL("find ids", 187 expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0", 188 NULL, ctx) == 0); 189 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 190 191 /* The expression is a constant 0.0 without needing to evaluate EVENT1. */ 192 expr__ctx_clear(ctx); 193 TEST_ASSERT_VAL("find ids", 194 expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0); 195 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 196 expr__ctx_clear(ctx); 197 TEST_ASSERT_VAL("find ids", 198 expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0); 199 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 200 expr__ctx_clear(ctx); 201 TEST_ASSERT_VAL("find ids", 202 expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0); 203 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 204 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 205 expr__ctx_clear(ctx); 206 TEST_ASSERT_VAL("find ids", 207 expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0); 208 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 209 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 210 211 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */ 212 expr__ctx_clear(ctx); 213 TEST_ASSERT_VAL("find ids", 214 expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0); 215 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 216 expr__ctx_clear(ctx); 217 TEST_ASSERT_VAL("find ids", 218 expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0); 219 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 220 expr__ctx_clear(ctx); 221 TEST_ASSERT_VAL("find ids", 222 expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0); 223 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 224 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 225 expr__ctx_clear(ctx); 226 TEST_ASSERT_VAL("find ids", 227 expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0); 228 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 229 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 230 231 /* Test toplogy constants appear well ordered. */ 232 expr__ctx_clear(ctx); 233 TEST_ASSERT_VAL("#num_cpus_online", 234 expr__parse(&num_cpus_online, ctx, "#num_cpus_online") == 0); 235 TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0); 236 TEST_ASSERT_VAL("#num_cpus >= #num_cpus_online", num_cpus >= num_cpus_online); 237 TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0); 238 TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores); 239 TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0); 240 TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies); 241 TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0); 242 243 if (num_dies) // Some platforms do not have CPU die support, for example s390 244 TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages); 245 246 247 if (expr__parse(&val, ctx, "#system_tsc_freq") == 0) { 248 bool is_intel = strstr(cpuid, "Intel") != NULL; 249 250 if (is_intel) 251 TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0); 252 else 253 TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO); 254 } else { 255 #if defined(__i386__) || defined(__x86_64__) 256 TEST_ASSERT_VAL("#system_tsc_freq unsupported", 0); 257 #endif 258 } 259 /* 260 * Source count returns the number of events aggregating in a leader 261 * event including the leader. Check parsing yields an id. 262 */ 263 expr__ctx_clear(ctx); 264 TEST_ASSERT_VAL("source count", 265 expr__find_ids("source_count(EVENT1)", 266 NULL, ctx) == 0); 267 TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1); 268 TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 269 270 271 /* Test no cpuid match */ 272 ret = test(ctx, "strcmp_cpuid_str(0x0)", 0); 273 274 /* 275 * Test cpuid match with current cpuid. Special chars have to be 276 * escaped. 277 */ 278 escaped_cpuid1 = strreplace_chars('-', cpuid, "\\-"); 279 free(cpuid); 280 escaped_cpuid2 = strreplace_chars(',', escaped_cpuid1, "\\,"); 281 free(escaped_cpuid1); 282 escaped_cpuid1 = strreplace_chars('=', escaped_cpuid2, "\\="); 283 free(escaped_cpuid2); 284 scnprintf(strcmp_cpuid_buf, sizeof(strcmp_cpuid_buf), 285 "strcmp_cpuid_str(%s)", escaped_cpuid1); 286 free(escaped_cpuid1); 287 ret |= test(ctx, strcmp_cpuid_buf, 1); 288 289 /* has_event returns 1 when an event exists. */ 290 expr__add_id_val(ctx, strdup("cycles"), 2); 291 ret |= test(ctx, "has_event(cycles)", 1); 292 293 expr__ctx_free(ctx); 294 295 return ret; 296 } 297 298 DEFINE_SUITE("Simple expression parser", expr); 299