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"
9494c403fSIan Rogers #include <perf/cpumap.h>
106923397cSIan Rogers #include <math.h>
1107516736SAndi Kleen #include <stdlib.h>
128520a98dSArnaldo Carvalho de Melo #include <string.h>
13a1ebf771SJames Clark #include <string2.h>
14d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
1507516736SAndi Kleen
test_ids_union(void)16114a9d6eSIan Rogers static int test_ids_union(void)
17114a9d6eSIan Rogers {
18114a9d6eSIan Rogers struct hashmap *ids1, *ids2;
19114a9d6eSIan Rogers
20114a9d6eSIan Rogers /* Empty union. */
21114a9d6eSIan Rogers ids1 = ids__new();
22114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids1);
23114a9d6eSIan Rogers ids2 = ids__new();
24114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
25114a9d6eSIan Rogers
26114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
27114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 0);
28114a9d6eSIan Rogers
29114a9d6eSIan Rogers /* Union {foo, bar} against {}. */
30114a9d6eSIan Rogers ids2 = ids__new();
31114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
32114a9d6eSIan Rogers
3380be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("foo")), 0);
3480be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids1, strdup("bar")), 0);
35114a9d6eSIan Rogers
36114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
37114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
38114a9d6eSIan Rogers
39114a9d6eSIan Rogers /* Union {foo, bar} against {foo}. */
40114a9d6eSIan Rogers ids2 = ids__new();
41114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
4280be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("foo")), 0);
43114a9d6eSIan Rogers
44114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
45114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 2);
46114a9d6eSIan Rogers
47114a9d6eSIan Rogers /* Union {foo, bar} against {bar,baz}. */
48114a9d6eSIan Rogers ids2 = ids__new();
49114a9d6eSIan Rogers TEST_ASSERT_VAL("ids__new", ids2);
5080be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("bar")), 0);
5180be6434SIan Rogers TEST_ASSERT_EQUAL("ids__insert", ids__insert(ids2, strdup("baz")), 0);
52114a9d6eSIan Rogers
53114a9d6eSIan Rogers ids1 = ids__union(ids1, ids2);
54114a9d6eSIan Rogers TEST_ASSERT_EQUAL("union", (int)hashmap__size(ids1), 3);
55114a9d6eSIan Rogers
56114a9d6eSIan Rogers ids__free(ids1);
57114a9d6eSIan Rogers
58114a9d6eSIan Rogers return 0;
59114a9d6eSIan Rogers }
60114a9d6eSIan Rogers
test(struct expr_parse_ctx * ctx,const char * e,double val2)61aecce63eSJiri Olsa static int test(struct expr_parse_ctx *ctx, const char *e, double val2)
6207516736SAndi Kleen {
6307516736SAndi Kleen double val;
6407516736SAndi Kleen
65fa831fbbSIan Rogers if (expr__parse(&val, ctx, e))
6607516736SAndi Kleen TEST_ASSERT_VAL("parse test failed", 0);
6707516736SAndi Kleen TEST_ASSERT_VAL("unexpected value", val == val2);
6807516736SAndi Kleen return 0;
6907516736SAndi Kleen }
7007516736SAndi Kleen
test__expr(struct test_suite * t __maybe_unused,int subtest __maybe_unused)7133f44bfdSIan Rogers static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
7207516736SAndi Kleen {
73070b3b5aSJiri Olsa struct expr_id_data *val_ptr;
7407516736SAndi Kleen const char *p;
75f0005f17SIan Rogers double val, num_cpus_online, num_cpus, num_cores, num_dies, num_packages;
76ded80bdaSIan Rogers int ret;
77cb94a02eSIan Rogers struct expr_parse_ctx *ctx;
78a1ebf771SJames Clark char strcmp_cpuid_buf[256];
79494c403fSIan Rogers struct perf_cpu cpu = {-1};
80494c403fSIan Rogers char *cpuid = get_cpuid_allow_env_override(cpu);
81a1ebf771SJames Clark char *escaped_cpuid1, *escaped_cpuid2;
826923397cSIan Rogers
83a1ebf771SJames Clark TEST_ASSERT_VAL("get_cpuid", cpuid);
8407516736SAndi Kleen
85114a9d6eSIan Rogers TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
86114a9d6eSIan Rogers
87cb94a02eSIan Rogers ctx = expr__ctx_new();
88cb94a02eSIan Rogers TEST_ASSERT_VAL("expr__ctx_new", ctx);
89cb94a02eSIan Rogers expr__add_id_val(ctx, strdup("FOO"), 1);
90cb94a02eSIan Rogers expr__add_id_val(ctx, strdup("BAR"), 2);
9107516736SAndi Kleen
92cb94a02eSIan Rogers ret = test(ctx, "1+1", 2);
93cb94a02eSIan Rogers ret |= test(ctx, "FOO+BAR", 3);
94cb94a02eSIan Rogers ret |= test(ctx, "(BAR/2)%2", 1);
95cb94a02eSIan Rogers ret |= test(ctx, "1 - -4", 5);
96cb94a02eSIan Rogers ret |= test(ctx, "(FOO-1)*2 + (BAR/2)%2 - -4", 5);
97cb94a02eSIan Rogers ret |= test(ctx, "1-1 | 1", 1);
98cb94a02eSIan Rogers ret |= test(ctx, "1-1 & 1", 0);
99cb94a02eSIan Rogers ret |= test(ctx, "min(1,2) + 1", 2);
100cb94a02eSIan Rogers ret |= test(ctx, "max(1,2) + 1", 3);
101cb94a02eSIan Rogers ret |= test(ctx, "1+1 if 3*4 else 0", 2);
1024b65fc7bSIan Rogers ret |= test(ctx, "100 if 1 else 200 if 1 else 300", 100);
1034b65fc7bSIan Rogers ret |= test(ctx, "100 if 0 else 200 if 1 else 300", 200);
1044b65fc7bSIan Rogers ret |= test(ctx, "100 if 1 else 200 if 0 else 300", 100);
1054b65fc7bSIan Rogers ret |= test(ctx, "100 if 0 else 200 if 0 else 300", 300);
106cb94a02eSIan Rogers ret |= test(ctx, "1.1 + 2.1", 3.2);
107cb94a02eSIan Rogers ret |= test(ctx, ".1 + 2.", 2.1);
108cb94a02eSIan Rogers ret |= test(ctx, "d_ratio(1, 2)", 0.5);
109cb94a02eSIan Rogers ret |= test(ctx, "d_ratio(2.5, 0)", 0);
110cb94a02eSIan Rogers ret |= test(ctx, "1.1 < 2.2", 1);
111cb94a02eSIan Rogers ret |= test(ctx, "2.2 > 1.1", 1);
112cb94a02eSIan Rogers ret |= test(ctx, "1.1 < 1.1", 0);
113cb94a02eSIan Rogers ret |= test(ctx, "2.2 > 2.2", 0);
114cb94a02eSIan Rogers ret |= test(ctx, "2.2 < 1.1", 0);
115cb94a02eSIan Rogers ret |= test(ctx, "1.1 > 2.2", 0);
116e5287e6dSIan Rogers ret |= test(ctx, "1.1e10 < 1.1e100", 1);
117e5287e6dSIan Rogers ret |= test(ctx, "1.1e2 > 1.1e-2", 1);
11807516736SAndi Kleen
119cb94a02eSIan Rogers if (ret) {
120cb94a02eSIan Rogers expr__ctx_free(ctx);
12107516736SAndi Kleen return ret;
122cb94a02eSIan Rogers }
12307516736SAndi Kleen
12407516736SAndi Kleen p = "FOO/0";
125fa831fbbSIan Rogers ret = expr__parse(&val, ctx, p);
1262a939c86SIan Rogers TEST_ASSERT_VAL("division by zero", ret == 0);
1272a939c86SIan Rogers TEST_ASSERT_VAL("division by zero", isnan(val));
12807516736SAndi Kleen
12907516736SAndi Kleen p = "BAR/";
130fa831fbbSIan Rogers ret = expr__parse(&val, ctx, p);
131d942815aSJiri Olsa TEST_ASSERT_VAL("missing operand", ret == -1);
13207516736SAndi Kleen
133cb94a02eSIan Rogers expr__ctx_clear(ctx);
1347e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids",
1357e06a5e3SIan Rogers expr__find_ids("FOO + BAR + BAZ + BOZO", "FOO",
136fa831fbbSIan Rogers ctx) == 0);
1377e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 3);
138c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAR", &val_ptr));
139c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BAZ", &val_ptr));
140c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "BOZO", &val_ptr));
141f97a8991SChangbin Du
142cb94a02eSIan Rogers expr__ctx_clear(ctx);
1431a6abddeSIan Rogers ctx->sctx.runtime = 3;
1447e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids",
1457e06a5e3SIan Rogers expr__find_ids("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@",
146fa831fbbSIan Rogers NULL, ctx) == 0);
1477e06a5e3SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
148c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1,param=3@", &val_ptr));
149c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT2,param=3@", &val_ptr));
1509022608eSKajol Jain
151604ce2f0SIan Rogers expr__ctx_clear(ctx);
152604ce2f0SIan Rogers TEST_ASSERT_VAL("find ids",
153604ce2f0SIan Rogers expr__find_ids("dash\\-event1 - dash\\-event2",
154604ce2f0SIan Rogers NULL, ctx) == 0);
155604ce2f0SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 2);
156c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event1", &val_ptr));
157c302378bSEduard Zingerman TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "dash-event2", &val_ptr));
158604ce2f0SIan Rogers
159a8e4e880SIan Rogers /* Only EVENT1 or EVENT2 need be measured depending on the value of smt_on. */
16009b73fe9SIan Rogers {
161207f7df7SIan Rogers bool smton = smt_on();
162f0c4b97aSIan Rogers bool corewide = core_wide(/*system_wide=*/false,
163207f7df7SIan Rogers /*user_requested_cpus=*/false);
16409b73fe9SIan Rogers
165a8e4e880SIan Rogers expr__ctx_clear(ctx);
166a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids",
167a8e4e880SIan Rogers expr__find_ids("EVENT1 if #smt_on else EVENT2",
168fa831fbbSIan Rogers NULL, ctx) == 0);
169a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
170a8e4e880SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
17109b73fe9SIan Rogers smton ? "EVENT1" : "EVENT2",
172c302378bSEduard Zingerman &val_ptr));
173f0c4b97aSIan Rogers
174f0c4b97aSIan Rogers expr__ctx_clear(ctx);
175f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids",
176f0c4b97aSIan Rogers expr__find_ids("EVENT1 if #core_wide else EVENT2",
177f0c4b97aSIan Rogers NULL, ctx) == 0);
178f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
179f0c4b97aSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids,
180f0c4b97aSIan Rogers corewide ? "EVENT1" : "EVENT2",
181c302378bSEduard Zingerman &val_ptr));
182f0c4b97aSIan Rogers
18309b73fe9SIan Rogers }
18494886961SIan Rogers /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
18594886961SIan Rogers expr__ctx_clear(ctx);
18694886961SIan Rogers TEST_ASSERT_VAL("find ids",
18794886961SIan Rogers expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0",
188fa831fbbSIan Rogers NULL, ctx) == 0);
18994886961SIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
19094886961SIan Rogers
1916f765bbbSIan Rogers /* The expression is a constant 0.0 without needing to evaluate EVENT1. */
1926f765bbbSIan Rogers expr__ctx_clear(ctx);
1936f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
1946f765bbbSIan Rogers expr__find_ids("0 & EVENT1 > 0", NULL, ctx) == 0);
1956f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
1966f765bbbSIan Rogers expr__ctx_clear(ctx);
1976f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
1986f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 & 0", NULL, ctx) == 0);
1996f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2006f765bbbSIan Rogers expr__ctx_clear(ctx);
2016f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2026f765bbbSIan Rogers expr__find_ids("1 & EVENT1 > 0", NULL, ctx) == 0);
2036f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2046f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2056f765bbbSIan Rogers expr__ctx_clear(ctx);
2066f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2076f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 & 1", NULL, ctx) == 0);
2086f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2096f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2106f765bbbSIan Rogers
2116f765bbbSIan Rogers /* The expression is a constant 1.0 without needing to evaluate EVENT1. */
2126f765bbbSIan Rogers expr__ctx_clear(ctx);
2136f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2146f765bbbSIan Rogers expr__find_ids("1 | EVENT1 > 0", NULL, ctx) == 0);
2156f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2166f765bbbSIan Rogers expr__ctx_clear(ctx);
2176f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2186f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 | 1", NULL, ctx) == 0);
2196f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
2206f765bbbSIan Rogers expr__ctx_clear(ctx);
2216f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2226f765bbbSIan Rogers expr__find_ids("0 | EVENT1 > 0", NULL, ctx) == 0);
2236f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2246f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2256f765bbbSIan Rogers expr__ctx_clear(ctx);
2266f765bbbSIan Rogers TEST_ASSERT_VAL("find ids",
2276f765bbbSIan Rogers expr__find_ids("EVENT1 > 0 | 0", NULL, ctx) == 0);
2286f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 1);
2296f765bbbSIan Rogers TEST_ASSERT_VAL("find ids", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2306f765bbbSIan Rogers
231fdf1e29bSIan Rogers /* Test toplogy constants appear well ordered. */
232fdf1e29bSIan Rogers expr__ctx_clear(ctx);
233f0005f17SIan Rogers TEST_ASSERT_VAL("#num_cpus_online",
234f0005f17SIan Rogers expr__parse(&num_cpus_online, ctx, "#num_cpus_online") == 0);
235fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
236f0005f17SIan Rogers TEST_ASSERT_VAL("#num_cpus >= #num_cpus_online", num_cpus >= num_cpus_online);
237fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
238fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
239fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
240fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
241fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
2426c481031SThomas Richter
2436c481031SThomas Richter if (num_dies) // Some platforms do not have CPU die support, for example s390
244fdf1e29bSIan Rogers TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
245fdf1e29bSIan Rogers
246*a93a620cSIan Rogers
247*a93a620cSIan Rogers if (expr__parse(&val, ctx, "#system_tsc_freq") == 0) {
248*a93a620cSIan Rogers bool is_intel = strstr(cpuid, "Intel") != NULL;
249*a93a620cSIan Rogers
2506923397cSIan Rogers if (is_intel)
2516923397cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq > 0", val > 0);
2526923397cSIan Rogers else
2536923397cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq == 0", fpclassify(val) == FP_ZERO);
254*a93a620cSIan Rogers } else {
255*a93a620cSIan Rogers #if defined(__i386__) || defined(__x86_64__)
256*a93a620cSIan Rogers TEST_ASSERT_VAL("#system_tsc_freq unsupported", 0);
257*a93a620cSIan Rogers #endif
258*a93a620cSIan Rogers }
2599aba0adaSIan Rogers /*
2609aba0adaSIan Rogers * Source count returns the number of events aggregating in a leader
2619aba0adaSIan Rogers * event including the leader. Check parsing yields an id.
2629aba0adaSIan Rogers */
2639aba0adaSIan Rogers expr__ctx_clear(ctx);
2649aba0adaSIan Rogers TEST_ASSERT_VAL("source count",
2659aba0adaSIan Rogers expr__find_ids("source_count(EVENT1)",
2669aba0adaSIan Rogers NULL, ctx) == 0);
2679aba0adaSIan Rogers TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
268c302378bSEduard Zingerman TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
2699aba0adaSIan Rogers
270a1ebf771SJames Clark
271a1ebf771SJames Clark /* Test no cpuid match */
272a1ebf771SJames Clark ret = test(ctx, "strcmp_cpuid_str(0x0)", 0);
273a1ebf771SJames Clark
274a1ebf771SJames Clark /*
275a1ebf771SJames Clark * Test cpuid match with current cpuid. Special chars have to be
276a1ebf771SJames Clark * escaped.
277a1ebf771SJames Clark */
278a1ebf771SJames Clark escaped_cpuid1 = strreplace_chars('-', cpuid, "\\-");
279a1ebf771SJames Clark free(cpuid);
280a1ebf771SJames Clark escaped_cpuid2 = strreplace_chars(',', escaped_cpuid1, "\\,");
281a1ebf771SJames Clark free(escaped_cpuid1);
282a1ebf771SJames Clark escaped_cpuid1 = strreplace_chars('=', escaped_cpuid2, "\\=");
283a1ebf771SJames Clark free(escaped_cpuid2);
284a1ebf771SJames Clark scnprintf(strcmp_cpuid_buf, sizeof(strcmp_cpuid_buf),
285a1ebf771SJames Clark "strcmp_cpuid_str(%s)", escaped_cpuid1);
286a1ebf771SJames Clark free(escaped_cpuid1);
287a1ebf771SJames Clark ret |= test(ctx, strcmp_cpuid_buf, 1);
288a1ebf771SJames Clark
2894a4a9bf9SIan Rogers /* has_event returns 1 when an event exists. */
2904a4a9bf9SIan Rogers expr__add_id_val(ctx, strdup("cycles"), 2);
291a1ebf771SJames Clark ret |= test(ctx, "has_event(cycles)", 1);
2924a4a9bf9SIan Rogers
293cb94a02eSIan Rogers expr__ctx_free(ctx);
29407516736SAndi Kleen
295d19a353cSJames Clark return ret;
29607516736SAndi Kleen }
297d68f0365SIan Rogers
298d68f0365SIan Rogers DEFINE_SUITE("Simple expression parser", expr);
299