xref: /openbmc/linux/tools/perf/util/expr.c (revision c302378b)
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"
11576a65b6SJiri Olsa #include "expr.h"
1226226a97SJiri Olsa #include "expr-bison.h"
1326226a97SJiri Olsa #include "expr-flex.h"
143613f6c1SIan Rogers #include "smt.h"
15bc2373a5SKan Liang #include "tsc.h"
160a515a06SMiaoqian Lin #include <linux/err.h>
17ded80bdaSIan Rogers #include <linux/kernel.h>
18fc393839SJiri Olsa #include <linux/zalloc.h>
19fc393839SJiri Olsa #include <ctype.h>
203613f6c1SIan Rogers #include <math.h>
2126226a97SJiri Olsa 
2226226a97SJiri Olsa #ifdef PARSER_DEBUG
2326226a97SJiri Olsa extern int expr_debug;
2426226a97SJiri Olsa #endif
25576a65b6SJiri Olsa 
2629396cd5SIan Rogers struct expr_id_data {
2729396cd5SIan Rogers 	union {
289aba0adaSIan Rogers 		struct {
2929396cd5SIan Rogers 			double val;
309aba0adaSIan Rogers 			int source_count;
319aba0adaSIan Rogers 		} val;
3229396cd5SIan Rogers 		struct {
3329396cd5SIan Rogers 			double val;
3429396cd5SIan Rogers 			const char *metric_name;
3529396cd5SIan Rogers 			const char *metric_expr;
3629396cd5SIan Rogers 		} ref;
3729396cd5SIan Rogers 	};
3829396cd5SIan Rogers 
3929396cd5SIan Rogers 	enum {
4029396cd5SIan Rogers 		/* Holding a double value. */
4129396cd5SIan Rogers 		EXPR_ID_DATA__VALUE,
4229396cd5SIan Rogers 		/* Reference to another metric. */
4329396cd5SIan Rogers 		EXPR_ID_DATA__REF,
4429396cd5SIan Rogers 		/* A reference but the value has been computed. */
4529396cd5SIan Rogers 		EXPR_ID_DATA__REF_VALUE,
4629396cd5SIan Rogers 	} kind;
4729396cd5SIan Rogers };
4829396cd5SIan Rogers 
49*c302378bSEduard Zingerman static size_t key_hash(long key, void *ctx __maybe_unused)
50576a65b6SJiri Olsa {
51ded80bdaSIan Rogers 	const char *str = (const char *)key;
52ded80bdaSIan Rogers 	size_t hash = 0;
53576a65b6SJiri Olsa 
54ded80bdaSIan Rogers 	while (*str != '\0') {
55ded80bdaSIan Rogers 		hash *= 31;
56ded80bdaSIan Rogers 		hash += *str;
57ded80bdaSIan Rogers 		str++;
58ded80bdaSIan Rogers 	}
59ded80bdaSIan Rogers 	return hash;
60ded80bdaSIan Rogers }
61ded80bdaSIan Rogers 
62*c302378bSEduard Zingerman static bool key_equal(long key1, long key2, void *ctx __maybe_unused)
63ded80bdaSIan Rogers {
64ded80bdaSIan Rogers 	return !strcmp((const char *)key1, (const char *)key2);
65ded80bdaSIan Rogers }
66ded80bdaSIan Rogers 
67114a9d6eSIan Rogers struct hashmap *ids__new(void)
68114a9d6eSIan Rogers {
699f3c16a4SMiaoqian Lin 	struct hashmap *hash;
709f3c16a4SMiaoqian Lin 
719f3c16a4SMiaoqian Lin 	hash = hashmap__new(key_hash, key_equal, NULL);
729f3c16a4SMiaoqian Lin 	if (IS_ERR(hash))
739f3c16a4SMiaoqian Lin 		return NULL;
749f3c16a4SMiaoqian Lin 	return hash;
75114a9d6eSIan Rogers }
76114a9d6eSIan Rogers 
77114a9d6eSIan Rogers void ids__free(struct hashmap *ids)
78114a9d6eSIan Rogers {
79114a9d6eSIan Rogers 	struct hashmap_entry *cur;
80114a9d6eSIan Rogers 	size_t bkt;
81114a9d6eSIan Rogers 
82114a9d6eSIan Rogers 	if (ids == NULL)
83114a9d6eSIan Rogers 		return;
84114a9d6eSIan Rogers 
85114a9d6eSIan Rogers 	hashmap__for_each_entry(ids, cur, bkt) {
86*c302378bSEduard Zingerman 		free((void *)cur->pkey);
87*c302378bSEduard Zingerman 		free((void *)cur->pvalue);
88114a9d6eSIan Rogers 	}
89114a9d6eSIan Rogers 
90114a9d6eSIan Rogers 	hashmap__free(ids);
91114a9d6eSIan Rogers }
92114a9d6eSIan Rogers 
9380be6434SIan Rogers int ids__insert(struct hashmap *ids, const char *id)
94332603c2SJiri Olsa {
95332603c2SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
96332603c2SJiri Olsa 	char *old_key = NULL;
97332603c2SJiri Olsa 	int ret;
98332603c2SJiri Olsa 
99*c302378bSEduard Zingerman 	ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data);
100332603c2SJiri Olsa 	if (ret)
101332603c2SJiri Olsa 		free(data_ptr);
102332603c2SJiri Olsa 	free(old_key);
103332603c2SJiri Olsa 	free(old_data);
104332603c2SJiri Olsa 	return ret;
105332603c2SJiri Olsa }
106332603c2SJiri Olsa 
107114a9d6eSIan Rogers struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
108114a9d6eSIan Rogers {
109114a9d6eSIan Rogers 	size_t bkt;
110114a9d6eSIan Rogers 	struct hashmap_entry *cur;
111114a9d6eSIan Rogers 	int ret;
112114a9d6eSIan Rogers 	struct expr_id_data *old_data = NULL;
113114a9d6eSIan Rogers 	char *old_key = NULL;
114114a9d6eSIan Rogers 
115114a9d6eSIan Rogers 	if (!ids1)
116114a9d6eSIan Rogers 		return ids2;
117114a9d6eSIan Rogers 
118114a9d6eSIan Rogers 	if (!ids2)
119114a9d6eSIan Rogers 		return ids1;
120114a9d6eSIan Rogers 
121114a9d6eSIan Rogers 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
122114a9d6eSIan Rogers 		struct hashmap *tmp = ids1;
123114a9d6eSIan Rogers 
124114a9d6eSIan Rogers 		ids1 = ids2;
125114a9d6eSIan Rogers 		ids2 = tmp;
126114a9d6eSIan Rogers 	}
127114a9d6eSIan Rogers 	hashmap__for_each_entry(ids2, cur, bkt) {
128*c302378bSEduard Zingerman 		ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data);
129114a9d6eSIan Rogers 		free(old_key);
130114a9d6eSIan Rogers 		free(old_data);
131114a9d6eSIan Rogers 
132114a9d6eSIan Rogers 		if (ret) {
133114a9d6eSIan Rogers 			hashmap__free(ids1);
134114a9d6eSIan Rogers 			hashmap__free(ids2);
135114a9d6eSIan Rogers 			return NULL;
136114a9d6eSIan Rogers 		}
137114a9d6eSIan Rogers 	}
138114a9d6eSIan Rogers 	hashmap__free(ids2);
139114a9d6eSIan Rogers 	return ids1;
140114a9d6eSIan Rogers }
141114a9d6eSIan Rogers 
142114a9d6eSIan Rogers /* Caller must make sure id is allocated */
143114a9d6eSIan Rogers int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
144114a9d6eSIan Rogers {
14580be6434SIan Rogers 	return ids__insert(ctx->ids, id);
146114a9d6eSIan Rogers }
147114a9d6eSIan Rogers 
148332603c2SJiri Olsa /* Caller must make sure id is allocated */
149070b3b5aSJiri Olsa int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
150ded80bdaSIan Rogers {
1519aba0adaSIan Rogers 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
1529aba0adaSIan Rogers }
1539aba0adaSIan Rogers 
1549aba0adaSIan Rogers /* Caller must make sure id is allocated */
1559aba0adaSIan Rogers int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
1569aba0adaSIan Rogers 				  double val, int source_count)
1579aba0adaSIan Rogers {
158070b3b5aSJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
159ded80bdaSIan Rogers 	char *old_key = NULL;
160ded80bdaSIan Rogers 	int ret;
161ded80bdaSIan Rogers 
162070b3b5aSJiri Olsa 	data_ptr = malloc(sizeof(*data_ptr));
163070b3b5aSJiri Olsa 	if (!data_ptr)
164ded80bdaSIan Rogers 		return -ENOMEM;
1659aba0adaSIan Rogers 	data_ptr->val.val = val;
1669aba0adaSIan Rogers 	data_ptr->val.source_count = source_count;
16729396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__VALUE;
168332603c2SJiri Olsa 
169*c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data);
17060e10c00SJiri Olsa 	if (ret)
17160e10c00SJiri Olsa 		free(data_ptr);
172ded80bdaSIan Rogers 	free(old_key);
173070b3b5aSJiri Olsa 	free(old_data);
174ded80bdaSIan Rogers 	return ret;
175ded80bdaSIan Rogers }
176ded80bdaSIan Rogers 
177fc393839SJiri Olsa int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
178fc393839SJiri Olsa {
179fc393839SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
180fc393839SJiri Olsa 	char *old_key = NULL;
181715b824fSIan Rogers 	char *name;
182fc393839SJiri Olsa 	int ret;
183fc393839SJiri Olsa 
184fc393839SJiri Olsa 	data_ptr = zalloc(sizeof(*data_ptr));
185fc393839SJiri Olsa 	if (!data_ptr)
186fc393839SJiri Olsa 		return -ENOMEM;
187fc393839SJiri Olsa 
188fc393839SJiri Olsa 	name = strdup(ref->metric_name);
189fc393839SJiri Olsa 	if (!name) {
190fc393839SJiri Olsa 		free(data_ptr);
191fc393839SJiri Olsa 		return -ENOMEM;
192fc393839SJiri Olsa 	}
193fc393839SJiri Olsa 
194fc393839SJiri Olsa 	/*
195fc393839SJiri Olsa 	 * Intentionally passing just const char pointers,
196fc393839SJiri Olsa 	 * originally from 'struct pmu_event' object.
197fc393839SJiri Olsa 	 * We don't need to change them, so there's no
198fc393839SJiri Olsa 	 * need to create our own copy.
199fc393839SJiri Olsa 	 */
200fc393839SJiri Olsa 	data_ptr->ref.metric_name = ref->metric_name;
201fc393839SJiri Olsa 	data_ptr->ref.metric_expr = ref->metric_expr;
20229396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__REF;
203fc393839SJiri Olsa 
204*c302378bSEduard Zingerman 	ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data);
205fc393839SJiri Olsa 	if (ret)
206fc393839SJiri Olsa 		free(data_ptr);
207fc393839SJiri Olsa 
208fc393839SJiri Olsa 	pr_debug2("adding ref metric %s: %s\n",
209fc393839SJiri Olsa 		  ref->metric_name, ref->metric_expr);
210fc393839SJiri Olsa 
211fc393839SJiri Olsa 	free(old_key);
212fc393839SJiri Olsa 	free(old_data);
213fc393839SJiri Olsa 	return ret;
214fc393839SJiri Olsa }
215fc393839SJiri Olsa 
2165c5f5e83SJiri Olsa int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
2175c5f5e83SJiri Olsa 		 struct expr_id_data **data)
218ded80bdaSIan Rogers {
219*c302378bSEduard Zingerman 	return hashmap__find(ctx->ids, id, data) ? 0 : -1;
220576a65b6SJiri Olsa }
221576a65b6SJiri Olsa 
222798c3f4aSIan Rogers bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
223798c3f4aSIan Rogers 			 struct expr_parse_ctx *needles)
224798c3f4aSIan Rogers {
225798c3f4aSIan Rogers 	struct hashmap_entry *cur;
226798c3f4aSIan Rogers 	size_t bkt;
227798c3f4aSIan Rogers 	struct expr_id_data *data;
228798c3f4aSIan Rogers 
229798c3f4aSIan Rogers 	hashmap__for_each_entry(needles->ids, cur, bkt) {
230*c302378bSEduard Zingerman 		if (expr__get_id(haystack, cur->pkey, &data))
231798c3f4aSIan Rogers 			return false;
232798c3f4aSIan Rogers 	}
233798c3f4aSIan Rogers 	return true;
234798c3f4aSIan Rogers }
235798c3f4aSIan Rogers 
236798c3f4aSIan Rogers 
237acf71b05SJiri Olsa int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
238acf71b05SJiri Olsa 		     struct expr_id_data **datap)
239acf71b05SJiri Olsa {
240acf71b05SJiri Olsa 	struct expr_id_data *data;
241acf71b05SJiri Olsa 
242acf71b05SJiri Olsa 	if (expr__get_id(ctx, id, datap) || !*datap) {
243acf71b05SJiri Olsa 		pr_debug("%s not found\n", id);
244acf71b05SJiri Olsa 		return -1;
245acf71b05SJiri Olsa 	}
246acf71b05SJiri Olsa 
247acf71b05SJiri Olsa 	data = *datap;
248acf71b05SJiri Olsa 
24929396cd5SIan Rogers 	switch (data->kind) {
25029396cd5SIan Rogers 	case EXPR_ID_DATA__VALUE:
2519aba0adaSIan Rogers 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
25229396cd5SIan Rogers 		break;
25329396cd5SIan Rogers 	case EXPR_ID_DATA__REF:
25429396cd5SIan Rogers 		pr_debug2("lookup(%s): ref metric name %s\n", id,
25529396cd5SIan Rogers 			data->ref.metric_name);
256acf71b05SJiri Olsa 		pr_debug("processing metric: %s ENTRY\n", id);
25729396cd5SIan Rogers 		data->kind = EXPR_ID_DATA__REF_VALUE;
258fa831fbbSIan Rogers 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
259acf71b05SJiri Olsa 			pr_debug("%s failed to count\n", id);
260acf71b05SJiri Olsa 			return -1;
261acf71b05SJiri Olsa 		}
2629aba0adaSIan Rogers 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
26329396cd5SIan Rogers 		break;
26429396cd5SIan Rogers 	case EXPR_ID_DATA__REF_VALUE:
26529396cd5SIan Rogers 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
26629396cd5SIan Rogers 			data->ref.val, data->ref.metric_name);
26729396cd5SIan Rogers 		break;
26829396cd5SIan Rogers 	default:
26929396cd5SIan Rogers 		assert(0);  /* Unreachable. */
270acf71b05SJiri Olsa 	}
271acf71b05SJiri Olsa 
272acf71b05SJiri Olsa 	return 0;
273acf71b05SJiri Olsa }
274acf71b05SJiri Olsa 
2753fd29fa6SJiri Olsa void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
2763fd29fa6SJiri Olsa {
2773fd29fa6SJiri Olsa 	struct expr_id_data *old_val = NULL;
2783fd29fa6SJiri Olsa 	char *old_key = NULL;
2793fd29fa6SJiri Olsa 
280*c302378bSEduard Zingerman 	hashmap__delete(ctx->ids, id, &old_key, &old_val);
2813fd29fa6SJiri Olsa 	free(old_key);
2823fd29fa6SJiri Olsa 	free(old_val);
2833fd29fa6SJiri Olsa }
2843fd29fa6SJiri Olsa 
285cb94a02eSIan Rogers struct expr_parse_ctx *expr__ctx_new(void)
286576a65b6SJiri Olsa {
287cb94a02eSIan Rogers 	struct expr_parse_ctx *ctx;
288cb94a02eSIan Rogers 
289cb94a02eSIan Rogers 	ctx = malloc(sizeof(struct expr_parse_ctx));
290cb94a02eSIan Rogers 	if (!ctx)
291cb94a02eSIan Rogers 		return NULL;
292cb94a02eSIan Rogers 
293cb94a02eSIan Rogers 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
2940a515a06SMiaoqian Lin 	if (IS_ERR(ctx->ids)) {
2950a515a06SMiaoqian Lin 		free(ctx);
2960a515a06SMiaoqian Lin 		return NULL;
2970a515a06SMiaoqian Lin 	}
2981725e9cdSIan Rogers 	ctx->sctx.user_requested_cpu_list = NULL;
2991a6abddeSIan Rogers 	ctx->sctx.runtime = 0;
3001725e9cdSIan Rogers 	ctx->sctx.system_wide = false;
30180be6434SIan Rogers 
302cb94a02eSIan Rogers 	return ctx;
303ded80bdaSIan Rogers }
304ded80bdaSIan Rogers 
305ded80bdaSIan Rogers void expr__ctx_clear(struct expr_parse_ctx *ctx)
306ded80bdaSIan Rogers {
307ded80bdaSIan Rogers 	struct hashmap_entry *cur;
308ded80bdaSIan Rogers 	size_t bkt;
309ded80bdaSIan Rogers 
310cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
311*c302378bSEduard Zingerman 		free((void *)cur->pkey);
312*c302378bSEduard Zingerman 		free(cur->pvalue);
313ded80bdaSIan Rogers 	}
314cb94a02eSIan Rogers 	hashmap__clear(ctx->ids);
315cb94a02eSIan Rogers }
316cb94a02eSIan Rogers 
317cb94a02eSIan Rogers void expr__ctx_free(struct expr_parse_ctx *ctx)
318cb94a02eSIan Rogers {
319cb94a02eSIan Rogers 	struct hashmap_entry *cur;
320cb94a02eSIan Rogers 	size_t bkt;
321cb94a02eSIan Rogers 
3221725e9cdSIan Rogers 	if (!ctx)
3231725e9cdSIan Rogers 		return;
3241725e9cdSIan Rogers 
3251725e9cdSIan Rogers 	free(ctx->sctx.user_requested_cpu_list);
326cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
327*c302378bSEduard Zingerman 		free((void *)cur->pkey);
328*c302378bSEduard Zingerman 		free(cur->pvalue);
329cb94a02eSIan Rogers 	}
330cb94a02eSIan Rogers 	hashmap__free(ctx->ids);
331cb94a02eSIan Rogers 	free(ctx);
332576a65b6SJiri Olsa }
33326226a97SJiri Olsa 
33426226a97SJiri Olsa static int
335aecce63eSJiri Olsa __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
336fa831fbbSIan Rogers 	      bool compute_ids)
33726226a97SJiri Olsa {
33826226a97SJiri Olsa 	YY_BUFFER_STATE buffer;
33926226a97SJiri Olsa 	void *scanner;
34026226a97SJiri Olsa 	int ret;
34126226a97SJiri Olsa 
342acf71b05SJiri Olsa 	pr_debug2("parsing metric: %s\n", expr);
343acf71b05SJiri Olsa 
3441a6abddeSIan Rogers 	ret = expr_lex_init_extra(&ctx->sctx, &scanner);
34526226a97SJiri Olsa 	if (ret)
34626226a97SJiri Olsa 		return ret;
34726226a97SJiri Olsa 
34826226a97SJiri Olsa 	buffer = expr__scan_string(expr, scanner);
34926226a97SJiri Olsa 
35026226a97SJiri Olsa #ifdef PARSER_DEBUG
35126226a97SJiri Olsa 	expr_debug = 1;
352e5e0e635SIan Rogers 	expr_set_debug(1, scanner);
35326226a97SJiri Olsa #endif
35426226a97SJiri Olsa 
3553f965a7dSIan Rogers 	ret = expr_parse(val, ctx, compute_ids, scanner);
35626226a97SJiri Olsa 
35726226a97SJiri Olsa 	expr__flush_buffer(buffer, scanner);
35826226a97SJiri Olsa 	expr__delete_buffer(buffer, scanner);
35926226a97SJiri Olsa 	expr_lex_destroy(scanner);
36026226a97SJiri Olsa 	return ret;
36126226a97SJiri Olsa }
36226226a97SJiri Olsa 
363ded80bdaSIan Rogers int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
364fa831fbbSIan Rogers 		const char *expr)
36526226a97SJiri Olsa {
366fa831fbbSIan Rogers 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
36726226a97SJiri Olsa }
36826226a97SJiri Olsa 
3697e06a5e3SIan Rogers int expr__find_ids(const char *expr, const char *one,
370fa831fbbSIan Rogers 		   struct expr_parse_ctx *ctx)
37126226a97SJiri Olsa {
372fa831fbbSIan Rogers 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
37326226a97SJiri Olsa 
3743fd29fa6SJiri Olsa 	if (one)
3753fd29fa6SJiri Olsa 		expr__del_id(ctx, one);
37626226a97SJiri Olsa 
377ded80bdaSIan Rogers 	return ret;
37826226a97SJiri Olsa }
37929396cd5SIan Rogers 
38029396cd5SIan Rogers double expr_id_data__value(const struct expr_id_data *data)
38129396cd5SIan Rogers {
38229396cd5SIan Rogers 	if (data->kind == EXPR_ID_DATA__VALUE)
3839aba0adaSIan Rogers 		return data->val.val;
38429396cd5SIan Rogers 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
38529396cd5SIan Rogers 	return data->ref.val;
38629396cd5SIan Rogers }
3873613f6c1SIan Rogers 
3889aba0adaSIan Rogers double expr_id_data__source_count(const struct expr_id_data *data)
3899aba0adaSIan Rogers {
3909aba0adaSIan Rogers 	assert(data->kind == EXPR_ID_DATA__VALUE);
3919aba0adaSIan Rogers 	return data->val.source_count;
3929aba0adaSIan Rogers }
3939aba0adaSIan Rogers 
394bc2373a5SKan Liang #if !defined(__i386__) && !defined(__x86_64__)
395bc2373a5SKan Liang double arch_get_tsc_freq(void)
396bc2373a5SKan Liang {
397bc2373a5SKan Liang 	return 0.0;
398bc2373a5SKan Liang }
399bc2373a5SKan Liang #endif
400bc2373a5SKan Liang 
4011725e9cdSIan Rogers double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
4023613f6c1SIan Rogers {
403fdf1e29bSIan Rogers 	static struct cpu_topology *topology;
404f56ef30aSIan Rogers 	double result = NAN;
405fdf1e29bSIan Rogers 
406f56ef30aSIan Rogers 	if (!strcmp("#num_cpus", literal)) {
407f56ef30aSIan Rogers 		result = cpu__max_present_cpu().cpu;
408f56ef30aSIan Rogers 		goto out;
409f56ef30aSIan Rogers 	}
410fdf1e29bSIan Rogers 
411bc2373a5SKan Liang 	if (!strcasecmp("#system_tsc_freq", literal)) {
412bc2373a5SKan Liang 		result = arch_get_tsc_freq();
413bc2373a5SKan Liang 		goto out;
414bc2373a5SKan Liang 	}
415bc2373a5SKan Liang 
416fdf1e29bSIan Rogers 	/*
417fdf1e29bSIan Rogers 	 * Assume that topology strings are consistent, such as CPUs "0-1"
418fdf1e29bSIan Rogers 	 * wouldn't be listed as "0,1", and so after deduplication the number of
419fdf1e29bSIan Rogers 	 * these strings gives an indication of the number of packages, dies,
420fdf1e29bSIan Rogers 	 * etc.
421fdf1e29bSIan Rogers 	 */
422fdf1e29bSIan Rogers 	if (!topology) {
423fdf1e29bSIan Rogers 		topology = cpu_topology__new();
424fdf1e29bSIan Rogers 		if (!topology) {
425fdf1e29bSIan Rogers 			pr_err("Error creating CPU topology");
426f56ef30aSIan Rogers 			goto out;
427fdf1e29bSIan Rogers 		}
428fdf1e29bSIan Rogers 	}
42909b73fe9SIan Rogers 	if (!strcasecmp("#smt_on", literal)) {
43009b73fe9SIan Rogers 		result = smt_on(topology) ? 1.0 : 0.0;
43109b73fe9SIan Rogers 		goto out;
43209b73fe9SIan Rogers 	}
4331725e9cdSIan Rogers 	if (!strcmp("#core_wide", literal)) {
4341725e9cdSIan Rogers 		result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list, topology)
4351725e9cdSIan Rogers 			? 1.0 : 0.0;
4361725e9cdSIan Rogers 		goto out;
4371725e9cdSIan Rogers 	}
438f56ef30aSIan Rogers 	if (!strcmp("#num_packages", literal)) {
439f56ef30aSIan Rogers 		result = topology->package_cpus_lists;
440f56ef30aSIan Rogers 		goto out;
441f56ef30aSIan Rogers 	}
442f56ef30aSIan Rogers 	if (!strcmp("#num_dies", literal)) {
443f56ef30aSIan Rogers 		result = topology->die_cpus_lists;
444f56ef30aSIan Rogers 		goto out;
445f56ef30aSIan Rogers 	}
446f56ef30aSIan Rogers 	if (!strcmp("#num_cores", literal)) {
447f56ef30aSIan Rogers 		result = topology->core_cpus_lists;
448f56ef30aSIan Rogers 		goto out;
449f56ef30aSIan Rogers 	}
450fdf1e29bSIan Rogers 
4513613f6c1SIan Rogers 	pr_err("Unrecognized literal '%s'", literal);
452f56ef30aSIan Rogers out:
453f56ef30aSIan Rogers 	pr_debug2("literal: %s = %f\n", literal, result);
454f56ef30aSIan Rogers 	return result;
4553613f6c1SIan Rogers }
456