blob: aacad82634c67acc51c37514e79d4ea2a5b29058 [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 Olsa52502bf2012-10-31 15:52:47 +010030
31#define ENV "PERF_TEST_ATTR"
32
Jiri Olsad898b242012-10-30 23:02:05 +010033extern int verbose;
34
Jiri Olsa52502bf2012-10-31 15:52:47 +010035bool test_attr__enabled;
36
37static char *dir;
38
39void test_attr__init(void)
40{
41 dir = getenv(ENV);
42 test_attr__enabled = (dir != NULL);
43}
44
45#define BUFSIZE 1024
46
47#define WRITE_ASS(str, fmt, data) \
48do { \
49 char buf[BUFSIZE]; \
50 size_t size; \
51 \
52 size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
53 if (1 != fwrite(buf, size, 1, file)) { \
54 perror("test attr - failed to write event file"); \
55 fclose(file); \
56 return -1; \
57 } \
58 \
59} while (0)
60
61static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
62 int fd, int group_fd, unsigned long flags)
63{
64 FILE *file;
65 char path[PATH_MAX];
66
67 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
68 attr->type, attr->config, fd);
69
70 file = fopen(path, "w+");
71 if (!file) {
72 perror("test attr - failed to open event file");
73 return -1;
74 }
75
76 if (fprintf(file, "[event-%d-%llu-%d]\n",
77 attr->type, attr->config, fd) < 0) {
78 perror("test attr - failed to write event file");
79 fclose(file);
80 return -1;
81 }
82
83 /* syscall arguments */
84 WRITE_ASS(fd, "d", fd);
85 WRITE_ASS(group_fd, "d", group_fd);
86 WRITE_ASS(cpu, "d", cpu);
87 WRITE_ASS(pid, "d", pid);
88 WRITE_ASS(flags, "lu", flags);
89
90 /* struct perf_event_attr */
91 WRITE_ASS(type, PRIu32, attr->type);
92 WRITE_ASS(size, PRIu32, attr->size);
93 WRITE_ASS(config, "llu", attr->config);
94 WRITE_ASS(sample_period, "llu", attr->sample_period);
95 WRITE_ASS(sample_type, "llu", attr->sample_type);
96 WRITE_ASS(read_format, "llu", attr->read_format);
97 WRITE_ASS(disabled, "d", attr->disabled);
98 WRITE_ASS(inherit, "d", attr->inherit);
99 WRITE_ASS(pinned, "d", attr->pinned);
100 WRITE_ASS(exclusive, "d", attr->exclusive);
101 WRITE_ASS(exclude_user, "d", attr->exclude_user);
102 WRITE_ASS(exclude_kernel, "d", attr->exclude_kernel);
103 WRITE_ASS(exclude_hv, "d", attr->exclude_hv);
104 WRITE_ASS(exclude_idle, "d", attr->exclude_idle);
105 WRITE_ASS(mmap, "d", attr->mmap);
106 WRITE_ASS(comm, "d", attr->comm);
107 WRITE_ASS(freq, "d", attr->freq);
108 WRITE_ASS(inherit_stat, "d", attr->inherit_stat);
109 WRITE_ASS(enable_on_exec, "d", attr->enable_on_exec);
110 WRITE_ASS(task, "d", attr->task);
111 WRITE_ASS(watermask, "d", attr->watermark);
112 WRITE_ASS(precise_ip, "d", attr->precise_ip);
113 WRITE_ASS(mmap_data, "d", attr->mmap_data);
114 WRITE_ASS(sample_id_all, "d", attr->sample_id_all);
115 WRITE_ASS(exclude_host, "d", attr->exclude_host);
116 WRITE_ASS(exclude_guest, "d", attr->exclude_guest);
117 WRITE_ASS(exclude_callchain_kernel, "d",
118 attr->exclude_callchain_kernel);
119 WRITE_ASS(exclude_callchain_user, "d",
120 attr->exclude_callchain_user);
121 WRITE_ASS(wakeup_events, PRIu32, attr->wakeup_events);
122 WRITE_ASS(bp_type, PRIu32, attr->bp_type);
123 WRITE_ASS(config1, "llu", attr->config1);
124 WRITE_ASS(config2, "llu", attr->config2);
125 WRITE_ASS(branch_sample_type, "llu", attr->branch_sample_type);
126 WRITE_ASS(sample_regs_user, "llu", attr->sample_regs_user);
127 WRITE_ASS(sample_stack_user, PRIu32, attr->sample_stack_user);
128 WRITE_ASS(optional, "d", 0);
129
130 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
155int test_attr__run(void)
156{
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}