diff options
author | Andy Lutomirski <luto@amacapital.net> | 2014-05-02 10:56:10 -0700 |
---|---|---|
committer | Andy Lutomirski <luto@amacapital.net> | 2014-05-02 10:56:10 -0700 |
commit | a148e717b8287d11620f32059474b4ab123a594f (patch) | |
tree | 78cb69e40708feea0d541c8609b2aec51bc9e4cd | |
parent | 10ffaf6773b02ef6e2eb9389d07082ba75839ab3 (diff) | |
download | misc-tests-a148e717b8287d11620f32059474b4ab123a594f.tar.gz |
Add kernel_pf and fix .gitignore
-rw-r--r-- | .gitignore | 8 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | kernel_pf.c | 54 |
3 files changed, 65 insertions, 4 deletions
@@ -1,8 +1,12 @@ dump-vdso +dump-vvar dump-vsyscall -timing_test +timing_test_32 +timing_test_64 time-warp-test evil-clock-test -test_vsyscall +test_vsyscall_32 +test_vsyscall_64 context_switch_latency +kernel_pf *~ @@ -1,8 +1,11 @@ .PHONY: all clean -all: timing_test_64 timing_test_32 evil-clock-test test_vsyscall_64 test_vsyscall_32 dump-vdso dump-vvar dump-vsyscall context_switch_latency +all: timing_test_64 timing_test_32 evil-clock-test test_vsyscall_64 test_vsyscall_32 dump-vdso dump-vvar dump-vsyscall context_switch_latency kernel_pf clean: - rm -f timing_test_64 timing_test_32 evil-clock-test test_vsyscall_64 test_vsyscall_32 dump-vdso dump-vvar dump-vsyscall context_switch_latency + rm -f timing_test_64 timing_test_32 evil-clock-test test_vsyscall_64 test_vsyscall_32 dump-vdso dump-vvar dump-vsyscall context_switch_latency kernel_pf + +kernel_pf: kernel_pf.c + gcc -o $@ -O2 -std=gnu99 -Wall $(EXTRA_CFLAGS) -g $^ -lrt -ldl timing_test_64: timing_test.cc g++ -m64 -o $@ -O2 -Wall $(EXTRA_CFLAGS) -g $^ -lrt -ldl diff --git a/kernel_pf.c b/kernel_pf.c new file mode 100644 index 0000000..0e0d5d6 --- /dev/null +++ b/kernel_pf.c @@ -0,0 +1,54 @@ +#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) +{ + void *addr = mmap(NULL, NPAGES * PAGE_SIZE, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (addr == MAP_FAILED) + err(1, "mmap"); + + for (int i = 0; i < NPAGES; i++) + clock_getres(CLOCK_REALTIME, addr + i * PAGE_SIZE); + + munmap(addr, NPAGES * PAGE_SIZE); + + faults += NPAGES; +} + +int main(int argc, char **argv) +{ + // Warm up + for (int i = 0; i < 1000; i++) + iter(); + faults = 0; + + struct timespec start; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < 1000; 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; +} |