[hackers] [slstatus] Add ram and swap components on FreeBSD || Michael Buch

From: <git_AT_suckless.org>
Date: Sat, 16 Feb 2019 16:58:39 +0100 (CET)

commit a1ac203d16c379d7fc05765545af2977a7a3584d
Author: Michael Buch <michaelbuch12_AT_gmail.com>
AuthorDate: Sat Feb 16 01:26:44 2019 +0000
Commit: Aaron Marcher <me_AT_drkhsh.at>
CommitDate: Sat Feb 16 16:58:34 2019 +0100

    Add ram and swap components on FreeBSD

diff --git a/components/ram.c b/components/ram.c
index 48144e4..47e6fda 100644
--- a/components/ram.c
+++ b/components/ram.c
_AT_@ -156,4 +156,67 @@
 
                 return NULL;
         }
+#elif defined(__FreeBSD__)
+ #include <sys/sysctl.h>
+ #include <sys/vmmeter.h>
+ #include <unistd.h>
+ #include <vm/vm_param.h>
+
+ const char *
+ ram_free(void) {
+ struct vmtotal vm_stats;
+ int mib[] = {CTL_VM, VM_TOTAL};
+ size_t len;
+
+ len = sizeof(struct vmtotal);
+ if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) == -1
+ || !len)
+ return NULL;
+
+ return fmt_human(vm_stats.t_free * getpagesize(), 1024);
+ }
+
+ const char *
+ ram_total(void) {
+ long npages;
+ size_t len;
+
+ len = sizeof(npages);
+ if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
+ || !len)
+ return NULL;
+
+ return fmt_human(npages * getpagesize(), 1024);
+ }
+
+ const char *
+ ram_perc(void) {
+ long npages;
+ long active;
+ size_t len;
+
+ len = sizeof(npages);
+ if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
+ || !len)
+ return NULL;
+
+ if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
+ || !len)
+ return NULL;
+
+ return bprintf("%d", active * 100 / npages);
+ }
+
+ const char *
+ ram_used(void) {
+ long active;
+ size_t len;
+
+ len = sizeof(active);
+ if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
+ || !len)
+ return NULL;
+
+ return fmt_human(active * getpagesize(), 1024);
+ }
 #endif
diff --git a/components/swap.c b/components/swap.c
index 97428de..2509db1 100644
--- a/components/swap.c
+++ b/components/swap.c
_AT_@ -197,4 +197,88 @@
 
                 return fmt_human(used * 1024, 1024);
         }
+#elif defined(__FreeBSD__)
+ #include <stdlib.h>
+ #include <sys/types.h>
+ #include <fcntl.h>
+ #include <unistd.h>
+ #include <kvm.h>
+
+ static int getswapinfo(struct kvm_swap *swap_info, size_t size)
+ {
+ kvm_t *kd;
+
+ kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL);
+ if(kd == NULL) {
+ warn("kvm_openfiles '/dev/null':");
+ return 0;
+ }
+
+ if(kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) {
+ warn("kvm_getswapinfo:");
+ kvm_close(kd);
+ return 0;
+ }
+
+ kvm_close(kd);
+ return 1;
+ }
+
+ const char *
+ swap_free(void)
+ {
+ struct kvm_swap swap_info[1];
+ long used, total;
+
+ if(!getswapinfo(swap_info, 1))
+ return NULL;
+
+ total = swap_info[0].ksw_total;
+ used = swap_info[0].ksw_used;
+
+ return fmt_human((total - used) * getpagesize(), 1024);
+ }
+
+ const char *
+ swap_perc(void)
+ {
+ struct kvm_swap swap_info[1];
+ long used, total;
+
+ if(!getswapinfo(swap_info, 1))
+ return NULL;
+
+ total = swap_info[0].ksw_total;
+ used = swap_info[0].ksw_used;
+
+ return bprintf("%d", used * 100 / total);
+ }
+
+ const char *
+ swap_total(void)
+ {
+ struct kvm_swap swap_info[1];
+ long total;
+
+ if(!getswapinfo(swap_info, 1))
+ return NULL;
+
+ total = swap_info[0].ksw_total;
+
+ return fmt_human(total * getpagesize(), 1024);
+ }
+
+ const char *
+ swap_used(void)
+ {
+ struct kvm_swap swap_info[1];
+ long used;
+
+ if(!getswapinfo(swap_info, 1))
+ return NULL;
+
+ used = swap_info[0].ksw_used;
+
+ return fmt_human(used * getpagesize(), 1024);
+ }
 #endif
Received on Sat Feb 16 2019 - 16:58:39 CET

This archive was generated by hypermail 2.3.0 : Sat Feb 16 2019 - 17:00:22 CET