From d13e7a781fc6be5f003511c727c99807a9926815 Mon Sep 17 00:00:00 2001 From: Bogdan-Andrei Iancu Date: Sun, 3 Feb 2019 19:18:23 +0200 Subject: [PATCH] Small rework of the processes groups Better use types to tag the processes. When creating a proc group, push also the min/max for procs auto-scaling --- net/net_tcp.c | 4 ++-- net/net_udp.c | 5 +++-- pt.c | 21 +++++++++++++++------ pt.h | 16 +++++++++------- sr_module.c | 2 +- timer.c | 8 ++++---- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/net/net_tcp.c b/net/net_tcp.c index 64ee931a883..76b0f18a39f 100644 --- a/net/net_tcp.c +++ b/net/net_tcp.c @@ -1853,7 +1853,7 @@ int tcp_start_processes(int *chd_rank, int *startup_done) } (*chd_rank)++; - pid=internal_fork("SIP receiver TCP", 0, GROUP_TCP); + pid=internal_fork("SIP receiver TCP", 0, TYPE_TCP); if (pid<0){ LM_ERR("fork failed\n"); goto error; @@ -1916,7 +1916,7 @@ int tcp_start_listener(void) return 0; /* start the TCP manager process */ - if ( (pid=internal_fork( "TCP main", 0, GROUP_NONE))<0 ) { + if ( (pid=internal_fork( "TCP main", 0, TYPE_NONE))<0 ) { LM_CRIT("cannot fork tcp main process\n"); goto error; }else if (pid==0){ diff --git a/net/net_udp.c b/net/net_udp.c index 7d6452d43cf..bee774b714a 100644 --- a/net/net_udp.c +++ b/net/net_udp.c @@ -343,14 +343,15 @@ int udp_start_processes(int *chd_rank, int *startup_done) for(si=protos[p].listeners; si ; si=si->next ) { if (enable_dynamic_workers && - register_process_group( GROUP_UDP, si, NULL)!=0) + create_process_group( TYPE_UDP, si, si->workers_min, + si->workers_max, NULL)!=0) LM_ERR("failed to create group of UDP processes for <%.*s>, " "auto forking will not be possible\n", si->name.len, si->name.s); for (i=0;iworkers;i++) { (*chd_rank)++; - if ( (pid=internal_fork( "UDP receiver", 0, GROUP_UDP))<0 ) { + if ( (pid=internal_fork( "UDP receiver", 0, TYPE_UDP))<0 ) { LM_CRIT("cannot fork UDP process\n"); goto error; } else if (pid==0) { diff --git a/pt.c b/pt.c index 0c374f8e5d8..2526a87799f 100644 --- a/pt.c +++ b/pt.c @@ -170,7 +170,7 @@ static int register_process_stats(int process_no) /* This function is to be called only by the main process! * */ pid_t internal_fork(char *proc_desc, unsigned int flags, - enum processes_group group) + enum process_type type) { #define CHILD_COUNTER_STOP 656565656 static int process_counter = 1; @@ -224,7 +224,7 @@ pid_t internal_fork(char *proc_desc, unsigned int flags, process_no = process_counter; pt[process_no].pid = getpid(); pt[process_no].flags = flags; - pt[process_no].group = group; + pt[process_no].type = type; process_counter = CHILD_COUNTER_STOP; /* each children need a unique seed */ seed_child(seed); @@ -285,16 +285,20 @@ int count_init_children(int flags) struct process_group { - enum processes_group group; + enum process_type type; struct socket_info *si_filter; fork_new_process_f *fork_func; + unsigned int max_procs; + unsigned int min_procs; struct process_group *next; }; struct process_group *pg_head = NULL; -int register_process_group(enum processes_group group, - struct socket_info *si_filter, fork_new_process_f *f) +int create_process_group(enum process_type type, + struct socket_info *si_filter, + unsigned int min_procs, unsigned int max_procs, + fork_new_process_f *f) { struct process_group *pg, *it; @@ -304,8 +308,13 @@ int register_process_group(enum processes_group group, return -1; } - pg->group = group; + LM_DBG("registering group of processes type %d, socket filter %p, " + "process range [%d,%d]\n", type, si_filter, min_procs, max_procs ); + + pg->type = type; pg->si_filter = si_filter; + pg->max_procs = max_procs; + pg->min_procs = min_procs; pg->fork_func = f; pg->next = NULL; diff --git a/pt.h b/pt.h index 04d891142bc..e4a1cbe1864 100644 --- a/pt.h +++ b/pt.h @@ -36,14 +36,14 @@ #define MAX_PT_DESC 128 -enum processes_group { GROUP_NONE=0, GROUP_UDP, GROUP_TCP, - GROUP_TIMER, GROUP_MODULE}; +enum process_type { TYPE_NONE=0, TYPE_UDP, TYPE_TCP, + TYPE_TIMER, TYPE_MODULE}; struct process_table { /* the UNIX pid of this process */ int pid; - /* the group this proc is part of - optional, used by dynamic forking */ - enum processes_group group; + /* the type/group of this process - optional, used by dynamic forking */ + enum process_type type; /* name/description of the process (null terminated) */ char desc[MAX_PT_DESC]; /* various flags describing properties of this process */ @@ -86,7 +86,7 @@ int count_init_children(int flags); #define counted_processes (counted_processes_p?*counted_processes_p:0) pid_t internal_fork(char *proc_desc, unsigned int flags, - enum processes_group group); + enum process_type type); /* return processes pid */ inline static int my_pid(void) @@ -110,8 +110,10 @@ inline static int get_process_ID_by_PID(pid_t pid) typedef int (fork_new_process_f)(void *); -int register_process_group(enum processes_group group, - struct socket_info *si_filter, fork_new_process_f *f); +int create_process_group(enum process_type type, + struct socket_info *si_filter, + unsigned int min_procs, unsigned int max_procs, + fork_new_process_f *f); #endif diff --git a/sr_module.c b/sr_module.c index f13a1921c9d..3072d6c6af9 100644 --- a/sr_module.c +++ b/sr_module.c @@ -769,7 +769,7 @@ int start_module_procs(void) m->exports->procs[n].name, l, m->exports->name); x = internal_fork( m->exports->procs[n].name, ((m->exports->procs[n].flags&PROC_FLAG_HAS_IPC) ? - 0 : OSS_FORK_NO_IPC)|OSS_FORK_IS_EXTRA, GROUP_MODULE ); + 0 : OSS_FORK_NO_IPC)|OSS_FORK_IS_EXTRA, TYPE_MODULE ); if (x<0) { LM_ERR("failed to fork process \"%s\"/%d for module %s\n", m->exports->procs[n].name, l, m->exports->name); diff --git a/timer.c b/timer.c index f7bd8cd6ec1..8b9f35dcd82 100644 --- a/timer.c +++ b/timer.c @@ -586,7 +586,7 @@ int start_timer_processes(void) */ if ( (pid=internal_fork("time_keeper", - OSS_FORK_NO_IPC|OSS_FORK_NO_LOAD, GROUP_NONE))<0 ) { + OSS_FORK_NO_IPC|OSS_FORK_NO_LOAD, TYPE_NONE))<0 ) { LM_CRIT("cannot fork time keeper process\n"); goto error; } else if (pid==0) { @@ -599,7 +599,7 @@ int start_timer_processes(void) /* fork a timer-trigger process */ if ( (pid=internal_fork("timer", OSS_FORK_NO_IPC|OSS_FORK_NO_LOAD, - GROUP_NONE))<0 ) { + TYPE_NONE))<0 ) { LM_CRIT("cannot fork timer process\n"); goto error; } else if (pid==0) { @@ -678,12 +678,12 @@ int start_timer_extra_processes(int *chd_rank) pid_t pid; if (enable_dynamic_workers && - register_process_group( GROUP_TIMER, NULL, NULL)!=0) + create_process_group( TYPE_TIMER, NULL, 0, 0,NULL)!=0) LM_ERR("failed to create group of TIMER processes, " "auto forking will not be possible\n"); (*chd_rank)++; - if ( (pid=internal_fork( "Timer handler", 0, GROUP_TIMER))<0 ) { + if ( (pid=internal_fork( "Timer handler", 0, TYPE_TIMER))<0 ) { LM_CRIT("cannot fork Timer handler process\n"); return -1; } else if (pid==0) {