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