Skip to content

Commit cff7f95

Browse files
Jiri Olsaacmel
Jiri Olsa
authored andcommitted
perf tests: Move pmu tests into separate object
Separating pmu's object tests into pmu object under tests directory. Signed-off-by: Jiri Olsa <[email protected]> Cc: Corey Ashford <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent dc447ee commit cff7f95

File tree

6 files changed

+191
-185
lines changed

6 files changed

+191
-185
lines changed

tools/perf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ LIB_OBJS += $(OUTPUT)tests/perf-record.o
440440
LIB_OBJS += $(OUTPUT)tests/rdpmc.o
441441
LIB_OBJS += $(OUTPUT)tests/evsel-roundtrip-name.o
442442
LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o
443+
LIB_OBJS += $(OUTPUT)tests/pmu.o
443444
LIB_OBJS += $(OUTPUT)tests/util.o
444445

445446
BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o

tools/perf/tests/builtin-test.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
#include <sched.h>
3131

3232

33-
static int test__perf_pmu(void)
34-
{
35-
return perf_pmu__test();
36-
}
37-
3833
static struct test {
3934
const char *desc;
4035
int (*func)(void);
@@ -71,7 +66,7 @@ static struct test {
7166
},
7267
{
7368
.desc = "Test perf pmu format parsing",
74-
.func = test__perf_pmu,
69+
.func = test__pmu,
7570
},
7671
{
7772
.desc = "Test dso data interface",

tools/perf/tests/pmu.c

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "parse-events.h"
2+
#include "pmu.h"
3+
#include "util.h"
4+
#include "tests.h"
5+
6+
/* Simulated format definitions. */
7+
static struct test_format {
8+
const char *name;
9+
const char *value;
10+
} test_formats[] = {
11+
{ "krava01", "config:0-1,62-63\n", },
12+
{ "krava02", "config:10-17\n", },
13+
{ "krava03", "config:5\n", },
14+
{ "krava11", "config1:0,2,4,6,8,20-28\n", },
15+
{ "krava12", "config1:63\n", },
16+
{ "krava13", "config1:45-47\n", },
17+
{ "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
18+
{ "krava22", "config2:8,18,48,58\n", },
19+
{ "krava23", "config2:28-29,38\n", },
20+
};
21+
22+
#define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
23+
24+
/* Simulated users input. */
25+
static struct parse_events__term test_terms[] = {
26+
{
27+
.config = (char *) "krava01",
28+
.val.num = 15,
29+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
30+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
31+
},
32+
{
33+
.config = (char *) "krava02",
34+
.val.num = 170,
35+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
36+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
37+
},
38+
{
39+
.config = (char *) "krava03",
40+
.val.num = 1,
41+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
42+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
43+
},
44+
{
45+
.config = (char *) "krava11",
46+
.val.num = 27,
47+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
48+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
49+
},
50+
{
51+
.config = (char *) "krava12",
52+
.val.num = 1,
53+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
54+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
55+
},
56+
{
57+
.config = (char *) "krava13",
58+
.val.num = 2,
59+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
60+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
61+
},
62+
{
63+
.config = (char *) "krava21",
64+
.val.num = 119,
65+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
66+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
67+
},
68+
{
69+
.config = (char *) "krava22",
70+
.val.num = 11,
71+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
72+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
73+
},
74+
{
75+
.config = (char *) "krava23",
76+
.val.num = 2,
77+
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
78+
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
79+
},
80+
};
81+
#define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
82+
83+
/*
84+
* Prepare format directory data, exported by kernel
85+
* at /sys/bus/event_source/devices/<dev>/format.
86+
*/
87+
static char *test_format_dir_get(void)
88+
{
89+
static char dir[PATH_MAX];
90+
unsigned int i;
91+
92+
snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
93+
if (!mkdtemp(dir))
94+
return NULL;
95+
96+
for (i = 0; i < TEST_FORMATS_CNT; i++) {
97+
static char name[PATH_MAX];
98+
struct test_format *format = &test_formats[i];
99+
FILE *file;
100+
101+
snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
102+
103+
file = fopen(name, "w");
104+
if (!file)
105+
return NULL;
106+
107+
if (1 != fwrite(format->value, strlen(format->value), 1, file))
108+
break;
109+
110+
fclose(file);
111+
}
112+
113+
return dir;
114+
}
115+
116+
/* Cleanup format directory. */
117+
static int test_format_dir_put(char *dir)
118+
{
119+
char buf[PATH_MAX];
120+
snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
121+
if (system(buf))
122+
return -1;
123+
124+
snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
125+
return system(buf);
126+
}
127+
128+
static struct list_head *test_terms_list(void)
129+
{
130+
static LIST_HEAD(terms);
131+
unsigned int i;
132+
133+
for (i = 0; i < TERMS_CNT; i++)
134+
list_add_tail(&test_terms[i].list, &terms);
135+
136+
return &terms;
137+
}
138+
139+
#undef TERMS_CNT
140+
141+
int test__pmu(void)
142+
{
143+
char *format = test_format_dir_get();
144+
LIST_HEAD(formats);
145+
struct list_head *terms = test_terms_list();
146+
int ret;
147+
148+
if (!format)
149+
return -EINVAL;
150+
151+
do {
152+
struct perf_event_attr attr;
153+
154+
memset(&attr, 0, sizeof(attr));
155+
156+
ret = perf_pmu__format_parse(format, &formats);
157+
if (ret)
158+
break;
159+
160+
ret = perf_pmu__config_terms(&formats, &attr, terms);
161+
if (ret)
162+
break;
163+
164+
ret = -EINVAL;
165+
166+
if (attr.config != 0xc00000000002a823)
167+
break;
168+
if (attr.config1 != 0x8000400000000145)
169+
break;
170+
if (attr.config2 != 0x0400000020041d07)
171+
break;
172+
173+
ret = 0;
174+
} while (0);
175+
176+
test_format_dir_put(format);
177+
return ret;
178+
}

tools/perf/tests/tests.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int test__rdpmc(void);
1111
int test__perf_evsel__roundtrip_name_test(void);
1212
int test__perf_evsel__tp_sched_test(void);
1313
int test__syscall_open_tp_fields(void);
14+
int test__pmu(void);
1415

1516
/* Util */
1617
int trace_event__id(const char *evname);

0 commit comments

Comments
 (0)