]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | /// darwin_fd_limit exists to work around an issue where launchctl on macOS | |
12 | /// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256 | |
13 | /// ends up being far too low for our multithreaded scheduler testing, depending | |
14 | /// on the number of cores available. | |
15 | /// | |
16 | /// This fixes issue #7772. | |
17 | #[cfg(any(target_os = "macos", target_os = "ios"))] | |
18 | #[allow(non_camel_case_types)] | |
19 | pub unsafe fn raise_fd_limit() { | |
20 | use libc; | |
21 | use std::cmp; | |
22 | use std::io; | |
23 | use std::mem::size_of_val; | |
24 | use std::ptr::null_mut; | |
25 | ||
26 | static CTL_KERN: libc::c_int = 1; | |
27 | static KERN_MAXFILESPERPROC: libc::c_int = 29; | |
28 | ||
29 | // The strategy here is to fetch the current resource limits, read the | |
30 | // kern.maxfilesperproc sysctl value, and bump the soft resource limit for | |
31 | // maxfiles up to the sysctl value. | |
32 | ||
33 | // Fetch the kern.maxfilesperproc value | |
34 | let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC]; | |
35 | let mut maxfiles: libc::c_int = 0; | |
36 | let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t; | |
37 | if libc::sysctl(&mut mib[0], | |
38 | 2, | |
39 | &mut maxfiles as *mut _ as *mut _, | |
40 | &mut size, | |
41 | null_mut(), | |
42 | 0) != 0 { | |
43 | let err = io::Error::last_os_error(); | |
44 | panic!("raise_fd_limit: error calling sysctl: {}", err); | |
45 | } | |
46 | ||
47 | // Fetch the current resource limits | |
48 | let mut rlim = libc::rlimit { | |
49 | rlim_cur: 0, | |
50 | rlim_max: 0, | |
51 | }; | |
52 | if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 { | |
53 | let err = io::Error::last_os_error(); | |
54 | panic!("raise_fd_limit: error calling getrlimit: {}", err); | |
55 | } | |
56 | ||
57 | // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard | |
58 | // limit | |
59 | rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max); | |
60 | ||
61 | // Set our newly-increased resource limit | |
62 | if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 { | |
63 | let err = io::Error::last_os_error(); | |
64 | panic!("raise_fd_limit: error calling setrlimit: {}", err); | |
65 | } | |
66 | } | |
67 | ||
68 | #[cfg(not(any(target_os = "macos", target_os = "ios")))] | |
69 | pub unsafe fn raise_fd_limit() {} |