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