1 // SPDX-License-Identifier: GPL-2.0
7 #include "metricgroup.h"
12 #include "expr-bison.h"
13 #include "expr-flex.h"
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/zalloc.h>
22 extern int expr_debug;
33 const char *metric_name;
34 const char *metric_expr;
39 /* Holding a double value. */
41 /* Reference to another metric. */
43 /* A reference but the value has been computed. */
44 EXPR_ID_DATA__REF_VALUE,
48 static size_t key_hash(const void *key, void *ctx __maybe_unused)
50 const char *str = (const char *)key;
53 while (*str != '\0') {
61 static bool key_equal(const void *key1, const void *key2,
62 void *ctx __maybe_unused)
64 return !strcmp((const char *)key1, (const char *)key2);
67 struct hashmap *ids__new(void)
71 hash = hashmap__new(key_hash, key_equal, NULL);
77 void ids__free(struct hashmap *ids)
79 struct hashmap_entry *cur;
85 hashmap__for_each_entry(ids, cur, bkt) {
86 free((char *)cur->key);
93 int ids__insert(struct hashmap *ids, const char *id)
95 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
99 ret = hashmap__set(ids, id, data_ptr,
100 (const void **)&old_key, (void **)&old_data);
108 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
111 struct hashmap_entry *cur;
113 struct expr_id_data *old_data = NULL;
114 char *old_key = NULL;
122 if (hashmap__size(ids1) < hashmap__size(ids2)) {
123 struct hashmap *tmp = ids1;
128 hashmap__for_each_entry(ids2, cur, bkt) {
129 ret = hashmap__set(ids1, cur->key, cur->value,
130 (const void **)&old_key, (void **)&old_data);
144 /* Caller must make sure id is allocated */
145 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
147 return ids__insert(ctx->ids, id);
150 /* Caller must make sure id is allocated */
151 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
153 return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
156 /* Caller must make sure id is allocated */
157 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
158 double val, int source_count)
160 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
161 char *old_key = NULL;
164 data_ptr = malloc(sizeof(*data_ptr));
167 data_ptr->val.val = val;
168 data_ptr->val.source_count = source_count;
169 data_ptr->kind = EXPR_ID_DATA__VALUE;
171 ret = hashmap__set(ctx->ids, id, data_ptr,
172 (const void **)&old_key, (void **)&old_data);
180 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
182 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
183 char *old_key = NULL;
187 data_ptr = zalloc(sizeof(*data_ptr));
191 name = strdup(ref->metric_name);
198 * The jevents tool converts all metric expressions
199 * to lowercase, including metric references, hence
200 * we need to add lowercase name for metric, so it's
203 for (p = name; *p; p++)
207 * Intentionally passing just const char pointers,
208 * originally from 'struct pmu_event' object.
209 * We don't need to change them, so there's no
210 * need to create our own copy.
212 data_ptr->ref.metric_name = ref->metric_name;
213 data_ptr->ref.metric_expr = ref->metric_expr;
214 data_ptr->kind = EXPR_ID_DATA__REF;
216 ret = hashmap__set(ctx->ids, name, data_ptr,
217 (const void **)&old_key, (void **)&old_data);
221 pr_debug2("adding ref metric %s: %s\n",
222 ref->metric_name, ref->metric_expr);
229 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
230 struct expr_id_data **data)
232 return hashmap__find(ctx->ids, id, (void **)data) ? 0 : -1;
235 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
236 struct expr_parse_ctx *needles)
238 struct hashmap_entry *cur;
240 struct expr_id_data *data;
242 hashmap__for_each_entry(needles->ids, cur, bkt) {
243 if (expr__get_id(haystack, cur->key, &data))
250 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
251 struct expr_id_data **datap)
253 struct expr_id_data *data;
255 if (expr__get_id(ctx, id, datap) || !*datap) {
256 pr_debug("%s not found\n", id);
262 switch (data->kind) {
263 case EXPR_ID_DATA__VALUE:
264 pr_debug2("lookup(%s): val %f\n", id, data->val.val);
266 case EXPR_ID_DATA__REF:
267 pr_debug2("lookup(%s): ref metric name %s\n", id,
268 data->ref.metric_name);
269 pr_debug("processing metric: %s ENTRY\n", id);
270 data->kind = EXPR_ID_DATA__REF_VALUE;
271 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
272 pr_debug("%s failed to count\n", id);
275 pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
277 case EXPR_ID_DATA__REF_VALUE:
278 pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
279 data->ref.val, data->ref.metric_name);
282 assert(0); /* Unreachable. */
288 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
290 struct expr_id_data *old_val = NULL;
291 char *old_key = NULL;
293 hashmap__delete(ctx->ids, id,
294 (const void **)&old_key, (void **)&old_val);
299 struct expr_parse_ctx *expr__ctx_new(void)
301 struct expr_parse_ctx *ctx;
303 ctx = malloc(sizeof(struct expr_parse_ctx));
307 ctx->ids = hashmap__new(key_hash, key_equal, NULL);
308 if (IS_ERR(ctx->ids)) {
317 void expr__ctx_clear(struct expr_parse_ctx *ctx)
319 struct hashmap_entry *cur;
322 hashmap__for_each_entry(ctx->ids, cur, bkt) {
323 free((char *)cur->key);
326 hashmap__clear(ctx->ids);
329 void expr__ctx_free(struct expr_parse_ctx *ctx)
331 struct hashmap_entry *cur;
334 hashmap__for_each_entry(ctx->ids, cur, bkt) {
335 free((char *)cur->key);
338 hashmap__free(ctx->ids);
343 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
346 struct expr_scanner_ctx scanner_ctx = {
347 .runtime = ctx->runtime,
349 YY_BUFFER_STATE buffer;
353 pr_debug2("parsing metric: %s\n", expr);
355 ret = expr_lex_init_extra(&scanner_ctx, &scanner);
359 buffer = expr__scan_string(expr, scanner);
363 expr_set_debug(1, scanner);
366 ret = expr_parse(val, ctx, compute_ids, scanner);
368 expr__flush_buffer(buffer, scanner);
369 expr__delete_buffer(buffer, scanner);
370 expr_lex_destroy(scanner);
374 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
377 return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
380 int expr__find_ids(const char *expr, const char *one,
381 struct expr_parse_ctx *ctx)
383 int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
386 expr__del_id(ctx, one);
391 double expr_id_data__value(const struct expr_id_data *data)
393 if (data->kind == EXPR_ID_DATA__VALUE)
394 return data->val.val;
395 assert(data->kind == EXPR_ID_DATA__REF_VALUE);
396 return data->ref.val;
399 double expr_id_data__source_count(const struct expr_id_data *data)
401 assert(data->kind == EXPR_ID_DATA__VALUE);
402 return data->val.source_count;
405 double expr__get_literal(const char *literal)
407 static struct cpu_topology *topology;
409 if (!strcmp("#smt_on", literal))
410 return smt_on() > 0 ? 1.0 : 0.0;
412 if (!strcmp("#num_cpus", literal))
413 return cpu__max_present_cpu();
416 * Assume that topology strings are consistent, such as CPUs "0-1"
417 * wouldn't be listed as "0,1", and so after deduplication the number of
418 * these strings gives an indication of the number of packages, dies,
422 topology = cpu_topology__new();
424 pr_err("Error creating CPU topology");
428 if (!strcmp("#num_packages", literal))
429 return topology->package_cpus_lists;
430 if (!strcmp("#num_dies", literal))
431 return topology->die_cpus_lists;
432 if (!strcmp("#num_cores", literal))
433 return topology->core_cpus_lists;
435 pr_err("Unrecognized literal '%s'", literal);