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:
LOLi 2017-05-02 22:43:53 +02:00 committed by Brian Behlendorf
parent 03b60eee78
commit e7fbeb606a
17 changed files with 124 additions and 67 deletions

View File

@ -3077,7 +3077,7 @@ zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
int sec_remaining =
(zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
zfs_nicenum(bytes, buf, sizeof (buf));
zfs_nicebytes(bytes, buf, sizeof (buf));
(void) fprintf(stderr,
"\r%5s completed (%4dMB/s) "
"estimated time remaining: %uhr %02umin %02usec ",

View File

@ -1313,7 +1313,7 @@ zfs_do_destroy(int argc, char **argv)
if (cb.cb_verbose) {
char buf[16];
zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
if (cb.cb_parsable) {
(void) printf("reclaim\t%llu\n",
(u_longlong_t)cb.cb_snapused);
@ -2549,11 +2549,17 @@ userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
}
/* Calculate/update width of USED/QUOTA fields */
if (cb->cb_nicenum)
zfs_nicenum(space, sizebuf, sizeof (sizebuf));
else
if (cb->cb_nicenum) {
if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA) {
zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
} else {
zfs_nicenum(space, sizebuf, sizeof (sizebuf));
}
} else {
(void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
(u_longlong_t)space);
}
sizelen = strlen(sizebuf);
if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
propname = "used";
@ -2646,22 +2652,36 @@ print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
break;
case USFIELD_USED:
case USFIELD_QUOTA:
if (type == DATA_TYPE_UINT64) {
if (parsable) {
(void) sprintf(valstr, "%llu",
(u_longlong_t)val64);
strval = valstr;
} else if (field == USFIELD_QUOTA &&
val64 == 0) {
strval = "none";
} else {
zfs_nicebytes(val64, valstr,
sizeof (valstr));
strval = valstr;
}
}
break;
case USFIELD_OBJUSED:
case USFIELD_OBJQUOTA:
if (type == DATA_TYPE_UINT64) {
if (parsable) {
(void) sprintf(valstr, "%llu",
(u_longlong_t)val64);
strval = valstr;
} else if (field == USFIELD_OBJQUOTA &&
val64 == 0) {
strval = "none";
} else {
zfs_nicenum(val64, valstr,
sizeof (valstr));
}
if ((field == USFIELD_QUOTA ||
field == USFIELD_OBJQUOTA) &&
strcmp(valstr, "0") == 0)
strval = "none";
else
strval = valstr;
}
}
break;
}

View File

@ -5730,7 +5730,7 @@ print_scan_status(pool_scan_stat_t *ps)
start = ps->pss_start_time;
end = ps->pss_end_time;
zfs_nicenum(ps->pss_processed, processed_buf, sizeof (processed_buf));
zfs_nicebytes(ps->pss_processed, processed_buf, sizeof (processed_buf));
assert(ps->pss_func == POOL_SCAN_SCRUB ||
ps->pss_func == POOL_SCAN_RESILVER);
@ -5792,9 +5792,9 @@ print_scan_status(pool_scan_stat_t *ps)
mins_left = ((total - examined) / rate) / 60;
hours_left = mins_left / 60;
zfs_nicenum(examined, examined_buf, sizeof (examined_buf));
zfs_nicenum(total, total_buf, sizeof (total_buf));
zfs_nicenum(rate, rate_buf, sizeof (rate_buf));
zfs_nicebytes(examined, examined_buf, sizeof (examined_buf));
zfs_nicebytes(total, total_buf, sizeof (total_buf));
zfs_nicebytes(rate, rate_buf, sizeof (rate_buf));
/*
* do not print estimated time if hours_left is more than 30 days
@ -5897,6 +5897,7 @@ print_dedup_stats(nvlist_t *config)
ddt_stat_t *dds;
ddt_object_t *ddo;
uint_t c;
char dspace[6], mspace[6];
/*
* If the pool was faulted then we may not have been able to
@ -5914,10 +5915,12 @@ print_dedup_stats(nvlist_t *config)
return;
}
(void) printf("DDT entries %llu, size %llu on disk, %llu in core\n",
zfs_nicebytes(ddo->ddo_dspace, dspace, sizeof (dspace));
zfs_nicebytes(ddo->ddo_mspace, mspace, sizeof (mspace));
(void) printf("DDT entries %llu, size %s on disk, %s in core\n",
(u_longlong_t)ddo->ddo_count,
(u_longlong_t)ddo->ddo_dspace,
(u_longlong_t)ddo->ddo_mspace);
dspace,
mspace);
verify(nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_DDT_STATS,
(uint64_t **)&dds, &c) == 0);

View File

@ -773,8 +773,9 @@ extern int zfs_deleg_share_nfs(libzfs_handle_t *, char *, char *, char *,
enum zfs_nicenum_format {
ZFS_NICENUM_1024 = 0,
ZFS_NICENUM_TIME = 1,
ZFS_NICENUM_RAW = 2
ZFS_NICENUM_BYTES = 1,
ZFS_NICENUM_TIME = 2,
ZFS_NICENUM_RAW = 3
};
/*
@ -786,6 +787,7 @@ extern void zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
extern void zfs_nicetime(uint64_t, char *, size_t);
extern void zfs_nicebytes(uint64_t, char *, size_t);
extern int zfs_nicestrtonum(libzfs_handle_t *, const char *, uint64_t *);
/*

View File

@ -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 @@ zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
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);

View File

@ -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 "

View File

@ -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);
}

View File

@ -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,

View File

@ -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)

View File

@ -58,11 +58,7 @@ function get_used_prop
typeset ds=$1
typeset used
used=`zfs list -H -o used $ds`
used=${used%[m|M]}
if [[ $used == *K ]]; then
used=0
fi
used=`zfs list -H -p -o used $ds`
echo $used
}
@ -79,7 +75,7 @@ function check_used
typeset -i cfactor
typeset -i fsize=${FILESIZE%[m|M]}
((used = ${charged_spc%[m|M]}))
((used = $charged_spc / 1024 / 1024))
((cfactor = used / fsize))
if ((cfactor != expected_cfactor)); then
log_fail "The space is not charged correctly while setting" \

View File

@ -88,20 +88,22 @@ else
fi
for val in 1 2 3; do
blks=`ls -ls /$TESTPOOL/fs_$val/$FILE | awk '{print $1}'`
(( used = blks * $blksize / (1024 * 1024) ))
(( used = blks * $blksize )) # bytes
check_used $used $val
done
log_note "Verify df(1M) can corectly display the space charged."
for val in 1 2 3; do
used=`df -F zfs -h /$TESTPOOL/fs_$val/$FILE | grep $TESTPOOL/fs_$val \
used=`df -F zfs -k /$TESTPOOL/fs_$val/$FILE | grep $TESTPOOL/fs_$val \
| awk '{print $3}'`
(( used = used * 1024 )) # kb -> bytes
check_used $used $val
done
log_note "Verify du(1) can correctly display the space charged."
for val in 1 2 3; do
used=`du -h /$TESTPOOL/fs_$val/$FILE | awk '{print $1}'`
used=`du -k /$TESTPOOL/fs_$val/$FILE | awk '{print $1}'`
(( used = used * 1024 )) # kb -> bytes
check_used $used $val
done

View File

@ -28,9 +28,9 @@
# Copyright (c) 2012, 2015 by Delphix. All rights reserved.
#
set -A RW_FS_PROP "quota=512M" \
"reservation=512M" \
"recordsize=256K" \
set -A RW_FS_PROP "quota=536870912" \
"reservation=536870912" \
"recordsize=262144" \
"mountpoint=/tmp/mnt$$" \
"checksum=fletcher2" \
"compression=lzjb" \
@ -43,7 +43,7 @@ set -A RW_FS_PROP "quota=512M" \
"canmount=off" \
"local:department=123"
set -A RW_VOL_PROP "volblocksize=16K" \
set -A RW_VOL_PROP "volblocksize=16384" \
"checksum=fletcher2" \
"compression=lzjb" \
"readonly=on" \
@ -54,8 +54,8 @@ set -A RW_VOL_CLONE_PROP "checksum=fletcher2" \
"readonly=on" \
"local:department=123"
set -A FS_ONLY_PROP "quota=512M" \
"recordsize=64K" \
set -A FS_ONLY_PROP "quota=536870912" \
"recordsize=65536" \
"mountpoint=/tmp/mnt$$" \
"atime=off" \
"devices=off" \
@ -66,4 +66,4 @@ set -A FS_ONLY_PROP "quota=512M" \
"canmount=off" \
"version=1"
set -A VOL_ONLY_PROP "volblocksize=16K" "volsize=512M"
set -A VOL_ONLY_PROP "volblocksize=16384" "volsize=536870912"

View File

@ -43,7 +43,7 @@ function propertycheck
typeset prop=$(echo $propstr | awk -F= '{print $1}')
typeset expect_value=$(echo $propstr | awk -F= '{print $2}')
typeset value=$(zfs get -H -o value $prop $dtst)
typeset value=$(zfs get -H -p -o value $prop $dtst)
if [[ "$expect_value" == "$value" ]]; then

View File

@ -56,9 +56,9 @@ log_onexit cleanup
log_assert "'zpool create -O property=value pool' can successfully create a pool \
with correct filesystem property set."
set -A RW_FS_PROP "quota=512M" \
"reservation=512M" \
"recordsize=64K" \
set -A RW_FS_PROP "quota=536870912" \
"reservation=536870912" \
"recordsize=262144" \
"mountpoint=/tmp/mnt$$" \
"checksum=fletcher2" \
"compression=lzjb" \

View File

@ -56,9 +56,9 @@ log_onexit cleanup
log_assert "'zpool create -O property=value pool' can successfully create a pool \
with multiple filesystem properties set."
set -A RW_FS_PROP "quota=512M" \
"reservation=512M" \
"recordsize=64K" \
set -A RW_FS_PROP "quota=536870912" \
"reservation=536870912" \
"recordsize=262144" \
"mountpoint=/tmp/mnt$$" \
"checksum=fletcher2" \
"compression=lzjb" \

View File

@ -53,6 +53,7 @@ log_assert "Check the basic function of {user|group} used"
sync_pool
typeset user_used=$(get_value "userused@$QUSER1" $QFS)
typeset group_used=$(get_value "groupused@$QGROUP" $QFS)
typeset file_size='100m'
if [[ $user_used != 0 ]]; then
log_fail "FAIL: userused is $user_used, should be 0"
@ -62,15 +63,17 @@ if [[ $group_used != 0 ]]; then
fi
mkmount_writable $QFS
log_must user_run $QUSER1 mkfile 100m $QFILE
log_must user_run $QUSER1 mkfile $file_size $QFILE
sync_pool
user_used=$(get_value "userused@$QUSER1" $QFS)
group_used=$(get_value "groupused@$QGROUP" $QFS)
if [[ $user_used != "100M" ]]; then
# get_value() reads the exact byte value which is slightly more than 100m
if [[ "$(($user_used/1024/1024))m" != "$file_size" ]]; then
log_note "user $QUSER1 used is $user_used"
log_fail "userused for user $QUSER1 expected to be 50.0M, not $user_used"
log_fail "userused for user $QUSER1 expected to be $file_size, " \
"not $user_used"
fi
if [[ $user_used != $group_used ]]; then

View File

@ -111,7 +111,7 @@ function get_value # property dataset
typeset prop=$1
typeset dataset=$2
prop_val=$(zfs get -H -o value $prop $dataset 2>/dev/null)
prop_val=$(zfs get -H -p -o value $prop $dataset 2>/dev/null)
if [[ $? -ne 0 ]]; then
log_note "Unable to get $prop property for dataset " \
"$dataset"