2 * The struct perf_event_attr test support.
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.
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.
13 * Besides 'struct perf_event_attr' values we also store 'fd' and
14 * 'group_fd' values to allow checking for groups created.
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.
23 #include <linux/types.h>
24 #include <linux/kernel.h>
30 #define ENV "PERF_TEST_ATTR"
36 void test_attr__init(void)
39 test_attr__enabled
= (dir
!= NULL
);
44 #define __WRITE_ASS(str, fmt, data) \
49 size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
50 if (1 != fwrite(buf, size, 1, file)) { \
51 perror("test attr - failed to write event file"); \
58 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
60 static int store_event(struct perf_event_attr
*attr
, pid_t pid
, int cpu
,
61 int fd
, int group_fd
, unsigned long flags
)
66 snprintf(path
, PATH_MAX
, "%s/event-%d-%llu-%d", dir
,
67 attr
->type
, attr
->config
, fd
);
69 file
= fopen(path
, "w+");
71 perror("test attr - failed to open event file");
75 if (fprintf(file
, "[event-%d-%llu-%d]\n",
76 attr
->type
, attr
->config
, fd
) < 0) {
77 perror("test attr - failed to write event file");
82 /* syscall arguments */
83 __WRITE_ASS(fd
, "d", fd
);
84 __WRITE_ASS(group_fd
, "d", group_fd
);
85 __WRITE_ASS(cpu
, "d", cpu
);
86 __WRITE_ASS(pid
, "d", pid
);
87 __WRITE_ASS(flags
, "lu", flags
);
89 /* struct perf_event_attr */
90 WRITE_ASS(type
, PRIu32
);
91 WRITE_ASS(size
, PRIu32
);
92 WRITE_ASS(config
, "llu");
93 WRITE_ASS(sample_period
, "llu");
94 WRITE_ASS(sample_type
, "llu");
95 WRITE_ASS(read_format
, "llu");
96 WRITE_ASS(disabled
, "d");
97 WRITE_ASS(inherit
, "d");
98 WRITE_ASS(pinned
, "d");
99 WRITE_ASS(exclusive
, "d");
100 WRITE_ASS(exclude_user
, "d");
101 WRITE_ASS(exclude_kernel
, "d");
102 WRITE_ASS(exclude_hv
, "d");
103 WRITE_ASS(exclude_idle
, "d");
104 WRITE_ASS(mmap
, "d");
105 WRITE_ASS(comm
, "d");
106 WRITE_ASS(freq
, "d");
107 WRITE_ASS(inherit_stat
, "d");
108 WRITE_ASS(enable_on_exec
, "d");
109 WRITE_ASS(task
, "d");
110 WRITE_ASS(watermark
, "d");
111 WRITE_ASS(precise_ip
, "d");
112 WRITE_ASS(mmap_data
, "d");
113 WRITE_ASS(sample_id_all
, "d");
114 WRITE_ASS(exclude_host
, "d");
115 WRITE_ASS(exclude_guest
, "d");
116 WRITE_ASS(exclude_callchain_kernel
, "d");
117 WRITE_ASS(exclude_callchain_user
, "d");
118 WRITE_ASS(wakeup_events
, PRIu32
);
119 WRITE_ASS(bp_type
, PRIu32
);
120 WRITE_ASS(config1
, "llu");
121 WRITE_ASS(config2
, "llu");
122 WRITE_ASS(branch_sample_type
, "llu");
123 WRITE_ASS(sample_regs_user
, "llu");
124 WRITE_ASS(sample_stack_user
, PRIu32
);
130 void test_attr__open(struct perf_event_attr
*attr
, pid_t pid
, int cpu
,
131 int fd
, int group_fd
, unsigned long flags
)
133 int errno_saved
= errno
;
135 if (store_event(attr
, pid
, cpu
, fd
, group_fd
, flags
))
136 die("test attr FAILED");
141 static int run_dir(const char *d
, const char *perf
)
144 int vcnt
= min(verbose
, (int) sizeof(v
) - 1);
145 char cmd
[3*PATH_MAX
];
150 snprintf(cmd
, 3*PATH_MAX
, PYTHON
" %s/attr.py -d %s/attr/ -p %s %.*s",
151 d
, d
, perf
, vcnt
, v
);
159 char path_perf
[PATH_MAX
];
160 char path_dir
[PATH_MAX
];
162 /* First try developement tree tests. */
163 if (!lstat("./tests", &st
))
164 return run_dir("./tests", "./perf");
166 /* Then installed path. */
167 snprintf(path_dir
, PATH_MAX
, "%s/tests", perf_exec_path());
168 snprintf(path_perf
, PATH_MAX
, "%s/perf", BINDIR
);
170 if (!lstat(path_dir
, &st
) &&
171 !lstat(path_perf
, &st
))
172 return run_dir(path_dir
, path_perf
);
174 fprintf(stderr
, " (omitted)");