Skip to content

Commit 2c3e873

Browse files
ikegami-tigaw
authored andcommitted
nvme-print-stdout: replace nvme simple list with generic table
Use the generic table code added instead of hard coded table. Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
1 parent 4628c5c commit 2c3e873

File tree

1 file changed

+104
-12
lines changed

1 file changed

+104
-12
lines changed

nvme-print-stdout.c

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
#include "logging.h"
2525
#include "common.h"
2626

27+
enum simple_list_col {
28+
SIMPLE_LIST_COL_NODE,
29+
SIMPLE_LIST_COL_GENERIC,
30+
SIMPLE_LIST_COL_SN,
31+
SIMPLE_LIST_COL_MODEL,
32+
SIMPLE_LIST_COL_NS,
33+
SIMPLE_LIST_COL_USAGE,
34+
SIMPLE_LIST_COL_FORMAT,
35+
SIMPLE_LIST_COL_FW_REV,
36+
SIMPLE_LIST_COL_NUM,
37+
};
38+
2739
static const uint8_t zero_uuid[16] = { 0 };
2840
static const uint8_t invalid_uuid[16] = {[0 ... 15] = 0xff };
2941
static const char dash[100] = {[0 ... 99] = '-'};
@@ -104,6 +116,11 @@ struct nvme_resources {
104116
struct strset namespaces;
105117
};
106118

119+
struct nvme_resources_table {
120+
struct nvme_resources *res;
121+
struct table *t;
122+
};
123+
107124
static int nvme_resources_init(nvme_root_t r, struct nvme_resources *res)
108125
{
109126
nvme_host_t h;
@@ -5355,7 +5372,7 @@ static void stdout_generic_full_path(nvme_ns_t n, char *path, size_t len)
53555372
snprintf(path, len, "ng%dn%d", instance, head_instance);
53565373
}
53575374

5358-
static void stdout_list_item(nvme_ns_t n)
5375+
static void list_item(nvme_ns_t n, struct table *t)
53595376
{
53605377
char usage[128] = { 0 }, format[128] = { 0 };
53615378
char devname[128] = { 0 }; char genname[128] = { 0 };
@@ -5367,6 +5384,8 @@ static void stdout_list_item(nvme_ns_t n)
53675384
const char *s_suffix = suffix_si_get(&nsze);
53685385
const char *u_suffix = suffix_si_get(&nuse);
53695386
const char *l_suffix = suffix_binary_get(&lba);
5387+
char ns[STR_LEN];
5388+
int row;
53705389

53715390
snprintf(usage, sizeof(usage), "%6.2f %2sB / %6.2f %2sB", nuse,
53725391
u_suffix, nsze, s_suffix);
@@ -5376,36 +5395,109 @@ static void stdout_list_item(nvme_ns_t n)
53765395
stdout_dev_full_path(n, devname, sizeof(devname));
53775396
stdout_generic_full_path(n, genname, sizeof(genname));
53785397

5379-
printf("%-21s %-21s %-20s %-40s %#-10x %-26s %-16s %-8s\n",
5380-
devname, genname, nvme_ns_get_serial(n),
5381-
nvme_ns_get_model(n), nvme_ns_get_nsid(n), usage, format,
5382-
nvme_ns_get_firmware(n));
5398+
if (!t) {
5399+
printf("%-21s %-21s %-20s %-40s %#-10x %-26s %-16s %-8s\n",
5400+
devname, genname, nvme_ns_get_serial(n),
5401+
nvme_ns_get_model(n), nvme_ns_get_nsid(n), usage, format,
5402+
nvme_ns_get_firmware(n));
5403+
return;
5404+
}
5405+
5406+
row = table_get_row_id(t);
5407+
if (row < 0) {
5408+
printf("Failed to add row\n");
5409+
return;
5410+
}
5411+
if (table_set_value_str(t, SIMPLE_LIST_COL_NODE, row, devname, LEFT)) {
5412+
printf("Failed to set node value\n");
5413+
return;
5414+
}
5415+
if (table_set_value_str(t, SIMPLE_LIST_COL_GENERIC, row, genname, LEFT)) {
5416+
printf("Failed to set generic value\n");
5417+
return;
5418+
}
5419+
if (table_set_value_str(t, SIMPLE_LIST_COL_SN, row, nvme_ns_get_serial(n), LEFT)) {
5420+
printf("Failed to set sn value\n");
5421+
return;
5422+
}
5423+
if (table_set_value_str(t, SIMPLE_LIST_COL_MODEL, row, nvme_ns_get_model(n), LEFT)) {
5424+
printf("Failed to set model value\n");
5425+
return;
5426+
}
5427+
if (!sprintf(ns, "0x%x", nvme_ns_get_nsid(n))) {
5428+
printf("Failed to output ns string\n");
5429+
return;
5430+
}
5431+
if (table_set_value_str(t, SIMPLE_LIST_COL_NS, row, ns, LEFT)) {
5432+
printf("Failed to set ns value\n");
5433+
return;
5434+
}
5435+
if (table_set_value_str(t, SIMPLE_LIST_COL_USAGE, row, usage, LEFT)) {
5436+
printf("Failed to set usage value\n");
5437+
return;
5438+
}
5439+
if (table_set_value_str(t, SIMPLE_LIST_COL_FORMAT, row, format, LEFT)) {
5440+
printf("Failed to set format value\n");
5441+
return;
5442+
}
5443+
if (table_set_value_str(t, SIMPLE_LIST_COL_FW_REV, row, nvme_ns_get_firmware(n), LEFT)) {
5444+
printf("Failed to set fw rev value\n");
5445+
return;
5446+
}
5447+
table_add_row(t, row);
5448+
}
5449+
5450+
static void stdout_list_item(nvme_ns_t n)
5451+
{
5452+
list_item(n, NULL);
5453+
}
5454+
5455+
static void stdout_list_item_table(nvme_ns_t n, struct table *t)
5456+
{
5457+
list_item(n, t);
53835458
}
53845459

53855460
static bool stdout_simple_ns(const char *name, void *arg)
53865461
{
5387-
struct nvme_resources *res = arg;
5462+
struct nvme_resources_table *rst_t = arg;
5463+
struct nvme_resources *res = rst_t->res;
53885464
nvme_ns_t n;
53895465

53905466
n = htable_ns_get(&res->ht_n, name);
5391-
stdout_list_item(n);
5467+
stdout_list_item_table(n, rst_t->t);
53925468

53935469
return true;
53945470
}
53955471

53965472
static void stdout_simple_list(nvme_root_t r)
53975473
{
53985474
struct nvme_resources res;
5475+
struct table_column columns[SIMPLE_LIST_COL_NUM] = {
5476+
{ "Node", LEFT, 21 },
5477+
{ "Generic", LEFT, 21 },
5478+
{ "SN", LEFT, 20 },
5479+
{ "Model", LEFT, 40 },
5480+
{ "Namespace", LEFT, 10 },
5481+
{ "Usage", LEFT, 26 },
5482+
{ "Format", LEFT, 16 },
5483+
{ "FW Rev", LEFT, 8 },
5484+
};
5485+
struct table *t = table_init_with_columns(columns, ARRAY_SIZE(columns));
5486+
struct nvme_resources_table res_t = { &res, t };
5487+
5488+
if (!t) {
5489+
printf("Failed to init table\n");
5490+
return;
5491+
}
53995492

54005493
nvme_resources_init(r, &res);
54015494

5402-
printf("%-21s %-21s %-20s %-40s %-10s %-26s %-16s %-8s\n",
5403-
"Node", "Generic", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev");
5404-
printf("%-.21s %-.21s %-.20s %-.40s %-.10s %-.26s %-.16s %-.8s\n",
5405-
dash, dash, dash, dash, dash, dash, dash, dash);
5406-
strset_iterate(&res.namespaces, stdout_simple_ns, &res);
5495+
strset_iterate(&res.namespaces, stdout_simple_ns, &res_t);
5496+
5497+
table_print(t);
54075498

54085499
nvme_resources_free(&res);
5500+
table_free(t);
54095501
}
54105502

54115503
static void stdout_ns_details(nvme_ns_t n)

0 commit comments

Comments
 (0)