xref: /linux-6.15/tools/perf/util/expr.c (revision ee8aef2d)
1576a65b6SJiri Olsa // SPDX-License-Identifier: GPL-2.0
226226a97SJiri Olsa #include <stdbool.h>
3576a65b6SJiri Olsa #include <assert.h>
43744ca1eSArnaldo Carvalho de Melo #include <errno.h>
53744ca1eSArnaldo Carvalho de Melo #include <stdlib.h>
63744ca1eSArnaldo Carvalho de Melo #include <string.h>
7fc393839SJiri Olsa #include "metricgroup.h"
8fc393839SJiri Olsa #include "debug.h"
94a4a9bf9SIan Rogers #include "evlist.h"
10576a65b6SJiri Olsa #include "expr.h"
1106905723SIan Rogers #include "smt.h"
1206905723SIan Rogers #include "tool_pmu.h"
13c7e97f21SNamhyung Kim #include <util/expr-bison.h>
14c7e97f21SNamhyung Kim #include <util/expr-flex.h>
15bd560973SIan Rogers #include "util/hashmap.h"
169d5da30eSJames Clark #include "util/header.h"
179d5da30eSJames Clark #include "util/pmu.h"
18494c403fSIan Rogers #include <perf/cpumap.h>
190a515a06SMiaoqian Lin #include <linux/err.h>
20ded80bdaSIan Rogers #include <linux/kernel.h>
21fc393839SJiri Olsa #include <linux/zalloc.h>
22fc393839SJiri Olsa #include <ctype.h>
233613f6c1SIan Rogers #include <math.h>
2426226a97SJiri Olsa 
2529396cd5SIan Rogers struct expr_id_data {
2629396cd5SIan Rogers 	union {
279aba0adaSIan Rogers 		struct {
2829396cd5SIan Rogers 			double val;
299aba0adaSIan Rogers 			int source_count;
309aba0adaSIan Rogers 		} val;
3129396cd5SIan Rogers 		struct {
3229396cd5SIan Rogers 			double val;
3329396cd5SIan Rogers 			const char *metric_name;
3429396cd5SIan Rogers 			const char *metric_expr;
3529396cd5SIan Rogers 		} ref;
3629396cd5SIan Rogers 	};
3729396cd5SIan Rogers 
3829396cd5SIan Rogers 	enum {
3929396cd5SIan Rogers 		/* Holding a double value. */
4029396cd5SIan Rogers 		EXPR_ID_DATA__VALUE,
4129396cd5SIan Rogers 		/* Reference to another metric. */
4229396cd5SIan Rogers 		EXPR_ID_DATA__REF,
4329396cd5SIan Rogers 		/* A reference but the value has been computed. */
4429396cd5SIan Rogers 		EXPR_ID_DATA__REF_VALUE,
4529396cd5SIan Rogers 	} kind;
4629396cd5SIan Rogers };
4729396cd5SIan Rogers 
key_hash(long key,void * ctx __maybe_unused)48c302378bSEduard Zingerman static size_t key_hash(long key, void *ctx __maybe_unused)
49576a65b6SJiri Olsa {
50ded80bdaSIan Rogers 	const char *str = (const char *)key;
51ded80bdaSIan Rogers 	size_t hash = 0;
52576a65b6SJiri Olsa 
53ded80bdaSIan Rogers 	while (*str != '\0') {
54ded80bdaSIan Rogers 		hash *= 31;
55ded80bdaSIan Rogers 		hash += *str;
56ded80bdaSIan Rogers 		str++;
57ded80bdaSIan Rogers 	}
58ded80bdaSIan Rogers 	return hash;
59ded80bdaSIan Rogers }
60ded80bdaSIan Rogers 
key_equal(long key1,long key2,void * ctx __maybe_unused)61c302378bSEduard Zingerman static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
62ded80bdaSIan Rogers {
63ded80bdaSIan Rogers 	return !strcmp((const char *)key1, (const char *)key2);
64ded80bdaSIan Rogers }
65ded80bdaSIan Rogers 
ids__new(void)66114a9d6eSIan Rogers struct hashmap *ids__new(void)
67114a9d6eSIan Rogers {
689f3c16a4SMiaoqian Lin 	struct hashmap *hash;
699f3c16a4SMiaoqian Lin 
709f3c16a4SMiaoqian Lin 	hash = hashmap__new(key_hash, key_equal, NULL);
719f3c16a4SMiaoqian Lin 	if (IS_ERR(hash))
729f3c16a4SMiaoqian Lin 		return NULL;
739f3c16a4SMiaoqian Lin 	return hash;
74114a9d6eSIan Rogers }
75114a9d6eSIan Rogers 
ids__free(struct hashmap * ids)76114a9d6eSIan Rogers void ids__free(struct hashmap *ids)
77114a9d6eSIan Rogers {
78114a9d6eSIan Rogers 	struct hashmap_entry *cur;
79114a9d6eSIan Rogers 	size_t bkt;
80114a9d6eSIan Rogers 
81114a9d6eSIan Rogers 	if (ids == NULL)
82114a9d6eSIan Rogers 		return;
83114a9d6eSIan Rogers 
84114a9d6eSIan Rogers 	hashmap__for_each_entry(ids, cur, bkt) {
85a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
86a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
87114a9d6eSIan Rogers 	}
88114a9d6eSIan Rogers 
89114a9d6eSIan Rogers 	hashmap__free(ids);
90114a9d6eSIan Rogers }
91114a9d6eSIan Rogers 
ids__insert(struct hashmap * ids,const char * id)9280be6434SIan Rogers int ids__insert(struct hashmap *ids, const char *id)
93332603c2SJiri Olsa {
94332603c2SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
95332603c2SJiri Olsa 	char *old_key = NULL;
96332603c2SJiri Olsa 	int ret;
97332603c2SJiri Olsa 
98c302378bSEduard Zingerman 	ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
99332603c2SJiri Olsa 	if (ret)
100332603c2SJiri Olsa 		free(data_ptr);
101332603c2SJiri Olsa 	free(old_key);
102332603c2SJiri Olsa 	free(old_data);
103332603c2SJiri Olsa 	return ret;
104332603c2SJiri Olsa }
105332603c2SJiri Olsa 
ids__union(struct hashmap * ids1,struct hashmap * ids2)106114a9d6eSIan Rogers struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
107114a9d6eSIan Rogers {
108114a9d6eSIan Rogers 	size_t bkt;
109114a9d6eSIan Rogers 	struct hashmap_entry *cur;
110114a9d6eSIan Rogers 	int ret;
111114a9d6eSIan Rogers 	struct expr_id_data *old_data = NULL;
112114a9d6eSIan Rogers 	char *old_key = NULL;
113114a9d6eSIan Rogers 
114114a9d6eSIan Rogers 	if (!ids1)
115114a9d6eSIan Rogers 		return ids2;
116114a9d6eSIan Rogers 
117114a9d6eSIan Rogers 	if (!ids2)
118114a9d6eSIan Rogers 		return ids1;
119114a9d6eSIan Rogers 
120114a9d6eSIan Rogers 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
121114a9d6eSIan Rogers 		struct hashmap *tmp = ids1;
122114a9d6eSIan Rogers 
123114a9d6eSIan Rogers 		ids1 = ids2;
124114a9d6eSIan Rogers 		ids2 = tmp;
125114a9d6eSIan Rogers 	}
126114a9d6eSIan Rogers 	hashmap__for_each_entry(ids2, cur, bkt) {
127c302378bSEduard Zingerman 		ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
128114a9d6eSIan Rogers 		free(old_key);
129114a9d6eSIan Rogers 		free(old_data);
130114a9d6eSIan Rogers 
131114a9d6eSIan Rogers 		if (ret) {
132114a9d6eSIan Rogers 			hashmap__free(ids1);
133114a9d6eSIan Rogers 			hashmap__free(ids2);
134114a9d6eSIan Rogers 			return NULL;
135114a9d6eSIan Rogers 		}
136114a9d6eSIan Rogers 	}
137114a9d6eSIan Rogers 	hashmap__free(ids2);
138114a9d6eSIan Rogers 	return ids1;
139114a9d6eSIan Rogers }
140114a9d6eSIan Rogers 
141114a9d6eSIan Rogers /* Caller must make sure id is allocated */
expr__add_id(struct expr_parse_ctx * ctx,const char * id)142114a9d6eSIan Rogers int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
143114a9d6eSIan Rogers {
14480be6434SIan Rogers 	return ids__insert(ctx->ids, id);
145114a9d6eSIan Rogers }
146114a9d6eSIan Rogers 
147332603c2SJiri Olsa /* Caller must make sure id is allocated */
expr__add_id_val(struct expr_parse_ctx * ctx,const char * id,double val)148070b3b5aSJiri Olsa int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
149ded80bdaSIan Rogers {
1509aba0adaSIan Rogers 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
1519aba0adaSIan Rogers }
1529aba0adaSIan Rogers 
1539aba0adaSIan Rogers /* Caller must make sure id is allocated */
expr__add_id_val_source_count(struct expr_parse_ctx * ctx,const char * id,double val,int source_count)1549aba0adaSIan Rogers int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
1559aba0adaSIan Rogers 				  double val, int source_count)
1569aba0adaSIan Rogers {
157070b3b5aSJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
158ded80bdaSIan Rogers 	char *old_key = NULL;
159ded80bdaSIan Rogers 	int ret;
160ded80bdaSIan Rogers 
161070b3b5aSJiri Olsa 	data_ptr = malloc(sizeof(*data_ptr));
162070b3b5aSJiri Olsa 	if (!data_ptr)
163ded80bdaSIan Rogers 		return -ENOMEM;
1649aba0adaSIan Rogers 	data_ptr->val.val = val;
1659aba0adaSIan Rogers 	data_ptr->val.source_count = source_count;
16629396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__VALUE;
167332603c2SJiri Olsa 
168c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
16960e10c00SJiri Olsa 	if (ret)
17060e10c00SJiri Olsa 		free(data_ptr);
171ded80bdaSIan Rogers 	free(old_key);
172070b3b5aSJiri Olsa 	free(old_data);
173ded80bdaSIan Rogers 	return ret;
174ded80bdaSIan Rogers }
175ded80bdaSIan Rogers 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)176fc393839SJiri Olsa int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
177fc393839SJiri Olsa {
178fc393839SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
179fc393839SJiri Olsa 	char *old_key = NULL;
180715b824fSIan Rogers 	char *name;
181fc393839SJiri Olsa 	int ret;
182fc393839SJiri Olsa 
183fc393839SJiri Olsa 	data_ptr = zalloc(sizeof(*data_ptr));
184fc393839SJiri Olsa 	if (!data_ptr)
185fc393839SJiri Olsa 		return -ENOMEM;
186fc393839SJiri Olsa 
187fc393839SJiri Olsa 	name = strdup(ref->metric_name);
188fc393839SJiri Olsa 	if (!name) {
189fc393839SJiri Olsa 		free(data_ptr);
190fc393839SJiri Olsa 		return -ENOMEM;
191fc393839SJiri Olsa 	}
192fc393839SJiri Olsa 
193fc393839SJiri Olsa 	/*
194fc393839SJiri Olsa 	 * Intentionally passing just const char pointers,
195fc393839SJiri Olsa 	 * originally from 'struct pmu_event' object.
196fc393839SJiri Olsa 	 * We don't need to change them, so there's no
197fc393839SJiri Olsa 	 * need to create our own copy.
198fc393839SJiri Olsa 	 */
199fc393839SJiri Olsa 	data_ptr->ref.metric_name = ref->metric_name;
200fc393839SJiri Olsa 	data_ptr->ref.metric_expr = ref->metric_expr;
20129396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__REF;
202fc393839SJiri Olsa 
203c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
204fc393839SJiri Olsa 	if (ret)
205fc393839SJiri Olsa 		free(data_ptr);
206fc393839SJiri Olsa 
207fc393839SJiri Olsa 	pr_debug2("adding ref metric %s: %s\n",
208fc393839SJiri Olsa 		  ref->metric_name, ref->metric_expr);
209fc393839SJiri Olsa 
210fc393839SJiri Olsa 	free(old_key);
211fc393839SJiri Olsa 	free(old_data);
212fc393839SJiri Olsa 	return ret;
213fc393839SJiri Olsa }
214fc393839SJiri Olsa 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)2155c5f5e83SJiri Olsa int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
2165c5f5e83SJiri Olsa 		 struct expr_id_data **data)
217ded80bdaSIan Rogers {
218*ee8aef2dSKan Liang 	if (!ctx || !id)
219*ee8aef2dSKan Liang 		return -1;
220c302378bSEduard Zingerman 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
221576a65b6SJiri Olsa }
222576a65b6SJiri Olsa 
expr__subset_of_ids(struct expr_parse_ctx * haystack,struct expr_parse_ctx * needles)223798c3f4aSIan Rogers bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
224798c3f4aSIan Rogers 			 struct expr_parse_ctx *needles)
225798c3f4aSIan Rogers {
226798c3f4aSIan Rogers 	struct hashmap_entry *cur;
227798c3f4aSIan Rogers 	size_t bkt;
228798c3f4aSIan Rogers 	struct expr_id_data *data;
229798c3f4aSIan Rogers 
230798c3f4aSIan Rogers 	hashmap__for_each_entry(needles->ids, cur, bkt) {
231c302378bSEduard Zingerman 		if (expr__get_id(haystack, cur->pkey, &data))
232798c3f4aSIan Rogers 			return false;
233798c3f4aSIan Rogers 	}
234798c3f4aSIan Rogers 	return true;
235798c3f4aSIan Rogers }
236798c3f4aSIan Rogers 
237798c3f4aSIan Rogers 
expr__resolve_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** datap)238acf71b05SJiri Olsa int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
239acf71b05SJiri Olsa 		     struct expr_id_data **datap)
240acf71b05SJiri Olsa {
241acf71b05SJiri Olsa 	struct expr_id_data *data;
242acf71b05SJiri Olsa 
243acf71b05SJiri Olsa 	if (expr__get_id(ctx, id, datap) || !*datap) {
244acf71b05SJiri Olsa 		pr_debug("%s not found\n", id);
245acf71b05SJiri Olsa 		return -1;
246acf71b05SJiri Olsa 	}
247acf71b05SJiri Olsa 
248acf71b05SJiri Olsa 	data = *datap;
249acf71b05SJiri Olsa 
25029396cd5SIan Rogers 	switch (data->kind) {
25129396cd5SIan Rogers 	case EXPR_ID_DATA__VALUE:
2529aba0adaSIan Rogers 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
25329396cd5SIan Rogers 		break;
25429396cd5SIan Rogers 	case EXPR_ID_DATA__REF:
25529396cd5SIan Rogers 		pr_debug2("lookup(%s): ref metric name %s\n", id,
25629396cd5SIan Rogers 			data->ref.metric_name);
257acf71b05SJiri Olsa 		pr_debug("processing metric: %s ENTRY\n", id);
25829396cd5SIan Rogers 		data->kind = EXPR_ID_DATA__REF_VALUE;
259fa831fbbSIan Rogers 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
260acf71b05SJiri Olsa 			pr_debug("%s failed to count\n", id);
261acf71b05SJiri Olsa 			return -1;
262acf71b05SJiri Olsa 		}
2639aba0adaSIan Rogers 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
26429396cd5SIan Rogers 		break;
26529396cd5SIan Rogers 	case EXPR_ID_DATA__REF_VALUE:
26629396cd5SIan Rogers 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
26729396cd5SIan Rogers 			data->ref.val, data->ref.metric_name);
26829396cd5SIan Rogers 		break;
26929396cd5SIan Rogers 	default:
27029396cd5SIan Rogers 		assert(0);  /* Unreachable. */
271acf71b05SJiri Olsa 	}
272acf71b05SJiri Olsa 
273acf71b05SJiri Olsa 	return 0;
274acf71b05SJiri Olsa }
275acf71b05SJiri Olsa 
expr__del_id(struct expr_parse_ctx * ctx,const char * id)2763fd29fa6SJiri Olsa void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
2773fd29fa6SJiri Olsa {
2783fd29fa6SJiri Olsa 	struct expr_id_data *old_val = NULL;
2793fd29fa6SJiri Olsa 	char *old_key = NULL;
2803fd29fa6SJiri Olsa 
281c302378bSEduard Zingerman 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
2823fd29fa6SJiri Olsa 	free(old_key);
2833fd29fa6SJiri Olsa 	free(old_val);
2843fd29fa6SJiri Olsa }
2853fd29fa6SJiri Olsa 
expr__ctx_new(void)286cb94a02eSIan Rogers struct expr_parse_ctx *expr__ctx_new(void)
287576a65b6SJiri Olsa {
288cb94a02eSIan Rogers 	struct expr_parse_ctx *ctx;
289cb94a02eSIan Rogers 
2901d18ebcfSLevi Yun 	ctx = calloc(1, sizeof(struct expr_parse_ctx));
291cb94a02eSIan Rogers 	if (!ctx)
292cb94a02eSIan Rogers 		return NULL;
293cb94a02eSIan Rogers 
294cb94a02eSIan Rogers 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
2950a515a06SMiaoqian Lin 	if (IS_ERR(ctx->ids)) {
2960a515a06SMiaoqian Lin 		free(ctx);
2970a515a06SMiaoqian Lin 		return NULL;
2980a515a06SMiaoqian Lin 	}
29980be6434SIan Rogers 
300cb94a02eSIan Rogers 	return ctx;
301ded80bdaSIan Rogers }
302ded80bdaSIan Rogers 
expr__ctx_clear(struct expr_parse_ctx * ctx)303ded80bdaSIan Rogers void expr__ctx_clear(struct expr_parse_ctx *ctx)
304ded80bdaSIan Rogers {
305ded80bdaSIan Rogers 	struct hashmap_entry *cur;
306ded80bdaSIan Rogers 	size_t bkt;
307ded80bdaSIan Rogers 
308cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
309a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
310a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
311ded80bdaSIan Rogers 	}
312cb94a02eSIan Rogers 	hashmap__clear(ctx->ids);
313cb94a02eSIan Rogers }
314cb94a02eSIan Rogers 
expr__ctx_free(struct expr_parse_ctx * ctx)315cb94a02eSIan Rogers void expr__ctx_free(struct expr_parse_ctx *ctx)
316cb94a02eSIan Rogers {
317cb94a02eSIan Rogers 	struct hashmap_entry *cur;
318cb94a02eSIan Rogers 	size_t bkt;
319cb94a02eSIan Rogers 
3201725e9cdSIan Rogers 	if (!ctx)
3211725e9cdSIan Rogers 		return;
3221725e9cdSIan Rogers 
323a77f8184SArnaldo Carvalho de Melo 	zfree(&ctx->sctx.user_requested_cpu_list);
324cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
325a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
326a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
327cb94a02eSIan Rogers 	}
328cb94a02eSIan Rogers 	hashmap__free(ctx->ids);
329cb94a02eSIan Rogers 	free(ctx);
330576a65b6SJiri Olsa }
33126226a97SJiri Olsa 
33226226a97SJiri Olsa static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)333aecce63eSJiri Olsa __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
334fa831fbbSIan Rogers 	      bool compute_ids)
33526226a97SJiri Olsa {
33626226a97SJiri Olsa 	YY_BUFFER_STATE buffer;
33726226a97SJiri Olsa 	void *scanner;
33826226a97SJiri Olsa 	int ret;
33926226a97SJiri Olsa 
340acf71b05SJiri Olsa 	pr_debug2("parsing metric: %s\n", expr);
341acf71b05SJiri Olsa 
3421a6abddeSIan Rogers 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
34326226a97SJiri Olsa 	if (ret)
34426226a97SJiri Olsa 		return ret;
34526226a97SJiri Olsa 
34626226a97SJiri Olsa 	buffer = expr__scan_string(expr, scanner);
34726226a97SJiri Olsa 
34826226a97SJiri Olsa #ifdef PARSER_DEBUG
34926226a97SJiri Olsa 	expr_debug = 1;
350e5e0e635SIan Rogers 	expr_set_debug(1, scanner);
35126226a97SJiri Olsa #endif
35226226a97SJiri Olsa 
3533f965a7dSIan Rogers 	ret = expr_parse(val, ctx, compute_ids, scanner);
35426226a97SJiri Olsa 
35526226a97SJiri Olsa 	expr__flush_buffer(buffer, scanner);
35626226a97SJiri Olsa 	expr__delete_buffer(buffer, scanner);
35726226a97SJiri Olsa 	expr_lex_destroy(scanner);
35826226a97SJiri Olsa 	return ret;
35926226a97SJiri Olsa }
36026226a97SJiri Olsa 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)361ded80bdaSIan Rogers int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
362fa831fbbSIan Rogers 		const char *expr)
36326226a97SJiri Olsa {
364fa831fbbSIan Rogers 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
36526226a97SJiri Olsa }
36626226a97SJiri Olsa 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)3677e06a5e3SIan Rogers int expr__find_ids(const char *expr, const char *one,
368fa831fbbSIan Rogers 		   struct expr_parse_ctx *ctx)
36926226a97SJiri Olsa {
370fa831fbbSIan Rogers 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
37126226a97SJiri Olsa 
3723fd29fa6SJiri Olsa 	if (one)
3733fd29fa6SJiri Olsa 		expr__del_id(ctx, one);
37426226a97SJiri Olsa 
375ded80bdaSIan Rogers 	return ret;
37626226a97SJiri Olsa }
37729396cd5SIan Rogers 
expr_id_data__value(const struct expr_id_data * data)37829396cd5SIan Rogers double expr_id_data__value(const struct expr_id_data *data)
37929396cd5SIan Rogers {
38029396cd5SIan Rogers 	if (data->kind == EXPR_ID_DATA__VALUE)
3819aba0adaSIan Rogers 		return data->val.val;
38229396cd5SIan Rogers 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
38329396cd5SIan Rogers 	return data->ref.val;
38429396cd5SIan Rogers }
3853613f6c1SIan Rogers 
expr_id_data__source_count(const struct expr_id_data * data)3869aba0adaSIan Rogers double expr_id_data__source_count(const struct expr_id_data *data)
3879aba0adaSIan Rogers {
3889aba0adaSIan Rogers 	assert(data->kind == EXPR_ID_DATA__VALUE);
3899aba0adaSIan Rogers 	return data->val.source_count;
3909aba0adaSIan Rogers }
3919aba0adaSIan Rogers 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)3921725e9cdSIan Rogers double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
3933613f6c1SIan Rogers {
394f56ef30aSIan Rogers 	double result = NAN;
39506905723SIan Rogers 	enum tool_pmu_event ev = tool_pmu__str_to_event(literal + 1);
396fdf1e29bSIan Rogers 
39706905723SIan Rogers 	if (ev != TOOL_PMU__EVENT_NONE) {
39806905723SIan Rogers 		u64 count;
399f0005f17SIan Rogers 
40006905723SIan Rogers 		if (tool_pmu__read_event(ev, &count))
40106905723SIan Rogers 			result = count;
40206905723SIan Rogers 		else
40306905723SIan Rogers 			pr_err("Failure to read '%s'", literal);
404fdf1e29bSIan Rogers 
40506905723SIan Rogers 	} else if (!strcmp("#core_wide", literal)) {
406207f7df7SIan Rogers 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
4071725e9cdSIan Rogers 			? 1.0 : 0.0;
40806905723SIan Rogers 	} else {
40906905723SIan Rogers 		pr_err("Unrecognized literal '%s'", literal);
410c3bf86f1SIan Rogers 	}
411fdf1e29bSIan Rogers 
412f56ef30aSIan Rogers 	pr_debug2("literal: %s = %f\n", literal, result);
413f56ef30aSIan Rogers 	return result;
4143613f6c1SIan Rogers }
4154a4a9bf9SIan Rogers 
4164a4a9bf9SIan Rogers /* Does the event 'id' parse? Determine via ctx->ids if possible. */
expr__has_event(const struct expr_parse_ctx * ctx,bool compute_ids,const char * id)4174a4a9bf9SIan Rogers double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
4184a4a9bf9SIan Rogers {
4194a4a9bf9SIan Rogers 	struct evlist *tmp;
4204a4a9bf9SIan Rogers 	double ret;
4214a4a9bf9SIan Rogers 
4224a4a9bf9SIan Rogers 	if (hashmap__find(ctx->ids, id, /*value=*/NULL))
4234a4a9bf9SIan Rogers 		return 1.0;
4244a4a9bf9SIan Rogers 
4254a4a9bf9SIan Rogers 	if (!compute_ids)
4264a4a9bf9SIan Rogers 		return 0.0;
4274a4a9bf9SIan Rogers 
4284a4a9bf9SIan Rogers 	tmp = evlist__new();
4294a4a9bf9SIan Rogers 	if (!tmp)
4304a4a9bf9SIan Rogers 		return NAN;
4316dd76680SIan Rogers 
4326dd76680SIan Rogers 	if (strchr(id, '@')) {
4336dd76680SIan Rogers 		char *tmp_id, *p;
4346dd76680SIan Rogers 
4356dd76680SIan Rogers 		tmp_id = strdup(id);
4366dd76680SIan Rogers 		if (!tmp_id) {
4376dd76680SIan Rogers 			ret = NAN;
4386dd76680SIan Rogers 			goto out;
4396dd76680SIan Rogers 		}
4406dd76680SIan Rogers 		p = strchr(tmp_id, '@');
4416dd76680SIan Rogers 		*p = '/';
4426dd76680SIan Rogers 		p = strrchr(tmp_id, '@');
4436dd76680SIan Rogers 		*p = '/';
4446dd76680SIan Rogers 		ret = parse_event(tmp, tmp_id) ? 0 : 1;
4456dd76680SIan Rogers 		free(tmp_id);
4466dd76680SIan Rogers 	} else {
4474a4a9bf9SIan Rogers 		ret = parse_event(tmp, id) ? 0 : 1;
4486dd76680SIan Rogers 	}
4496dd76680SIan Rogers out:
4504a4a9bf9SIan Rogers 	evlist__delete(tmp);
4514a4a9bf9SIan Rogers 	return ret;
4524a4a9bf9SIan Rogers }
4539d5da30eSJames Clark 
expr__strcmp_cpuid_str(const struct expr_parse_ctx * ctx __maybe_unused,bool compute_ids __maybe_unused,const char * test_id)4549d5da30eSJames Clark double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
4559d5da30eSJames Clark 		       bool compute_ids __maybe_unused, const char *test_id)
4569d5da30eSJames Clark {
4579d5da30eSJames Clark 	double ret;
458494c403fSIan Rogers 	struct perf_cpu cpu = {-1};
459494c403fSIan Rogers 	char *cpuid = get_cpuid_allow_env_override(cpu);
4609d5da30eSJames Clark 
4619d5da30eSJames Clark 	if (!cpuid)
4629d5da30eSJames Clark 		return NAN;
4639d5da30eSJames Clark 
4649d5da30eSJames Clark 	ret = !strcmp_cpuid_str(test_id, cpuid);
4659d5da30eSJames Clark 
4669d5da30eSJames Clark 	free(cpuid);
4679d5da30eSJames Clark 	return ret;
4689d5da30eSJames Clark }
469