]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/raise_fd_limit.rs
Auto merge of #57118 - Zoxc:query-stats, r=wesleywiser
[rust.git] / src / tools / compiletest / src / raise_fd_limit.rs
1 /// darwin_fd_limit exists to work around an issue where launchctl on macOS
2 /// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256
3 /// ends up being far too low for our multithreaded scheduler testing, depending
4 /// on the number of cores available.
5 ///
6 /// This fixes issue #7772.
7 #[cfg(any(target_os = "macos", target_os = "ios"))]
8 #[allow(non_camel_case_types)]
9 pub unsafe fn raise_fd_limit() {
10     use libc;
11     use std::cmp;
12     use std::io;
13     use std::mem::size_of_val;
14     use std::ptr::null_mut;
15
16     static CTL_KERN: libc::c_int = 1;
17     static KERN_MAXFILESPERPROC: libc::c_int = 29;
18
19     // The strategy here is to fetch the current resource limits, read the
20     // kern.maxfilesperproc sysctl value, and bump the soft resource limit for
21     // maxfiles up to the sysctl value.
22
23     // Fetch the kern.maxfilesperproc value
24     let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
25     let mut maxfiles: libc::c_int = 0;
26     let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
27     if libc::sysctl(
28         &mut mib[0],
29         2,
30         &mut maxfiles as *mut _ as *mut _,
31         &mut size,
32         null_mut(),
33         0,
34     ) != 0
35     {
36         let err = io::Error::last_os_error();
37         panic!("raise_fd_limit: error calling sysctl: {}", err);
38     }
39
40     // Fetch the current resource limits
41     let mut rlim = libc::rlimit {
42         rlim_cur: 0,
43         rlim_max: 0,
44     };
45     if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
46         let err = io::Error::last_os_error();
47         panic!("raise_fd_limit: error calling getrlimit: {}", err);
48     }
49
50     // Make sure we're only ever going to increase the rlimit.
51     if rlim.rlim_cur < maxfiles as libc::rlim_t {
52         // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard limit.
53         rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max);
54
55         // Set our newly-increased resource limit.
56         if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
57             let err = io::Error::last_os_error();
58             panic!("raise_fd_limit: error calling setrlimit: {}", err);
59         }
60     }
61 }
62
63 #[cfg(not(any(target_os = "macos", target_os = "ios")))]
64 pub unsafe fn raise_fd_limit() {}