1 // SPDX-License-Identifier: GPL-2.0
7 #include "metricgroup.h"
12 #include "expr-bison.h"
13 #include "expr-flex.h"
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/zalloc.h>
23 extern int expr_debug;
34 const char *metric_name;
35 const char *metric_expr;
40 /* Holding a double value. */
42 /* Reference to another metric. */
44 /* A reference but the value has been computed. */
45 EXPR_ID_DATA__REF_VALUE,
49 static size_t key_hash(const void *key, void *ctx __maybe_unused)
51 const char *str = (const char *)key;
54 while (*str != '\0') {
62 static bool key_equal(const void *key1, const void *key2,
63 void *ctx __maybe_unused)
65 return !strcmp((const char *)key1, (const char *)key2);
68 struct hashmap *ids__new(void)
72 hash = hashmap__new(key_hash, key_equal, NULL);
78 void ids__free(struct hashmap *ids)
80 struct hashmap_entry *cur;
86 hashmap__for_each_entry(ids, cur, bkt) {
87 free((char *)cur->key);
94 int ids__insert(struct hashmap *ids, const char *id)
96 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
100 ret = hashmap__set(ids, id, data_ptr,
101 (const void **)&old_key, (void **)&old_data);
109 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
112 struct hashmap_entry *cur;
114 struct expr_id_data *old_data = NULL;
115 char *old_key = NULL;
123 if (hashmap__size(ids1) < hashmap__size(ids2)) {
124 struct hashmap *tmp = ids1;
129 hashmap__for_each_entry(ids2, cur, bkt) {
130 ret = hashmap__set(ids1, cur->key, cur->value,
131 (const void **)&old_key, (void **)&old_data);
145 /* Caller must make sure id is allocated */
146 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
148 return ids__insert(ctx->ids, id);
151 /* Caller must make sure id is allocated */
152 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
154 return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
157 /* Caller must make sure id is allocated */
158 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
159 double val, int source_count)
161 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
162 char *old_key = NULL;
165 data_ptr = malloc(sizeof(*data_ptr));
168 data_ptr->val.val = val;
169 data_ptr->val.source_count = source_count;
170 data_ptr->kind = EXPR_ID_DATA__VALUE;
172 ret = hashmap__set(ctx->ids, id, data_ptr,
173 (const void **)&old_key, (void **)&old_data);
181 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
183 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
184 char *old_key = NULL;
188 data_ptr = zalloc(sizeof(*data_ptr));
192 name = strdup(ref->metric_name);
199 * Intentionally passing just const char pointers,
200 * originally from 'struct pmu_event' object.
201 * We don't need to change them, so there's no
202 * need to create our own copy.
204 data_ptr->ref.metric_name = ref->metric_name;
205 data_ptr->ref.metric_expr = ref->metric_expr;
206 data_ptr->kind = EXPR_ID_DATA__REF;
208 ret = hashmap__set(ctx->ids, name, data_ptr,
209 (const void **)&old_key, (void **)&old_data);
213 pr_debug2("adding ref metric %s: %s\n",
214 ref->metric_name, ref->metric_expr);
221 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
222 struct expr_id_data **data)
224 return hashmap__find(ctx->ids, id, (void **)data) ? 0 : -1;
227 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
228 struct expr_parse_ctx *needles)
230 struct hashmap_entry *cur;
232 struct expr_id_data *data;
234 hashmap__for_each_entry(needles->ids, cur, bkt) {
235 if (expr__get_id(haystack, cur->key, &data))
242 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
243 struct expr_id_data **datap)
245 struct expr_id_data *data;
247 if (expr__get_id(ctx, id, datap) || !*datap) {
248 pr_debug("%s not found\n", id);
254 switch (data->kind) {
255 case EXPR_ID_DATA__VALUE:
256 pr_debug2("lookup(%s): val %f\n", id, data->val.val);
258 case EXPR_ID_DATA__REF:
259 pr_debug2("lookup(%s): ref metric name %s\n", id,
260 data->ref.metric_name);
261 pr_debug("processing metric: %s ENTRY\n", id);
262 data->kind = EXPR_ID_DATA__REF_VALUE;
263 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
264 pr_debug("%s failed to count\n", id);
267 pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
269 case EXPR_ID_DATA__REF_VALUE:
270 pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
271 data->ref.val, data->ref.metric_name);
274 assert(0); /* Unreachable. */
280 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
282 struct expr_id_data *old_val = NULL;
283 char *old_key = NULL;
285 hashmap__delete(ctx->ids, id,
286 (const void **)&old_key, (void **)&old_val);
291 struct expr_parse_ctx *expr__ctx_new(void)
293 struct expr_parse_ctx *ctx;
295 ctx = malloc(sizeof(struct expr_parse_ctx));
299 ctx->ids = hashmap__new(key_hash, key_equal, NULL);
300 if (IS_ERR(ctx->ids)) {
304 ctx->sctx.user_requested_cpu_list = NULL;
305 ctx->sctx.runtime = 0;
306 ctx->sctx.system_wide = false;
311 void expr__ctx_clear(struct expr_parse_ctx *ctx)
313 struct hashmap_entry *cur;
316 hashmap__for_each_entry(ctx->ids, cur, bkt) {
317 free((char *)cur->key);
320 hashmap__clear(ctx->ids);
323 void expr__ctx_free(struct expr_parse_ctx *ctx)
325 struct hashmap_entry *cur;
331 free(ctx->sctx.user_requested_cpu_list);
332 hashmap__for_each_entry(ctx->ids, cur, bkt) {
333 free((char *)cur->key);
336 hashmap__free(ctx->ids);
341 __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
344 YY_BUFFER_STATE buffer;
348 pr_debug2("parsing metric: %s\n", expr);
350 ret = expr_lex_init_extra(&ctx->sctx, &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 #if !defined(__i386__) && !defined(__x86_64__)
401 double arch_get_tsc_freq(void)
407 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
409 static struct cpu_topology *topology;
412 if (!strcmp("#num_cpus", literal)) {
413 result = cpu__max_present_cpu().cpu;
417 if (!strcasecmp("#system_tsc_freq", literal)) {
418 result = arch_get_tsc_freq();
423 * Assume that topology strings are consistent, such as CPUs "0-1"
424 * wouldn't be listed as "0,1", and so after deduplication the number of
425 * these strings gives an indication of the number of packages, dies,
429 topology = cpu_topology__new();
431 pr_err("Error creating CPU topology");
435 if (!strcasecmp("#smt_on", literal)) {
436 result = smt_on(topology) ? 1.0 : 0.0;
439 if (!strcmp("#core_wide", literal)) {
440 result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list, topology)
444 if (!strcmp("#num_packages", literal)) {
445 result = topology->package_cpus_lists;
448 if (!strcmp("#num_dies", literal)) {
449 result = topology->die_cpus_lists;
452 if (!strcmp("#num_cores", literal)) {
453 result = topology->core_cpus_lists;
457 pr_err("Unrecognized literal '%s'", literal);
459 pr_debug2("literal: %s = %f\n", literal, result);