slstatus

status monitor
git clone git://git.suckless.org/slstatus
Log | Files | Refs | README | LICENSE

commit 46c4540dd2f6181e77b0800a4e007d78d0162487
parent 74c4f4ebdae8a12fc95840954dde574692234b01
Author: Laslo Hunhold <dev@frign.de>
Date:   Sat, 19 May 2018 22:52:17 +0200

Implement fmt_human_2() and fmt_human_10()

These functions take the raw number and a unit and automatically
print it out "scaled down" to a proper SI-prefix, for powers of 2
and 10 respectively.

Apply them to the 2-power cases and keep the 10-power for a later
commit.

Diffstat:
Mcomponents/disk.c | 6+++---
Mcomponents/netspeeds.c | 28++++++++++++++++++++--------
Mcomponents/ram.c | 12++++++------
Mcomponents/swap.c | 12++++++------
Mutil.c | 32++++++++++++++++++++++++--------
Mutil.h | 3++-
6 files changed, 61 insertions(+), 32 deletions(-)

diff --git a/components/disk.c b/components/disk.c @@ -16,7 +16,7 @@ disk_free(const char *mnt) return NULL; } - return fmt_scaled(fs.f_frsize * fs.f_bavail); + return fmt_human_2(fs.f_frsize * fs.f_bavail, "B"); } const char * @@ -43,7 +43,7 @@ disk_total(const char *mnt) return NULL; } - return fmt_scaled(fs.f_frsize * fs.f_blocks); + return fmt_human_2(fs.f_frsize * fs.f_blocks, "B"); } const char * @@ -56,5 +56,5 @@ disk_used(const char *mnt) return NULL; } - return fmt_scaled(fs.f_frsize * (fs.f_blocks - fs.f_bfree)); + return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree), "B"); } diff --git a/components/netspeeds.c b/components/netspeeds.c @@ -25,9 +25,12 @@ if (pscanf(path, "%llu", &rxbytes) != 1) { return NULL; } + if (oldrxbytes == 0) { + return NULL; + } - return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((rxbytes - oldrxbytes) * + 1000 / interval, "B/s"); } const char * @@ -48,9 +51,12 @@ if (pscanf(path, "%llu", &txbytes) != 1) { return NULL; } + if (oldtxbytes == 0) { + return NULL; + } - return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((txbytes - oldtxbytes) * + 1000 / interval, "B/s"); } #elif defined(__OpenBSD__) #include <string.h> @@ -87,9 +93,12 @@ warn("reading 'if_data' failed"); return NULL; } + if (oldrxbytes == 0) { + return NULL; + } - return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((rxbytes - oldrxbytes) * + 1000 / interval, "B/s"); } const char * @@ -120,8 +129,11 @@ warn("reading 'if_data' failed"); return NULL; } + if (oldtxbytes == 0) { + return NULL; + } - return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) * - 1000 / interval) : NULL; + return fmt_human_2((txbytes - oldtxbytes) * + 1000 / interval, "B/s"); } #endif diff --git a/components/ram.c b/components/ram.c @@ -14,7 +14,7 @@ "MemFree: %ld kB\n" "MemAvailable: %ld kB\n", &free, &free, &free) == 3) ? - fmt_scaled(free * 1024) : NULL; + fmt_human_2(free * 1024, "B") : NULL; } const char * @@ -39,7 +39,7 @@ long total; return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", &total) == 1) ? - fmt_scaled(total * 1024) : NULL; + fmt_human_2(total * 1024, "B") : NULL; } const char * @@ -53,7 +53,7 @@ "MemAvailable: %ld kB\nBuffers: %ld kB\n" "Cached: %ld kB\n", &total, &free, &buffers, &buffers, &cached) == 5) ? - fmt_scaled((total - free - buffers - cached) * 1024) : NULL; + fmt_human_2((total - free - buffers - cached) * 1024, "B") : NULL; } #elif defined(__OpenBSD__) #include <stdlib.h> @@ -83,7 +83,7 @@ if (load_uvmexp(&uvmexp)) { free_pages = uvmexp.npages - uvmexp.active; - return fmt_scaled(pagetok(free_pages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -109,7 +109,7 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B"); } return NULL; @@ -121,7 +121,7 @@ struct uvmexp uvmexp; if (load_uvmexp(&uvmexp)) { - return fmt_scaled(pagetok(uvmexp.active, uvmexp.pageshift) * 1024); + return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B"); } return NULL; diff --git a/components/swap.c b/components/swap.c @@ -48,7 +48,7 @@ } sscanf(match, "SwapFree: %ld kB\n", &free); - return fmt_scaled(free * 1024); + return fmt_human_2(free * 1024, "B"); } const char * @@ -94,7 +94,7 @@ } sscanf(match, "SwapTotal: %ld kB\n", &total); - return fmt_scaled(total * 1024); + return fmt_human_2(total * 1024, "B"); } const char * @@ -122,7 +122,7 @@ } sscanf(match, "SwapFree: %ld kB\n", &free); - return fmt_scaled((total - free - cached) * 1024); + return fmt_human_2((total - free - cached) * 1024, "B"); } #elif defined(__OpenBSD__) #include <stdlib.h> @@ -174,7 +174,7 @@ getstats(&total, &used); - return fmt_scaled((total - used) * 1024); + return fmt_human_2((total - used) * 1024, "B"); } const char * @@ -194,7 +194,7 @@ getstats(&total, &used); - return fmt_scaled(total * 1024); + return fmt_human_2(total * 1024, "B"); } const char * @@ -204,6 +204,6 @@ getstats(&total, &used); - return fmt_scaled(used * 1024); + return fmt_human_2(used * 1024, "B"); } #endif diff --git a/util.c b/util.c @@ -87,19 +87,35 @@ bprintf(const char *fmt, ...) } const char * -fmt_scaled(size_t bytes) +fmt_human_2(size_t num, char *unit) { - unsigned int i; - float scaled; - const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", - "ZiB", "YiB" }; + size_t i; + double scaled; + const char *prefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", + "Zi", "Yi" }; - scaled = bytes; - for (i = 0; i < LEN(units) && scaled >= 1024; i++) { + scaled = num; + for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) { scaled /= 1024.0; } - return bprintf("%.1f%s", scaled, units[i]); + return bprintf("%.1f%s%s", scaled, prefix[i], unit); +} + +const char * +fmt_human_10(size_t num, char *unit) +{ + size_t i; + double scaled; + const char *prefix[] = { "", "K", "M", "G", "T", "P", "E", + "Z", "Y" }; + + scaled = num; + for (i = 0; i < LEN(prefix) && scaled >= 1000; i++) { + scaled /= 1000.0; + } + + return bprintf("%.1f%s%s", scaled, prefix[i], unit); } int diff --git a/util.h b/util.h @@ -10,5 +10,6 @@ void die(const char *, ...); int esnprintf(char *str, size_t size, const char *fmt, ...); const char *bprintf(const char *fmt, ...); -const char *fmt_scaled(size_t); +const char *fmt_human_2(size_t num, char *unit); +const char *fmt_human_10(size_t num, char *unit); int pscanf(const char *path, const char *fmt, ...);