12 #define B(f) void f(int);
16 static int verbose = 1;
18 void error__(const char *n, int l, const char *s, ...) {
19 dprintf(1, "use error in tests only\n");
23 static unsigned long long start;
24 static unsigned long long dt;
25 //static unsigned long long bytes;
27 #define SEC 1000000000ULL
28 #define MAXN 500000000
31 static unsigned long long tic() {
34 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
35 dprintf(1, "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, "%*x-%*x %*s %*x %x:%x %*u %*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 dprintf(1, " %7zu virt %7zu res %7zu dirty", vm_size, vm_rss, vm_priv_dirty);
106 dprintf(1, "%10d N %10llu ns/op ", N, dt/N);
108 dprintf(1, "%10d N %13.2f ns/op", N, (double)dt/N);
114 static char *pattern;
116 static void run(const char *name, void (*f)(int)) {
119 if (pattern && !strstr(name, pattern))
124 if (p<0 || wait(&s)<0 || !WIFEXITED(s) || WEXITSTATUS(s))
125 dprintf(1, "benchmark %s failed\n", name);
128 dprintf(1, "%-32s", name);
129 for (N=1; ; N=nextN()) {
130 // TODO: fork at each iteration and pass N,dt..?
135 // dprintf(1, "%10d%12llu next: %d\n", N, dt, nextN());
136 if (dt >= SEC/2 || N >= MAXN) {
141 dprintf(1, "bench: fatal: N <= 0\n");
147 static void usage() {
148 fprintf(stderr, "usage: ./t [-vq] [pat]\n");
152 int main(int argc, char *argv[]) {
155 while((c = getopt(argc, argv, "vq")) != -1)
167 pattern = argv[optind];
168 #define B(t) run(#t, t);