ethrex_common/fd_limit.rs
1// Copyright 2016-2020 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Outcome of raising file descriptor resource limit
16pub enum Outcome {
17 /// Limit was raised successfully
18 LimitRaised {
19 /// Previous limit (likely soft limit)
20 from: u64,
21 /// New limit (likely hard limit)
22 to: u64,
23 },
24 /// Raising limit is not supported on this platform
25 Unsupported,
26}
27
28/// Errors that happen when trying to raise file descriptor resource limit
29#[derive(Debug, thiserror::Error)]
30pub enum Error {
31 /// Failed to call sysctl to get max supported value configured in sysctl
32 #[error("Failed to call sysctl to get max supported value configured in sysctl: {0}")]
33 #[cfg(any(target_os = "macos", target_os = "ios"))]
34 FailedToCallSysctl(std::io::Error),
35 /// Failed to get current limit
36 #[error("Failed to get current limit: {0}")]
37 FailedToGetLimit(std::io::Error),
38 /// Failed to set new limit
39 #[error("Failed to set new limit ({from}->{to}): {error}")]
40 FailedToSetLimit {
41 /// Current limit
42 from: u64,
43 /// New desired limit
44 to: u64,
45 /// Low level OS error
46 error: std::io::Error,
47 },
48}
49
50/// Raise the soft open file descriptor resource limit to the smaller of the
51/// kernel limit and the hard resource limit.
52///
53/// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X
54/// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256
55/// ends up being far too low for our multithreaded scheduler testing, depending
56/// on the number of cores available.
57#[cfg(any(target_os = "macos", target_os = "ios"))]
58#[allow(clippy::useless_conversion, non_camel_case_types)]
59pub fn raise_fd_limit() -> Result<Outcome, Error> {
60 use std::cmp;
61 use std::io;
62 use std::mem::size_of_val;
63 use std::ptr::null_mut;
64
65 unsafe {
66 static CTL_KERN: libc::c_int = 1;
67 static KERN_MAXFILESPERPROC: libc::c_int = 29;
68
69 // The strategy here is to fetch the current resource limits, read the
70 // kern.maxfilesperproc sysctl value, and bump the soft resource limit for
71 // maxfiles up to the sysctl value.
72
73 // Fetch the kern.maxfilesperproc value
74 let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
75 let mut maxfiles: libc::c_int = 0;
76 let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
77 if libc::sysctl(
78 &mut mib[0],
79 2,
80 &mut maxfiles as *mut _ as *mut _,
81 &mut size,
82 null_mut(),
83 0,
84 ) != 0
85 {
86 return Err(Error::FailedToCallSysctl(io::Error::last_os_error()));
87 }
88
89 // Fetch the current resource limits
90 let mut rlim = libc::rlimit {
91 rlim_cur: 0,
92 rlim_max: 0,
93 };
94 if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
95 return Err(Error::FailedToGetLimit(io::Error::last_os_error()));
96 }
97
98 let old_value = rlim.rlim_cur;
99
100 // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard
101 // limit
102 rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max);
103
104 // Set our newly-increased resource limit
105 if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
106 return Err(Error::FailedToSetLimit {
107 from: old_value.into(),
108 to: rlim.rlim_cur.into(),
109 error: io::Error::last_os_error(),
110 });
111 }
112
113 Ok(Outcome::LimitRaised {
114 from: old_value.into(),
115 to: rlim.rlim_cur.into(),
116 })
117 }
118}
119
120/// Raise the soft open file descriptor resource limit to the hard resource
121/// limit.
122#[cfg(target_os = "linux")]
123#[allow(clippy::useless_conversion, non_camel_case_types)]
124pub fn raise_fd_limit() -> Result<Outcome, Error> {
125 use std::io;
126
127 unsafe {
128 // Fetch the current resource limits
129 let mut rlim = libc::rlimit {
130 rlim_cur: 0,
131 rlim_max: 0,
132 };
133 if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
134 return Err(Error::FailedToGetLimit(io::Error::last_os_error()));
135 }
136
137 let old_value = rlim.rlim_cur;
138
139 // Set soft limit to hard imit
140 rlim.rlim_cur = rlim.rlim_max;
141
142 // Set our newly-increased resource limit
143 if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
144 return Err(Error::FailedToSetLimit {
145 from: old_value.into(),
146 to: rlim.rlim_cur.into(),
147 error: io::Error::last_os_error(),
148 });
149 }
150
151 Ok(Outcome::LimitRaised {
152 from: old_value.into(),
153 to: rlim.rlim_cur.into(),
154 })
155 }
156}
157
158/// Does nothing on unsupported platform
159#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))]
160pub fn raise_fd_limit() -> Result<Outcome, Error> {
161 Ok(Outcome::Unsupported)
162}