WIP FPC-III support
[linux/fpc-iii.git] / tools / perf / tests / pfm.c
blob76a53126efdf173bdd035017a547a56b0739c081
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test support for libpfm4 event encodings.
5 * Copyright 2020 Google LLC.
6 */
7 #include "tests.h"
8 #include "util/debug.h"
9 #include "util/evlist.h"
10 #include "util/pfm.h"
12 #include <linux/kernel.h>
14 #ifdef HAVE_LIBPFM
15 static int test__pfm_events(void);
16 static int test__pfm_group(void);
17 #endif
19 static const struct {
20 int (*func)(void);
21 const char *desc;
22 } pfm_testcase_table[] = {
23 #ifdef HAVE_LIBPFM
25 .func = test__pfm_events,
26 .desc = "test of individual --pfm-events",
29 .func = test__pfm_group,
30 .desc = "test groups of --pfm-events",
32 #endif
35 #ifdef HAVE_LIBPFM
36 static int count_pfm_events(struct perf_evlist *evlist)
38 struct perf_evsel *evsel;
39 int count = 0;
41 perf_evlist__for_each_entry(evlist, evsel) {
42 count++;
44 return count;
47 static int test__pfm_events(void)
49 struct evlist *evlist;
50 struct option opt;
51 size_t i;
52 const struct {
53 const char *events;
54 int nr_events;
55 } table[] = {
57 .events = "",
58 .nr_events = 0,
61 .events = "instructions",
62 .nr_events = 1,
65 .events = "instructions,cycles",
66 .nr_events = 2,
69 .events = "stereolab",
70 .nr_events = 0,
73 .events = "instructions,instructions",
74 .nr_events = 2,
77 .events = "stereolab,instructions",
78 .nr_events = 0,
81 .events = "instructions,stereolab",
82 .nr_events = 1,
86 for (i = 0; i < ARRAY_SIZE(table); i++) {
87 evlist = evlist__new();
88 if (evlist == NULL)
89 return -ENOMEM;
91 opt.value = evlist;
92 parse_libpfm_events_option(&opt,
93 table[i].events,
94 0);
95 TEST_ASSERT_EQUAL(table[i].events,
96 count_pfm_events(&evlist->core),
97 table[i].nr_events);
98 TEST_ASSERT_EQUAL(table[i].events,
99 evlist->nr_groups,
102 evlist__delete(evlist);
104 return 0;
107 static int test__pfm_group(void)
109 struct evlist *evlist;
110 struct option opt;
111 size_t i;
112 const struct {
113 const char *events;
114 int nr_events;
115 int nr_groups;
116 } table[] = {
118 .events = "{},",
119 .nr_events = 0,
120 .nr_groups = 0,
123 .events = "{instructions}",
124 .nr_events = 1,
125 .nr_groups = 1,
128 .events = "{instructions},{}",
129 .nr_events = 1,
130 .nr_groups = 1,
133 .events = "{},{instructions}",
134 .nr_events = 0,
135 .nr_groups = 0,
138 .events = "{instructions},{instructions}",
139 .nr_events = 2,
140 .nr_groups = 2,
143 .events = "{instructions,cycles},{instructions,cycles}",
144 .nr_events = 4,
145 .nr_groups = 2,
148 .events = "{stereolab}",
149 .nr_events = 0,
150 .nr_groups = 0,
153 .events =
154 "{instructions,cycles},{instructions,stereolab}",
155 .nr_events = 3,
156 .nr_groups = 1,
160 for (i = 0; i < ARRAY_SIZE(table); i++) {
161 evlist = evlist__new();
162 if (evlist == NULL)
163 return -ENOMEM;
165 opt.value = evlist;
166 parse_libpfm_events_option(&opt,
167 table[i].events,
169 TEST_ASSERT_EQUAL(table[i].events,
170 count_pfm_events(&evlist->core),
171 table[i].nr_events);
172 TEST_ASSERT_EQUAL(table[i].events,
173 evlist->nr_groups,
174 table[i].nr_groups);
176 evlist__delete(evlist);
178 return 0;
180 #endif
182 const char *test__pfm_subtest_get_desc(int i)
184 if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
185 return NULL;
186 return pfm_testcase_table[i].desc;
189 int test__pfm_subtest_get_nr(void)
191 return (int)ARRAY_SIZE(pfm_testcase_table);
194 int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
196 #ifdef HAVE_LIBPFM
197 if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
198 return TEST_FAIL;
199 return pfm_testcase_table[i].func();
200 #else
201 return TEST_SKIP;
202 #endif