1 // SPDX-License-Identifier: GPL-2.0
7 #include "metricgroup.h"
8 #include "parse-events.h"
9 #include "pmu-events/pmu-events.h"
11 #include <subcmd/parse-options.h>
16 static int test_expand_events(struct evlist
*evlist
,
17 struct rblist
*metric_events
)
19 int i
, ret
= TEST_FAIL
;
22 int nr_members
; /* for the first evsel only */
23 const char cgrp_str
[] = "A,B,C";
24 const char *cgrp_name
[] = { "A", "B", "C" };
25 int nr_cgrps
= ARRAY_SIZE(cgrp_name
);
29 TEST_ASSERT_VAL("evlist is empty", !evlist__empty(evlist
));
31 nr_events
= evlist
->core
.nr_entries
;
32 ev_name
= calloc(nr_events
, sizeof(*ev_name
));
33 if (ev_name
== NULL
) {
34 pr_debug("memory allocation failure\n");
38 evlist__for_each_entry(evlist
, evsel
) {
39 ev_name
[i
] = strdup(evsel
->name
);
40 if (ev_name
[i
] == NULL
) {
41 pr_debug("memory allocation failure\n");
46 /* remember grouping info */
47 was_group_event
= evsel__is_group_event(evlist__first(evlist
));
48 nr_members
= evlist__first(evlist
)->core
.nr_members
;
50 ret
= evlist__expand_cgroup(evlist
, cgrp_str
, metric_events
, false);
52 pr_debug("failed to expand events for cgroups\n");
57 if (evlist
->core
.nr_entries
!= nr_events
* nr_cgrps
) {
58 pr_debug("event count doesn't match\n");
63 evlist__for_each_entry(evlist
, evsel
) {
64 if (strcmp(evsel
->name
, ev_name
[i
% nr_events
])) {
65 pr_debug("event name doesn't match:\n");
66 pr_debug(" evsel[%d]: %s\n expected: %s\n",
67 i
, evsel
->name
, ev_name
[i
% nr_events
]);
70 if (strcmp(evsel
->cgrp
->name
, cgrp_name
[i
/ nr_events
])) {
71 pr_debug("cgroup name doesn't match:\n");
72 pr_debug(" evsel[%d]: %s\n expected: %s\n",
73 i
, evsel
->cgrp
->name
, cgrp_name
[i
/ nr_events
]);
77 if ((i
% nr_events
) == 0) {
78 if (evsel__is_group_event(evsel
) != was_group_event
) {
79 pr_debug("event group doesn't match: got %s, expect %s\n",
80 evsel__is_group_event(evsel
) ? "true" : "false",
81 was_group_event
? "true" : "false");
84 if (evsel
->core
.nr_members
!= nr_members
) {
85 pr_debug("event group member doesn't match: %d vs %d\n",
86 evsel
->core
.nr_members
, nr_members
);
94 out
: for (i
= 0; i
< nr_events
; i
++)
100 static int expand_default_events(void)
103 struct rblist metric_events
;
104 struct evlist
*evlist
= evlist__new_default();
106 TEST_ASSERT_VAL("failed to get evlist", evlist
);
108 rblist__init(&metric_events
);
109 ret
= test_expand_events(evlist
, &metric_events
);
110 evlist__delete(evlist
);
114 static int expand_group_events(void)
117 struct evlist
*evlist
;
118 struct rblist metric_events
;
119 struct parse_events_error err
;
120 const char event_str
[] = "{cycles,instructions}";
122 symbol_conf
.event_group
= true;
124 evlist
= evlist__new();
125 TEST_ASSERT_VAL("failed to get evlist", evlist
);
127 ret
= parse_events(evlist
, event_str
, &err
);
129 pr_debug("failed to parse event '%s', err %d, str '%s'\n",
130 event_str
, ret
, err
.str
);
131 parse_events_print_error(&err
, event_str
);
135 rblist__init(&metric_events
);
136 ret
= test_expand_events(evlist
, &metric_events
);
138 evlist__delete(evlist
);
142 static int expand_libpfm_events(void)
145 struct evlist
*evlist
;
146 struct rblist metric_events
;
147 const char event_str
[] = "CYCLES";
148 struct option opt
= {
152 symbol_conf
.event_group
= true;
154 evlist
= evlist__new();
155 TEST_ASSERT_VAL("failed to get evlist", evlist
);
157 ret
= parse_libpfm_events_option(&opt
, event_str
, 0);
159 pr_debug("failed to parse libpfm event '%s', err %d\n",
163 if (evlist__empty(evlist
)) {
164 pr_debug("libpfm was not enabled\n");
168 rblist__init(&metric_events
);
169 ret
= test_expand_events(evlist
, &metric_events
);
171 evlist__delete(evlist
);
175 static int expand_metric_events(void)
178 struct evlist
*evlist
;
179 struct rblist metric_events
;
180 const char metric_str
[] = "CPI";
182 struct pmu_event pme_test
[] = {
184 .metric_expr
= "instructions / cycles",
185 .metric_name
= "IPC",
188 .metric_expr
= "1 / IPC",
189 .metric_name
= "CPI",
196 struct pmu_events_map ev_map
= {
203 evlist
= evlist__new();
204 TEST_ASSERT_VAL("failed to get evlist", evlist
);
206 rblist__init(&metric_events
);
207 ret
= metricgroup__parse_groups_test(evlist
, &ev_map
, metric_str
,
208 false, false, &metric_events
);
210 pr_debug("failed to parse '%s' metric\n", metric_str
);
214 ret
= test_expand_events(evlist
, &metric_events
);
217 metricgroup__rblist_exit(&metric_events
);
218 evlist__delete(evlist
);
222 int test__expand_cgroup_events(struct test
*test __maybe_unused
,
223 int subtest __maybe_unused
)
227 ret
= expand_default_events();
228 TEST_ASSERT_EQUAL("failed to expand default events", ret
, 0);
230 ret
= expand_group_events();
231 TEST_ASSERT_EQUAL("failed to expand event group", ret
, 0);
233 ret
= expand_libpfm_events();
234 TEST_ASSERT_EQUAL("failed to expand event group", ret
, 0);
236 ret
= expand_metric_events();
237 TEST_ASSERT_EQUAL("failed to expand metric events", ret
, 0);