xref: /openbmc/linux/tools/perf/tests/expr.c (revision 6f2bde9b)
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 	/* Test toplogy constants appear well ordered. */
189 	expr__ctx_clear(ctx);
190 	TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
191 	TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
192 	TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
193 	TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
194 	TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
195 	TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
196 
197 	if (num_dies) // Some platforms do not have CPU die support, for example s390
198 		TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
199 
200 	TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
201 	if (is_intel)
202 		TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
203 	else
204 		TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
205 
206 	/*
207 	 * Source count returns the number of events aggregating in a leader
208 	 * event including the leader. Check parsing yields an id.
209 	 */
210 	expr__ctx_clear(ctx);
211 	TEST_ASSERT_VAL("source count",
212 			expr__find_ids("source_count(EVENT1)",
213 			NULL, ctx) == 0);
214 	TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
215 	TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
216 
217 	expr__ctx_free(ctx);
218 
219 	return 0;
220 }
221 
222 DEFINE_SUITE("Simple expression parser", expr);
223