summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-05-19 22:52:17 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-19 22:58:21 +0200
commit46c4540dd2f6181e77b0800a4e007d78d0162487 (patch)
tree7fcec772ef1dd3dbb8bdcaf30aa48c2fea1d096f /components
parent74c4f4ebdae8a12fc95840954dde574692234b01 (diff)
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 (limited to 'components')
-rw-r--r--components/disk.c6
-rw-r--r--components/netspeeds.c28
-rw-r--r--components/ram.c12
-rw-r--r--components/swap.c12
4 files changed, 35 insertions, 23 deletions
diff --git a/components/disk.c b/components/disk.c
index db18d12..998ad47 100644
--- 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
index 32e78d6..f0f7455 100644
--- 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
index 0eb2ea9..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) ?
- 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
index 64feceb..c005691 100644
--- 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