]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mworker/cli: extract worker "show proc" row printer
authorAlexander Stephan <alexander.stephan@sap.com>
Tue, 30 Dec 2025 11:04:10 +0000 (11:04 +0000)
committerWilliam Lallemand <wlallemand@haproxy.com>
Mon, 5 Jan 2026 07:59:45 +0000 (08:59 +0100)
Introduce cli_append_worker_row() to centralize formatting of a single
worker row. Also, replace duplicated row-printing code in both current
and old workers loops with the helper. Motivation: Reduces LOC and
improves readability by removing duplication.

src/mworker.c

index 83c438c350ce8c4ab2c3ad902e5f9b7b744a5771..cdf9a9332c26ab36ac44622061f63d257519655f 100644 (file)
@@ -810,6 +810,23 @@ struct cli_showproc_ctx {
        int next_reload; /* reload number to resume from, 0 = from the beginning  */
 };
 
+/* Append a single worker row to trash (shared between current/old sections) */
+static void cli_append_worker_row(struct cli_showproc_ctx *ctx, struct mworker_proc *child, time_t tv_sec)
+{
+       char *uptime = NULL;
+       int up = tv_sec - child->timestamp;
+
+       if (up < 0) /* must never be negative because of clock drift */
+               up = 0;
+
+       memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
+       chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
+       if (ctx->debug)
+               chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
+       chunk_appendf(&trash, "\n");
+       ha_free(&uptime);
+}
+
 /*  Displays workers and processes  */
 static int cli_io_handler_show_proc(struct appctx *appctx)
 {
@@ -851,10 +868,6 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
                if (ctx->next_reload != 0)
                        continue;
 
-               up = date.tv_sec - child->timestamp;
-               if (up < 0) /* must never be negative because of clock drift */
-                       up = 0;
-
                if (!(child->options & PROC_O_TYPE_WORKER))
                        continue;
 
@@ -862,12 +875,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
                        old++;
                        continue;
                }
-               memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
-               chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
-               if (ctx->debug)
-                       chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
-               chunk_appendf(&trash, "\n");
-               ha_free(&uptime);
+               cli_append_worker_row(ctx, child, date.tv_sec);
        }
 
        if (applet_putchk(appctx, &trash) == -1)
@@ -878,10 +886,6 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
                if (ctx->next_reload == 0)
                        chunk_appendf(&trash, "# old workers\n");
                list_for_each_entry(child, &proc_list, list) {
-                       up = date.tv_sec - child->timestamp;
-                       if (up <= 0) /* must never be negative because of clock drift */
-                               up = 0;
-
                        /* If we're resuming, skip entries that were already printed (reload >= ctx->next_reload) */
                        if (ctx->next_reload && child->reloads >= ctx->next_reload)
                                continue;
@@ -890,12 +894,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
                                continue;
 
                        if (child->options & PROC_O_LEAVING) {
-                               memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
-                               chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
-                               if (ctx->debug)
-                                       chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
-                               chunk_appendf(&trash, "\n");
-                               ha_free(&uptime);
+                               cli_append_worker_row(ctx, child, date.tv_sec);
 
                                /* Try to flush so we can resume after this reload on next page if the buffer is full. */
                                if (applet_putchk(appctx, &trash) == -1) {