Skip to content

Commit 4a8dc04

Browse files
committed
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 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]>
1 parent b2ca510 commit 4a8dc04

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

include/libzutil.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,15 @@ _LIBZUTIL_H boolean_t zfs_isnumber(const char *);
144144
* ZFS_NICENUM_TIME: Print nanosecs, microsecs, millisecs, seconds...
145145
* ZFS_NICENUM_RAW: Print the raw number without any formatting
146146
* ZFS_NICENUM_RAWTIME: Same as RAW, but print dashes ('-') for zero.
147+
* ZFS_NICENUM_BYTES_1000: Same as ZFS_NICENUM_BYTES but use powers of 1000.
147148
*/
148149
enum zfs_nicenum_format {
149150
ZFS_NICENUM_1024 = 0,
150151
ZFS_NICENUM_BYTES = 1,
151152
ZFS_NICENUM_TIME = 2,
152153
ZFS_NICENUM_RAW = 3,
153-
ZFS_NICENUM_RAWTIME = 4
154+
ZFS_NICENUM_RAWTIME = 4,
155+
ZFS_NICENUM_BYTES_1000 = 5
154156
};
155157

156158
/*

lib/libzutil/zutil_nicenum.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,19 @@ zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
6767
const char *units[3][7] = {
6868
[ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"},
6969
[ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"},
70-
[ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}
70+
[ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"},
71+
[ZFS_NICENUM_BYTES_1000] = {"B", "K", "M", "G", "T", "P", "E"}
7172
};
7273

7374
const int units_len[] = {[ZFS_NICENUM_1024] = 6,
7475
[ZFS_NICENUM_BYTES] = 6,
75-
[ZFS_NICENUM_TIME] = 4};
76+
[ZFS_NICENUM_TIME] = 4,
77+
[ZFS_NICENUM_BYTES_1000] = 6};
7678

7779
const int k_unit[] = { [ZFS_NICENUM_1024] = 1024,
7880
[ZFS_NICENUM_BYTES] = 1024,
79-
[ZFS_NICENUM_TIME] = 1000};
81+
[ZFS_NICENUM_TIME] = 1000,
82+
[ZFS_NICENUM_BYTES_1000] = 1000};
8083

8184
double val;
8285

@@ -180,5 +183,9 @@ zfs_niceraw(uint64_t num, char *buf, size_t buflen)
180183
void
181184
zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
182185
{
183-
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
186+
if (libzfs_envvar_is_set("ZFS_KB_IS_1000")) {
187+
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES_1000);
188+
} else {
189+
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
190+
}
184191
}

0 commit comments

Comments
 (0)