X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/c12b9cc50b5f476cd7b1de1dbd49e6282b1a042f..refs/heads/master:/components/ram.c diff --git a/components/ram.c b/components/ram.c index 7c0870c..47e6fda 100644 --- a/components/ram.c +++ b/components/ram.c @@ -4,69 +4,77 @@ #include "../util.h" #if defined(__linux__) + #include + const char * ram_free(void) { - long free; + uintmax_t free; if (pscanf("/proc/meminfo", - "MemTotal: %ld kB\n" - "MemFree: %ld kB\n" - "MemAvailable: %ld kB\n", + "MemTotal: %ju kB\n" + "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n", &free, &free, &free) != 3) { return NULL; } - return fmt_human_2(free * 1024, "B"); + return fmt_human(free * 1024, 1024); } const char * ram_perc(void) { - long total, free, buffers, cached; + uintmax_t total, free, buffers, cached; if (pscanf("/proc/meminfo", - "MemTotal: %ld kB\n" - "MemFree: %ld kB\n" - "MemAvailable: %ld kB\nBuffers: %ld kB\n" - "Cached: %ld kB\n", + "MemTotal: %ju kB\n" + "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n" + "Buffers: %ju kB\n" + "Cached: %ju kB\n", &total, &free, &buffers, &buffers, &cached) != 5) { return NULL; } - return bprintf("%d%%", 100 * ((total - free) - - (buffers + cached)) / total); + if (total == 0) { + return NULL; + } + + return bprintf("%d", 100 * ((total - free) - (buffers + cached)) + / total); } const char * ram_total(void) { - long total; + uintmax_t total; - if (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", - &total) != 1) { + if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total) + != 1) { return NULL; } - return fmt_human_2(total * 1024, "B"); + return fmt_human(total * 1024, 1024); } const char * ram_used(void) { - long total, free, buffers, cached; + uintmax_t total, free, buffers, cached; if (pscanf("/proc/meminfo", - "MemTotal: %ld kB\n" - "MemFree: %ld kB\n" - "MemAvailable: %ld kB\nBuffers: %ld kB\n" - "Cached: %ld kB\n", + "MemTotal: %ju kB\n" + "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n" + "Buffers: %ju kB\n" + "Cached: %ju kB\n", &total, &free, &buffers, &buffers, &cached) != 5) { return NULL; } - return fmt_human_2((total - free - buffers - cached) * 1024, - "B"); + return fmt_human((total - free - buffers - cached) * 1024, + 1024); } #elif defined(__OpenBSD__) #include @@ -74,8 +82,8 @@ #include #include - #define LOG1024 10 - #define pagetok(size, pageshift) ((size) << (pageshift - LOG1024)) + #define LOG1024 10 + #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024)) inline int load_uvmexp(struct uvmexp *uvmexp) @@ -85,7 +93,11 @@ size = sizeof(*uvmexp); - return sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0 ? 1 : 0; + if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) { + return 1; + } + + return 0; } const char * @@ -96,7 +108,8 @@ if (load_uvmexp(&uvmexp)) { free_pages = uvmexp.npages - uvmexp.active; - return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B"); + return fmt_human(pagetok(free_pages, uvmexp.pageshift) * + 1024, 1024); } return NULL; @@ -110,7 +123,7 @@ if (load_uvmexp(&uvmexp)) { percent = uvmexp.active * 100 / uvmexp.npages; - return bprintf("%d%%", percent); + return bprintf("%d", percent); } return NULL; @@ -122,7 +135,9 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B"); + return fmt_human(pagetok(uvmexp.npages, + uvmexp.pageshift) * 1024, + 1024); } return NULL; @@ -134,9 +149,74 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B"); + return fmt_human(pagetok(uvmexp.active, + uvmexp.pageshift) * 1024, + 1024); } return NULL; } +#elif defined(__FreeBSD__) + #include + #include + #include + #include + + 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