xref: /linux/tools/perf/tests/expr.c (revision 058443934524590d5537a80f490267cc95a61c05)
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, "1.1 + 2.1", 3.2);
99 	ret |= test(ctx, ".1 + 2.", 2.1);
100 	ret |= test(ctx, "d_ratio(1, 2)", 0.5);
101 	ret |= test(ctx, "d_ratio(2.5, 0)", 0);
102 	ret |= test(ctx, "1.1 < 2.2", 1);
103 	ret |= test(ctx, "2.2 > 1.1", 1);
104 	ret |= test(ctx, "1.1 < 1.1", 0);
105 	ret |= test(ctx, "2.2 > 2.2", 0);
106 	ret |= test(ctx, "2.2 < 1.1", 0);
107 	ret |= test(ctx, "1.1 > 2.2", 0);
108 	ret |= test(ctx, "1.1e10 < 1.1e100", 1);
109 	ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
110 
111 	if (ret) {
112 		expr__ctx_free(ctx);
113 		return ret;
114 	}
115 
116 	p = "FOO/0";
117 	ret = expr__parse(&val, ctx, p);
118 	TEST_ASSERT_VAL("division by zero", ret == -1);
119 
120 	p = "BAR/";
121 	ret = expr__parse(&val, ctx, p);
122 	TEST_ASSERT_VAL("missing operand", ret == -1);
123 
124 	expr__ctx_clear(ctx);
125 	TEST_ASSERT_VAL("find ids",
126 			expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
127 					ctx) == 0);
128 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
129 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR",
130 						    (void **)&val_ptr));
131 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ",
132 						    (void **)&val_ptr));
133 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO",
134 						    (void **)&val_ptr));
135 
136 	expr__ctx_clear(ctx);
137 	ctx->sctx.runtime = 3;
138 	TEST_ASSERT_VAL("find ids",
139 			expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
140 					NULL, ctx) == 0);
141 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
142 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@",
143 						    (void **)&val_ptr));
144 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@",
145 						    (void **)&val_ptr));
146 
147 	expr__ctx_clear(ctx);
148 	TEST_ASSERT_VAL("find ids",
149 			expr__find_ids("dash\\-event1 - dash\\-event2",
150 				       NULL, ctx) == 0);
151 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
152 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1",
153 						    (void **)&val_ptr));
154 	TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2",
155 						    (void **)&val_ptr));
156 
157 	/* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
158 	{
159 		struct cpu_topology *topology = cpu_topology__new();
160 		bool smton = smt_on(topology);
161 		bool corewide = core_wide(/*system_wide=*/false,
162 					  /*user_requested_cpus=*/false,
163 					  topology);
164 
165 		cpu_topology__delete(topology);
166 		expr__ctx_clear(ctx);
167 		TEST_ASSERT_VAL("find ids",
168 				expr__find_ids("EVENT1 if #smt_on else EVENT2",
169 					NULL, ctx) == 0);
170 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
171 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
172 							  smton ? "EVENT1" : "EVENT2",
173 							  (void **)&val_ptr));
174 
175 		expr__ctx_clear(ctx);
176 		TEST_ASSERT_VAL("find ids",
177 				expr__find_ids("EVENT1 if #core_wide else EVENT2",
178 					NULL, ctx) == 0);
179 		TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
180 		TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
181 							  corewide ? "EVENT1" : "EVENT2",
182 							  (void **)&val_ptr));
183 
184 	}
185 	/* The expression is a constant 1.0 without needing to evaluate EVENT1. */
186 	expr__ctx_clear(ctx);
187 	TEST_ASSERT_VAL("find ids",
188 			expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
189 			NULL, ctx) == 0);
190 	TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
191 
192 	/* Test toplogy constants appear well ordered. */
193 	expr__ctx_clear(ctx);
194 	TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
195 	TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
196 	TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
197 	TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
198 	TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
199 	TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
200 
201 	if (num_dies) // Some platforms do not have CPU die support, for example s390
202 		TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
203 
204 	TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
205 	if (is_intel)
206 		TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
207 	else
208 		TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
209 
210 	/*
211 	 * Source count returns the number of events aggregating in a leader
212 	 * event including the leader. Check parsing yields an id.
213 	 */
214 	expr__ctx_clear(ctx);
215 	TEST_ASSERT_VAL("source count",
216 			expr__find_ids("source_count(EVENT1)",
217 			NULL, ctx) == 0);
218 	TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
219 	TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1",
220 							(void **)&val_ptr));
221 
222 	expr__ctx_free(ctx);
223 
224 	return 0;
225 }
226 
227 DEFINE_SUITE("Simple expression parser", expr);
228