#include <u.h>
#include <libc.h>
#include <bio.h>
vlong rdtsc(void);
int
ncpu(void)
{
char buf[4096];
int fd, i, n, nl;
if((fd = open("#c/sysstat", OREAD)) < 0)
sysfatal("open #c/sysstat: %r");
n = read(fd, buf, sizeof buf);
close(fd);
nl = 0;
for(i=0; i<n; i++)
if(buf[i] == '\n')
nl++;
return nl;
}
int
pctl(char *fmt, ...)
{
static int fd = -1;
char buf[128];
int n;
va_list arg;
if(fd < 0){
snprint(buf, sizeof buf, "#p/%d/ctl", getpid());
if((fd = open(buf, OWRITE)) < 0)
sysfatal("open %s: %r", buf);
}
va_start(arg, fmt);
n = vsnprint(buf, sizeof buf, fmt, arg);
va_end(arg);
return write(fd, buf, n);
}
vlong tsc0;
vlong tsc;
Biobuf bout;
void
main(void)
{
int i, n;
Binit(&bout, OWRITE, 1);
n = ncpu();
print("%d cpus\n", n);
if(n <= 0)
exits("no cpus");
for(i=0; i<8*n; i++){
if(pctl("wired %d", i%n) < 0)
sysfatal("pctl: %r");
if(rfork(RFMEM|RFPROC) == 0){
tsc = rdtsc();
_exits(0);
}
waitpid();
if(i == 0)
tsc0 = tsc;
Bprint(&bout, "cpu%d: %lld %lld\n", i%n, tsc, tsc - tsc0);
tsc0 = tsc;
}
Bflush(&bout);
exits(0);
}
|