blob: 25638a986257a58ef664f5d68b39833987988406 [file] [log] [blame]
Jiri Olsa52502bf2012-10-31 15:52:47 +01001
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 <stdlib.h>
23#include <stdio.h>
24#include <inttypes.h>
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include "../perf.h"
28#include "util.h"
Jiri Olsad898b242012-10-30 23:02:05 +010029#include "exec_cmd.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010030#include "tests.h"
Jiri Olsa52502bf2012-10-31 15:52:47 +010031
32#define ENV "PERF_TEST_ATTR"
33
Jiri Olsad898b242012-10-30 23:02:05 +010034extern int verbose;
35
Jiri Olsa52502bf2012-10-31 15:52:47 +010036bool test_attr__enabled;
37
38static char *dir;
39
40void test_attr__init(void)
41{
42 dir = getenv(ENV);
43 test_attr__enabled = (dir != NULL);
44}
45
46#define BUFSIZE 1024
47
Jiri Olsa89f552d2012-11-05 16:49:37 +010048#define __WRITE_ASS(str, fmt, data) \
Jiri Olsa52502bf2012-10-31 15:52:47 +010049do { \
50 char buf[BUFSIZE]; \
51 size_t size; \
52 \
53 size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
54 if (1 != fwrite(buf, size, 1, file)) { \
55 perror("test attr - failed to write event file"); \
56 fclose(file); \
57 return -1; \
58 } \
59 \
60} while (0)
61
Jiri Olsa89f552d2012-11-05 16:49:37 +010062#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
63
Jiri Olsa52502bf2012-10-31 15:52:47 +010064static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
65 int fd, int group_fd, unsigned long flags)
66{
67 FILE *file;
68 char path[PATH_MAX];
69
70 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
71 attr->type, attr->config, fd);
72
73 file = fopen(path, "w+");
74 if (!file) {
75 perror("test attr - failed to open event file");
76 return -1;
77 }
78
79 if (fprintf(file, "[event-%d-%llu-%d]\n",
80 attr->type, attr->config, fd) < 0) {
81 perror("test attr - failed to write event file");
82 fclose(file);
83 return -1;
84 }
85
86 /* syscall arguments */
Jiri Olsa89f552d2012-11-05 16:49:37 +010087 __WRITE_ASS(fd, "d", fd);
88 __WRITE_ASS(group_fd, "d", group_fd);
89 __WRITE_ASS(cpu, "d", cpu);
90 __WRITE_ASS(pid, "d", pid);
91 __WRITE_ASS(flags, "lu", flags);
Jiri Olsa52502bf2012-10-31 15:52:47 +010092
93 /* struct perf_event_attr */
Jiri Olsa89f552d2012-11-05 16:49:37 +010094 WRITE_ASS(type, PRIu32);
95 WRITE_ASS(size, PRIu32);
96 WRITE_ASS(config, "llu");
97 WRITE_ASS(sample_period, "llu");
98 WRITE_ASS(sample_type, "llu");
99 WRITE_ASS(read_format, "llu");
100 WRITE_ASS(disabled, "d");
101 WRITE_ASS(inherit, "d");
102 WRITE_ASS(pinned, "d");
103 WRITE_ASS(exclusive, "d");
104 WRITE_ASS(exclude_user, "d");
105 WRITE_ASS(exclude_kernel, "d");
106 WRITE_ASS(exclude_hv, "d");
107 WRITE_ASS(exclude_idle, "d");
108 WRITE_ASS(mmap, "d");
109 WRITE_ASS(comm, "d");
110 WRITE_ASS(freq, "d");
111 WRITE_ASS(inherit_stat, "d");
112 WRITE_ASS(enable_on_exec, "d");
113 WRITE_ASS(task, "d");
Jiri Olsa45e40892012-11-05 16:49:38 +0100114 WRITE_ASS(watermark, "d");
Jiri Olsa89f552d2012-11-05 16:49:37 +0100115 WRITE_ASS(precise_ip, "d");
116 WRITE_ASS(mmap_data, "d");
117 WRITE_ASS(sample_id_all, "d");
118 WRITE_ASS(exclude_host, "d");
119 WRITE_ASS(exclude_guest, "d");
120 WRITE_ASS(exclude_callchain_kernel, "d");
121 WRITE_ASS(exclude_callchain_user, "d");
122 WRITE_ASS(wakeup_events, PRIu32);
123 WRITE_ASS(bp_type, PRIu32);
124 WRITE_ASS(config1, "llu");
125 WRITE_ASS(config2, "llu");
126 WRITE_ASS(branch_sample_type, "llu");
127 WRITE_ASS(sample_regs_user, "llu");
128 WRITE_ASS(sample_stack_user, PRIu32);
129
Jiri Olsa52502bf2012-10-31 15:52:47 +0100130 fclose(file);
131 return 0;
132}
133
134void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
135 int fd, int group_fd, unsigned long flags)
136{
137 int errno_saved = errno;
138
139 if (store_event(attr, pid, cpu, fd, group_fd, flags))
140 die("test attr FAILED");
141
142 errno = errno_saved;
143}
Jiri Olsad898b242012-10-30 23:02:05 +0100144
145static int run_dir(const char *d, const char *perf)
146{
147 char cmd[3*PATH_MAX];
148
149 snprintf(cmd, 3*PATH_MAX, "python %s/attr.py -d %s/attr/ -p %s %s",
150 d, d, perf, verbose ? "-v" : "");
151
152 return system(cmd);
153}
154
Jiri Olsac81251e2012-11-10 01:46:51 +0100155int test__attr(void)
Jiri Olsad898b242012-10-30 23:02:05 +0100156{
157 struct stat st;
158 char path_perf[PATH_MAX];
159 char path_dir[PATH_MAX];
160
161 /* First try developement tree tests. */
162 if (!lstat("./tests", &st))
163 return run_dir("./tests", "./perf");
164
165 /* Then installed path. */
166 snprintf(path_dir, PATH_MAX, "%s/tests", perf_exec_path());
167 snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
168
169 if (!lstat(path_dir, &st) &&
170 !lstat(path_perf, &st))
171 return run_dir(path_dir, path_perf);
172
173 fprintf(stderr, " (ommitted)");
174 return 0;
175}