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