FreeBSD は sysctl でいろいろわかる。top のソース (machine.c) とか読めば簡単。
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <stdio.h>
#include <unistd.h>
#define UNIT (1024*1024)
int getsysctl(char* name){
int mib[4];
size_t len = 4;
int size;
sysctlnametomib(name, mib, &len);
if (sysctl(mib, 4, &size, &len, NULL, 0) != -1)
return size;
return 0;
}
int main(){
int pagesize = getpagesize();
int pa, pi, pw, pc, pf;
pa = getsysctl("vm.stats.vm.v_active_count");
pi = getsysctl("vm.stats.vm.v_inactive_count");
pw = getsysctl("vm.stats.vm.v_wire_count");
pc = getsysctl("vm.stats.vm.v_cache_count");
pf = getsysctl("vm.stats.vm.v_free_count");
printf("active: %d\n", pa * pagesize / UNIT);
printf("inactive: %d\n", pi * pagesize / UNIT);
printf("wired: %d\n", pw * pagesize / UNIT);
printf("cache: %d\n", pc * pagesize / UNIT);
printf("free: %d\n", pf * pagesize / UNIT);
return 0;
}
FreeBSD 4.11-RELEASE では
#include <sys/resource.h>
の前に
#include <sys/time.h>
が必要でした。
ありがとうございます。
世の中だんだん、微妙に必要なヘッダファイルとかが変わりますよね…