Skip to content

Commit fad8cbd

Browse files
committed
sandbox: allow limiting the cpu usage
1 parent ed160f7 commit fad8cbd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## Unreleased
77

8+
### Added
9+
10+
- New method `SandboxBuilder::limit_cpu`
11+
812
## [0.5.1] - 2020-01-31
913

1014
### Fixed

src/cmd/sandbox.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub struct SandboxBuilder {
121121
mounts: Vec<MountConfig>,
122122
env: Vec<(String, String)>,
123123
memory_limit: Option<usize>,
124+
cpu_limit: Option<f32>,
124125
workdir: Option<String>,
125126
cmd: Vec<String>,
126127
enable_networking: bool,
@@ -134,6 +135,7 @@ impl SandboxBuilder {
134135
env: Vec::new(),
135136
workdir: None,
136137
memory_limit: None,
138+
cpu_limit: None,
137139
cmd: Vec::new(),
138140
enable_networking: true,
139141
}
@@ -159,6 +161,17 @@ impl SandboxBuilder {
159161
self
160162
}
161163

164+
/// Enable or disable the sandbox's CPU limit. The value of the limit is the fraction of CPU
165+
/// cores the sandbox is allowed to use.
166+
///
167+
/// For example, on a 4-core machine, setting a CPU limit of `2.0` will only allow two of the
168+
/// cores to be used, while a CPU limit of `0.5` will only allow half of a single CPU core to
169+
/// be used.
170+
pub fn cpu_limit(mut self, limit: Option<f32>) -> Self {
171+
self.cpu_limit = limit;
172+
self
173+
}
174+
162175
/// Enable or disable the sandbox's networking. When it's disabled processes inside the sandbox
163176
/// won't be able to reach network service on the Internet or the host machine.
164177
///
@@ -215,6 +228,11 @@ impl SandboxBuilder {
215228
args.push(limit.to_string());
216229
}
217230

231+
if let Some(limit) = self.cpu_limit {
232+
args.push("--cpus".into());
233+
args.push(limit.to_string());
234+
}
235+
218236
if !self.enable_networking {
219237
args.push("--network".into());
220238
args.push("none".into());

0 commit comments

Comments
 (0)