Sigrid Solveig Haflínudóttirchange read/write sizes42e3ee26 years agoHistory
116 lines · 1.7 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <thread.h>#include "b.h" static voidbmallocfree(int out){	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]; 	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); 		for(; bm->n > 0 || bf->n > 0;){			benchin(bm);			ptr = malloc(sz[i]);			benchout(bm); 			benchin(bf);			free(ptr);			benchout(bf);		}	} 	benchprintgr(&g, out);	benchfreegr(&g);} static voidbzeronull(int out){	Bgr g;	B *br, *bw;	int sz[] = {		{0},		{1},		{64*Kb},		{1*Mb},		{4*Mb},		{16*Mb},		{64*Mb},		{256*Mb},	};	int i, zero, null;	void *ptr;	char t[32]; 	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); 		for(; br->n > 0 || bw->n > 0;){			benchin(br);			if(readn(zero, ptr, sz[i]) != sz[i])				sysfatal("/dev/zero: readn");			benchout(br); 			benchin(bw);			if(write(null, ptr, sz[i]) != sz[i])				sysfatal("/dev/null: write");			benchout(bw);		}	} 	benchprintgr(&g, out);	benchfreegr(&g); 	close(zero);	close(null);	free(ptr);} voidthreadmain(int argc, char **argv){	ARGBEGIN{	}ARGEND 	if(benchwire(0) != 0)		fprint(2, "failed to wire: %r\n"); 	bmallocfree(1);	bseparator(1);	bzeronull(1); 	threadexitsall(nil);}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t