aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/perf_event_open/perf_event_open02.c
blob: 20dbb6133f474dedb0d95e67604809680a7d62b6 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2009 Paul Mackerras <paulus@samba.org>
 * Copyright (c) 2014-2022 Linux Test Project
 */
/*
 * Here's a little test program that checks whether software counters
 * (specifically, the task clock counter) work correctly when they're in
 * a group with hardware counters.
 *
 * What it does is to create several groups, each with one hardware
 * counter, counting instructions, plus a task clock counter.  It needs
 * to know an upper bound N on the number of hardware counters you have
 * (N defaults to 8), and it creates N+4 groups to force them to be
 * multiplexed.  It also creates an overall task clock counter.
 *
 * Then it spins for a while, and then stops all the counters and reads
 * them.  It takes the total of the task clock counters in the groups and
 * computes the ratio of that total to the overall execution time from
 * the overall task clock counter.
 *
 * That ratio should be equal to the number of actual hardware counters
 * that can count instructions.  If the task clock counters in the groups
 * don't stop when their group gets taken off the PMU, the ratio will
 * instead be close to N+4.  The program will declare that the test fails
 * if the ratio is greater than N (actually, N + 0.0005 to allow for FP
 * rounding errors and RT throttling overhead).
 */

#define _GNU_SOURCE
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/time.h>
#include <sys/types.h>

#include "config.h"
#include "tst_test.h"
#include "lapi/cpuset.h"
#include "lapi/syscalls.h"

#include "perf_event_open.h"

#define MAX_CTRS	1000

struct read_format {
	unsigned long long value;
	/* if PERF_FORMAT_TOTAL_TIME_ENABLED */
	unsigned long long time_enabled;
	/* if PERF_FORMAT_TOTAL_TIME_RUNNING */
	unsigned long long time_running;
};

static char *verbose;

static int ntotal, nhw;
static int tsk0 = -1, hwfd[MAX_CTRS], tskfd[MAX_CTRS];
static int volatile work_done;
static unsigned int est_loops;

static void all_counters_set(int state)
{
	if (prctl(state) == -1)
		tst_brk(TBROK | TERRNO, "prctl(%d) failed", state);
}

static void alarm_handler(int sig LTP_ATTRIBUTE_UNUSED)
{
	work_done = 1;
}

static void bench_work(int time_ms)
{
	unsigned int i;
	struct itimerval val;
	struct sigaction sa;

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = alarm_handler;
	sa.sa_flags = SA_RESETHAND;
	SAFE_SIGACTION(SIGALRM, &sa, NULL);

	work_done = 0;
	memset(&val, 0, sizeof(val));
	val.it_value.tv_sec = time_ms / 1000;
	val.it_value.tv_usec = (time_ms % 1000) * 1000;

	if (setitimer(ITIMER_REAL, &val, NULL))
		tst_brk(TBROK | TERRNO, "setitimer");

	while (!work_done) {
		for (i = 0; i < 100000; ++i)
			asm volatile (""::"g" (i));
		est_loops++;
	}

	tst_res(TINFO, "bench_work estimated loops = %u in %d ms", est_loops, time_ms);
}

static void do_work(int mult)
{
	unsigned long i, j, loops = mult * est_loops;

	for (j = 0; j < loops; j++)
		for (i = 0; i < 100000; i++)
			asm volatile (""::"g" (i));
}

#ifndef __s390__
static int count_hardware_counters(void)
{
	struct perf_event_attr hw_event;
	int i, hwctrs = 0;
	int fdarry[MAX_CTRS];
	struct read_format buf, buf2, diff;

	memset(&hw_event, 0, sizeof(struct perf_event_attr));

	hw_event.type = PERF_TYPE_HARDWARE;
	hw_event.size = sizeof(struct perf_event_attr);
	hw_event.disabled = 1;
	hw_event.config =  PERF_COUNT_HW_INSTRUCTIONS;
	hw_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
		PERF_FORMAT_TOTAL_TIME_RUNNING;

	for (i = 0; i < MAX_CTRS; i++) {
		fdarry[i] = perf_event_open(&hw_event, 0, -1, -1, 0);

		all_counters_set(PR_TASK_PERF_EVENTS_ENABLE);
		do_work(1);
		if (read(fdarry[i], &buf, sizeof(buf)) != sizeof(buf))
			tst_brk(TBROK | TERRNO, "error reading counter(s) #1");
		do_work(1);
		all_counters_set(PR_TASK_PERF_EVENTS_DISABLE);
		if (read(fdarry[i], &buf2, sizeof(buf2)) != sizeof(buf2))
			tst_brk(TBROK | TERRNO, "error reading counter(s) #2");

		diff.value = buf2.value - buf.value;
		diff.time_enabled = buf2.time_enabled - buf.time_enabled;
		diff.time_running = buf2.time_running - buf.time_running;

		tst_res(TINFO, "[%d] value:%lld time_enabled:%lld "
		       "time_running:%lld", i, diff.value,
		       diff.time_enabled, diff.time_running);

		/*
		 * Normally time_enabled and time_running are the same value.
		 * But if more events are started than available counter slots
		 * on the PMU, then multiplexing happens and events run only
		 * part of the time. Time_enabled and time_running's values
		 * will be different. In this case the time_enabled and time_
		 * running values can be used to scale an estimated value for
		 * the count. So if buf.time_enabled and buf.time_running are
		 * not equal, we can think that PMU hardware counters
		 * multiplexing happens and the number of the opened events
		 * are the number of max available hardware counters.
		 */
		if (diff.time_enabled != diff.time_running) {
			hwctrs = i;
			break;
		}
	}

	for (i = 0; i <= hwctrs; i++)
		SAFE_CLOSE(fdarry[i]);

	return hwctrs;
}
#endif /* __s390__ */

static void bind_to_current_cpu(void)
{
#ifdef HAVE_SCHED_GETCPU
	int cpu = sched_getcpu();
	size_t mask_size;
	cpu_set_t *mask;

	if (cpu == -1)
		tst_brk(TBROK | TERRNO, "sched_getcpu() failed");

	mask = CPU_ALLOC(cpu + 1);
	mask_size = CPU_ALLOC_SIZE(cpu + 1);
	CPU_ZERO_S(mask_size, mask);
	CPU_SET(cpu, mask);
	if (sched_setaffinity(0, mask_size, mask) == -1)
		tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
	CPU_FREE(mask);
#endif
}

static void setup(void)
{
	int i;
	struct perf_event_attr tsk_event, hw_event;

	for (i = 0; i < MAX_CTRS; i++) {
		hwfd[i] = -1;
		tskfd[i] = -1;
	}

	bench_work(500);

	/*
	 * According to perf_event_open's manpage, the official way of
	 * knowing if perf_event_open() support is enabled is checking for
	 * the existence of the file /proc/sys/kernel/perf_event_paranoid.
	 */
	if (access("/proc/sys/kernel/perf_event_paranoid", F_OK) == -1)
		tst_brk(TCONF, "Kernel doesn't have perf_event support");

	bind_to_current_cpu();
#ifdef __s390__
	/*
	 * On s390 the "time_enabled" and "time_running" values are always the
	 * same, therefore count_hardware_counters() does not work.
	 *
	 * There are distinct/dedicated counters that can be used independently.
	 * Use the dedicated counter for instructions here.
	 */
	ntotal = nhw = 1;
#else
	nhw = count_hardware_counters();
	ntotal = nhw + 4;
#endif

	memset(&hw_event, 0, sizeof(struct perf_event_attr));
	memset(&tsk_event, 0, sizeof(struct perf_event_attr));

	tsk_event.type =  PERF_TYPE_SOFTWARE;
	tsk_event.size = sizeof(struct perf_event_attr);
	tsk_event.disabled = 1;
	tsk_event.config = PERF_COUNT_SW_TASK_CLOCK;

	hw_event.type = PERF_TYPE_HARDWARE;
	hw_event.size = sizeof(struct perf_event_attr);
	hw_event.disabled = 1;
	hw_event.config =  PERF_COUNT_HW_INSTRUCTIONS;

	tsk0 = perf_event_open(&tsk_event, 0, -1, -1, 0);
	tsk_event.disabled = 0;
	for (i = 0; i < ntotal; ++i) {
		hwfd[i] = perf_event_open(&hw_event, 0, -1, -1, 0);
		tskfd[i] = perf_event_open(&tsk_event, 0, -1, hwfd[i], 0);
	}
}

static void cleanup(void)
{
	int i;

	for (i = 0; i < ntotal; i++) {
		if (hwfd[i] != -1)
			SAFE_CLOSE(hwfd[i]);
		if (tskfd[i] != -1)
			SAFE_CLOSE(tskfd[i]);
	}

	if (tsk0 != -1)
		SAFE_CLOSE(tsk0);
}

static void verify(void)
{
	unsigned long long vt0, vt[MAX_CTRS], vh[MAX_CTRS];
	unsigned long long vtsum = 0, vhsum = 0;
	int i;
	double ratio;
	struct sched_param sparam = {.sched_priority = 1};

	if (sched_setscheduler(0, SCHED_FIFO, &sparam)) {
		tst_brk(TBROK | TERRNO,
			"sched_setscheduler(0, SCHED_FIFO, ...) failed");
	}

	all_counters_set(PR_TASK_PERF_EVENTS_ENABLE);
	do_work(8);
	/* stop groups with hw counters first before tsk0 */
	for (i = 0; i < ntotal; i++) {
		ioctl(hwfd[i], PERF_EVENT_IOC_DISABLE);
		ioctl(tskfd[i], PERF_EVENT_IOC_DISABLE);
	}
	all_counters_set(PR_TASK_PERF_EVENTS_DISABLE);

	sparam.sched_priority = 0;
	if (sched_setscheduler(0, SCHED_OTHER, &sparam)) {
		tst_brk(TBROK | TERRNO,
			"sched_setscheduler(0, SCHED_OTHER, ...) failed");
	}

	if (read(tsk0, &vt0, sizeof(vt0)) != sizeof(vt0))
		tst_brk(TBROK | TERRNO, "error reading task clock counter");

	for (i = 0; i < ntotal; ++i) {
		if (read(tskfd[i], &vt[i], sizeof(vt[i])) != sizeof(vt[i]) ||
		    read(hwfd[i], &vh[i], sizeof(vh[i])) != sizeof(vh[i]))
			tst_brk(TBROK | TERRNO, "error reading counter(s)");
		vtsum += vt[i];
		vhsum += vh[i];
	}

	tst_res(TINFO, "nhw: %d, overall task clock: %llu", nhw, vt0);
	tst_res(TINFO, "hw sum: %llu, task clock sum: %llu", vhsum, vtsum);

	if (verbose) {
		tst_res(TINFO, "hw counters:");
		for (i = 0; i < ntotal; ++i)
			tst_res(TINFO, " %llu", vh[i]);
		tst_res(TINFO, "task clock counters:");
		for (i = 0; i < ntotal; ++i)
			tst_res(TINFO, " %llu", vt[i]);
	}

	ratio = (double)vtsum / vt0;
	tst_res(TINFO, "ratio: %lf", ratio);
	if (ratio > nhw + 0.0005) {
		tst_res(TFAIL, "test failed (ratio was greater than %d)", nhw);
	} else {
		tst_res(TPASS, "test passed");
	}
}

static struct tst_test test = {
	.setup = setup,
	.cleanup = cleanup,
	.options = (struct tst_option[]) {
		{"v", &verbose, "Verbose output"},
		{},
	},
	.test_all = verify,
	.needs_root = 1,
	.max_runtime = 72
};