summaryrefslogtreecommitdiff
path: root/src/cmd/timebench.c
blob: 9985c7fef4cb949db136360bd176f0bfba346168 (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
#include <camellia.h>
#include <camellia/syscalls.h>
#include <err.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

uint64_t
gettime_ascii(hid_t h)
{
	char buf[32];
	_sys_read(h, buf, 32, 0);
	return strtol(buf, NULL, 10);
}

uint64_t
gettime_binary(hid_t h)
{
	uint64_t t;
	_sys_read(h, &t, 8, 0);
	return t;
}

uint64_t
gettime_syscall(hid_t)
{
	return _sys_time(0);
}

void
usage(int ret)
{
	fprintf(stderr,
		"usage: timebench [-a file | -b file | -s]\n"
		"  e.g. timebench -a /dev/ntp\n"
		"       timebench -b /dev/bintime\n"
		"       timebench -s\n"
	);
	exit(ret);
}

int
main(int argc, char *argv[])
{
	uint64_t (*fn)(hid_t) = NULL;
	hid_t h = -1;

	int opt;
	while ((opt = getopt(argc, argv, "abs")) != -1) {
		switch (opt) {
		case 'a':
			if (fn) usage(1);
			fn = gettime_ascii;
			break;
		case 'b':
			if (fn) usage(1);
			fn = gettime_binary;
			break;
		case 's':
			if (fn) usage(1);
			fn = gettime_syscall;
			break;
		default:
			usage(1);
		}
	}
	if (fn == NULL) {
		usage(1);
	} else if (fn == gettime_syscall) {
		if (optind < argc) {
			usage(1);
		}
	} else {
		if (optind + 1 != argc) {
			usage(1);
		}
		h = camellia_open(argv[optind], OPEN_READ);
		if (h < 0) {
			err(1, "open %s", argv[optind]);
		}
	}

	uint64_t target = 5000l * 1000000l;
	uint64_t start;
	uint64_t count = 0;
	uint64_t last;

	start = fn(h);
	target += start;
	for (;;) {
		last = fn(h);
		count++;
		if (last >= target) break;
	}
	printf("%lu calls in %lums\n", count, (last-start) / 1000000);
	printf("%luμs per call\n", (last-start) / count);
	printf("last %lu\n", last);
}