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