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