@@ -121,6 +121,7 @@ pub struct SandboxBuilder {
121
121
mounts : Vec < MountConfig > ,
122
122
env : Vec < ( String , String ) > ,
123
123
memory_limit : Option < usize > ,
124
+ cpu_limit : Option < f32 > ,
124
125
workdir : Option < String > ,
125
126
cmd : Vec < String > ,
126
127
enable_networking : bool ,
@@ -134,6 +135,7 @@ impl SandboxBuilder {
134
135
env : Vec :: new ( ) ,
135
136
workdir : None ,
136
137
memory_limit : None ,
138
+ cpu_limit : None ,
137
139
cmd : Vec :: new ( ) ,
138
140
enable_networking : true ,
139
141
}
@@ -159,6 +161,17 @@ impl SandboxBuilder {
159
161
self
160
162
}
161
163
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
+
162
175
/// Enable or disable the sandbox's networking. When it's disabled processes inside the sandbox
163
176
/// won't be able to reach network service on the Internet or the host machine.
164
177
///
@@ -215,6 +228,11 @@ impl SandboxBuilder {
215
228
args. push ( limit. to_string ( ) ) ;
216
229
}
217
230
231
+ if let Some ( limit) = self . cpu_limit {
232
+ args. push ( "--cpus" . into ( ) ) ;
233
+ args. push ( limit. to_string ( ) ) ;
234
+ }
235
+
218
236
if !self . enable_networking {
219
237
args. push ( "--network" . into ( ) ) ;
220
238
args. push ( "none" . into ( ) ) ;
0 commit comments