1/*
2 * from ftp://ftp.netbsd.org/pub/NetBSD/misc/gmcgarry/bench/syscallbench.tar.gz
3 *
4 * gcc -Wall -Werror -O3 -static -o ctx ctx.c
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/time.h>
10#include <unistd.h>
11
12#include <OS.h>
13
14#define ITERATIONS	500000
15
16static void
17usage(void)
18{
19	printf("syscallbench [-h]\n");
20	exit(1);
21}
22
23static int null_func(void)
24{
25	return 0;
26}
27
28static unsigned long test_func(int (*func)(void))
29{
30	struct timeval before, after;
31	unsigned long elapsed;
32	int i;
33
34	gettimeofday(&before, NULL);
35	for (i=0; i<ITERATIONS; i++) {
36		func();
37	}
38	gettimeofday(&after, NULL);
39
40	elapsed = 1000000 * (after.tv_sec - before.tv_sec);
41	elapsed += after.tv_usec - before.tv_usec;
42	return elapsed;
43}
44
45int
46main(int argc, char *argv[])
47{
48	unsigned long overhead;
49	unsigned long libcall;
50	unsigned long syscall;
51
52	if (argc > 1)
53		usage();
54
55	overhead = test_func(&null_func);
56	libcall = test_func((void *)&getpid); // getpid is currently implemented as a library function returning the value of a global
57	syscall = test_func((void *)&is_computer_on);
58
59	printf("overhead time: %ld nanoseconds\n",
60	    (1000*(overhead))/ITERATIONS);
61
62	printf("libcall time: %ld nanoseconds\n",
63	    (1000*(libcall-overhead))/ITERATIONS);
64
65	printf("syscall time: %ld nanoseconds\n",
66	    (1000*(syscall-overhead))/ITERATIONS);
67
68	return (0);
69}
70