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