authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 15:09:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 15:09:13-04:00
logb564e7ca59818e4904fc421fc8b1914cefd79538
treefbbc0d1380bb77c8a6ac8f28570f4d4f076ab13b
parent2045b4d93240cd95eee7143f2cfc360eb63c5802
signaturelock-open Commit is signed but in an unrecognized format.

os: raise maximum file descriptor limit

Do a binary search for the maximum RLIMIT_NOFILE. Patch lifted from node.js commit 6820054d2d42ff9274ea0755bea59cfc4f26f353

1 files changed, 24 insertions(+), 0 deletions(-)

src/os.cpp+24
......@@ -45,6 +45,7 @@ typedef SSIZE_T ssize_t;
4545#include <sys/types.h>
4646#include <sys/stat.h>
4747#include <sys/wait.h>
48#include <sys/resource.h>
4849#include <fcntl.h>
4950#include <limits.h>
5051#include <spawn.h>
......@@ -1374,6 +1375,29 @@ int os_init(void) {
13741375#elif defined(__MACH__)
13751376 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &macos_monotonic_clock);
13761377 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &macos_calendar_clock);
1378#endif
1379#if defined(ZIG_OS_POSIX)
1380 // Raise the open file descriptor limit.
1381 // Code lifted from node.js
1382 struct rlimit lim;
1383 if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
1384 // Do a binary search for the limit.
1385 rlim_t min = lim.rlim_cur;
1386 rlim_t max = 1 << 20;
1387 // But if there's a defined upper bound, don't search, just set it.
1388 if (lim.rlim_max != RLIM_INFINITY) {
1389 min = lim.rlim_max;
1390 max = lim.rlim_max;
1391 }
1392 do {
1393 lim.rlim_cur = min + (max - min) / 2;
1394 if (setrlimit(RLIMIT_NOFILE, &lim)) {
1395 max = lim.rlim_cur;
1396 } else {
1397 min = lim.rlim_cur;
1398 }
1399 } while (min + 1 < max);
1400 }
13771401#endif
13781402 return 0;
13791403}