Skip to content

Commit 7516b6b

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 such display by defining an environment variable. Signed-off-by: Julien Cassette <[email protected]>
1 parent b2ca510 commit 7516b6b

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <ctype.h>
2727
#include <math.h>
2828
#include <stdio.h>
29+
#include <stdlib.h>
2930
#include <libzutil.h>
3031
#include <string.h>
3132

@@ -64,19 +65,22 @@ zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
6465
uint64_t n = num;
6566
int index = 0;
6667
const char *u;
67-
const char *units[3][7] = {
68+
const char *units[5][7] = {
6869
[ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"},
6970
[ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"},
70-
[ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}
71+
[ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"},
72+
[ZFS_NICENUM_BYTES_1000] = {"B", "K", "M", "G", "T", "P", "E"}
7173
};
7274

7375
const int units_len[] = {[ZFS_NICENUM_1024] = 6,
7476
[ZFS_NICENUM_BYTES] = 6,
75-
[ZFS_NICENUM_TIME] = 4};
77+
[ZFS_NICENUM_TIME] = 4,
78+
[ZFS_NICENUM_BYTES_1000] = 6};
7679

7780
const int k_unit[] = { [ZFS_NICENUM_1024] = 1024,
7881
[ZFS_NICENUM_BYTES] = 1024,
79-
[ZFS_NICENUM_TIME] = 1000};
82+
[ZFS_NICENUM_TIME] = 1000,
83+
[ZFS_NICENUM_BYTES_1000] = 1000};
8084

8185
double val;
8286

@@ -180,5 +184,9 @@ zfs_niceraw(uint64_t num, char *buf, size_t buflen)
180184
void
181185
zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
182186
{
183-
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
187+
if (getenv("ZFS_KB_IS_1000") != NULL) {
188+
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES_1000);
189+
} else {
190+
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
191+
}
184192
}

0 commit comments

Comments
 (0)