Line | Count | Source (jump to first uncovered line) |
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 | | |
32 | | /* one reactor per process variable */ |
33 | | io_wait_h _worker_io; |
34 | | /* max number of fds per reactor */ |
35 | | unsigned int reactor_size = 0; |
36 | | |
37 | 0 | #define FD_MEM_PERCENT 10 |
38 | | |
39 | | int init_reactor_size(void) |
40 | 0 | { |
41 | 0 | struct rlimit lim; |
42 | 0 | int n, pc; |
43 | 0 | char *mem_type; |
44 | 0 | unsigned long mem_size; |
45 | |
|
46 | | #if defined(PKG_MALLOC) |
47 | | mem_type = "pkg"; |
48 | | mem_size = pkg_mem_size; |
49 | | #else |
50 | 0 | mem_type = "system"; |
51 | 0 | mem_size = 2*1024*1024*1024UL; // assume 2Gb per process to avoid reaching limits below |
52 | 0 | #endif |
53 | |
|
54 | 0 | n = sizeof(struct fd_map) + sizeof(struct pollfd); |
55 | |
|
56 | 0 | if (open_files_limit > 0) { |
57 | | /* the fd limit was explicitly set, so just follow but only warn |
58 | | * if too much memory is to consumed by reactor */ |
59 | 0 | pc = 100 * n * open_files_limit / mem_size; |
60 | 0 | if (pc >= 80) { |
61 | 0 | LM_ERR("required memory for a %d files reactor is over 80%% of " |
62 | 0 | "the configured %s mem (%luMb)\n", |
63 | 0 | open_files_limit, mem_type, mem_size/1024/1024); |
64 | 0 | LM_ERR("Please consider increasing the %s memory or reduce the " |
65 | 0 | "limit of open files...Exiting\n", mem_type); |
66 | 0 | return -1; |
67 | 0 | } else if (pc >= 50) { |
68 | 0 | LM_WARN("required memory for a %d files reactor is over 50%% of " |
69 | 0 | "the configured %s mem (%luMb)\n", |
70 | 0 | open_files_limit, mem_type, mem_size/1024/1024); |
71 | 0 | LM_WARN("%s memory may not be enough at runtime (consider " |
72 | 0 | "increasing it), still continuing\n", mem_type); |
73 | 0 | } |
74 | | /* seems to have enough mem -> size the reactor based on open files */ |
75 | 0 | reactor_size = open_files_limit; |
76 | 0 | } else { |
77 | | /* auto detect the limit of open files */ |
78 | 0 | if (getrlimit(RLIMIT_NOFILE, &lim)<0){ |
79 | 0 | LM_ERR("cannot get the maximum number of file descriptors: %s\n", |
80 | 0 | strerror(errno)); |
81 | 0 | return -1; |
82 | 0 | } |
83 | | |
84 | | /* calculate the size to fit into 10% of mem */ |
85 | 0 | reactor_size = mem_size / n * FD_MEM_PERCENT / 100; |
86 | |
|
87 | 0 | if (reactor_size < lim.rlim_cur) { |
88 | 0 | LM_WARN("shrinking reactor size from %lu (autodetected via rlimit) " |
89 | 0 | "to %d (limited by memory of %d%% from %luMb)\n", |
90 | 0 | (unsigned long)lim.rlim_cur, reactor_size, FD_MEM_PERCENT, mem_size/1024/1024); |
91 | 0 | LM_WARN("use 'open_files_limit' to enforce other limit or " |
92 | 0 | "increase %s memory\n", mem_type); |
93 | 0 | } else { |
94 | | /* enouhg memory, use as limit the fd limit */ |
95 | 0 | reactor_size = lim.rlim_cur; |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | 0 | LM_INFO("reactor size %d (using up to %.2fMb of memory per process)\n", |
100 | 0 | reactor_size, 1.0 * n * reactor_size / 1024 / 1024); |
101 | |
|
102 | 0 | return 0; |
103 | 0 | } |
104 | | |