1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * The struct perf_event_attr test support.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This test is embedded inside into perf directly and is governed
6*4882a593Smuzhiyun * by the PERF_TEST_ATTR environment variable and hook inside
7*4882a593Smuzhiyun * sys_perf_event_open function.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * The general idea is to store 'struct perf_event_attr' details for
10*4882a593Smuzhiyun * each event created within single perf command. Each event details
11*4882a593Smuzhiyun * are stored into separate text file. Once perf command is finished
12*4882a593Smuzhiyun * these files can be checked for values we expect for command.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Besides 'struct perf_event_attr' values we also store 'fd' and
15*4882a593Smuzhiyun * 'group_fd' values to allow checking for groups created.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * This all is triggered by setting PERF_TEST_ATTR environment variable.
18*4882a593Smuzhiyun * It must contain name of existing directory with access and write
19*4882a593Smuzhiyun * permissions. All the event text files are stored there.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <debug.h>
23*4882a593Smuzhiyun #include <errno.h>
24*4882a593Smuzhiyun #include <inttypes.h>
25*4882a593Smuzhiyun #include <stdlib.h>
26*4882a593Smuzhiyun #include <stdio.h>
27*4882a593Smuzhiyun #include <linux/types.h>
28*4882a593Smuzhiyun #include <linux/kernel.h>
29*4882a593Smuzhiyun #include <sys/param.h>
30*4882a593Smuzhiyun #include <sys/types.h>
31*4882a593Smuzhiyun #include <sys/stat.h>
32*4882a593Smuzhiyun #include <unistd.h>
33*4882a593Smuzhiyun #include <subcmd/exec-cmd.h>
34*4882a593Smuzhiyun #include "event.h"
35*4882a593Smuzhiyun #include "util.h"
36*4882a593Smuzhiyun #include "tests.h"
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define ENV "PERF_TEST_ATTR"
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static char *dir;
41*4882a593Smuzhiyun static bool ready;
42*4882a593Smuzhiyun
test_attr__init(void)43*4882a593Smuzhiyun void test_attr__init(void)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun dir = getenv(ENV);
46*4882a593Smuzhiyun test_attr__enabled = (dir != NULL);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define BUFSIZE 1024
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #define __WRITE_ASS(str, fmt, data) \
52*4882a593Smuzhiyun do { \
53*4882a593Smuzhiyun char buf[BUFSIZE]; \
54*4882a593Smuzhiyun size_t size; \
55*4882a593Smuzhiyun \
56*4882a593Smuzhiyun size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
57*4882a593Smuzhiyun if (1 != fwrite(buf, size, 1, file)) { \
58*4882a593Smuzhiyun perror("test attr - failed to write event file"); \
59*4882a593Smuzhiyun fclose(file); \
60*4882a593Smuzhiyun return -1; \
61*4882a593Smuzhiyun } \
62*4882a593Smuzhiyun \
63*4882a593Smuzhiyun } while (0)
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
66*4882a593Smuzhiyun
store_event(struct perf_event_attr * attr,pid_t pid,int cpu,int fd,int group_fd,unsigned long flags)67*4882a593Smuzhiyun static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
68*4882a593Smuzhiyun int fd, int group_fd, unsigned long flags)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun FILE *file;
71*4882a593Smuzhiyun char path[PATH_MAX];
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (!ready)
74*4882a593Smuzhiyun return 0;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
77*4882a593Smuzhiyun attr->type, attr->config, fd);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun file = fopen(path, "w+");
80*4882a593Smuzhiyun if (!file) {
81*4882a593Smuzhiyun perror("test attr - failed to open event file");
82*4882a593Smuzhiyun return -1;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (fprintf(file, "[event-%d-%llu-%d]\n",
86*4882a593Smuzhiyun attr->type, attr->config, fd) < 0) {
87*4882a593Smuzhiyun perror("test attr - failed to write event file");
88*4882a593Smuzhiyun fclose(file);
89*4882a593Smuzhiyun return -1;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* syscall arguments */
93*4882a593Smuzhiyun __WRITE_ASS(fd, "d", fd);
94*4882a593Smuzhiyun __WRITE_ASS(group_fd, "d", group_fd);
95*4882a593Smuzhiyun __WRITE_ASS(cpu, "d", cpu);
96*4882a593Smuzhiyun __WRITE_ASS(pid, "d", pid);
97*4882a593Smuzhiyun __WRITE_ASS(flags, "lu", flags);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* struct perf_event_attr */
100*4882a593Smuzhiyun WRITE_ASS(type, PRIu32);
101*4882a593Smuzhiyun WRITE_ASS(size, PRIu32);
102*4882a593Smuzhiyun WRITE_ASS(config, "llu");
103*4882a593Smuzhiyun WRITE_ASS(sample_period, "llu");
104*4882a593Smuzhiyun WRITE_ASS(sample_type, "llu");
105*4882a593Smuzhiyun WRITE_ASS(read_format, "llu");
106*4882a593Smuzhiyun WRITE_ASS(disabled, "d");
107*4882a593Smuzhiyun WRITE_ASS(inherit, "d");
108*4882a593Smuzhiyun WRITE_ASS(pinned, "d");
109*4882a593Smuzhiyun WRITE_ASS(exclusive, "d");
110*4882a593Smuzhiyun WRITE_ASS(exclude_user, "d");
111*4882a593Smuzhiyun WRITE_ASS(exclude_kernel, "d");
112*4882a593Smuzhiyun WRITE_ASS(exclude_hv, "d");
113*4882a593Smuzhiyun WRITE_ASS(exclude_idle, "d");
114*4882a593Smuzhiyun WRITE_ASS(mmap, "d");
115*4882a593Smuzhiyun WRITE_ASS(comm, "d");
116*4882a593Smuzhiyun WRITE_ASS(freq, "d");
117*4882a593Smuzhiyun WRITE_ASS(inherit_stat, "d");
118*4882a593Smuzhiyun WRITE_ASS(enable_on_exec, "d");
119*4882a593Smuzhiyun WRITE_ASS(task, "d");
120*4882a593Smuzhiyun WRITE_ASS(watermark, "d");
121*4882a593Smuzhiyun WRITE_ASS(precise_ip, "d");
122*4882a593Smuzhiyun WRITE_ASS(mmap_data, "d");
123*4882a593Smuzhiyun WRITE_ASS(sample_id_all, "d");
124*4882a593Smuzhiyun WRITE_ASS(exclude_host, "d");
125*4882a593Smuzhiyun WRITE_ASS(exclude_guest, "d");
126*4882a593Smuzhiyun WRITE_ASS(exclude_callchain_kernel, "d");
127*4882a593Smuzhiyun WRITE_ASS(exclude_callchain_user, "d");
128*4882a593Smuzhiyun WRITE_ASS(mmap2, "d");
129*4882a593Smuzhiyun WRITE_ASS(comm_exec, "d");
130*4882a593Smuzhiyun WRITE_ASS(context_switch, "d");
131*4882a593Smuzhiyun WRITE_ASS(write_backward, "d");
132*4882a593Smuzhiyun WRITE_ASS(namespaces, "d");
133*4882a593Smuzhiyun WRITE_ASS(use_clockid, "d");
134*4882a593Smuzhiyun WRITE_ASS(wakeup_events, PRIu32);
135*4882a593Smuzhiyun WRITE_ASS(bp_type, PRIu32);
136*4882a593Smuzhiyun WRITE_ASS(config1, "llu");
137*4882a593Smuzhiyun WRITE_ASS(config2, "llu");
138*4882a593Smuzhiyun WRITE_ASS(branch_sample_type, "llu");
139*4882a593Smuzhiyun WRITE_ASS(sample_regs_user, "llu");
140*4882a593Smuzhiyun WRITE_ASS(sample_stack_user, PRIu32);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun fclose(file);
143*4882a593Smuzhiyun return 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
test_attr__open(struct perf_event_attr * attr,pid_t pid,int cpu,int fd,int group_fd,unsigned long flags)146*4882a593Smuzhiyun void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
147*4882a593Smuzhiyun int fd, int group_fd, unsigned long flags)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun int errno_saved = errno;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
152*4882a593Smuzhiyun pr_err("test attr FAILED");
153*4882a593Smuzhiyun exit(128);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun errno = errno_saved;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
test_attr__ready(void)159*4882a593Smuzhiyun void test_attr__ready(void)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun if (unlikely(test_attr__enabled) && !ready)
162*4882a593Smuzhiyun ready = true;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
run_dir(const char * d,const char * perf)165*4882a593Smuzhiyun static int run_dir(const char *d, const char *perf)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun char v[] = "-vvvvv";
168*4882a593Smuzhiyun int vcnt = min(verbose, (int) sizeof(v) - 1);
169*4882a593Smuzhiyun char cmd[3*PATH_MAX];
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun if (verbose > 0)
172*4882a593Smuzhiyun vcnt++;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
175*4882a593Smuzhiyun d, d, perf, vcnt, v);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun return system(cmd) ? TEST_FAIL : TEST_OK;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
test__attr(struct test * test __maybe_unused,int subtest __maybe_unused)180*4882a593Smuzhiyun int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun struct stat st;
183*4882a593Smuzhiyun char path_perf[PATH_MAX];
184*4882a593Smuzhiyun char path_dir[PATH_MAX];
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* First try development tree tests. */
187*4882a593Smuzhiyun if (!lstat("./tests", &st))
188*4882a593Smuzhiyun return run_dir("./tests", "./perf");
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /* Then installed path. */
191*4882a593Smuzhiyun snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
192*4882a593Smuzhiyun snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (!lstat(path_dir, &st) &&
195*4882a593Smuzhiyun !lstat(path_perf, &st))
196*4882a593Smuzhiyun return run_dir(path_dir, path_perf);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return TEST_SKIP;
199*4882a593Smuzhiyun }
200