xref: /linux/tools/perf/tests/expr.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
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 == 0);
124 	TEST_ASSERT_VAL("division by zero", isnan(val));
125 
126 	p = "BAR/";
127 	ret = expr__parse(&val, ctx, p);
128 	TEST_ASSERT_VAL("missing operand", ret == -1);
129 
130 	expr__ctx_clear(ctx);
131 	TEST_ASSERT_VAL("find ids",
132 			expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
133 					ctx) == 0);
134 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
135 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr));
136 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr));
137 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr));
138 
139 	expr__ctx_clear(ctx);
140 	ctx->sctx.runtime = 3;
141 	TEST_ASSERT_VAL("find ids",
142 			expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
143 					NULL, ctx) == 0);
144 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
145 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr));
146 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr));
147 
148 	expr__ctx_clear(ctx);
149 	TEST_ASSERT_VAL("find ids",
150 			expr__find_ids("dash\\-event1 - dash\\-event2",
151 				       NULL, ctx) == 0);
152 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
153 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr));
154 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr));
155 
156 	/* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
157 	{
158 		bool smton = smt_on();
159 		bool corewide = core_wide(/*system_wide=*/false,
160 					  /*user_requested_cpus=*/false);
161 
162 		expr__ctx_clear(ctx);
163 		TEST_ASSERT_VAL("find ids",
164 				expr__find_ids("EVENT1 if #smt_on else EVENT2",
165 					NULL, ctx) == 0);
166 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
167 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
168 							  smton ? "EVENT1" : "EVENT2",
169 							  &val_ptr));
170 
171 		expr__ctx_clear(ctx);
172 		TEST_ASSERT_VAL("find ids",
173 				expr__find_ids("EVENT1 if #core_wide else EVENT2",
174 					NULL, ctx) == 0);
175 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
176 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
177 							  corewide ? "EVENT1" : "EVENT2",
178 							  &val_ptr));
179 
180 	}
181 	/* The expression is a constant 1.0 without needing to evaluate EVENT1. */
182 	expr__ctx_clear(ctx);
183 	TEST_ASSERT_VAL("find ids",
184 			expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
185 			NULL, ctx) == 0);
186 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
187 
188 	/* The expression is a constant 0.0 without needing to evaluate EVENT1. */
189 	expr__ctx_clear(ctx);
190 	TEST_ASSERT_VAL("find ids",
191 			expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
192 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
193 	expr__ctx_clear(ctx);
194 	TEST_ASSERT_VAL("find ids",
195 			expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
196 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
197 	expr__ctx_clear(ctx);
198 	TEST_ASSERT_VAL("find ids",
199 			expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
200 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
201 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
202 	expr__ctx_clear(ctx);
203 	TEST_ASSERT_VAL("find ids",
204 			expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
205 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
206 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
207 
208 	/* The expression is a constant 1.0 without needing to evaluate EVENT1. */
209 	expr__ctx_clear(ctx);
210 	TEST_ASSERT_VAL("find ids",
211 			expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
212 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
213 	expr__ctx_clear(ctx);
214 	TEST_ASSERT_VAL("find ids",
215 			expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
216 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
217 	expr__ctx_clear(ctx);
218 	TEST_ASSERT_VAL("find ids",
219 			expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
220 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
221 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
222 	expr__ctx_clear(ctx);
223 	TEST_ASSERT_VAL("find ids",
224 			expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
225 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
226 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
227 
228 	/* Test toplogy constants appear well ordered. */
229 	expr__ctx_clear(ctx);
230 	TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
231 	TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
232 	TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
233 	TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
234 	TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
235 	TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
236 
237 	if (num_dies) // Some platforms do not have CPU die support, for example s390
238 		TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
239 
240 	TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
241 	if (is_intel)
242 		TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
243 	else
244 		TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
245 
246 	/*
247 	 * Source count returns the number of events aggregating in a leader
248 	 * event including the leader. Check parsing yields an id.
249 	 */
250 	expr__ctx_clear(ctx);
251 	TEST_ASSERT_VAL("source count",
252 			expr__find_ids("source_count(EVENT1)",
253 			NULL, ctx) == 0);
254 	TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
255 	TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
256 
257 	/* has_event returns 1 when an event exists. */
258 	expr__add_id_val(ctx, strdup("cycles"), 2);
259 	ret = test(ctx, "has_event(cycles)", 1);
260 
261 	expr__ctx_free(ctx);
262 
263 	return 0;
264 }
265 
266 DEFINE_SUITE("Simple expression parser", expr);
267