^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * The struct perf_event_attr test support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This test is embedded inside into perf directly and is governed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * by the PERF_TEST_ATTR environment variable and hook inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * sys_perf_event_open function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * The general idea is to store 'struct perf_event_attr' details for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * each event created within single perf command. Each event details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * are stored into separate text file. Once perf command is finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * these files can be checked for values we expect for command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Besides 'struct perf_event_attr' values we also store 'fd' and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 'group_fd' values to allow checking for groups created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * This all is triggered by setting PERF_TEST_ATTR environment variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * It must contain name of existing directory with access and write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * permissions. All the event text files are stored there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <sys/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <subcmd/exec-cmd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define ENV "PERF_TEST_ATTR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static char *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static bool ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void test_attr__init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) dir = getenv(ENV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) test_attr__enabled = (dir != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define BUFSIZE 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define __WRITE_ASS(str, fmt, data) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char buf[BUFSIZE]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) size_t size; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (1 != fwrite(buf, size, 1, file)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) perror("test attr - failed to write event file"); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) fclose(file); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int fd, int group_fd, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) attr->type, attr->config, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) file = fopen(path, "w+");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) perror("test attr - failed to open event file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (fprintf(file, "[event-%d-%llu-%d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) attr->type, attr->config, fd) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) perror("test attr - failed to write event file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* syscall arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __WRITE_ASS(fd, "d", fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __WRITE_ASS(group_fd, "d", group_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) __WRITE_ASS(cpu, "d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __WRITE_ASS(pid, "d", pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) __WRITE_ASS(flags, "lu", flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* struct perf_event_attr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) WRITE_ASS(type, PRIu32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) WRITE_ASS(size, PRIu32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) WRITE_ASS(config, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) WRITE_ASS(sample_period, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) WRITE_ASS(sample_type, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) WRITE_ASS(read_format, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) WRITE_ASS(disabled, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) WRITE_ASS(inherit, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) WRITE_ASS(pinned, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) WRITE_ASS(exclusive, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) WRITE_ASS(exclude_user, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) WRITE_ASS(exclude_kernel, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) WRITE_ASS(exclude_hv, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) WRITE_ASS(exclude_idle, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) WRITE_ASS(mmap, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) WRITE_ASS(comm, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) WRITE_ASS(freq, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) WRITE_ASS(inherit_stat, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) WRITE_ASS(enable_on_exec, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) WRITE_ASS(task, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) WRITE_ASS(watermark, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) WRITE_ASS(precise_ip, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) WRITE_ASS(mmap_data, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) WRITE_ASS(sample_id_all, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) WRITE_ASS(exclude_host, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) WRITE_ASS(exclude_guest, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) WRITE_ASS(exclude_callchain_kernel, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) WRITE_ASS(exclude_callchain_user, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) WRITE_ASS(mmap2, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) WRITE_ASS(comm_exec, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) WRITE_ASS(context_switch, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) WRITE_ASS(write_backward, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) WRITE_ASS(namespaces, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) WRITE_ASS(use_clockid, "d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) WRITE_ASS(wakeup_events, PRIu32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) WRITE_ASS(bp_type, PRIu32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) WRITE_ASS(config1, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) WRITE_ASS(config2, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) WRITE_ASS(branch_sample_type, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) WRITE_ASS(sample_regs_user, "llu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) WRITE_ASS(sample_stack_user, PRIu32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int fd, int group_fd, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int errno_saved = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pr_err("test attr FAILED");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) exit(128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) errno = errno_saved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void test_attr__ready(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (unlikely(test_attr__enabled) && !ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ready = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int run_dir(const char *d, const char *perf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) char v[] = "-vvvvv";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int vcnt = min(verbose, (int) sizeof(v) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) char cmd[3*PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (verbose > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) vcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) d, d, perf, vcnt, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return system(cmd) ? TEST_FAIL : TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) char path_perf[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) char path_dir[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* First try development tree tests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!lstat("./tests", &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return run_dir("./tests", "./perf");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Then installed path. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!lstat(path_dir, &st) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) !lstat(path_perf, &st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return run_dir(path_dir, path_perf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return TEST_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }