1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/cputopo.h"
3 #include "util/debug.h"
5 #include "util/hashmap.h"
6 #include "util/header.h"
9 #include <perf/cpumap.h>
14 #include <linux/zalloc.h>
16 static int test_ids_union(void)
18 struct hashmap
*ids1
, *ids2
;
22 TEST_ASSERT_VAL("ids__new", ids1
);
24 TEST_ASSERT_VAL("ids__new", ids2
);
26 ids1
= ids__union(ids1
, ids2
);
27 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1
), 0);
29 /* Union {foo, bar} against {}. */
31 TEST_ASSERT_VAL("ids__new", ids2
);
33 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1
, strdup("foo")), 0);
34 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1
, strdup("bar")), 0);
36 ids1
= ids__union(ids1
, ids2
);
37 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1
), 2);
39 /* Union {foo, bar} against {foo}. */
41 TEST_ASSERT_VAL("ids__new", ids2
);
42 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2
, strdup("foo")), 0);
44 ids1
= ids__union(ids1
, ids2
);
45 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1
), 2);
47 /* Union {foo, bar} against {bar,baz}. */
49 TEST_ASSERT_VAL("ids__new", ids2
);
50 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2
, strdup("bar")), 0);
51 TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2
, strdup("baz")), 0);
53 ids1
= ids__union(ids1
, ids2
);
54 TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1
), 3);
61 static int test(struct expr_parse_ctx
*ctx
, const char *e
, double val2
)
65 if (expr__parse(&val
, ctx
, e
))
66 TEST_ASSERT_VAL("parse test failed", 0);
67 TEST_ASSERT_VAL("unexpected value", val
== val2
);
71 static int test__expr(struct test_suite
*t __maybe_unused
, int subtest __maybe_unused
)
73 struct expr_id_data
*val_ptr
;
75 double val
, num_cpus_online
, num_cpus
, num_cores
, num_dies
, num_packages
;
77 struct expr_parse_ctx
*ctx
;
78 bool is_intel
= false;
79 char strcmp_cpuid_buf
[256];
80 struct perf_cpu cpu
= {-1};
81 char *cpuid
= get_cpuid_allow_env_override(cpu
);
82 char *escaped_cpuid1
, *escaped_cpuid2
;
84 TEST_ASSERT_VAL("get_cpuid", cpuid
);
85 is_intel
= strstr(cpuid
, "Intel") != NULL
;
87 TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
89 ctx
= expr__ctx_new();
90 TEST_ASSERT_VAL("expr__ctx_new", ctx
);
91 expr__add_id_val(ctx
, strdup("FOO"), 1);
92 expr__add_id_val(ctx
, strdup("BAR"), 2);
94 ret
= test(ctx
, "1+1", 2);
95 ret
|= test(ctx
, "FOO+BAR", 3);
96 ret
|= test(ctx
, "(BAR/2)%2", 1);
97 ret
|= test(ctx
, "1 - -4", 5);
98 ret
|= test(ctx
, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
99 ret
|= test(ctx
, "1-1 | 1", 1);
100 ret
|= test(ctx
, "1-1 & 1", 0);
101 ret
|= test(ctx
, "min(1,2) + 1", 2);
102 ret
|= test(ctx
, "max(1,2) + 1", 3);
103 ret
|= test(ctx
, "1+1 if 3*4 else 0", 2);
104 ret
|= test(ctx
, "100 if 1 else 200 if 1 else 300", 100);
105 ret
|= test(ctx
, "100 if 0 else 200 if 1 else 300", 200);
106 ret
|= test(ctx
, "100 if 1 else 200 if 0 else 300", 100);
107 ret
|= test(ctx
, "100 if 0 else 200 if 0 else 300", 300);
108 ret
|= test(ctx
, "1.1 + 2.1", 3.2);
109 ret
|= test(ctx
, ".1 + 2.", 2.1);
110 ret
|= test(ctx
, "d_ratio(1, 2)", 0.5);
111 ret
|= test(ctx
, "d_ratio(2.5, 0)", 0);
112 ret
|= test(ctx
, "1.1 < 2.2", 1);
113 ret
|= test(ctx
, "2.2 > 1.1", 1);
114 ret
|= test(ctx
, "1.1 < 1.1", 0);
115 ret
|= test(ctx
, "2.2 > 2.2", 0);
116 ret
|= test(ctx
, "2.2 < 1.1", 0);
117 ret
|= test(ctx
, "1.1 > 2.2", 0);
118 ret
|= test(ctx
, "1.1e10 < 1.1e100", 1);
119 ret
|= test(ctx
, "1.1e2 > 1.1e-2", 1);
127 ret
= expr__parse(&val
, ctx
, p
);
128 TEST_ASSERT_VAL("division by zero", ret
== 0);
129 TEST_ASSERT_VAL("division by zero", isnan(val
));
132 ret
= expr__parse(&val
, ctx
, p
);
133 TEST_ASSERT_VAL("missing operand", ret
== -1);
135 expr__ctx_clear(ctx
);
136 TEST_ASSERT_VAL("find ids",
137 expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
139 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 3);
140 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "BAR", &val_ptr
));
141 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "BAZ", &val_ptr
));
142 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "BOZO", &val_ptr
));
144 expr__ctx_clear(ctx
);
145 ctx
->sctx
.runtime
= 3;
146 TEST_ASSERT_VAL("find ids",
147 expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
149 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 2);
150 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "EVENT1,param=3@", &val_ptr
));
151 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "EVENT2,param=3@", &val_ptr
));
153 expr__ctx_clear(ctx
);
154 TEST_ASSERT_VAL("find ids",
155 expr__find_ids("dash\\-event1 - dash\\-event2",
157 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 2);
158 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "dash-event1", &val_ptr
));
159 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "dash-event2", &val_ptr
));
161 /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
163 bool smton
= smt_on();
164 bool corewide
= core_wide(/*system_wide=*/false,
165 /*user_requested_cpus=*/false);
167 expr__ctx_clear(ctx
);
168 TEST_ASSERT_VAL("find ids",
169 expr__find_ids("EVENT1 if #smt_on else EVENT2",
171 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 1);
172 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
,
173 smton
? "EVENT1" : "EVENT2",
176 expr__ctx_clear(ctx
);
177 TEST_ASSERT_VAL("find ids",
178 expr__find_ids("EVENT1 if #core_wide else EVENT2",
180 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 1);
181 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
,
182 corewide
? "EVENT1" : "EVENT2",
186 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
187 expr__ctx_clear(ctx
);
188 TEST_ASSERT_VAL("find ids",
189 expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
191 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 0);
193 /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
194 expr__ctx_clear(ctx
);
195 TEST_ASSERT_VAL("find ids",
196 expr__find_ids("0 & EVENT1 > 0", NULL
, ctx
) == 0);
197 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 0);
198 expr__ctx_clear(ctx
);
199 TEST_ASSERT_VAL("find ids",
200 expr__find_ids("EVENT1 > 0 & 0", NULL
, ctx
) == 0);
201 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 0);
202 expr__ctx_clear(ctx
);
203 TEST_ASSERT_VAL("find ids",
204 expr__find_ids("1 & EVENT1 > 0", 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 expr__ctx_clear(ctx
);
208 TEST_ASSERT_VAL("find ids",
209 expr__find_ids("EVENT1 > 0 & 1", NULL
, ctx
) == 0);
210 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 1);
211 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "EVENT1", &val_ptr
));
213 /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
214 expr__ctx_clear(ctx
);
215 TEST_ASSERT_VAL("find ids",
216 expr__find_ids("1 | EVENT1 > 0", NULL
, ctx
) == 0);
217 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 0);
218 expr__ctx_clear(ctx
);
219 TEST_ASSERT_VAL("find ids",
220 expr__find_ids("EVENT1 > 0 | 1", NULL
, ctx
) == 0);
221 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 0);
222 expr__ctx_clear(ctx
);
223 TEST_ASSERT_VAL("find ids",
224 expr__find_ids("0 | EVENT1 > 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 expr__ctx_clear(ctx
);
228 TEST_ASSERT_VAL("find ids",
229 expr__find_ids("EVENT1 > 0 | 0", NULL
, ctx
) == 0);
230 TEST_ASSERT_VAL("find ids", hashmap__size(ctx
->ids
) == 1);
231 TEST_ASSERT_VAL("find ids", hashmap__find(ctx
->ids
, "EVENT1", &val_ptr
));
233 /* Test toplogy constants appear well ordered. */
234 expr__ctx_clear(ctx
);
235 TEST_ASSERT_VAL("#num_cpus_online",
236 expr__parse(&num_cpus_online
, ctx
, "#num_cpus_online") == 0);
237 TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus
, ctx
, "#num_cpus") == 0);
238 TEST_ASSERT_VAL("#num_cpus >= #num_cpus_online", num_cpus
>= num_cpus_online
);
239 TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores
, ctx
, "#num_cores") == 0);
240 TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus
>= num_cores
);
241 TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies
, ctx
, "#num_dies") == 0);
242 TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores
>= num_dies
);
243 TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages
, ctx
, "#num_packages") == 0);
245 if (num_dies
) // Some platforms do not have CPU die support, for example s390
246 TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies
>= num_packages
);
248 TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val
, ctx
, "#system_tsc_freq") == 0);
250 TEST_ASSERT_VAL("#system_tsc_freq > 0", val
> 0);
252 TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val
) == FP_ZERO
);
255 * Source count returns the number of events aggregating in a leader
256 * event including the leader. Check parsing yields an id.
258 expr__ctx_clear(ctx
);
259 TEST_ASSERT_VAL("source count",
260 expr__find_ids("source_count(EVENT1)",
262 TEST_ASSERT_VAL("source count", hashmap__size(ctx
->ids
) == 1);
263 TEST_ASSERT_VAL("source count", hashmap__find(ctx
->ids
, "EVENT1", &val_ptr
));
266 /* Test no cpuid match */
267 ret
= test(ctx
, "strcmp_cpuid_str(0x0)", 0);
270 * Test cpuid match with current cpuid. Special chars have to be
273 escaped_cpuid1
= strreplace_chars('-', cpuid
, "\\-");
275 escaped_cpuid2
= strreplace_chars(',', escaped_cpuid1
, "\\,");
276 free(escaped_cpuid1
);
277 escaped_cpuid1
= strreplace_chars('=', escaped_cpuid2
, "\\=");
278 free(escaped_cpuid2
);
279 scnprintf(strcmp_cpuid_buf
, sizeof(strcmp_cpuid_buf
),
280 "strcmp_cpuid_str(%s)", escaped_cpuid1
);
281 free(escaped_cpuid1
);
282 ret
|= test(ctx
, strcmp_cpuid_buf
, 1);
284 /* has_event returns 1 when an event exists. */
285 expr__add_id_val(ctx
, strdup("cycles"), 2);
286 ret
|= test(ctx
, "has_event(cycles)", 1);
293 DEFINE_SUITE("Simple expression parser", expr
);