Skip to content

Commit

Permalink
libzutil: allow to display powers of 1000 bytes
Browse files Browse the repository at this point in the history
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 it by defining an
environment variable.

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 <[email protected]>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 <[email protected]>
  • Loading branch information
Julien Cassette committed Sep 28, 2024
1 parent b2ca510 commit 4a8dc04
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion include/libzutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_BYTES_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_BYTES_1000 = 5
};

/*
Expand Down
15 changes: 11 additions & 4 deletions lib/libzutil/zutil_nicenum.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
const char *units[3][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_BYTES_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_BYTES_1000] = 6};

const int k_unit[] = { [ZFS_NICENUM_1024] = 1024,
[ZFS_NICENUM_BYTES] = 1024,
[ZFS_NICENUM_TIME] = 1000};
[ZFS_NICENUM_TIME] = 1000,
[ZFS_NICENUM_BYTES_1000] = 1000};

double val;

Expand Down Expand Up @@ -180,5 +183,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 (libzfs_envvar_is_set("ZFS_KB_IS_1000")) {
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES_1000);
} else {
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
}
}

0 comments on commit 4a8dc04

Please sign in to comment.