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