ref: d818e0fc9c7b36b6cc1494bb2253792ea2c35f4b
dir: /rdwr.c/
#include <u.h>
#include <libc.h>
#include "bench.h"
#include "fns.h"
void
benchsysr1(B *b)
{
int i;
extern int sysr1(void);
for(i = 0; i < b->N; i++)
sysr1();
}
void
benchread(B *b, char *path, int n)
{
int i, fd;
char buf[128*IOUNIT];
if((fd = open(path, OREAD)) == -1)
sysfatal("open %s: %r", path);
for(i = 0; i < b->N; i++)
pread(fd, buf, n, 0);
close(fd);
}
void
benchwrite(B *b, char *path, int n)
{
int i, fd;
char buf[128*IOUNIT];
if((fd = open(path, OWRITE)) == -1)
sysfatal("open %s: %r", path);
for(i = 0; i < b->N; i++)
pwrite(fd, buf, n, 0);
close(fd);
}
void
benchpipe(B *b, int n)
{
char buf[128*IOUNIT];
int i, pfd[2];
if(pipe(pfd) == -1)
sysfatal("pipe: %r");
switch(rfork(RFPROC)){
case -1:
sysfatal("fork: %r");
case 0:
for(i = 0; i < b->N; i++)
if(write(pfd[0], buf, n) == -1)
sysfatal("write: %r");
exits(nil);
break;
default:
for(i = 0; i < b->N; i++)
if(read(pfd[1], buf, n) == -1)
sysfatal("read: %r");
free(wait());
close(pfd[0]);
close(pfd[1]);
break;
}
}
void benchreadzero(B *b) { benchread(b, "/dev/zero", 4); }
void benchwritenull(B *b) { benchwrite(b, "/dev/null", 4); }
void benchreadmordor(B *b) { benchread(b, "/dev/mordor", 4); }
void benchwritemordor(B *b) { benchwrite(b, "/dev/mordor", 4); }
void benchpipe1(B *b) { benchpipe(b, 1); }
void benchpipe16(B *b) { benchpipe(b, 16); }
void benchpipe256(B *b) { benchpipe(b, 256); }
void benchpipe4096(B *b) { benchpipe(b, 4096); }
void benchpipe4097(B *b) { benchpipe(b, 4097); }
void benchpipe32768(B *b) { benchpipe(b, 32768); }