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 
key_hash(const void * key,void * ctx __maybe_unused)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 
key_equal(const void * key1,const void * key2,void * ctx __maybe_unused)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 
ids__new(void)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 
ids__free(struct hashmap * ids)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 
ids__insert(struct hashmap * ids,const char * id)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 
ids__union(struct hashmap * ids1,struct hashmap * ids2)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 */
expr__add_id(struct expr_parse_ctx * ctx,const char * id)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 */
expr__add_id_val(struct expr_parse_ctx * ctx,const char * id,double val)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 */
expr__add_id_val_source_count(struct expr_parse_ctx * ctx,const char * id,double val,int source_count)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 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)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;
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 	 * Intentionally passing just const char pointers,
200 	 * originally from 'struct pmu_event' object.
201 	 * We don't need to change them, so there's no
202 	 * need to create our own copy.
203 	 */
204 	data_ptr->ref.metric_name = ref->metric_name;
205 	data_ptr->ref.metric_expr = ref->metric_expr;
206 	data_ptr->kind = EXPR_ID_DATA__REF;
207 
208 	ret = hashmap__set(ctx->ids, name, data_ptr,
209 			   (const void **)&old_key, (void **)&old_data);
210 	if (ret)
211 		free(data_ptr);
212 
213 	pr_debug2("adding ref metric %s: %s\n",
214 		  ref->metric_name, ref->metric_expr);
215 
216 	free(old_key);
217 	free(old_data);
218 	return ret;
219 }
220 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)221 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
222 		 struct expr_id_data **data)
223 {
224 	return hashmap__find(ctx->ids, id, (void **)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->key, &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,
286 			(const void **)&old_key, (void **)&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 = malloc(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 	ctx->sctx.user_requested_cpu_list = NULL;
305 	ctx->sctx.runtime = 0;
306 	ctx->sctx.system_wide = false;
307 
308 	return ctx;
309 }
310 
expr__ctx_clear(struct expr_parse_ctx * ctx)311 void expr__ctx_clear(struct expr_parse_ctx *ctx)
312 {
313 	struct hashmap_entry *cur;
314 	size_t bkt;
315 
316 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
317 		free((char *)cur->key);
318 		free(cur->value);
319 	}
320 	hashmap__clear(ctx->ids);
321 }
322 
expr__ctx_free(struct expr_parse_ctx * ctx)323 void expr__ctx_free(struct expr_parse_ctx *ctx)
324 {
325 	struct hashmap_entry *cur;
326 	size_t bkt;
327 
328 	if (!ctx)
329 		return;
330 
331 	free(ctx->sctx.user_requested_cpu_list);
332 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
333 		free((char *)cur->key);
334 		free(cur->value);
335 	}
336 	hashmap__free(ctx->ids);
337 	free(ctx);
338 }
339 
340 static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)341 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
342 	      bool compute_ids)
343 {
344 	YY_BUFFER_STATE buffer;
345 	void *scanner;
346 	int ret;
347 
348 	pr_debug2("parsing metric: %s\n", expr);
349 
350 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
351 	if (ret)
352 		return ret;
353 
354 	buffer = expr__scan_string(expr, scanner);
355 
356 #ifdef PARSER_DEBUG
357 	expr_debug = 1;
358 	expr_set_debug(1, scanner);
359 #endif
360 
361 	ret = expr_parse(val, ctx, compute_ids, scanner);
362 
363 	expr__flush_buffer(buffer, scanner);
364 	expr__delete_buffer(buffer, scanner);
365 	expr_lex_destroy(scanner);
366 	return ret;
367 }
368 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)369 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
370 		const char *expr)
371 {
372 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
373 }
374 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)375 int expr__find_ids(const char *expr, const char *one,
376 		   struct expr_parse_ctx *ctx)
377 {
378 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
379 
380 	if (one)
381 		expr__del_id(ctx, one);
382 
383 	return ret;
384 }
385 
expr_id_data__value(const struct expr_id_data * data)386 double expr_id_data__value(const struct expr_id_data *data)
387 {
388 	if (data->kind == EXPR_ID_DATA__VALUE)
389 		return data->val.val;
390 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
391 	return data->ref.val;
392 }
393 
expr_id_data__source_count(const struct expr_id_data * data)394 double expr_id_data__source_count(const struct expr_id_data *data)
395 {
396 	assert(data->kind == EXPR_ID_DATA__VALUE);
397 	return data->val.source_count;
398 }
399 
400 #if !defined(__i386__) && !defined(__x86_64__)
arch_get_tsc_freq(void)401 double arch_get_tsc_freq(void)
402 {
403 	return 0.0;
404 }
405 #endif
406 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)407 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
408 {
409 	static struct cpu_topology *topology;
410 	double result = NAN;
411 
412 	if (!strcmp("#num_cpus", literal)) {
413 		result = cpu__max_present_cpu().cpu;
414 		goto out;
415 	}
416 
417 	if (!strcasecmp("#system_tsc_freq", literal)) {
418 		result = arch_get_tsc_freq();
419 		goto out;
420 	}
421 
422 	/*
423 	 * Assume that topology strings are consistent, such as CPUs "0-1"
424 	 * wouldn't be listed as "0,1", and so after deduplication the number of
425 	 * these strings gives an indication of the number of packages, dies,
426 	 * etc.
427 	 */
428 	if (!topology) {
429 		topology = cpu_topology__new();
430 		if (!topology) {
431 			pr_err("Error creating CPU topology");
432 			goto out;
433 		}
434 	}
435 	if (!strcasecmp("#smt_on", literal)) {
436 		result = smt_on(topology) ? 1.0 : 0.0;
437 		goto out;
438 	}
439 	if (!strcmp("#core_wide", literal)) {
440 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list, topology)
441 			? 1.0 : 0.0;
442 		goto out;
443 	}
444 	if (!strcmp("#num_packages", literal)) {
445 		result = topology->package_cpus_lists;
446 		goto out;
447 	}
448 	if (!strcmp("#num_dies", literal)) {
449 		result = topology->die_cpus_lists;
450 		goto out;
451 	}
452 	if (!strcmp("#num_cores", literal)) {
453 		result = topology->core_cpus_lists;
454 		goto out;
455 	}
456 
457 	pr_err("Unrecognized literal '%s'", literal);
458 out:
459 	pr_debug2("literal: %s = %f\n", literal, result);
460 	return result;
461 }
462