#
int	tflg;
int	iflg;
int	aflg;
int	sflg;
struct
{
	char	name[8];
	int	type;
	int	value;
} nl[]
{
	"_dk_busy", 0, 0,
	"_rfintr", 0, 0,
	"_rkintr", 0, 0,
	"_rpintr", 0, 0,
	"_hpintr", 0, 0,
	"_hsintr", 0, 0,
	"\0\0\0\0\0\0\0\0", 0, 0
};
#define IRF	1
#define IRK	2
#define IRP	3
#define IHP	4
#define IHS	5
struct
{
	int	busy;
	long	etime[32];
	long	numb[3];
	long	wds[3];
	long	tin;
	long	tout;
} s, s1;
double	etime;

double	xf[]
{
	16.0,
	11.1,
	7.5,
};
char	*device[]
{
	0,
	0,
	0,
	0
};
char	*blank	"                                              ";
main(argc, argv)
char *argv[];
{
	register mf, i;
	char *cp;
	int iter;
	double f1, f2, f3, f4;
	long t;

	nlist("/unix", nl);
	if(nl[0].type == -1) {
		printf("dk_busy not found in /unix namelist\n");
		exit(1);
	}
	if(nl[IRF].type > 0){
		device[0] = "RF";
		xf[0] = 16.0;
	}
	if(nl[IRP].type > 0){
		device[2] = "RP";
		xf[2] = 7.5;
	}
	if(nl[IRK].type > 0){
		device[1] = "RK";
		xf[1] = 11.1;
	}
	if(nl[IHP].type > 0){
		device[2] = "HP";
		xf[2] = 2.5;
	}
	if(nl[IHS].type > 0){
		device[0] = "HS";
		xf[0] = 4.0;
	}
	mf = open("/dev/mem", 0);
	if(mf < 0) {
		printf("cannot open /dev/mem\n");
		exit(1);
	}
	iter = 0;
	if (argc>1&&argv[1][0]=='-') {
		if (argv[1][1]=='s')
			sflg++;
		else if (argv[1][1]=='a')
			aflg++;
		else if (argv[1][1]=='t')
			tflg++;
		else if (argv[1][1]=='i')
			iflg++;
		argc--;
		argv++;
	}
	if(argc > 2)
		iter = atoi(argv[2]);
	if (!(sflg|iflg)) {
	if(tflg)
		printf("         TTY");
	printf("%9.9s",blank);
	for(i=0;i<3;i++){
		if(device[i] == 0)
			continue;
		printf("%s%16.16s",device[i],blank);
	}
	printf("PERCENT\n");
	if(tflg)
		printf("   tin  tout");
	for(i=0;i<3;i++){
		if(device[i] == 0)
			continue;
		printf("   tpm  msps  mspt");
	}
	printf("  user  nice systm  idle\n");
	}

loop:
	seek(mf, nl[0].value, 0);
	read(mf, &s, sizeof s);
	for(i=0; i<40; i++) {
		t = s.etime[i];
		s.etime[i] =- s1.etime[i];
		s1.etime[i] = t;
	}
	t = 0;
	for(i=0; i<32; i++)
		t =+ s.etime[i];
	etime = t;
	if(etime == 0.)
		etime = 1.;
	if (aflg)
		printf("%.2f minutes total\n", etime/3600);
	if (sflg) {
		stats2(etime);
		goto contin;
	}
	if (iflg) {
		stats3(etime);
		goto contin;
	}
	etime =/ 60.;
	if(tflg) {
		f1 = s.tin;
		f2 = s.tout;
		printf("%6.1f", f1/etime);
		printf("%6.1f", f2/etime);
	}
	for(i=0; i<3; i++){
		if(device[i] == 0)
			continue;
		stats(i);
	}
	for(i=0; i<4; i++){
		stat1(i*8);
	}
	printf("\n");
contin:
	--iter;
	if(iter)
	if(argc > 1) {
		sleep(atoi(argv[1]));
		goto loop;
	}
}


stats(dn)
{
	register i;
	double f1, f2, f3;
	double f4, f5, f6;
	long t;

	t = 0;
	for(i=0; i<32; i++)
		if(i & (1<<dn))
			t =+ s.etime[i];
	f1 = t;
	f1 = f1/60.;
	f2 = s.numb[dn];
	if(f2 == 0.) {
		printf("%6.0f%6.1f%6.1f", 0.0, 0.0, 0.0);
		return;
	}
	f3 = s.wds[dn];
	f3 = f3*32.;
	f4 = xf[dn];
	f4 = f4*1.0e-6;
	f5 = f1 - f4*f3;
	f6 = f1 - f5;
	printf("%6.0f", f2*60./etime);
	printf("%6.1f", f5*1000./f2);
	printf("%6.1f", f6*1000./f2);
}

stat1(o)
{
	register i;
	long t;
	double f1, f2;

	t = 0;
	for(i=0; i<32; i++)
		t =+ s.etime[i];
	f1 = t;
	if(f1 == 0.)
		f1 = 1.;
	t = 0;
	for(i=0; i<8; i++)
		t =+ s.etime[o+i];
	f2 = t;
	printf("%6.2f", f2*100./f1);
}

stats2(t)
double t;
{
	register i, j;

	for (i=0; i<4; i++) {
		for (j=0; j<8; j++)
			printf("%6.2f\n", s.etime[8*i+j]/(t/100));
		printf("\n");
	}
}

stats3(t)
double t;
{
	register i, j;
	double sum;

	t =/ 100;
	printf("%6.2f idle\n", s.etime[24]/t);
	sum = 0;
	for (i=0; i<8; i++)
		sum =+ s.etime[i];
	printf("%6.2f user\n", sum/t);
	sum = 0;
	for (i=0; i<8; i++)
		sum =+ s.etime[8+i];
	printf("%6.2f nice\n", sum/t);
	sum = 0;
	for (i=0; i<8; i++)
		sum =+ s.etime[16+i];
	printf("%6.2f system\n", sum/t);
	sum = 0;
	for (i=1; i<8; i++)
		sum =+ s.etime[24+i];
	printf("%6.2f IO wait\n", sum/t);
	sum = 0;
	for (i=1; i<8; i++)
		sum =+ s.etime[i]+s.etime[i+8]+s.etime[i+16]+s.etime[i+24];
	printf("%6.2f IO active\n", sum/t);
	sum = 0;
	for (i=0; i<32; i++)
		if (i&01)
			sum =+ s.etime[i];
	if(device[0])
		printf("%6.2f %s active\n", sum/t,device[0]);
	sum = 0;
	for (i=0; i<32; i++)
		if (i&02)
			sum =+ s.etime[i];
	if(device[1])
		printf("%6.2f %s active\n", sum/t,device[1]);
	sum = 0;
	for (i=0; i<32; i++)
		if (i&04)
			sum =+ s.etime[i];
	if(device[2])
		printf("%6.2f %s active\n", sum/t,device[2]);
}
