]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cli: show master information in 'show proc'
authorWilliam Lallemand <wlallemand@haproxy.com>
Mon, 19 Nov 2018 17:46:18 +0000 (18:46 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 20 Nov 2018 03:43:54 +0000 (04:43 +0100)
Displays the master information in show proc.

include/types/global.h
src/cli.c
src/haproxy.c

index be4d7c5338ee5ceb048067faefb3304308459945..effc51c2c6e7ec35ffae89f20c644401a4f67619 100644 (file)
@@ -209,6 +209,8 @@ struct activity {
  */
 struct mworker_proc {
        int pid;
+       char type;  /* m(aster), w(orker)  */
+       /* 3 bytes hole here */
        int ipc_fd[2]; /* 0 is master side, 1 is worker side */
        int relative_pid;
        int reloads;
@@ -242,6 +244,7 @@ extern struct task *global_listener_queue_task;
 extern unsigned int warned;     /* bitfield of a few warnings to emit just once */
 extern volatile unsigned long sleeping_thread_mask;
 extern struct list proc_list; /* list of process in mworker mode */
+extern struct mworker_proc *proc_self; /* process structure of current process */
 
 /* bit values to go with "warned" above */
 #define WARN_BLOCK_DEPRECATED       0x00000001
index 24cb2a48f789124c724a8abf3ca9f4cf63274ba8..4356586bf6c04459d6f720cf6df9e7ed76a690e7 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -1399,6 +1399,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
        struct stream_interface *si = appctx->owner;
        struct mworker_proc *child;
        int old = 0;
+       int up = now.tv_sec - proc_self->timestamp;
 
        if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
                return 1;
@@ -1406,13 +1407,16 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
        chunk_reset(&trash);
 
        chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>");
-       chunk_appendf(&trash, "%-15u %-15s %-15u %-15s %s\n", getpid(), "master", 0, "-", "-");
+       chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
 
        /* displays current processes */
 
        chunk_appendf(&trash, "# workers\n");
        list_for_each_entry(child, &proc_list, list) {
-               int up = now.tv_sec - child->timestamp;
+               up = now.tv_sec - child->timestamp;
+
+               if (child->type != 'w')
+                       continue;
 
                if (child->reloads > 0) {
                        old++;
@@ -1426,7 +1430,11 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
        if (old) {
                chunk_appendf(&trash, "# old workers\n");
                list_for_each_entry(child, &proc_list, list) {
-                       int up = now.tv_sec - child->timestamp;
+                       up = now.tv_sec - child->timestamp;
+
+                       if (child->type != 'w')
+                               continue;
+
                        if (child->reloads > 0)
                                chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
                }
@@ -1707,6 +1715,8 @@ static int pcli_prefix_to_pid(const char *prefix)
                if (*errtol != '\0')
                        return -1;
                list_for_each_entry(child, &proc_list, list) {
+                       if (child->type != 'w')
+                               continue;
                        if (child->pid == proc_pid){
                                return child->pid;
                        }
@@ -1725,6 +1735,8 @@ static int pcli_prefix_to_pid(const char *prefix)
                /* chose the right process, the current one is the one with the
                 least number of reloads */
                list_for_each_entry(child, &proc_list, list) {
+                       if (child->type != 'w')
+                               continue;
                        if (child->relative_pid == proc_pid){
                                if (child->reloads == 0)
                                        return child->pid;
index 276652b9941be6db28f854cde0d6b23083534d02..225dc6933397ff1f4b37bee37279d100ee851e30 100644 (file)
@@ -219,7 +219,7 @@ struct list proc_list = LIST_HEAD_INIT(proc_list);
 
 int master = 0; /* 1 if in master, 0 if in child */
 
-struct mworker_proc *proc_self;
+struct mworker_proc *proc_self = NULL;
 
 /* list of the temporarily limited listeners because of lack of resource */
 struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
@@ -540,13 +540,10 @@ static void mworker_proc_list_to_env()
        struct mworker_proc *child;
 
        list_for_each_entry(child, &proc_list, list) {
-               if (msg)
-                       memprintf(&msg, "%s|type=worker;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp);
-               else
-                       memprintf(&msg, "type=worker;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp);
+               memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg ? msg : "", child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp);
        }
        if (msg)
-               setenv("HAPROXY_CHILDREN", msg, 1);
+               setenv("HAPROXY_PROCESSES", msg, 1);
 }
 
 /*
@@ -556,7 +553,7 @@ static void mworker_env_to_proc_list()
 {
        char *msg, *token = NULL, *s1;
 
-       msg = getenv("HAPROXY_CHILDREN");
+       msg = getenv("HAPROXY_PROCESSES");
        if (!msg)
                return;
 
@@ -573,7 +570,11 @@ static void mworker_env_to_proc_list()
 
                        token = NULL;
 
-                       if (strncmp(subtoken, "fd=", 3) == 0) {
+                       if (strncmp(subtoken, "type=", 5) == 0) {
+                               child->type = *(subtoken+5);
+                               if (child->type == 'm') /* we are in the master, assign it */
+                                       proc_self = child;
+                       } else if (strncmp(subtoken, "fd=", 3) == 0) {
                                child->ipc_fd[0] = atoi(subtoken+3);
                        } else if (strncmp(subtoken, "pid=", 4) == 0) {
                                child->pid = atoi(subtoken+4);
@@ -592,7 +593,7 @@ static void mworker_env_to_proc_list()
                        free(child);
        }
 
-       unsetenv("HAPROXY_CHILDREN");
+       unsetenv("HAPROXY_PROCESSES");
 }
 
 /*
@@ -1735,9 +1736,29 @@ static void init(int argc, char **argv)
        if (global.mode & MODE_MWORKER) {
                int proc;
                struct wordlist *it, *c;
+               struct mworker_proc *tmproc;
+
+               if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
+
+                       tmproc = malloc(sizeof(*tmproc));
+                       if (!tmproc) {
+                               ha_alert("Cannot allocate process structures.\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       tmproc->type = 'm'; /* master */
+                       tmproc->reloads = 0;
+                       tmproc->relative_pid = 0;
+                       tmproc->pid = pid;
+                       tmproc->timestamp = start_date.tv_sec;
+                       tmproc->ipc_fd[0] = -1;
+                       tmproc->ipc_fd[1] = -1;
+
+                       proc_self = tmproc;
+
+                       LIST_ADDQ(&proc_list, &tmproc->list);
+               }
 
                for (proc = 0; proc < global.nbproc; proc++) {
-                       struct mworker_proc *tmproc;
 
                        tmproc = malloc(sizeof(*tmproc));
                        if (!tmproc) {
@@ -1745,6 +1766,7 @@ static void init(int argc, char **argv)
                                exit(EXIT_FAILURE);
                        }
 
+                       tmproc->type = 'w'; /* worker */
                        tmproc->pid = -1;
                        tmproc->reloads = 0;
                        tmproc->timestamp = -1;