// The code from experts-exchange.com // http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_10154679.html#1 // To be compiled with gcc -o kstat kstat.c -lkstat #include #include #include #define BYTES_PER_PAGE 8192 #define KBYTE 1024 #define MUL (BYTES_PER_PAGE/KBYTE) void print_chain(kstat_ctl_t *kc) { kstat_t *ksp; for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) { printf("Module: %s Class: %s Name: %s\n", ksp->ks_module, ksp->ks_class, ksp->ks_name); } } void main(int argc, char*argv[]) { kstat_ctl_t *kc; kstat_t *ksp; int first=1; vminfo_t vmlast,vm; cpu_stat_t statlast, cpu_stat; kc = kstat_open(); print_chain(kc); while(1) { ksp = kstat_lookup(kc, "unix", 0, "vminfo"); if (ksp) { kstat_read(kc, ksp, &vm); if (!first) { printf("---------------------------------\n"); printf("freemem =%8llu KByte\n",(vm.freemem-vmlast.freemem)*MUL); printf("reserved swap =%8llu KByte\n",(vm.swap_resv-vmlast.swap_resv)*MUL); printf("allocated swap=%8llu KByte\n",(vm.swap_alloc-vmlast.swap_alloc)*MUL); printf("avail swap =%8llu KByte\n",(vm.swap_avail-vmlast.swap_avail)*MUL); printf("free swap =%8llu KByte\n",(vm.swap_free-vmlast.swap_free)*MUL); } vmlast=vm; } ksp = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0"); if (ksp) { kstat_read(kc, ksp, &cpu_stat); if (!first) { printf("stat:syscall =%8lu\n", cpu_stat.cpu_sysinfo.syscall-statlast.cpu_sysinfo.syscall); } statlast=cpu_stat; } first=0; sleep(1); kstat_chain_update(kc); } kstat_close(kc); exit(0); }