plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

OpenBSD.c (3276B)


      1 #include <u.h>
      2 #include <sys/types.h>
      3 #include <sys/param.h>
      4 #include <sys/sched.h>
      5 #include <sys/socket.h>
      6 #include <ifaddrs.h>
      7 #include <sys/sysctl.h>
      8 #include <sys/time.h>
      9 #include <net/if.h>
     10 #include <machine/apmvar.h>
     11 #include <sys/ioctl.h>
     12 #include <uvm/uvm_param.h>
     13 #include <uvm/uvm_extern.h>
     14 #include <limits.h>
     15 #include <libc.h>
     16 #include <bio.h>
     17 #include "dat.h"
     18 
     19 void xapm(int);
     20 void xloadavg(int);
     21 void xcpu(int);
     22 void xswap(int);
     23 void xsysctl(int);
     24 void xnet(int);
     25 
     26 void (*statfn[])(int) =
     27 {
     28 	xapm,
     29 	xloadavg,
     30 	xcpu,
     31 	xsysctl,
     32 	xnet,
     33 	0
     34 };
     35 
     36 void
     37 xloadavg(int first)
     38 {
     39 	double l[3];
     40 
     41 	if(first)
     42 		return;
     43 
     44 	if(getloadavg(l, 3) < 0)
     45 		return;
     46 	Bprint(&bout, "load =%d 1000\n", (int)(l[0]*1000.0));
     47 }
     48 
     49 void
     50 xapm(int first)
     51 {
     52 	static int fd;
     53 	struct apm_power_info ai;
     54 
     55 	if(first){
     56 		fd = open("/dev/apm", OREAD);
     57 		return;
     58 	}
     59 
     60 	if(ioctl(fd, APM_IOC_GETPOWER, &ai) < 0)
     61 		return;
     62 
     63 	if(ai.battery_life <= 100)
     64 		Bprint(&bout, "battery =%d 100\n", ai.battery_life);
     65 }
     66 
     67 void
     68 xnet(int first)
     69 {
     70 	ulong out, in, outb, inb, err;
     71 	struct ifaddrs *ifa, *ifap;
     72 	struct if_data *ifd = NULL;
     73 
     74 	if (first)
     75 		return;
     76 
     77 	out = in = outb = inb = err = 0;
     78 
     79 	if (getifaddrs(&ifap) == -1)
     80 		return;
     81 
     82 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
     83 		if (ifa->ifa_addr == NULL ||
     84 		    ifa->ifa_addr->sa_family != AF_LINK)
     85 			continue;
     86 
     87 		ifd = ifa->ifa_data;
     88 
     89 		if (ifd != NULL) {
     90 			out += ifd->ifi_opackets;
     91 			in += ifd->ifi_ipackets;
     92 			outb += ifd->ifi_obytes;
     93 			inb += ifd->ifi_ibytes;
     94 			err += ifd->ifi_ierrors;
     95 		}
     96 	}
     97 
     98 	Bprint(&bout, "etherin %lud 1000\n", in);
     99 	Bprint(&bout, "etherout %lud 1000\n", out);
    100 	Bprint(&bout, "etherinb %lud 1000000\n", inb);
    101 	Bprint(&bout, "etheroutb %lud 1000000\n", outb);
    102 	Bprint(&bout, "ethererr %lud 1000\n", err);
    103 	Bprint(&bout, "ether %lud 1000\n", in+out);
    104 	Bprint(&bout, "etherb %lud 1000000\n", inb+outb);
    105 
    106 	freeifaddrs(ifap);
    107 }
    108 
    109 void
    110 xcpu(int first)
    111 {
    112 	static int stathz;
    113 	ulong x[20];
    114 	struct clockinfo *ci;
    115 	int mib[2];
    116 	size_t l;
    117 
    118 	if(first){
    119 		mib[0] = CTL_KERN;
    120 		mib[1] = KERN_CLOCKRATE;
    121 		l = sizeof(x);
    122 		sysctl(mib, 2, (char *)&x, &l, nil, 0);
    123 		x[l] = 0;
    124 		if (l < sizeof(ci))
    125 			stathz = 128;
    126 		else{
    127 			ci = (struct clockinfo*)x;
    128 			stathz = ci->stathz;
    129 		}
    130 		return;
    131 	}
    132 
    133 	mib[0] = CTL_KERN;
    134 	mib[1] = KERN_CPTIME;
    135 	l = sizeof(x);
    136 	sysctl(mib, 2, (char *)&x, &l, nil, 0);
    137 	if (l < 5*sizeof(ulong))
    138 		return;
    139 	x[l] = 0;
    140 
    141 	Bprint(&bout, "user %lud %d\n", x[CP_USER]+x[CP_NICE], stathz);
    142 	Bprint(&bout, "sys %lud %d\n", x[CP_SYS], stathz);
    143 	Bprint(&bout, "cpu %lud %d\n", x[CP_USER]+x[CP_NICE]+x[CP_SYS], stathz);
    144 	Bprint(&bout, "idle %lud %d\n", x[CP_IDLE], stathz);
    145 }
    146 
    147 void
    148 xsysctl(int first)
    149 {
    150 	struct uvmexp vm;
    151 	static int pgsize;
    152 	int mib[2];
    153 	size_t l;
    154 
    155 	l = sizeof(vm);
    156 	mib[0] = CTL_VM;
    157 	mib[1] = VM_UVMEXP;
    158 	sysctl(mib, 2, &vm, &l, nil, 0);
    159 	if (l < sizeof(vm))
    160 		return;
    161 
    162 	if (first)
    163 		pgsize = vm.pagesize;
    164 
    165 	Bprint(&bout, "mem =%lud %lud\n", vm.active*pgsize, vm.npages*pgsize);
    166 	Bprint(&bout, "context %lud 1000\n", vm.swtch);
    167 	Bprint(&bout, "syscall %lud 1000\n", vm.syscalls);
    168 	Bprint(&bout, "intr %lud 1000\n", vm.intrs+vm.traps);
    169 	Bprint(&bout, "fault %lud 1000\n", vm.faults);
    170 
    171 	Bprint(&bout, "fork %ud 1000\n", vm.forks);
    172 	Bprint(&bout, "swap =%lud %lud\n", vm.swpginuse*pgsize, vm.swpages*pgsize);
    173 }