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