summaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/event-times.c
blob: 1e8a9f5c356dd623226c5fb7dee5e4f30b002b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/string.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <sys/wait.h>
#include <perf/cpumap.h>
#include "tests.h"
#include "evlist.h"
#include "evsel.h"
#include "debug.h"
#include "parse-events.h"
#include "thread_map.h"
#include "target.h"

static int attach__enable_on_exec(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);
	struct target target = {
		.uid = UINT_MAX,
	};
	const char *argv[] = { "true", NULL, };
	char sbuf[STRERR_BUFSIZE];
	int err;

	pr_debug("attaching to spawned child, enable on exec\n");

	err = perf_evlist__create_maps(evlist, &target);
	if (err < 0) {
		pr_debug("Not enough memory to create thread/cpu maps\n");
		return err;
	}

	err = perf_evlist__prepare_workload(evlist, &target, argv, false, NULL);
	if (err < 0) {
		pr_debug("Couldn't run the workload!\n");
		return err;
	}

	evsel->core.attr.enable_on_exec = 1;

	err = evlist__open(evlist);
	if (err < 0) {
		pr_debug("perf_evlist__open: %s\n",
			 str_error_r(errno, sbuf, sizeof(sbuf)));
		return err;
	}

	return perf_evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL;
}

static int detach__enable_on_exec(struct evlist *evlist)
{
	waitpid(evlist->workload.pid, NULL, 0);
	return 0;
}

static int attach__current_disabled(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);
	struct perf_thread_map *threads;
	int err;

	pr_debug("attaching to current thread as disabled\n");

	threads = thread_map__new(-1, getpid(), UINT_MAX);
	if (threads == NULL) {
		pr_debug("thread_map__new\n");
		return -1;
	}

	evsel->core.attr.disabled = 1;

	err = perf_evsel__open_per_thread(evsel, threads);
	if (err) {
		pr_debug("Failed to open event cpu-clock:u\n");
		return err;
	}

	perf_thread_map__put(threads);
	return evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL;
}

static int attach__current_enabled(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);
	struct perf_thread_map *threads;
	int err;

	pr_debug("attaching to current thread as enabled\n");

	threads = thread_map__new(-1, getpid(), UINT_MAX);
	if (threads == NULL) {
		pr_debug("failed to call thread_map__new\n");
		return -1;
	}

	err = perf_evsel__open_per_thread(evsel, threads);

	perf_thread_map__put(threads);
	return err == 0 ? TEST_OK : TEST_FAIL;
}

static int detach__disable(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);

	return evsel__enable(evsel);
}

static int attach__cpu_disabled(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);
	struct perf_cpu_map *cpus;
	int err;

	pr_debug("attaching to CPU 0 as enabled\n");

	cpus = perf_cpu_map__new("0");
	if (cpus == NULL) {
		pr_debug("failed to call perf_cpu_map__new\n");
		return -1;
	}

	evsel->core.attr.disabled = 1;

	err = perf_evsel__open_per_cpu(evsel, cpus, -1);
	if (err) {
		if (err == -EACCES)
			return TEST_SKIP;

		pr_debug("Failed to open event cpu-clock:u\n");
		return err;
	}

	perf_cpu_map__put(cpus);
	return evsel__enable(evsel);
}

static int attach__cpu_enabled(struct evlist *evlist)
{
	struct evsel *evsel = evlist__last(evlist);
	struct perf_cpu_map *cpus;
	int err;

	pr_debug("attaching to CPU 0 as enabled\n");

	cpus = perf_cpu_map__new("0");
	if (cpus == NULL) {
		pr_debug("failed to call perf_cpu_map__new\n");
		return -1;
	}

	err = perf_evsel__open_per_cpu(evsel, cpus, -1);
	if (err == -EACCES)
		return TEST_SKIP;

	perf_cpu_map__put(cpus);
	return err ? TEST_FAIL : TEST_OK;
}

static int test_times(int (attach)(struct evlist *),
		      int (detach)(struct evlist *))
{
	struct perf_counts_values count;
	struct evlist *evlist = NULL;
	struct evsel *evsel;
	int err = -1, i;

	evlist = evlist__new();
	if (!evlist) {
		pr_debug("failed to create event list\n");
		goto out_err;
	}

	err = parse_events(evlist, "cpu-clock:u", NULL);
	if (err) {
		pr_debug("failed to parse event cpu-clock:u\n");
		goto out_err;
	}

	evsel = evlist__last(evlist);
	evsel->core.attr.read_format |=
		PERF_FORMAT_TOTAL_TIME_ENABLED |
		PERF_FORMAT_TOTAL_TIME_RUNNING;

	err = attach(evlist);
	if (err == TEST_SKIP) {
		pr_debug("  SKIP  : not enough rights\n");
		return err;
	}

	TEST_ASSERT_VAL("failed to attach", !err);

	for (i = 0; i < 100000000; i++) { }

	TEST_ASSERT_VAL("failed to detach", !detach(evlist));

	perf_evsel__read(&evsel->core, 0, 0, &count);

	err = !(count.ena == count.run);

	pr_debug("  %s: ena %" PRIu64", run %" PRIu64"\n",
		 !err ? "OK    " : "FAILED",
		 count.ena, count.run);

out_err:
	evlist__delete(evlist);
	return !err ? TEST_OK : TEST_FAIL;
}

/*
 * This test creates software event 'cpu-clock'
 * attaches it in several ways (explained below)
 * and checks that enabled and running times
 * match.
 */
int test__event_times(struct test *test __maybe_unused, int subtest __maybe_unused)
{
	int err, ret = 0;

#define _T(attach, detach)			\
	err = test_times(attach, detach);	\
	if (err && (ret == TEST_OK || ret == TEST_SKIP))	\
		ret = err;

	/* attach on newly spawned process after exec */
	_T(attach__enable_on_exec,   detach__enable_on_exec)
	/* attach on current process as enabled */
	_T(attach__current_enabled,  detach__disable)
	/* attach on current process as disabled */
	_T(attach__current_disabled, detach__disable)
	/* attach on cpu as disabled */
	_T(attach__cpu_disabled,     detach__disable)
	/* attach on cpu as enabled */
	_T(attach__cpu_enabled,      detach__disable)

#undef _T
	return ret;
}