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