Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 1 | |
| 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 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include "../perf.h" |
| 28 | #include "util.h" |
| 29 | |
| 30 | #define ENV "PERF_TEST_ATTR" |
| 31 | |
| 32 | bool test_attr__enabled; |
| 33 | |
| 34 | static char *dir; |
| 35 | |
| 36 | void test_attr__init(void) |
| 37 | { |
| 38 | dir = getenv(ENV); |
| 39 | test_attr__enabled = (dir != NULL); |
| 40 | } |
| 41 | |
| 42 | #define BUFSIZE 1024 |
| 43 | |
| 44 | #define WRITE_ASS(str, fmt, data) \ |
| 45 | do { \ |
| 46 | char buf[BUFSIZE]; \ |
| 47 | size_t size; \ |
| 48 | \ |
| 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"); \ |
| 52 | fclose(file); \ |
| 53 | return -1; \ |
| 54 | } \ |
| 55 | \ |
| 56 | } while (0) |
| 57 | |
| 58 | static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 59 | int fd, int group_fd, unsigned long flags) |
| 60 | { |
| 61 | FILE *file; |
| 62 | char path[PATH_MAX]; |
| 63 | |
| 64 | snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir, |
| 65 | attr->type, attr->config, fd); |
| 66 | |
| 67 | file = fopen(path, "w+"); |
| 68 | if (!file) { |
| 69 | perror("test attr - failed to open event file"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | if (fprintf(file, "[event-%d-%llu-%d]\n", |
| 74 | attr->type, attr->config, fd) < 0) { |
| 75 | perror("test attr - failed to write event file"); |
| 76 | fclose(file); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | /* syscall arguments */ |
| 81 | WRITE_ASS(fd, "d", fd); |
| 82 | WRITE_ASS(group_fd, "d", group_fd); |
| 83 | WRITE_ASS(cpu, "d", cpu); |
| 84 | WRITE_ASS(pid, "d", pid); |
| 85 | WRITE_ASS(flags, "lu", flags); |
| 86 | |
| 87 | /* struct perf_event_attr */ |
| 88 | WRITE_ASS(type, PRIu32, attr->type); |
| 89 | WRITE_ASS(size, PRIu32, attr->size); |
| 90 | WRITE_ASS(config, "llu", attr->config); |
| 91 | WRITE_ASS(sample_period, "llu", attr->sample_period); |
| 92 | WRITE_ASS(sample_type, "llu", attr->sample_type); |
| 93 | WRITE_ASS(read_format, "llu", attr->read_format); |
| 94 | WRITE_ASS(disabled, "d", attr->disabled); |
| 95 | WRITE_ASS(inherit, "d", attr->inherit); |
| 96 | WRITE_ASS(pinned, "d", attr->pinned); |
| 97 | WRITE_ASS(exclusive, "d", attr->exclusive); |
| 98 | WRITE_ASS(exclude_user, "d", attr->exclude_user); |
| 99 | WRITE_ASS(exclude_kernel, "d", attr->exclude_kernel); |
| 100 | WRITE_ASS(exclude_hv, "d", attr->exclude_hv); |
| 101 | WRITE_ASS(exclude_idle, "d", attr->exclude_idle); |
| 102 | WRITE_ASS(mmap, "d", attr->mmap); |
| 103 | WRITE_ASS(comm, "d", attr->comm); |
| 104 | WRITE_ASS(freq, "d", attr->freq); |
| 105 | WRITE_ASS(inherit_stat, "d", attr->inherit_stat); |
| 106 | WRITE_ASS(enable_on_exec, "d", attr->enable_on_exec); |
| 107 | WRITE_ASS(task, "d", attr->task); |
| 108 | WRITE_ASS(watermask, "d", attr->watermark); |
| 109 | WRITE_ASS(precise_ip, "d", attr->precise_ip); |
| 110 | WRITE_ASS(mmap_data, "d", attr->mmap_data); |
| 111 | WRITE_ASS(sample_id_all, "d", attr->sample_id_all); |
| 112 | WRITE_ASS(exclude_host, "d", attr->exclude_host); |
| 113 | WRITE_ASS(exclude_guest, "d", attr->exclude_guest); |
| 114 | WRITE_ASS(exclude_callchain_kernel, "d", |
| 115 | attr->exclude_callchain_kernel); |
| 116 | WRITE_ASS(exclude_callchain_user, "d", |
| 117 | attr->exclude_callchain_user); |
| 118 | WRITE_ASS(wakeup_events, PRIu32, attr->wakeup_events); |
| 119 | WRITE_ASS(bp_type, PRIu32, attr->bp_type); |
| 120 | WRITE_ASS(config1, "llu", attr->config1); |
| 121 | WRITE_ASS(config2, "llu", attr->config2); |
| 122 | WRITE_ASS(branch_sample_type, "llu", attr->branch_sample_type); |
| 123 | WRITE_ASS(sample_regs_user, "llu", attr->sample_regs_user); |
| 124 | WRITE_ASS(sample_stack_user, PRIu32, attr->sample_stack_user); |
| 125 | WRITE_ASS(optional, "d", 0); |
| 126 | |
| 127 | fclose(file); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 132 | int fd, int group_fd, unsigned long flags) |
| 133 | { |
| 134 | int errno_saved = errno; |
| 135 | |
| 136 | if (store_event(attr, pid, cpu, fd, group_fd, flags)) |
| 137 | die("test attr FAILED"); |
| 138 | |
| 139 | errno = errno_saved; |
| 140 | } |