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+
2739static const uint8_t zero_uuid [16 ] = { 0 };
2840static const uint8_t invalid_uuid [16 ] = {[0 ... 15 ] = 0xff };
2941static 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+
107124static int nvme_resources_init (nvme_root_t r , struct nvme_resources * res )
108125{
109126 nvme_host_t h ;
@@ -5346,7 +5363,7 @@ static void stdout_generic_full_path(nvme_ns_t n, char *path, size_t len)
53465363 snprintf (path , len , "ng%dn%d" , instance , head_instance );
53475364}
53485365
5349- static void stdout_list_item (nvme_ns_t n )
5366+ static void list_item (nvme_ns_t n , struct table * t )
53505367{
53515368 char usage [128 ] = { 0 }, format [128 ] = { 0 };
53525369 char devname [128 ] = { 0 }; char genname [128 ] = { 0 };
@@ -5358,6 +5375,8 @@ static void stdout_list_item(nvme_ns_t n)
53585375 const char * s_suffix = suffix_si_get (& nsze );
53595376 const char * u_suffix = suffix_si_get (& nuse );
53605377 const char * l_suffix = suffix_binary_get (& lba );
5378+ char ns [STR_LEN ];
5379+ int row ;
53615380
53625381 snprintf (usage , sizeof (usage ), "%6.2f %2sB / %6.2f %2sB" , nuse ,
53635382 u_suffix , nsze , s_suffix );
@@ -5367,36 +5386,109 @@ static void stdout_list_item(nvme_ns_t n)
53675386 stdout_dev_full_path (n , devname , sizeof (devname ));
53685387 stdout_generic_full_path (n , genname , sizeof (genname ));
53695388
5370- printf ("%-21s %-21s %-20s %-40s %#-10x %-26s %-16s %-8s\n" ,
5371- devname , genname , nvme_ns_get_serial (n ),
5372- nvme_ns_get_model (n ), nvme_ns_get_nsid (n ), usage , format ,
5373- nvme_ns_get_firmware (n ));
5389+ if (!t ) {
5390+ printf ("%-21s %-21s %-20s %-40s %#-10x %-26s %-16s %-8s\n" ,
5391+ devname , genname , nvme_ns_get_serial (n ),
5392+ nvme_ns_get_model (n ), nvme_ns_get_nsid (n ), usage , format ,
5393+ nvme_ns_get_firmware (n ));
5394+ return ;
5395+ }
5396+
5397+ row = table_get_row_id (t );
5398+ if (row < 0 ) {
5399+ printf ("Failed to add row\n" );
5400+ return ;
5401+ }
5402+ if (table_set_value_str (t , SIMPLE_LIST_COL_NODE , row , devname , LEFT )) {
5403+ printf ("Failed to set node value\n" );
5404+ return ;
5405+ }
5406+ if (table_set_value_str (t , SIMPLE_LIST_COL_GENERIC , row , genname , LEFT )) {
5407+ printf ("Failed to set generic value\n" );
5408+ return ;
5409+ }
5410+ if (table_set_value_str (t , SIMPLE_LIST_COL_SN , row , nvme_ns_get_serial (n ), LEFT )) {
5411+ printf ("Failed to set sn value\n" );
5412+ return ;
5413+ }
5414+ if (table_set_value_str (t , SIMPLE_LIST_COL_MODEL , row , nvme_ns_get_model (n ), LEFT )) {
5415+ printf ("Failed to set model value\n" );
5416+ return ;
5417+ }
5418+ if (!sprintf (ns , "0x%x" , nvme_ns_get_nsid (n ))) {
5419+ printf ("Failed to output ns string\n" );
5420+ return ;
5421+ }
5422+ if (table_set_value_str (t , SIMPLE_LIST_COL_NS , row , ns , LEFT )) {
5423+ printf ("Failed to set ns value\n" );
5424+ return ;
5425+ }
5426+ if (table_set_value_str (t , SIMPLE_LIST_COL_USAGE , row , usage , LEFT )) {
5427+ printf ("Failed to set usage value\n" );
5428+ return ;
5429+ }
5430+ if (table_set_value_str (t , SIMPLE_LIST_COL_FORMAT , row , format , LEFT )) {
5431+ printf ("Failed to set format value\n" );
5432+ return ;
5433+ }
5434+ if (table_set_value_str (t , SIMPLE_LIST_COL_FW_REV , row , nvme_ns_get_firmware (n ), LEFT )) {
5435+ printf ("Failed to set fw rev value\n" );
5436+ return ;
5437+ }
5438+ table_add_row (t , row );
5439+ }
5440+
5441+ static void stdout_list_item (nvme_ns_t n )
5442+ {
5443+ list_item (n , NULL );
5444+ }
5445+
5446+ static void stdout_list_item_table (nvme_ns_t n , struct table * t )
5447+ {
5448+ list_item (n , t );
53745449}
53755450
53765451static bool stdout_simple_ns (const char * name , void * arg )
53775452{
5378- struct nvme_resources * res = arg ;
5453+ struct nvme_resources_table * rst_t = arg ;
5454+ struct nvme_resources * res = rst_t -> res ;
53795455 nvme_ns_t n ;
53805456
53815457 n = htable_ns_get (& res -> ht_n , name );
5382- stdout_list_item ( n );
5458+ stdout_list_item_table ( n , rst_t -> t );
53835459
53845460 return true;
53855461}
53865462
53875463static void stdout_simple_list (nvme_root_t r )
53885464{
53895465 struct nvme_resources res ;
5466+ struct table_column columns [SIMPLE_LIST_COL_NUM ] = {
5467+ { "Node" , LEFT , 21 },
5468+ { "Generic" , LEFT , 21 },
5469+ { "SN" , LEFT , 20 },
5470+ { "Model" , LEFT , 40 },
5471+ { "Namespace" , LEFT , 10 },
5472+ { "Usage" , LEFT , 26 },
5473+ { "Format" , LEFT , 16 },
5474+ { "FW Rev" , LEFT , 8 },
5475+ };
5476+ struct table * t = table_init_with_columns (columns , ARRAY_SIZE (columns ));
5477+ struct nvme_resources_table res_t = { & res , t };
5478+
5479+ if (!t ) {
5480+ printf ("Failed to init table\n" );
5481+ return ;
5482+ }
53905483
53915484 nvme_resources_init (r , & res );
53925485
5393- printf ("%-21s %-21s %-20s %-40s %-10s %-26s %-16s %-8s\n" ,
5394- "Node" , "Generic" , "SN" , "Model" , "Namespace" , "Usage" , "Format" , "FW Rev" );
5395- printf ("%-.21s %-.21s %-.20s %-.40s %-.10s %-.26s %-.16s %-.8s\n" ,
5396- dash , dash , dash , dash , dash , dash , dash , dash );
5397- strset_iterate (& res .namespaces , stdout_simple_ns , & res );
5486+ strset_iterate (& res .namespaces , stdout_simple_ns , & res_t );
5487+
5488+ table_print (t );
53985489
53995490 nvme_resources_free (& res );
5491+ table_free (t );
54005492}
54015493
54025494static void stdout_ns_details (nvme_ns_t n )
0 commit comments