readme update
[libc-test] / common / b.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include "test.h"
11
12 #define T(t)
13 #define B(t) void t();
14 #include "main.h"
15 #undef B
16
17 static int verbose = 1;
18
19 void error__(const char *n, int l, const char *s, ...) {
20         fprintf(stderr, "use error in tests only\n");
21 }
22
23 int N;
24 static unsigned long long start;
25 static unsigned long long dt;
26 //static unsigned long long bytes;
27
28 #define SEC  1000000000ULL
29 #define MAXN  500000000
30
31 static unsigned long long tic() {
32         struct timespec ts;
33
34         if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
35                 fprintf(stderr, "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
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         return n;
73 }
74
75 void vmstats() {
76         FILE *f;
77         char buf[256];
78         int maj, min, in_heap=0;
79         unsigned long l;
80         size_t vm_size=0, vm_rss=0, vm_priv_dirty=0;
81
82         f = fopen("/proc/self/smaps", "rb");
83         if (f) while (fgets(buf, sizeof buf, f)) {
84                 if (sscanf(buf, "%*lx-%*lx %*s %*lx %x:%x %*lu %*s", &maj, &min)==2)
85                         in_heap = (!maj && !min && !strstr(buf, "---p") && (strstr(buf, "[heap]") || !strchr(buf, '[')));
86                 if (in_heap) {
87                         if (sscanf(buf, "Size: %lu", &l)==1) vm_size += l;
88                         else if (sscanf(buf, "Rss: %lu", &l)==1) vm_rss += l;
89                         else if (sscanf(buf, "Private_Dirty: %lu", &l)==1) vm_priv_dirty += l;
90                 }
91         }
92         if (f) fclose(f);
93         fprintf(stderr, " %7zu virt %7zu res %7zu dirty", vm_size, vm_rss, vm_priv_dirty);
94 }
95
96 void stats() {
97         if (dt/N > 100)
98                 fprintf(stderr, "%10d N %10llu ns/op   ", N, dt/N);
99         else
100                 fprintf(stderr, "%10d N %13.2f ns/op", N, (double)dt/N);
101         if (verbose)
102                 vmstats();
103         fputc('\n', stderr);
104 }
105
106 static void run(const char *name, void (*f)()) {
107         int p = fork();
108         if (p) {
109                 int s;
110                 if (p<0 || wait(&s)<0 || !WIFEXITED(s) || WEXITSTATUS(s))
111                         fprintf(stderr, "benchmark %s failed\n", name);
112                 return;
113         }
114         fprintf(stderr, "%-32s", name);
115         for (N=1; ; N=nextN()) {
116                 reset_timer();
117                 start_timer();
118                 f();
119                 stop_timer();
120 //              fprintf(stderr, "%10d%12llu next: %d\n", N, dt, nextN());
121                 if (dt >= SEC/2 || N >= MAXN) {
122                         stats();
123                         exit(0);
124                 }
125                 if (N <= 0) {
126                         fprintf(stderr, "bench: fatal: N <= 0\n");
127                         exit(1);
128                 }
129         }
130 }
131
132 int main() {
133 #define B(t) run(#t, t);
134 #include "main.h"
135         return 0;
136 }