blob: ba87cd529bfcf2a8b6f7a039b5cdfdb55097e11a [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>
27#include "../perf.h"
28#include "util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060029#include <subcmd/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 +010036static 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
Jiri Olsa89f552d2012-11-05 16:49:37 +010046#define __WRITE_ASS(str, fmt, data) \
Jiri Olsa52502bf2012-10-31 15:52:47 +010047do { \
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
Jiri Olsa89f552d2012-11-05 16:49:37 +010060#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
61
Jiri Olsa52502bf2012-10-31 15:52:47 +010062static 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 */
Jiri Olsa89f552d2012-11-05 16:49:37 +010085 __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);
Jiri Olsa52502bf2012-10-31 15:52:47 +010090
91 /* struct perf_event_attr */
Jiri Olsa89f552d2012-11-05 16:49:37 +010092 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");
Jiri Olsa45e40892012-11-05 16:49:38 +0100112 WRITE_ASS(watermark, "d");
Jiri Olsa89f552d2012-11-05 16:49:37 +0100113 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
Jiri Olsa52502bf2012-10-31 15:52:47 +0100128 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}
Jiri Olsad898b242012-10-30 23:02:05 +0100142
143static int run_dir(const char *d, const char *perf)
144{
Jiri Olsa097c8752013-02-25 10:52:49 +0100145 char v[] = "-vvvvv";
146 int vcnt = min(verbose, (int) sizeof(v) - 1);
Jiri Olsad898b242012-10-30 23:02:05 +0100147 char cmd[3*PATH_MAX];
148
Namhyung Kimbb963e12017-02-17 17:17:38 +0900149 if (verbose > 0)
Jiri Olsa097c8752013-02-25 10:52:49 +0100150 vcnt++;
151
152 snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
153 d, d, perf, vcnt, v);
Jiri Olsad898b242012-10-30 23:02:05 +0100154
155 return system(cmd);
156}
157
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300158int test__attr(int subtest __maybe_unused)
Jiri Olsad898b242012-10-30 23:02:05 +0100159{
160 struct stat st;
161 char path_perf[PATH_MAX];
162 char path_dir[PATH_MAX];
163
164 /* First try developement tree tests. */
165 if (!lstat("./tests", &st))
166 return run_dir("./tests", "./perf");
167
168 /* Then installed path. */
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600169 snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
Jiri Olsad898b242012-10-30 23:02:05 +0100170 snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
171
172 if (!lstat(path_dir, &st) &&
173 !lstat(path_perf, &st))
174 return run_dir(path_dir, path_perf);
175
Wang Nan597bdeb2015-11-03 10:44:42 +0000176 return TEST_SKIP;
Jiri Olsad898b242012-10-30 23:02:05 +0100177}