build system updates, port libc-testsuit changes
[libc-test] / common / b.c
index b82531c..6fa1963 100644 (file)
@@ -1,4 +1,3 @@
-#define _POSIX_C_SOURCE 200809L
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -9,9 +8,9 @@
 #include <unistd.h>
 #include "test.h"
 
-#define T(t)
-#define B(t) void t();
-#include "main.h"
+#define T(f)
+#define B(f) void f(int);
+#include "tests.h"
 #undef B
 
 static int verbose = 1;
@@ -20,7 +19,7 @@ void error__(const char *n, int l, const char *s, ...) {
        fprintf(stderr, "use error in tests only\n");
 }
 
-int N;
+static int N;
 static unsigned long long start;
 static unsigned long long dt;
 //static unsigned long long bytes;
@@ -31,7 +30,7 @@ static unsigned long long dt;
 static unsigned long long tic() {
        struct timespec ts;
 
-       if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
+       if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
                fprintf(stderr, "bench: clock_gettime failed: %s\n", strerror(errno));
                return 0;
        }
@@ -57,6 +56,7 @@ void reset_timer() {
 
 static int nextN() {
        unsigned long long n = dt/N;
+       unsigned long long i;
 
        if (n)
                n = SEC/2/n;
@@ -69,7 +69,14 @@ static int nextN() {
                n = N+1;
        if (n > MAXN)
                n = MAXN;
-       return n;
+
+       /* round up to a nice number */
+       for (i = 1; i < n; i *= 10);
+       if (i/2 >= n)
+               i /= 2;
+       if (i/2 >= n)
+               i /= 2;
+       return i;
 }
 
 void vmstats() {
@@ -81,7 +88,7 @@ void vmstats() {
 
        f = fopen("/proc/self/smaps", "rb");
        if (f) while (fgets(buf, sizeof buf, f)) {
-               if (sscanf(buf, "%*lx-%*lx %*s %*lx %x:%x %*lu %*s", &maj, &min)==2)
+               if (sscanf(buf, "%*x-%*x %*s %*x %x:%x %*u %*s", &maj, &min)==2)
                        in_heap = (!maj && !min && !strstr(buf, "---p") && (strstr(buf, "[heap]") || !strchr(buf, '[')));
                if (in_heap) {
                        if (sscanf(buf, "Size: %lu", &l)==1) vm_size += l;
@@ -103,7 +110,7 @@ void stats() {
        fputc('\n', stderr);
 }
 
-static void run(const char *name, void (*f)()) {
+static void run(const char *name, void (*f)(int)) {
        int p = fork();
        if (p) {
                int s;
@@ -113,9 +120,10 @@ static void run(const char *name, void (*f)()) {
        }
        fprintf(stderr, "%-32s", name);
        for (N=1; ; N=nextN()) {
+               // TODO: fork at each iteration and pass N,dt..?
                reset_timer();
                start_timer();
-               f();
+               f(N);
                stop_timer();
 //             fprintf(stderr, "%10d%12llu next: %d\n", N, dt, nextN());
                if (dt >= SEC/2 || N >= MAXN) {
@@ -131,6 +139,6 @@ static void run(const char *name, void (*f)()) {
 
 int main() {
 #define B(t) run(#t, t);
-#include "main.h"
+#include "tests.h"
        return 0;
 }