Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2014 OpenSIPS Solutions |
3 | | * |
4 | | * This file is part of opensips, a free SIP server. |
5 | | * |
6 | | * opensips is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * opensips is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | | * |
20 | | * |
21 | | * history: |
22 | | * --------- |
23 | | * 2014-08-23 created (bogdan) |
24 | | */ |
25 | | |
26 | | #include <sys/time.h> |
27 | | #include <sys/resource.h> |
28 | | |
29 | | #include "io_wait.h" |
30 | | #include "globals.h" |
31 | | #include "reactor_proc.h" |
32 | | |
33 | | /* one reactor per process variable */ |
34 | | io_wait_h _worker_io; |
35 | | /* max number of fds per reactor */ |
36 | | unsigned int reactor_size = 0; |
37 | | |
38 | | /* the reactor timeout (ms) to be used by the OpenSIPS worker procs; |
39 | | * By "worker" we understand here processes handling SIP traffic */ |
40 | | unsigned int worker_reactor_timeout = REACTOR_PROC_TIMEOUT; |
41 | | |
42 | 0 | #define FD_MEM_PERCENT 10 |
43 | | |
44 | | int init_reactor_size(void) |
45 | 0 | { |
46 | 0 | struct rlimit lim; |
47 | 0 | int n, pc; |
48 | 0 | char *mem_type; |
49 | 0 | unsigned long mem_size; |
50 | |
|
51 | | #if defined(PKG_MALLOC) |
52 | | mem_type = "pkg"; |
53 | | mem_size = pkg_mem_size; |
54 | | #else |
55 | 0 | mem_type = "system"; |
56 | 0 | mem_size = 2*1024*1024*1024UL; // assume 2Gb per process to avoid reaching limits below |
57 | 0 | #endif |
58 | |
|
59 | 0 | n = sizeof(struct fd_map) + sizeof(struct pollfd); |
60 | |
|
61 | 0 | if (open_files_limit > 0) { |
62 | | /* the fd limit was explicitly set, so just follow but only warn |
63 | | * if too much memory is to consumed by reactor */ |
64 | 0 | pc = 100 * n * open_files_limit / mem_size; |
65 | 0 | if (pc >= 80) { |
66 | 0 | LM_ERR("required memory for a %d files reactor is over 80%% of " |
67 | 0 | "the configured %s mem (%luMb)\n", |
68 | 0 | open_files_limit, mem_type, mem_size/1024/1024); |
69 | 0 | LM_ERR("Please consider increasing the %s memory or reduce the " |
70 | 0 | "limit of open files...Exiting\n", mem_type); |
71 | 0 | return -1; |
72 | 0 | } else if (pc >= 50) { |
73 | 0 | LM_WARN("required memory for a %d files reactor is over 50%% of " |
74 | 0 | "the configured %s mem (%luMb)\n", |
75 | 0 | open_files_limit, mem_type, mem_size/1024/1024); |
76 | 0 | LM_WARN("%s memory may not be enough at runtime (consider " |
77 | 0 | "increasing it), still continuing\n", mem_type); |
78 | 0 | } |
79 | | /* seems to have enough mem -> size the reactor based on open files */ |
80 | 0 | reactor_size = open_files_limit; |
81 | 0 | } else { |
82 | | /* auto detect the limit of open files */ |
83 | 0 | if (getrlimit(RLIMIT_NOFILE, &lim)<0){ |
84 | 0 | LM_ERR("cannot get the maximum number of file descriptors: %s\n", |
85 | 0 | strerror(errno)); |
86 | 0 | return -1; |
87 | 0 | } |
88 | | |
89 | | /* calculate the size to fit into 10% of mem */ |
90 | 0 | reactor_size = mem_size / n * FD_MEM_PERCENT / 100; |
91 | |
|
92 | 0 | if (reactor_size < lim.rlim_cur) { |
93 | 0 | LM_WARN("shrinking reactor size from %lu (autodetected via rlimit) " |
94 | 0 | "to %d (limited by memory of %d%% from %luMb)\n", |
95 | 0 | (unsigned long)lim.rlim_cur, reactor_size, FD_MEM_PERCENT, mem_size/1024/1024); |
96 | 0 | LM_WARN("use 'open_files_limit' to enforce other limit or " |
97 | 0 | "increase %s memory\n", mem_type); |
98 | 0 | } else { |
99 | | /* enouhg memory, use as limit the fd limit */ |
100 | 0 | reactor_size = lim.rlim_cur; |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | | /* not smaller than 100ms, but not higher than 1sec */ |
105 | 0 | if (worker_reactor_timeout<100) |
106 | 0 | worker_reactor_timeout = 100; |
107 | 0 | else if (worker_reactor_timeout>1000) |
108 | 0 | worker_reactor_timeout = 1000; |
109 | |
|
110 | 0 | LM_INFO("reactor size %d (using up to %.2fMb of memory per process)" |
111 | 0 | ", worker timeout %d ms\n", |
112 | 0 | reactor_size, 1.0 * n * reactor_size / 1024 / 1024, |
113 | 0 | worker_reactor_timeout); |
114 | |
|
115 | 0 | return 0; |
116 | 0 | } |
117 | | |