Skip to content

Commit

Permalink
Fix the sizing of the Async Reactors.
Browse files Browse the repository at this point in the history
Adjust the size of the reactor accroding the limit of opens files.
Closing #765.

(cherry picked from commit 2efdbf8)
  • Loading branch information
bogdan-iancu committed Jan 28, 2016
1 parent bbaf821 commit bce5306
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
26 changes: 17 additions & 9 deletions daemonize.c
Expand Up @@ -453,7 +453,7 @@ int do_suid(const int uid, const int gid)
* \param target target that should be reached
* \return return 0 on success, -1 on error
*/
int increase_open_fds(unsigned int target)
int set_open_fds_limit(void)
{
struct rlimit lim, orig;

Expand All @@ -465,18 +465,23 @@ int increase_open_fds(unsigned int target)
orig=lim;
LM_DBG("current open file limits: %lu/%lu\n",
(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
/* nothing to do */
if (open_files_limit<=0) {
/* if not set from cfg, we just read the existing value */
open_files_limit = lim.rlim_cur;
goto done;
else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
lim.rlim_cur=target; /* increase soft limit to target */
}else{
}
if ((lim.rlim_cur==RLIM_INFINITY) || (open_files_limit<=lim.rlim_cur))
/* nothing to do (we do no reduce the limit) */
goto done;
if ((lim.rlim_max==RLIM_INFINITY) || (open_files_limit<=lim.rlim_max)) {
lim.rlim_cur=open_files_limit; /* increase soft limit to target */
} else {
/* more than the hard limit */
LM_INFO("trying to increase the open file limit"
" past the hard limit (%ld -> %d)\n",
(unsigned long)lim.rlim_max, target);
lim.rlim_max=target;
lim.rlim_cur=target;
(unsigned long)lim.rlim_max, open_files_limit);
lim.rlim_max=open_files_limit;
lim.rlim_cur=open_files_limit;
}
LM_DBG("increasing open file limits to: %lu/%lu\n",
(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
Expand All @@ -493,11 +498,14 @@ int increase_open_fds(unsigned int target)
if (setrlimit(RLIMIT_NOFILE, &lim)==0){
LM_CRIT("maximum number of file descriptors increased to"
" %u\n",(unsigned)orig.rlim_max);
open_files_limit = orig.rlim_max;
goto done;
}
}
goto error;
}
done:
LM_DBG("open files limit set to %d\n",open_files_limit);
return 0;
error:
return -1;
Expand Down
2 changes: 1 addition & 1 deletion daemonize.h
Expand Up @@ -29,7 +29,7 @@

int daemonize(char* name, int * own_pgid);
int do_suid(const int uid, const int gid);
int increase_open_fds(unsigned int target);
int set_open_fds_limit(void);
int set_core_dump(int enable, unsigned int size);

int send_status_code(char val);
Expand Down
8 changes: 3 additions & 5 deletions main.c
Expand Up @@ -1154,11 +1154,9 @@ int main(int argc, char** argv)

if (disable_core_dump) set_core_dump(0, 0);
else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
if (open_files_limit>0){
if(increase_open_fds(open_files_limit)<0){
LM_ERR("ERROR: error could not increase file limits\n");
goto error;
}
if(set_open_fds_limit()<0){
LM_ERR("ERROR: error could not increase file limits\n");
goto error;
}

/* print OpenSIPS version to log for history tracking */
Expand Down
8 changes: 2 additions & 6 deletions net/net_tcp.c
Expand Up @@ -102,7 +102,6 @@ int tcp_listen_backlog=DEFAULT_TCP_LISTEN_BACKLOG;
/*!< by default choose the best method */
enum poll_types tcp_poll_method=0;
int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
int tcp_max_fd_no=0;
/* number of TCP workers */
int tcp_children_no = CHILD_NO;
/* Max number of seconds that we except a full SIP message
Expand Down Expand Up @@ -1454,7 +1453,7 @@ static void tcp_main_server(void)

/* we run in a separate, dedicated process, with its own reactor
* (reactors are per process) */
if (init_worker_reactor("TCP_main", tcp_max_fd_no, RCT_PRIO_MAX)<0)
if (init_worker_reactor("TCP_main", open_files_limit, RCT_PRIO_MAX)<0)
goto error;

/* now start watching all the fds*/
Expand Down Expand Up @@ -1683,9 +1682,6 @@ int tcp_start_processes(int *chd_rank, int *startup_done)
if ( is_tcp_based_proto(n) )
for(si=protos[n].listeners; si ; si=si->next,r++ );

tcp_max_fd_no=counted_processes*2 + r - 1/*timer*/ + 3/*stdin/out/err*/;
tcp_max_fd_no+=tcp_max_connections;

if (register_tcp_load_stat( &load_p )!=0) {
LM_ERR("failed to init tcp load statistic\n");
goto error;
Expand Down Expand Up @@ -1739,7 +1735,7 @@ int tcp_start_processes(int *chd_rank, int *startup_done)

report_conditional_status( (1), 0);

tcp_worker_proc( reader_fd[1], tcp_max_fd_no);
tcp_worker_proc( reader_fd[1] );
exit(-1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions net/net_tcp_proc.c
Expand Up @@ -284,11 +284,11 @@ static inline void tcp_receive_timeout(void)



void tcp_worker_proc( int unix_sock, int max_fd )
void tcp_worker_proc( int unix_sock)
{
/* init reactor for TCP worker */
tcpmain_sock=unix_sock; /* init com. socket */
if ( init_worker_reactor( "TCP_worker", max_fd, RCT_PRIO_MAX)<0 ) {
if ( init_worker_reactor("TCP_worker",open_files_limit,RCT_PRIO_MAX)<0 ) {
goto error;
}

Expand Down
2 changes: 1 addition & 1 deletion net/net_tcp_proc.h
Expand Up @@ -27,6 +27,6 @@
#define _NET_net_tcp_proc_h

/* Loop implementing a TCP worker */
void tcp_worker_proc( int fd, int max_fd);
void tcp_worker_proc( int fd);

#endif
2 changes: 1 addition & 1 deletion net/net_udp.c
Expand Up @@ -287,7 +287,7 @@ inline static int handle_io(struct fd_map* fm, int idx,int event_type)
int udp_rcv_loop( struct socket_info *si )
{
/* create the reactor for UDP proc */
if ( init_worker_reactor( "UDP_worker", 100/*max_fd*/, RCT_PRIO_MAX)<0 ) {
if ( init_worker_reactor("UDP_worker",open_files_limit,RCT_PRIO_MAX)<0 ) {
LM_ERR("failed to init reactor\n");
goto error;
}
Expand Down

0 comments on commit bce5306

Please sign in to comment.