Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The struct perf_event_attr test support.
0004  *
0005  * This test is embedded inside into perf directly and is governed
0006  * by the PERF_TEST_ATTR environment variable and hook inside
0007  * sys_perf_event_open function.
0008  *
0009  * The general idea is to store 'struct perf_event_attr' details for
0010  * each event created within single perf command. Each event details
0011  * are stored into separate text file. Once perf command is finished
0012  * these files can be checked for values we expect for command.
0013  *
0014  * Besides 'struct perf_event_attr' values we also store 'fd' and
0015  * 'group_fd' values to allow checking for groups created.
0016  *
0017  * This all is triggered by setting PERF_TEST_ATTR environment variable.
0018  * It must contain name of existing directory with access and write
0019  * permissions. All the event text files are stored there.
0020  */
0021 
0022 #include <debug.h>
0023 #include <errno.h>
0024 #include <inttypes.h>
0025 #include <stdlib.h>
0026 #include <stdio.h>
0027 #include <linux/types.h>
0028 #include <linux/kernel.h>
0029 #include <sys/param.h>
0030 #include <sys/types.h>
0031 #include <sys/stat.h>
0032 #include <unistd.h>
0033 #include <subcmd/exec-cmd.h>
0034 #include "event.h"
0035 #include "util.h"
0036 #include "tests.h"
0037 #include "pmu.h"
0038 
0039 #define ENV "PERF_TEST_ATTR"
0040 
0041 static char *dir;
0042 static bool ready;
0043 
0044 void test_attr__init(void)
0045 {
0046     dir = getenv(ENV);
0047     test_attr__enabled = (dir != NULL);
0048 }
0049 
0050 #define BUFSIZE 1024
0051 
0052 #define __WRITE_ASS(str, fmt, data)                 \
0053 do {                                    \
0054     char buf[BUFSIZE];                      \
0055     size_t size;                            \
0056                                     \
0057     size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data);     \
0058     if (1 != fwrite(buf, size, 1, file)) {              \
0059         perror("test attr - failed to write event file");   \
0060         fclose(file);                       \
0061         return -1;                      \
0062     }                               \
0063                                     \
0064 } while (0)
0065 
0066 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
0067 
0068 static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
0069                int fd, int group_fd, unsigned long flags)
0070 {
0071     FILE *file;
0072     char path[PATH_MAX];
0073 
0074     if (!ready)
0075         return 0;
0076 
0077     snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
0078          attr->type, attr->config, fd);
0079 
0080     file = fopen(path, "w+");
0081     if (!file) {
0082         perror("test attr - failed to open event file");
0083         return -1;
0084     }
0085 
0086     if (fprintf(file, "[event-%d-%llu-%d]\n",
0087             attr->type, attr->config, fd) < 0) {
0088         perror("test attr - failed to write event file");
0089         fclose(file);
0090         return -1;
0091     }
0092 
0093     /* syscall arguments */
0094     __WRITE_ASS(fd,       "d", fd);
0095     __WRITE_ASS(group_fd, "d", group_fd);
0096     __WRITE_ASS(cpu,      "d", cpu.cpu);
0097     __WRITE_ASS(pid,      "d", pid);
0098     __WRITE_ASS(flags,   "lu", flags);
0099 
0100     /* struct perf_event_attr */
0101     WRITE_ASS(type,   PRIu32);
0102     WRITE_ASS(size,   PRIu32);
0103     WRITE_ASS(config,  "llu");
0104     WRITE_ASS(sample_period, "llu");
0105     WRITE_ASS(sample_type,   "llu");
0106     WRITE_ASS(read_format,   "llu");
0107     WRITE_ASS(disabled,       "d");
0108     WRITE_ASS(inherit,        "d");
0109     WRITE_ASS(pinned,         "d");
0110     WRITE_ASS(exclusive,      "d");
0111     WRITE_ASS(exclude_user,   "d");
0112     WRITE_ASS(exclude_kernel, "d");
0113     WRITE_ASS(exclude_hv,     "d");
0114     WRITE_ASS(exclude_idle,   "d");
0115     WRITE_ASS(mmap,           "d");
0116     WRITE_ASS(comm,           "d");
0117     WRITE_ASS(freq,           "d");
0118     WRITE_ASS(inherit_stat,   "d");
0119     WRITE_ASS(enable_on_exec, "d");
0120     WRITE_ASS(task,           "d");
0121     WRITE_ASS(watermark,      "d");
0122     WRITE_ASS(precise_ip,     "d");
0123     WRITE_ASS(mmap_data,      "d");
0124     WRITE_ASS(sample_id_all,  "d");
0125     WRITE_ASS(exclude_host,   "d");
0126     WRITE_ASS(exclude_guest,  "d");
0127     WRITE_ASS(exclude_callchain_kernel, "d");
0128     WRITE_ASS(exclude_callchain_user, "d");
0129     WRITE_ASS(mmap2,      "d");
0130     WRITE_ASS(comm_exec,      "d");
0131     WRITE_ASS(context_switch, "d");
0132     WRITE_ASS(write_backward, "d");
0133     WRITE_ASS(namespaces,     "d");
0134     WRITE_ASS(use_clockid,    "d");
0135     WRITE_ASS(wakeup_events, PRIu32);
0136     WRITE_ASS(bp_type, PRIu32);
0137     WRITE_ASS(config1, "llu");
0138     WRITE_ASS(config2, "llu");
0139     WRITE_ASS(branch_sample_type, "llu");
0140     WRITE_ASS(sample_regs_user,   "llu");
0141     WRITE_ASS(sample_stack_user,  PRIu32);
0142 
0143     fclose(file);
0144     return 0;
0145 }
0146 
0147 void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
0148              int fd, int group_fd, unsigned long flags)
0149 {
0150     int errno_saved = errno;
0151 
0152     if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
0153         pr_err("test attr FAILED");
0154         exit(128);
0155     }
0156 
0157     errno = errno_saved;
0158 }
0159 
0160 void test_attr__ready(void)
0161 {
0162     if (unlikely(test_attr__enabled) && !ready)
0163         ready = true;
0164 }
0165 
0166 static int run_dir(const char *d, const char *perf)
0167 {
0168     char v[] = "-vvvvv";
0169     int vcnt = min(verbose, (int) sizeof(v) - 1);
0170     char cmd[3*PATH_MAX];
0171 
0172     if (verbose > 0)
0173         vcnt++;
0174 
0175     scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
0176           d, d, perf, vcnt, v);
0177 
0178     return system(cmd) ? TEST_FAIL : TEST_OK;
0179 }
0180 
0181 static int test__attr(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0182 {
0183     struct stat st;
0184     char path_perf[PATH_MAX];
0185     char path_dir[PATH_MAX];
0186     char *exec_path;
0187 
0188     if (perf_pmu__has_hybrid())
0189         return TEST_SKIP;
0190 
0191     /* First try development tree tests. */
0192     if (!lstat("./tests", &st))
0193         return run_dir("./tests", "./perf");
0194 
0195     exec_path = get_argv_exec_path();
0196     if (exec_path == NULL)
0197         return -1;
0198 
0199     /* Then installed path. */
0200     snprintf(path_dir,  PATH_MAX, "%s/tests", exec_path);
0201     snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
0202     free(exec_path);
0203 
0204     if (!lstat(path_dir, &st) &&
0205         !lstat(path_perf, &st))
0206         return run_dir(path_dir, path_perf);
0207 
0208     return TEST_SKIP;
0209 }
0210 
0211 DEFINE_SUITE("Setup struct perf_event_attr", attr);