Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | fef2a73 | 2017-06-27 11:49:13 -0300 | [diff] [blame] | 22 | #include <debug.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 23 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 24 | #include <inttypes.h> |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 27 | #include <linux/types.h> |
| 28 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 391e420 | 2017-04-19 18:51:14 -0300 | [diff] [blame] | 29 | #include <sys/param.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 33 | #include "../perf.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 34 | #include <subcmd/exec-cmd.h> |
Jiri Olsa | c81251e | 2012-11-10 01:46:51 +0100 | [diff] [blame] | 35 | #include "tests.h" |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 36 | |
| 37 | #define ENV "PERF_TEST_ATTR" |
| 38 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 39 | static char *dir; |
Jiri Olsa | 10213e2 | 2017-07-03 16:50:18 +0200 | [diff] [blame] | 40 | static bool ready; |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 41 | |
| 42 | void test_attr__init(void) |
| 43 | { |
| 44 | dir = getenv(ENV); |
| 45 | test_attr__enabled = (dir != NULL); |
| 46 | } |
| 47 | |
| 48 | #define BUFSIZE 1024 |
| 49 | |
Jiri Olsa | 89f552d | 2012-11-05 16:49:37 +0100 | [diff] [blame] | 50 | #define __WRITE_ASS(str, fmt, data) \ |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 51 | do { \ |
| 52 | char buf[BUFSIZE]; \ |
| 53 | size_t size; \ |
| 54 | \ |
| 55 | size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \ |
| 56 | if (1 != fwrite(buf, size, 1, file)) { \ |
| 57 | perror("test attr - failed to write event file"); \ |
| 58 | fclose(file); \ |
| 59 | return -1; \ |
| 60 | } \ |
| 61 | \ |
| 62 | } while (0) |
| 63 | |
Jiri Olsa | 89f552d | 2012-11-05 16:49:37 +0100 | [diff] [blame] | 64 | #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field) |
| 65 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 66 | static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 67 | int fd, int group_fd, unsigned long flags) |
| 68 | { |
| 69 | FILE *file; |
| 70 | char path[PATH_MAX]; |
| 71 | |
Jiri Olsa | 10213e2 | 2017-07-03 16:50:18 +0200 | [diff] [blame] | 72 | if (!ready) |
| 73 | return 0; |
| 74 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 75 | snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir, |
| 76 | attr->type, attr->config, fd); |
| 77 | |
| 78 | file = fopen(path, "w+"); |
| 79 | if (!file) { |
| 80 | perror("test attr - failed to open event file"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | if (fprintf(file, "[event-%d-%llu-%d]\n", |
| 85 | attr->type, attr->config, fd) < 0) { |
| 86 | perror("test attr - failed to write event file"); |
| 87 | fclose(file); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | /* syscall arguments */ |
Jiri Olsa | 89f552d | 2012-11-05 16:49:37 +0100 | [diff] [blame] | 92 | __WRITE_ASS(fd, "d", fd); |
| 93 | __WRITE_ASS(group_fd, "d", group_fd); |
| 94 | __WRITE_ASS(cpu, "d", cpu); |
| 95 | __WRITE_ASS(pid, "d", pid); |
| 96 | __WRITE_ASS(flags, "lu", flags); |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 97 | |
| 98 | /* struct perf_event_attr */ |
Jiri Olsa | 89f552d | 2012-11-05 16:49:37 +0100 | [diff] [blame] | 99 | WRITE_ASS(type, PRIu32); |
| 100 | WRITE_ASS(size, PRIu32); |
| 101 | WRITE_ASS(config, "llu"); |
| 102 | WRITE_ASS(sample_period, "llu"); |
| 103 | WRITE_ASS(sample_type, "llu"); |
| 104 | WRITE_ASS(read_format, "llu"); |
| 105 | WRITE_ASS(disabled, "d"); |
| 106 | WRITE_ASS(inherit, "d"); |
| 107 | WRITE_ASS(pinned, "d"); |
| 108 | WRITE_ASS(exclusive, "d"); |
| 109 | WRITE_ASS(exclude_user, "d"); |
| 110 | WRITE_ASS(exclude_kernel, "d"); |
| 111 | WRITE_ASS(exclude_hv, "d"); |
| 112 | WRITE_ASS(exclude_idle, "d"); |
| 113 | WRITE_ASS(mmap, "d"); |
| 114 | WRITE_ASS(comm, "d"); |
| 115 | WRITE_ASS(freq, "d"); |
| 116 | WRITE_ASS(inherit_stat, "d"); |
| 117 | WRITE_ASS(enable_on_exec, "d"); |
| 118 | WRITE_ASS(task, "d"); |
Jiri Olsa | 45e4089 | 2012-11-05 16:49:38 +0100 | [diff] [blame] | 119 | WRITE_ASS(watermark, "d"); |
Jiri Olsa | 89f552d | 2012-11-05 16:49:37 +0100 | [diff] [blame] | 120 | WRITE_ASS(precise_ip, "d"); |
| 121 | WRITE_ASS(mmap_data, "d"); |
| 122 | WRITE_ASS(sample_id_all, "d"); |
| 123 | WRITE_ASS(exclude_host, "d"); |
| 124 | WRITE_ASS(exclude_guest, "d"); |
| 125 | WRITE_ASS(exclude_callchain_kernel, "d"); |
| 126 | WRITE_ASS(exclude_callchain_user, "d"); |
| 127 | WRITE_ASS(wakeup_events, PRIu32); |
| 128 | WRITE_ASS(bp_type, PRIu32); |
| 129 | WRITE_ASS(config1, "llu"); |
| 130 | WRITE_ASS(config2, "llu"); |
| 131 | WRITE_ASS(branch_sample_type, "llu"); |
| 132 | WRITE_ASS(sample_regs_user, "llu"); |
| 133 | WRITE_ASS(sample_stack_user, PRIu32); |
| 134 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 135 | fclose(file); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 140 | int fd, int group_fd, unsigned long flags) |
| 141 | { |
| 142 | int errno_saved = errno; |
| 143 | |
Jiri Olsa | d78ada4 | 2017-07-03 16:50:17 +0200 | [diff] [blame] | 144 | if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) { |
Arnaldo Carvalho de Melo | fef2a73 | 2017-06-27 11:49:13 -0300 | [diff] [blame] | 145 | pr_err("test attr FAILED"); |
| 146 | exit(128); |
| 147 | } |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 148 | |
| 149 | errno = errno_saved; |
| 150 | } |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 151 | |
Jiri Olsa | 10213e2 | 2017-07-03 16:50:18 +0200 | [diff] [blame] | 152 | void test_attr__ready(void) |
| 153 | { |
| 154 | if (unlikely(test_attr__enabled) && !ready) |
| 155 | ready = true; |
| 156 | } |
| 157 | |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 158 | static int run_dir(const char *d, const char *perf) |
| 159 | { |
Jiri Olsa | 097c875 | 2013-02-25 10:52:49 +0100 | [diff] [blame] | 160 | char v[] = "-vvvvv"; |
| 161 | int vcnt = min(verbose, (int) sizeof(v) - 1); |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 162 | char cmd[3*PATH_MAX]; |
| 163 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 164 | if (verbose > 0) |
Jiri Olsa | 097c875 | 2013-02-25 10:52:49 +0100 | [diff] [blame] | 165 | vcnt++; |
| 166 | |
| 167 | snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s", |
| 168 | d, d, perf, vcnt, v); |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 169 | |
| 170 | return system(cmd); |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 173 | int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 174 | { |
| 175 | struct stat st; |
| 176 | char path_perf[PATH_MAX]; |
| 177 | char path_dir[PATH_MAX]; |
| 178 | |
| 179 | /* First try developement tree tests. */ |
| 180 | if (!lstat("./tests", &st)) |
| 181 | return run_dir("./tests", "./perf"); |
| 182 | |
| 183 | /* Then installed path. */ |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 184 | snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path()); |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 185 | snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR); |
| 186 | |
| 187 | if (!lstat(path_dir, &st) && |
| 188 | !lstat(path_perf, &st)) |
| 189 | return run_dir(path_dir, path_perf); |
| 190 | |
Wang Nan | 597bdeb | 2015-11-03 10:44:42 +0000 | [diff] [blame] | 191 | return TEST_SKIP; |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 192 | } |