xref: /openbmc/linux/tools/perf/util/expr.c (revision f56ef30a)
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"
150a515a06SMiaoqian Lin #include <linux/err.h>
16ded80bdaSIan Rogers #include <linux/kernel.h>
17fc393839SJiri Olsa #include <linux/zalloc.h>
18fc393839SJiri Olsa #include <ctype.h>
193613f6c1SIan Rogers #include <math.h>
2026226a97SJiri Olsa 
2126226a97SJiri Olsa #ifdef PARSER_DEBUG
2226226a97SJiri Olsa extern int expr_debug;
2326226a97SJiri Olsa #endif
24576a65b6SJiri 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 
48ded80bdaSIan Rogers static size_t key_hash(const void *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 
61ded80bdaSIan Rogers static bool key_equal(const void *key1, const void *key2,
62ded80bdaSIan Rogers 		    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) {
86114a9d6eSIan Rogers 		free((char *)cur->key);
87114a9d6eSIan Rogers 		free(cur->value);
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 
99114a9d6eSIan Rogers 	ret = hashmap__set(ids, id, data_ptr,
100332603c2SJiri Olsa 			   (const void **)&old_key, (void **)&old_data);
101332603c2SJiri Olsa 	if (ret)
102332603c2SJiri Olsa 		free(data_ptr);
103332603c2SJiri Olsa 	free(old_key);
104332603c2SJiri Olsa 	free(old_data);
105332603c2SJiri Olsa 	return ret;
106332603c2SJiri Olsa }
107332603c2SJiri Olsa 
108114a9d6eSIan Rogers struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
109114a9d6eSIan Rogers {
110114a9d6eSIan Rogers 	size_t bkt;
111114a9d6eSIan Rogers 	struct hashmap_entry *cur;
112114a9d6eSIan Rogers 	int ret;
113114a9d6eSIan Rogers 	struct expr_id_data *old_data = NULL;
114114a9d6eSIan Rogers 	char *old_key = NULL;
115114a9d6eSIan Rogers 
116114a9d6eSIan Rogers 	if (!ids1)
117114a9d6eSIan Rogers 		return ids2;
118114a9d6eSIan Rogers 
119114a9d6eSIan Rogers 	if (!ids2)
120114a9d6eSIan Rogers 		return ids1;
121114a9d6eSIan Rogers 
122114a9d6eSIan Rogers 	if (hashmap__size(ids1) <  hashmap__size(ids2)) {
123114a9d6eSIan Rogers 		struct hashmap *tmp = ids1;
124114a9d6eSIan Rogers 
125114a9d6eSIan Rogers 		ids1 = ids2;
126114a9d6eSIan Rogers 		ids2 = tmp;
127114a9d6eSIan Rogers 	}
128114a9d6eSIan Rogers 	hashmap__for_each_entry(ids2, cur, bkt) {
129114a9d6eSIan Rogers 		ret = hashmap__set(ids1, cur->key, cur->value,
130114a9d6eSIan Rogers 				(const void **)&old_key, (void **)&old_data);
131114a9d6eSIan Rogers 		free(old_key);
132114a9d6eSIan Rogers 		free(old_data);
133114a9d6eSIan Rogers 
134114a9d6eSIan Rogers 		if (ret) {
135114a9d6eSIan Rogers 			hashmap__free(ids1);
136114a9d6eSIan Rogers 			hashmap__free(ids2);
137114a9d6eSIan Rogers 			return NULL;
138114a9d6eSIan Rogers 		}
139114a9d6eSIan Rogers 	}
140114a9d6eSIan Rogers 	hashmap__free(ids2);
141114a9d6eSIan Rogers 	return ids1;
142114a9d6eSIan Rogers }
143114a9d6eSIan Rogers 
144114a9d6eSIan Rogers /* Caller must make sure id is allocated */
145114a9d6eSIan Rogers int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
146114a9d6eSIan Rogers {
14780be6434SIan Rogers 	return ids__insert(ctx->ids, id);
148114a9d6eSIan Rogers }
149114a9d6eSIan Rogers 
150332603c2SJiri Olsa /* Caller must make sure id is allocated */
151070b3b5aSJiri Olsa int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
152ded80bdaSIan Rogers {
1539aba0adaSIan Rogers 	return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1);
1549aba0adaSIan Rogers }
1559aba0adaSIan Rogers 
1569aba0adaSIan Rogers /* Caller must make sure id is allocated */
1579aba0adaSIan Rogers int expr__add_id_val_source_count(struct expr_parse_ctx *ctx, const char *id,
1589aba0adaSIan Rogers 				  double val, int source_count)
1599aba0adaSIan Rogers {
160070b3b5aSJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
161ded80bdaSIan Rogers 	char *old_key = NULL;
162ded80bdaSIan Rogers 	int ret;
163ded80bdaSIan Rogers 
164070b3b5aSJiri Olsa 	data_ptr = malloc(sizeof(*data_ptr));
165070b3b5aSJiri Olsa 	if (!data_ptr)
166ded80bdaSIan Rogers 		return -ENOMEM;
1679aba0adaSIan Rogers 	data_ptr->val.val = val;
1689aba0adaSIan Rogers 	data_ptr->val.source_count = source_count;
16929396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__VALUE;
170332603c2SJiri Olsa 
171cb94a02eSIan Rogers 	ret = hashmap__set(ctx->ids, id, data_ptr,
172070b3b5aSJiri Olsa 			   (const void **)&old_key, (void **)&old_data);
17360e10c00SJiri Olsa 	if (ret)
17460e10c00SJiri Olsa 		free(data_ptr);
175ded80bdaSIan Rogers 	free(old_key);
176070b3b5aSJiri Olsa 	free(old_data);
177ded80bdaSIan Rogers 	return ret;
178ded80bdaSIan Rogers }
179ded80bdaSIan Rogers 
180fc393839SJiri Olsa int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
181fc393839SJiri Olsa {
182fc393839SJiri Olsa 	struct expr_id_data *data_ptr = NULL, *old_data = NULL;
183fc393839SJiri Olsa 	char *old_key = NULL;
184fc393839SJiri Olsa 	char *name, *p;
185fc393839SJiri Olsa 	int ret;
186fc393839SJiri Olsa 
187fc393839SJiri Olsa 	data_ptr = zalloc(sizeof(*data_ptr));
188fc393839SJiri Olsa 	if (!data_ptr)
189fc393839SJiri Olsa 		return -ENOMEM;
190fc393839SJiri Olsa 
191fc393839SJiri Olsa 	name = strdup(ref->metric_name);
192fc393839SJiri Olsa 	if (!name) {
193fc393839SJiri Olsa 		free(data_ptr);
194fc393839SJiri Olsa 		return -ENOMEM;
195fc393839SJiri Olsa 	}
196fc393839SJiri Olsa 
197fc393839SJiri Olsa 	/*
198fc393839SJiri Olsa 	 * The jevents tool converts all metric expressions
199fc393839SJiri Olsa 	 * to lowercase, including metric references, hence
200fc393839SJiri Olsa 	 * we need to add lowercase name for metric, so it's
201fc393839SJiri Olsa 	 * properly found.
202fc393839SJiri Olsa 	 */
203fc393839SJiri Olsa 	for (p = name; *p; p++)
204fc393839SJiri Olsa 		*p = tolower(*p);
205fc393839SJiri Olsa 
206fc393839SJiri Olsa 	/*
207fc393839SJiri Olsa 	 * Intentionally passing just const char pointers,
208fc393839SJiri Olsa 	 * originally from 'struct pmu_event' object.
209fc393839SJiri Olsa 	 * We don't need to change them, so there's no
210fc393839SJiri Olsa 	 * need to create our own copy.
211fc393839SJiri Olsa 	 */
212fc393839SJiri Olsa 	data_ptr->ref.metric_name = ref->metric_name;
213fc393839SJiri Olsa 	data_ptr->ref.metric_expr = ref->metric_expr;
21429396cd5SIan Rogers 	data_ptr->kind = EXPR_ID_DATA__REF;
215fc393839SJiri Olsa 
216cb94a02eSIan Rogers 	ret = hashmap__set(ctx->ids, name, data_ptr,
217fc393839SJiri Olsa 			   (const void **)&old_key, (void **)&old_data);
218fc393839SJiri Olsa 	if (ret)
219fc393839SJiri Olsa 		free(data_ptr);
220fc393839SJiri Olsa 
221fc393839SJiri Olsa 	pr_debug2("adding ref metric %s: %s\n",
222fc393839SJiri Olsa 		  ref->metric_name, ref->metric_expr);
223fc393839SJiri Olsa 
224fc393839SJiri Olsa 	free(old_key);
225fc393839SJiri Olsa 	free(old_data);
226fc393839SJiri Olsa 	return ret;
227fc393839SJiri Olsa }
228fc393839SJiri Olsa 
2295c5f5e83SJiri Olsa int expr__get_id(struct expr_parse_ctx *ctx, const char *id,
2305c5f5e83SJiri Olsa 		 struct expr_id_data **data)
231ded80bdaSIan Rogers {
232cb94a02eSIan Rogers 	return hashmap__find(ctx->ids, id, (void **)data) ? 0 : -1;
233576a65b6SJiri Olsa }
234576a65b6SJiri Olsa 
235798c3f4aSIan Rogers bool expr__subset_of_ids(struct expr_parse_ctx *haystack,
236798c3f4aSIan Rogers 			 struct expr_parse_ctx *needles)
237798c3f4aSIan Rogers {
238798c3f4aSIan Rogers 	struct hashmap_entry *cur;
239798c3f4aSIan Rogers 	size_t bkt;
240798c3f4aSIan Rogers 	struct expr_id_data *data;
241798c3f4aSIan Rogers 
242798c3f4aSIan Rogers 	hashmap__for_each_entry(needles->ids, cur, bkt) {
243798c3f4aSIan Rogers 		if (expr__get_id(haystack, cur->key, &data))
244798c3f4aSIan Rogers 			return false;
245798c3f4aSIan Rogers 	}
246798c3f4aSIan Rogers 	return true;
247798c3f4aSIan Rogers }
248798c3f4aSIan Rogers 
249798c3f4aSIan Rogers 
250acf71b05SJiri Olsa int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
251acf71b05SJiri Olsa 		     struct expr_id_data **datap)
252acf71b05SJiri Olsa {
253acf71b05SJiri Olsa 	struct expr_id_data *data;
254acf71b05SJiri Olsa 
255acf71b05SJiri Olsa 	if (expr__get_id(ctx, id, datap) || !*datap) {
256acf71b05SJiri Olsa 		pr_debug("%s not found\n", id);
257acf71b05SJiri Olsa 		return -1;
258acf71b05SJiri Olsa 	}
259acf71b05SJiri Olsa 
260acf71b05SJiri Olsa 	data = *datap;
261acf71b05SJiri Olsa 
26229396cd5SIan Rogers 	switch (data->kind) {
26329396cd5SIan Rogers 	case EXPR_ID_DATA__VALUE:
2649aba0adaSIan Rogers 		pr_debug2("lookup(%s): val %f\n", id, data->val.val);
26529396cd5SIan Rogers 		break;
26629396cd5SIan Rogers 	case EXPR_ID_DATA__REF:
26729396cd5SIan Rogers 		pr_debug2("lookup(%s): ref metric name %s\n", id,
26829396cd5SIan Rogers 			data->ref.metric_name);
269acf71b05SJiri Olsa 		pr_debug("processing metric: %s ENTRY\n", id);
27029396cd5SIan Rogers 		data->kind = EXPR_ID_DATA__REF_VALUE;
271fa831fbbSIan Rogers 		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) {
272acf71b05SJiri Olsa 			pr_debug("%s failed to count\n", id);
273acf71b05SJiri Olsa 			return -1;
274acf71b05SJiri Olsa 		}
2759aba0adaSIan Rogers 		pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val);
27629396cd5SIan Rogers 		break;
27729396cd5SIan Rogers 	case EXPR_ID_DATA__REF_VALUE:
27829396cd5SIan Rogers 		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
27929396cd5SIan Rogers 			data->ref.val, data->ref.metric_name);
28029396cd5SIan Rogers 		break;
28129396cd5SIan Rogers 	default:
28229396cd5SIan Rogers 		assert(0);  /* Unreachable. */
283acf71b05SJiri Olsa 	}
284acf71b05SJiri Olsa 
285acf71b05SJiri Olsa 	return 0;
286acf71b05SJiri Olsa }
287acf71b05SJiri Olsa 
2883fd29fa6SJiri Olsa void expr__del_id(struct expr_parse_ctx *ctx, const char *id)
2893fd29fa6SJiri Olsa {
2903fd29fa6SJiri Olsa 	struct expr_id_data *old_val = NULL;
2913fd29fa6SJiri Olsa 	char *old_key = NULL;
2923fd29fa6SJiri Olsa 
293cb94a02eSIan Rogers 	hashmap__delete(ctx->ids, id,
2943fd29fa6SJiri Olsa 			(const void **)&old_key, (void **)&old_val);
2953fd29fa6SJiri Olsa 	free(old_key);
2963fd29fa6SJiri Olsa 	free(old_val);
2973fd29fa6SJiri Olsa }
2983fd29fa6SJiri Olsa 
299cb94a02eSIan Rogers struct expr_parse_ctx *expr__ctx_new(void)
300576a65b6SJiri Olsa {
301cb94a02eSIan Rogers 	struct expr_parse_ctx *ctx;
302cb94a02eSIan Rogers 
303cb94a02eSIan Rogers 	ctx = malloc(sizeof(struct expr_parse_ctx));
304cb94a02eSIan Rogers 	if (!ctx)
305cb94a02eSIan Rogers 		return NULL;
306cb94a02eSIan Rogers 
307cb94a02eSIan Rogers 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
3080a515a06SMiaoqian Lin 	if (IS_ERR(ctx->ids)) {
3090a515a06SMiaoqian Lin 		free(ctx);
3100a515a06SMiaoqian Lin 		return NULL;
3110a515a06SMiaoqian Lin 	}
312fa831fbbSIan Rogers 	ctx->runtime = 0;
31380be6434SIan Rogers 
314cb94a02eSIan Rogers 	return ctx;
315ded80bdaSIan Rogers }
316ded80bdaSIan Rogers 
317ded80bdaSIan Rogers void expr__ctx_clear(struct expr_parse_ctx *ctx)
318ded80bdaSIan Rogers {
319ded80bdaSIan Rogers 	struct hashmap_entry *cur;
320ded80bdaSIan Rogers 	size_t bkt;
321ded80bdaSIan Rogers 
322cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
323ded80bdaSIan Rogers 		free((char *)cur->key);
324ded80bdaSIan Rogers 		free(cur->value);
325ded80bdaSIan Rogers 	}
326cb94a02eSIan Rogers 	hashmap__clear(ctx->ids);
327cb94a02eSIan Rogers }
328cb94a02eSIan Rogers 
329cb94a02eSIan Rogers void expr__ctx_free(struct expr_parse_ctx *ctx)
330cb94a02eSIan Rogers {
331cb94a02eSIan Rogers 	struct hashmap_entry *cur;
332cb94a02eSIan Rogers 	size_t bkt;
333cb94a02eSIan Rogers 
334cb94a02eSIan Rogers 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
335cb94a02eSIan Rogers 		free((char *)cur->key);
336cb94a02eSIan Rogers 		free(cur->value);
337cb94a02eSIan Rogers 	}
338cb94a02eSIan Rogers 	hashmap__free(ctx->ids);
339cb94a02eSIan Rogers 	free(ctx);
340576a65b6SJiri Olsa }
34126226a97SJiri Olsa 
34226226a97SJiri Olsa static int
343aecce63eSJiri Olsa __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,
344fa831fbbSIan Rogers 	      bool compute_ids)
34526226a97SJiri Olsa {
346871f9f59SJiri Olsa 	struct expr_scanner_ctx scanner_ctx = {
347fa831fbbSIan Rogers 		.runtime = ctx->runtime,
348871f9f59SJiri Olsa 	};
34926226a97SJiri Olsa 	YY_BUFFER_STATE buffer;
35026226a97SJiri Olsa 	void *scanner;
35126226a97SJiri Olsa 	int ret;
35226226a97SJiri Olsa 
353acf71b05SJiri Olsa 	pr_debug2("parsing metric: %s\n", expr);
354acf71b05SJiri Olsa 
355871f9f59SJiri Olsa 	ret = expr_lex_init_extra(&scanner_ctx, &scanner);
35626226a97SJiri Olsa 	if (ret)
35726226a97SJiri Olsa 		return ret;
35826226a97SJiri Olsa 
35926226a97SJiri Olsa 	buffer = expr__scan_string(expr, scanner);
36026226a97SJiri Olsa 
36126226a97SJiri Olsa #ifdef PARSER_DEBUG
36226226a97SJiri Olsa 	expr_debug = 1;
363e5e0e635SIan Rogers 	expr_set_debug(1, scanner);
36426226a97SJiri Olsa #endif
36526226a97SJiri Olsa 
3663f965a7dSIan Rogers 	ret = expr_parse(val, ctx, compute_ids, scanner);
36726226a97SJiri Olsa 
36826226a97SJiri Olsa 	expr__flush_buffer(buffer, scanner);
36926226a97SJiri Olsa 	expr__delete_buffer(buffer, scanner);
37026226a97SJiri Olsa 	expr_lex_destroy(scanner);
37126226a97SJiri Olsa 	return ret;
37226226a97SJiri Olsa }
37326226a97SJiri Olsa 
374ded80bdaSIan Rogers int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
375fa831fbbSIan Rogers 		const char *expr)
37626226a97SJiri Olsa {
377fa831fbbSIan Rogers 	return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0;
37826226a97SJiri Olsa }
37926226a97SJiri Olsa 
3807e06a5e3SIan Rogers int expr__find_ids(const char *expr, const char *one,
381fa831fbbSIan Rogers 		   struct expr_parse_ctx *ctx)
38226226a97SJiri Olsa {
383fa831fbbSIan Rogers 	int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true);
38426226a97SJiri Olsa 
3853fd29fa6SJiri Olsa 	if (one)
3863fd29fa6SJiri Olsa 		expr__del_id(ctx, one);
38726226a97SJiri Olsa 
388ded80bdaSIan Rogers 	return ret;
38926226a97SJiri Olsa }
39029396cd5SIan Rogers 
39129396cd5SIan Rogers double expr_id_data__value(const struct expr_id_data *data)
39229396cd5SIan Rogers {
39329396cd5SIan Rogers 	if (data->kind == EXPR_ID_DATA__VALUE)
3949aba0adaSIan Rogers 		return data->val.val;
39529396cd5SIan Rogers 	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
39629396cd5SIan Rogers 	return data->ref.val;
39729396cd5SIan Rogers }
3983613f6c1SIan Rogers 
3999aba0adaSIan Rogers double expr_id_data__source_count(const struct expr_id_data *data)
4009aba0adaSIan Rogers {
4019aba0adaSIan Rogers 	assert(data->kind == EXPR_ID_DATA__VALUE);
4029aba0adaSIan Rogers 	return data->val.source_count;
4039aba0adaSIan Rogers }
4049aba0adaSIan Rogers 
4053613f6c1SIan Rogers double expr__get_literal(const char *literal)
4063613f6c1SIan Rogers {
407fdf1e29bSIan Rogers 	static struct cpu_topology *topology;
408*f56ef30aSIan Rogers 	double result = NAN;
409fdf1e29bSIan Rogers 
410*f56ef30aSIan Rogers 	if (!strcmp("#smt_on", literal)) {
411*f56ef30aSIan Rogers 		result = smt_on() > 0 ? 1.0 : 0.0;
412*f56ef30aSIan Rogers 		goto out;
413*f56ef30aSIan Rogers 	}
4143613f6c1SIan Rogers 
415*f56ef30aSIan Rogers 	if (!strcmp("#num_cpus", literal)) {
416*f56ef30aSIan Rogers 		result = cpu__max_present_cpu().cpu;
417*f56ef30aSIan Rogers 		goto out;
418*f56ef30aSIan Rogers 	}
419fdf1e29bSIan Rogers 
420fdf1e29bSIan Rogers 	/*
421fdf1e29bSIan Rogers 	 * Assume that topology strings are consistent, such as CPUs "0-1"
422fdf1e29bSIan Rogers 	 * wouldn't be listed as "0,1", and so after deduplication the number of
423fdf1e29bSIan Rogers 	 * these strings gives an indication of the number of packages, dies,
424fdf1e29bSIan Rogers 	 * etc.
425fdf1e29bSIan Rogers 	 */
426fdf1e29bSIan Rogers 	if (!topology) {
427fdf1e29bSIan Rogers 		topology = cpu_topology__new();
428fdf1e29bSIan Rogers 		if (!topology) {
429fdf1e29bSIan Rogers 			pr_err("Error creating CPU topology");
430*f56ef30aSIan Rogers 			goto out;
431fdf1e29bSIan Rogers 		}
432fdf1e29bSIan Rogers 	}
433*f56ef30aSIan Rogers 	if (!strcmp("#num_packages", literal)) {
434*f56ef30aSIan Rogers 		result = topology->package_cpus_lists;
435*f56ef30aSIan Rogers 		goto out;
436*f56ef30aSIan Rogers 	}
437*f56ef30aSIan Rogers 	if (!strcmp("#num_dies", literal)) {
438*f56ef30aSIan Rogers 		result = topology->die_cpus_lists;
439*f56ef30aSIan Rogers 		goto out;
440*f56ef30aSIan Rogers 	}
441*f56ef30aSIan Rogers 	if (!strcmp("#num_cores", literal)) {
442*f56ef30aSIan Rogers 		result = topology->core_cpus_lists;
443*f56ef30aSIan Rogers 		goto out;
444*f56ef30aSIan Rogers 	}
445fdf1e29bSIan Rogers 
4463613f6c1SIan Rogers 	pr_err("Unrecognized literal '%s'", literal);
447*f56ef30aSIan Rogers out:
448*f56ef30aSIan Rogers 	pr_debug2("literal: %s = %f\n", literal, result);
449*f56ef30aSIan Rogers 	return result;
4503613f6c1SIan Rogers }
451