From a60bc03f5b8f3c2cd0e7c10a1021f7c87bccdeb5 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Fri, 30 Dec 2016 16:08:50 -0800 Subject: [PATCH] df, pstat - Use HN_FRACTIONAL * Use the new HN_FRACTIONAL to display fractional digits in a better way than HN_DECIMAL. The general problem being solved is that in numerous cases HN_DECIMAL would only display two digits, which is not enough precision. For example, if you have a 32.3G volume it would previously display as 32G, and will now display as 32.3G. If I configured 14.6TB of swap it would previously display as 14T and will now display as 14.6T. --- bin/df/df.c | 10 +++++++--- usr.sbin/pstat/pstat.c | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bin/df/df.c b/bin/df/df.c index 3512775a1e..15e5f8c99e 100644 --- a/bin/df/df.c +++ b/bin/df/df.c @@ -59,6 +59,10 @@ #define UNITS_SI 1 #define UNITS_2 2 +#ifndef HN_FRACTIONAL +#define HN_FRACTIONAL HN_DECIMAL +#endif + /* Maximum widths of various fields. */ struct maxwidths { int mntfrom; @@ -329,10 +333,10 @@ prthuman(struct statvfs *vsfsp, int64_t used) static void prthumanval(int64_t bytes) { - char buf[6]; + char buf[7]; int flags; - flags = HN_B | HN_NOSPACE | HN_DECIMAL; + flags = HN_B | HN_NOSPACE | HN_FRACTIONAL; if (hflag == UNITS_SI) flags |= HN_DIVISOR_1000; @@ -351,7 +355,7 @@ prthumanvalinode(int64_t bytes) char buf[6]; int flags; - flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000; + flags = HN_NOSPACE | HN_FRACTIONAL | HN_DIVISOR_1000; humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), bytes, "", HN_AUTOSCALE, flags); diff --git a/usr.sbin/pstat/pstat.c b/usr.sbin/pstat/pstat.c index 156c03326e..3d7ef801bd 100644 --- a/usr.sbin/pstat/pstat.c +++ b/usr.sbin/pstat/pstat.c @@ -74,6 +74,13 @@ # include #endif +/* + * Backoff to DECIMAL mode if humanize_number does not have FRACTIONAL mode. + */ +#ifndef HN_FRACTIONAL +#define HN_FRACTIONAL HN_DECIMAL +#endif + enum { NL_MOUNTLIST, NL_NUMVNODES, @@ -957,11 +964,13 @@ swapmode(void) humanize_number(buf1, sizeof(buf1), CONVERTB(kswap[n].ksw_used), "", - HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); + HN_AUTOSCALE, HN_NOSPACE | + HN_FRACTIONAL); humanize_number(buf2, sizeof(buf2), CONVERTB(kswap[n].ksw_total), "", - HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); + HN_AUTOSCALE, HN_NOSPACE | + HN_FRACTIONAL); printf("%s/%s swap space\n", buf1, buf2); } else { blocksize = 1024 * 1024; @@ -987,18 +996,18 @@ Output(const char *name, int hlen, struct kvm_swap *kswap, int flags) CONVERTB(kswap->ksw_total), "", HN_AUTOSCALE, - HN_NOSPACE | HN_DECIMAL); + HN_NOSPACE | HN_FRACTIONAL); humanize_number(buf2, 6, CONVERTB(kswap->ksw_used), "", HN_AUTOSCALE, - HN_NOSPACE | HN_DECIMAL); + HN_NOSPACE | HN_FRACTIONAL); humanize_number(buf3, 6, CONVERTB(kswap->ksw_total - kswap->ksw_used), "", HN_AUTOSCALE, - HN_NOSPACE | HN_DECIMAL); + HN_NOSPACE | HN_FRACTIONAL); } else { snprintf(buf1, sizeof(buf1), "%*ld", hlen, -- 2.11.4.GIT