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
;
28 struct expr_id
*parent
;
32 /* Holding a double value. */
34 /* Reference to another metric. */
36 /* A reference but the value has been computed. */
37 EXPR_ID_DATA__REF_VALUE
,
38 /* A parent is remembered for the recursion check. */
43 static size_t key_hash(const void *key
, void *ctx __maybe_unused
)
45 const char *str
= (const char *)key
;
48 while (*str
!= '\0') {
56 static bool key_equal(const void *key1
, const void *key2
,
57 void *ctx __maybe_unused
)
59 return !strcmp((const char *)key1
, (const char *)key2
);
62 /* Caller must make sure id is allocated */
63 int expr__add_id(struct expr_parse_ctx
*ctx
, const char *id
)
65 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
69 data_ptr
= malloc(sizeof(*data_ptr
));
73 data_ptr
->parent
= ctx
->parent
;
74 data_ptr
->kind
= EXPR_ID_DATA__PARENT
;
76 ret
= hashmap__set(&ctx
->ids
, id
, data_ptr
,
77 (const void **)&old_key
, (void **)&old_data
);
85 /* Caller must make sure id is allocated */
86 int expr__add_id_val(struct expr_parse_ctx
*ctx
, const char *id
, double val
)
88 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
92 data_ptr
= malloc(sizeof(*data_ptr
));
96 data_ptr
->kind
= EXPR_ID_DATA__VALUE
;
98 ret
= hashmap__set(&ctx
->ids
, id
, data_ptr
,
99 (const void **)&old_key
, (void **)&old_data
);
107 int expr__add_ref(struct expr_parse_ctx
*ctx
, struct metric_ref
*ref
)
109 struct expr_id_data
*data_ptr
= NULL
, *old_data
= NULL
;
110 char *old_key
= NULL
;
114 data_ptr
= zalloc(sizeof(*data_ptr
));
118 name
= strdup(ref
->metric_name
);
125 * The jevents tool converts all metric expressions
126 * to lowercase, including metric references, hence
127 * we need to add lowercase name for metric, so it's
130 for (p
= name
; *p
; p
++)
134 * Intentionally passing just const char pointers,
135 * originally from 'struct pmu_event' object.
136 * We don't need to change them, so there's no
137 * need to create our own copy.
139 data_ptr
->ref
.metric_name
= ref
->metric_name
;
140 data_ptr
->ref
.metric_expr
= ref
->metric_expr
;
141 data_ptr
->kind
= EXPR_ID_DATA__REF
;
143 ret
= hashmap__set(&ctx
->ids
, name
, data_ptr
,
144 (const void **)&old_key
, (void **)&old_data
);
148 pr_debug2("adding ref metric %s: %s\n",
149 ref
->metric_name
, ref
->metric_expr
);
156 int expr__get_id(struct expr_parse_ctx
*ctx
, const char *id
,
157 struct expr_id_data
**data
)
159 return hashmap__find(&ctx
->ids
, id
, (void **)data
) ? 0 : -1;
162 int expr__resolve_id(struct expr_parse_ctx
*ctx
, const char *id
,
163 struct expr_id_data
**datap
)
165 struct expr_id_data
*data
;
167 if (expr__get_id(ctx
, id
, datap
) || !*datap
) {
168 pr_debug("%s not found\n", id
);
174 switch (data
->kind
) {
175 case EXPR_ID_DATA__VALUE
:
176 pr_debug2("lookup(%s): val %f\n", id
, data
->val
);
178 case EXPR_ID_DATA__PARENT
:
179 pr_debug2("lookup(%s): parent %s\n", id
, data
->parent
->id
);
181 case EXPR_ID_DATA__REF
:
182 pr_debug2("lookup(%s): ref metric name %s\n", id
,
183 data
->ref
.metric_name
);
184 pr_debug("processing metric: %s ENTRY\n", id
);
185 data
->kind
= EXPR_ID_DATA__REF_VALUE
;
186 if (expr__parse(&data
->ref
.val
, ctx
, data
->ref
.metric_expr
, 1)) {
187 pr_debug("%s failed to count\n", id
);
190 pr_debug("processing metric: %s EXIT: %f\n", id
, data
->val
);
192 case EXPR_ID_DATA__REF_VALUE
:
193 pr_debug2("lookup(%s): ref val %f metric name %s\n", id
,
194 data
->ref
.val
, data
->ref
.metric_name
);
197 assert(0); /* Unreachable. */
203 void expr__del_id(struct expr_parse_ctx
*ctx
, const char *id
)
205 struct expr_id_data
*old_val
= NULL
;
206 char *old_key
= NULL
;
208 hashmap__delete(&ctx
->ids
, id
,
209 (const void **)&old_key
, (void **)&old_val
);
214 void expr__ctx_init(struct expr_parse_ctx
*ctx
)
216 hashmap__init(&ctx
->ids
, key_hash
, key_equal
, NULL
);
219 void expr__ctx_clear(struct expr_parse_ctx
*ctx
)
221 struct hashmap_entry
*cur
;
224 hashmap__for_each_entry((&ctx
->ids
), cur
, bkt
) {
225 free((char *)cur
->key
);
228 hashmap__clear(&ctx
->ids
);
232 __expr__parse(double *val
, struct expr_parse_ctx
*ctx
, const char *expr
,
233 int start
, int runtime
)
235 struct expr_scanner_ctx scanner_ctx
= {
236 .start_token
= start
,
239 YY_BUFFER_STATE buffer
;
243 pr_debug2("parsing metric: %s\n", expr
);
245 ret
= expr_lex_init_extra(&scanner_ctx
, &scanner
);
249 buffer
= expr__scan_string(expr
, scanner
);
253 expr_set_debug(1, scanner
);
256 ret
= expr_parse(val
, ctx
, scanner
);
258 expr__flush_buffer(buffer
, scanner
);
259 expr__delete_buffer(buffer
, scanner
);
260 expr_lex_destroy(scanner
);
264 int expr__parse(double *final_val
, struct expr_parse_ctx
*ctx
,
265 const char *expr
, int runtime
)
267 return __expr__parse(final_val
, ctx
, expr
, EXPR_PARSE
, runtime
) ? -1 : 0;
270 int expr__find_other(const char *expr
, const char *one
,
271 struct expr_parse_ctx
*ctx
, int runtime
)
273 int ret
= __expr__parse(NULL
, ctx
, expr
, EXPR_OTHER
, runtime
);
276 expr__del_id(ctx
, one
);
281 double expr_id_data__value(const struct expr_id_data
*data
)
283 if (data
->kind
== EXPR_ID_DATA__VALUE
)
285 assert(data
->kind
== EXPR_ID_DATA__REF_VALUE
);
286 return data
->ref
.val
;
289 struct expr_id
*expr_id_data__parent(struct expr_id_data
*data
)
291 assert(data
->kind
== EXPR_ID_DATA__PARENT
);