return NULL;
}
- return fmt_scaled(fs.f_frsize * fs.f_bavail);
+ return fmt_human_2(fs.f_frsize * fs.f_bavail, "B");
}
const char *
return NULL;
}
- return fmt_scaled(fs.f_frsize * fs.f_blocks);
+ return fmt_human_2(fs.f_frsize * fs.f_blocks, "B");
}
const char *
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");
}
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 *
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>
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 *
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
"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 *
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 *
"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>
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;
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;
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;
}
sscanf(match, "SwapFree: %ld kB\n", &free);
- return fmt_scaled(free * 1024);
+ return fmt_human_2(free * 1024, "B");
}
const char *
}
sscanf(match, "SwapTotal: %ld kB\n", &total);
- return fmt_scaled(total * 1024);
+ return fmt_human_2(total * 1024, "B");
}
const char *
}
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>
getstats(&total, &used);
- return fmt_scaled((total - used) * 1024);
+ return fmt_human_2((total - used) * 1024, "B");
}
const char *
getstats(&total, &used);
- return fmt_scaled(total * 1024);
+ return fmt_human_2(total * 1024, "B");
}
const char *
getstats(&total, &used);
- return fmt_scaled(used * 1024);
+ return fmt_human_2(used * 1024, "B");
}
#endif
}
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
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, ...);