diff options
Diffstat (limited to 'usr.bin/kstat/kstat.c')
-rw-r--r-- | usr.bin/kstat/kstat.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/usr.bin/kstat/kstat.c b/usr.bin/kstat/kstat.c index e3b7e6a..f062015 100644 --- a/usr.bin/kstat/kstat.c +++ b/usr.bin/kstat/kstat.c @@ -28,10 +28,51 @@ */ #include <sys/sched.h> +#include <sys/vmstat.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> +#define MIB_PER_GIB 1024 + +static void +print_size_mib(const char *name, size_t mib) +{ + if (name == NULL) { + return; + } + + if (mib >= MIB_PER_GIB) { + printf("%s: %d GiB\n", name, mib / MIB_PER_GIB); + } else { + printf("%s: %d MiB\n", name, mib); + } +} + +static void +get_vm_stat(void) +{ + struct vm_stat vmstat; + int retval, fd; + + fd = open("/ctl/vm/stat", O_RDONLY); + if (fd < 0) { + printf("failed to open '/ctl/vm/stat'\n"); + return; + } + + retval = read(fd, &vmstat, sizeof(vmstat)); + if (retval <= 0) { + printf("failed to read vmstat\n"); + return; + } + + close(fd); + print_size_mib("memory available", vmstat.mem_avail); + print_size_mib("memory used", vmstat.mem_used); + print_size_mib("memory total", vmstat.mem_total); +} + static void get_sched_stat(void) { @@ -76,5 +117,6 @@ int main(void) { get_sched_stat(); + get_vm_stat(); return 0; } |