MEDIUM: global: remove the relative_pid from global and mworker

The relative_pid is always 1. In mworker mode we also have a
child->relative_pid which is always equalt relative_pid, except for a
master (0) or external process (-1), but these types are usually tested
for, except for one place that was amended to carefully check for the
PROC_O_TYPE_WORKER option.

Changes were pretty limited as most usages of relative_pid were for
designating a process in stats output and peers protocol.
This commit is contained in:
Willy Tarreau 2021-06-15 09:08:18 +02:00
parent 06987f4238
commit e8422bf56b
9 changed files with 25 additions and 37 deletions

View File

@ -28,7 +28,6 @@
extern const char *build_features; extern const char *build_features;
extern struct global global; extern struct global global;
extern int pid; /* current process id */ extern int pid; /* current process id */
extern int relative_pid; /* process id starting at 1 */
extern int actconn; /* # of active sessions */ extern int actconn; /* # of active sessions */
extern int listeners; extern int listeners;
extern int jobs; /* # of active jobs (listeners, sessions, open devices) */ extern int jobs; /* # of active jobs (listeners, sessions, open devices) */

View File

@ -39,7 +39,6 @@ struct mworker_proc {
char *path; char *path;
char *version; char *version;
int ipc_fd[2]; /* 0 is master side, 1 is worker side */ int ipc_fd[2]; /* 0 is master side, 1 is worker side */
int relative_pid;
int reloads; int reloads;
int timestamp; int timestamp;
struct server *srv; /* the server entry in the master proxy */ struct server *srv; /* the server entry in the master proxy */

View File

@ -2159,12 +2159,10 @@ static int pcli_prefix_to_pid(const char *prefix)
list_for_each_entry(child, &proc_list, list) { list_for_each_entry(child, &proc_list, list) {
if (!(child->options & PROC_O_TYPE_WORKER)) if (!(child->options & PROC_O_TYPE_WORKER))
continue; continue;
if (child->relative_pid == proc_pid){ if (child->reloads == 0)
if (child->reloads == 0) return child->pid;
return child->pid; else if (chosen == NULL || child->reloads < chosen->reloads)
else if (chosen == NULL || child->reloads < chosen->reloads) chosen = child;
chosen = child;
}
} }
if (chosen) if (chosen)
return chosen->pid; return chosen->pid;
@ -2721,7 +2719,7 @@ int mworker_cli_proxy_create()
/* we don't know the new pid yet */ /* we don't know the new pid yet */
if (child->pid == -1) if (child->pid == -1)
memprintf(&msg, "cur-%d", child->relative_pid); memprintf(&msg, "cur-%d", 1);
else else
memprintf(&msg, "old-%d", child->pid); memprintf(&msg, "old-%d", child->pid);

View File

@ -157,7 +157,6 @@ const char *build_features = "";
/* list of config files */ /* list of config files */
static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles); static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
int pid; /* current process id */ int pid; /* current process id */
int relative_pid = 1; /* process id starting at 1 */
volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */ volatile unsigned long sleeping_thread_mask = 0; /* Threads that are about to sleep in poll() */
volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */ volatile unsigned long stopping_thread_mask = 0; /* Threads acknowledged stopping */
@ -821,7 +820,6 @@ static void mworker_loop()
some SIGCHLD were lost */ some SIGCHLD were lost */
global.nbthread = 1; global.nbthread = 1;
relative_pid = 1;
#ifdef USE_THREAD #ifdef USE_THREAD
tid_bit = 1; tid_bit = 1;
@ -1909,7 +1907,6 @@ static void init(int argc, char **argv)
} }
tmproc->options |= PROC_O_TYPE_MASTER; /* master */ tmproc->options |= PROC_O_TYPE_MASTER; /* master */
tmproc->reloads = 0; tmproc->reloads = 0;
tmproc->relative_pid = 0;
tmproc->pid = pid; tmproc->pid = pid;
tmproc->timestamp = start_date.tv_sec; tmproc->timestamp = start_date.tv_sec;
tmproc->ipc_fd[0] = -1; tmproc->ipc_fd[0] = -1;
@ -1930,7 +1927,6 @@ static void init(int argc, char **argv)
tmproc->pid = -1; tmproc->pid = -1;
tmproc->reloads = 0; tmproc->reloads = 0;
tmproc->timestamp = -1; tmproc->timestamp = -1;
tmproc->relative_pid = 1;
tmproc->ipc_fd[0] = -1; tmproc->ipc_fd[0] = -1;
tmproc->ipc_fd[1] = -1; tmproc->ipc_fd[1] = -1;
@ -3148,7 +3144,7 @@ int main(int argc, char **argv)
exit(1); /* there has been an error */ exit(1); /* there has been an error */
} }
else if (ret == 0) { /* child breaks here */ else if (ret == 0) { /* child breaks here */
ha_random_jump96(relative_pid); ha_random_jump96(1);
} }
else { /* parent here */ else { /* parent here */
in_parent = 1; in_parent = 1;
@ -3161,11 +3157,10 @@ int main(int argc, char **argv)
if (global.mode & MODE_MWORKER) { if (global.mode & MODE_MWORKER) {
struct mworker_proc *child; struct mworker_proc *child;
ha_notice("New worker #%d (%d) forked\n", relative_pid, ret); ha_notice("New worker #%d (%d) forked\n", 1, ret);
/* find the right mworker_proc */ /* find the right mworker_proc */
list_for_each_entry(child, &proc_list, list) { list_for_each_entry(child, &proc_list, list) {
if (child->relative_pid == relative_pid && if (child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
child->timestamp = now.tv_sec; child->timestamp = now.tv_sec;
child->pid = ret; child->pid = ret;
child->version = strdup(haproxy_version); child->version = strdup(haproxy_version);
@ -3242,7 +3237,7 @@ int main(int argc, char **argv)
* the bind_proc */ * the bind_proc */
if (child->ipc_fd[0] >= 0) if (child->ipc_fd[0] >= 0)
close(child->ipc_fd[0]); close(child->ipc_fd[0]);
if (child->relative_pid == relative_pid && if (child->options & PROC_O_TYPE_WORKER &&
child->reloads == 0) { child->reloads == 0) {
/* keep this struct if this is our pid */ /* keep this struct if this is our pid */
proc_self = child; proc_self = child;

View File

@ -156,7 +156,6 @@ int cfg_parse_program(const char *file, int linenum, char **args, int kwm)
ext_child->path = NULL; ext_child->path = NULL;
ext_child->id = NULL; ext_child->id = NULL;
ext_child->pid = -1; ext_child->pid = -1;
ext_child->relative_pid = -1;
ext_child->reloads = 0; ext_child->reloads = 0;
ext_child->timestamp = -1; ext_child->timestamp = -1;
ext_child->ipc_fd[0] = -1; ext_child->ipc_fd[0] = -1;

View File

@ -122,7 +122,7 @@ void mworker_proc_list_to_env()
type = 'w'; type = 'w';
if (child->pid > -1) if (child->pid > -1)
memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "", child->version); memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, 1, child->reloads, child->timestamp, child->id ? child->id : "", child->version);
} }
if (msg) if (msg)
setenv("HAPROXY_PROCESSES", msg, 1); setenv("HAPROXY_PROCESSES", msg, 1);
@ -173,8 +173,6 @@ int mworker_env_to_proc_list()
child->ipc_fd[0] = atoi(subtoken+3); child->ipc_fd[0] = atoi(subtoken+3);
} else if (strncmp(subtoken, "pid=", 4) == 0) { } else if (strncmp(subtoken, "pid=", 4) == 0) {
child->pid = atoi(subtoken+4); child->pid = atoi(subtoken+4);
} else if (strncmp(subtoken, "rpid=", 5) == 0) {
child->relative_pid = atoi(subtoken+5);
} else if (strncmp(subtoken, "reloads=", 8) == 0) { } else if (strncmp(subtoken, "reloads=", 8) == 0) {
/* we reloaded this process once more */ /* we reloaded this process once more */
child->reloads = atoi(subtoken+8) + 1; child->reloads = atoi(subtoken+8) + 1;
@ -298,9 +296,9 @@ restart_wait:
if (!(child->options & PROC_O_LEAVING)) { if (!(child->options & PROC_O_LEAVING)) {
if (child->options & PROC_O_TYPE_WORKER) { if (child->options & PROC_O_TYPE_WORKER) {
if (status < 128) if (status < 128)
ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit"); ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", 1, exitpid, status, "Exit");
else else
ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128)); ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", 1, exitpid, status, strsignal(status - 128));
} }
else if (child->options & PROC_O_TYPE_PROG) else if (child->options & PROC_O_TYPE_PROG)
ha_alert("Current program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); ha_alert("Current program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
@ -315,7 +313,7 @@ restart_wait:
exitcode = status; exitcode = status;
} else { } else {
if (child->options & PROC_O_TYPE_WORKER) { if (child->options & PROC_O_TYPE_WORKER) {
ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", 1, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
delete_oldpid(exitpid); delete_oldpid(exitpid);
} else if (child->options & PROC_O_TYPE_PROG) { } else if (child->options & PROC_O_TYPE_PROG) {
ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
@ -482,7 +480,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
continue; continue;
} }
memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", child->pid, "worker", child->relative_pid, child->reloads, uptime, child->version); chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", child->pid, "worker", 1, child->reloads, uptime, child->version);
ha_free(&uptime); ha_free(&uptime);
} }
@ -499,7 +497,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
continue; continue;
if (child->options & PROC_O_LEAVING) { if (child->options & PROC_O_LEAVING) {
memprintf(&msg, "[was: %u]", child->relative_pid); memprintf(&msg, "[was: %u]", 1);
memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, "worker", msg, child->reloads, uptime, child->version); chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, "worker", msg, child->reloads, uptime, child->version);
ha_free(&uptime); ha_free(&uptime);

View File

@ -567,7 +567,7 @@ static int peer_prepare_hellomsg(char *msg, size_t size, struct peer_prep_params
min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER; min_ver = (peer->flags & PEER_F_DWNGRD) ? PEER_DWNGRD_MINOR_VER : PEER_MINOR_VER;
/* Prepare headers */ /* Prepare headers */
ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n", ret = snprintf(msg, size, PEER_SESSION_PROTO_NAME " %u.%u\n%s\n%s %d %d\n",
PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), relative_pid); PEER_MAJOR_VER, min_ver, peer->id, localpeer, (int)getpid(), 1);
if (ret >= size) if (ret >= size)
return 0; return 0;

View File

@ -3950,7 +3950,7 @@ static int
smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private) smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
{ {
smp->data.type = SMP_T_SINT; smp->data.type = SMP_T_SINT;
smp->data.u.sint = relative_pid; smp->data.u.sint = 1;
return 1; return 1;
} }

View File

@ -87,7 +87,7 @@ const struct name_desc info_fields[INF_TOTAL_FIELDS] = {
[INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" }, [INF_RELEASE_DATE] = { .name = "Release_date", .desc = "Date of latest source code update" },
[INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" }, [INF_NBTHREAD] = { .name = "Nbthread", .desc = "Number of started threads (global.nbthread)" },
[INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" }, [INF_NBPROC] = { .name = "Nbproc", .desc = "Number of started worker processes (historical, always 1)" },
[INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1..Nbproc)" }, [INF_PROCESS_NUM] = { .name = "Process_num", .desc = "Relative worker process number (1)" },
[INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" }, [INF_PID] = { .name = "Pid", .desc = "This worker process identifier for the system" },
[INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" }, [INF_UPTIME] = { .name = "Uptime", .desc = "How long ago this worker process was started (days+hours+minutes+seconds)" },
[INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" }, [INF_UPTIME_SEC] = { .name = "Uptime_sec", .desc = "How long ago this worker process was started (seconds)" },
@ -183,7 +183,7 @@ const struct name_desc stat_fields[ST_F_TOTAL_FIELDS] = {
[ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" }, [ST_F_LASTCHG] = { .name = "lastchg", .desc = "How long ago the last server state changed, in seconds" },
[ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" }, [ST_F_DOWNTIME] = { .name = "downtime", .desc = "Total time spent in DOWN state, for server or backend" },
[ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" }, [ST_F_QLIMIT] = { .name = "qlimit", .desc = "Limit on the number of connections in queue, for servers only (maxqueue argument)" },
[ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1..nbproc)" }, [ST_F_PID] = { .name = "pid", .desc = "Relative worker process number (1)" },
[ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" }, [ST_F_IID] = { .name = "iid", .desc = "Frontend or Backend numeric identifier ('id' setting)" },
[ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" }, [ST_F_SID] = { .name = "sid", .desc = "Server numeric identifier ('id' setting)" },
[ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" }, [ST_F_THROTTLE] = { .name = "throttle", .desc = "Throttling ratio applied to a server's maxconn and weight during the slowstart period (0 to 100%)" },
@ -1696,7 +1696,7 @@ int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len,
metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN"); metric = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
break; break;
case ST_F_PID: case ST_F_PID:
metric = mkf_u32(FO_KEY, relative_pid); metric = mkf_u32(FO_KEY, 1);
break; break;
case ST_F_IID: case ST_F_IID:
metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
@ -1911,7 +1911,7 @@ int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]); metric = mkf_str(FO_STATUS, li_status_st[get_li_status(l)]);
break; break;
case ST_F_PID: case ST_F_PID:
metric = mkf_u32(FO_KEY, relative_pid); metric = mkf_u32(FO_KEY, 1);
break; break;
case ST_F_IID: case ST_F_IID:
metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
@ -2259,7 +2259,7 @@ int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue); metric = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
break; break;
case ST_F_PID: case ST_F_PID:
metric = mkf_u32(FO_KEY, relative_pid); metric = mkf_u32(FO_KEY, 1);
break; break;
case ST_F_IID: case ST_F_IID:
metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
@ -2638,7 +2638,7 @@ int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int le
metric = mkf_u32(FN_COUNTER, be_downtime(px)); metric = mkf_u32(FN_COUNTER, be_downtime(px));
break; break;
case ST_F_PID: case ST_F_PID:
metric = mkf_u32(FO_KEY, relative_pid); metric = mkf_u32(FO_KEY, 1);
break; break;
case ST_F_IID: case ST_F_IID:
metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid); metric = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
@ -3339,7 +3339,7 @@ static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *u
(appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "", (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
(appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "", (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
(appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "", (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
pid, relative_pid, 1, global.nbthread, pid, 1, 1, global.nbthread,
up / 86400, (up % 86400) / 3600, up / 86400, (up % 86400) / 3600,
(up % 3600) / 60, (up % 60), (up % 3600) / 60, (up % 60),
global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
@ -4320,7 +4320,7 @@ int stats_fill_info(struct field *info, int len, uint flags)
info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread); info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1); info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, 1);
info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid); info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, 1);
info[INF_PID] = mkf_u32(FO_STATUS, pid); info[INF_PID] = mkf_u32(FO_STATUS, pid);
info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out)); info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));