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>
12d37e53e8SJames Clark #include <string2.h>
13d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
1407516736SAndi Kleen
test_ids_union(void)15114a9d6eSIan Rogers static int test_ids_union(void)
16114a9d6eSIan Rogers {
17114a9d6eSIan Rogers struct hashmap *ids1, *ids2;
18114a9d6eSIan Rogers
19114a9d6eSIan Rogers /* Empty union. */
20114a9d6eSIan Rogers ids1 = ids__new();
21114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids1);
22114a9d6eSIan Rogers ids2 = ids__new();
23114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
24114a9d6eSIan Rogers
25114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
26114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
27114a9d6eSIan Rogers
28114a9d6eSIan Rogers /* Union {foo, bar} against {}. */
29114a9d6eSIan Rogers ids2 = ids__new();
30114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
31114a9d6eSIan Rogers
3280be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
3380be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
34114a9d6eSIan Rogers
35114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
36114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
37114a9d6eSIan Rogers
38114a9d6eSIan Rogers /* Union {foo, bar} against {foo}. */
39114a9d6eSIan Rogers ids2 = ids__new();
40114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
4180be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
42114a9d6eSIan Rogers
43114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
44114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
45114a9d6eSIan Rogers
46114a9d6eSIan Rogers /* Union {foo, bar} against {bar,baz}. */
47114a9d6eSIan Rogers ids2 = ids__new();
48114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
4980be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
5080be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
51114a9d6eSIan Rogers
52114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
53114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
54114a9d6eSIan Rogers
55114a9d6eSIan Rogers ids__free(ids1);
56114a9d6eSIan Rogers
57114a9d6eSIan Rogers return 0;
58114a9d6eSIan Rogers }
59114a9d6eSIan Rogers
test(struct expr_parse_ctx * ctx,const char * e,double val2)60aecce63eSJiri Olsa static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
6107516736SAndi Kleen {
6207516736SAndi Kleen double val;
6307516736SAndi Kleen
64fa831fbbSIan Rogers if (expr__parse(&val, ctx, e))
6507516736SAndi Kleen TEST_ASSERT_VAL("parse test failed", 0);
6607516736SAndi Kleen TEST_ASSERT_VAL("unexpected value", val == val2);
6707516736SAndi Kleen return 0;
6807516736SAndi Kleen }
6907516736SAndi Kleen
test__expr(struct test_suite * t __maybe_unused,int subtest __maybe_unused)7033f44bfdSIan Rogers static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
7107516736SAndi Kleen {
72070b3b5aSJiri Olsa struct expr_id_data *val_ptr;
7307516736SAndi Kleen const char *p;
74f0005f17SIan Rogers double val, num_cpus_online, num_cpus, num_cores, num_dies, num_packages;
75ded80bdaSIan Rogers int ret;
76cb94a02eSIan Rogers struct expr_parse_ctx *ctx;
776923397cSIan Rogers bool is_intel = false;
78d37e53e8SJames Clark char strcmp_cpuid_buf[256];
79f989dc00SJames Clark struct perf_pmu *pmu = perf_pmus__find_core_pmu();
80d37e53e8SJames Clark char *cpuid = perf_pmu__getcpuid(pmu);
81d37e53e8SJames Clark char *escaped_cpuid1, *escaped_cpuid2;
826923397cSIan Rogers
83d37e53e8SJames Clark TEST_ASSERT_VAL("get_cpuid", cpuid);
84d37e53e8SJames Clark is_intel = strstr(cpuid, "Intel") != NULL;
8507516736SAndi Kleen
86114a9d6eSIan Rogers TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
87114a9d6eSIan Rogers
88cb94a02eSIan Rogers ctx = expr__ctx_new();
89cb94a02eSIan Rogers TEST_ASSERT_VAL("expr__ctx_new", ctx);
90cb94a02eSIan Rogers expr__add_id_val(ctx, strdup("FOO"), 1);
91cb94a02eSIan Rogers expr__add_id_val(ctx, strdup("BAR"), 2);
9207516736SAndi Kleen
93cb94a02eSIan Rogers ret = test(ctx, "1+1", 2);
94cb94a02eSIan Rogers ret |= test(ctx, "FOO+BAR", 3);
95cb94a02eSIan Rogers ret |= test(ctx, "(BAR/2)%2", 1);
96cb94a02eSIan Rogers ret |= test(ctx, "1 - -4", 5);
97cb94a02eSIan Rogers ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
98cb94a02eSIan Rogers ret |= test(ctx, "1-1 | 1", 1);
99cb94a02eSIan Rogers ret |= test(ctx, "1-1 & 1", 0);
100cb94a02eSIan Rogers ret |= test(ctx, "min(1,2) + 1", 2);
101cb94a02eSIan Rogers ret |= test(ctx, "max(1,2) + 1", 3);
102cb94a02eSIan Rogers ret |= test(ctx, "1+1 if 3*4 else 0", 2);
1034b65fc7bSIan Rogers ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100);
1044b65fc7bSIan Rogers ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200);
1054b65fc7bSIan Rogers ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100);
1064b65fc7bSIan Rogers ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300);
107cb94a02eSIan Rogers ret |= test(ctx, "1.1 + 2.1", 3.2);
108cb94a02eSIan Rogers ret |= test(ctx, ".1 + 2.", 2.1);
109cb94a02eSIan Rogers ret |= test(ctx, "d_ratio(1, 2)", 0.5);
110cb94a02eSIan Rogers ret |= test(ctx, "d_ratio(2.5, 0)", 0);
111cb94a02eSIan Rogers ret |= test(ctx, "1.1 < 2.2", 1);
112cb94a02eSIan Rogers ret |= test(ctx, "2.2 > 1.1", 1);
113cb94a02eSIan Rogers ret |= test(ctx, "1.1 < 1.1", 0);
114cb94a02eSIan Rogers ret |= test(ctx, "2.2 > 2.2", 0);
115cb94a02eSIan Rogers ret |= test(ctx, "2.2 < 1.1", 0);
116cb94a02eSIan Rogers ret |= test(ctx, "1.1 > 2.2", 0);
117e5287e6dSIan Rogers ret |= test(ctx, "1.1e10 < 1.1e100", 1);
118e5287e6dSIan Rogers ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
11907516736SAndi Kleen
120cb94a02eSIan Rogers if (ret) {
121cb94a02eSIan Rogers expr__ctx_free(ctx);
12207516736SAndi Kleen return ret;
123cb94a02eSIan Rogers }
12407516736SAndi Kleen
12507516736SAndi Kleen p = "FOO/0";
126fa831fbbSIan Rogers ret = expr__parse(&val, ctx, p);
1272a939c86SIan Rogers TEST_ASSERT_VAL("division by zero", ret == 0);
1282a939c86SIan Rogers TEST_ASSERT_VAL("division by zero", isnan(val));
12907516736SAndi Kleen
13007516736SAndi Kleen p = "BAR/";
131fa831fbbSIan Rogers ret = expr__parse(&val, ctx, p);
132d942815aSJiri Olsa TEST_ASSERT_VAL("missing operand", ret == -1);
13307516736SAndi Kleen
134cb94a02eSIan Rogers expr__ctx_clear(ctx);
1357e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids",
1367e06a5e3SIan Rogers expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
137fa831fbbSIan Rogers ctx) == 0);
1387e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
139c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr));
140c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr));
141c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr));
142f97a8991SChangbin Du
143cb94a02eSIan Rogers expr__ctx_clear(ctx);
1441a6abddeSIan Rogers ctx->sctx.runtime = 3;
1457e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids",
1467e06a5e3SIan Rogers expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
147fa831fbbSIan Rogers NULL, ctx) == 0);
1487e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
149c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr));
150c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr));
1519022608eSKajol Jain
152604ce2f0SIan Rogers expr__ctx_clear(ctx);
153604ce2f0SIan Rogers TEST_ASSERT_VAL("find ids",
154604ce2f0SIan Rogers expr__find_ids("dash\\-event1 - dash\\-event2",
155604ce2f0SIan Rogers NULL, ctx) == 0);
156604ce2f0SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
157c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr));
158c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr));
159604ce2f0SIan Rogers
160a8e4e880SIan Rogers /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
16109b73fe9SIan Rogers {
162207f7df7SIan Rogers bool smton = smt_on();
163f0c4b97aSIan Rogers bool corewide = core_wide(/*system_wide=*/false,
164207f7df7SIan Rogers /*user_requested_cpus=*/false);
16509b73fe9SIan Rogers
166a8e4e880SIan Rogers expr__ctx_clear(ctx);
167a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids",
168a8e4e880SIan Rogers expr__find_ids("EVENT1 if #smt_on else EVENT2",
169fa831fbbSIan Rogers NULL, ctx) == 0);
170a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
171a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
17209b73fe9SIan Rogers smton ? "EVENT1" : "EVENT2",
173c302378bSEduard Zingerman &val_ptr));
174f0c4b97aSIan Rogers
175f0c4b97aSIan Rogers expr__ctx_clear(ctx);
176f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids",
177f0c4b97aSIan Rogers expr__find_ids("EVENT1 if #core_wide else EVENT2",
178f0c4b97aSIan Rogers NULL, ctx) == 0);
179f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
180f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
181f0c4b97aSIan Rogers corewide ? "EVENT1" : "EVENT2",
182c302378bSEduard Zingerman &val_ptr));
183f0c4b97aSIan Rogers
18409b73fe9SIan Rogers }
18594886961SIan Rogers /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
18694886961SIan Rogers expr__ctx_clear(ctx);
18794886961SIan Rogers TEST_ASSERT_VAL("find ids",
18894886961SIan Rogers expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
189fa831fbbSIan Rogers NULL, ctx) == 0);
19094886961SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
19194886961SIan Rogers
1926f765bbbSIan Rogers /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
1936f765bbbSIan Rogers expr__ctx_clear(ctx);
1946f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
1956f765bbbSIan Rogers expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
1966f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
1976f765bbbSIan Rogers expr__ctx_clear(ctx);
1986f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
1996f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
2006f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2016f765bbbSIan Rogers expr__ctx_clear(ctx);
2026f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2036f765bbbSIan Rogers expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
2046f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2056f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2066f765bbbSIan Rogers expr__ctx_clear(ctx);
2076f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2086f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
2096f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2106f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2116f765bbbSIan Rogers
2126f765bbbSIan Rogers /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
2136f765bbbSIan Rogers expr__ctx_clear(ctx);
2146f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2156f765bbbSIan Rogers expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
2166f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2176f765bbbSIan Rogers expr__ctx_clear(ctx);
2186f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2196f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
2206f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2216f765bbbSIan Rogers expr__ctx_clear(ctx);
2226f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2236f765bbbSIan Rogers expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
2246f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2256f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2266f765bbbSIan Rogers expr__ctx_clear(ctx);
2276f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2286f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
2296f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2306f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2316f765bbbSIan Rogers
232fdf1e29bSIan Rogers /* Test toplogy constants appear well ordered. */
233fdf1e29bSIan Rogers expr__ctx_clear(ctx);
234f0005f17SIan Rogers TEST_ASSERT_VAL("#num_cpus_online",
235f0005f17SIan Rogers expr__parse(&num_cpus_online, ctx, "#num_cpus_online") == 0);
236fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
237f0005f17SIan Rogers TEST_ASSERT_VAL("#num_cpus >= #num_cpus_online", num_cpus >= num_cpus_online);
238fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
239fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
240fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
241fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
242fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
2436c481031SThomas Richter
2446c481031SThomas Richter if (num_dies) // Some platforms do not have CPU die support, for example s390
245fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
246fdf1e29bSIan Rogers
2476923397cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq", expr__parse(&val, ctx, "#system_tsc_freq") == 0);
2486923397cSIan Rogers if (is_intel)
2496923397cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
2506923397cSIan Rogers else
2516923397cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
2526923397cSIan Rogers
2539aba0adaSIan Rogers /*
2549aba0adaSIan Rogers * Source count returns the number of events aggregating in a leader
2559aba0adaSIan Rogers * event including the leader. Check parsing yields an id.
2569aba0adaSIan Rogers */
2579aba0adaSIan Rogers expr__ctx_clear(ctx);
2589aba0adaSIan Rogers TEST_ASSERT_VAL("source count",
2599aba0adaSIan Rogers expr__find_ids("source_count(EVENT1)",
2609aba0adaSIan Rogers NULL, ctx) == 0);
2619aba0adaSIan Rogers TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
262c302378bSEduard Zingerman TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2639aba0adaSIan Rogers
264d37e53e8SJames Clark
265d37e53e8SJames Clark /* Test no cpuid match */
266d37e53e8SJames Clark ret = test(ctx, "strcmp_cpuid_str(0x0)", 0);
267d37e53e8SJames Clark
268d37e53e8SJames Clark /*
269d37e53e8SJames Clark * Test cpuid match with current cpuid. Special chars have to be
270d37e53e8SJames Clark * escaped.
271d37e53e8SJames Clark */
272d37e53e8SJames Clark escaped_cpuid1 = strreplace_chars('-', cpuid, "\\-");
273d37e53e8SJames Clark free(cpuid);
274d37e53e8SJames Clark escaped_cpuid2 = strreplace_chars(',', escaped_cpuid1, "\\,");
275d37e53e8SJames Clark free(escaped_cpuid1);
276d37e53e8SJames Clark escaped_cpuid1 = strreplace_chars('=', escaped_cpuid2, "\\=");
277d37e53e8SJames Clark free(escaped_cpuid2);
278d37e53e8SJames Clark scnprintf(strcmp_cpuid_buf, sizeof(strcmp_cpuid_buf),
279d37e53e8SJames Clark "strcmp_cpuid_str(%s)", escaped_cpuid1);
280d37e53e8SJames Clark free(escaped_cpuid1);
281d37e53e8SJames Clark ret |= test(ctx, strcmp_cpuid_buf, 1);
282d37e53e8SJames Clark
2834a4a9bf9SIan Rogers /* has_event returns 1 when an event exists. */
2844a4a9bf9SIan Rogers expr__add_id_val(ctx, strdup("cycles"), 2);
285d37e53e8SJames Clark ret |= test(ctx, "has_event(cycles)", 1);
2864a4a9bf9SIan Rogers
287cb94a02eSIan Rogers expr__ctx_free(ctx);
28807516736SAndi Kleen
28907516736SAndi Kleen return 0;
29007516736SAndi Kleen }
291d68f0365SIan Rogers
292d68f0365SIan Rogers DEFINE_SUITE("Simple expression parser", expr);
293