1 // SPDX-License-Identifier: GPL-2.0
7 #include "metricgroup.h"
10 #include "expr-bison.h"
11 #include "expr-flex.h"
12 #include <linux/kernel.h>
13 #include <linux/zalloc.h>
17 extern int expr_debug;
25 const char *metric_name;
26 const char *metric_expr;
31 /* Holding a double value. */
33 /* Reference to another metric. */
35 /* A reference but the value has been computed. */
36 EXPR_ID_DATA__REF_VALUE,
40 static size_t key_hash(const void *key, void *ctx __maybe_unused)
42 const char *str = (const char *)key;
45 while (*str != '\0') {
53 static bool key_equal(const void *key1, const void *key2,
54 void *ctx __maybe_unused)
56 return !strcmp((const char *)key1, (const char *)key2);
59 struct hashmap *ids__new(void)
61 return hashmap__new(key_hash, key_equal, NULL);
64 void ids__free(struct hashmap *ids)
66 struct hashmap_entry *cur;
72 hashmap__for_each_entry(ids, cur, bkt) {
73 free((char *)cur->key);
80 int ids__insert(struct hashmap *ids, const char *id)
82 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
86 ret = hashmap__set(ids, id, data_ptr,
87 (const void **)&old_key, (void **)&old_data);
95 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
98 struct hashmap_entry *cur;
100 struct expr_id_data *old_data = NULL;
101 char *old_key = NULL;
109 if (hashmap__size(ids1) < hashmap__size(ids2)) {
110 struct hashmap *tmp = ids1;
115 hashmap__for_each_entry(ids2, cur, bkt) {
116 ret = hashmap__set(ids1, cur->key, cur->value,
117 (const void **)&old_key, (void **)&old_data);
131 /* Caller must make sure id is allocated */
132 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
134 return ids__insert(ctx->ids, id);
137 /* Caller must make sure id is allocated */
138 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
140 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
141 char *old_key = NULL;
144 data_ptr = malloc(sizeof(*data_ptr));
148 data_ptr->kind = EXPR_ID_DATA__VALUE;
150 ret = hashmap__set(ctx->ids, id, data_ptr,
151 (const void **)&old_key, (void **)&old_data);
159 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
161 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
162 char *old_key = NULL;
166 data_ptr = zalloc(sizeof(*data_ptr));
170 name = strdup(ref->metric_name);
177 * The jevents tool converts all metric expressions
178 * to lowercase, including metric references, hence
179 * we need to add lowercase name for metric, so it's
182 for (p = name; *p; p++)
186 * Intentionally passing just const char pointers,
187 * originally from 'struct pmu_event' object.
188 * We don't need to change them, so there's no
189 * need to create our own copy.
191 data_ptr->ref.metric_name = ref->metric_name;
192 data_ptr->ref.metric_expr = ref->metric_expr;
193 data_ptr->kind = EXPR_ID_DATA__REF;
195 ret = hashmap__set(ctx->ids, name, data_ptr,
196 (const void **)&old_key, (void **)&old_data);
200 pr_debug2("adding ref metric %s: %s\n",
201 ref->metric_name, ref->metric_expr);
208 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
209 struct expr_id_data **data)
211 return hashmap__find(ctx->ids, id, (void **)data) ? 0 : -1;
214 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
215 struct expr_parse_ctx *needles)
217 struct hashmap_entry *cur;
219 struct expr_id_data *data;
221 hashmap__for_each_entry(needles->ids, cur, bkt) {
222 if (expr__get_id(haystack, cur->key, &data))
229 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
230 struct expr_id_data **datap)
232 struct expr_id_data *data;
234 if (expr__get_id(ctx, id, datap) || !*datap) {
235 pr_debug("%s not found\n", id);
241 switch (data->kind) {
242 case EXPR_ID_DATA__VALUE:
243 pr_debug2("lookup(%s): val %f\n", id, data->val);
245 case EXPR_ID_DATA__REF:
246 pr_debug2("lookup(%s): ref metric name %s\n", id,
247 data->ref.metric_name);
248 pr_debug("processing metric: %s ENTRY\n", id);
249 data->kind = EXPR_ID_DATA__REF_VALUE;
250 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
251 pr_debug("%s failed to count\n", id);
254 pr_debug("processing metric: %s EXIT: %f\n", id, data->val);
256 case EXPR_ID_DATA__REF_VALUE:
257 pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
258 data->ref.val, data->ref.metric_name);
261 assert(0); /* Unreachable. */
267 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
269 struct expr_id_data *old_val = NULL;
270 char *old_key = NULL;
272 hashmap__delete(ctx->ids, id,
273 (const void **)&old_key, (void **)&old_val);
278 struct expr_parse_ctx *expr__ctx_new(void)
280 struct expr_parse_ctx *ctx;
282 ctx = malloc(sizeof(struct expr_parse_ctx));
286 ctx->ids = hashmap__new(key_hash, key_equal, NULL);
292 void expr__ctx_clear(struct expr_parse_ctx *ctx)
294 struct hashmap_entry *cur;
297 hashmap__for_each_entry(ctx->ids, cur, bkt) {
298 free((char *)cur->key);
301 hashmap__clear(ctx->ids);
304 void expr__ctx_free(struct expr_parse_ctx *ctx)
306 struct hashmap_entry *cur;
309 hashmap__for_each_entry(ctx->ids, cur, bkt) {
310 free((char *)cur->key);
313 hashmap__free(ctx->ids);
318 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
321 struct expr_scanner_ctx scanner_ctx = {
322 .runtime = ctx->runtime,
324 YY_BUFFER_STATE buffer;
328 pr_debug2("parsing metric: %s\n", expr);
330 ret = expr_lex_init_extra(&scanner_ctx, &scanner);
334 buffer = expr__scan_string(expr, scanner);
338 expr_set_debug(1, scanner);
341 ret = expr_parse(val, ctx, compute_ids, scanner);
343 expr__flush_buffer(buffer, scanner);
344 expr__delete_buffer(buffer, scanner);
345 expr_lex_destroy(scanner);
349 int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
352 return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
355 int expr__find_ids(const char *expr, const char *one,
356 struct expr_parse_ctx *ctx)
358 int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
361 expr__del_id(ctx, one);
366 double expr_id_data__value(const struct expr_id_data *data)
368 if (data->kind == EXPR_ID_DATA__VALUE)
370 assert(data->kind == EXPR_ID_DATA__REF_VALUE);
371 return data->ref.val;