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