Linux Audio

Check our new training course

Loading...
v4.6
 
  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
 
 
 
 21#include <stdlib.h>
 22#include <stdio.h>
 23#include <linux/types.h>
 24#include <linux/kernel.h>
 25#include "../perf.h"
 26#include "util.h"
 
 
 27#include <subcmd/exec-cmd.h>
 
 
 28#include "tests.h"
 
 29
 30#define ENV "PERF_TEST_ATTR"
 31
 32extern int verbose;
 33
 34static char *dir;
 
 35
 36void 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)					\
 45do {									\
 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#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
 59
 60static int store_event(struct perf_event_attr *attr, pid_t pid, int cpu,
 61		       int fd, int group_fd, unsigned long flags)
 62{
 63	FILE *file;
 64	char path[PATH_MAX];
 65
 
 
 
 66	snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
 67		 attr->type, attr->config, fd);
 68
 69	file = fopen(path, "w+");
 70	if (!file) {
 71		perror("test attr - failed to open event file");
 72		return -1;
 73	}
 74
 75	if (fprintf(file, "[event-%d-%llu-%d]\n",
 76		    attr->type, attr->config, fd) < 0) {
 77		perror("test attr - failed to write event file");
 78		fclose(file);
 79		return -1;
 80	}
 81
 82	/* syscall arguments */
 83	__WRITE_ASS(fd,       "d", fd);
 84	__WRITE_ASS(group_fd, "d", group_fd);
 85	__WRITE_ASS(cpu,      "d", cpu);
 86	__WRITE_ASS(pid,      "d", pid);
 87	__WRITE_ASS(flags,   "lu", flags);
 88
 89	/* struct perf_event_attr */
 90	WRITE_ASS(type,   PRIu32);
 91	WRITE_ASS(size,   PRIu32);
 92	WRITE_ASS(config,  "llu");
 93	WRITE_ASS(sample_period, "llu");
 94	WRITE_ASS(sample_type,   "llu");
 95	WRITE_ASS(read_format,   "llu");
 96	WRITE_ASS(disabled,       "d");
 97	WRITE_ASS(inherit,        "d");
 98	WRITE_ASS(pinned,         "d");
 99	WRITE_ASS(exclusive,      "d");
100	WRITE_ASS(exclude_user,   "d");
101	WRITE_ASS(exclude_kernel, "d");
102	WRITE_ASS(exclude_hv,     "d");
103	WRITE_ASS(exclude_idle,   "d");
104	WRITE_ASS(mmap,           "d");
105	WRITE_ASS(comm,           "d");
106	WRITE_ASS(freq,           "d");
107	WRITE_ASS(inherit_stat,   "d");
108	WRITE_ASS(enable_on_exec, "d");
109	WRITE_ASS(task,           "d");
110	WRITE_ASS(watermark,      "d");
111	WRITE_ASS(precise_ip,     "d");
112	WRITE_ASS(mmap_data,      "d");
113	WRITE_ASS(sample_id_all,  "d");
114	WRITE_ASS(exclude_host,   "d");
115	WRITE_ASS(exclude_guest,  "d");
116	WRITE_ASS(exclude_callchain_kernel, "d");
117	WRITE_ASS(exclude_callchain_user, "d");
 
 
 
 
 
 
118	WRITE_ASS(wakeup_events, PRIu32);
119	WRITE_ASS(bp_type, PRIu32);
120	WRITE_ASS(config1, "llu");
121	WRITE_ASS(config2, "llu");
122	WRITE_ASS(branch_sample_type, "llu");
123	WRITE_ASS(sample_regs_user,   "llu");
124	WRITE_ASS(sample_stack_user,  PRIu32);
125
126	fclose(file);
127	return 0;
128}
129
130void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
131		     int fd, int group_fd, unsigned long flags)
132{
133	int errno_saved = errno;
134
135	if (store_event(attr, pid, cpu, fd, group_fd, flags))
136		die("test attr FAILED");
 
 
137
138	errno = errno_saved;
139}
140
 
 
 
 
 
 
141static int run_dir(const char *d, const char *perf)
142{
143	char v[] = "-vvvvv";
144	int vcnt = min(verbose, (int) sizeof(v) - 1);
145	char cmd[3*PATH_MAX];
146
147	if (verbose)
148		vcnt++;
149
150	snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
151		 d, d, perf, vcnt, v);
152
153	return system(cmd);
154}
155
156int test__attr(int subtest __maybe_unused)
157{
158	struct stat st;
159	char path_perf[PATH_MAX];
160	char path_dir[PATH_MAX];
 
 
 
 
 
 
 
 
 
 
 
161
162	/* First try developement tree tests. */
163	if (!lstat("./tests", &st))
164		return run_dir("./tests", "./perf");
165
 
 
 
 
166	/* Then installed path. */
167	snprintf(path_dir,  PATH_MAX, "%s/tests", get_argv_exec_path());
168	snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
 
169
170	if (!lstat(path_dir, &st) &&
171	    !lstat(path_perf, &st))
172		return run_dir(path_dir, path_perf);
173
174	return TEST_SKIP;
175}
v6.8
  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 <subcmd/exec-cmd.h>
 34#include "event.h"
 35#include "util.h"
 36#include "tests.h"
 37#include "pmus.h"
 38
 39#define ENV "PERF_TEST_ATTR"
 40
 
 
 41static char *dir;
 42static bool ready;
 43
 44void test_attr__init(void)
 45{
 46	dir = getenv(ENV);
 47	test_attr__enabled = (dir != NULL);
 48}
 49
 50#define BUFSIZE 1024
 51
 52#define __WRITE_ASS(str, fmt, data)					\
 53do {									\
 54	char buf[BUFSIZE];						\
 55	size_t size;							\
 56									\
 57	size = snprintf(buf, BUFSIZE, #str "=%"fmt "\n", data);		\
 58	if (1 != fwrite(buf, size, 1, file)) {				\
 59		perror("test attr - failed to write event file");	\
 60		fclose(file);						\
 61		return -1;						\
 62	}								\
 63									\
 64} while (0)
 65
 66#define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field)
 67
 68static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
 69		       int fd, int group_fd, unsigned long flags)
 70{
 71	FILE *file;
 72	char path[PATH_MAX];
 73
 74	if (!ready)
 75		return 0;
 76
 77	snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir,
 78		 attr->type, attr->config, fd);
 79
 80	file = fopen(path, "w+");
 81	if (!file) {
 82		perror("test attr - failed to open event file");
 83		return -1;
 84	}
 85
 86	if (fprintf(file, "[event-%d-%llu-%d]\n",
 87		    attr->type, attr->config, fd) < 0) {
 88		perror("test attr - failed to write event file");
 89		fclose(file);
 90		return -1;
 91	}
 92
 93	/* syscall arguments */
 94	__WRITE_ASS(fd,       "d", fd);
 95	__WRITE_ASS(group_fd, "d", group_fd);
 96	__WRITE_ASS(cpu,      "d", cpu.cpu);
 97	__WRITE_ASS(pid,      "d", pid);
 98	__WRITE_ASS(flags,   "lu", flags);
 99
100	/* struct perf_event_attr */
101	WRITE_ASS(type,   PRIu32);
102	WRITE_ASS(size,   PRIu32);
103	WRITE_ASS(config,  "llu");
104	WRITE_ASS(sample_period, "llu");
105	WRITE_ASS(sample_type,   "llu");
106	WRITE_ASS(read_format,   "llu");
107	WRITE_ASS(disabled,       "d");
108	WRITE_ASS(inherit,        "d");
109	WRITE_ASS(pinned,         "d");
110	WRITE_ASS(exclusive,      "d");
111	WRITE_ASS(exclude_user,   "d");
112	WRITE_ASS(exclude_kernel, "d");
113	WRITE_ASS(exclude_hv,     "d");
114	WRITE_ASS(exclude_idle,   "d");
115	WRITE_ASS(mmap,           "d");
116	WRITE_ASS(comm,           "d");
117	WRITE_ASS(freq,           "d");
118	WRITE_ASS(inherit_stat,   "d");
119	WRITE_ASS(enable_on_exec, "d");
120	WRITE_ASS(task,           "d");
121	WRITE_ASS(watermark,      "d");
122	WRITE_ASS(precise_ip,     "d");
123	WRITE_ASS(mmap_data,      "d");
124	WRITE_ASS(sample_id_all,  "d");
125	WRITE_ASS(exclude_host,   "d");
126	WRITE_ASS(exclude_guest,  "d");
127	WRITE_ASS(exclude_callchain_kernel, "d");
128	WRITE_ASS(exclude_callchain_user, "d");
129	WRITE_ASS(mmap2,	  "d");
130	WRITE_ASS(comm_exec,	  "d");
131	WRITE_ASS(context_switch, "d");
132	WRITE_ASS(write_backward, "d");
133	WRITE_ASS(namespaces,	  "d");
134	WRITE_ASS(use_clockid,    "d");
135	WRITE_ASS(wakeup_events, PRIu32);
136	WRITE_ASS(bp_type, PRIu32);
137	WRITE_ASS(config1, "llu");
138	WRITE_ASS(config2, "llu");
139	WRITE_ASS(branch_sample_type, "llu");
140	WRITE_ASS(sample_regs_user,   "llu");
141	WRITE_ASS(sample_stack_user,  PRIu32);
142
143	fclose(file);
144	return 0;
145}
146
147void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
148		     int fd, int group_fd, unsigned long flags)
149{
150	int errno_saved = errno;
151
152	if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) {
153		pr_err("test attr FAILED");
154		exit(128);
155	}
156
157	errno = errno_saved;
158}
159
160void test_attr__ready(void)
161{
162	if (unlikely(test_attr__enabled) && !ready)
163		ready = true;
164}
165
166static int run_dir(const char *d, const char *perf)
167{
168	char v[] = "-vvvvv";
169	int vcnt = min(verbose, (int) sizeof(v) - 1);
170	char cmd[3*PATH_MAX];
171
172	if (verbose > 0)
173		vcnt++;
174
175	scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
176		  d, d, perf, vcnt, v);
177
178	return system(cmd) ? TEST_FAIL : TEST_OK;
179}
180
181static int test__attr(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
182{
183	struct stat st;
184	char path_perf[PATH_MAX];
185	char path_dir[PATH_MAX];
186	char *exec_path;
187
188	if (perf_pmus__num_core_pmus() > 1) {
189		/*
190		 * TODO: Attribute tests hard code the PMU type. If there are >1
191		 * core PMU then each PMU will have a different type which
192		 * requires additional support.
193		 */
194		pr_debug("Skip test on hybrid systems");
195		return TEST_SKIP;
196	}
197
198	/* First try development tree tests. */
199	if (!lstat("./tests", &st))
200		return run_dir("./tests", "./perf");
201
202	exec_path = get_argv_exec_path();
203	if (exec_path == NULL)
204		return -1;
205
206	/* Then installed path. */
207	snprintf(path_dir,  PATH_MAX, "%s/tests", exec_path);
208	snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
209	free(exec_path);
210
211	if (!lstat(path_dir, &st) &&
212	    !lstat(path_perf, &st))
213		return run_dir(path_dir, path_perf);
214
215	return TEST_SKIP;
216}
217
218DEFINE_SUITE("Setup struct perf_event_attr", attr);