summaryrefslogtreecommitdiffstats
path: root/kernel_pf.c
blob: 20f9784eac1f5fa4526a4b80654c6e2ca71c56c8 (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
#define __STDC_FORMAT_MACROS

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <err.h>

#define NPAGES 512
#define PAGE_SIZE 4096
static uint64_t faults = 0;

static void iter(void)
{
	clock_getres(CLOCK_REALTIME, (void *)8);
	faults++;
}

int main(int argc, char **argv)
{
	// Warm up
	for (int i = 0; i < 1000000; i++)
		iter();
	faults = 0;

	struct timespec start;
	clock_gettime(CLOCK_MONOTONIC, &start);

	for (int i = 0; i < 1000000; i++)
		iter();

	struct timespec end;
	clock_gettime(CLOCK_MONOTONIC, &end);
	unsigned long long duration = (end.tv_nsec - start.tv_nsec) + 1000000000ULL * (end.tv_sec - start.tv_sec);

	printf("%ld faults in %.5fs = %.2f nsec / loop\n",
	       (long)faults, (float)duration * 1e-9f,
	       (float)duration / faults);
	return 0;
}