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
;
20 static size_t key_hash(const void *key
, void *ctx __maybe_unused
)
22 const char *str
= (const char *)key
;
25 while (*str
!= '\0') {
33 static bool key_equal(const void *key1
, const void *key2
,
34 void *ctx __maybe_unused
)
36 return !strcmp((const char *)key1
, (const char *)key2
);
39 /* Caller must make sure id is allocated */
40 int expr__add_id(struct expr_parse_ctx
*ctx
, const char *id
)
42 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
46 data_ptr
= malloc(sizeof(*data_ptr
));
50 data_ptr
->parent
= ctx
->parent
;
52 ret
= hashmap__set(&ctx
->ids
, id
, data_ptr
,
53 (const void **)&old_key
, (void **)&old_data
);
61 /* Caller must make sure id is allocated */
62 int expr__add_id_val(struct expr_parse_ctx
*ctx
, const char *id
, double val
)
64 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
68 data_ptr
= malloc(sizeof(*data_ptr
));
72 data_ptr
->is_ref
= false;
74 ret
= hashmap__set(&ctx
->ids
, id
, data_ptr
,
75 (const void **)&old_key
, (void **)&old_data
);
83 int expr__add_ref(struct expr_parse_ctx
*ctx
, struct metric_ref
*ref
)
85 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
90 data_ptr
= zalloc(sizeof(*data_ptr
));
94 name
= strdup(ref
->metric_name
);
101 * The jevents tool converts all metric expressions
102 * to lowercase, including metric references, hence
103 * we need to add lowercase name for metric, so it's
106 for (p
= name
; *p
; p
++)
110 * Intentionally passing just const char pointers,
111 * originally from 'struct pmu_event' object.
112 * We don't need to change them, so there's no
113 * need to create our own copy.
115 data_ptr
->ref
.metric_name
= ref
->metric_name
;
116 data_ptr
->ref
.metric_expr
= ref
->metric_expr
;
117 data_ptr
->ref
.counted
= false;
118 data_ptr
->is_ref
= true;
120 ret
= hashmap__set(&ctx
->ids
, name
, data_ptr
,
121 (const void **)&old_key
, (void **)&old_data
);
125 pr_debug2("adding ref metric %s: %s\n",
126 ref
->metric_name
, ref
->metric_expr
);
133 int expr__get_id(struct expr_parse_ctx
*ctx
, const char *id
,
134 struct expr_id_data
**data
)
136 return hashmap__find(&ctx
->ids
, id
, (void **)data
) ? 0 : -1;
139 int expr__resolve_id(struct expr_parse_ctx
*ctx
, const char *id
,
140 struct expr_id_data
**datap
)
142 struct expr_id_data
*data
;
144 if (expr__get_id(ctx
, id
, datap
) || !*datap
) {
145 pr_debug("%s not found\n", id
);
151 pr_debug2("lookup: is_ref %d, counted %d, val %f: %s\n",
152 data
->is_ref
, data
->ref
.counted
, data
->val
, id
);
154 if (data
->is_ref
&& !data
->ref
.counted
) {
155 data
->ref
.counted
= true;
156 pr_debug("processing metric: %s ENTRY\n", id
);
157 if (expr__parse(&data
->val
, ctx
, data
->ref
.metric_expr
, 1)) {
158 pr_debug("%s failed to count\n", id
);
161 pr_debug("processing metric: %s EXIT: %f\n", id
, data
->val
);
167 void expr__del_id(struct expr_parse_ctx
*ctx
, const char *id
)
169 struct expr_id_data
*old_val
= NULL
;
170 char *old_key
= NULL
;
172 hashmap__delete(&ctx
->ids
, id
,
173 (const void **)&old_key
, (void **)&old_val
);
178 void expr__ctx_init(struct expr_parse_ctx
*ctx
)
180 hashmap__init(&ctx
->ids
, key_hash
, key_equal
, NULL
);
183 void expr__ctx_clear(struct expr_parse_ctx
*ctx
)
185 struct hashmap_entry
*cur
;
188 hashmap__for_each_entry((&ctx
->ids
), cur
, bkt
) {
189 free((char *)cur
->key
);
192 hashmap__clear(&ctx
->ids
);
196 __expr__parse(double *val
, struct expr_parse_ctx
*ctx
, const char *expr
,
197 int start
, int runtime
)
199 struct expr_scanner_ctx scanner_ctx
= {
200 .start_token
= start
,
203 YY_BUFFER_STATE buffer
;
207 pr_debug2("parsing metric: %s\n", expr
);
209 ret
= expr_lex_init_extra(&scanner_ctx
, &scanner
);
213 buffer
= expr__scan_string(expr
, scanner
);
217 expr_set_debug(1, scanner
);
220 ret
= expr_parse(val
, ctx
, scanner
);
222 expr__flush_buffer(buffer
, scanner
);
223 expr__delete_buffer(buffer
, scanner
);
224 expr_lex_destroy(scanner
);
228 int expr__parse(double *final_val
, struct expr_parse_ctx
*ctx
,
229 const char *expr
, int runtime
)
231 return __expr__parse(final_val
, ctx
, expr
, EXPR_PARSE
, runtime
) ? -1 : 0;
234 int expr__find_other(const char *expr
, const char *one
,
235 struct expr_parse_ctx
*ctx
, int runtime
)
237 int ret
= __expr__parse(NULL
, ctx
, expr
, EXPR_OTHER
, runtime
);
240 expr__del_id(ctx
, one
);