aboutsummaryrefslogtreecommitdiffstats
path: root/bin/testcounters.c
blob: 207851ae140b85989288a2bdead0b518f2f8ffe5 (plain) (blame)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#if 0
/* done in Makefile */
#define _GNU_SOURCE        /* or _BSD_SOURCE or _SVID_SOURCE */
#endif

#include "asm/unistd.h"		/* from kernel source tree */
#include <unistd.h>		/* for syscall */

#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <stdint.h>	/* rt_param needs uint32 */

#include "../../litmus-rt/include/linux/perf_event.h"

#include <litmus/rt_param.h> /* page size macro */

/* from kernel tools/perf/perf.h */
static inline int sys_perf_event_open(struct perf_event_attr *attr, pid_t pid,
		int cpu, int group_fd, unsigned long flags)
{
        attr->size = sizeof(*attr);
	return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
}

#define C(x)	(PERF_COUNT_HW_CACHE_##x)
#define ATTR_CONFIG_CACHE(cache, op, result) \
	(((C(cache)  & 0xffULL) <<  0) | \
	 ((C(op)     & 0xffULL) <<  8) | \
	 ((C(result) & 0xffULL) << 16))

#define ATTR_CONFIG(event, umask)   \
	((((event) & 0xffULL) << 0) | \
	 (((umask) & 0xffULL) << 8))

struct perf_event_attr perf_event_attr = {
	.type            = 0,     /* set per initilized event */
	.size            = 0,     /* set later */
	.config          = 0,     /* set per initilized event */
	{ .sample_period = 0, }, /* is a counter, so no period */
	.disabled        = 0,     /* event is enabled */
	.inherit         = 0,     /* children don't inherit */
	.pinned          = 0,     /* set per initilized event */
	.exclusive       = 0,     /* set per initilized event */
	.exclude_user    = 0,     /* don't count user      */
	.exclude_kernel  = 0,     /* ditto kernel          */
	.exclude_hv      = 0,     /* ditto hypervisor      */
	.exclude_idle    = 0,     /* don't count when idle */
	.mmap            = 0,     /* include mmap data     */
	.comm            = 0,     /* include comm data     */
};

/* Pound */
#define NR_CPUS		4
#define CACHE_SIZE_MB	8
#define ASSOC		16
#define LINE_SIZE	64
#define CACHE_SIZE	(CACHE_SIZE_MB * 1024 * 1024)

/* arena size in bytes */
//#define ARENA_SIZE	(CACHE_SIZE * 14 / 16)
#define ARENA_SIZE	(CACHE_SIZE * 1)

/* number of pages in arena */
#define ARENA_PAGES	(ARENA_SIZE / PAGE_SIZE)

/* number of cache lines per page */
#define PAGE_LINES	(PAGE_SIZE / LINE_SIZE)

/* number of cache lines in arena */
#define ARENA_LINES	(ARENA_SIZE / LINE_SIZE)

/* number of integers in arena */
#define ARENA_INTS	(ARENA_SIZE / sizeof(int))

/* number of integers in a page */
#define PAGE_INTS	(PAGE_SIZE / sizeof(int))

/* number of integers in a cache line */
#define LINE_INTS	(LINE_SIZE / sizeof(int))

/* convert page number and cache line number to an integer index */
#define PAGE_AND_LINE_TO_IDX(page, line) \
	(((page) * PAGE_INTS) + ((line) * LINE_INTS))


/* not really a good way to do this */
inline int randrange(const int max)
{
	return (rand() / (RAND_MAX / max + 1));
}

void sequential(int *items, const int len)
{
	int i;
	for (i = 0; i < len; i++)
		items[i] = (i + 1) % len;
}

/* Sattolo's algorithm makes a cycle. */
void sattolo(int *items, const int len)
{
	int i;
	for (i = 0; i < len; i++)
		items[i] = i;
	while (1 < i--) {
		/* 0 <= j < i */
		int t, j = randrange(i);
		t = items[i];
		items[i] = items[j];
		items[j] = t;
	}
}

/*
 * Write the order to read the arena into the arena. Each page in the arena is
 * read back, but the page is read in a random order to prevent the prefetcher
 * from working.
 */
static void init_arena_page_line_order(int *arena, int *page_line_order)
{
	int cur_page;
	for (cur_page = 0; cur_page < ARENA_PAGES; cur_page++) {
		/* for each page in the arena */
		int cur_line;
		for (cur_line = 0; cur_line < PAGE_LINES; cur_line++) {
			/* for each line in the page */
			const int idx = PAGE_AND_LINE_TO_IDX(cur_page,
					cur_line);
			const int next_line = page_line_order[cur_line];
			int next_idx = PAGE_AND_LINE_TO_IDX(cur_page,
					next_line);

			if (!next_line) {
				/* special case: last line in the page */
				if (cur_page < ARENA_PAGES - 1) {
					/* arena has more pages: go to next */
					next_idx = PAGE_AND_LINE_TO_IDX(
							(cur_page + 1), 0);
				} else {
					/* the very last element */
					next_idx = 0;
				}
			}
			arena[idx] = next_idx;
		}
	}
}

static int loop_once(const int perf_fd, int *arena)
{
	int i = 0, j;
	do {
		i = arena[i];
		j = i;
	} while (i);
	return j;
}

static int set_affinity(int cpu)
{
	cpu_set_t cpu_set;
	CPU_ZERO(&cpu_set);
	CPU_SET(cpu, &cpu_set);
	return sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set);
}

struct perf_fd {
	int			fd;
	char			*name;
	enum perf_type_id	type;
	__u64			config;
	__u64			exclusive    :  1,
				pinned       :  1,
				__reserved_1 : 62;
};

#define PERF_FD_EMPTY(p) \
	((p)->fd == 0 && (p)->name == NULL && \
	 (p)->type == 0 && (p)->config == 0)
#define PERF_FD_NON_EMPTY(p) (!PERF_FD_EMPTY(p))


#if 0
/* these events are always zero */
static struct perf_fd perf_fds[] = {
	{
		.fd	   = -1,
		.name	   = "MEM_UNCORE_RETIRED.REMOTE_CACHE_LOCAL_HOME_HIT",
		.type	   = PERF_TYPE_RAW,
		.config	   = ATTR_CONFIG(0x0f, 0x08),
		.exclusive = 0,
		.pinned    = 0,
	},
	{
		.fd	= -1,
		.name	= "MEM_UNCORE_RETIRED.REMOTE_DRAM",
		.type	= PERF_TYPE_RAW,
		.config	= ATTR_CONFIG(0x0f, 0x10),
		.exclusive = 0, /* child events cannot be exclusive */
		.pinned    = 0, /* child events cannot be pinned */
	},
	{ },
};
#endif

static struct perf_fd perf_fds[] = {
	/* first element is assumed to be group leader */
#if 0
	{
		.fd	= -1,
		.name	= "MEM_UNCORE_RETIRED.LOCAL_DRAM",
		.type	= PERF_TYPE_RAW,
		.config	= ATTR_CONFIG(0x0f, 0x20),
		.exclusive = 1, /* group leader is scheduled exclusively */
		.pinned    = 1, /* group leader is pinnned to CPU (always on) */
	},
#endif
	{
		.fd	= -1,
		.name	= "L2_RQSTS.PREFETCH_HIT",
		.type	= PERF_TYPE_RAW,
		.config	= ATTR_CONFIG(0x24, 0x40),
#if 0
		.exclusive = 0,
		.pinned    = 0,
#endif
		.exclusive = 1, /* group leader is scheduled exclusively */
		.pinned    = 1, /* group leader is pinnned to CPU (always on) */
	},
	{
		.fd	= -1,
		.name	= "L2_RQSTS.PREFETCH_MISS",
		.type	= PERF_TYPE_RAW,
		.config	= ATTR_CONFIG(0x24, 0x80),
		.exclusive = 0,
		.pinned    = 0,
	},
	{
		.fd	= -1,
		.name	= "MEM_LOAD_RETIRED.L3_MISS",
		.type	= PERF_TYPE_RAW,
		.config	= ATTR_CONFIG(0xcb, 0x10),
		.exclusive = 0,
		.pinned    = 0,
	},
	{
		.fd	   = -1,
		.name	   = "Off Core Response Counter",
		.type	   = PERF_TYPE_HW_CACHE,
		.config	   = ATTR_CONFIG_CACHE(LL, OP_READ, RESULT_MISS),
#if 0
		/* read misses */
		.config	= ATTR_CONFIG_CACHE(LL, OP_READ, RESULT_MISS),
		/* write misses */
		.config	= ATTR_CONFIG_CACHE(LL, OP_WRITE, RESULT_MISS),
		/* prefetch misses */
		.config	= ATTR_CONFIG_CACHE(LL, OP_PREFETCH, RESULT_MISS),
#endif
		.exclusive = 0,
		.pinned    = 0,
	},
	{ },
};


static inline void events_ioctl(const int request)
{
	ioctl(perf_fds[0].fd, request);
}

static void do_read(double divide)
{
	struct perf_fd *perf_fd;
	for (perf_fd = perf_fds; PERF_FD_NON_EMPTY(perf_fd); perf_fd++) {
		__u64 perf_val;
		ssize_t ret;
		ret = read(perf_fd->fd, &perf_val, sizeof(perf_val));
		if (0 >= ret)
			printf("%50s: ERROR\n", perf_fd->name);
		else
			printf("%50s: %10.3f\n",
					perf_fd->name, (perf_val / divide));
		ioctl(perf_fd->fd, PERF_EVENT_IOC_RESET);
	}
}
  
static void write_global_perf_attr(struct perf_fd *perf_fd)
{
		perf_event_attr.type      = perf_fd->type;
		perf_event_attr.config    = perf_fd->config;
		perf_event_attr.exclusive = perf_fd->exclusive;
		perf_event_attr.pinned    = perf_fd->pinned;
}

#define CPU 0
static int setup_perf(void)
{
	/* cannot have pid == -1 and cpu == -1 */
	const int perf_pid	= -1;	/* -1: all tasks, 0: this task */
	const int perf_cpu	= CPU;	/* -1: all CPUs (follow task) */
	struct perf_fd *perf_fd;
	int err = 0;

	for (perf_fd = perf_fds; PERF_FD_NON_EMPTY(perf_fd); perf_fd++) {
		/* make a group whose leader is the zeroth element */
		const int perf_group = perf_fds[0].fd;

		/* setup the attributes to pass in */
		write_global_perf_attr(perf_fd);

		perf_fd->fd = sys_perf_event_open(&perf_event_attr, perf_pid,
				perf_cpu, perf_group, 0);

		if (0 > perf_fd->fd) {
			fprintf(stderr, "could not setup %s\n", perf_fd->name);
			err = -1;
			goto out;
		}
	}
out:
	return err;
}

int main(int argc, char **argv)
{

	const int task_cpu = CPU;
	int ret = 0, i;
	int *arena, *page_line_order;

	if (set_affinity(task_cpu)) {
		fprintf(stderr, "could not set affinity\n");
		ret = -1;
		goto out;
	}

	arena = malloc(ARENA_SIZE);
	if (!arena) {
		fprintf(stderr, "could not allocate memory\n");
		ret = -1;
		goto out;
	}

	page_line_order = malloc(PAGE_LINES * sizeof(*page_line_order));
	if (!page_line_order) {
		fprintf(stderr, "could not allocate memory\n");
		ret = -1;
		goto out;
	}

	sattolo(page_line_order, PAGE_LINES);
	//sequential(page_line_order, PAGE_LINES);
	init_arena_page_line_order(arena, page_line_order);

	if (setup_perf()) {
		ret = -1;
		goto out;
	}

	printf("arena_size:  %d\n", ARENA_SIZE);
	printf("arena_lines: %d\n", ARENA_LINES);

	printf("initially\n");
	do_read(1.0);

	events_ioctl(PERF_EVENT_IOC_ENABLE);
	loop_once(perf_fds[0].fd, arena);
	events_ioctl(PERF_EVENT_IOC_DISABLE);
	printf("after a loop\n");
	do_read(1.0);

	events_ioctl(PERF_EVENT_IOC_ENABLE);
	loop_once(perf_fds[0].fd, arena);
	events_ioctl(PERF_EVENT_IOC_DISABLE);
	printf("after another loop\n");
	do_read(1.0);

	events_ioctl(PERF_EVENT_IOC_ENABLE);
	loop_once(perf_fds[0].fd, arena);
	events_ioctl(PERF_EVENT_IOC_DISABLE);
	printf("after another loop\n");
	do_read(1.0);

	events_ioctl(PERF_EVENT_IOC_ENABLE);
	loop_once(perf_fds[0].fd, arena);
	events_ioctl(PERF_EVENT_IOC_DISABLE);
	printf("after another loop\n");
	do_read(1.0);

	events_ioctl(PERF_EVENT_IOC_ENABLE);
	for (i = 0; i < 100; i++)
		loop_once(perf_fds[0].fd, arena);
	events_ioctl(PERF_EVENT_IOC_DISABLE);
	printf("after 100 loops\n");
	do_read(100.0);

out:
	return ret;
}