1 // SPDX-License-Identifier: GPL-2.0
3 * The struct perf_event_attr test support.
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.
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.
14 * Besides 'struct perf_event_attr' values we also store 'fd' and
15 * 'group_fd' values to allow checking for groups created.
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.
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
33 #include <subcmd/exec-cmd.h>
39 #define ENV "PERF_TEST_ATTR"
44 void test_attr__init(void)
47 test_attr__enabled = (dir != NULL);
52 #define __WRITE_ASS(str, fmt, data) \
57 size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
58 if (1 != fwrite(buf, size, 1, file)) { \
59 perror("test attr - failed to write event file"); \
66 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
68 static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
69 int fd, int group_fd, unsigned long flags)
77 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
78 attr->type, attr->config, fd);
80 file = fopen(path, "w+");
82 perror("test attr - failed to open event file");
86 if (fprintf(file, "[event-%d-%llu-%d]\n",
87 attr->type, attr->config, fd) < 0) {
88 perror("test attr - failed to write event file");
93 /* syscall arguments */
94 __WRITE_ASS(fd, "d", fd);
95 __WRITE_ASS(group_fd, "d", group_fd);
96 __WRITE_ASS(cpu, "d", cpu.cpu);
97 __WRITE_ASS(pid, "d", pid);
98 __WRITE_ASS(flags, "lu", flags);
100 /* struct perf_event_attr */
101 WRITE_ASS(type, PRIu32);
102 WRITE_ASS(size, PRIu32);
103 WRITE_ASS(config, "llu");
104 WRITE_ASS(sample_period, "llu");
105 WRITE_ASS(sample_type, "llu");
106 WRITE_ASS(read_format, "llu");
107 WRITE_ASS(disabled, "d");
108 WRITE_ASS(inherit, "d");
109 WRITE_ASS(pinned, "d");
110 WRITE_ASS(exclusive, "d");
111 WRITE_ASS(exclude_user, "d");
112 WRITE_ASS(exclude_kernel, "d");
113 WRITE_ASS(exclude_hv, "d");
114 WRITE_ASS(exclude_idle, "d");
115 WRITE_ASS(mmap, "d");
116 WRITE_ASS(comm, "d");
117 WRITE_ASS(freq, "d");
118 WRITE_ASS(inherit_stat, "d");
119 WRITE_ASS(enable_on_exec, "d");
120 WRITE_ASS(task, "d");
121 WRITE_ASS(watermark, "d");
122 WRITE_ASS(precise_ip, "d");
123 WRITE_ASS(mmap_data, "d");
124 WRITE_ASS(sample_id_all, "d");
125 WRITE_ASS(exclude_host, "d");
126 WRITE_ASS(exclude_guest, "d");
127 WRITE_ASS(exclude_callchain_kernel, "d");
128 WRITE_ASS(exclude_callchain_user, "d");
129 WRITE_ASS(mmap2, "d");
130 WRITE_ASS(comm_exec, "d");
131 WRITE_ASS(context_switch, "d");
132 WRITE_ASS(write_backward, "d");
133 WRITE_ASS(namespaces, "d");
134 WRITE_ASS(use_clockid, "d");
135 WRITE_ASS(wakeup_events, PRIu32);
136 WRITE_ASS(bp_type, PRIu32);
137 WRITE_ASS(config1, "llu");
138 WRITE_ASS(config2, "llu");
139 WRITE_ASS(branch_sample_type, "llu");
140 WRITE_ASS(sample_regs_user, "llu");
141 WRITE_ASS(sample_stack_user, PRIu32);
147 void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
148 int fd, int group_fd, unsigned long flags)
150 int errno_saved = errno;
152 if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
153 pr_err("test attr FAILED");
160 void test_attr__ready(void)
162 if (unlikely(test_attr__enabled) && !ready)
166 static int run_dir(const char *d, const char *perf)
169 int vcnt = min(verbose, (int) sizeof(v) - 1);
170 char cmd[3*PATH_MAX];
175 scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
176 d, d, perf, vcnt, v);
178 return system(cmd) ? TEST_FAIL : TEST_OK;
181 static int test__attr(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
184 char path_perf[PATH_MAX];
185 char path_dir[PATH_MAX];
188 if (perf_pmus__num_core_pmus() > 1) {
190 * TODO: Attribute tests hard code the PMU type. If there are >1
191 * core PMU then each PMU will have a different type whic
192 * requires additional support.
194 pr_debug("Skip test on hybrid systems");
198 /* First try development tree tests. */
199 if (!lstat("./tests", &st))
200 return run_dir("./tests", "./perf");
202 exec_path = get_argv_exec_path();
203 if (exec_path == NULL)
206 /* Then installed path. */
207 snprintf(path_dir, PATH_MAX, "%s/tests", exec_path);
208 snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
211 if (!lstat(path_dir, &st) &&
212 !lstat(path_perf, &st))
213 return run_dir(path_dir, path_perf);
218 DEFINE_SUITE("Setup struct perf_event_attr", attr);