|
23 | 23 | * 2014-08-23 created (bogdan)
|
24 | 24 | */
|
25 | 25 |
|
| 26 | +#include <sys/time.h> |
| 27 | +#include <sys/resource.h> |
| 28 | + |
26 | 29 | #include "io_wait.h"
|
| 30 | +#include "globals.h" |
27 | 31 |
|
28 | 32 | /* one reactor per process variable */
|
29 | 33 | io_wait_h _worker_io;
|
| 34 | +/* max number of fds per reactor */ |
| 35 | +unsigned int reactor_size = 0; |
| 36 | + |
| 37 | +#define FD_MEM_PERCENT 10 |
| 38 | + |
| 39 | +int init_reactor_size(void) |
| 40 | +{ |
| 41 | + struct rlimit lim; |
| 42 | + int n, pc; |
| 43 | + |
| 44 | + n = sizeof(struct fd_map) + sizeof(struct pollfd); |
| 45 | + |
| 46 | + if (open_files_limit>0) { |
| 47 | + |
| 48 | + /* the fd limit was explicitly set, so just follow but only warn |
| 49 | + * if too much memory is to consumed by reactor */ |
| 50 | + pc = 100*n*open_files_limit / pkg_mem_size; |
| 51 | + if (pc>=80) { |
| 52 | + LM_ERR("required memory for a %d files reactor is over 80%% of" |
| 53 | + " the configured pkg mem (%luMb)\n", |
| 54 | + open_files_limit, pkg_mem_size); |
| 55 | + LM_ERR("Please consider increasing the pkg memory or reduce the" |
| 56 | + " limit of open files...Exiting\n"); |
| 57 | + return -1; |
| 58 | + } else if (pc>=50) { |
| 59 | + LM_WARN("required memory for a %d files reactor is over 50%% of" |
| 60 | + " the configured pkg mem (%luMb)\n", |
| 61 | + open_files_limit, pkg_mem_size); |
| 62 | + LM_WARN("PKG memory may not be enough at runtime (consider " |
| 63 | + "increasing it), still continuing\n"); |
| 64 | + } |
| 65 | + /* seems to have enough mem -> size the reactor based on open files */ |
| 66 | + reactor_size = open_files_limit; |
| 67 | + |
| 68 | + } else { |
| 69 | + |
| 70 | + /* auto detect the limit of open files */ |
| 71 | + if (getrlimit(RLIMIT_NOFILE, &lim)<0){ |
| 72 | + LM_ERR("cannot get the maximum number of file descriptors: %s\n", |
| 73 | + strerror(errno)); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + /* calculate the size to fit into 10% PKG mem */ |
| 78 | + reactor_size = pkg_mem_size * FD_MEM_PERCENT / (100*n); |
| 79 | + |
| 80 | + if (reactor_size<lim.rlim_cur) { |
| 81 | + LM_WARN("shrinking reactor size from %lu (autodetected via rlimit)" |
| 82 | + " to %d (limited by memory of %d%% from %luMb)\n", |
| 83 | + lim.rlim_cur,reactor_size,FD_MEM_PERCENT,pkg_mem_size); |
| 84 | + LM_WARN("use 'open_files_limit' to enforce other limit or " |
| 85 | + "increase PKG memory\n"); |
| 86 | + } else { |
| 87 | + /* enouhg memory, use as limit the fd limit */ |
| 88 | + reactor_size = lim.rlim_cur; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + LM_DBG("using reactor size %d\n",reactor_size); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
30 | 96 |
|
0 commit comments