From 048d813fcdc6d410fb20b0c858705e3db5755631 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 26 Sep 2025 11:05:19 +0200 Subject: [PATCH] cgtop: use table to parse --cpu= --- src/cgtop/cgtop.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 60d550c5190..7e06ee12558 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -65,6 +65,13 @@ typedef enum { _ORDER_INVALID = -EINVAL, } Order; +typedef enum { + CPU_PERCENT, + CPU_TIME, + _CPU_MAX, + _CPU_INVALID = -EINVAL, +} CPUType; + static unsigned arg_depth = 3; static unsigned arg_iterations = UINT_MAX; static bool arg_batch = false; @@ -76,11 +83,7 @@ static bool arg_recursive = true; static bool arg_recursive_unset = false; static PidsCount arg_count = COUNT_PIDS; static Order arg_order = ORDER_CPU; - -static enum { - CPU_PERCENT, - CPU_TIME, -} arg_cpu_type = CPU_PERCENT; +static CPUType arg_cpu_type = CPU_PERCENT; static const char *order_table[_ORDER_MAX] = { [ORDER_PATH] = "path", @@ -92,6 +95,13 @@ static const char *order_table[_ORDER_MAX] = { DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(order, Order); +static const char *cpu_type_table[_CPU_MAX] = { + [CPU_PERCENT] = "percentage", + [CPU_TIME] = "time", +}; + +DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(cpu_type, CPUType); + static Group *group_free(Group *g) { if (!g) return NULL; @@ -758,12 +768,9 @@ static int parse_argv(int argc, char *argv[]) { case ARG_CPU_TYPE: if (optarg) { - if (streq(optarg, "time")) - arg_cpu_type = CPU_TIME; - else if (streq(optarg, "percentage")) - arg_cpu_type = CPU_PERCENT; - else - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + arg_cpu_type = cpu_type_from_string(optarg); + if (arg_cpu_type < 0) + return log_error_errno(arg_cpu_type, "Unknown argument to --cpu=: %s", optarg); } else