ref: 03bf5f5359c5a4919b97bc5f7843341f4a43f42d
parent: a7f13757d67c3169a938c95d2db667d1bd4ee407
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Nov 1 10:54:33 EST 2020
change benchmarks, grouping
--- a/b.c
+++ b/b.c
@@ -54,7 +54,7 @@
return fmtprint(f, "%zd", c.ns);
}
-void
+B *
benchinit(B *b, char *name)
{
Res r;
@@ -84,8 +84,57 @@
}
adj = v / i;
}
+
+ return b;
}
+B *
+benchadd(Bgr *gr, char *name)
+{
+ B **gb, *b;
+
+ if((gb = realloc(gr->b, (gr->nb+1)*sizeof(b))) == nil)
+ return nil;
+ gr->b = gb;
+ if((b = malloc(sizeof(*b)+strlen(name)+1)) == nil)
+ return nil;
+ gr->b[gr->nb++] = b;
+
+ return benchinit(b, strcpy((char*)(b+1), name));
+}
+
+Bgr *
+benchinitgr(Bgr *gr, char *name)
+{
+ gr->name = name;
+ gr->b = nil;
+ gr->nb = 0;
+
+ return gr;
+}
+
+void
+benchprintgr(Bgr *gr, int fd)
+{
+ fprint(fd, "%s\n", gr->name);
+ benchprint(gr->b, gr->nb, fd);
+}
+
+void
+benchfreegr(Bgr *gr)
+{
+ int i;
+
+ for(i = 0; i < gr->nb; i++)
+ free(gr->b[i]);
+}
+
+void
+bseparator(int fd)
+{
+ fprint(fd, "\n");
+}
+
static int
ccmp(void *a, void *b)
{
@@ -124,6 +173,9 @@
{
uvlong c;
+ if(b->n < 1)
+ return;
+
if(b->step == 0)
b->t0 = b->tin;
b->step++;
@@ -156,7 +208,7 @@
}
void
-benchprint(B *b, int nb, int fd)
+benchprint(B **b, int nb, int fd)
{
static char *header[] = {" ", "op/s", "98%", "96%", "75%", "med", "avg", "min", "max"};
static int w[] = {16, 8, 8, 8, 8, 8, 8, 8, 8};
@@ -175,10 +227,10 @@
int i, j, n, x;
for(i = 0; i < nb; i++){
- w[0] = MAX(w[0], snprint(t, sizeof(t), "%s ", b[i].name ? b[i].name : ""));
- w[1] = MAX(w[1], snprint(t, sizeof(t), "%σ ", &b[i]));
+ w[0] = MAX(w[0], snprint(t, sizeof(t), "%s ", b[i]->name ? b[i]->name : ""));
+ w[1] = MAX(w[1], snprint(t, sizeof(t), "%σ ", b[i]));
for(j = 2; j < nelem(header); j++)
- w[j] = MAX(w[j], snprint(t, sizeof(t), "%τ ", *(Cns*)((char*)(b+i) + off[j])));
+ w[j] = MAX(w[j], snprint(t, sizeof(t), "%τ ", *(Cns*)((char*)b[i] + off[j])));
}
for(j = n = 0; j < nelem(header); j++)
@@ -195,16 +247,33 @@
for(i = 0; i < nb; i++){
memset(s, ' ', n);
p = s;
- p[sprint(p, "%s ", b[i].name)] = ' ';
+ p[sprint(p, "%s ", b[i]->name)] = ' ';
p += w[0];
- x = snprint(t, sizeof(t), "%σ ", b+i);
+ x = snprint(t, sizeof(t), "%σ ", b[i]);
memmove(p+w[1]-x, t, x);
p += w[1];
for(j = 2; j < nelem(header); p += w[j], j++){
- x = snprint(t, sizeof(t), "%τ ", *(Cns*)((char*)(b+i) + off[j]));
+ x = snprint(t, sizeof(t), "%τ ", *(Cns*)((char*)b[i] + off[j]));
memmove(p+w[j]-x, t, x);
}
*p = 0;
fprint(fd, "%s\n", s);
}
+}
+
+int
+benchwire(int cpu)
+{
+ char t[64];
+ int r, f;
+
+ r = -1;
+ snprint(t, sizeof(t), "/proc/%d/ctl", getpid());
+ if((f = open(t, OWRITE)) >= 0){
+ if(fprint(f, "wired %d\n", cpu) >= 8)
+ r = 0;
+ close(f);
+ }
+
+ return r;
}
--- a/b.h
+++ b/b.h
@@ -1,6 +1,13 @@
typedef struct B B;
+typedef struct Bgr Bgr;
typedef struct Cns Cns;
+enum {
+ Kb = 1024ULL,
+ Mb = 1024ULL*1024ULL,
+ Gb = 1024ULL*1024ULL*1024ULL,
+};
+
struct Cns {
uvlong c;
uvlong ns;
@@ -22,6 +29,12 @@
char *name;
};
+struct Bgr {
+ char *name;
+ B **b;
+ int nb;
+};
+
/* private */
void benchstep(B *b);
void tstart(uvlong *c);
@@ -28,7 +41,15 @@
extern void (*tend)(uvlong *c);
/* public */
-void benchinit(B *b, char *name);
-void benchprint(B *b, int nb, int fd);
+int benchwire(int cpu);
+
+Bgr *benchinitgr(Bgr *gr, char *name);
+B *benchadd(Bgr *gr, char *name);
#define benchin(b) do{ tstart(&(b)->tin); }while(0)
#define benchout(b) do{ tend(&(b)->tout); benchstep(b); }while(0)
+void benchprintgr(Bgr *gr, int fd);
+void benchfreegr(Bgr *gr);
+void bseparator(int fd);
+
+B *benchinit(B *b, char *name);
+void benchprint(B **b, int nb, int fd);
--- a/main.c
+++ b/main.c
@@ -4,109 +4,113 @@
#include "b.h"
static void
-proc(void *)
+bmallocfree(int out)
{
- threadexits(nil);
-}
+ Bgr g;
+ B *bm, *bf;
+ int sz[] = {
+ {0},
+ {1},
+ {16},
+ {64},
+ {256},
+ {1*Kb},
+ {4*Kb},
+ {64*Kb},
+ {1*Mb},
+ {2*Mb},
+ {4*Mb},
+ };
+ int i;
+ void *ptr;
+ char t[32];
-void
-threadmain(int argc, char **argv)
-{
- B *group, *b;
- uchar d[1];
- char t[64];
- int f;
+ benchinitgr(&g, "malloc & free");
+ for(i = 0; i < nelem(sz); i++){
+ snprint(t, sizeof(t), "malloc/%d", sz[i]);
+ bm = benchadd(&g, t);
+ snprint(t, sizeof(t), "free/%d", sz[i]);
+ bf = benchadd(&g, t);
- ARGBEGIN{
- }ARGEND
+ for(; bm->n > 0 || bf->n > 0;){
+ benchin(bm);
+ ptr = malloc(sz[i]);
+ benchout(bm);
- snprint(t, sizeof(t), "/proc/%d/ctl", getpid());
- if((f = open(t, OWRITE)) < 0)
- fprint(2, "%r\n");
- else{
- if(write(f, "wired 0\n", 8) != 8)
- fprint(2, "failed to wire\n");
- close(f);
+ benchin(bf);
+ free(ptr);
+ benchout(bf);
+ }
}
- b = group = malloc(32 * sizeof(*b));
+ benchprintgr(&g, out);
+ benchfreegr(&g);
+}
- f = open("/dev/null", OREAD);
- benchinit(b, "read/1");
- while(b->n > 0){
- benchin(b);
- read(f, d, 1);
- benchout(b);
- }
+static void
+bzeronull(int out)
+{
+ Bgr g;
+ B *br, *bw;
+ int sz[] = {
+ {0},
+ {1},
+ {64*Kb},
+ {1*Mb},
+ {2*Mb},
+ {4*Mb},
+ {16*Mb},
+ {64*Mb},
+ };
+ int i, zero, null;
+ void *ptr;
+ char t[32];
- b++;
- benchinit(b, "read/0");
- while(b->n > 0){
- benchin(b);
- read(f, d, 0);
- benchout(b);
- }
- close(f);
+ ptr = malloc(sz[nelem(sz)-1]);
+ if((zero = open("/dev/zero", OREAD)) < 0)
+ sysfatal("%r");
+ if((null = open("/dev/null", OWRITE)) < 0)
+ sysfatal("%r");
+ benchinitgr(&g, "/dev/zero → /dev/null");
+ for(i = 0; i < nelem(sz); i++){
+ snprint(t, sizeof(t), "read/%d", sz[i]);
+ br = benchadd(&g, t);
+ snprint(t, sizeof(t), "write/%d", sz[i]);
+ bw = benchadd(&g, t);
- b++;
- benchinit(b, "sin(n)");
- while(b->n > 0){
- benchin(b);
- sin(b->n);
- benchout(b);
- }
+ for(; br->n > 0 || bw->n > 0;){
+ benchin(br);
+ if(readn(zero, ptr, sz[i]) != sz[i])
+ sysfatal("/dev/zero: readn");
+ benchout(br);
- b++;
- benchinit(b, "cos(n)");
- while(b->n > 0){
- benchin(b);
- cos(b->n);
- benchout(b);
+ benchin(bw);
+ if(write(null, ptr, sz[i]) != sz[i])
+ sysfatal("/dev/null: write");
+ benchout(bw);
+ }
}
- b++;
- benchinit(b, "tan(n)");
- while(b->n > 0){
- benchin(b);
- tan(b->n);
- benchout(b);
- }
+ benchprintgr(&g, out);
+ benchfreegr(&g);
- b++;
- benchinit(b, "sleep(0)");
- while(b->n > 0){
- benchin(b);
- sleep(0);
- benchout(b);
- }
+ close(zero);
+ close(null);
+ free(ptr);
+}
- b++;
- benchinit(b, "sleep(5)");
- while(b->n > 0){
- benchin(b);
- sleep(5);
- benchout(b);
- }
+void
+threadmain(int argc, char **argv)
+{
+ ARGBEGIN{
+ }ARGEND
- b++;
- benchinit(b, "sleep(10)");
- while(b->n > 0){
- benchin(b);
- sleep(10);
- benchout(b);
- }
+ if(benchwire(0) != 0)
+ fprint(2, "failed to wire: %r\n");
- b++;
- benchinit(b, "proccreate");
- while(b->n > 0){
- benchin(b);
- proccreate(proc, (void*)b->n, 1024);
- benchout(b);
- }
-
- b++;
-
- benchprint(group, b-group, 1);
+ bmallocfree(1);
+ bseparator(1);
+ bzeronull(1);
threadexitsall(nil);
}