1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdbool.h>
3 #include <assert.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "metricgroup.h"
8 #include "debug.h"
9 #include "evlist.h"
10 #include "expr.h"
11 #include "smt.h"
12 #include "tool_pmu.h"
13 #include <util/expr-bison.h>
14 #include <util/expr-flex.h>
15 #include "util/hashmap.h"
16 #include "util/header.h"
17 #include "util/pmu.h"
18 #include <perf/cpumap.h>
19 #include <linux/err.h>
20 #include <linux/kernel.h>
21 #include <linux/zalloc.h>
22 #include <ctype.h>
23 #include <math.h>
24 
25 struct expr_id_data {
26 	union {
27 		struct {
28 			double val;
29 			int source_count;
30 		} val;
31 		struct {
32 			double val;
33 			const char *metric_name;
34 			const char *metric_expr;
35 		} ref;
36 	};
37 
38 	enum {
39 		/* Holding a double value. */
40 		EXPR_ID_DATA__VALUE,
41 		/* Reference to another metric. */
42 		EXPR_ID_DATA__REF,
43 		/* A reference but the value has been computed. */
44 		EXPR_ID_DATA__REF_VALUE,
45 	} kind;
46 };
47 
key_hash(long key,void * ctx __maybe_unused)48 static size_t key_hash(long key, void *ctx __maybe_unused)
49 {
50 	const char *str = (const char *)key;
51 	size_t hash = 0;
52 
53 	while (*str != '\0') {
54 		hash *= 31;
55 		hash += *str;
56 		str++;
57 	}
58 	return hash;
59 }
60 
key_equal(long key1,long key2,void * ctx __maybe_unused)61 static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
62 {
63 	return !strcmp((const char *)key1, (const char *)key2);
64 }
65 
ids__new(void)66 struct hashmap *ids__new(void)
67 {
68 	struct hashmap *hash;
69 
70 	hash = hashmap__new(key_hash, key_equal, NULL);
71 	if (IS_ERR(hash))
72 		return NULL;
73 	return hash;
74 }
75 
ids__free(struct hashmap * ids)76 void ids__free(struct hashmap *ids)
77 {
78 	struct hashmap_entry *cur;
79 	size_t bkt;
80 
81 	if (ids == NULL)
82 		return;
83 
84 	hashmap__for_each_entry(ids, cur, bkt) {
85 		zfree(&cur->pkey);
86 		zfree(&cur->pvalue);
87 	}
88 
89 	hashmap__free(ids);
90 }
91 
ids__insert(struct hashmap * ids,const char * id)92 int ids__insert(struct hashmap *ids, const char *id)
93 {
94 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
95 	char *old_key = NULL;
96 	int ret;
97 
98 	ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
99 	if (ret)
100 		free(data_ptr);
101 	free(old_key);
102 	free(old_data);
103 	return ret;
104 }
105 
ids__union(struct hashmap * ids1,struct hashmap * ids2)106 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
107 {
108 	size_t bkt;
109 	struct hashmap_entry *cur;
110 	int ret;
111 	struct expr_id_data *old_data = NULL;
112 	char *old_key = NULL;
113 
114 	if (!ids1)
115 		return ids2;
116 
117 	if (!ids2)
118 		return ids1;
119 
120 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
121 		struct hashmap *tmp = ids1;
122 
123 		ids1 = ids2;
124 		ids2 = tmp;
125 	}
126 	hashmap__for_each_entry(ids2, cur, bkt) {
127 		ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
128 		free(old_key);
129 		free(old_data);
130 
131 		if (ret) {
132 			hashmap__free(ids1);
133 			hashmap__free(ids2);
134 			return NULL;
135 		}
136 	}
137 	hashmap__free(ids2);
138 	return ids1;
139 }
140 
141 /* Caller must make sure id is allocated */
expr__add_id(struct expr_parse_ctx * ctx,const char * id)142 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
143 {
144 	return ids__insert(ctx->ids, id);
145 }
146 
147 /* Caller must make sure id is allocated */
expr__add_id_val(struct expr_parse_ctx * ctx,const char * id,double val)148 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
149 {
150 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
151 }
152 
153 /* Caller must make sure id is allocated */
expr__add_id_val_source_count(struct expr_parse_ctx * ctx,const char * id,double val,int source_count)154 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
155 				  double val, int source_count)
156 {
157 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
158 	char *old_key = NULL;
159 	int ret;
160 
161 	data_ptr = malloc(sizeof(*data_ptr));
162 	if (!data_ptr)
163 		return -ENOMEM;
164 	data_ptr->val.val = val;
165 	data_ptr->val.source_count = source_count;
166 	data_ptr->kind = EXPR_ID_DATA__VALUE;
167 
168 	ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
169 	if (ret)
170 		free(data_ptr);
171 	free(old_key);
172 	free(old_data);
173 	return ret;
174 }
175 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)176 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
177 {
178 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
179 	char *old_key = NULL;
180 	char *name;
181 	int ret;
182 
183 	data_ptr = zalloc(sizeof(*data_ptr));
184 	if (!data_ptr)
185 		return -ENOMEM;
186 
187 	name = strdup(ref->metric_name);
188 	if (!name) {
189 		free(data_ptr);
190 		return -ENOMEM;
191 	}
192 
193 	/*
194 	 * Intentionally passing just const char pointers,
195 	 * originally from 'struct pmu_event' object.
196 	 * We don't need to change them, so there's no
197 	 * need to create our own copy.
198 	 */
199 	data_ptr->ref.metric_name = ref->metric_name;
200 	data_ptr->ref.metric_expr = ref->metric_expr;
201 	data_ptr->kind = EXPR_ID_DATA__REF;
202 
203 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
204 	if (ret)
205 		free(data_ptr);
206 
207 	pr_debug2("adding ref metric %s: %s\n",
208 		  ref->metric_name, ref->metric_expr);
209 
210 	free(old_key);
211 	free(old_data);
212 	return ret;
213 }
214 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)215 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
216 		 struct expr_id_data **data)
217 {
218 	if (!ctx || !id)
219 		return -1;
220 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
221 }
222 
expr__subset_of_ids(struct expr_parse_ctx * haystack,struct expr_parse_ctx * needles)223 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
224 			 struct expr_parse_ctx *needles)
225 {
226 	struct hashmap_entry *cur;
227 	size_t bkt;
228 	struct expr_id_data *data;
229 
230 	hashmap__for_each_entry(needles->ids, cur, bkt) {
231 		if (expr__get_id(haystack, cur->pkey, &data))
232 			return false;
233 	}
234 	return true;
235 }
236 
237 
expr__resolve_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** datap)238 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
239 		     struct expr_id_data **datap)
240 {
241 	struct expr_id_data *data;
242 
243 	if (expr__get_id(ctx, id, datap) || !*datap) {
244 		pr_debug("%s not found\n", id);
245 		return -1;
246 	}
247 
248 	data = *datap;
249 
250 	switch (data->kind) {
251 	case EXPR_ID_DATA__VALUE:
252 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
253 		break;
254 	case EXPR_ID_DATA__REF:
255 		pr_debug2("lookup(%s): ref metric name %s\n", id,
256 			data->ref.metric_name);
257 		pr_debug("processing metric: %s ENTRY\n", id);
258 		data->kind = EXPR_ID_DATA__REF_VALUE;
259 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
260 			pr_debug("%s failed to count\n", id);
261 			return -1;
262 		}
263 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
264 		break;
265 	case EXPR_ID_DATA__REF_VALUE:
266 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
267 			data->ref.val, data->ref.metric_name);
268 		break;
269 	default:
270 		assert(0);  /* Unreachable. */
271 	}
272 
273 	return 0;
274 }
275 
expr__del_id(struct expr_parse_ctx * ctx,const char * id)276 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
277 {
278 	struct expr_id_data *old_val = NULL;
279 	char *old_key = NULL;
280 
281 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
282 	free(old_key);
283 	free(old_val);
284 }
285 
expr__ctx_new(void)286 struct expr_parse_ctx *expr__ctx_new(void)
287 {
288 	struct expr_parse_ctx *ctx;
289 
290 	ctx = calloc(1, sizeof(struct expr_parse_ctx));
291 	if (!ctx)
292 		return NULL;
293 
294 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
295 	if (IS_ERR(ctx->ids)) {
296 		free(ctx);
297 		return NULL;
298 	}
299 
300 	return ctx;
301 }
302 
expr__ctx_clear(struct expr_parse_ctx * ctx)303 void expr__ctx_clear(struct expr_parse_ctx *ctx)
304 {
305 	struct hashmap_entry *cur;
306 	size_t bkt;
307 
308 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
309 		zfree(&cur->pkey);
310 		zfree(&cur->pvalue);
311 	}
312 	hashmap__clear(ctx->ids);
313 }
314 
expr__ctx_free(struct expr_parse_ctx * ctx)315 void expr__ctx_free(struct expr_parse_ctx *ctx)
316 {
317 	struct hashmap_entry *cur;
318 	size_t bkt;
319 
320 	if (!ctx)
321 		return;
322 
323 	zfree(&ctx->sctx.user_requested_cpu_list);
324 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
325 		zfree(&cur->pkey);
326 		zfree(&cur->pvalue);
327 	}
328 	hashmap__free(ctx->ids);
329 	free(ctx);
330 }
331 
332 static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)333 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
334 	      bool compute_ids)
335 {
336 	YY_BUFFER_STATE buffer;
337 	void *scanner;
338 	int ret;
339 
340 	pr_debug2("parsing metric: %s\n", expr);
341 
342 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
343 	if (ret)
344 		return ret;
345 
346 	buffer = expr__scan_string(expr, scanner);
347 
348 #ifdef PARSER_DEBUG
349 	expr_debug = 1;
350 	expr_set_debug(1, scanner);
351 #endif
352 
353 	ret = expr_parse(val, ctx, compute_ids, scanner);
354 
355 	expr__flush_buffer(buffer, scanner);
356 	expr__delete_buffer(buffer, scanner);
357 	expr_lex_destroy(scanner);
358 	return ret;
359 }
360 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)361 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
362 		const char *expr)
363 {
364 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
365 }
366 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)367 int expr__find_ids(const char *expr, const char *one,
368 		   struct expr_parse_ctx *ctx)
369 {
370 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
371 
372 	if (one)
373 		expr__del_id(ctx, one);
374 
375 	return ret;
376 }
377 
expr_id_data__value(const struct expr_id_data * data)378 double expr_id_data__value(const struct expr_id_data *data)
379 {
380 	if (data->kind == EXPR_ID_DATA__VALUE)
381 		return data->val.val;
382 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
383 	return data->ref.val;
384 }
385 
expr_id_data__source_count(const struct expr_id_data * data)386 double expr_id_data__source_count(const struct expr_id_data *data)
387 {
388 	assert(data->kind == EXPR_ID_DATA__VALUE);
389 	return data->val.source_count;
390 }
391 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)392 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
393 {
394 	double result = NAN;
395 	enum tool_pmu_event ev = tool_pmu__str_to_event(literal + 1);
396 
397 	if (ev != TOOL_PMU__EVENT_NONE) {
398 		u64 count;
399 
400 		if (tool_pmu__read_event(ev, &count))
401 			result = count;
402 		else
403 			pr_err("Failure to read '%s'", literal);
404 
405 	} else if (!strcmp("#core_wide", literal)) {
406 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
407 			? 1.0 : 0.0;
408 	} else {
409 		pr_err("Unrecognized literal '%s'", literal);
410 	}
411 
412 	pr_debug2("literal: %s = %f\n", literal, result);
413 	return result;
414 }
415 
416 /* Does the event 'id' parse? Determine via ctx->ids if possible. */
expr__has_event(const struct expr_parse_ctx * ctx,bool compute_ids,const char * id)417 double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
418 {
419 	struct evlist *tmp;
420 	double ret;
421 
422 	if (hashmap__find(ctx->ids, id, /*value=*/NULL))
423 		return 1.0;
424 
425 	if (!compute_ids)
426 		return 0.0;
427 
428 	tmp = evlist__new();
429 	if (!tmp)
430 		return NAN;
431 
432 	if (strchr(id, '@')) {
433 		char *tmp_id, *p;
434 
435 		tmp_id = strdup(id);
436 		if (!tmp_id) {
437 			ret = NAN;
438 			goto out;
439 		}
440 		p = strchr(tmp_id, '@');
441 		*p = '/';
442 		p = strrchr(tmp_id, '@');
443 		*p = '/';
444 		ret = parse_event(tmp, tmp_id) ? 0 : 1;
445 		free(tmp_id);
446 	} else {
447 		ret = parse_event(tmp, id) ? 0 : 1;
448 	}
449 out:
450 	evlist__delete(tmp);
451 	return ret;
452 }
453 
expr__strcmp_cpuid_str(const struct expr_parse_ctx * ctx __maybe_unused,bool compute_ids __maybe_unused,const char * test_id)454 double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
455 		       bool compute_ids __maybe_unused, const char *test_id)
456 {
457 	double ret;
458 	struct perf_cpu cpu = {-1};
459 	char *cpuid = get_cpuid_allow_env_override(cpu);
460 
461 	if (!cpuid)
462 		return NAN;
463 
464 	ret = !strcmp_cpuid_str(test_id, cpuid);
465 
466 	free(cpuid);
467 	return ret;
468 }
469