cgtop: split out the main loop into a separate function
This way the initial setup is nicely separated from the main loop logic.
This commit is contained in:
+41
-40
@@ -96,6 +96,7 @@ static Group *group_free(Group *g) {
|
||||
return mfree(g);
|
||||
}
|
||||
|
||||
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(group_hash_ops, char, path_hash_func, path_compare, Group, group_free);
|
||||
|
||||
static const char *maybe_format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
|
||||
if (arg_raw) {
|
||||
@@ -911,52 +912,19 @@ static const char* counting_what(void) {
|
||||
return "userspace processes (excl. kernel)";
|
||||
}
|
||||
|
||||
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(group_hash_ops, char, path_hash_func, path_compare, Group, group_free);
|
||||
|
||||
static int run(int argc, char *argv[]) {
|
||||
static int loop(const char *root) {
|
||||
_cleanup_hashmap_free_ Hashmap *a = NULL, *b = NULL;
|
||||
unsigned iteration = 0;
|
||||
usec_t last_refresh = 0;
|
||||
bool quit = false, immediate_refresh = false;
|
||||
_cleanup_free_ char *root = NULL;
|
||||
PidsCount possible_count;
|
||||
CGroupMask mask;
|
||||
bool immediate_refresh = false;
|
||||
int r;
|
||||
|
||||
log_setup();
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
r = cg_mask_supported(&mask);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to determine supported controllers: %m");
|
||||
|
||||
/* honor user selection unless pids controller is unavailable */
|
||||
possible_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_ALL_PROCESSES;
|
||||
arg_count = MIN(possible_count, arg_count);
|
||||
|
||||
if (arg_recursive_unset && arg_count == COUNT_PIDS)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k.");
|
||||
|
||||
r = show_cgroup_get_path_and_warn(arg_machine, arg_root, &root);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get root control group path: %m");
|
||||
log_debug("CGroup path: %s", root);
|
||||
|
||||
a = hashmap_new(&group_hash_ops);
|
||||
b = hashmap_new(&group_hash_ops);
|
||||
if (!a || !b)
|
||||
return log_oom();
|
||||
|
||||
signal(SIGWINCH, columns_lines_cache_reset);
|
||||
|
||||
if (arg_iterations == UINT_MAX)
|
||||
arg_iterations = on_tty() ? 0 : 1;
|
||||
|
||||
while (!quit) {
|
||||
for (;;) {
|
||||
usec_t t;
|
||||
char key;
|
||||
|
||||
@@ -978,7 +946,7 @@ static int run(int argc, char *argv[]) {
|
||||
display(b);
|
||||
|
||||
if (arg_iterations && iteration >= arg_iterations)
|
||||
break;
|
||||
return 0;
|
||||
|
||||
if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */
|
||||
fputs("\n", stdout);
|
||||
@@ -1009,8 +977,7 @@ static int run(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case 'q':
|
||||
quit = true;
|
||||
break;
|
||||
return 0;
|
||||
|
||||
case 'p':
|
||||
arg_order = ORDER_PATH;
|
||||
@@ -1103,8 +1070,42 @@ static int run(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
static int run(int argc, char *argv[]) {
|
||||
_cleanup_free_ char *root = NULL;
|
||||
CGroupMask mask;
|
||||
int r;
|
||||
|
||||
log_setup();
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
r = cg_mask_supported(&mask);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to determine supported controllers: %m");
|
||||
|
||||
/* honor user selection unless pids controller is unavailable */
|
||||
PidsCount possible_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_ALL_PROCESSES;
|
||||
arg_count = MIN(possible_count, arg_count);
|
||||
|
||||
if (arg_recursive_unset && arg_count == COUNT_PIDS)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k.");
|
||||
|
||||
r = show_cgroup_get_path_and_warn(arg_machine, arg_root, &root);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get root control group path: %m");
|
||||
log_debug("CGroup path: %s", root);
|
||||
|
||||
signal(SIGWINCH, columns_lines_cache_reset);
|
||||
|
||||
if (arg_iterations == UINT_MAX)
|
||||
arg_iterations = on_tty() ? 0 : 1;
|
||||
|
||||
return loop(root);
|
||||
}
|
||||
|
||||
DEFINE_MAIN_FUNCTION(run);
|
||||
|
||||
Reference in New Issue
Block a user