xref: /openbmc/linux/tools/perf/tests/expr.c (revision 6f765bbb)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
209b73fe9SIan Rogers #include "util/cputopo.h"
307516736SAndi Kleen #include "util/debug.h"
407516736SAndi Kleen #include "util/expr.h"
5bd560973SIan Rogers #include "util/hashmap.h"
66923397cSIan Rogers #include "util/header.h"
7a8e4e880SIan Rogers #include "util/smt.h"
807516736SAndi Kleen #include "tests.h"
96923397cSIan Rogers #include <math.h>
1007516736SAndi Kleen #include <stdlib.h>
118520a98dSArnaldo Carvalho de Melo #include <string.h>
12d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
1307516736SAndi Kleen 
14114a9d6eSIan Rogers static int test_ids_union(void)
15114a9d6eSIan Rogers {
16114a9d6eSIan Rogers 	struct hashmap *ids1, *ids2;
17114a9d6eSIan Rogers 
18114a9d6eSIan Rogers 	/* Empty union. */
19114a9d6eSIan Rogers 	ids1 = ids__new();
20114a9d6eSIan Rogers 	TEST_ASSERT_VAL("ids__new", ids1);
21114a9d6eSIan Rogers 	ids2 = ids__new();
22114a9d6eSIan Rogers 	TEST_ASSERT_VAL("ids__new", ids2);
23114a9d6eSIan Rogers 
24114a9d6eSIan Rogers 	ids1 = ids__union(ids1, ids2);
25114a9d6eSIan Rogers 	TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
26114a9d6eSIan Rogers 
27114a9d6eSIan Rogers 	/* Union {foo, bar} against {}. */
28114a9d6eSIan Rogers 	ids2 = ids__new();
29114a9d6eSIan Rogers 	TEST_ASSERT_VAL("ids__new", ids2);
30114a9d6eSIan Rogers 
3180be6434SIan Rogers 	TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
3280be6434SIan Rogers 	TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
33114a9d6eSIan Rogers 
34114a9d6eSIan Rogers 	ids1 = ids__union(ids1, ids2);
35114a9d6eSIan Rogers 	TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
36114a9d6eSIan Rogers 
37114a9d6eSIan Rogers 	/* Union {foo, bar} against {foo}. */
38114a9d6eSIan Rogers 	ids2 = ids__new();
39114a9d6eSIan Rogers 	TEST_ASSERT_VAL("ids__new", ids2);
4080be6434SIan Rogers 	TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
41114a9d6eSIan Rogers 
42114a9d6eSIan Rogers 	ids1 = ids__union(ids1, ids2);
43114a9d6eSIan Rogers 	TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
44114a9d6eSIan Rogers 
45114a9d6eSIan Rogers 	/* Union {foo, bar} against {bar,baz}. */
46114a9d6eSIan Rogers 	ids2 = ids__new();
47114a9d6eSIan Rogers 	TEST_ASSERT_VAL("ids__new", ids2);
4880be6434SIan Rogers 	TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
4980be6434SIan Rogers 	TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
50114a9d6eSIan Rogers 
51114a9d6eSIan Rogers 	ids1 = ids__union(ids1, ids2);
52114a9d6eSIan Rogers 	TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
53114a9d6eSIan Rogers 
54114a9d6eSIan Rogers 	ids__free(ids1);
55114a9d6eSIan Rogers 
56114a9d6eSIan Rogers 	return 0;
57114a9d6eSIan Rogers }
58114a9d6eSIan Rogers 
59aecce63eSJiri Olsa static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
6007516736SAndi Kleen {
6107516736SAndi Kleen 	double val;
6207516736SAndi Kleen 
63fa831fbbSIan Rogers 	if (expr__parse(&val, ctx, e))
6407516736SAndi Kleen 		TEST_ASSERT_VAL("parse test failed", 0);
6507516736SAndi Kleen 	TEST_ASSERT_VAL("unexpected value", val == val2);
6607516736SAndi Kleen 	return 0;
6707516736SAndi Kleen }
6807516736SAndi Kleen 
6933f44bfdSIan Rogers static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
7007516736SAndi Kleen {
71070b3b5aSJiri Olsa 	struct expr_id_data *val_ptr;
7207516736SAndi Kleen 	const char *p;
73fdf1e29bSIan Rogers 	double val, num_cpus, num_cores, num_dies, num_packages;
74ded80bdaSIan Rogers 	int ret;
75cb94a02eSIan Rogers 	struct expr_parse_ctx *ctx;
766923397cSIan Rogers 	bool is_intel = false;
776923397cSIan Rogers 	char buf[128];
786923397cSIan Rogers 
796923397cSIan Rogers 	if (!get_cpuid(buf, sizeof(buf)))
806923397cSIan Rogers 		is_intel = strstr(buf, "Intel") != NULL;
8107516736SAndi Kleen 
82114a9d6eSIan Rogers 	TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
83114a9d6eSIan Rogers 
84cb94a02eSIan Rogers 	ctx = expr__ctx_new();
85cb94a02eSIan Rogers 	TEST_ASSERT_VAL("expr__ctx_new", ctx);
86cb94a02eSIan Rogers 	expr__add_id_val(ctx, strdup("FOO"), 1);
87cb94a02eSIan Rogers 	expr__add_id_val(ctx, strdup("BAR"), 2);
8807516736SAndi Kleen 
89cb94a02eSIan Rogers 	ret = test(ctx, "1+1", 2);
90cb94a02eSIan Rogers 	ret |= test(ctx, "FOO+BAR", 3);
91cb94a02eSIan Rogers 	ret |= test(ctx, "(BAR/2)%2", 1);
92cb94a02eSIan Rogers 	ret |= test(ctx, "1 - -4",  5);
93cb94a02eSIan Rogers 	ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4",  5);
94cb94a02eSIan Rogers 	ret |= test(ctx, "1-1 | 1", 1);
95cb94a02eSIan Rogers 	ret |= test(ctx, "1-1 & 1", 0);
96cb94a02eSIan Rogers 	ret |= test(ctx, "min(1,2) + 1", 2);
97cb94a02eSIan Rogers 	ret |= test(ctx, "max(1,2) + 1", 3);
98cb94a02eSIan Rogers 	ret |= test(ctx, "1+1 if 3*4 else 0", 2);
994b65fc7bSIan Rogers 	ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100);
1004b65fc7bSIan Rogers 	ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200);
1014b65fc7bSIan Rogers 	ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100);
1024b65fc7bSIan Rogers 	ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300);
103cb94a02eSIan Rogers 	ret |= test(ctx, "1.1 + 2.1", 3.2);
104cb94a02eSIan Rogers 	ret |= test(ctx, ".1 + 2.", 2.1);
105cb94a02eSIan Rogers 	ret |= test(ctx, "d_ratio(1, 2)", 0.5);
106cb94a02eSIan Rogers 	ret |= test(ctx, "d_ratio(2.5, 0)", 0);
107cb94a02eSIan Rogers 	ret |= test(ctx, "1.1 < 2.2", 1);
108cb94a02eSIan Rogers 	ret |= test(ctx, "2.2 > 1.1", 1);
109cb94a02eSIan Rogers 	ret |= test(ctx, "1.1 < 1.1", 0);
110cb94a02eSIan Rogers 	ret |= test(ctx, "2.2 > 2.2", 0);
111cb94a02eSIan Rogers 	ret |= test(ctx, "2.2 < 1.1", 0);
112cb94a02eSIan Rogers 	ret |= test(ctx, "1.1 > 2.2", 0);
113e5287e6dSIan Rogers 	ret |= test(ctx, "1.1e10 < 1.1e100", 1);
114e5287e6dSIan Rogers 	ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
11507516736SAndi Kleen 
116cb94a02eSIan Rogers 	if (ret) {
117cb94a02eSIan Rogers 		expr__ctx_free(ctx);
11807516736SAndi Kleen 		return ret;
119cb94a02eSIan Rogers 	}
12007516736SAndi Kleen 
12107516736SAndi Kleen 	p = "FOO/0";
122fa831fbbSIan Rogers 	ret = expr__parse(&val, ctx, p);
1232a939c86SIan Rogers 	TEST_ASSERT_VAL("division by zero", ret == 0);
1242a939c86SIan Rogers 	TEST_ASSERT_VAL("division by zero", isnan(val));
12507516736SAndi Kleen 
12607516736SAndi Kleen 	p = "BAR/";
127fa831fbbSIan Rogers 	ret = expr__parse(&val, ctx, p);
128d942815aSJiri Olsa 	TEST_ASSERT_VAL("missing operand", ret == -1);
12907516736SAndi Kleen 
130cb94a02eSIan Rogers 	expr__ctx_clear(ctx);
1317e06a5e3SIan Rogers 	TEST_ASSERT_VAL("find ids",
1327e06a5e3SIan Rogers 			expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
133fa831fbbSIan Rogers 					ctx) == 0);
1347e06a5e3SIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
135c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr));
136c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr));
137c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr));
138f97a8991SChangbin Du 
139cb94a02eSIan Rogers 	expr__ctx_clear(ctx);
1401a6abddeSIan Rogers 	ctx->sctx.runtime = 3;
1417e06a5e3SIan Rogers 	TEST_ASSERT_VAL("find ids",
1427e06a5e3SIan Rogers 			expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
143fa831fbbSIan Rogers 					NULL, ctx) == 0);
1447e06a5e3SIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
145c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr));
146c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr));
1479022608eSKajol Jain 
148604ce2f0SIan Rogers 	expr__ctx_clear(ctx);
149604ce2f0SIan Rogers 	TEST_ASSERT_VAL("find ids",
150604ce2f0SIan Rogers 			expr__find_ids("dash\\-event1 - dash\\-event2",
151604ce2f0SIan Rogers 				       NULL, ctx) == 0);
152604ce2f0SIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
153c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr));
154c302378bSEduard Zingerman 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr));
155604ce2f0SIan Rogers 
156a8e4e880SIan Rogers 	/* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
15709b73fe9SIan Rogers 	{
158207f7df7SIan Rogers 		bool smton = smt_on();
159f0c4b97aSIan Rogers 		bool corewide = core_wide(/*system_wide=*/false,
160207f7df7SIan Rogers 					  /*user_requested_cpus=*/false);
16109b73fe9SIan Rogers 
162a8e4e880SIan Rogers 		expr__ctx_clear(ctx);
163a8e4e880SIan Rogers 		TEST_ASSERT_VAL("find ids",
164a8e4e880SIan Rogers 				expr__find_ids("EVENT1 if #smt_on else EVENT2",
165fa831fbbSIan Rogers 					NULL, ctx) == 0);
166a8e4e880SIan Rogers 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
167a8e4e880SIan Rogers 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
16809b73fe9SIan Rogers 							  smton ? "EVENT1" : "EVENT2",
169c302378bSEduard Zingerman 							  &val_ptr));
170f0c4b97aSIan Rogers 
171f0c4b97aSIan Rogers 		expr__ctx_clear(ctx);
172f0c4b97aSIan Rogers 		TEST_ASSERT_VAL("find ids",
173f0c4b97aSIan Rogers 				expr__find_ids("EVENT1 if #core_wide else EVENT2",
174f0c4b97aSIan Rogers 					NULL, ctx) == 0);
175f0c4b97aSIan Rogers 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
176f0c4b97aSIan Rogers 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
177f0c4b97aSIan Rogers 							  corewide ? "EVENT1" : "EVENT2",
178c302378bSEduard Zingerman 							  &val_ptr));
179f0c4b97aSIan Rogers 
18009b73fe9SIan Rogers 	}
18194886961SIan Rogers 	/* The expression is a constant 1.0 without needing to evaluate EVENT1. */
18294886961SIan Rogers 	expr__ctx_clear(ctx);
18394886961SIan Rogers 	TEST_ASSERT_VAL("find ids",
18494886961SIan Rogers 			expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
185fa831fbbSIan Rogers 			NULL, ctx) == 0);
18694886961SIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
18794886961SIan Rogers 
188*6f765bbbSIan Rogers 	/* The expression is a constant 0.0 without needing to evaluate EVENT1. */
189*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
190*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
191*6f765bbbSIan Rogers 			expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
192*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
193*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
194*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
195*6f765bbbSIan Rogers 			expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
196*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
197*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
198*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
199*6f765bbbSIan Rogers 			expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
200*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
201*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
202*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
203*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
204*6f765bbbSIan Rogers 			expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
205*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
206*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
207*6f765bbbSIan Rogers 
208*6f765bbbSIan Rogers 	/* The expression is a constant 1.0 without needing to evaluate EVENT1. */
209*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
210*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
211*6f765bbbSIan Rogers 			expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
212*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
213*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
214*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
215*6f765bbbSIan Rogers 			expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
216*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
217*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
218*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
219*6f765bbbSIan Rogers 			expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
220*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
221*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
222*6f765bbbSIan Rogers 	expr__ctx_clear(ctx);
223*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids",
224*6f765bbbSIan Rogers 			expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
225*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
226*6f765bbbSIan Rogers 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
227*6f765bbbSIan Rogers 
228fdf1e29bSIan Rogers 	/* Test toplogy constants appear well ordered. */
229fdf1e29bSIan Rogers 	expr__ctx_clear(ctx);
230fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
231fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
232fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
233fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
234fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
235fdf1e29bSIan Rogers 	TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
2366c481031SThomas Richter 
2376c481031SThomas Richter 	if (num_dies) // Some platforms do not have CPU die support, for example s390
238fdf1e29bSIan Rogers 		TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
239fdf1e29bSIan Rogers 
2406923397cSIan Rogers 	TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
2416923397cSIan Rogers 	if (is_intel)
2426923397cSIan Rogers 		TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
2436923397cSIan Rogers 	else
2446923397cSIan Rogers 		TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
2456923397cSIan Rogers 
2469aba0adaSIan Rogers 	/*
2479aba0adaSIan Rogers 	 * Source count returns the number of events aggregating in a leader
2489aba0adaSIan Rogers 	 * event including the leader. Check parsing yields an id.
2499aba0adaSIan Rogers 	 */
2509aba0adaSIan Rogers 	expr__ctx_clear(ctx);
2519aba0adaSIan Rogers 	TEST_ASSERT_VAL("source count",
2529aba0adaSIan Rogers 			expr__find_ids("source_count(EVENT1)",
2539aba0adaSIan Rogers 			NULL, ctx) == 0);
2549aba0adaSIan Rogers 	TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
255c302378bSEduard Zingerman 	TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2569aba0adaSIan Rogers 
257cb94a02eSIan Rogers 	expr__ctx_free(ctx);
25807516736SAndi Kleen 
25907516736SAndi Kleen 	return 0;
26007516736SAndi Kleen }
261d68f0365SIan Rogers 
262d68f0365SIan Rogers DEFINE_SUITE("Simple expression parser", expr);
263