diff --git a/zconf/chp/chpstat/chpstat.8 b/zconf/chp/chpstat/chpstat.8 index 95e84901..3c505290 100644 --- a/zconf/chp/chpstat/chpstat.8 +++ b/zconf/chp/chpstat/chpstat.8 @@ -290,7 +290,8 @@ values are: .IP \(bu 3 .B auto: -Scale automatically to fit value into each column. This is the default. +Scale automatically to fit value into each column. To indicate the current scaling factor, +an SI-suffix is added to each scaled number (e.g. K for 1000). This is the default. .PP .IP \(bu 3 .IR number : @@ -299,19 +300,19 @@ Scale by .PP .IP \(bu 3 .B K: -Scale by 1024 (KiB) +Scale by 1000 (KB) .PP .IP \(bu 3 .B M: -Scale by 1,048,576 (MiB) +Scale by 1,000,000 (MB) .PP .IP \(bu 3 .B G: -Scale by 1,073,741,824 (GiB) +Scale by 1,000,000,000 (GB) .PP .IP \(bu 3 .B T: -Scale by 1,099,511,627,776 (TiB) +Scale by 1,000,000,000,000 (TB) .PP .RE diff --git a/zconf/chp/chpstat/chpstat.c b/zconf/chp/chpstat/chpstat.c index e822d858..a4abc7a7 100644 --- a/zconf/chp/chpstat/chpstat.c +++ b/zconf/chp/chpstat/chpstat.c @@ -459,6 +459,30 @@ static void parse_cmgs(char *arg) } } +static bool suffix_to_unit(char *arg, unsigned long *unit_ptr, char *suffix_ptr) +{ + const char *suffixes = "KMGT"; + unsigned long unit, base; + size_t len = strlen(arg); + char suffix; + int i; + + if (len != 1) + return false; + suffix = (char)toupper(*arg); + base = UNIT_DEC; + unit = base; + for (i = 0; suffixes[i]; i++) { + if (suffix == suffixes[i]) { + *unit_ptr = unit; + *suffix_ptr = suffix; + return true; + } + unit *= base; + } + return false; +} + /* * Parse a scale unit value in @arg and return the resulting scaling factor. */ @@ -467,22 +491,8 @@ static unsigned long parse_unit(char *arg) unsigned long unit; char *endptr; - if (strlen(arg) == 1) { - opts.unit_suffix = (char)toupper(*arg); - switch (opts.unit_suffix) { - case 'K': - return UNIT_BIN; - case 'M': - return UNIT_BIN * UNIT_BIN; - case 'G': - return UNIT_BIN * UNIT_BIN * UNIT_BIN; - case 'T': - return UNIT_BIN * UNIT_BIN * UNIT_BIN * UNIT_BIN; - default: - break; - } - } - opts.unit_suffix = 0; + if (suffix_to_unit(arg, &unit, &opts.unit_suffix)) + return unit; if (strcmp(arg, "auto") == 0) return UNIT_AUTO; /* Parse as number. */ @@ -1355,7 +1365,7 @@ static void add_pair_value(struct util_rec *table, struct column_t *col, suffix = scale_auto(pair, UNIT_DEC); } else if (pair->unit == CMG_BPS) { if (opts.unit == UNIT_AUTO) - suffix = scale_auto(pair, UNIT_BIN); + suffix = scale_auto(pair, UNIT_DEC); else scale_fixed(pair, opts.unit); } diff --git a/zconf/chp/chpstat/column.c b/zconf/chp/chpstat/column.c index 0dc4e80a..621d4d18 100644 --- a/zconf/chp/chpstat/column.c +++ b/zconf/chp/chpstat/column.c @@ -486,7 +486,7 @@ void column_update_bps_suffix(bool auto_scale, char suffix_char) if (auto_scale) util_asprintf(&str, "(B/s)"); else if (suffix_char) - util_asprintf(&str, "(%ciB/s)", suffix_char); + util_asprintf(&str, "(%cB/s)", suffix_char); else str = util_strdup("(*)");