xref: /openbmc/linux/tools/perf/util/expr.c (revision f989dc00)
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"
8fdf1e29bSIan Rogers #include "cpumap.h"
9fdf1e29bSIan Rogers #include "cputopo.h"
10fc393839SJiri Olsa #include "debug.h"
114a4a9bf9SIan Rogers #include "evlist.h"
12576a65b6SJiri Olsa #include "expr.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"
183613f6c1SIan Rogers #include "smt.h"
19bc2373a5SKan Liang #include "tsc.h"
20c3bf86f1SIan Rogers #include <api/fs/fs.h>
210a515a06SMiaoqian Lin #include <linux/err.h>
22ded80bdaSIan Rogers #include <linux/kernel.h>
23fc393839SJiri Olsa #include <linux/zalloc.h>
24fc393839SJiri Olsa #include <ctype.h>
253613f6c1SIan Rogers #include <math.h>
26acef233bSJing Zhang #include "pmu.h"
2726226a97SJiri Olsa 
2826226a97SJiri Olsa #ifdef PARSER_DEBUG
2926226a97SJiri Olsa extern int expr_debug;
3026226a97SJiri Olsa #endif
31576a65b6SJiri Olsa 
3229396cd5SIan Rogers struct expr_id_data {
3329396cd5SIan Rogers 	union {
349aba0adaSIan Rogers 		struct {
3529396cd5SIan Rogers 			double val;
369aba0adaSIan Rogers 			int source_count;
379aba0adaSIan Rogers 		} val;
3829396cd5SIan Rogers 		struct {
3929396cd5SIan Rogers 			double val;
4029396cd5SIan Rogers 			const char *metric_name;
4129396cd5SIan Rogers 			const char *metric_expr;
4229396cd5SIan Rogers 		} ref;
4329396cd5SIan Rogers 	};
4429396cd5SIan Rogers 
4529396cd5SIan Rogers 	enum {
4629396cd5SIan Rogers 		/* Holding a double value. */
4729396cd5SIan Rogers 		EXPR_ID_DATA__VALUE,
4829396cd5SIan Rogers 		/* Reference to another metric. */
4929396cd5SIan Rogers 		EXPR_ID_DATA__REF,
5029396cd5SIan Rogers 		/* A reference but the value has been computed. */
5129396cd5SIan Rogers 		EXPR_ID_DATA__REF_VALUE,
5229396cd5SIan Rogers 	} kind;
5329396cd5SIan Rogers };
5429396cd5SIan Rogers 
key_hash(long key,void * ctx __maybe_unused)55c302378bSEduard Zingerman static size_t key_hash(long key, void *ctx __maybe_unused)
56576a65b6SJiri Olsa {
57ded80bdaSIan Rogers 	const char *str = (const char *)key;
58ded80bdaSIan Rogers 	size_t hash = 0;
59576a65b6SJiri Olsa 
60ded80bdaSIan Rogers 	while (*str != '\0') {
61ded80bdaSIan Rogers 		hash *= 31;
62ded80bdaSIan Rogers 		hash += *str;
63ded80bdaSIan Rogers 		str++;
64ded80bdaSIan Rogers 	}
65ded80bdaSIan Rogers 	return hash;
66ded80bdaSIan Rogers }
67ded80bdaSIan Rogers 
key_equal(long key1,long key2,void * ctx __maybe_unused)68c302378bSEduard Zingerman static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
69ded80bdaSIan Rogers {
70ded80bdaSIan Rogers 	return !strcmp((const char *)key1, (const char *)key2);
71ded80bdaSIan Rogers }
72ded80bdaSIan Rogers 
ids__new(void)73114a9d6eSIan Rogers struct hashmap *ids__new(void)
74114a9d6eSIan Rogers {
759f3c16a4SMiaoqian Lin 	struct hashmap *hash;
769f3c16a4SMiaoqian Lin 
779f3c16a4SMiaoqian Lin 	hash = hashmap__new(key_hash, key_equal, NULL);
789f3c16a4SMiaoqian Lin 	if (IS_ERR(hash))
799f3c16a4SMiaoqian Lin 		return NULL;
809f3c16a4SMiaoqian Lin 	return hash;
81114a9d6eSIan Rogers }
82114a9d6eSIan Rogers 
ids__free(struct hashmap * ids)83114a9d6eSIan Rogers void ids__free(struct hashmap *ids)
84114a9d6eSIan Rogers {
85114a9d6eSIan Rogers 	struct hashmap_entry *cur;
86114a9d6eSIan Rogers 	size_t bkt;
87114a9d6eSIan Rogers 
88114a9d6eSIan Rogers 	if (ids == NULL)
89114a9d6eSIan Rogers 		return;
90114a9d6eSIan Rogers 
91114a9d6eSIan Rogers 	hashmap__for_each_entry(ids, cur, bkt) {
92a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
93a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
94114a9d6eSIan Rogers 	}
95114a9d6eSIan Rogers 
96114a9d6eSIan Rogers 	hashmap__free(ids);
97114a9d6eSIan Rogers }
98114a9d6eSIan Rogers 
ids__insert(struct hashmap * ids,const char * id)9980be6434SIan Rogers int ids__insert(struct hashmap *ids, const char *id)
100332603c2SJiri Olsa {
101332603c2SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
102332603c2SJiri Olsa 	char *old_key = NULL;
103332603c2SJiri Olsa 	int ret;
104332603c2SJiri Olsa 
105c302378bSEduard Zingerman 	ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
106332603c2SJiri Olsa 	if (ret)
107332603c2SJiri Olsa 		free(data_ptr);
108332603c2SJiri Olsa 	free(old_key);
109332603c2SJiri Olsa 	free(old_data);
110332603c2SJiri Olsa 	return ret;
111332603c2SJiri Olsa }
112332603c2SJiri Olsa 
ids__union(struct hashmap * ids1,struct hashmap * ids2)113114a9d6eSIan Rogers struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
114114a9d6eSIan Rogers {
115114a9d6eSIan Rogers 	size_t bkt;
116114a9d6eSIan Rogers 	struct hashmap_entry *cur;
117114a9d6eSIan Rogers 	int ret;
118114a9d6eSIan Rogers 	struct expr_id_data *old_data = NULL;
119114a9d6eSIan Rogers 	char *old_key = NULL;
120114a9d6eSIan Rogers 
121114a9d6eSIan Rogers 	if (!ids1)
122114a9d6eSIan Rogers 		return ids2;
123114a9d6eSIan Rogers 
124114a9d6eSIan Rogers 	if (!ids2)
125114a9d6eSIan Rogers 		return ids1;
126114a9d6eSIan Rogers 
127114a9d6eSIan Rogers 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
128114a9d6eSIan Rogers 		struct hashmap *tmp = ids1;
129114a9d6eSIan Rogers 
130114a9d6eSIan Rogers 		ids1 = ids2;
131114a9d6eSIan Rogers 		ids2 = tmp;
132114a9d6eSIan Rogers 	}
133114a9d6eSIan Rogers 	hashmap__for_each_entry(ids2, cur, bkt) {
134c302378bSEduard Zingerman 		ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
135114a9d6eSIan Rogers 		free(old_key);
136114a9d6eSIan Rogers 		free(old_data);
137114a9d6eSIan Rogers 
138114a9d6eSIan Rogers 		if (ret) {
139114a9d6eSIan Rogers 			hashmap__free(ids1);
140114a9d6eSIan Rogers 			hashmap__free(ids2);
141114a9d6eSIan Rogers 			return NULL;
142114a9d6eSIan Rogers 		}
143114a9d6eSIan Rogers 	}
144114a9d6eSIan Rogers 	hashmap__free(ids2);
145114a9d6eSIan Rogers 	return ids1;
146114a9d6eSIan Rogers }
147114a9d6eSIan Rogers 
148114a9d6eSIan Rogers /* Caller must make sure id is allocated */
expr__add_id(struct expr_parse_ctx * ctx,const char * id)149114a9d6eSIan Rogers int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
150114a9d6eSIan Rogers {
15180be6434SIan Rogers 	return ids__insert(ctx->ids, id);
152114a9d6eSIan Rogers }
153114a9d6eSIan Rogers 
154332603c2SJiri Olsa /* Caller must make sure id is allocated */
expr__add_id_val(struct expr_parse_ctx * ctx,const char * id,double val)155070b3b5aSJiri Olsa int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
156ded80bdaSIan Rogers {
1579aba0adaSIan Rogers 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
1589aba0adaSIan Rogers }
1599aba0adaSIan Rogers 
1609aba0adaSIan 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)1619aba0adaSIan Rogers int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
1629aba0adaSIan Rogers 				  double val, int source_count)
1639aba0adaSIan Rogers {
164070b3b5aSJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
165ded80bdaSIan Rogers 	char *old_key = NULL;
166ded80bdaSIan Rogers 	int ret;
167ded80bdaSIan Rogers 
168070b3b5aSJiri Olsa 	data_ptr = malloc(sizeof(*data_ptr));
169070b3b5aSJiri Olsa 	if (!data_ptr)
170ded80bdaSIan Rogers 		return -ENOMEM;
1719aba0adaSIan Rogers 	data_ptr->val.val = val;
1729aba0adaSIan Rogers 	data_ptr->val.source_count = source_count;
17329396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__VALUE;
174332603c2SJiri Olsa 
175c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
17660e10c00SJiri Olsa 	if (ret)
17760e10c00SJiri Olsa 		free(data_ptr);
178ded80bdaSIan Rogers 	free(old_key);
179070b3b5aSJiri Olsa 	free(old_data);
180ded80bdaSIan Rogers 	return ret;
181ded80bdaSIan Rogers }
182ded80bdaSIan Rogers 
expr__add_ref(struct expr_parse_ctx * ctx,struct metric_ref * ref)183fc393839SJiri Olsa int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
184fc393839SJiri Olsa {
185fc393839SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
186fc393839SJiri Olsa 	char *old_key = NULL;
187715b824fSIan Rogers 	char *name;
188fc393839SJiri Olsa 	int ret;
189fc393839SJiri Olsa 
190fc393839SJiri Olsa 	data_ptr = zalloc(sizeof(*data_ptr));
191fc393839SJiri Olsa 	if (!data_ptr)
192fc393839SJiri Olsa 		return -ENOMEM;
193fc393839SJiri Olsa 
194fc393839SJiri Olsa 	name = strdup(ref->metric_name);
195fc393839SJiri Olsa 	if (!name) {
196fc393839SJiri Olsa 		free(data_ptr);
197fc393839SJiri Olsa 		return -ENOMEM;
198fc393839SJiri Olsa 	}
199fc393839SJiri Olsa 
200fc393839SJiri Olsa 	/*
201fc393839SJiri Olsa 	 * Intentionally passing just const char pointers,
202fc393839SJiri Olsa 	 * originally from 'struct pmu_event' object.
203fc393839SJiri Olsa 	 * We don't need to change them, so there's no
204fc393839SJiri Olsa 	 * need to create our own copy.
205fc393839SJiri Olsa 	 */
206fc393839SJiri Olsa 	data_ptr->ref.metric_name = ref->metric_name;
207fc393839SJiri Olsa 	data_ptr->ref.metric_expr = ref->metric_expr;
20829396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__REF;
209fc393839SJiri Olsa 
210c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
211fc393839SJiri Olsa 	if (ret)
212fc393839SJiri Olsa 		free(data_ptr);
213fc393839SJiri Olsa 
214fc393839SJiri Olsa 	pr_debug2("adding ref metric %s: %s\n",
215fc393839SJiri Olsa 		  ref->metric_name, ref->metric_expr);
216fc393839SJiri Olsa 
217fc393839SJiri Olsa 	free(old_key);
218fc393839SJiri Olsa 	free(old_data);
219fc393839SJiri Olsa 	return ret;
220fc393839SJiri Olsa }
221fc393839SJiri Olsa 
expr__get_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** data)2225c5f5e83SJiri Olsa int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
2235c5f5e83SJiri Olsa 		 struct expr_id_data **data)
224ded80bdaSIan Rogers {
225c302378bSEduard Zingerman 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
226576a65b6SJiri Olsa }
227576a65b6SJiri Olsa 
expr__subset_of_ids(struct expr_parse_ctx * haystack,struct expr_parse_ctx * needles)228798c3f4aSIan Rogers bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
229798c3f4aSIan Rogers 			 struct expr_parse_ctx *needles)
230798c3f4aSIan Rogers {
231798c3f4aSIan Rogers 	struct hashmap_entry *cur;
232798c3f4aSIan Rogers 	size_t bkt;
233798c3f4aSIan Rogers 	struct expr_id_data *data;
234798c3f4aSIan Rogers 
235798c3f4aSIan Rogers 	hashmap__for_each_entry(needles->ids, cur, bkt) {
236c302378bSEduard Zingerman 		if (expr__get_id(haystack, cur->pkey, &data))
237798c3f4aSIan Rogers 			return false;
238798c3f4aSIan Rogers 	}
239798c3f4aSIan Rogers 	return true;
240798c3f4aSIan Rogers }
241798c3f4aSIan Rogers 
242798c3f4aSIan Rogers 
expr__resolve_id(struct expr_parse_ctx * ctx,const char * id,struct expr_id_data ** datap)243acf71b05SJiri Olsa int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
244acf71b05SJiri Olsa 		     struct expr_id_data **datap)
245acf71b05SJiri Olsa {
246acf71b05SJiri Olsa 	struct expr_id_data *data;
247acf71b05SJiri Olsa 
248acf71b05SJiri Olsa 	if (expr__get_id(ctx, id, datap) || !*datap) {
249acf71b05SJiri Olsa 		pr_debug("%s not found\n", id);
250acf71b05SJiri Olsa 		return -1;
251acf71b05SJiri Olsa 	}
252acf71b05SJiri Olsa 
253acf71b05SJiri Olsa 	data = *datap;
254acf71b05SJiri Olsa 
25529396cd5SIan Rogers 	switch (data->kind) {
25629396cd5SIan Rogers 	case EXPR_ID_DATA__VALUE:
2579aba0adaSIan Rogers 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
25829396cd5SIan Rogers 		break;
25929396cd5SIan Rogers 	case EXPR_ID_DATA__REF:
26029396cd5SIan Rogers 		pr_debug2("lookup(%s): ref metric name %s\n", id,
26129396cd5SIan Rogers 			data->ref.metric_name);
262acf71b05SJiri Olsa 		pr_debug("processing metric: %s ENTRY\n", id);
26329396cd5SIan Rogers 		data->kind = EXPR_ID_DATA__REF_VALUE;
264fa831fbbSIan Rogers 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
265acf71b05SJiri Olsa 			pr_debug("%s failed to count\n", id);
266acf71b05SJiri Olsa 			return -1;
267acf71b05SJiri Olsa 		}
2689aba0adaSIan Rogers 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
26929396cd5SIan Rogers 		break;
27029396cd5SIan Rogers 	case EXPR_ID_DATA__REF_VALUE:
27129396cd5SIan Rogers 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
27229396cd5SIan Rogers 			data->ref.val, data->ref.metric_name);
27329396cd5SIan Rogers 		break;
27429396cd5SIan Rogers 	default:
27529396cd5SIan Rogers 		assert(0);  /* Unreachable. */
276acf71b05SJiri Olsa 	}
277acf71b05SJiri Olsa 
278acf71b05SJiri Olsa 	return 0;
279acf71b05SJiri Olsa }
280acf71b05SJiri Olsa 
expr__del_id(struct expr_parse_ctx * ctx,const char * id)2813fd29fa6SJiri Olsa void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
2823fd29fa6SJiri Olsa {
2833fd29fa6SJiri Olsa 	struct expr_id_data *old_val = NULL;
2843fd29fa6SJiri Olsa 	char *old_key = NULL;
2853fd29fa6SJiri Olsa 
286c302378bSEduard Zingerman 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
2873fd29fa6SJiri Olsa 	free(old_key);
2883fd29fa6SJiri Olsa 	free(old_val);
2893fd29fa6SJiri Olsa }
2903fd29fa6SJiri Olsa 
expr__ctx_new(void)291cb94a02eSIan Rogers struct expr_parse_ctx *expr__ctx_new(void)
292576a65b6SJiri Olsa {
293cb94a02eSIan Rogers 	struct expr_parse_ctx *ctx;
294cb94a02eSIan Rogers 
295cb94a02eSIan Rogers 	ctx = malloc(sizeof(struct expr_parse_ctx));
296cb94a02eSIan Rogers 	if (!ctx)
297cb94a02eSIan Rogers 		return NULL;
298cb94a02eSIan Rogers 
299cb94a02eSIan Rogers 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
3000a515a06SMiaoqian Lin 	if (IS_ERR(ctx->ids)) {
3010a515a06SMiaoqian Lin 		free(ctx);
3020a515a06SMiaoqian Lin 		return NULL;
3030a515a06SMiaoqian Lin 	}
3041725e9cdSIan Rogers 	ctx->sctx.user_requested_cpu_list = NULL;
3051a6abddeSIan Rogers 	ctx->sctx.runtime = 0;
3061725e9cdSIan Rogers 	ctx->sctx.system_wide = false;
30780be6434SIan Rogers 
308cb94a02eSIan Rogers 	return ctx;
309ded80bdaSIan Rogers }
310ded80bdaSIan Rogers 
expr__ctx_clear(struct expr_parse_ctx * ctx)311ded80bdaSIan Rogers void expr__ctx_clear(struct expr_parse_ctx *ctx)
312ded80bdaSIan Rogers {
313ded80bdaSIan Rogers 	struct hashmap_entry *cur;
314ded80bdaSIan Rogers 	size_t bkt;
315ded80bdaSIan Rogers 
316cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
317a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
318a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
319ded80bdaSIan Rogers 	}
320cb94a02eSIan Rogers 	hashmap__clear(ctx->ids);
321cb94a02eSIan Rogers }
322cb94a02eSIan Rogers 
expr__ctx_free(struct expr_parse_ctx * ctx)323cb94a02eSIan Rogers void expr__ctx_free(struct expr_parse_ctx *ctx)
324cb94a02eSIan Rogers {
325cb94a02eSIan Rogers 	struct hashmap_entry *cur;
326cb94a02eSIan Rogers 	size_t bkt;
327cb94a02eSIan Rogers 
3281725e9cdSIan Rogers 	if (!ctx)
3291725e9cdSIan Rogers 		return;
3301725e9cdSIan Rogers 
331a77f8184SArnaldo Carvalho de Melo 	zfree(&ctx->sctx.user_requested_cpu_list);
332cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
333a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pkey);
334a77f8184SArnaldo Carvalho de Melo 		zfree(&cur->pvalue);
335cb94a02eSIan Rogers 	}
336cb94a02eSIan Rogers 	hashmap__free(ctx->ids);
337cb94a02eSIan Rogers 	free(ctx);
338576a65b6SJiri Olsa }
33926226a97SJiri Olsa 
34026226a97SJiri Olsa static int
__expr__parse(double * val,struct expr_parse_ctx * ctx,const char * expr,bool compute_ids)341aecce63eSJiri Olsa __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
342fa831fbbSIan Rogers 	      bool compute_ids)
34326226a97SJiri Olsa {
34426226a97SJiri Olsa 	YY_BUFFER_STATE buffer;
34526226a97SJiri Olsa 	void *scanner;
34626226a97SJiri Olsa 	int ret;
34726226a97SJiri Olsa 
348acf71b05SJiri Olsa 	pr_debug2("parsing metric: %s\n", expr);
349acf71b05SJiri Olsa 
3501a6abddeSIan Rogers 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
35126226a97SJiri Olsa 	if (ret)
35226226a97SJiri Olsa 		return ret;
35326226a97SJiri Olsa 
35426226a97SJiri Olsa 	buffer = expr__scan_string(expr, scanner);
35526226a97SJiri Olsa 
35626226a97SJiri Olsa #ifdef PARSER_DEBUG
35726226a97SJiri Olsa 	expr_debug = 1;
358e5e0e635SIan Rogers 	expr_set_debug(1, scanner);
35926226a97SJiri Olsa #endif
36026226a97SJiri Olsa 
3613f965a7dSIan Rogers 	ret = expr_parse(val, ctx, compute_ids, scanner);
36226226a97SJiri Olsa 
36326226a97SJiri Olsa 	expr__flush_buffer(buffer, scanner);
36426226a97SJiri Olsa 	expr__delete_buffer(buffer, scanner);
36526226a97SJiri Olsa 	expr_lex_destroy(scanner);
36626226a97SJiri Olsa 	return ret;
36726226a97SJiri Olsa }
36826226a97SJiri Olsa 
expr__parse(double * final_val,struct expr_parse_ctx * ctx,const char * expr)369ded80bdaSIan Rogers int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
370fa831fbbSIan Rogers 		const char *expr)
37126226a97SJiri Olsa {
372fa831fbbSIan Rogers 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
37326226a97SJiri Olsa }
37426226a97SJiri Olsa 
expr__find_ids(const char * expr,const char * one,struct expr_parse_ctx * ctx)3757e06a5e3SIan Rogers int expr__find_ids(const char *expr, const char *one,
376fa831fbbSIan Rogers 		   struct expr_parse_ctx *ctx)
37726226a97SJiri Olsa {
378fa831fbbSIan Rogers 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
37926226a97SJiri Olsa 
3803fd29fa6SJiri Olsa 	if (one)
3813fd29fa6SJiri Olsa 		expr__del_id(ctx, one);
38226226a97SJiri Olsa 
383ded80bdaSIan Rogers 	return ret;
38426226a97SJiri Olsa }
38529396cd5SIan Rogers 
expr_id_data__value(const struct expr_id_data * data)38629396cd5SIan Rogers double expr_id_data__value(const struct expr_id_data *data)
38729396cd5SIan Rogers {
38829396cd5SIan Rogers 	if (data->kind == EXPR_ID_DATA__VALUE)
3899aba0adaSIan Rogers 		return data->val.val;
39029396cd5SIan Rogers 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
39129396cd5SIan Rogers 	return data->ref.val;
39229396cd5SIan Rogers }
3933613f6c1SIan Rogers 
expr_id_data__source_count(const struct expr_id_data * data)3949aba0adaSIan Rogers double expr_id_data__source_count(const struct expr_id_data *data)
3959aba0adaSIan Rogers {
3969aba0adaSIan Rogers 	assert(data->kind == EXPR_ID_DATA__VALUE);
3979aba0adaSIan Rogers 	return data->val.source_count;
3989aba0adaSIan Rogers }
3999aba0adaSIan Rogers 
400bc2373a5SKan Liang #if !defined(__i386__) && !defined(__x86_64__)
arch_get_tsc_freq(void)401bc2373a5SKan Liang double arch_get_tsc_freq(void)
402bc2373a5SKan Liang {
403bc2373a5SKan Liang 	return 0.0;
404bc2373a5SKan Liang }
405bc2373a5SKan Liang #endif
406bc2373a5SKan Liang 
has_pmem(void)407c3bf86f1SIan Rogers static double has_pmem(void)
408c3bf86f1SIan Rogers {
409c3bf86f1SIan Rogers 	static bool has_pmem, cached;
410c3bf86f1SIan Rogers 	const char *sysfs = sysfs__mountpoint();
411c3bf86f1SIan Rogers 	char path[PATH_MAX];
412c3bf86f1SIan Rogers 
413c3bf86f1SIan Rogers 	if (!cached) {
414c3bf86f1SIan Rogers 		snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs);
415c3bf86f1SIan Rogers 		has_pmem = access(path, F_OK) == 0;
416c3bf86f1SIan Rogers 		cached = true;
417c3bf86f1SIan Rogers 	}
418c3bf86f1SIan Rogers 	return has_pmem ? 1.0 : 0.0;
419c3bf86f1SIan Rogers }
420c3bf86f1SIan Rogers 
expr__get_literal(const char * literal,const struct expr_scanner_ctx * ctx)4211725e9cdSIan Rogers double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
4223613f6c1SIan Rogers {
423207f7df7SIan Rogers 	const struct cpu_topology *topology;
424f56ef30aSIan Rogers 	double result = NAN;
425fdf1e29bSIan Rogers 
426f56ef30aSIan Rogers 	if (!strcmp("#num_cpus", literal)) {
427f56ef30aSIan Rogers 		result = cpu__max_present_cpu().cpu;
428f56ef30aSIan Rogers 		goto out;
429f56ef30aSIan Rogers 	}
430f0005f17SIan Rogers 	if (!strcmp("#num_cpus_online", literal)) {
431f0005f17SIan Rogers 		struct perf_cpu_map *online = cpu_map__online();
432f0005f17SIan Rogers 
433f0005f17SIan Rogers 		if (online)
434f0005f17SIan Rogers 			result = perf_cpu_map__nr(online);
435f0005f17SIan Rogers 		goto out;
436f0005f17SIan Rogers 	}
437fdf1e29bSIan Rogers 
438bc2373a5SKan Liang 	if (!strcasecmp("#system_tsc_freq", literal)) {
439bc2373a5SKan Liang 		result = arch_get_tsc_freq();
440bc2373a5SKan Liang 		goto out;
441bc2373a5SKan Liang 	}
442bc2373a5SKan Liang 
443fdf1e29bSIan Rogers 	/*
444fdf1e29bSIan Rogers 	 * Assume that topology strings are consistent, such as CPUs "0-1"
445fdf1e29bSIan Rogers 	 * wouldn't be listed as "0,1", and so after deduplication the number of
446fdf1e29bSIan Rogers 	 * these strings gives an indication of the number of packages, dies,
447fdf1e29bSIan Rogers 	 * etc.
448fdf1e29bSIan Rogers 	 */
44909b73fe9SIan Rogers 	if (!strcasecmp("#smt_on", literal)) {
450207f7df7SIan Rogers 		result = smt_on() ? 1.0 : 0.0;
45109b73fe9SIan Rogers 		goto out;
45209b73fe9SIan Rogers 	}
4531725e9cdSIan Rogers 	if (!strcmp("#core_wide", literal)) {
454207f7df7SIan Rogers 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list)
4551725e9cdSIan Rogers 			? 1.0 : 0.0;
4561725e9cdSIan Rogers 		goto out;
4571725e9cdSIan Rogers 	}
458f56ef30aSIan Rogers 	if (!strcmp("#num_packages", literal)) {
459207f7df7SIan Rogers 		topology = online_topology();
460f56ef30aSIan Rogers 		result = topology->package_cpus_lists;
461f56ef30aSIan Rogers 		goto out;
462f56ef30aSIan Rogers 	}
463f56ef30aSIan Rogers 	if (!strcmp("#num_dies", literal)) {
464207f7df7SIan Rogers 		topology = online_topology();
465f56ef30aSIan Rogers 		result = topology->die_cpus_lists;
466f56ef30aSIan Rogers 		goto out;
467f56ef30aSIan Rogers 	}
468f56ef30aSIan Rogers 	if (!strcmp("#num_cores", literal)) {
469207f7df7SIan Rogers 		topology = online_topology();
470f56ef30aSIan Rogers 		result = topology->core_cpus_lists;
471f56ef30aSIan Rogers 		goto out;
472f56ef30aSIan Rogers 	}
473acef233bSJing Zhang 	if (!strcmp("#slots", literal)) {
474acef233bSJing Zhang 		result = perf_pmu__cpu_slots_per_cycle();
475acef233bSJing Zhang 		goto out;
476acef233bSJing Zhang 	}
477c3bf86f1SIan Rogers 	if (!strcmp("#has_pmem", literal)) {
478c3bf86f1SIan Rogers 		result = has_pmem();
479c3bf86f1SIan Rogers 		goto out;
480c3bf86f1SIan Rogers 	}
481fdf1e29bSIan Rogers 
4823613f6c1SIan Rogers 	pr_err("Unrecognized literal '%s'", literal);
483f56ef30aSIan Rogers out:
484f56ef30aSIan Rogers 	pr_debug2("literal: %s = %f\n", literal, result);
485f56ef30aSIan Rogers 	return result;
4863613f6c1SIan Rogers }
4874a4a9bf9SIan Rogers 
4884a4a9bf9SIan 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)4894a4a9bf9SIan Rogers double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id)
4904a4a9bf9SIan Rogers {
4914a4a9bf9SIan Rogers 	struct evlist *tmp;
4924a4a9bf9SIan Rogers 	double ret;
4934a4a9bf9SIan Rogers 
4944a4a9bf9SIan Rogers 	if (hashmap__find(ctx->ids, id, /*value=*/NULL))
4954a4a9bf9SIan Rogers 		return 1.0;
4964a4a9bf9SIan Rogers 
4974a4a9bf9SIan Rogers 	if (!compute_ids)
4984a4a9bf9SIan Rogers 		return 0.0;
4994a4a9bf9SIan Rogers 
5004a4a9bf9SIan Rogers 	tmp = evlist__new();
5014a4a9bf9SIan Rogers 	if (!tmp)
5024a4a9bf9SIan Rogers 		return NAN;
503e40ef597SIan Rogers 
504e40ef597SIan Rogers 	if (strchr(id, '@')) {
505e40ef597SIan Rogers 		char *tmp_id, *p;
506e40ef597SIan Rogers 
507e40ef597SIan Rogers 		tmp_id = strdup(id);
508e40ef597SIan Rogers 		if (!tmp_id) {
509e40ef597SIan Rogers 			ret = NAN;
510e40ef597SIan Rogers 			goto out;
511e40ef597SIan Rogers 		}
512e40ef597SIan Rogers 		p = strchr(tmp_id, '@');
513e40ef597SIan Rogers 		*p = '/';
514e40ef597SIan Rogers 		p = strrchr(tmp_id, '@');
515e40ef597SIan Rogers 		*p = '/';
516e40ef597SIan Rogers 		ret = parse_event(tmp, tmp_id) ? 0 : 1;
517e40ef597SIan Rogers 		free(tmp_id);
518e40ef597SIan Rogers 	} else {
5194a4a9bf9SIan Rogers 		ret = parse_event(tmp, id) ? 0 : 1;
520e40ef597SIan Rogers 	}
521e40ef597SIan Rogers out:
5224a4a9bf9SIan Rogers 	evlist__delete(tmp);
5234a4a9bf9SIan Rogers 	return ret;
5244a4a9bf9SIan Rogers }
5259d5da30eSJames Clark 
expr__strcmp_cpuid_str(const struct expr_parse_ctx * ctx __maybe_unused,bool compute_ids __maybe_unused,const char * test_id)5269d5da30eSJames Clark double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
5279d5da30eSJames Clark 		       bool compute_ids __maybe_unused, const char *test_id)
5289d5da30eSJames Clark {
5299d5da30eSJames Clark 	double ret;
530f989dc00SJames Clark 	struct perf_pmu *pmu = perf_pmus__find_core_pmu();
5319d5da30eSJames Clark 	char *cpuid = perf_pmu__getcpuid(pmu);
5329d5da30eSJames Clark 
5339d5da30eSJames Clark 	if (!cpuid)
5349d5da30eSJames Clark 		return NAN;
5359d5da30eSJames Clark 
5369d5da30eSJames Clark 	ret = !strcmp_cpuid_str(test_id, cpuid);
5379d5da30eSJames Clark 
5389d5da30eSJames Clark 	free(cpuid);
5399d5da30eSJames Clark 	return ret;
5409d5da30eSJames Clark }
541