git: 9c1bec9c21a2 - main - nvmecontrol: improve namespace size unit of devlist command output

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sat, 08 Oct 2022 00:00:04 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=9c1bec9c21a2b14ac13b5f0ad6e447c082042e6a

commit 9c1bec9c21a2b14ac13b5f0ad6e447c082042e6a
Author:     Wanpeng Qian <wanpengqian@gmail.com>
AuthorDate: 2022-10-07 23:59:02 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-10-07 23:59:20 +0000

    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
---
 sbin/nvmecontrol/devlist.c     | 37 +++++++++++++++++++++++++++++++------
 sbin/nvmecontrol/nvmecontrol.8 |  8 ++++++++
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/sbin/nvmecontrol/devlist.c b/sbin/nvmecontrol/devlist.c
index 35dd1b47a811..e9df5faadb12 100644
--- a/sbin/nvmecontrol/devlist.c
+++ b/sbin/nvmecontrol/devlist.c
@@ -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);
diff --git a/sbin/nvmecontrol/nvmecontrol.8 b/sbin/nvmecontrol/nvmecontrol.8
index d6b41889a727..3d130497f1bb 100644
--- a/sbin/nvmecontrol/nvmecontrol.8
+++ b/sbin/nvmecontrol/nvmecontrol.8
@@ -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