1 #define _POSIX_C_SOURCE 200809L
13 #define B(f) void f(int);
17 static int verbose = 1;
19 void error__(const char *n, int l, const char *s, ...) {
20 fprintf(stderr, "use error in tests only\n");
24 static unsigned long long start;
25 static unsigned long long dt;
26 //static unsigned long long bytes;
28 #define SEC 1000000000ULL
29 #define MAXN 500000000
31 static unsigned long long tic() {
34 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
35 fprintf(stderr, "bench: clock_gettime failed: %s\n", strerror(errno));
38 return ts.tv_sec*SEC + ts.tv_nsec;
59 unsigned long long n = dt/N;
74 /* round up to a nice number */
75 for (i = 1; i < n; i *= 10);
86 int maj, min, in_heap=0;
88 size_t vm_size=0, vm_rss=0, vm_priv_dirty=0;
90 f = fopen("/proc/self/smaps", "rb");
91 if (f) while (fgets(buf, sizeof buf, f)) {
92 if (sscanf(buf, "%*lx-%*lx %*s %*lx %x:%x %*lu %*s", &maj, &min)==2)
93 in_heap = (!maj && !min && !strstr(buf, "---p") && (strstr(buf, "[heap]") || !strchr(buf, '[')));
95 if (sscanf(buf, "Size: %lu", &l)==1) vm_size += l;
96 else if (sscanf(buf, "Rss: %lu", &l)==1) vm_rss += l;
97 else if (sscanf(buf, "Private_Dirty: %lu", &l)==1) vm_priv_dirty += l;
101 fprintf(stderr, " %7zu virt %7zu res %7zu dirty", vm_size, vm_rss, vm_priv_dirty);
106 fprintf(stderr, "%10d N %10llu ns/op ", N, dt/N);
108 fprintf(stderr, "%10d N %13.2f ns/op", N, (double)dt/N);
114 static void run(const char *name, void (*f)(int)) {
118 if (p<0 || wait(&s)<0 || !WIFEXITED(s) || WEXITSTATUS(s))
119 fprintf(stderr, "benchmark %s failed\n", name);
122 fprintf(stderr, "%-32s", name);
123 for (N=1; ; N=nextN()) {
124 // TODO: fork at each iteration and pass N,dt..?
129 // fprintf(stderr, "%10d%12llu next: %d\n", N, dt, nextN());
130 if (dt >= SEC/2 || N >= MAXN) {
135 fprintf(stderr, "bench: fatal: N <= 0\n");
142 #define B(t) run(#t, t);