From a8980aa2d714231eb4448ca181ddd33661298a26 Mon Sep 17 00:00:00 2001 From: Julien Cassette Date: Sat, 28 Sep 2024 14:45:03 +0200 Subject: [PATCH] libzutil: allow to display powers of 1000 bytes ZFS displays bytes with K/M/G/T/P/E prefixes. They represent powers of 1024 bytes, i.e. KiB, MiB, GiB, TiB, PiB, EiB. Some users may want these prefixes to represent powers of 1000 bytes, i.e. KB, MB, GB, TB, PB, EB. This adds the new unit format and allows to use such display by defining an environment variable. Signed-off-by: Julien Cassette --- include/libzutil.h | 4 +++- lib/libzutil/zutil_nicenum.c | 18 +++++++++++++----- man/man8/zdb.8 | 9 +++++++++ man/man8/zfs.8 | 6 ++++++ man/man8/zpool.8 | 6 ++++++ 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/include/libzutil.h b/include/libzutil.h index f8712340cc5e..f6c8102a1e31 100644 --- a/include/libzutil.h +++ b/include/libzutil.h @@ -144,13 +144,15 @@ _LIBZUTIL_H boolean_t zfs_isnumber(const char *); * ZFS_NICENUM_TIME: Print nanosecs, microsecs, millisecs, seconds... * ZFS_NICENUM_RAW: Print the raw number without any formatting * ZFS_NICENUM_RAWTIME: Same as RAW, but print dashes ('-') for zero. + * ZFS_NICENUM_1000: Same as ZFS_NICENUM_BYTES but use powers of 1000. */ enum zfs_nicenum_format { ZFS_NICENUM_1024 = 0, ZFS_NICENUM_BYTES = 1, ZFS_NICENUM_TIME = 2, ZFS_NICENUM_RAW = 3, - ZFS_NICENUM_RAWTIME = 4 + ZFS_NICENUM_RAWTIME = 4, + ZFS_NICENUM_1000 = 5 }; /* diff --git a/lib/libzutil/zutil_nicenum.c b/lib/libzutil/zutil_nicenum.c index e05bcb40a8ce..54b9a34a5dae 100644 --- a/lib/libzutil/zutil_nicenum.c +++ b/lib/libzutil/zutil_nicenum.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -64,19 +65,22 @@ zfs_nicenum_format(uint64_t num, char *buf, size_t buflen, uint64_t n = num; int index = 0; const char *u; - const char *units[3][7] = { + const char *units[6][7] = { [ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"}, [ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"}, - [ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"} + [ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}, + [ZFS_NICENUM_1000] = {"B", "K", "M", "G", "T", "P", "E"} }; const int units_len[] = {[ZFS_NICENUM_1024] = 6, [ZFS_NICENUM_BYTES] = 6, - [ZFS_NICENUM_TIME] = 4}; + [ZFS_NICENUM_TIME] = 4, + [ZFS_NICENUM_1000] = 6}; const int k_unit[] = { [ZFS_NICENUM_1024] = 1024, [ZFS_NICENUM_BYTES] = 1024, - [ZFS_NICENUM_TIME] = 1000}; + [ZFS_NICENUM_TIME] = 1000, + [ZFS_NICENUM_1000] = 1000}; double val; @@ -180,5 +184,9 @@ zfs_niceraw(uint64_t num, char *buf, size_t buflen) void zfs_nicebytes(uint64_t num, char *buf, size_t buflen) { - zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES); + if (getenv("ZFS_OUTPUT_BYTES_SI") != NULL) { + zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_1000); + } else { + zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES); + } } diff --git a/man/man8/zdb.8 b/man/man8/zdb.8 index ae35454ad083..81eb3d7de157 100644 --- a/man/man8/zdb.8 +++ b/man/man8/zdb.8 @@ -567,6 +567,15 @@ refcnt blocks LSIZE PSIZE DSIZE blocks LSIZE PSIZE DSIZE dedup = 1.11, compress = 1.80, copies = 1.00, dedup * compress / copies = 2.00 .Ed . +.Sh ENVIRONMENT VARIABLES +.Bl -tag -width "ZFS_OUTPUT_BYTES_SI" +.\" Shared with zfs.8 and zpool.8 +.It Sy ZFS_OUTPUT_BYTES_SI +Make K/M/G/T/P/E prefixes in +.Nm zdb +output to represent powers of 1000 bytes, i.e. KB, MB, GB, TB, PB, EB. +.El +. .Sh SEE ALSO .Xr zfs 8 , .Xr zpool 8 diff --git a/man/man8/zfs.8 b/man/man8/zfs.8 index 2ee15ab21806..febaa3c33f0f 100644 --- a/man/man8/zfs.8 +++ b/man/man8/zfs.8 @@ -763,6 +763,12 @@ to set the maximum pipe size for sends/recieves. Disabled by default on Linux due to an unfixed deadlock in Linux's pipe size handling code. . +.\" Shared with zdb.8 and zpool.8 +.It Sy ZFS_OUTPUT_BYTES_SI +Make K/M/G/T/P/E prefixes in +.Nm zfs +output to represent powers of 1000 bytes, i.e. KB, MB, GB, TB, PB, EB. +. .\" Shared with zpool.8 .It Sy ZFS_MODULE_TIMEOUT Time, in seconds, to wait for diff --git a/man/man8/zpool.8 b/man/man8/zpool.8 index 02a258f66708..5c215dd2f559 100644 --- a/man/man8/zpool.8 +++ b/man/man8/zpool.8 @@ -561,6 +561,12 @@ If .Sy ZPOOL_SCRIPTS_ENABLED is not set, it is assumed that the user is allowed to run .Nm zpool Cm status Ns / Ns Cm iostat Fl c . +.\" Shared with zdb.8 and zfs.8 +.It Sy ZFS_OUTPUT_BYTES_SI +Make K/M/G/T/P/E prefixes in +.Nm zpool +output to represent powers of 1000 bytes, i.e. KB, MB, GB, TB, PB, EB. +. .\" Shared with zfs.8 .It Sy ZFS_MODULE_TIMEOUT Time, in seconds, to wait for