mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 10:37:35 +03:00
Add zfs_nicebytes() to print human-readable sizes
* Add zfs_nicebytes() to print human-readable sizes Some 'zfs', 'zpool' and 'zdb' output strings can be confusing to the user when no units are specified. This add a new zfs_nicenum_format "ZFS_NICENUM_BYTES" used to print bytes in their human-readable form. Additionally, update some test cases to use machine-parsable 'zfs get'. Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: loli10K <ezomori.nozomu@gmail.com> Closes #2414 Closes #3185 Closes #3594 Closes #6032
This commit is contained in:
@@ -1173,7 +1173,7 @@ zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
|
||||
*/
|
||||
if (intval < SPA_MINBLOCKSIZE ||
|
||||
intval > maxbs || !ISP2(intval)) {
|
||||
zfs_nicenum(maxbs, buf, sizeof (buf));
|
||||
zfs_nicebytes(maxbs, buf, sizeof (buf));
|
||||
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
||||
"'%s' must be power of 2 from 512B "
|
||||
"to %s"), propname, buf);
|
||||
@@ -1428,7 +1428,7 @@ badlabel:
|
||||
|
||||
case ZFS_PROP_VOLSIZE:
|
||||
if (intval % blocksize != 0) {
|
||||
zfs_nicenum(blocksize, buf,
|
||||
zfs_nicebytes(blocksize, buf,
|
||||
sizeof (buf));
|
||||
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
||||
"'%s' must be a multiple of "
|
||||
@@ -2569,7 +2569,7 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
|
||||
(void) snprintf(propbuf, proplen, "%llu",
|
||||
(u_longlong_t)val);
|
||||
else
|
||||
zfs_nicenum(val, propbuf, proplen);
|
||||
zfs_nicebytes(val, propbuf, proplen);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -2705,6 +2705,22 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
|
||||
(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
|
||||
break;
|
||||
|
||||
case ZFS_PROP_REFERENCED:
|
||||
case ZFS_PROP_AVAILABLE:
|
||||
case ZFS_PROP_USED:
|
||||
case ZFS_PROP_USEDSNAP:
|
||||
case ZFS_PROP_USEDDS:
|
||||
case ZFS_PROP_USEDREFRESERV:
|
||||
case ZFS_PROP_USEDCHILD:
|
||||
if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
|
||||
return (-1);
|
||||
if (literal)
|
||||
(void) snprintf(propbuf, proplen, "%llu",
|
||||
(u_longlong_t)val);
|
||||
else
|
||||
zfs_nicebytes(val, propbuf, proplen);
|
||||
break;
|
||||
|
||||
default:
|
||||
switch (zfs_prop_get_type(prop)) {
|
||||
case PROP_TYPE_NUMBER:
|
||||
@@ -3001,6 +3017,9 @@ zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
|
||||
(type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
|
||||
type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA)) {
|
||||
(void) strlcpy(propbuf, "none", proplen);
|
||||
} else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
|
||||
type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED) {
|
||||
zfs_nicebytes(propvalue, propbuf, proplen);
|
||||
} else {
|
||||
zfs_nicenum(propvalue, propbuf, proplen);
|
||||
}
|
||||
@@ -3057,7 +3076,7 @@ zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
|
||||
(void) snprintf(propbuf, proplen, "%llu",
|
||||
(u_longlong_t)propvalue);
|
||||
} else {
|
||||
zfs_nicenum(propvalue, propbuf, proplen);
|
||||
zfs_nicebytes(propvalue, propbuf, proplen);
|
||||
}
|
||||
|
||||
return (0);
|
||||
|
||||
@@ -322,7 +322,7 @@ zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
|
||||
(void) snprintf(buf, len, "%llu",
|
||||
(u_longlong_t)intval);
|
||||
} else {
|
||||
(void) zfs_nicenum(intval, buf, len);
|
||||
(void) zfs_nicebytes(intval, buf, len);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1253,7 +1253,8 @@ zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
|
||||
{
|
||||
char buf[64];
|
||||
|
||||
zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
|
||||
zfs_nicebytes(SPA_MINDEVSIZE, buf,
|
||||
sizeof (buf));
|
||||
|
||||
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
||||
"one or more devices is less than the "
|
||||
@@ -1390,7 +1391,8 @@ zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
|
||||
{
|
||||
char buf[64];
|
||||
|
||||
zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
|
||||
zfs_nicebytes(SPA_MINDEVSIZE, buf,
|
||||
sizeof (buf));
|
||||
|
||||
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
|
||||
"device is less than the minimum "
|
||||
|
||||
@@ -1166,7 +1166,7 @@ send_progress_thread(void *arg)
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec,
|
||||
bytes, zhp->zfs_name);
|
||||
} else {
|
||||
zfs_nicenum(bytes, buf, sizeof (buf));
|
||||
zfs_nicebytes(bytes, buf, sizeof (buf));
|
||||
(void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec,
|
||||
buf, zhp->zfs_name);
|
||||
@@ -1211,7 +1211,7 @@ send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
|
||||
(longlong_t)size);
|
||||
} else {
|
||||
char buf[16];
|
||||
zfs_nicenum(size, buf, sizeof (buf));
|
||||
zfs_nicebytes(size, buf, sizeof (buf));
|
||||
(void) fprintf(fout, dgettext(TEXT_DOMAIN,
|
||||
" estimated size is %s"), buf);
|
||||
}
|
||||
@@ -1959,7 +1959,7 @@ zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
|
||||
(longlong_t)sdd.size);
|
||||
} else {
|
||||
char buf[16];
|
||||
zfs_nicenum(sdd.size, buf, sizeof (buf));
|
||||
zfs_nicebytes(sdd.size, buf, sizeof (buf));
|
||||
(void) fprintf(fout, dgettext(TEXT_DOMAIN,
|
||||
"total estimated size is %s\n"), buf);
|
||||
}
|
||||
@@ -3645,10 +3645,10 @@ zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
|
||||
time_t delta = time(NULL) - begin_time;
|
||||
if (delta == 0)
|
||||
delta = 1;
|
||||
zfs_nicenum(bytes, buf1, sizeof (buf1));
|
||||
zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
|
||||
zfs_nicebytes(bytes, buf1, sizeof (buf1));
|
||||
zfs_nicebytes(bytes/delta, buf2, sizeof (buf1));
|
||||
|
||||
(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
|
||||
(void) printf("received %s stream in %lu seconds (%s/sec)\n",
|
||||
buf1, delta, buf2);
|
||||
}
|
||||
|
||||
|
||||
@@ -412,13 +412,13 @@ dump_ddt_stat(const ddt_stat_t *dds, int h)
|
||||
zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
|
||||
|
||||
zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
|
||||
zfs_nicenum(dds->dds_lsize, lsize, sizeof (lsize));
|
||||
zfs_nicenum(dds->dds_psize, psize, sizeof (psize));
|
||||
zfs_nicenum(dds->dds_dsize, dsize, sizeof (dsize));
|
||||
zfs_nicebytes(dds->dds_lsize, lsize, sizeof (lsize));
|
||||
zfs_nicebytes(dds->dds_psize, psize, sizeof (psize));
|
||||
zfs_nicebytes(dds->dds_dsize, dsize, sizeof (dsize));
|
||||
zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
|
||||
zfs_nicenum(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
|
||||
zfs_nicenum(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
|
||||
zfs_nicenum(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
|
||||
zfs_nicebytes(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
|
||||
zfs_nicebytes(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
|
||||
zfs_nicebytes(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
|
||||
|
||||
(void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
|
||||
refcnt,
|
||||
|
||||
@@ -606,13 +606,16 @@ zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
|
||||
const char *u;
|
||||
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", "?", "?", "?"}
|
||||
};
|
||||
|
||||
const int units_len[] = {[ZFS_NICENUM_1024] = 6,
|
||||
[ZFS_NICENUM_BYTES] = 6,
|
||||
[ZFS_NICENUM_TIME] = 4};
|
||||
|
||||
const int k_unit[] = { [ZFS_NICENUM_1024] = 1024,
|
||||
[ZFS_NICENUM_BYTES] = 1024,
|
||||
[ZFS_NICENUM_TIME] = 1000};
|
||||
|
||||
double val;
|
||||
@@ -706,7 +709,14 @@ zfs_niceraw(uint64_t num, char *buf, size_t buflen)
|
||||
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_RAW);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Convert a number of bytes to an appropriately human-readable output.
|
||||
*/
|
||||
void
|
||||
zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
|
||||
{
|
||||
zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
|
||||
|
||||
Reference in New Issue
Block a user