Commit | Line | Data |
---|---|---|
576a65b6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
26226a97 | 2 | #include <stdbool.h> |
576a65b6 | 3 | #include <assert.h> |
3744ca1e ACM |
4 | #include <errno.h> |
5 | #include <stdlib.h> | |
6 | #include <string.h> | |
fc393839 | 7 | #include "metricgroup.h" |
fdf1e29b IR |
8 | #include "cpumap.h" |
9 | #include "cputopo.h" | |
fc393839 | 10 | #include "debug.h" |
4a4a9bf9 | 11 | #include "evlist.h" |
576a65b6 | 12 | #include "expr.h" |
c7e97f21 NK |
13 | #include <util/expr-bison.h> |
14 | #include <util/expr-flex.h> | |
bd560973 | 15 | #include "util/hashmap.h" |
9d5da30e JC |
16 | #include "util/header.h" |
17 | #include "util/pmu.h" | |
3613f6c1 | 18 | #include "smt.h" |
bc2373a5 | 19 | #include "tsc.h" |
c3bf86f1 | 20 | #include <api/fs/fs.h> |
0a515a06 | 21 | #include <linux/err.h> |
ded80bda | 22 | #include <linux/kernel.h> |
fc393839 JO |
23 | #include <linux/zalloc.h> |
24 | #include <ctype.h> | |
3613f6c1 | 25 | #include <math.h> |
acef233b | 26 | #include "pmu.h" |
26226a97 JO |
27 | |
28 | #ifdef PARSER_DEBUG | |
29 | extern int expr_debug; | |
30 | #endif | |
576a65b6 | 31 | |
29396cd5 IR |
32 | struct expr_id_data { |
33 | union { | |
9aba0ada IR |
34 | struct { |
35 | double val; | |
36 | int source_count; | |
37 | } val; | |
29396cd5 IR |
38 | struct { |
39 | double val; | |
40 | const char *metric_name; | |
41 | const char *metric_expr; | |
42 | } ref; | |
29396cd5 IR |
43 | }; |
44 | ||
45 | enum { | |
46 | /* Holding a double value. */ | |
47 | EXPR_ID_DATA__VALUE, | |
48 | /* Reference to another metric. */ | |
49 | EXPR_ID_DATA__REF, | |
50 | /* A reference but the value has been computed. */ | |
51 | EXPR_ID_DATA__REF_VALUE, | |
29396cd5 IR |
52 | } kind; |
53 | }; | |
54 | ||
c302378b | 55 | static size_t key_hash(long key, void *ctx __maybe_unused) |
ded80bda IR |
56 | { |
57 | const char *str = (const char *)key; | |
58 | size_t hash = 0; | |
59 | ||
60 | while (*str != '\0') { | |
61 | hash *= 31; | |
62 | hash += *str; | |
63 | str++; | |
64 | } | |
65 | return hash; | |
66 | } | |
67 | ||
c302378b | 68 | static bool key_equal(long key1, long key2, void *ctx __maybe_unused) |
ded80bda IR |
69 | { |
70 | return !strcmp((const char *)key1, (const char *)key2); | |
71 | } | |
72 | ||
114a9d6e IR |
73 | struct hashmap *ids__new(void) |
74 | { | |
9f3c16a4 ML |
75 | struct hashmap *hash; |
76 | ||
77 | hash = hashmap__new(key_hash, key_equal, NULL); | |
78 | if (IS_ERR(hash)) | |
79 | return NULL; | |
80 | return hash; | |
114a9d6e IR |
81 | } |
82 | ||
83 | void ids__free(struct hashmap *ids) | |
84 | { | |
85 | struct hashmap_entry *cur; | |
86 | size_t bkt; | |
87 | ||
88 | if (ids == NULL) | |
89 | return; | |
90 | ||
91 | hashmap__for_each_entry(ids, cur, bkt) { | |
a77f8184 ACM |
92 | zfree(&cur->pkey); |
93 | zfree(&cur->pvalue); | |
114a9d6e IR |
94 | } |
95 | ||
96 | hashmap__free(ids); | |
97 | } | |
98 | ||
80be6434 | 99 | int ids__insert(struct hashmap *ids, const char *id) |
332603c2 JO |
100 | { |
101 | struct expr_id_data *data_ptr = NULL, *old_data = NULL; | |
102 | char *old_key = NULL; | |
103 | int ret; | |
104 | ||
c302378b | 105 | ret = hashmap__set(ids, id, data_ptr, &old_key, &old_data); |
332603c2 JO |
106 | if (ret) |
107 | free(data_ptr); | |
108 | free(old_key); | |
109 | free(old_data); | |
110 | return ret; | |
111 | } | |
112 | ||
114a9d6e IR |
113 | struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2) |
114 | { | |
115 | size_t bkt; | |
116 | struct hashmap_entry *cur; | |
117 | int ret; | |
118 | struct expr_id_data *old_data = NULL; | |
119 | char *old_key = NULL; | |
120 | ||
121 | if (!ids1) | |
122 | return ids2; | |
123 | ||
124 | if (!ids2) | |
125 | return ids1; | |
126 | ||
127 | if (hashmap__size(ids1) < hashmap__size(ids2)) { | |
128 | struct hashmap *tmp = ids1; | |
129 | ||
130 | ids1 = ids2; | |
131 | ids2 = tmp; | |
132 | } | |
133 | hashmap__for_each_entry(ids2, cur, bkt) { | |
c302378b | 134 | ret = hashmap__set(ids1, cur->key, cur->value, &old_key, &old_data); |
114a9d6e IR |
135 | free(old_key); |
136 | free(old_data); | |
137 | ||
138 | if (ret) { | |
139 | hashmap__free(ids1); | |
140 | hashmap__free(ids2); | |
141 | return NULL; | |
142 | } | |
143 | } | |
144 | hashmap__free(ids2); | |
145 | return ids1; | |
146 | } | |
147 | ||
148 | /* Caller must make sure id is allocated */ | |
149 | int expr__add_id(struct expr_parse_ctx *ctx, const char *id) | |
150 | { | |
80be6434 | 151 | return ids__insert(ctx->ids, id); |
114a9d6e IR |
152 | } |
153 | ||
576a65b6 | 154 | /* Caller must make sure id is allocated */ |
070b3b5a | 155 | int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val) |
9aba0ada IR |
156 | { |
157 | return expr__add_id_val_source_count(ctx, id, val, /*source_count=*/1); | |
158 | } | |
159 | ||
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) | |
576a65b6 | 163 | { |
070b3b5a | 164 | struct expr_id_data *data_ptr = NULL, *old_data = NULL; |
ded80bda IR |
165 | char *old_key = NULL; |
166 | int ret; | |
167 | ||
332603c2 JO |
168 | data_ptr = malloc(sizeof(*data_ptr)); |
169 | if (!data_ptr) | |
170 | return -ENOMEM; | |
9aba0ada IR |
171 | data_ptr->val.val = val; |
172 | data_ptr->val.source_count = source_count; | |
29396cd5 | 173 | data_ptr->kind = EXPR_ID_DATA__VALUE; |
332603c2 | 174 | |
c302378b | 175 | ret = hashmap__set(ctx->ids, id, data_ptr, &old_key, &old_data); |
60e10c00 JO |
176 | if (ret) |
177 | free(data_ptr); | |
ded80bda | 178 | free(old_key); |
070b3b5a | 179 | free(old_data); |
ded80bda IR |
180 | return ret; |
181 | } | |
182 | ||
fc393839 JO |
183 | int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref) |
184 | { | |
185 | struct expr_id_data *data_ptr = NULL, *old_data = NULL; | |
186 | char *old_key = NULL; | |
715b824f | 187 | char *name; |
fc393839 JO |
188 | int ret; |
189 | ||
190 | data_ptr = zalloc(sizeof(*data_ptr)); | |
191 | if (!data_ptr) | |
192 | return -ENOMEM; | |
193 | ||
194 | name = strdup(ref->metric_name); | |
195 | if (!name) { | |
196 | free(data_ptr); | |
197 | return -ENOMEM; | |
198 | } | |
199 | ||
fc393839 JO |
200 | /* |
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. | |
205 | */ | |
206 | data_ptr->ref.metric_name = ref->metric_name; | |
207 | data_ptr->ref.metric_expr = ref->metric_expr; | |
29396cd5 | 208 | data_ptr->kind = EXPR_ID_DATA__REF; |
fc393839 | 209 | |
c302378b | 210 | ret = hashmap__set(ctx->ids, name, data_ptr, &old_key, &old_data); |
fc393839 JO |
211 | if (ret) |
212 | free(data_ptr); | |
213 | ||
214 | pr_debug2("adding ref metric %s: %s\n", | |
215 | ref->metric_name, ref->metric_expr); | |
216 | ||
217 | free(old_key); | |
218 | free(old_data); | |
219 | return ret; | |
220 | } | |
221 | ||
5c5f5e83 JO |
222 | int expr__get_id(struct expr_parse_ctx *ctx, const char *id, |
223 | struct expr_id_data **data) | |
ded80bda | 224 | { |
c302378b | 225 | return hashmap__find(ctx->ids, id, data) ? 0 : -1; |
576a65b6 JO |
226 | } |
227 | ||
798c3f4a IR |
228 | bool expr__subset_of_ids(struct expr_parse_ctx *haystack, |
229 | struct expr_parse_ctx *needles) | |
230 | { | |
231 | struct hashmap_entry *cur; | |
232 | size_t bkt; | |
233 | struct expr_id_data *data; | |
234 | ||
235 | hashmap__for_each_entry(needles->ids, cur, bkt) { | |
c302378b | 236 | if (expr__get_id(haystack, cur->pkey, &data)) |
798c3f4a IR |
237 | return false; |
238 | } | |
239 | return true; | |
240 | } | |
241 | ||
242 | ||
acf71b05 JO |
243 | int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id, |
244 | struct expr_id_data **datap) | |
245 | { | |
246 | struct expr_id_data *data; | |
247 | ||
248 | if (expr__get_id(ctx, id, datap) || !*datap) { | |
249 | pr_debug("%s not found\n", id); | |
250 | return -1; | |
251 | } | |
252 | ||
253 | data = *datap; | |
254 | ||
29396cd5 IR |
255 | switch (data->kind) { |
256 | case EXPR_ID_DATA__VALUE: | |
9aba0ada | 257 | pr_debug2("lookup(%s): val %f\n", id, data->val.val); |
29396cd5 | 258 | break; |
29396cd5 IR |
259 | case EXPR_ID_DATA__REF: |
260 | pr_debug2("lookup(%s): ref metric name %s\n", id, | |
261 | data->ref.metric_name); | |
acf71b05 | 262 | pr_debug("processing metric: %s ENTRY\n", id); |
29396cd5 | 263 | data->kind = EXPR_ID_DATA__REF_VALUE; |
fa831fbb | 264 | if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr)) { |
acf71b05 JO |
265 | pr_debug("%s failed to count\n", id); |
266 | return -1; | |
267 | } | |
9aba0ada | 268 | pr_debug("processing metric: %s EXIT: %f\n", id, data->ref.val); |
29396cd5 IR |
269 | break; |
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); | |
273 | break; | |
274 | default: | |
275 | assert(0); /* Unreachable. */ | |
acf71b05 JO |
276 | } |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
3fd29fa6 JO |
281 | void expr__del_id(struct expr_parse_ctx *ctx, const char *id) |
282 | { | |
283 | struct expr_id_data *old_val = NULL; | |
284 | char *old_key = NULL; | |
285 | ||
c302378b | 286 | hashmap__delete(ctx->ids, id, &old_key, &old_val); |
3fd29fa6 JO |
287 | free(old_key); |
288 | free(old_val); | |
289 | } | |
290 | ||
cb94a02e | 291 | struct expr_parse_ctx *expr__ctx_new(void) |
576a65b6 | 292 | { |
cb94a02e IR |
293 | struct expr_parse_ctx *ctx; |
294 | ||
295 | ctx = malloc(sizeof(struct expr_parse_ctx)); | |
296 | if (!ctx) | |
297 | return NULL; | |
298 | ||
299 | ctx->ids = hashmap__new(key_hash, key_equal, NULL); | |
0a515a06 ML |
300 | if (IS_ERR(ctx->ids)) { |
301 | free(ctx); | |
302 | return NULL; | |
303 | } | |
1725e9cd | 304 | ctx->sctx.user_requested_cpu_list = NULL; |
1a6abdde | 305 | ctx->sctx.runtime = 0; |
1725e9cd | 306 | ctx->sctx.system_wide = false; |
80be6434 | 307 | |
cb94a02e | 308 | return ctx; |
ded80bda IR |
309 | } |
310 | ||
311 | void expr__ctx_clear(struct expr_parse_ctx *ctx) | |
312 | { | |
313 | struct hashmap_entry *cur; | |
314 | size_t bkt; | |
315 | ||
cb94a02e | 316 | hashmap__for_each_entry(ctx->ids, cur, bkt) { |
a77f8184 ACM |
317 | zfree(&cur->pkey); |
318 | zfree(&cur->pvalue); | |
cb94a02e IR |
319 | } |
320 | hashmap__clear(ctx->ids); | |
321 | } | |
322 | ||
323 | void expr__ctx_free(struct expr_parse_ctx *ctx) | |
324 | { | |
325 | struct hashmap_entry *cur; | |
326 | size_t bkt; | |
327 | ||
1725e9cd IR |
328 | if (!ctx) |
329 | return; | |
330 | ||
a77f8184 | 331 | zfree(&ctx->sctx.user_requested_cpu_list); |
cb94a02e | 332 | hashmap__for_each_entry(ctx->ids, cur, bkt) { |
a77f8184 ACM |
333 | zfree(&cur->pkey); |
334 | zfree(&cur->pvalue); | |
ded80bda | 335 | } |
cb94a02e IR |
336 | hashmap__free(ctx->ids); |
337 | free(ctx); | |
576a65b6 | 338 | } |
26226a97 JO |
339 | |
340 | static int | |
aecce63e | 341 | __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, |
fa831fbb | 342 | bool compute_ids) |
26226a97 JO |
343 | { |
344 | YY_BUFFER_STATE buffer; | |
345 | void *scanner; | |
346 | int ret; | |
347 | ||
acf71b05 JO |
348 | pr_debug2("parsing metric: %s\n", expr); |
349 | ||
1a6abdde | 350 | ret = expr_lex_init_extra(&ctx->sctx, &scanner); |
26226a97 JO |
351 | if (ret) |
352 | return ret; | |
353 | ||
354 | buffer = expr__scan_string(expr, scanner); | |
355 | ||
356 | #ifdef PARSER_DEBUG | |
357 | expr_debug = 1; | |
e5e0e635 | 358 | expr_set_debug(1, scanner); |
26226a97 JO |
359 | #endif |
360 | ||
3f965a7d | 361 | ret = expr_parse(val, ctx, compute_ids, scanner); |
26226a97 JO |
362 | |
363 | expr__flush_buffer(buffer, scanner); | |
364 | expr__delete_buffer(buffer, scanner); | |
365 | expr_lex_destroy(scanner); | |
366 | return ret; | |
367 | } | |
368 | ||
ded80bda | 369 | int expr__parse(double *final_val, struct expr_parse_ctx *ctx, |
fa831fbb | 370 | const char *expr) |
26226a97 | 371 | { |
fa831fbb | 372 | return __expr__parse(final_val, ctx, expr, /*compute_ids=*/false) ? -1 : 0; |
26226a97 JO |
373 | } |
374 | ||
7e06a5e3 | 375 | int expr__find_ids(const char *expr, const char *one, |
fa831fbb | 376 | struct expr_parse_ctx *ctx) |
26226a97 | 377 | { |
fa831fbb | 378 | int ret = __expr__parse(NULL, ctx, expr, /*compute_ids=*/true); |
ded80bda | 379 | |
3fd29fa6 JO |
380 | if (one) |
381 | expr__del_id(ctx, one); | |
26226a97 | 382 | |
ded80bda | 383 | return ret; |
26226a97 | 384 | } |
29396cd5 IR |
385 | |
386 | double expr_id_data__value(const struct expr_id_data *data) | |
387 | { | |
388 | if (data->kind == EXPR_ID_DATA__VALUE) | |
9aba0ada | 389 | return data->val.val; |
29396cd5 IR |
390 | assert(data->kind == EXPR_ID_DATA__REF_VALUE); |
391 | return data->ref.val; | |
392 | } | |
3613f6c1 | 393 | |
9aba0ada IR |
394 | double expr_id_data__source_count(const struct expr_id_data *data) |
395 | { | |
396 | assert(data->kind == EXPR_ID_DATA__VALUE); | |
397 | return data->val.source_count; | |
398 | } | |
399 | ||
bc2373a5 KL |
400 | #if !defined(__i386__) && !defined(__x86_64__) |
401 | double arch_get_tsc_freq(void) | |
402 | { | |
403 | return 0.0; | |
404 | } | |
405 | #endif | |
406 | ||
c3bf86f1 IR |
407 | static double has_pmem(void) |
408 | { | |
409 | static bool has_pmem, cached; | |
410 | const char *sysfs = sysfs__mountpoint(); | |
411 | char path[PATH_MAX]; | |
412 | ||
413 | if (!cached) { | |
414 | snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs); | |
415 | has_pmem = access(path, F_OK) == 0; | |
416 | cached = true; | |
417 | } | |
418 | return has_pmem ? 1.0 : 0.0; | |
419 | } | |
420 | ||
1725e9cd | 421 | double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx) |
3613f6c1 | 422 | { |
207f7df7 | 423 | const struct cpu_topology *topology; |
f56ef30a | 424 | double result = NAN; |
fdf1e29b | 425 | |
f56ef30a IR |
426 | if (!strcmp("#num_cpus", literal)) { |
427 | result = cpu__max_present_cpu().cpu; | |
428 | goto out; | |
429 | } | |
f0005f17 IR |
430 | if (!strcmp("#num_cpus_online", literal)) { |
431 | struct perf_cpu_map *online = cpu_map__online(); | |
432 | ||
433 | if (online) | |
434 | result = perf_cpu_map__nr(online); | |
435 | goto out; | |
436 | } | |
fdf1e29b | 437 | |
bc2373a5 KL |
438 | if (!strcasecmp("#system_tsc_freq", literal)) { |
439 | result = arch_get_tsc_freq(); | |
440 | goto out; | |
441 | } | |
442 | ||
fdf1e29b IR |
443 | /* |
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, | |
447 | * etc. | |
448 | */ | |
09b73fe9 | 449 | if (!strcasecmp("#smt_on", literal)) { |
207f7df7 | 450 | result = smt_on() ? 1.0 : 0.0; |
09b73fe9 IR |
451 | goto out; |
452 | } | |
1725e9cd | 453 | if (!strcmp("#core_wide", literal)) { |
207f7df7 | 454 | result = core_wide(ctx->system_wide, ctx->user_requested_cpu_list) |
1725e9cd IR |
455 | ? 1.0 : 0.0; |
456 | goto out; | |
457 | } | |
f56ef30a | 458 | if (!strcmp("#num_packages", literal)) { |
207f7df7 | 459 | topology = online_topology(); |
f56ef30a IR |
460 | result = topology->package_cpus_lists; |
461 | goto out; | |
462 | } | |
463 | if (!strcmp("#num_dies", literal)) { | |
207f7df7 | 464 | topology = online_topology(); |
f56ef30a IR |
465 | result = topology->die_cpus_lists; |
466 | goto out; | |
467 | } | |
468 | if (!strcmp("#num_cores", literal)) { | |
207f7df7 | 469 | topology = online_topology(); |
f56ef30a IR |
470 | result = topology->core_cpus_lists; |
471 | goto out; | |
472 | } | |
acef233b JZ |
473 | if (!strcmp("#slots", literal)) { |
474 | result = perf_pmu__cpu_slots_per_cycle(); | |
475 | goto out; | |
476 | } | |
c3bf86f1 IR |
477 | if (!strcmp("#has_pmem", literal)) { |
478 | result = has_pmem(); | |
479 | goto out; | |
480 | } | |
fdf1e29b | 481 | |
3613f6c1 | 482 | pr_err("Unrecognized literal '%s'", literal); |
f56ef30a IR |
483 | out: |
484 | pr_debug2("literal: %s = %f\n", literal, result); | |
485 | return result; | |
3613f6c1 | 486 | } |
4a4a9bf9 IR |
487 | |
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) | |
490 | { | |
491 | struct evlist *tmp; | |
492 | double ret; | |
493 | ||
494 | if (hashmap__find(ctx->ids, id, /*value=*/NULL)) | |
495 | return 1.0; | |
496 | ||
497 | if (!compute_ids) | |
498 | return 0.0; | |
499 | ||
500 | tmp = evlist__new(); | |
501 | if (!tmp) | |
502 | return NAN; | |
6dd76680 IR |
503 | |
504 | if (strchr(id, '@')) { | |
505 | char *tmp_id, *p; | |
506 | ||
507 | tmp_id = strdup(id); | |
508 | if (!tmp_id) { | |
509 | ret = NAN; | |
510 | goto out; | |
511 | } | |
512 | p = strchr(tmp_id, '@'); | |
513 | *p = '/'; | |
514 | p = strrchr(tmp_id, '@'); | |
515 | *p = '/'; | |
516 | ret = parse_event(tmp, tmp_id) ? 0 : 1; | |
517 | free(tmp_id); | |
518 | } else { | |
519 | ret = parse_event(tmp, id) ? 0 : 1; | |
520 | } | |
521 | out: | |
4a4a9bf9 IR |
522 | evlist__delete(tmp); |
523 | return ret; | |
524 | } | |
9d5da30e JC |
525 | |
526 | double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused, | |
527 | bool compute_ids __maybe_unused, const char *test_id) | |
528 | { | |
529 | double ret; | |
3d0f5f45 | 530 | struct perf_pmu *pmu = perf_pmus__find_core_pmu(); |
9d5da30e JC |
531 | char *cpuid = perf_pmu__getcpuid(pmu); |
532 | ||
533 | if (!cpuid) | |
534 | return NAN; | |
535 | ||
536 | ret = !strcmp_cpuid_str(test_id, cpuid); | |
537 | ||
538 | free(cpuid); | |
539 | return ret; | |
540 | } |