@@ -699,7 +699,7 @@ static const char * const cmd_inspect_list_chunks_usage[] = {
699699 "Show chunks (block groups) layout for all devices" ,
700700 "" ,
701701 HELPINFO_UNITS_LONG ,
702- OPTLINE ("--sort MODE" , "sort by the physical or logical chunk start MODE is one of pstart or lstart (default: pstart)" ),
702+ OPTLINE ("--sort MODE" , "sort by a column ascending: pstart, lstart, usage (default: pstart)" ),
703703 OPTLINE ("--usage" , "show usage per block group (note: this can be slow)" ),
704704 OPTLINE ("--no-usage" , "don't show usage per block group" ),
705705 OPTLINE ("--empty" , "show empty space between block groups" ),
@@ -710,6 +710,7 @@ static const char * const cmd_inspect_list_chunks_usage[] = {
710710enum {
711711 CHUNK_SORT_PSTART ,
712712 CHUNK_SORT_LSTART ,
713+ CHUNK_SORT_USAGE ,
713714 CHUNK_SORT_DEFAULT = CHUNK_SORT_PSTART
714715};
715716
@@ -772,6 +773,22 @@ static int cmp_cse_devid_lstart(const void *va, const void *vb)
772773 return 1 ;
773774}
774775
776+ /* Compare entries by usage percent, descending. */
777+ static int cmp_cse_devid_usage (const void * va , const void * vb )
778+ {
779+ const struct list_chunks_entry * a = va ;
780+ const struct list_chunks_entry * b = vb ;
781+ const float usage_a = (float )a -> used / a -> length * 100 ;
782+ const float usage_b = (float )b -> used / b -> length * 100 ;
783+ const float epsilon = 1e-8 ;
784+
785+ if (usage_b - usage_a < epsilon )
786+ return 1 ;
787+ if (usage_a - usage_b < epsilon )
788+ return -1 ;
789+ return 0 ;
790+ }
791+
775792static int print_list_chunks (struct list_chunks_ctx * ctx , unsigned sort_mode ,
776793 unsigned unit_mode , bool with_usage , bool with_empty )
777794{
@@ -810,6 +827,9 @@ static int print_list_chunks(struct list_chunks_ctx *ctx, unsigned sort_mode,
810827 if (sort_mode == CHUNK_SORT_LSTART )
811828 qsort (ctx -> stats , ctx -> length , sizeof (ctx -> stats [0 ]),
812829 cmp_cse_devid_lstart );
830+ else if (sort_mode == CHUNK_SORT_USAGE )
831+ qsort (ctx -> stats , ctx -> length , sizeof (ctx -> stats [0 ]),
832+ cmp_cse_devid_usage );
813833
814834 /* Optional usage, two rows for header and separator, gaps */
815835 table = table_create (7 + (int )with_usage , 2 + ctx -> length + gaps );
@@ -969,6 +989,9 @@ static int cmd_inspect_list_chunks(const struct cmd_struct *cmd,
969989 sort_mode = CHUNK_SORT_PSTART ;
970990 } else if (strcmp (optarg , "lstart" ) == 0 ) {
971991 sort_mode = CHUNK_SORT_LSTART ;
992+ } else if (strcmp (optarg , "usage" ) == 0 ) {
993+ sort_mode = CHUNK_SORT_USAGE ;
994+ with_usage = true;
972995 } else {
973996 error ("unknown sort mode: %s" , optarg );
974997 exit (1 );
0 commit comments