]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgtop: split out the main loop into a separate function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 14 Mar 2023 16:24:07 +0000 (17:24 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 14 Mar 2023 17:00:08 +0000 (18:00 +0100)
This way the initial setup is nicely separated from the main loop logic.

src/cgtop/cgtop.c

index 5d82d656bcad145770180a193c6f2f8d67ecb530..65d9941f7a0bc051e4eadf65457e323936de3536 100644 (file)
@@ -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);