1 // SPDX-License-Identifier: GPL-2.0
3 #include "parse-events.h"
9 #include <linux/kernel.h>
10 #include <linux/zalloc.h>
12 #include "../pmu-events/pmu-events.h"
13 #include <perf/evlist.h>
14 #include "util/evlist.h"
15 #include "util/expr.h"
16 #include "util/hashmap.h"
17 #include "util/parse-events.h"
18 #include "metricgroup.h"
21 struct perf_pmu_test_event
{
22 /* used for matching against events from generated pmu-events.c */
23 struct pmu_event event
;
25 /* used for matching against event aliases */
26 /* extra events for aliases */
27 const char *alias_str
;
30 * Note: For when PublicDescription does not exist in the JSON, we
31 * will have no long_desc in pmu_event.long_desc, but long_desc may
32 * be set in the alias.
34 const char *alias_long_desc
;
36 /* PMU which we should match against */
37 const char *matching_pmu
;
40 struct perf_pmu_test_pmu
{
42 struct perf_pmu_test_event
const *aliases
[10];
45 static const struct perf_pmu_test_event bp_l1_btb_correct
= {
47 .pmu
= "default_core",
48 .name
= "bp_l1_btb_correct",
49 .event
= "event=0x8a",
50 .desc
= "L1 BTB Correction",
53 .alias_str
= "event=0x8a",
54 .alias_long_desc
= "L1 BTB Correction",
57 static const struct perf_pmu_test_event bp_l2_btb_correct
= {
59 .pmu
= "default_core",
60 .name
= "bp_l2_btb_correct",
61 .event
= "event=0x8b",
62 .desc
= "L2 BTB Correction",
65 .alias_str
= "event=0x8b",
66 .alias_long_desc
= "L2 BTB Correction",
69 static const struct perf_pmu_test_event segment_reg_loads_any
= {
71 .pmu
= "default_core",
72 .name
= "segment_reg_loads.any",
73 .event
= "event=6,period=200000,umask=0x80",
74 .desc
= "Number of segment register loads",
77 .alias_str
= "event=0x6,period=0x30d40,umask=0x80",
78 .alias_long_desc
= "Number of segment register loads",
81 static const struct perf_pmu_test_event dispatch_blocked_any
= {
83 .pmu
= "default_core",
84 .name
= "dispatch_blocked.any",
85 .event
= "event=9,period=200000,umask=0x20",
86 .desc
= "Memory cluster signals to block micro-op dispatch for any reason",
89 .alias_str
= "event=0x9,period=0x30d40,umask=0x20",
90 .alias_long_desc
= "Memory cluster signals to block micro-op dispatch for any reason",
93 static const struct perf_pmu_test_event eist_trans
= {
95 .pmu
= "default_core",
97 .event
= "event=0x3a,period=200000",
98 .desc
= "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
101 .alias_str
= "event=0x3a,period=0x30d40",
102 .alias_long_desc
= "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
105 static const struct perf_pmu_test_event l3_cache_rd
= {
107 .pmu
= "default_core",
108 .name
= "l3_cache_rd",
109 .event
= "event=0x40",
110 .desc
= "L3 cache access, read",
111 .long_desc
= "Attributable Level 3 cache access, read",
114 .alias_str
= "event=0x40",
115 .alias_long_desc
= "Attributable Level 3 cache access, read",
118 static const struct perf_pmu_test_event
*core_events
[] = {
121 &segment_reg_loads_any
,
122 &dispatch_blocked_any
,
128 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd
= {
130 .name
= "uncore_hisi_ddrc.flux_wcmd",
132 .desc
= "DDRC write commands",
134 .long_desc
= "DDRC write commands",
135 .pmu
= "hisi_sccl,ddrc",
137 .alias_str
= "event=0x2",
138 .alias_long_desc
= "DDRC write commands",
139 .matching_pmu
= "hisi_sccl1_ddrc2",
142 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction
= {
144 .name
= "unc_cbo_xsnp_response.miss_eviction",
145 .event
= "event=0x22,umask=0x81",
146 .desc
= "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
148 .long_desc
= "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
149 .pmu
= "uncore_cbox",
151 .alias_str
= "event=0x22,umask=0x81",
152 .alias_long_desc
= "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
153 .matching_pmu
= "uncore_cbox_0",
156 static const struct perf_pmu_test_event uncore_hyphen
= {
158 .name
= "event-hyphen",
159 .event
= "event=0xe0",
160 .desc
= "UNC_CBO_HYPHEN",
162 .long_desc
= "UNC_CBO_HYPHEN",
163 .pmu
= "uncore_cbox",
165 .alias_str
= "event=0xe0",
166 .alias_long_desc
= "UNC_CBO_HYPHEN",
167 .matching_pmu
= "uncore_cbox_0",
170 static const struct perf_pmu_test_event uncore_two_hyph
= {
172 .name
= "event-two-hyph",
173 .event
= "event=0xc0",
174 .desc
= "UNC_CBO_TWO_HYPH",
176 .long_desc
= "UNC_CBO_TWO_HYPH",
177 .pmu
= "uncore_cbox",
179 .alias_str
= "event=0xc0",
180 .alias_long_desc
= "UNC_CBO_TWO_HYPH",
181 .matching_pmu
= "uncore_cbox_0",
184 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe
= {
186 .name
= "uncore_hisi_l3c.rd_hit_cpipe",
188 .desc
= "Total read hits",
190 .long_desc
= "Total read hits",
191 .pmu
= "hisi_sccl,l3c",
193 .alias_str
= "event=0x7",
194 .alias_long_desc
= "Total read hits",
195 .matching_pmu
= "hisi_sccl3_l3c7",
198 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss
= {
200 .name
= "uncore_imc_free_running.cache_miss",
201 .event
= "event=0x12",
202 .desc
= "Total cache misses",
204 .long_desc
= "Total cache misses",
205 .pmu
= "uncore_imc_free_running",
207 .alias_str
= "event=0x12",
208 .alias_long_desc
= "Total cache misses",
209 .matching_pmu
= "uncore_imc_free_running_0",
212 static const struct perf_pmu_test_event uncore_imc_cache_hits
= {
214 .name
= "uncore_imc.cache_hits",
215 .event
= "event=0x34",
216 .desc
= "Total cache hits",
218 .long_desc
= "Total cache hits",
221 .alias_str
= "event=0x34",
222 .alias_long_desc
= "Total cache hits",
223 .matching_pmu
= "uncore_imc_0",
226 static const struct perf_pmu_test_event
*uncore_events
[] = {
227 &uncore_hisi_ddrc_flux_wcmd
,
228 &unc_cbo_xsnp_response_miss_eviction
,
231 &uncore_hisi_l3c_rd_hit_cpipe
,
232 &uncore_imc_free_running_cache_miss
,
233 &uncore_imc_cache_hits
,
237 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles
= {
239 .name
= "sys_ddr_pmu.write_cycles",
240 .event
= "event=0x2b",
241 .desc
= "ddr write-cycles event",
243 .pmu
= "uncore_sys_ddr_pmu",
246 .alias_str
= "event=0x2b",
247 .alias_long_desc
= "ddr write-cycles event",
248 .matching_pmu
= "uncore_sys_ddr_pmu0",
251 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles
= {
253 .name
= "sys_ccn_pmu.read_cycles",
254 .event
= "config=0x2c",
255 .desc
= "ccn read-cycles event",
257 .pmu
= "uncore_sys_ccn_pmu",
260 .alias_str
= "config=0x2c",
261 .alias_long_desc
= "ccn read-cycles event",
262 .matching_pmu
= "uncore_sys_ccn_pmu4",
265 static const struct perf_pmu_test_event sys_cmn_pmu_hnf_cache_miss
= {
267 .name
= "sys_cmn_pmu.hnf_cache_miss",
268 .event
= "eventid=1,type=5",
269 .desc
= "Counts total cache misses in first lookup result (high priority)",
271 .pmu
= "uncore_sys_cmn_pmu",
272 .compat
= "(434|436|43c|43a).*",
274 .alias_str
= "eventid=0x1,type=0x5",
275 .alias_long_desc
= "Counts total cache misses in first lookup result (high priority)",
276 .matching_pmu
= "uncore_sys_cmn_pmu0",
279 static const struct perf_pmu_test_event
*sys_events
[] = {
280 &sys_ddr_pmu_write_cycles
,
281 &sys_ccn_pmu_read_cycles
,
282 &sys_cmn_pmu_hnf_cache_miss
,
286 static bool is_same(const char *reference
, const char *test
)
288 if (!reference
&& !test
)
291 if (reference
&& !test
)
294 if (!reference
&& test
)
297 return !strcmp(reference
, test
);
300 static int compare_pmu_events(const struct pmu_event
*e1
, const struct pmu_event
*e2
)
302 if (!is_same(e1
->name
, e2
->name
)) {
303 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
304 e1
->name
, e1
->name
, e2
->name
);
308 if (!is_same(e1
->compat
, e2
->compat
)) {
309 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
310 e1
->name
, e1
->compat
, e2
->compat
);
314 if (!is_same(e1
->event
, e2
->event
)) {
315 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
316 e1
->name
, e1
->event
, e2
->event
);
320 if (!is_same(e1
->desc
, e2
->desc
)) {
321 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
322 e1
->name
, e1
->desc
, e2
->desc
);
326 if (!is_same(e1
->topic
, e2
->topic
)) {
327 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
328 e1
->name
, e1
->topic
, e2
->topic
);
332 if (!is_same(e1
->long_desc
, e2
->long_desc
)) {
333 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
334 e1
->name
, e1
->long_desc
, e2
->long_desc
);
338 if (!is_same(e1
->pmu
, e2
->pmu
)) {
339 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
340 e1
->name
, e1
->pmu
, e2
->pmu
);
344 if (!is_same(e1
->unit
, e2
->unit
)) {
345 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
346 e1
->name
, e1
->unit
, e2
->unit
);
350 if (e1
->perpkg
!= e2
->perpkg
) {
351 pr_debug2("testing event e1 %s: mismatched perpkg, %d vs %d\n",
352 e1
->name
, e1
->perpkg
, e2
->perpkg
);
356 if (e1
->deprecated
!= e2
->deprecated
) {
357 pr_debug2("testing event e1 %s: mismatched deprecated, %d vs %d\n",
358 e1
->name
, e1
->deprecated
, e2
->deprecated
);
365 static int compare_alias_to_test_event(struct pmu_event_info
*alias
,
366 struct perf_pmu_test_event
const *test_event
,
367 char const *pmu_name
)
369 struct pmu_event
const *event
= &test_event
->event
;
371 /* An alias was found, ensure everything is in order */
372 if (!is_same(alias
->name
, event
->name
)) {
373 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
374 pmu_name
, alias
->name
, event
->name
);
378 if (!is_same(alias
->desc
, event
->desc
)) {
379 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
380 pmu_name
, alias
->desc
, event
->desc
);
384 if (!is_same(alias
->long_desc
, test_event
->alias_long_desc
)) {
385 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
386 pmu_name
, alias
->long_desc
,
387 test_event
->alias_long_desc
);
391 if (!is_same(alias
->topic
, event
->topic
)) {
392 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
393 pmu_name
, alias
->topic
, event
->topic
);
397 if (!is_same(alias
->str
, test_event
->alias_str
)) {
398 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
399 pmu_name
, alias
->str
, test_event
->alias_str
);
403 if (!is_same(alias
->long_desc
, test_event
->alias_long_desc
)) {
404 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
405 pmu_name
, alias
->str
, test_event
->alias_long_desc
);
409 if (!is_same(alias
->pmu_name
, test_event
->event
.pmu
) &&
410 !is_same(alias
->pmu_name
, "default_core")) {
411 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
412 pmu_name
, alias
->pmu_name
, test_event
->event
.pmu
);
419 static int test__pmu_event_table_core_callback(const struct pmu_event
*pe
,
420 const struct pmu_events_table
*table __maybe_unused
,
423 int *map_events
= data
;
424 struct perf_pmu_test_event
const **test_event_table
;
427 if (strcmp(pe
->pmu
, "default_core"))
428 test_event_table
= &uncore_events
[0];
430 test_event_table
= &core_events
[0];
432 for (; *test_event_table
; test_event_table
++) {
433 struct perf_pmu_test_event
const *test_event
= *test_event_table
;
434 struct pmu_event
const *event
= &test_event
->event
;
436 if (strcmp(pe
->name
, event
->name
))
441 if (compare_pmu_events(pe
, event
))
444 pr_debug("testing event table %s: pass\n", pe
->name
);
447 pr_err("testing event table: could not find event %s\n", pe
->name
);
453 static int test__pmu_event_table_sys_callback(const struct pmu_event
*pe
,
454 const struct pmu_events_table
*table __maybe_unused
,
457 int *map_events
= data
;
458 struct perf_pmu_test_event
const **test_event_table
;
461 test_event_table
= &sys_events
[0];
463 for (; *test_event_table
; test_event_table
++) {
464 struct perf_pmu_test_event
const *test_event
= *test_event_table
;
465 struct pmu_event
const *event
= &test_event
->event
;
467 if (strcmp(pe
->name
, event
->name
))
472 if (compare_pmu_events(pe
, event
))
475 pr_debug("testing sys event table %s: pass\n", pe
->name
);
478 pr_debug("testing sys event table: could not find event %s\n", pe
->name
);
484 /* Verify generated events from pmu-events.c are as expected */
485 static int test__pmu_event_table(struct test_suite
*test __maybe_unused
,
486 int subtest __maybe_unused
)
488 const struct pmu_events_table
*sys_event_table
=
489 find_sys_events_table("pmu_events__test_soc_sys");
490 const struct pmu_events_table
*table
= find_core_events_table("testarch", "testcpu");
491 int map_events
= 0, expected_events
, err
;
493 /* ignore 3x sentinels */
494 expected_events
= ARRAY_SIZE(core_events
) +
495 ARRAY_SIZE(uncore_events
) +
496 ARRAY_SIZE(sys_events
) - 3;
498 if (!table
|| !sys_event_table
)
501 err
= pmu_events_table__for_each_event(table
, /*pmu=*/ NULL
,
502 test__pmu_event_table_core_callback
,
507 err
= pmu_events_table__for_each_event(sys_event_table
, /*pmu=*/ NULL
,
508 test__pmu_event_table_sys_callback
,
513 if (map_events
!= expected_events
) {
514 pr_err("testing event table: found %d, but expected %d\n",
515 map_events
, expected_events
);
522 struct test_core_pmu_event_aliases_cb_args
{
523 struct perf_pmu_test_event
const *test_event
;
527 static int test_core_pmu_event_aliases_cb(void *state
, struct pmu_event_info
*alias
)
529 struct test_core_pmu_event_aliases_cb_args
*args
= state
;
531 if (compare_alias_to_test_event(alias
, args
->test_event
, alias
->pmu
->name
))
534 pr_debug2("testing aliases core PMU %s: matched event %s\n",
535 alias
->pmu_name
, alias
->name
);
539 /* Verify aliases are as expected */
540 static int __test_core_pmu_event_aliases(const char *pmu_name
, int *count
)
542 struct perf_pmu_test_event
const **test_event_table
;
543 struct perf_pmu
*pmu
;
545 const struct pmu_events_table
*table
= find_core_events_table("testarch", "testcpu");
550 test_event_table
= &core_events
[0];
552 pmu
= zalloc(sizeof(*pmu
));
556 INIT_LIST_HEAD(&pmu
->format
);
557 INIT_LIST_HEAD(&pmu
->aliases
);
558 INIT_LIST_HEAD(&pmu
->caps
);
559 INIT_LIST_HEAD(&pmu
->list
);
560 pmu
->name
= strdup(pmu_name
);
563 pmu
->events_table
= table
;
564 pmu_add_cpu_aliases_table(pmu
, table
);
565 pmu
->cpu_aliases_added
= true;
566 pmu
->sysfs_aliases_loaded
= true;
568 res
= pmu_events_table__find_event(table
, pmu
, "bp_l1_btb_correct", NULL
, NULL
);
570 pr_debug("Missing test event in test architecture");
573 for (; *test_event_table
; test_event_table
++) {
574 struct perf_pmu_test_event test_event
= **test_event_table
;
575 struct pmu_event
const *event
= &test_event
.event
;
576 struct test_core_pmu_event_aliases_cb_args args
= {
577 .test_event
= &test_event
,
582 test_event
.event
.pmu
= pmu_name
;
583 err
= perf_pmu__find_event(pmu
, event
->name
, &args
,
584 test_core_pmu_event_aliases_cb
);
588 perf_pmu__delete(pmu
);
593 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu
*test_pmu
)
595 int alias_count
= 0, to_match_count
= 0, matched_count
= 0;
596 struct perf_pmu_test_event
const **table
;
597 struct perf_pmu
*pmu
= &test_pmu
->pmu
;
598 const char *pmu_name
= pmu
->name
;
599 const struct pmu_events_table
*events_table
;
602 events_table
= find_core_events_table("testarch", "testcpu");
605 pmu
->events_table
= events_table
;
606 pmu_add_cpu_aliases_table(pmu
, events_table
);
607 pmu
->cpu_aliases_added
= true;
608 pmu
->sysfs_aliases_loaded
= true;
609 pmu_add_sys_aliases(pmu
);
611 /* Count how many aliases we generated */
612 alias_count
= perf_pmu__num_events(pmu
);
614 /* Count how many aliases we expect from the known table */
615 for (table
= &test_pmu
->aliases
[0]; *table
; table
++)
618 if (alias_count
!= to_match_count
) {
619 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
620 pmu_name
, to_match_count
, alias_count
);
624 for (table
= &test_pmu
->aliases
[0]; *table
; table
++) {
625 struct perf_pmu_test_event test_event
= **table
;
626 struct pmu_event
const *event
= &test_event
.event
;
628 struct test_core_pmu_event_aliases_cb_args args
= {
629 .test_event
= &test_event
,
630 .count
= &matched_count
,
633 if (strcmp(pmu_name
, test_event
.matching_pmu
)) {
634 pr_debug("testing aliases uncore PMU %s: mismatched matching_pmu, %s vs %s\n",
635 pmu_name
, test_event
.matching_pmu
, pmu_name
);
639 err
= perf_pmu__find_event(pmu
, event
->name
, &args
,
640 test_core_pmu_event_aliases_cb
);
643 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
644 pmu_name
, event
->name
);
649 if (alias_count
!= matched_count
) {
650 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
651 pmu_name
, matched_count
, alias_count
);
657 static struct perf_pmu_test_pmu test_pmus
[] = {
660 .name
= "hisi_sccl1_ddrc2",
664 &uncore_hisi_ddrc_flux_wcmd
,
669 .name
= "uncore_cbox_0",
673 &unc_cbo_xsnp_response_miss_eviction
,
680 .name
= "hisi_sccl3_l3c7",
684 &uncore_hisi_l3c_rd_hit_cpipe
,
689 .name
= "uncore_imc_free_running_0",
693 &uncore_imc_free_running_cache_miss
,
698 .name
= "uncore_imc_0",
702 &uncore_imc_cache_hits
,
707 .name
= "uncore_sys_ddr_pmu0",
712 &sys_ddr_pmu_write_cycles
,
717 .name
= "uncore_sys_ccn_pmu4",
722 &sys_ccn_pmu_read_cycles
,
727 .name
= (char *)"uncore_sys_cmn_pmu0",
729 .id
= (char *)"43401",
732 &sys_cmn_pmu_hnf_cache_miss
,
737 .name
= (char *)"uncore_sys_cmn_pmu0",
739 .id
= (char *)"43602",
742 &sys_cmn_pmu_hnf_cache_miss
,
747 .name
= (char *)"uncore_sys_cmn_pmu0",
749 .id
= (char *)"43c03",
752 &sys_cmn_pmu_hnf_cache_miss
,
757 .name
= (char *)"uncore_sys_cmn_pmu0",
759 .id
= (char *)"43a01",
762 &sys_cmn_pmu_hnf_cache_miss
,
767 /* Test that aliases generated are as expected */
768 static int test__aliases(struct test_suite
*test __maybe_unused
,
769 int subtest __maybe_unused
)
771 struct perf_pmu
*pmu
= NULL
;
774 while ((pmu
= perf_pmus__scan_core(pmu
)) != NULL
) {
777 if (list_empty(&pmu
->format
)) {
778 pr_debug2("skipping testing core PMU %s\n", pmu
->name
);
782 if (__test_core_pmu_event_aliases(pmu
->name
, &count
)) {
783 pr_debug("testing core PMU %s aliases: failed\n", pmu
->name
);
788 pr_debug("testing core PMU %s aliases: no events to match\n",
793 pr_debug("testing core PMU %s aliases: pass\n", pmu
->name
);
796 for (i
= 0; i
< ARRAY_SIZE(test_pmus
); i
++) {
799 INIT_LIST_HEAD(&test_pmus
[i
].pmu
.format
);
800 INIT_LIST_HEAD(&test_pmus
[i
].pmu
.aliases
);
801 INIT_LIST_HEAD(&test_pmus
[i
].pmu
.caps
);
803 res
= __test_uncore_pmu_event_aliases(&test_pmus
[i
]);
811 static bool is_number(const char *str
)
817 v
= strtod(str
, &end_ptr
);
818 (void)v
; // We're not interested in this value, only if it is valid
819 return errno
== 0 && end_ptr
!= str
;
822 static int check_parse_id(const char *id
, struct parse_events_error
*error
)
824 struct evlist
*evlist
;
828 /* Numbers are always valid. */
832 evlist
= evlist__new();
840 for (cur
= strchr(dup
, '@') ; cur
; cur
= strchr(++cur
, '@'))
843 ret
= __parse_events(evlist
, dup
, /*pmu_filter=*/NULL
, error
, /*fake_pmu=*/true,
844 /*warn_if_reordered=*/true, /*fake_tp=*/false);
847 evlist__delete(evlist
);
851 static int check_parse_fake(const char *id
)
853 struct parse_events_error error
;
856 parse_events_error__init(&error
);
857 ret
= check_parse_id(id
, &error
);
858 parse_events_error__exit(&error
);
863 struct list_head list
;
864 struct metric_ref metric_ref
;
867 static int test__parsing_callback(const struct pmu_metric
*pm
,
868 const struct pmu_metrics_table
*table
,
871 int *failures
= data
;
873 struct evlist
*evlist
;
874 struct perf_cpu_map
*cpus
;
876 struct rblist metric_events
= {
881 if (!pm
->metric_expr
)
884 pr_debug("Found metric '%s'\n", pm
->metric_name
);
888 * We need to prepare evlist for stat mode running on CPU 0
889 * because that's where all the stats are going to be created.
891 evlist
= evlist__new();
895 cpus
= perf_cpu_map__new("0");
897 evlist__delete(evlist
);
901 perf_evlist__set_maps(&evlist
->core
, cpus
, NULL
);
903 err
= metricgroup__parse_groups_test(evlist
, table
, pm
->metric_name
, &metric_events
);
905 if (!strcmp(pm
->metric_name
, "M1") || !strcmp(pm
->metric_name
, "M2") ||
906 !strcmp(pm
->metric_name
, "M3")) {
908 pr_debug("Expected broken metric %s skipping\n", pm
->metric_name
);
914 err
= evlist__alloc_stats(/*config=*/NULL
, evlist
, /*alloc_raw=*/false);
918 * Add all ids with a made up value. The value may trigger divide by
919 * zero when subtracted and so try to make them unique.
922 evlist__alloc_aggr_stats(evlist
, 1);
923 evlist__for_each_entry(evlist
, evsel
) {
924 evsel
->stats
->aggr
->counts
.val
= k
;
925 if (evsel__name_is(evsel
, "duration_time"))
926 update_stats(&walltime_nsecs_stats
, k
);
929 evlist__for_each_entry(evlist
, evsel
) {
930 struct metric_event
*me
= metricgroup__lookup(&metric_events
, evsel
, false);
933 struct metric_expr
*mexp
;
935 list_for_each_entry (mexp
, &me
->head
, nd
) {
936 if (strcmp(mexp
->metric_name
, pm
->metric_name
))
938 pr_debug("Result %f\n", test_generic_metric(mexp
, 0));
945 pr_debug("Didn't find parsed metric %s", pm
->metric_name
);
949 pr_debug("Broken metric %s\n", pm
->metric_name
);
952 metricgroup__rblist_exit(&metric_events
);
953 evlist__free_stats(evlist
);
954 perf_cpu_map__put(cpus
);
955 evlist__delete(evlist
);
959 static int test__parsing(struct test_suite
*test __maybe_unused
,
960 int subtest __maybe_unused
)
964 pmu_for_each_core_metric(test__parsing_callback
, &failures
);
965 pmu_for_each_sys_metric(test__parsing_callback
, &failures
);
967 return failures
== 0 ? TEST_OK
: TEST_FAIL
;
974 static struct test_metric metrics
[] = {
975 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
976 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
977 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
978 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
979 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
982 static int metric_parse_fake(const char *metric_name
, const char *str
)
984 struct expr_parse_ctx
*ctx
;
985 struct hashmap_entry
*cur
;
991 pr_debug("parsing '%s': '%s'\n", metric_name
, str
);
993 ctx
= expr__ctx_new();
995 pr_debug("expr__ctx_new failed");
998 ctx
->sctx
.is_test
= true;
999 if (expr__find_ids(str
, NULL
, ctx
) < 0) {
1000 pr_err("expr__find_ids failed\n");
1005 * Add all ids with a made up value. The value may
1006 * trigger divide by zero when subtracted and so try to
1010 hashmap__for_each_entry(ctx
->ids
, cur
, bkt
)
1011 expr__add_id_val(ctx
, strdup(cur
->pkey
), i
++);
1013 hashmap__for_each_entry(ctx
->ids
, cur
, bkt
) {
1014 if (check_parse_fake(cur
->pkey
)) {
1015 pr_err("check_parse_fake failed\n");
1021 if (expr__parse(&result
, ctx
, str
)) {
1023 * Parsing failed, make numbers go from large to small which can
1024 * resolve divide by zero issues.
1027 hashmap__for_each_entry(ctx
->ids
, cur
, bkt
)
1028 expr__add_id_val(ctx
, strdup(cur
->pkey
), i
--);
1029 if (expr__parse(&result
, ctx
, str
)) {
1030 pr_err("expr__parse failed for %s\n", metric_name
);
1031 /* The following have hard to avoid divide by zero. */
1032 if (!strcmp(metric_name
, "tma_clears_resteers") ||
1033 !strcmp(metric_name
, "tma_mispredicts_resteers"))
1041 expr__ctx_free(ctx
);
1045 static int test__parsing_fake_callback(const struct pmu_metric
*pm
,
1046 const struct pmu_metrics_table
*table __maybe_unused
,
1047 void *data __maybe_unused
)
1049 return metric_parse_fake(pm
->metric_name
, pm
->metric_expr
);
1053 * Parse all the metrics for current architecture, or all defined cpus via the
1054 * 'fake_pmu' in parse_events.
1056 static int test__parsing_fake(struct test_suite
*test __maybe_unused
,
1057 int subtest __maybe_unused
)
1061 for (size_t i
= 0; i
< ARRAY_SIZE(metrics
); i
++) {
1062 err
= metric_parse_fake("", metrics
[i
].str
);
1067 err
= pmu_for_each_core_metric(test__parsing_fake_callback
, NULL
);
1071 return pmu_for_each_sys_metric(test__parsing_fake_callback
, NULL
);
1074 static int test__parsing_threshold_callback(const struct pmu_metric
*pm
,
1075 const struct pmu_metrics_table
*table __maybe_unused
,
1076 void *data __maybe_unused
)
1078 if (!pm
->metric_threshold
)
1080 return metric_parse_fake(pm
->metric_name
, pm
->metric_threshold
);
1083 static int test__parsing_threshold(struct test_suite
*test __maybe_unused
,
1084 int subtest __maybe_unused
)
1088 err
= pmu_for_each_core_metric(test__parsing_threshold_callback
, NULL
);
1092 return pmu_for_each_sys_metric(test__parsing_threshold_callback
, NULL
);
1095 static struct test_case pmu_events_tests
[] = {
1096 TEST_CASE("PMU event table sanity", pmu_event_table
),
1097 TEST_CASE("PMU event map aliases", aliases
),
1098 TEST_CASE_REASON("Parsing of PMU event table metrics", parsing
,
1099 "some metrics failed"),
1100 TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake
),
1101 TEST_CASE("Parsing of metric thresholds with fake PMUs", parsing_threshold
),
1105 struct test_suite suite__pmu_events
= {
1106 .desc
= "PMU JSON event tests",
1107 .test_cases
= pmu_events_tests
,