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