X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/49d1e5fae2e4957abcf0f1056b3e8df8d695094c..3f45a5f7caf37ec04bbc3aea5e7def1452d1118c:/components/ram.c diff --git a/components/ram.c b/components/ram.c index f451601..1c12aab 100644 --- a/components/ram.c +++ b/components/ram.c @@ -14,7 +14,7 @@ "MemFree: %ld kB\n" "MemAvailable: %ld kB\n", &free, &free, &free) == 3) ? - bprintf("%f", (float)free / 1024 / 1024) : NULL; + fmt_human_2(free * 1024, "B") : NULL; } const char * @@ -28,7 +28,7 @@ "MemAvailable: %ld kB\nBuffers: %ld kB\n" "Cached: %ld kB\n", &total, &free, &buffers, &buffers, &cached) == 5) ? - bprintf("%d", 100 * ((total - free) - (buffers + cached)) / + bprintf("%d%%", 100 * ((total - free) - (buffers + cached)) / total) : NULL; } @@ -39,7 +39,7 @@ long total; return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ? - bprintf("%f", (float)total / 1024 / 1024) : NULL; + fmt_human_2(total * 1024, "B") : NULL; } const char * @@ -53,9 +53,7 @@ "MemAvailable: %ld kB\nBuffers: %ld kB\n" "Cached: %ld kB\n", &total, &free, &buffers, &buffers, &cached) == 5) ? - bprintf("%f", (float)(total - free - buffers - cached) / - 1024 / 1024) : - NULL; + fmt_human_2((total - free - buffers - cached) * 1024, "B") : NULL; } #elif defined(__OpenBSD__) #include @@ -81,13 +79,11 @@ ram_free(void) { struct uvmexp uvmexp; - float free; int free_pages; if (load_uvmexp(&uvmexp)) { free_pages = uvmexp.npages - uvmexp.active; - free = (float)(pagetok(free_pages, uvmexp.pageshift)) / 1024 / 1024; - return bprintf("%f", free); + return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -101,7 +97,7 @@ if (load_uvmexp(&uvmexp)) { percent = uvmexp.active * 100 / uvmexp.npages; - return bprintf("%d", percent); + return bprintf("%d%%", percent); } return NULL; @@ -111,11 +107,9 @@ ram_total(void) { struct uvmexp uvmexp; - float total; if (load_uvmexp(&uvmexp)) { - total = (float)(pagetok(uvmexp.npages, uvmexp.pageshift)) / 1024 / 1024; - return bprintf("%f", total); + return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -125,11 +119,9 @@ ram_used(void) { struct uvmexp uvmexp; - float used; if (load_uvmexp(&uvmexp)) { - used = (float)(pagetok(uvmexp.active, uvmexp.pageshift)) / 1024 / 1024; - return bprintf("%f", used); + return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B"); } return NULL;