1 // SPDX-License-Identifier: GPL-2.0
3 #include "parse-events.h"
8 #include <linux/kernel.h>
9 #include <linux/zalloc.h>
11 #include "../pmu-events/pmu-events.h"
12 #include "util/evlist.h"
13 #include "util/expr.h"
14 #include "util/parse-events.h"
15 #include "metricgroup.h"
17 struct perf_pmu_test_event {
18 /* used for matching against events from generated pmu-events.c */
19 struct pmu_event event;
21 /* used for matching against event aliases */
22 /* extra events for aliases */
23 const char *alias_str;
26 * Note: For when PublicDescription does not exist in the JSON, we
27 * will have no long_desc in pmu_event.long_desc, but long_desc may
28 * be set in the alias.
30 const char *alias_long_desc;
33 static struct perf_pmu_test_event test_cpu_events[] = {
36 .name = "bp_l1_btb_correct",
37 .event = "event=0x8a",
38 .desc = "L1 BTB Correction",
41 .alias_str = "event=0x8a",
42 .alias_long_desc = "L1 BTB Correction",
46 .name = "bp_l2_btb_correct",
47 .event = "event=0x8b",
48 .desc = "L2 BTB Correction",
51 .alias_str = "event=0x8b",
52 .alias_long_desc = "L2 BTB Correction",
56 .name = "segment_reg_loads.any",
57 .event = "umask=0x80,period=200000,event=0x6",
58 .desc = "Number of segment register loads",
61 .alias_str = "umask=0x80,(null)=0x30d40,event=0x6",
62 .alias_long_desc = "Number of segment register loads",
66 .name = "dispatch_blocked.any",
67 .event = "umask=0x20,period=200000,event=0x9",
68 .desc = "Memory cluster signals to block micro-op dispatch for any reason",
71 .alias_str = "umask=0x20,(null)=0x30d40,event=0x9",
72 .alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
77 .event = "umask=0x0,period=200000,event=0x3a",
78 .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
81 .alias_str = "umask=0,(null)=0x30d40,event=0x3a",
82 .alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
86 .name = "l3_cache_rd",
87 .event = "event=0x40",
88 .desc = "L3 cache access, read",
89 .long_desc = "Attributable Level 3 cache access, read",
92 .alias_str = "event=0x40",
93 .alias_long_desc = "Attributable Level 3 cache access, read",
102 static struct perf_pmu_test_event test_uncore_events[] = {
105 .name = "uncore_hisi_ddrc.flux_wcmd",
106 .event = "event=0x2",
107 .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
109 .long_desc = "DDRC write commands",
110 .pmu = "hisi_sccl,ddrc",
112 .alias_str = "event=0x2",
113 .alias_long_desc = "DDRC write commands",
117 .name = "unc_cbo_xsnp_response.miss_eviction",
118 .event = "umask=0x81,event=0x22",
119 .desc = "Unit: uncore_cbox A cross-core snoop resulted from L3 Eviction which misses in some processor core",
121 .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
122 .pmu = "uncore_cbox",
124 .alias_str = "umask=0x81,event=0x22",
125 .alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
134 const int total_test_events_size = ARRAY_SIZE(test_uncore_events);
136 static bool is_same(const char *reference, const char *test)
138 if (!reference && !test)
141 if (reference && !test)
144 if (!reference && test)
147 return !strcmp(reference, test);
150 static struct pmu_events_map *__test_pmu_get_events_map(void)
152 struct pmu_events_map *map;
154 for (map = &pmu_events_map[0]; map->cpuid; map++) {
155 if (!strcmp(map->cpuid, "testcpu"))
159 pr_err("could not find test events map\n");
164 /* Verify generated events from pmu-events.c is as expected */
165 static int test_pmu_event_table(void)
167 struct pmu_events_map *map = __test_pmu_get_events_map();
168 struct pmu_event *table;
169 int map_events = 0, expected_events;
171 /* ignore 2x sentinels */
172 expected_events = ARRAY_SIZE(test_cpu_events) +
173 ARRAY_SIZE(test_uncore_events) - 2;
178 for (table = map->table; table->name; table++) {
179 struct perf_pmu_test_event *test;
180 struct pmu_event *te;
184 test = &test_uncore_events[0];
186 test = &test_cpu_events[0];
190 for (; te->name; test++, te = &test->event) {
191 if (strcmp(table->name, te->name))
196 if (!is_same(table->desc, te->desc)) {
197 pr_debug2("testing event table %s: mismatched desc, %s vs %s\n",
198 table->name, table->desc, te->desc);
202 if (!is_same(table->topic, te->topic)) {
203 pr_debug2("testing event table %s: mismatched topic, %s vs %s\n",
204 table->name, table->topic,
209 if (!is_same(table->long_desc, te->long_desc)) {
210 pr_debug2("testing event table %s: mismatched long_desc, %s vs %s\n",
211 table->name, table->long_desc,
216 if (!is_same(table->unit, te->unit)) {
217 pr_debug2("testing event table %s: mismatched unit, %s vs %s\n",
218 table->name, table->unit,
223 if (!is_same(table->perpkg, te->perpkg)) {
224 pr_debug2("testing event table %s: mismatched perpkg, %s vs %s\n",
225 table->name, table->perpkg,
230 if (!is_same(table->metric_expr, te->metric_expr)) {
231 pr_debug2("testing event table %s: mismatched metric_expr, %s vs %s\n",
232 table->name, table->metric_expr,
237 if (!is_same(table->metric_name, te->metric_name)) {
238 pr_debug2("testing event table %s: mismatched metric_name, %s vs %s\n",
239 table->name, table->metric_name,
244 if (!is_same(table->deprecated, te->deprecated)) {
245 pr_debug2("testing event table %s: mismatched deprecated, %s vs %s\n",
246 table->name, table->deprecated,
251 pr_debug("testing event table %s: pass\n", table->name);
255 pr_err("testing event table: could not find event %s\n",
261 if (map_events != expected_events) {
262 pr_err("testing event table: found %d, but expected %d\n",
263 map_events, expected_events);
270 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
272 struct perf_pmu_alias *alias;
274 list_for_each_entry(alias, aliases, list)
275 if (!strcmp(test_event, alias->name))
281 /* Verify aliases are as expected */
282 static int __test__pmu_event_aliases(char *pmu_name, int *count)
284 struct perf_pmu_test_event *test;
285 struct pmu_event *te;
286 struct perf_pmu *pmu;
289 bool use_uncore_table;
290 struct pmu_events_map *map = __test_pmu_get_events_map();
291 struct perf_pmu_alias *a, *tmp;
296 if (is_pmu_core(pmu_name)) {
297 test = &test_cpu_events[0];
298 use_uncore_table = false;
300 test = &test_uncore_events[0];
301 use_uncore_table = true;
304 pmu = zalloc(sizeof(*pmu));
308 pmu->name = pmu_name;
310 pmu_add_cpu_aliases_map(&aliases, pmu, map);
312 for (te = &test->event; te->name; test++, te = &test->event) {
313 struct perf_pmu_alias *alias = find_alias(te->name, &aliases);
316 bool uncore_match = pmu_uncore_alias_match(pmu_name,
319 if (use_uncore_table && !uncore_match) {
320 pr_debug3("testing aliases PMU %s: skip matching alias %s\n",
325 pr_debug2("testing aliases PMU %s: no alias, alias_table->name=%s\n",
331 if (!is_same(alias->desc, te->desc)) {
332 pr_debug2("testing aliases PMU %s: mismatched desc, %s vs %s\n",
333 pmu_name, alias->desc, te->desc);
338 if (!is_same(alias->long_desc, test->alias_long_desc)) {
339 pr_debug2("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
340 pmu_name, alias->long_desc,
341 test->alias_long_desc);
346 if (!is_same(alias->str, test->alias_str)) {
347 pr_debug2("testing aliases PMU %s: mismatched str, %s vs %s\n",
348 pmu_name, alias->str, test->alias_str);
353 if (!is_same(alias->topic, te->topic)) {
354 pr_debug2("testing aliases PMU %s: mismatched topic, %s vs %s\n",
355 pmu_name, alias->topic, te->topic);
361 pr_debug2("testing aliases PMU %s: matched event %s\n",
362 pmu_name, alias->name);
365 list_for_each_entry_safe(a, tmp, &aliases, list) {
367 perf_pmu_free_alias(a);
374 /* Test that aliases generated are as expected */
375 static int test_aliases(void)
377 struct perf_pmu *pmu = NULL;
379 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
382 if (list_empty(&pmu->format)) {
383 pr_debug2("skipping testing PMU %s\n", pmu->name);
387 if (__test__pmu_event_aliases(pmu->name, &count)) {
388 pr_debug("testing PMU %s aliases: failed\n", pmu->name);
393 pr_debug3("testing PMU %s aliases: no events to match\n",
396 pr_debug("testing PMU %s aliases: pass\n", pmu->name);
402 static bool is_number(const char *str)
408 v = strtod(str, &end_ptr);
409 (void)v; // We're not interested in this value, only if it is valid
410 return errno == 0 && end_ptr != str;
413 static int check_parse_id(const char *id, struct parse_events_error *error,
414 struct perf_pmu *fake_pmu)
416 struct evlist *evlist;
419 /* Numbers are always valid. */
423 evlist = evlist__new();
426 ret = __parse_events(evlist, id, error, fake_pmu);
427 evlist__delete(evlist);
431 static int check_parse_cpu(const char *id, bool same_cpu, struct pmu_event *pe)
433 struct parse_events_error error = { .idx = 0, };
435 int ret = check_parse_id(id, &error, NULL);
436 if (ret && same_cpu) {
437 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
438 pe->metric_name, id, pe->metric_expr);
439 pr_warning("Error string '%s' help '%s'\n", error.str,
442 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
443 id, pe->metric_name, pe->metric_expr);
448 free(error.first_str);
449 free(error.first_help);
453 static int check_parse_fake(const char *id)
455 struct parse_events_error error = { .idx = 0, };
456 int ret = check_parse_id(id, &error, &perf_pmu__fake);
460 free(error.first_str);
461 free(error.first_help);
465 static void expr_failure(const char *msg,
466 const struct pmu_events_map *map,
467 const struct pmu_event *pe)
469 pr_debug("%s for map %s %s %s\n",
470 msg, map->cpuid, map->version, map->type);
471 pr_debug("On metric %s\n", pe->metric_name);
472 pr_debug("On expression %s\n", pe->metric_expr);
476 struct list_head list;
477 struct metric_ref metric_ref;
480 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
481 struct list_head *compound_list,
482 struct pmu_events_map *map,
483 const char *metric_name)
485 struct hashmap_entry *cur, *cur_tmp;
486 struct metric *metric, *tmp;
493 hashmap__for_each_entry_safe((&pctx->ids), cur, cur_tmp, bkt) {
494 struct metric_ref *ref;
495 struct pmu_event *pe;
497 pe = metricgroup__find_metric(cur->key, map);
501 if (!strcmp(metric_name, (char *)cur->key)) {
502 pr_warning("Recursion detected for metric %s\n", metric_name);
509 /* The metric key itself needs to go out.. */
510 expr__del_id(pctx, cur->key);
512 metric = malloc(sizeof(*metric));
518 ref = &metric->metric_ref;
519 ref->metric_name = pe->metric_name;
520 ref->metric_expr = pe->metric_expr;
521 list_add_tail(&metric->list, compound_list);
523 rc = expr__find_other(pe->metric_expr, NULL, pctx, 0);
526 break; /* The hashmap has been modified, so restart */
533 list_for_each_entry_safe(metric, tmp, compound_list, list)
540 static int test_parsing(void)
542 struct pmu_events_map *cpus_map = pmu_events_map__find();
543 struct pmu_events_map *map;
544 struct pmu_event *pe;
547 struct expr_parse_ctx ctx;
552 map = &pmu_events_map[i++];
557 struct metric *metric, *tmp;
558 struct hashmap_entry *cur;
559 LIST_HEAD(compound_list);
562 pe = &map->table[j++];
563 if (!pe->name && !pe->metric_group && !pe->metric_name)
565 if (!pe->metric_expr)
567 expr__ctx_init(&ctx);
568 if (expr__find_other(pe->metric_expr, NULL, &ctx, 0)
570 expr_failure("Parse other failed", map, pe);
575 if (resolve_metric_simple(&ctx, &compound_list, map,
577 expr_failure("Could not resolve metrics", map, pe);
579 goto exit; /* Don't tolerate errors due to severity */
583 * Add all ids with a made up value. The value may
584 * trigger divide by zero when subtracted and so try to
588 hashmap__for_each_entry((&ctx.ids), cur, bkt)
589 expr__add_id_val(&ctx, strdup(cur->key), k++);
591 hashmap__for_each_entry((&ctx.ids), cur, bkt) {
592 if (check_parse_cpu(cur->key, map == cpus_map,
597 list_for_each_entry_safe(metric, tmp, &compound_list, list) {
598 expr__add_ref(&ctx, &metric->metric_ref);
602 if (expr__parse(&result, &ctx, pe->metric_expr, 0)) {
603 expr_failure("Parse failed", map, pe);
606 expr__ctx_clear(&ctx);
609 /* TODO: fail when not ok */
611 return ret == 0 ? TEST_OK : TEST_SKIP;
618 static struct test_metric metrics[] = {
619 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
620 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
621 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
622 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
623 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
626 static int metric_parse_fake(const char *str)
628 struct expr_parse_ctx ctx;
629 struct hashmap_entry *cur;
635 pr_debug("parsing '%s'\n", str);
637 expr__ctx_init(&ctx);
638 if (expr__find_other(str, NULL, &ctx, 0) < 0) {
639 pr_err("expr__find_other failed\n");
644 * Add all ids with a made up value. The value may
645 * trigger divide by zero when subtracted and so try to
649 hashmap__for_each_entry((&ctx.ids), cur, bkt)
650 expr__add_id_val(&ctx, strdup(cur->key), i++);
652 hashmap__for_each_entry((&ctx.ids), cur, bkt) {
653 if (check_parse_fake(cur->key)) {
654 pr_err("check_parse_fake failed\n");
659 if (expr__parse(&result, &ctx, str, 0))
660 pr_err("expr__parse failed\n");
665 expr__ctx_clear(&ctx);
670 * Parse all the metrics for current architecture,
671 * or all defined cpus via the 'fake_pmu'
674 static int test_parsing_fake(void)
676 struct pmu_events_map *map;
677 struct pmu_event *pe;
681 for (i = 0; i < ARRAY_SIZE(metrics); i++) {
682 err = metric_parse_fake(metrics[i].str);
689 map = &pmu_events_map[i++];
694 pe = &map->table[j++];
695 if (!pe->name && !pe->metric_group && !pe->metric_name)
697 if (!pe->metric_expr)
699 err = metric_parse_fake(pe->metric_expr);
708 static const struct {
711 } pmu_events_testcase_table[] = {
713 .func = test_pmu_event_table,
714 .desc = "PMU event table sanity",
717 .func = test_aliases,
718 .desc = "PMU event map aliases",
721 .func = test_parsing,
722 .desc = "Parsing of PMU event table metrics",
725 .func = test_parsing_fake,
726 .desc = "Parsing of PMU event table metrics with fake PMUs",
730 const char *test__pmu_events_subtest_get_desc(int subtest)
733 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
735 return pmu_events_testcase_table[subtest].desc;
738 const char *test__pmu_events_subtest_skip_reason(int subtest)
741 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
743 if (pmu_events_testcase_table[subtest].func != test_parsing)
745 return "some metrics failed";
748 int test__pmu_events_subtest_get_nr(void)
750 return (int)ARRAY_SIZE(pmu_events_testcase_table);
753 int test__pmu_events(struct test *test __maybe_unused, int subtest)
756 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
758 return pmu_events_testcase_table[subtest].func();