perf tests: Check python path on attr and binding test
[linux-2.6-block.git] / tools / perf / tests / attr.c
CommitLineData
52502bf2
JO
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#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"
d898b241 29#include "exec_cmd.h"
c81251e8 30#include "tests.h"
52502bf2
JO
31
32#define ENV "PERF_TEST_ATTR"
33
d898b241
JO
34extern int verbose;
35
52502bf2
JO
36static char *dir;
37
38void test_attr__init(void)
39{
40 dir = getenv(ENV);
41 test_attr__enabled = (dir != NULL);
42}
43
44#define BUFSIZE 1024
45
89f552d6 46#define __WRITE_ASS(str, fmt, data) \
52502bf2
JO
47do { \
48 char buf[BUFSIZE]; \
49 size_t size; \
50 \
51 size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
52 if (1 != fwrite(buf, size, 1, file)) { \
53 perror("test attr - failed to write event file"); \
54 fclose(file); \
55 return -1; \
56 } \
57 \
58} while (0)
59
89f552d6
JO
60#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
61
52502bf2
JO
62static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
63 int fd, int group_fd, unsigned long flags)
64{
65 FILE *file;
66 char path[PATH_MAX];
67
68 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
69 attr->type, attr->config, fd);
70
71 file = fopen(path, "w+");
72 if (!file) {
73 perror("test attr - failed to open event file");
74 return -1;
75 }
76
77 if (fprintf(file, "[event-%d-%llu-%d]\n",
78 attr->type, attr->config, fd) < 0) {
79 perror("test attr - failed to write event file");
80 fclose(file);
81 return -1;
82 }
83
84 /* syscall arguments */
89f552d6
JO
85 __WRITE_ASS(fd, "d", fd);
86 __WRITE_ASS(group_fd, "d", group_fd);
87 __WRITE_ASS(cpu, "d", cpu);
88 __WRITE_ASS(pid, "d", pid);
89 __WRITE_ASS(flags, "lu", flags);
52502bf2
JO
90
91 /* struct perf_event_attr */
89f552d6
JO
92 WRITE_ASS(type, PRIu32);
93 WRITE_ASS(size, PRIu32);
94 WRITE_ASS(config, "llu");
95 WRITE_ASS(sample_period, "llu");
96 WRITE_ASS(sample_type, "llu");
97 WRITE_ASS(read_format, "llu");
98 WRITE_ASS(disabled, "d");
99 WRITE_ASS(inherit, "d");
100 WRITE_ASS(pinned, "d");
101 WRITE_ASS(exclusive, "d");
102 WRITE_ASS(exclude_user, "d");
103 WRITE_ASS(exclude_kernel, "d");
104 WRITE_ASS(exclude_hv, "d");
105 WRITE_ASS(exclude_idle, "d");
106 WRITE_ASS(mmap, "d");
107 WRITE_ASS(comm, "d");
108 WRITE_ASS(freq, "d");
109 WRITE_ASS(inherit_stat, "d");
110 WRITE_ASS(enable_on_exec, "d");
111 WRITE_ASS(task, "d");
45e4089b 112 WRITE_ASS(watermark, "d");
89f552d6
JO
113 WRITE_ASS(precise_ip, "d");
114 WRITE_ASS(mmap_data, "d");
115 WRITE_ASS(sample_id_all, "d");
116 WRITE_ASS(exclude_host, "d");
117 WRITE_ASS(exclude_guest, "d");
118 WRITE_ASS(exclude_callchain_kernel, "d");
119 WRITE_ASS(exclude_callchain_user, "d");
120 WRITE_ASS(wakeup_events, PRIu32);
121 WRITE_ASS(bp_type, PRIu32);
122 WRITE_ASS(config1, "llu");
123 WRITE_ASS(config2, "llu");
124 WRITE_ASS(branch_sample_type, "llu");
125 WRITE_ASS(sample_regs_user, "llu");
126 WRITE_ASS(sample_stack_user, PRIu32);
127
52502bf2
JO
128 fclose(file);
129 return 0;
130}
131
132void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
133 int fd, int group_fd, unsigned long flags)
134{
135 int errno_saved = errno;
136
137 if (store_event(attr, pid, cpu, fd, group_fd, flags))
138 die("test attr FAILED");
139
140 errno = errno_saved;
141}
d898b241
JO
142
143static int run_dir(const char *d, const char *perf)
144{
145 char cmd[3*PATH_MAX];
146
000ae33f 147 snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %s",
d898b241
JO
148 d, d, perf, verbose ? "-v" : "");
149
150 return system(cmd);
151}
152
c81251e8 153int test__attr(void)
d898b241
JO
154{
155 struct stat st;
156 char path_perf[PATH_MAX];
157 char path_dir[PATH_MAX];
158
159 /* First try developement tree tests. */
160 if (!lstat("./tests", &st))
161 return run_dir("./tests", "./perf");
162
163 /* Then installed path. */
164 snprintf(path_dir, PATH_MAX, "%s/tests", perf_exec_path());
165 snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
166
167 if (!lstat(path_dir, &st) &&
168 !lstat(path_perf, &st))
169 return run_dir(path_dir, path_perf);
170
171 fprintf(stderr, " (ommitted)");
172 return 0;
173}