authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-03 10:07:39+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-03 10:07:39+01:00
log50604971745c0ddd2373bba933c30b59db624d2b
tree6bbf752e96849ad05ac269ba769daebc8bb5937e
parentb30a765b95cf666409c1e5324718a7c8c60af4af
parente023a5fe5d8e0c2849c160ffab993ec1e378681c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6921 from xackus/gimmeMoreOfThoseSweetSweetFileDescriptors

stage2: ask for more file descriptors

3 files changed, 30 insertions(+), 48 deletions(-)

lib/std/os/bits/darwin.zig+3
......@@ -1475,6 +1475,9 @@ pub const CLOCK_UPTIME_RAW_APPROX = 9;
14751475pub const CLOCK_PROCESS_CPUTIME_ID = 12;
14761476pub const CLOCK_THREAD_CPUTIME_ID = 16;
14771477
1478/// Max open files per process
1479/// https://opensource.apple.com/source/xnu/xnu-4903.221.2/bsd/sys/syslimits.h.auto.html
1480pub const OPEN_MAX = 10240;
14781481pub const RUSAGE_SELF = 0;
14791482pub const RUSAGE_CHILDREN = -1;
14801483
src/main.zig+27-25
......@@ -2982,35 +2982,37 @@ fn parseCodeModel(arg: []const u8) std.builtin.CodeModel {
29822982/// garbage collector to run concurrently to zig processes, and to allow multiple
29832983/// zig processes to run concurrently with each other, without clobbering each other.
29842984fn gimmeMoreOfThoseSweetSweetFileDescriptors() void {
2985 switch (std.Target.current.os.tag) {
2986 .windows, .wasi, .uefi, .other, .freestanding => return,
2987 // std lib is missing getrlimit/setrlimit.
2988 // https://github.com/ziglang/zig/issues/6361
2989 //else => {},
2990 else => return,
2991 }
2985 if (!@hasDecl(std.os, "rlimit")) return;
29922986 const posix = std.os;
2993 var lim = posix.getrlimit(posix.RLIMIT_NOFILE, &lim) catch return; // Oh well; we tried.
2987
2988 var lim = posix.getrlimit(.NOFILE) catch return; // Oh well; we tried.
2989 if (comptime std.Target.current.isDarwin()) {
2990 // On Darwin, `NOFILE` is bounded by a hardcoded value `OPEN_MAX`.
2991 // According to the man pages for setrlimit():
2992 // setrlimit() now returns with errno set to EINVAL in places that historically succeeded.
2993 // It no longer accepts "rlim_cur = RLIM_INFINITY" for RLIM_NOFILE.
2994 // Use "rlim_cur = min(OPEN_MAX, rlim_max)".
2995 lim.max = std.math.min(std.os.darwin.OPEN_MAX, lim.max);
2996 }
29942997 if (lim.cur == lim.max) return;
2998
2999 // Do a binary search for the limit.
3000 var min: posix.rlim_t = lim.cur;
3001 var max: posix.rlim_t = 1 << 20;
3002 // But if there's a defined upper bound, don't search, just set it.
3003 if (lim.max != posix.RLIM_INFINITY) {
3004 min = lim.max;
3005 max = lim.max;
3006 }
3007
29953008 while (true) {
2996 // Do a binary search for the limit.
2997 var min: posix.rlim_t = lim.cur;
2998 var max: posix.rlim_t = 1 << 20;
2999 // But if there's a defined upper bound, don't search, just set it.
3000 if (lim.max != posix.RLIM_INFINITY) {
3001 min = lim.max;
3002 max = lim.max;
3003 }
3004 while (true) {
3005 lim.cur = min + (max - min) / 2;
3006 if (posix.setrlimit(posix.RLIMIT_NOFILE, lim)) |_| {
3007 min = lim.cur;
3008 } else |_| {
3009 max = lim.cur;
3010 }
3011 if (min + 1 < max) continue;
3012 return;
3009 lim.cur = min + @divTrunc(max - min, 2); // on freebsd rlim_t is signed
3010 if (posix.setrlimit(.NOFILE, lim)) |_| {
3011 min = lim.cur;
3012 } else |_| {
3013 max = lim.cur;
30133014 }
3015 if (min + 1 >= max) break;
30143016 }
30153017}
30163018
src/stage1/os.cpp-23
......@@ -977,29 +977,6 @@ int os_init(void) {
977977#elif defined(__MACH__)
978978 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &macos_monotonic_clock);
979979 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &macos_calendar_clock);
980#endif
981#if defined(ZIG_OS_POSIX)
982 // Raise the open file descriptor limit.
983 // Code lifted from node.js
984 struct rlimit lim;
985 if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
986 // Do a binary search for the limit.
987 rlim_t min = lim.rlim_cur;
988 rlim_t max = 1 << 20;
989 // But if there's a defined upper bound, don't search, just set it.
990 if (lim.rlim_max != RLIM_INFINITY) {
991 min = lim.rlim_max;
992 max = lim.rlim_max;
993 }
994 do {
995 lim.rlim_cur = min + (max - min) / 2;
996 if (setrlimit(RLIMIT_NOFILE, &lim)) {
997 max = lim.rlim_cur;
998 } else {
999 min = lim.rlim_cur;
1000 }
1001 } while (min + 1 < max);
1002 }
1003980#endif
1004981 return 0;
1005982}