1
0
mirror of https://github.com/systemd/systemd synced 2025-10-05 16:03:15 +02:00

cgtop: use table to parse --order=

This commit is contained in:
David Tardon
2025-09-26 10:59:31 +02:00
parent 854789543b
commit 3a21f6aa6e

View File

@@ -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;