nvmecontrol: improve namespace size unit of devlist command output

Add an option of -h --human to output human readable size unit instead
of the fixed unit (MB).

Signed-off-by:		Wanpeng Qian <wanpengqian@gmail.com>
Reviewed by:		imp, bcr
Differential Revision:	https://reviews.freebsd.org/D32957
This commit is contained in:
Wanpeng Qian 2022-10-07 17:59:02 -06:00 committed by Warner Losh
parent 34aac98e35
commit 9c1bec9c21
2 changed files with 39 additions and 6 deletions

View File

@ -34,6 +34,8 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <libutil.h>
#include <paths.h>
#include <stddef.h>
#include <stdio.h>
@ -51,10 +53,27 @@ __FBSDID("$FreeBSD$");
static cmd_fn_t devlist;
static struct options {
bool human;
} opt = {
.human = false,
};
static const struct opts devlist_opts[] = {
#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
OPT("human", 'h', arg_none, opt, human,
"Show human readable disk size"),
{ NULL, 0, arg_none, NULL, NULL }
};
#undef OPT
static struct cmd devlist_cmd = {
.name = "devlist",
.fn = devlist,
.descr = "List NVMe controllers and namespaces"
.descr = "List NVMe controllers and namespaces",
.ctx_size = sizeof(opt),
.opts = devlist_opts,
.args = NULL,
};
CMD_COMMAND(devlist_cmd);
@ -81,7 +100,9 @@ devlist(const struct cmd *f, int argc, char *argv[])
struct nvme_namespace_data nsdata;
char name[64];
uint8_t mn[64];
uint8_t buf[7];
uint32_t i;
uint64_t size;
int ctrlr, fd, found, ret;
if (arg_parse(argc, argv, f))
@ -115,11 +136,15 @@ devlist(const struct cmd *f, int argc, char *argv[])
continue;
sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
NVME_NS_PREFIX, i + 1);
printf(" %10s (%lldMB)\n",
name,
nsdata.nsze *
(long long)ns_get_sector_size(&nsdata) /
1024 / 1024);
size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata);
if (opt.human) {
humanize_number(buf, sizeof(buf), size, "B",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
printf(" %10s (%s)\n", name, buf);
} else {
printf(" %10s (%luMB)\n", name, size / 1024 / 1024);
}
}
close(fd);

View File

@ -44,6 +44,7 @@
.Sh SYNOPSIS
.Nm
.Ic devlist
.Op Fl h
.Nm
.Ic identify
.Op Fl v
@ -209,6 +210,13 @@
.Sh DESCRIPTION
NVM Express (NVMe) is a storage protocol standard, for SSDs and other
high-speed storage devices over PCI Express.
.Ss devlist
List all NVMe controllers and namespaces along with their device nodes.
With the
.Fl h
argument, use unit suffixes: Byte, Kibibyte, Mebibyte, Gibibyte, Tebibyte
and Pebibyte (based on powers of 1024) when showing the disk space.
By default, uses Mebibyte.
.Ss identify
The identify commands reports information from the drive's
.Dv IDENTIFY_CONTROLLER