xref: /openbmc/linux/tools/perf/util/expr.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
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 "cpumap.h"
9 #include "cputopo.h"
10 #include "debug.h"
11 #include "evlist.h"
12 #include "expr.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 "smt.h"
19 #include "tsc.h"
20 #include <api/fs/fs.h>
21 #include <linux/err.h>
22 #include <linux/kernel.h>
23 #include <linux/zalloc.h>
24 #include <ctype.h>
25 #include <math.h>
26 #include "pmu.h"
27 
28 #ifdef PARSER_DEBUG
29 extern int expr_debug;
30 #endif
31 
32 struct expr_id_data {
33 	union {
34 		struct {
35 			double val;
36 			int source_count;
37 		} val;
38 		struct {
39 			double val;
40 			const char *metric_name;
41 			const char *metric_expr;
42 		} ref;
43 	};
44 
45 	enum {
46 		/* Holding a double value. */
47 		EXPR_ID_DATA__VALUE,
48 		/* Reference to another metric. */
49 		EXPR_ID_DATA__REF,
50 		/* A reference but the value has been computed. */
51 		EXPR_ID_DATA__REF_VALUE,
52 	} kind;
53 };
54 
key_hash(long key,void * ctx __maybe_unused)55 static size_t key_hash(long key, void *ctx __maybe_unused)
56 {
57 	const char *str = (const char *)key;
58 	size_t hash = 0;
59 
60 	while (*str != '\0') {
61 		hash *= 31;
62 		hash += *str;
63 		str++;
64 	}
65 	return hash;
66 }
67 
key_equal(long key1,long key2,void * ctx __maybe_unused)68 static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
69 {
70 	return !strcmp((const char *)key1, (const char *)key2);
71 }
72 
ids__new(void)73 struct hashmap *ids__new(void)
74 {
75 	struct hashmap *hash;
76 
77 	hash = hashmap__new(key_hash, key_equal, NULL);
78 	if (IS_ERR(hash))
79 		return NULL;
80 	return hash;
81 }
82 
ids__free(struct hashmap * ids)83 void ids__free(struct hashmap *ids)
84 {
85 	struct hashmap_entry *cur;
86 	size_t bkt;
87 
88 	if (ids == NULL)
89 		return;
90 
91 	hashmap__for_each_entry(ids, cur, bkt) {
92 		zfree(&cur->pkey);
93 		zfree(&cur->pvalue);
94 	}
95 
96 	hashmap__free(ids);
97 }
98 
ids__insert(struct hashmap * ids,const char * id)99 int ids__insert(struct hashmap *ids, const char *id)
100 {
101 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
102 	char *old_key = NULL;
103 	int ret;
104 
105 	ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
106 	if (ret)
107 		free(data_ptr);
108 	free(old_key);
109 	free(old_data);
110 	return ret;
111 }
112 
ids__union(struct hashmap * ids1,struct hashmap * ids2)113 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
114 {
115 	size_t bkt;
116 	struct hashmap_entry *cur;
117 	int ret;
118 	struct expr_id_data *old_data = NULL;
119 	char *old_key = NULL;
120 
121 	if (!ids1)
122 		return ids2;
123 
124 	if (!ids2)
125 		return ids1;
126 
127 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
128 		struct hashmap *tmp = ids1;
129 
130 		ids1 = ids2;
131 		ids2 = tmp;
132 	}
133 	hashmap__for_each_entry(ids2, cur, bkt) {
134 		ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
135 		free(old_key);
136 		free(old_data);
137 
138 		if (ret) {
139 			hashmap__free(ids1);
140 			hashmap__free(ids2);
141 			return NULL;
142 		}
143 	}
144 	hashmap__free(ids2);
145 	return ids1;
146 }
147 
148 /* Caller must make sure id is allocated */
expr__add_id(struct expr_parse_ctx * ctx,const char * id)149 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
150 {
151 	return ids__insert(ctx->ids, id);
152 }
153 
154 /* Caller must make sure id is allocated */
expr__add_id_val(struct expr_parse_ctx * ctx,const char * id,double val)155 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
156 {
157 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
158 }
159 
160 /* 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)161 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
162 				  double val, int source_count)
163 {
164 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
165 	char *old_key = NULL;
166 	int ret;
167 
168 	data_ptr = malloc(sizeof(*data_ptr));
169 	if (!data_ptr)
170 		return -ENOMEM;
171 	data_ptr->val.val = val;
172 	data_ptr->val.source_count = source_count;
173 	data_ptr->kind = EXPR_ID_DATA__VALUE;
174 
175 	ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
176 	if (ret)
177 		free(data_ptr);
178 	free(old_key);
179 	free(old_data);
180 	return ret;
181 }
182 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)183 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
184 {
185 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
186 	char *old_key = NULL;
187 	char *name;
188 	int ret;
189 
190 	data_ptr = zalloc(sizeof(*data_ptr));
191 	if (!data_ptr)
192 		return -ENOMEM;
193 
194 	name = strdup(ref->metric_name);
195 	if (!name) {
196 		free(data_ptr);
197 		return -ENOMEM;
198 	}
199 
200 	/*
201 	 * Intentionally passing just const char pointers,
202 	 * originally from 'struct pmu_event' object.
203 	 * We don't need to change them, so there's no
204 	 * need to create our own copy.
205 	 */
206 	data_ptr->ref.metric_name = ref->metric_name;
207 	data_ptr->ref.metric_expr = ref->metric_expr;
208 	data_ptr->kind = EXPR_ID_DATA__REF;
209 
210 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
211 	if (ret)
212 		free(data_ptr);
213 
214 	pr_debug2("adding ref metric %s: %s\n",
215 		  ref->metric_name, ref->metric_expr);
216 
217 	free(old_key);
218 	free(old_data);
219 	return ret;
220 }
221 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)222 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
223 		 struct expr_id_data **data)
224 {
225 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
226 }
227 
expr__subset_of_ids(struct expr_parse_ctx * haystack,struct expr_parse_ctx * needles)228 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
229 			 struct expr_parse_ctx *needles)
230 {
231 	struct hashmap_entry *cur;
232 	size_t bkt;
233 	struct expr_id_data *data;
234 
235 	hashmap__for_each_entry(needles->ids, cur, bkt) {
236 		if (expr__get_id(haystack, cur->pkey, &data))
237 			return false;
238 	}
239 	return true;
240 }
241 
242 
expr__resolve_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** datap)243 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
244 		     struct expr_id_data **datap)
245 {
246 	struct expr_id_data *data;
247 
248 	if (expr__get_id(ctx, id, datap) || !*datap) {
249 		pr_debug("%s not found\n", id);
250 		return -1;
251 	}
252 
253 	data = *datap;
254 
255 	switch (data->kind) {
256 	case EXPR_ID_DATA__VALUE:
257 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
258 		break;
259 	case EXPR_ID_DATA__REF:
260 		pr_debug2("lookup(%s): ref metric name %s\n", id,
261 			data->ref.metric_name);
262 		pr_debug("processing metric: %s ENTRY\n", id);
263 		data->kind = EXPR_ID_DATA__REF_VALUE;
264 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
265 			pr_debug("%s failed to count\n", id);
266 			return -1;
267 		}
268 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
269 		break;
270 	case EXPR_ID_DATA__REF_VALUE:
271 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
272 			data->ref.val, data->ref.metric_name);
273 		break;
274 	default:
275 		assert(0);  /* Unreachable. */
276 	}
277 
278 	return 0;
279 }
280 
expr__del_id(struct expr_parse_ctx * ctx,const char * id)281 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
282 {
283 	struct expr_id_data *old_val = NULL;
284 	char *old_key = NULL;
285 
286 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
287 	free(old_key);
288 	free(old_val);
289 }
290 
expr__ctx_new(void)291 struct expr_parse_ctx *expr__ctx_new(void)
292 {
293 	struct expr_parse_ctx *ctx;
294 
295 	ctx = calloc(1, sizeof(struct expr_parse_ctx));
296 	if (!ctx)
297 		return NULL;
298 
299 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
300 	if (IS_ERR(ctx->ids)) {
301 		free(ctx);
302 		return NULL;
303 	}
304 
305 	return ctx;
306 }
307 
expr__ctx_clear(struct expr_parse_ctx * ctx)308 void expr__ctx_clear(struct expr_parse_ctx *ctx)
309 {
310 	struct hashmap_entry *cur;
311 	size_t bkt;
312 
313 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
314 		zfree(&cur->pkey);
315 		zfree(&cur->pvalue);
316 	}
317 	hashmap__clear(ctx->ids);
318 }
319 
expr__ctx_free(struct expr_parse_ctx * ctx)320 void expr__ctx_free(struct expr_parse_ctx *ctx)
321 {
322 	struct hashmap_entry *cur;
323 	size_t bkt;
324 
325 	if (!ctx)
326 		return;
327 
328 	zfree(&ctx->sctx.user_requested_cpu_list);
329 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
330 		zfree(&cur->pkey);
331 		zfree(&cur->pvalue);
332 	}
333 	hashmap__free(ctx->ids);
334 	free(ctx);
335 }
336 
337 static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)338 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
339 	      bool compute_ids)
340 {
341 	YY_BUFFER_STATE buffer;
342 	void *scanner;
343 	int ret;
344 
345 	pr_debug2("parsing metric: %s\n", expr);
346 
347 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
348 	if (ret)
349 		return ret;
350 
351 	buffer = expr__scan_string(expr, scanner);
352 
353 #ifdef PARSER_DEBUG
354 	expr_debug = 1;
355 	expr_set_debug(1, scanner);
356 #endif
357 
358 	ret = expr_parse(val, ctx, compute_ids, scanner);
359 
360 	expr__flush_buffer(buffer, scanner);
361 	expr__delete_buffer(buffer, scanner);
362 	expr_lex_destroy(scanner);
363 	return ret;
364 }
365 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)366 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
367 		const char *expr)
368 {
369 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
370 }
371 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)372 int expr__find_ids(const char *expr, const char *one,
373 		   struct expr_parse_ctx *ctx)
374 {
375 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
376 
377 	if (one)
378 		expr__del_id(ctx, one);
379 
380 	return ret;
381 }
382 
expr_id_data__value(const struct expr_id_data * data)383 double expr_id_data__value(const struct expr_id_data *data)
384 {
385 	if (data->kind == EXPR_ID_DATA__VALUE)
386 		return data->val.val;
387 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
388 	return data->ref.val;
389 }
390 
expr_id_data__source_count(const struct expr_id_data * data)391 double expr_id_data__source_count(const struct expr_id_data *data)
392 {
393 	assert(data->kind == EXPR_ID_DATA__VALUE);
394 	return data->val.source_count;
395 }
396 
397 #if !defined(__i386__) && !defined(__x86_64__)
arch_get_tsc_freq(void)398 double arch_get_tsc_freq(void)
399 {
400 	return 0.0;
401 }
402 #endif
403 
has_pmem(void)404 static double has_pmem(void)
405 {
406 	static bool has_pmem, cached;
407 	const char *sysfs = sysfs__mountpoint();
408 	char path[PATH_MAX];
409 
410 	if (!cached) {
411 		snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs);
412 		has_pmem = access(path, F_OK) == 0;
413 		cached = true;
414 	}
415 	return has_pmem ? 1.0 : 0.0;
416 }
417 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)418 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
419 {
420 	const struct cpu_topology *topology;
421 	double result = NAN;
422 
423 	if (!strcmp("#num_cpus", literal)) {
424 		result = cpu__max_present_cpu().cpu;
425 		goto out;
426 	}
427 	if (!strcmp("#num_cpus_online", literal)) {
428 		struct perf_cpu_map *online = cpu_map__online();
429 
430 		if (online)
431 			result = perf_cpu_map__nr(online);
432 		goto out;
433 	}
434 
435 	if (!strcasecmp("#system_tsc_freq", literal)) {
436 		result = arch_get_tsc_freq();
437 		goto out;
438 	}
439 
440 	/*
441 	 * Assume that topology strings are consistent, such as CPUs "0-1"
442 	 * wouldn't be listed as "0,1", and so after deduplication the number of
443 	 * these strings gives an indication of the number of packages, dies,
444 	 * etc.
445 	 */
446 	if (!strcasecmp("#smt_on", literal)) {
447 		result = smt_on() ? 1.0 : 0.0;
448 		goto out;
449 	}
450 	if (!strcmp("#core_wide", literal)) {
451 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
452 			? 1.0 : 0.0;
453 		goto out;
454 	}
455 	if (!strcmp("#num_packages", literal)) {
456 		topology = online_topology();
457 		result = topology->package_cpus_lists;
458 		goto out;
459 	}
460 	if (!strcmp("#num_dies", literal)) {
461 		topology = online_topology();
462 		result = topology->die_cpus_lists;
463 		goto out;
464 	}
465 	if (!strcmp("#num_cores", literal)) {
466 		topology = online_topology();
467 		result = topology->core_cpus_lists;
468 		goto out;
469 	}
470 	if (!strcmp("#slots", literal)) {
471 		result = perf_pmu__cpu_slots_per_cycle();
472 		goto out;
473 	}
474 	if (!strcmp("#has_pmem", literal)) {
475 		result = has_pmem();
476 		goto out;
477 	}
478 
479 	pr_err("Unrecognized literal '%s'", literal);
480 out:
481 	pr_debug2("literal: %s = %f\n", literal, result);
482 	return result;
483 }
484 
485 /* 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)486 double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
487 {
488 	struct evlist *tmp;
489 	double ret;
490 
491 	if (hashmap__find(ctx->ids, id, /*value=*/NULL))
492 		return 1.0;
493 
494 	if (!compute_ids)
495 		return 0.0;
496 
497 	tmp = evlist__new();
498 	if (!tmp)
499 		return NAN;
500 
501 	if (strchr(id, '@')) {
502 		char *tmp_id, *p;
503 
504 		tmp_id = strdup(id);
505 		if (!tmp_id) {
506 			ret = NAN;
507 			goto out;
508 		}
509 		p = strchr(tmp_id, '@');
510 		*p = '/';
511 		p = strrchr(tmp_id, '@');
512 		*p = '/';
513 		ret = parse_event(tmp, tmp_id) ? 0 : 1;
514 		free(tmp_id);
515 	} else {
516 		ret = parse_event(tmp, id) ? 0 : 1;
517 	}
518 out:
519 	evlist__delete(tmp);
520 	return ret;
521 }
522 
expr__strcmp_cpuid_str(const struct expr_parse_ctx * ctx __maybe_unused,bool compute_ids __maybe_unused,const char * test_id)523 double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
524 		       bool compute_ids __maybe_unused, const char *test_id)
525 {
526 	double ret;
527 	struct perf_pmu *pmu = perf_pmus__find_core_pmu();
528 	char *cpuid = perf_pmu__getcpuid(pmu);
529 
530 	if (!cpuid)
531 		return NAN;
532 
533 	ret = !strcmp_cpuid_str(test_id, cpuid);
534 
535 	free(cpuid);
536 	return ret;
537 }
538