aboutsummaryrefslogtreecommitdiff
path: root/src/os.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-09-05 15:09:13 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-09-05 15:09:13 -0400
commitb564e7ca59818e4904fc421fc8b1914cefd79538 (patch)
treefbbc0d1380bb77c8a6ac8f28570f4d4f076ab13b /src/os.cpp
parent2045b4d93240cd95eee7143f2cfc360eb63c5802 (diff)
downloadzig-b564e7ca59818e4904fc421fc8b1914cefd79538.tar.gz
zig-b564e7ca59818e4904fc421fc8b1914cefd79538.zip
os: raise maximum file descriptor limit
Do a binary search for the maximum RLIMIT_NOFILE. Patch lifted from node.js commit 6820054d2d42ff9274ea0755bea59cfc4f26f353
Diffstat (limited to 'src/os.cpp')
-rw-r--r--src/os.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/os.cpp b/src/os.cpp
index 5fa70bd260..3a6ed2c286 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -45,6 +45,7 @@ typedef SSIZE_T ssize_t;
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
+#include <sys/resource.h>
#include <fcntl.h>
#include <limits.h>
#include <spawn.h>
@@ -1375,6 +1376,29 @@ int os_init(void) {
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &macos_monotonic_clock);
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &macos_calendar_clock);
#endif
+#if defined(ZIG_OS_POSIX)
+ // Raise the open file descriptor limit.
+ // Code lifted from node.js
+ struct rlimit lim;
+ if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
+ // Do a binary search for the limit.
+ rlim_t min = lim.rlim_cur;
+ rlim_t max = 1 << 20;
+ // But if there's a defined upper bound, don't search, just set it.
+ if (lim.rlim_max != RLIM_INFINITY) {
+ min = lim.rlim_max;
+ max = lim.rlim_max;
+ }
+ do {
+ lim.rlim_cur = min + (max - min) / 2;
+ if (setrlimit(RLIMIT_NOFILE, &lim)) {
+ max = lim.rlim_cur;
+ } else {
+ min = lim.rlim_cur;
+ }
+ } while (min + 1 < max);
+ }
+#endif
return 0;
}