1 // SPDX-License-Identifier: GPL-2.0
7 #include "metricgroup.h"
13 #include <util/expr-bison.h>
14 #include <util/expr-flex.h>
15 #include "util/hashmap.h"
16 #include "util/header.h"
20 #include <api/fs/fs.h>
21 #include <linux/err.h>
22 #include <linux/kernel.h>
23 #include <linux/zalloc.h>
29 extern int expr_debug;
40 const char *metric_name;
41 const char *metric_expr;
46 /* Holding a double value. */
48 /* Reference to another metric. */
50 /* A reference but the value has been computed. */
51 EXPR_ID_DATA__REF_VALUE,
55 static size_t key_hash(long key, void *ctx __maybe_unused)
57 const char *str = (const char *)key;
60 while (*str != '\0') {
68 static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
70 return !strcmp((const char *)key1, (const char *)key2);
73 struct hashmap *ids__new(void)
77 hash = hashmap__new(key_hash, key_equal, NULL);
83 void ids__free(struct hashmap *ids)
85 struct hashmap_entry *cur;
91 hashmap__for_each_entry(ids, cur, bkt) {
99 int ids__insert(struct hashmap *ids, const char *id)
101 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
102 char *old_key = NULL;
105 ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
113 struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
116 struct hashmap_entry *cur;
118 struct expr_id_data *old_data = NULL;
119 char *old_key = NULL;
127 if (hashmap__size(ids1) < hashmap__size(ids2)) {
128 struct hashmap *tmp = ids1;
133 hashmap__for_each_entry(ids2, cur, bkt) {
134 ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
148 /* Caller must make sure id is allocated */
149 int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
151 return ids__insert(ctx->ids, id);
154 /* Caller must make sure id is allocated */
155 int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
157 return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
160 /* Caller must make sure id is allocated */
161 int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
162 double val, int source_count)
164 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
165 char *old_key = NULL;
168 data_ptr = malloc(sizeof(*data_ptr));
171 data_ptr->val.val = val;
172 data_ptr->val.source_count = source_count;
173 data_ptr->kind = EXPR_ID_DATA__VALUE;
175 ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
183 int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
185 struct expr_id_data *data_ptr = NULL, *old_data = NULL;
186 char *old_key = NULL;
190 data_ptr = zalloc(sizeof(*data_ptr));
194 name = strdup(ref->metric_name);
201 * Intentionally passing just const char pointers,
202 * originally from 'struct pmu_event' object.
203 * We don't need to change them, so there's no
204 * need to create our own copy.
206 data_ptr->ref.metric_name = ref->metric_name;
207 data_ptr->ref.metric_expr = ref->metric_expr;
208 data_ptr->kind = EXPR_ID_DATA__REF;
210 ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
214 pr_debug2("adding ref metric %s: %s\n",
215 ref->metric_name, ref->metric_expr);
222 int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
223 struct expr_id_data **data)
225 return hashmap__find(ctx->ids, id, data) ? 0 : -1;
228 bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
229 struct expr_parse_ctx *needles)
231 struct hashmap_entry *cur;
233 struct expr_id_data *data;
235 hashmap__for_each_entry(needles->ids, cur, bkt) {
236 if (expr__get_id(haystack, cur->pkey, &data))
243 int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
244 struct expr_id_data **datap)
246 struct expr_id_data *data;
248 if (expr__get_id(ctx, id, datap) || !*datap) {
249 pr_debug("%s not found\n", id);
255 switch (data->kind) {
256 case EXPR_ID_DATA__VALUE:
257 pr_debug2("lookup(%s): val %f\n", id, data->val.val);
259 case EXPR_ID_DATA__REF:
260 pr_debug2("lookup(%s): ref metric name %s\n", id,
261 data->ref.metric_name);
262 pr_debug("processing metric: %s ENTRY\n", id);
263 data->kind = EXPR_ID_DATA__REF_VALUE;
264 if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
265 pr_debug("%s failed to count\n", id);
268 pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
270 case EXPR_ID_DATA__REF_VALUE:
271 pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
272 data->ref.val, data->ref.metric_name);
275 assert(0); /* Unreachable. */
281 void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
283 struct expr_id_data *old_val = NULL;
284 char *old_key = NULL;
286 hashmap__delete(ctx->ids, id, &old_key, &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) {
320 hashmap__clear(ctx->ids);
323 void expr__ctx_free(struct expr_parse_ctx *ctx)
325 struct hashmap_entry *cur;
331 zfree(&ctx->sctx.user_requested_cpu_list);
332 hashmap__for_each_entry(ctx->ids, cur, bkt) {
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 static double has_pmem(void)
409 static bool has_pmem, cached;
410 const char *sysfs = sysfs__mountpoint();
414 snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs);
415 has_pmem = access(path, F_OK) == 0;
418 return has_pmem ? 1.0 : 0.0;
421 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
423 const struct cpu_topology *topology;
426 if (!strcmp("#num_cpus", literal)) {
427 result = cpu__max_present_cpu().cpu;
430 if (!strcmp("#num_cpus_online", literal)) {
431 struct perf_cpu_map *online = cpu_map__online();
434 result = perf_cpu_map__nr(online);
438 if (!strcasecmp("#system_tsc_freq", literal)) {
439 result = arch_get_tsc_freq();
444 * Assume that topology strings are consistent, such as CPUs "0-1"
445 * wouldn't be listed as "0,1", and so after deduplication the number of
446 * these strings gives an indication of the number of packages, dies,
449 if (!strcasecmp("#smt_on", literal)) {
450 result = smt_on() ? 1.0 : 0.0;
453 if (!strcmp("#core_wide", literal)) {
454 result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
458 if (!strcmp("#num_packages", literal)) {
459 topology = online_topology();
460 result = topology->package_cpus_lists;
463 if (!strcmp("#num_dies", literal)) {
464 topology = online_topology();
465 result = topology->die_cpus_lists;
468 if (!strcmp("#num_cores", literal)) {
469 topology = online_topology();
470 result = topology->core_cpus_lists;
473 if (!strcmp("#slots", literal)) {
474 result = perf_pmu__cpu_slots_per_cycle();
477 if (!strcmp("#has_pmem", literal)) {
482 pr_err("Unrecognized literal '%s'", literal);
484 pr_debug2("literal: %s = %f\n", literal, result);
488 /* Does the event 'id' parse? Determine via ctx->ids if possible. */
489 double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
494 if (hashmap__find(ctx->ids, id, /*value=*/NULL))
503 ret = parse_event(tmp, id) ? 0 : 1;
508 double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
509 bool compute_ids __maybe_unused, const char *test_id)
512 struct perf_pmu *pmu = pmu__find_core_pmu();
513 char *cpuid = perf_pmu__getcpuid(pmu);
518 ret = !strcmp_cpuid_str(test_id, cpuid);