blob: c19e0da543374b2c4afeb76d56446cfac53547a0 [file] [log] [blame]
Jiri Olsa52502bf2012-10-31 15:52:47 +01001/*
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
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030021#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030022#include <inttypes.h>
Jiri Olsa52502bf2012-10-31 15:52:47 +010023#include <stdlib.h>
24#include <stdio.h>
Jiri Olsa52502bf2012-10-31 15:52:47 +010025#include <linux/types.h>
26#include <linux/kernel.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030027#include <sys/param.h>
Jiri Olsa52502bf2012-10-31 15:52:47 +010028#include "../perf.h"
29#include "util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060030#include <subcmd/exec-cmd.h>
Jiri Olsac81251e2012-11-10 01:46:51 +010031#include "tests.h"
Jiri Olsa52502bf2012-10-31 15:52:47 +010032
33#define ENV "PERF_TEST_ATTR"
34
Jiri Olsad898b242012-10-30 23:02:05 +010035extern int verbose;
36
Jiri Olsa52502bf2012-10-31 15:52:47 +010037static 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
Jiri Olsa89f552d2012-11-05 16:49:37 +010047#define __WRITE_ASS(str, fmt, data) \
Jiri Olsa52502bf2012-10-31 15:52:47 +010048do { \
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
Jiri Olsa89f552d2012-11-05 16:49:37 +010061#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
62
Jiri Olsa52502bf2012-10-31 15:52:47 +010063static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
64 int fd, int group_fd, unsigned long flags)
65{
66 FILE *file;
67 char path[PATH_MAX];
68
69 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
70 attr->type, attr->config, fd);
71
72 file = fopen(path, "w+");
73 if (!file) {
74 perror("test attr - failed to open event file");
75 return -1;
76 }
77
78 if (fprintf(file, "[event-%d-%llu-%d]\n",
79 attr->type, attr->config, fd) < 0) {
80 perror("test attr - failed to write event file");
81 fclose(file);
82 return -1;
83 }
84
85 /* syscall arguments */
Jiri Olsa89f552d2012-11-05 16:49:37 +010086 __WRITE_ASS(fd, "d", fd);
87 __WRITE_ASS(group_fd, "d", group_fd);
88 __WRITE_ASS(cpu, "d", cpu);
89 __WRITE_ASS(pid, "d", pid);
90 __WRITE_ASS(flags, "lu", flags);
Jiri Olsa52502bf2012-10-31 15:52:47 +010091
92 /* struct perf_event_attr */
Jiri Olsa89f552d2012-11-05 16:49:37 +010093 WRITE_ASS(type, PRIu32);
94 WRITE_ASS(size, PRIu32);
95 WRITE_ASS(config, "llu");
96 WRITE_ASS(sample_period, "llu");
97 WRITE_ASS(sample_type, "llu");
98 WRITE_ASS(read_format, "llu");
99 WRITE_ASS(disabled, "d");
100 WRITE_ASS(inherit, "d");
101 WRITE_ASS(pinned, "d");
102 WRITE_ASS(exclusive, "d");
103 WRITE_ASS(exclude_user, "d");
104 WRITE_ASS(exclude_kernel, "d");
105 WRITE_ASS(exclude_hv, "d");
106 WRITE_ASS(exclude_idle, "d");
107 WRITE_ASS(mmap, "d");
108 WRITE_ASS(comm, "d");
109 WRITE_ASS(freq, "d");
110 WRITE_ASS(inherit_stat, "d");
111 WRITE_ASS(enable_on_exec, "d");
112 WRITE_ASS(task, "d");
Jiri Olsa45e40892012-11-05 16:49:38 +0100113 WRITE_ASS(watermark, "d");
Jiri Olsa89f552d2012-11-05 16:49:37 +0100114 WRITE_ASS(precise_ip, "d");
115 WRITE_ASS(mmap_data, "d");
116 WRITE_ASS(sample_id_all, "d");
117 WRITE_ASS(exclude_host, "d");
118 WRITE_ASS(exclude_guest, "d");
119 WRITE_ASS(exclude_callchain_kernel, "d");
120 WRITE_ASS(exclude_callchain_user, "d");
121 WRITE_ASS(wakeup_events, PRIu32);
122 WRITE_ASS(bp_type, PRIu32);
123 WRITE_ASS(config1, "llu");
124 WRITE_ASS(config2, "llu");
125 WRITE_ASS(branch_sample_type, "llu");
126 WRITE_ASS(sample_regs_user, "llu");
127 WRITE_ASS(sample_stack_user, PRIu32);
128
Jiri Olsa52502bf2012-10-31 15:52:47 +0100129 fclose(file);
130 return 0;
131}
132
133void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
134 int fd, int group_fd, unsigned long flags)
135{
136 int errno_saved = errno;
137
138 if (store_event(attr, pid, cpu, fd, group_fd, flags))
139 die("test attr FAILED");
140
141 errno = errno_saved;
142}
Jiri Olsad898b242012-10-30 23:02:05 +0100143
144static int run_dir(const char *d, const char *perf)
145{
Jiri Olsa097c8752013-02-25 10:52:49 +0100146 char v[] = "-vvvvv";
147 int vcnt = min(verbose, (int) sizeof(v) - 1);
Jiri Olsad898b242012-10-30 23:02:05 +0100148 char cmd[3*PATH_MAX];
149
Namhyung Kimbb963e12017-02-17 17:17:38 +0900150 if (verbose > 0)
Jiri Olsa097c8752013-02-25 10:52:49 +0100151 vcnt++;
152
153 snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
154 d, d, perf, vcnt, v);
Jiri Olsad898b242012-10-30 23:02:05 +0100155
156 return system(cmd);
157}
158
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300159int test__attr(int subtest __maybe_unused)
Jiri Olsad898b242012-10-30 23:02:05 +0100160{
161 struct stat st;
162 char path_perf[PATH_MAX];
163 char path_dir[PATH_MAX];
164
165 /* First try developement tree tests. */
166 if (!lstat("./tests", &st))
167 return run_dir("./tests", "./perf");
168
169 /* Then installed path. */
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600170 snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
Jiri Olsad898b242012-10-30 23:02:05 +0100171 snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
172
173 if (!lstat(path_dir, &st) &&
174 !lstat(path_perf, &st))
175 return run_dir(path_dir, path_perf);
176
Wang Nan597bdeb2015-11-03 10:44:42 +0000177 return TEST_SKIP;
Jiri Olsad898b242012-10-30 23:02:05 +0100178}