~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/attr.c

Version: ~ [ linux-5.6 ] ~ [ linux-5.5.13 ] ~ [ linux-5.4.28 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.113 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.174 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.217 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.217 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  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 <debug.h>
 23 #include <errno.h>
 24 #include <inttypes.h>
 25 #include <stdlib.h>
 26 #include <stdio.h>
 27 #include <linux/types.h>
 28 #include <linux/kernel.h>
 29 #include <sys/param.h>
 30 #include <sys/types.h>
 31 #include <sys/stat.h>
 32 #include <unistd.h>
 33 #include "../perf.h"
 34 #include <subcmd/exec-cmd.h>
 35 #include "tests.h"
 36 
 37 #define ENV "PERF_TEST_ATTR"
 38 
 39 static char *dir;
 40 static bool ready;
 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 
 50 #define __WRITE_ASS(str, fmt, data)                                     \
 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 
 64 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
 65 
 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 
 72         if (!ready)
 73                 return 0;
 74 
 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 */
 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);
 97 
 98         /* struct perf_event_attr */
 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");
119         WRITE_ASS(watermark,      "d");
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(mmap2,          "d");
128         WRITE_ASS(comm_exec,      "d");
129         WRITE_ASS(context_switch, "d");
130         WRITE_ASS(write_backward, "d");
131         WRITE_ASS(namespaces,     "d");
132         WRITE_ASS(use_clockid,    "d");
133         WRITE_ASS(wakeup_events, PRIu32);
134         WRITE_ASS(bp_type, PRIu32);
135         WRITE_ASS(config1, "llu");
136         WRITE_ASS(config2, "llu");
137         WRITE_ASS(branch_sample_type, "llu");
138         WRITE_ASS(sample_regs_user,   "llu");
139         WRITE_ASS(sample_stack_user,  PRIu32);
140 
141         fclose(file);
142         return 0;
143 }
144 
145 void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
146                      int fd, int group_fd, unsigned long flags)
147 {
148         int errno_saved = errno;
149 
150         if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
151                 pr_err("test attr FAILED");
152                 exit(128);
153         }
154 
155         errno = errno_saved;
156 }
157 
158 void test_attr__ready(void)
159 {
160         if (unlikely(test_attr__enabled) && !ready)
161                 ready = true;
162 }
163 
164 static int run_dir(const char *d, const char *perf)
165 {
166         char v[] = "-vvvvv";
167         int vcnt = min(verbose, (int) sizeof(v) - 1);
168         char cmd[3*PATH_MAX];
169 
170         if (verbose > 0)
171                 vcnt++;
172 
173         scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
174                   d, d, perf, vcnt, v);
175 
176         return system(cmd) ? TEST_FAIL : TEST_OK;
177 }
178 
179 int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused)
180 {
181         struct stat st;
182         char path_perf[PATH_MAX];
183         char path_dir[PATH_MAX];
184 
185         /* First try developement tree tests. */
186         if (!lstat("./tests", &st))
187                 return run_dir("./tests", "./perf");
188 
189         /* Then installed path. */
190         snprintf(path_dir,  PATH_MAX, "%s/tests", get_argv_exec_path());
191         snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
192 
193         if (!lstat(path_dir, &st) &&
194             !lstat(path_perf, &st))
195                 return run_dir(path_dir, path_perf);
196 
197         return TEST_SKIP;
198 }
199 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp