xref: /linux/tools/perf/util/expr.c (revision f4f346c3465949ebba80c6cc52cd8d2eeaa545fd)
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 	} else if (old_data) {
172 		data_ptr->val.val += old_data->val.val;
173 		data_ptr->val.source_count += old_data->val.source_count;
174 	}
175 	free(old_key);
176 	free(old_data);
177 	return ret;
178 }
179 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)180 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
181 {
182 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
183 	char *old_key = NULL;
184 	char *name;
185 	int ret;
186 
187 	data_ptr = zalloc(sizeof(*data_ptr));
188 	if (!data_ptr)
189 		return -ENOMEM;
190 
191 	name = strdup(ref->metric_name);
192 	if (!name) {
193 		free(data_ptr);
194 		return -ENOMEM;
195 	}
196 
197 	/*
198 	 * Intentionally passing just const char pointers,
199 	 * originally from 'struct pmu_event' object.
200 	 * We don't need to change them, so there's no
201 	 * need to create our own copy.
202 	 */
203 	data_ptr->ref.metric_name = ref->metric_name;
204 	data_ptr->ref.metric_expr = ref->metric_expr;
205 	data_ptr->kind = EXPR_ID_DATA__REF;
206 
207 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
208 	if (ret)
209 		free(data_ptr);
210 
211 	pr_debug2("adding ref metric %s: %s\n",
212 		  ref->metric_name, ref->metric_expr);
213 
214 	free(old_key);
215 	free(old_data);
216 	return ret;
217 }
218 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)219 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
220 		 struct expr_id_data **data)
221 {
222 	if (!ctx || !id)
223 		return -1;
224 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
225 }
226 
expr__subset_of_ids(struct expr_parse_ctx * haystack,struct expr_parse_ctx * needles)227 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
228 			 struct expr_parse_ctx *needles)
229 {
230 	struct hashmap_entry *cur;
231 	size_t bkt;
232 	struct expr_id_data *data;
233 
234 	hashmap__for_each_entry(needles->ids, cur, bkt) {
235 		if (expr__get_id(haystack, cur->pkey, &data))
236 			return false;
237 	}
238 	return true;
239 }
240 
241 
expr__resolve_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** datap)242 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
243 		     struct expr_id_data **datap)
244 {
245 	struct expr_id_data *data;
246 
247 	if (expr__get_id(ctx, id, datap) || !*datap) {
248 		pr_debug("%s not found\n", id);
249 		return -1;
250 	}
251 
252 	data = *datap;
253 
254 	switch (data->kind) {
255 	case EXPR_ID_DATA__VALUE:
256 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
257 		break;
258 	case EXPR_ID_DATA__REF:
259 		pr_debug2("lookup(%s): ref metric name %s\n", id,
260 			data->ref.metric_name);
261 		pr_debug("processing metric: %s ENTRY\n", id);
262 		data->kind = EXPR_ID_DATA__REF_VALUE;
263 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
264 			pr_debug("%s failed to count\n", id);
265 			return -1;
266 		}
267 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
268 		break;
269 	case EXPR_ID_DATA__REF_VALUE:
270 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
271 			data->ref.val, data->ref.metric_name);
272 		break;
273 	default:
274 		assert(0);  /* Unreachable. */
275 	}
276 
277 	return 0;
278 }
279 
expr__del_id(struct expr_parse_ctx * ctx,const char * id)280 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
281 {
282 	struct expr_id_data *old_val = NULL;
283 	char *old_key = NULL;
284 
285 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
286 	free(old_key);
287 	free(old_val);
288 }
289 
expr__ctx_new(void)290 struct expr_parse_ctx *expr__ctx_new(void)
291 {
292 	struct expr_parse_ctx *ctx;
293 
294 	ctx = calloc(1, sizeof(struct expr_parse_ctx));
295 	if (!ctx)
296 		return NULL;
297 
298 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
299 	if (IS_ERR(ctx->ids)) {
300 		free(ctx);
301 		return NULL;
302 	}
303 
304 	return ctx;
305 }
306 
expr__ctx_clear(struct expr_parse_ctx * ctx)307 void expr__ctx_clear(struct expr_parse_ctx *ctx)
308 {
309 	struct hashmap_entry *cur;
310 	size_t bkt;
311 
312 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
313 		zfree(&cur->pkey);
314 		zfree(&cur->pvalue);
315 	}
316 	hashmap__clear(ctx->ids);
317 }
318 
expr__ctx_free(struct expr_parse_ctx * ctx)319 void expr__ctx_free(struct expr_parse_ctx *ctx)
320 {
321 	struct hashmap_entry *cur;
322 	size_t bkt;
323 
324 	if (!ctx)
325 		return;
326 
327 	zfree(&ctx->sctx.user_requested_cpu_list);
328 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
329 		zfree(&cur->pkey);
330 		zfree(&cur->pvalue);
331 	}
332 	hashmap__free(ctx->ids);
333 	free(ctx);
334 }
335 
336 static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)337 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
338 	      bool compute_ids)
339 {
340 	YY_BUFFER_STATE buffer;
341 	void *scanner;
342 	int ret;
343 
344 	pr_debug2("parsing metric: %s\n", expr);
345 
346 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
347 	if (ret)
348 		return ret;
349 
350 	buffer = expr__scan_string(expr, scanner);
351 
352 #ifdef PARSER_DEBUG
353 	expr_debug = 1;
354 	expr_set_debug(1, scanner);
355 #endif
356 
357 	ret = expr_parse(val, ctx, compute_ids, scanner);
358 
359 	expr__flush_buffer(buffer, scanner);
360 	expr__delete_buffer(buffer, scanner);
361 	expr_lex_destroy(scanner);
362 	return ret;
363 }
364 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)365 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
366 		const char *expr)
367 {
368 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
369 }
370 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)371 int expr__find_ids(const char *expr, const char *one,
372 		   struct expr_parse_ctx *ctx)
373 {
374 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
375 
376 	if (one)
377 		expr__del_id(ctx, one);
378 
379 	return ret;
380 }
381 
expr_id_data__value(const struct expr_id_data * data)382 double expr_id_data__value(const struct expr_id_data *data)
383 {
384 	if (data->kind == EXPR_ID_DATA__VALUE)
385 		return data->val.val;
386 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
387 	return data->ref.val;
388 }
389 
expr_id_data__source_count(const struct expr_id_data * data)390 double expr_id_data__source_count(const struct expr_id_data *data)
391 {
392 	assert(data->kind == EXPR_ID_DATA__VALUE);
393 	return data->val.source_count;
394 }
395 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)396 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
397 {
398 	double result = NAN;
399 	enum tool_pmu_event ev = tool_pmu__str_to_event(literal + 1);
400 
401 	if (ev != TOOL_PMU__EVENT_NONE) {
402 		u64 count;
403 
404 		if (tool_pmu__read_event(ev, /*evsel=*/NULL, &count))
405 			result = count;
406 		else
407 			pr_err("Failure to read '%s'", literal);
408 
409 	} else if (!strcmp("#core_wide", literal)) {
410 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
411 			? 1.0 : 0.0;
412 	} else {
413 		pr_err("Unrecognized literal '%s'", literal);
414 	}
415 
416 	pr_debug2("literal: %s = %f\n", literal, result);
417 	return result;
418 }
419 
420 /* 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)421 double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
422 {
423 	struct evlist *tmp;
424 	double ret;
425 
426 	if (hashmap__find(ctx->ids, id, /*value=*/NULL))
427 		return 1.0;
428 
429 	if (!compute_ids)
430 		return 0.0;
431 
432 	tmp = evlist__new();
433 	if (!tmp)
434 		return NAN;
435 
436 	if (strchr(id, '@')) {
437 		char *tmp_id, *p;
438 
439 		tmp_id = strdup(id);
440 		if (!tmp_id) {
441 			ret = NAN;
442 			goto out;
443 		}
444 		p = strchr(tmp_id, '@');
445 		*p = '/';
446 		p = strrchr(tmp_id, '@');
447 		*p = '/';
448 		ret = parse_event(tmp, tmp_id) ? 0 : 1;
449 		free(tmp_id);
450 	} else {
451 		ret = parse_event(tmp, id) ? 0 : 1;
452 	}
453 out:
454 	evlist__delete(tmp);
455 	return ret;
456 }
457 
expr__strcmp_cpuid_str(const struct expr_parse_ctx * ctx __maybe_unused,bool compute_ids __maybe_unused,const char * test_id)458 double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
459 		       bool compute_ids __maybe_unused, const char *test_id)
460 {
461 	double ret;
462 	struct perf_cpu cpu = {-1};
463 	char *cpuid = get_cpuid_allow_env_override(cpu);
464 
465 	if (!cpuid)
466 		return NAN;
467 
468 	ret = !strcmp_cpuid_str(test_id, cpuid);
469 
470 	free(cpuid);
471 	return ret;
472 }
473