diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 9b56af7bebd..60d550c5190 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -20,6 +20,7 @@ #include "process-util.h" #include "procfs-util.h" #include "sort-util.h" +#include "string-table.h" #include "terminal-util.h" #include "time-util.h" #include "virt.h" @@ -54,6 +55,16 @@ typedef enum PidsCount { COUNT_PIDS, /* most, requires pids controller */ } PidsCount; +typedef enum { + ORDER_PATH, + ORDER_TASKS, + ORDER_CPU, + ORDER_MEMORY, + ORDER_IO, + _ORDER_MAX, + _ORDER_INVALID = -EINVAL, +} Order; + static unsigned arg_depth = 3; static unsigned arg_iterations = UINT_MAX; static bool arg_batch = false; @@ -63,22 +74,24 @@ static char* arg_machine = NULL; static char* arg_root = NULL; static bool arg_recursive = true; static bool arg_recursive_unset = false; - static PidsCount arg_count = COUNT_PIDS; - -static enum { - ORDER_PATH, - ORDER_TASKS, - ORDER_CPU, - ORDER_MEMORY, - ORDER_IO, -} arg_order = ORDER_CPU; +static Order arg_order = ORDER_CPU; static enum { CPU_PERCENT, CPU_TIME, } arg_cpu_type = CPU_PERCENT; +static const char *order_table[_ORDER_MAX] = { + [ORDER_PATH] = "path", + [ORDER_TASKS] = "tasks", + [ORDER_CPU] = "cpu", + [ORDER_MEMORY] = "memory", + [ORDER_IO] = "io", +}; + +DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(order, Order); + static Group *group_free(Group *g) { if (!g) return NULL; @@ -557,6 +570,12 @@ static int group_compare(Group * const *a, Group * const *b) { return -1; else if (y->io_valid) return 1; + + break; + + case _ORDER_MAX: + case _ORDER_INVALID: + assert_not_reached(); } return path_compare(x->path, y->path); @@ -810,18 +829,9 @@ static int parse_argv(int argc, char *argv[]) { break; case ARG_ORDER: - if (streq(optarg, "path")) - arg_order = ORDER_PATH; - else if (streq(optarg, "tasks")) - arg_order = ORDER_TASKS; - else if (streq(optarg, "cpu")) - arg_order = ORDER_CPU; - else if (streq(optarg, "memory")) - arg_order = ORDER_MEMORY; - else if (streq(optarg, "io")) - arg_order = ORDER_IO; - else - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + arg_order = order_from_string(optarg); + if (arg_order < 0) + return log_error_errno(arg_order, "Invalid argument to --order=: %s", optarg); break;