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/header.h" 6 #include "util/smt.h" 7 #include "tests.h" 8 #include <math.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <linux/zalloc.h> 12 13 static int test_ids_union(void) 14 { 15 struct hashmap *ids1, *ids2; 16 17 /* Empty union. */ 18 ids1 = ids__new(); 19 TEST_ASSERT_VAL("ids__new", ids1); 20 ids2 = ids__new(); 21 TEST_ASSERT_VAL("ids__new", ids2); 22 23 ids1 = ids__union(ids1, ids2); 24 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0); 25 26 /* Union {foo, bar} against {}. */ 27 ids2 = ids__new(); 28 TEST_ASSERT_VAL("ids__new", ids2); 29 30 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0); 31 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0); 32 33 ids1 = ids__union(ids1, ids2); 34 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2); 35 36 /* Union {foo, bar} against {foo}. */ 37 ids2 = ids__new(); 38 TEST_ASSERT_VAL("ids__new", ids2); 39 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0); 40 41 ids1 = ids__union(ids1, ids2); 42 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2); 43 44 /* Union {foo, bar} against {bar,baz}. */ 45 ids2 = ids__new(); 46 TEST_ASSERT_VAL("ids__new", ids2); 47 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0); 48 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0); 49 50 ids1 = ids__union(ids1, ids2); 51 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3); 52 53 ids__free(ids1); 54 55 return 0; 56 } 57 58 static int test(struct expr_parse_ctx *ctx, const char *e, double val2) 59 { 60 double val; 61 62 if (expr__parse(&val, ctx, e)) 63 TEST_ASSERT_VAL("parse test failed", 0); 64 TEST_ASSERT_VAL("unexpected value", val == val2); 65 return 0; 66 } 67 68 static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused) 69 { 70 struct expr_id_data *val_ptr; 71 const char *p; 72 double val, num_cpus, num_cores, num_dies, num_packages; 73 int ret; 74 struct expr_parse_ctx *ctx; 75 bool is_intel = false; 76 char buf[128]; 77 78 if (!get_cpuid(buf, sizeof(buf))) 79 is_intel = strstr(buf, "Intel") != NULL; 80 81 TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0); 82 83 ctx = expr__ctx_new(); 84 TEST_ASSERT_VAL("expr__ctx_new", ctx); 85 expr__add_id_val(ctx, strdup("FOO"), 1); 86 expr__add_id_val(ctx, strdup("BAR"), 2); 87 88 ret = test(ctx, "1+1", 2); 89 ret |= test(ctx, "FOO+BAR", 3); 90 ret |= test(ctx, "(BAR/2)%2", 1); 91 ret |= test(ctx, "1 - -4", 5); 92 ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5); 93 ret |= test(ctx, "1-1 | 1", 1); 94 ret |= test(ctx, "1-1 & 1", 0); 95 ret |= test(ctx, "min(1,2) + 1", 2); 96 ret |= test(ctx, "max(1,2) + 1", 3); 97 ret |= test(ctx, "1+1 if 3*4 else 0", 2); 98 ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100); 99 ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200); 100 ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100); 101 ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300); 102 ret |= test(ctx, "1.1 + 2.1", 3.2); 103 ret |= test(ctx, ".1 + 2.", 2.1); 104 ret |= test(ctx, "d_ratio(1, 2)", 0.5); 105 ret |= test(ctx, "d_ratio(2.5, 0)", 0); 106 ret |= test(ctx, "1.1 < 2.2", 1); 107 ret |= test(ctx, "2.2 > 1.1", 1); 108 ret |= test(ctx, "1.1 < 1.1", 0); 109 ret |= test(ctx, "2.2 > 2.2", 0); 110 ret |= test(ctx, "2.2 < 1.1", 0); 111 ret |= test(ctx, "1.1 > 2.2", 0); 112 ret |= test(ctx, "1.1e10 < 1.1e100", 1); 113 ret |= test(ctx, "1.1e2 > 1.1e-2", 1); 114 115 if (ret) { 116 expr__ctx_free(ctx); 117 return ret; 118 } 119 120 p = "FOO/0"; 121 ret = expr__parse(&val, ctx, p); 122 TEST_ASSERT_VAL("division by zero", ret == -1); 123 124 p = "BAR/"; 125 ret = expr__parse(&val, ctx, p); 126 TEST_ASSERT_VAL("missing operand", ret == -1); 127 128 expr__ctx_clear(ctx); 129 TEST_ASSERT_VAL("find ids", 130 expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO", 131 ctx) == 0); 132 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3); 133 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr)); 134 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr)); 135 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr)); 136 137 expr__ctx_clear(ctx); 138 ctx->sctx.runtime = 3; 139 TEST_ASSERT_VAL("find ids", 140 expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@", 141 NULL, ctx) == 0); 142 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2); 143 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr)); 144 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr)); 145 146 expr__ctx_clear(ctx); 147 TEST_ASSERT_VAL("find ids", 148 expr__find_ids("dash\\-event1 - dash\\-event2", 149 NULL, ctx) == 0); 150 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2); 151 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr)); 152 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr)); 153 154 /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */ 155 { 156 struct cpu_topology *topology = cpu_topology__new(); 157 bool smton = smt_on(topology); 158 bool corewide = core_wide(/*system_wide=*/false, 159 /*user_requested_cpus=*/false, 160 topology); 161 162 cpu_topology__delete(topology); 163 expr__ctx_clear(ctx); 164 TEST_ASSERT_VAL("find ids", 165 expr__find_ids("EVENT1 if #smt_on else EVENT2", 166 NULL, ctx) == 0); 167 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 168 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, 169 smton ? "EVENT1" : "EVENT2", 170 &val_ptr)); 171 172 expr__ctx_clear(ctx); 173 TEST_ASSERT_VAL("find ids", 174 expr__find_ids("EVENT1 if #core_wide else EVENT2", 175 NULL, ctx) == 0); 176 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1); 177 TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, 178 corewide ? "EVENT1" : "EVENT2", 179 &val_ptr)); 180 181 } 182 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */ 183 expr__ctx_clear(ctx); 184 TEST_ASSERT_VAL("find ids", 185 expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0", 186 NULL, ctx) == 0); 187 TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); 188 189 /* Test toplogy constants appear well ordered. */ 190 expr__ctx_clear(ctx); 191 TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0); 192 TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0); 193 TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores); 194 TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0); 195 TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies); 196 TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0); 197 198 if (num_dies) // Some platforms do not have CPU die support, for example s390 199 TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages); 200 201 TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0); 202 if (is_intel) 203 TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0); 204 else 205 TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO); 206 207 /* 208 * Source count returns the number of events aggregating in a leader 209 * event including the leader. Check parsing yields an id. 210 */ 211 expr__ctx_clear(ctx); 212 TEST_ASSERT_VAL("source count", 213 expr__find_ids("source_count(EVENT1)", 214 NULL, ctx) == 0); 215 TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1); 216 TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr)); 217 218 expr__ctx_free(ctx); 219 220 return 0; 221 } 222 223 DEFINE_SUITE("Simple expression parser", expr); 224