From 7703f4c60a501c4f4bc74c7d814619aa818d601b Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Sun, 1 Nov 2020 23:23:05 +0100 Subject: [PATCH] stage2: ask for more file descriptors --- src/main.zig | 44 +++++++++++++++++++------------------------- src/stage1/os.cpp | 23 ----------------------- 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/main.zig b/src/main.zig index 45fcb5ce6..77fa2bc20 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2982,35 +2982,29 @@ fn parseCodeModel(arg: []const u8) std.builtin.CodeModel { /// garbage collector to run concurrently to zig processes, and to allow multiple /// zig processes to run concurrently with each other, without clobbering each other. fn gimmeMoreOfThoseSweetSweetFileDescriptors() void { - switch (std.Target.current.os.tag) { - .windows, .wasi, .uefi, .other, .freestanding => return, - // std lib is missing getrlimit/setrlimit. - // https://github.com/ziglang/zig/issues/6361 - //else => {}, - else => return, - } + if (!@hasDecl(std.os, "rlimit")) return; const posix = std.os; - var lim = posix.getrlimit(posix.RLIMIT_NOFILE, &lim) catch return; // Oh well; we tried. + + var lim = posix.getrlimit(.NOFILE) catch return; // Oh well; we tried. if (lim.cur == lim.max) return; + + // Do a binary search for the limit. + var min: posix.rlim_t = lim.cur; + var max: posix.rlim_t = 1 << 20; + // But if there's a defined upper bound, don't search, just set it. + if (lim.max != posix.RLIM_INFINITY) { + min = lim.max; + max = lim.max; + } + while (true) { - // Do a binary search for the limit. - var min: posix.rlim_t = lim.cur; - var max: posix.rlim_t = 1 << 20; - // But if there's a defined upper bound, don't search, just set it. - if (lim.max != posix.RLIM_INFINITY) { - min = lim.max; - max = lim.max; - } - while (true) { - lim.cur = min + (max - min) / 2; - if (posix.setrlimit(posix.RLIMIT_NOFILE, lim)) |_| { - min = lim.cur; - } else |_| { - max = lim.cur; - } - if (min + 1 < max) continue; - return; + lim.cur = min + (max - min) / 2; + if (posix.setrlimit(.NOFILE, lim)) |_| { + min = lim.cur; + } else |_| { + max = lim.cur; } + if (min + 1 >= max) break; } } diff --git a/src/stage1/os.cpp b/src/stage1/os.cpp index c9dd49071..1cb015c1a 100644 --- a/src/stage1/os.cpp +++ b/src/stage1/os.cpp @@ -977,29 +977,6 @@ int os_init(void) { #elif defined(__MACH__) 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; }