6fa1963e0f80eea350961080edea76e17ffb7021
[libc-test] / common / b.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9 #include "test.h"
10
11 #define T(f)
12 #define B(f) void f(int);
13 #include "tests.h"
14 #undef B
15
16 static int verbose = 1;
17
18 void error__(const char *n, int l, const char *s, ...) {
19         fprintf(stderr, "use error in tests only\n");
20 }
21
22 static int N;
23 static unsigned long long start;
24 static unsigned long long dt;
25 //static unsigned long long bytes;
26
27 #define SEC  1000000000ULL
28 #define MAXN  500000000
29
30 static unsigned long long tic() {
31         struct timespec ts;
32
33         if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
34                 fprintf(stderr, "bench: clock_gettime failed: %s\n", strerror(errno));
35                 return 0;
36         }
37         return ts.tv_sec*SEC + ts.tv_nsec;
38 }
39
40 void start_timer() {
41         if (!start)
42                 start = tic();
43 }
44
45 void stop_timer() {
46         if (start)
47                 dt += tic() - start;
48         start = 0;
49 }
50
51 void reset_timer() {
52         if (start)
53                 start = tic();
54         dt = 0;
55 }
56
57 static int nextN() {
58         unsigned long long n = dt/N;
59         unsigned long long i;
60
61         if (n)
62                 n = SEC/2/n;
63         else
64                 n = SEC/2;
65         n += n/2;
66         if (n > N*100ULL)
67                 n = N*100ULL;
68         else if (n <= N)
69                 n = N+1;
70         if (n > MAXN)
71                 n = MAXN;
72
73         /* round up to a nice number */
74         for (i = 1; i < n; i *= 10);
75         if (i/2 >= n)
76                 i /= 2;
77         if (i/2 >= n)
78                 i /= 2;
79         return i;
80 }
81
82 void vmstats() {
83         FILE *f;
84         char buf[256];
85         int maj, min, in_heap=0;
86         unsigned long l;
87         size_t vm_size=0, vm_rss=0, vm_priv_dirty=0;
88
89         f = fopen("/proc/self/smaps", "rb");
90         if (f) while (fgets(buf, sizeof buf, f)) {
91                 if (sscanf(buf, "%*x-%*x %*s %*x %x:%x %*u %*s", &maj, &min)==2)
92                         in_heap = (!maj && !min && !strstr(buf, "---p") && (strstr(buf, "[heap]") || !strchr(buf, '[')));
93                 if (in_heap) {
94                         if (sscanf(buf, "Size: %lu", &l)==1) vm_size += l;
95                         else if (sscanf(buf, "Rss: %lu", &l)==1) vm_rss += l;
96                         else if (sscanf(buf, "Private_Dirty: %lu", &l)==1) vm_priv_dirty += l;
97                 }
98         }
99         if (f) fclose(f);
100         fprintf(stderr, " %7zu virt %7zu res %7zu dirty", vm_size, vm_rss, vm_priv_dirty);
101 }
102
103 void stats() {
104         if (dt/N > 100)
105                 fprintf(stderr, "%10d N %10llu ns/op   ", N, dt/N);
106         else
107                 fprintf(stderr, "%10d N %13.2f ns/op", N, (double)dt/N);
108         if (verbose)
109                 vmstats();
110         fputc('\n', stderr);
111 }
112
113 static void run(const char *name, void (*f)(int)) {
114         int p = fork();
115         if (p) {
116                 int s;
117                 if (p<0 || wait(&s)<0 || !WIFEXITED(s) || WEXITSTATUS(s))
118                         fprintf(stderr, "benchmark %s failed\n", name);
119                 return;
120         }
121         fprintf(stderr, "%-32s", name);
122         for (N=1; ; N=nextN()) {
123                 // TODO: fork at each iteration and pass N,dt..?
124                 reset_timer();
125                 start_timer();
126                 f(N);
127                 stop_timer();
128 //              fprintf(stderr, "%10d%12llu next: %d\n", N, dt, nextN());
129                 if (dt >= SEC/2 || N >= MAXN) {
130                         stats();
131                         exit(0);
132                 }
133                 if (N <= 0) {
134                         fprintf(stderr, "bench: fatal: N <= 0\n");
135                         exit(1);
136                 }
137         }
138 }
139
140 int main() {
141 #define B(t) run(#t, t);
142 #include "tests.h"
143         return 0;
144 }