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)
69 return hashmap__new(key_hash, key_equal, NULL);
72 void ids__free(struct hashmap *ids)
74 struct hashmap_entry *cur;
80 hashmap__for_each_entry(ids, cur, bkt) {
81 free((char *)cur->key);
88 int ids__insert(struct hashmap *ids, const char *id)
90 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
94 ret = hashmap__set(ids, id, data_ptr,
95 (const void **)&old_key, (void **)&old_data);
103 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
106 struct hashmap_entry *cur;
108 struct expr_id_data *old_data = NULL;
109 char *old_key = NULL;
117 if (hashmap__size(ids1) < hashmap__size(ids2)) {
118 struct hashmap *tmp = ids1;
123 hashmap__for_each_entry(ids2, cur, bkt) {
124 ret = hashmap__set(ids1, cur->key, cur->value,
125 (const void **)&old_key, (void **)&old_data);
139 /* Caller must make sure id is allocated */
140 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
142 return ids__insert(ctx->ids, id);
145 /* Caller must make sure id is allocated */
146 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
148 return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
151 /* Caller must make sure id is allocated */
152 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
153 double val, int source_count)
155 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
156 char *old_key = NULL;
159 data_ptr = malloc(sizeof(*data_ptr));
162 data_ptr->val.val = val;
163 data_ptr->val.source_count = source_count;
164 data_ptr->kind = EXPR_ID_DATA__VALUE;
166 ret = hashmap__set(ctx->ids, id, data_ptr,
167 (const void **)&old_key, (void **)&old_data);
175 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
177 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
178 char *old_key = NULL;
182 data_ptr = zalloc(sizeof(*data_ptr));
186 name = strdup(ref->metric_name);
193 * The jevents tool converts all metric expressions
194 * to lowercase, including metric references, hence
195 * we need to add lowercase name for metric, so it's
198 for (p = name; *p; p++)
202 * Intentionally passing just const char pointers,
203 * originally from 'struct pmu_event' object.
204 * We don't need to change them, so there's no
205 * need to create our own copy.
207 data_ptr->ref.metric_name = ref->metric_name;
208 data_ptr->ref.metric_expr = ref->metric_expr;
209 data_ptr->kind = EXPR_ID_DATA__REF;
211 ret = hashmap__set(ctx->ids, name, data_ptr,
212 (const void **)&old_key, (void **)&old_data);
216 pr_debug2("adding ref metric %s: %s\n",
217 ref->metric_name, ref->metric_expr);
224 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
225 struct expr_id_data **data)
227 return hashmap__find(ctx->ids, id, (void **)data) ? 0 : -1;
230 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
231 struct expr_parse_ctx *needles)
233 struct hashmap_entry *cur;
235 struct expr_id_data *data;
237 hashmap__for_each_entry(needles->ids, cur, bkt) {
238 if (expr__get_id(haystack, cur->key, &data))
245 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
246 struct expr_id_data **datap)
248 struct expr_id_data *data;
250 if (expr__get_id(ctx, id, datap) || !*datap) {
251 pr_debug("%s not found\n", id);
257 switch (data->kind) {
258 case EXPR_ID_DATA__VALUE:
259 pr_debug2("lookup(%s): val %f\n", id, data->val.val);
261 case EXPR_ID_DATA__REF:
262 pr_debug2("lookup(%s): ref metric name %s\n", id,
263 data->ref.metric_name);
264 pr_debug("processing metric: %s ENTRY\n", id);
265 data->kind = EXPR_ID_DATA__REF_VALUE;
266 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
267 pr_debug("%s failed to count\n", id);
270 pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
272 case EXPR_ID_DATA__REF_VALUE:
273 pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
274 data->ref.val, data->ref.metric_name);
277 assert(0); /* Unreachable. */
283 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
285 struct expr_id_data *old_val = NULL;
286 char *old_key = NULL;
288 hashmap__delete(ctx->ids, id,
289 (const void **)&old_key, (void **)&old_val);
294 struct expr_parse_ctx *expr__ctx_new(void)
296 struct expr_parse_ctx *ctx;
298 ctx = malloc(sizeof(struct expr_parse_ctx));
302 ctx->ids = hashmap__new(key_hash, key_equal, NULL);
303 if (IS_ERR(ctx->ids)) {
312 void expr__ctx_clear(struct expr_parse_ctx *ctx)
314 struct hashmap_entry *cur;
317 hashmap__for_each_entry(ctx->ids, cur, bkt) {
318 free((char *)cur->key);
321 hashmap__clear(ctx->ids);
324 void expr__ctx_free(struct expr_parse_ctx *ctx)
326 struct hashmap_entry *cur;
329 hashmap__for_each_entry(ctx->ids, cur, bkt) {
330 free((char *)cur->key);
333 hashmap__free(ctx->ids);
338 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
341 struct expr_scanner_ctx scanner_ctx = {
342 .runtime = ctx->runtime,
344 YY_BUFFER_STATE buffer;
348 pr_debug2("parsing metric: %s\n", expr);
350 ret = expr_lex_init_extra(&scanner_ctx, &scanner);
354 buffer = expr__scan_string(expr, scanner);
358 expr_set_debug(1, scanner);
361 ret = expr_parse(val, ctx, compute_ids, scanner);
363 expr__flush_buffer(buffer, scanner);
364 expr__delete_buffer(buffer, scanner);
365 expr_lex_destroy(scanner);
369 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
372 return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
375 int expr__find_ids(const char *expr, const char *one,
376 struct expr_parse_ctx *ctx)
378 int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
381 expr__del_id(ctx, one);
386 double expr_id_data__value(const struct expr_id_data *data)
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;
394 double expr_id_data__source_count(const struct expr_id_data *data)
396 assert(data->kind == EXPR_ID_DATA__VALUE);
397 return data->val.source_count;
400 double expr__get_literal(const char *literal)
402 static struct cpu_topology *topology;
404 if (!strcmp("#smt_on", literal))
405 return smt_on() > 0 ? 1.0 : 0.0;
407 if (!strcmp("#num_cpus", literal))
408 return cpu__max_present_cpu();
411 * Assume that topology strings are consistent, such as CPUs "0-1"
412 * wouldn't be listed as "0,1", and so after deduplication the number of
413 * these strings gives an indication of the number of packages, dies,
417 topology = cpu_topology__new();
419 pr_err("Error creating CPU topology");
423 if (!strcmp("#num_packages", literal))
424 return topology->package_cpus_lists;
425 if (!strcmp("#num_dies", literal))
426 return topology->die_cpus_lists;
427 if (!strcmp("#num_cores", literal))
428 return topology->core_cpus_lists;
430 pr_err("Unrecognized literal '%s'", literal);