Skip to content

Commit 54851fd

Browse files
committed
Why is everything i64
1 parent 283a5e7 commit 54851fd

File tree

3 files changed

+351
-51
lines changed

3 files changed

+351
-51
lines changed

testcontainers/src/core/containers/request.rs

Lines changed: 110 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
time::Duration,
77
};
88

9-
use bollard::models::HostConfig;
109
use bollard_stubs::models::ResourcesUlimits;
1110

1211
use crate::{
@@ -42,7 +41,64 @@ pub struct ContainerRequest<I: Image> {
4241
pub(crate) startup_timeout: Option<Duration>,
4342
pub(crate) working_dir: Option<String>,
4443
pub(crate) log_consumers: Vec<Box<dyn LogConsumer + 'static>>,
45-
pub(crate) host_config: Option<HostConfig>,
44+
45+
/// The length of a CPU period in microseconds. Default is 100000, this configures how
46+
/// CFS will schedule the threads for this container. Normally you don't adjust this and
47+
/// just set the CPU quota or nano CPUs. You might want to set this if you want to increase
48+
/// or reduce context-switching the container is subjected to.
49+
pub(crate) cpu_period: Option<i64>,
50+
51+
/// Microseconds of CPU time that the container can get in a CPU period.
52+
/// Most users will want to set CPU quota to their desired CPU count * 100000.
53+
/// For example, to limit a container to 2 CPUs, set CPU quota to 200000.
54+
/// This is based on the default CPU period of 100000.
55+
/// If CPU quota is set to 0, the container will not be limited.
56+
pub(crate) cpu_quota: Option<i64>,
57+
58+
/// The length of a CPU real-time period in microseconds. Set to 0 to allocate no time allocated to real-time tasks.
59+
pub(crate) cpu_realtime_period: Option<i64>,
60+
61+
/// The length of a CPU real-time runtime in microseconds. Set to 0 to allocate no time allocated to real-time tasks.
62+
pub(crate) cpu_realtime_runtime: Option<i64>,
63+
64+
/// CPUs in which to allow execution (e.g., `0-3`, `0,1`).
65+
/// Core pinning should help with performance consistency and context switching in some cases.
66+
pub(crate) cpuset_cpus: Option<String>,
67+
68+
/// CPU quota in units of 10<sup>-9</sup> CPUs. This is basically what the --cpus flag turns into, but the
69+
/// raw value is denominated in billionths of a CPU. cpu_period and cpu_quota give you more control over the scheduler.
70+
pub nano_cpus: Option<i64>,
71+
72+
/// Memory limit for the container, the _minimum_ is 6 MiB.
73+
/// This is the same as `HostConfig::memory`.
74+
pub(crate) memory: Option<i64>,
75+
76+
/// Memory reservation, soft limit. Analogous to the JVM's `-Xms` option.
77+
/// The _minimum_ is 6 MiB.
78+
/// This is the same as `HostConfig::memory_reservation`.
79+
pub(crate) memory_reservation: Option<i64>,
80+
81+
/// Total memory limit (memory + swap). Set as `-1` to enable unlimited swap.
82+
/// Same 6 MiB minimum as `memory`.
83+
pub memory_swap: Option<i64>,
84+
85+
/// Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100.
86+
pub memory_swappiness: Option<i64>,
87+
88+
/// Disable OOM Killer for the container. This will not do anything unless -m (memory limit, cf. memory on this struct) is set.
89+
/// You can disable OOM-killer by writing "1" to memory.oom_control file, as:
90+
/// ```ignore
91+
/// echo 1 > memory.oom_control
92+
/// ```
93+
/// This operation is only allowed to the top cgroup of sub-hierarchy.
94+
/// If OOM-killer is disabled, tasks under cgroup will hang/sleep
95+
/// in memory cgroup's OOM-waitqueue when they request accountable memory.
96+
/// https://lwn.net/Articles/432224/
97+
pub oom_kill_disable: Option<bool>,
98+
99+
/// Tune a container's PIDs limit. Set `0` or `-1` for unlimited, or `null` to not change.
100+
pub pids_limit: Option<i64>,
101+
46102
#[cfg(feature = "reusable-containers")]
47103
pub(crate) reuse: crate::ReuseDirective,
48104
}
@@ -189,10 +245,42 @@ impl<I: Image> ContainerRequest<I> {
189245
self.working_dir.as_deref()
190246
}
191247

192-
pub fn host_config(&self) -> &Option<HostConfig> {
193-
&self.host_config
248+
pub fn cpu_period(&self) -> Option<i64> {
249+
self.cpu_period
250+
}
251+
pub fn cpu_quota(&self) -> Option<i64> {
252+
self.cpu_quota
253+
}
254+
pub fn cpu_realtime_period(&self) -> Option<i64> {
255+
self.cpu_realtime_period
256+
}
257+
pub fn cpu_realtime_runtime(&self) -> Option<i64> {
258+
self.cpu_realtime_runtime
259+
}
260+
pub fn cpuset_cpus(&self) -> Option<&str> {
261+
self.cpuset_cpus.as_deref()
262+
}
263+
pub fn nano_cpus(&self) -> Option<i64> {
264+
self.nano_cpus
265+
}
266+
pub fn memory(&self) -> Option<i64> {
267+
self.memory
268+
}
269+
pub fn memory_reservation(&self) -> Option<i64> {
270+
self.memory_reservation
271+
}
272+
pub fn memory_swap(&self) -> Option<i64> {
273+
self.memory_swap
274+
}
275+
pub fn memory_swappiness(&self) -> Option<i64> {
276+
self.memory_swappiness
277+
}
278+
pub fn oom_kill_disable(&self) -> Option<bool> {
279+
self.oom_kill_disable
280+
}
281+
pub fn pids_limit(&self) -> Option<i64> {
282+
self.pids_limit
194283
}
195-
196284
/// Indicates that the container will not be stopped when it is dropped
197285
#[cfg(feature = "reusable-containers")]
198286
pub fn reuse(&self) -> crate::ReuseDirective {
@@ -225,7 +313,18 @@ impl<I: Image> From<I> for ContainerRequest<I> {
225313
startup_timeout: None,
226314
working_dir: None,
227315
log_consumers: vec![],
228-
host_config: None,
316+
cpu_period: None,
317+
cpu_quota: None,
318+
cpu_realtime_period: None,
319+
cpu_realtime_runtime: None,
320+
cpuset_cpus: None,
321+
nano_cpus: None,
322+
memory: None,
323+
memory_reservation: None,
324+
memory_swap: None,
325+
memory_swappiness: None,
326+
oom_kill_disable: None,
327+
pids_limit: None,
229328
#[cfg(feature = "reusable-containers")]
230329
reuse: crate::ReuseDirective::Never,
231330
}
@@ -273,8 +372,11 @@ impl<I: Image + Debug> Debug for ContainerRequest<I> {
273372
.field("userns_mode", &self.userns_mode)
274373
.field("startup_timeout", &self.startup_timeout)
275374
.field("working_dir", &self.working_dir)
276-
.field("host_config", &self.host_config);
277-
375+
.field("cpu_period", &self.cpu_period)
376+
.field("cpu_quota", &self.cpu_quota)
377+
.field("cpu_realtime_period", &self.cpu_realtime_period)
378+
.field("cpu_realtime_runtime", &self.cpu_realtime_runtime)
379+
.field("cpuset_cpus", &self.cpuset_cpus);
278380
#[cfg(feature = "reusable-containers")]
279381
repr.field("reusable", &self.reuse);
280382

testcontainers/src/core/image/image_ext.rs

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::time::Duration;
22

3-
use bollard::models::HostConfig;
43
use bollard_stubs::models::ResourcesUlimits;
54

65
use crate::{
@@ -158,8 +157,65 @@ pub trait ImageExt<I: Image> {
158157
/// Allows to follow the container logs for the whole lifecycle of the container, starting from the creation.
159158
fn with_log_consumer(self, log_consumer: impl LogConsumer + 'static) -> ContainerRequest<I>;
160159

161-
/// Adds a Bollard HostConfig override to the container
162-
fn with_host_config(self, host_config: impl Into<HostConfig>) -> ContainerRequest<I>;
160+
/// Sets the CPU period for the container.
161+
/// The default is defined by the underlying image.
162+
/// The length of a CPU period in microseconds.
163+
/// https://docs.docker.com/engine/reference/commandline/run/#cpu-period
164+
fn with_cpu_period(self, cpu_period: impl Into<i64>) -> ContainerRequest<I>;
165+
166+
/// Sets the CPU quota for the container.
167+
/// The default is defined by the underlying image.
168+
/// Microseconds of CPU time that the container can get in a CPU period.
169+
/// https://docs.docker.com/engine/reference/commandline/run/#cpu-quota
170+
/// Most users will want to set CPU quota to their desired CPU count * 100000.
171+
/// For example, to limit a container to 2 CPUs, set CPU quota to 200000.
172+
/// This is based on the default CPU period of 100000.
173+
/// If CPU quota is set to 0, the container will not be limited.
174+
fn with_cpu_quota(self, cpu_quota: impl Into<i64>) -> ContainerRequest<I>;
175+
176+
/// Sets the CPU realtime period for the container.
177+
/// The default is defined by the underlying image.
178+
/// The length of a CPU real-time period in microseconds.
179+
fn with_cpu_realtime_period(self, cpu_realtime_period: impl Into<i64>) -> ContainerRequest<I>;
180+
181+
/// Sets the CPU realtime runtime for the container.
182+
fn with_cpu_realtime_runtime(self, cpu_realtime_runtime: impl Into<i64>)
183+
-> ContainerRequest<I>;
184+
185+
/// Sets the CPUs in which to allow execution (e.g., `0-3`, `0,1`).
186+
/// Core pinning should help with performance consistency and context switching in some cases.
187+
/// The default is defined by the underlying image.
188+
fn with_cpuset_cpus(self, cpuset_cpus: impl Into<String>) -> ContainerRequest<I>;
189+
190+
/// Memory limit for the container, the _minimum_ is 6 MiB.
191+
/// This is the same as `HostConfig::memory`.
192+
fn with_memory(self, bytes: i64) -> ContainerRequest<I>;
193+
194+
/// Memory reservation, soft limit. Analogous to the JVM's `-Xms` option.
195+
/// The _minimum_ is 6 MiB.
196+
/// This is the same as `HostConfig::memory_reservation`.
197+
fn with_memory_reservation(self, bytes: i64) -> ContainerRequest<I>;
198+
199+
/// Total memory limit (memory + swap). Set as `-1` to enable unlimited swap.
200+
/// Same 6 MiB minimum as `memory`. I do not know why everything is i64.
201+
fn with_memory_swap(self, bytes: i64) -> ContainerRequest<I>;
202+
203+
/// Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100.
204+
fn with_memory_swappiness(self, swappiness: i64) -> ContainerRequest<I>;
205+
206+
/// Disable OOM Killer for the container. This will not do anything unless -m (memory limit, cf. memory on this struct) is set.
207+
/// You can disable OOM-killer by writing "1" to memory.oom_control file, as:
208+
/// ```ignore
209+
/// echo 1 > memory.oom_control
210+
/// ```
211+
/// This operation is only allowed to the top cgroup of sub-hierarchy.
212+
/// If OOM-killer is disabled, tasks under cgroup will hang/sleep
213+
/// in memory cgroup's OOM-waitqueue when they request accountable memory.
214+
/// https://lwn.net/Articles/432224/
215+
fn with_oom_kill_disable(self, disable: bool) -> ContainerRequest<I>;
216+
217+
/// Tune a container's PIDs limit. Set `0` or `-1` for unlimited, or `null` to not change.
218+
fn with_pids_limit(self, limit: i64) -> ContainerRequest<I>;
163219

164220
/// Flag the container as being exempt from the default `testcontainers` remove-on-drop lifecycle,
165221
/// indicating that the container should be kept running, and that executions with the same configuration
@@ -377,10 +433,93 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
377433
container_req
378434
}
379435

380-
fn with_host_config(self, host_config: impl Into<HostConfig>) -> ContainerRequest<I> {
436+
fn with_cpu_period(self, cpu_period: impl Into<i64>) -> ContainerRequest<I> {
437+
let container_req = self.into();
438+
ContainerRequest {
439+
cpu_period: Some(cpu_period.into()),
440+
..container_req
441+
}
442+
}
443+
444+
fn with_cpu_quota(self, cpu_quota: impl Into<i64>) -> ContainerRequest<I> {
445+
let container_req = self.into();
446+
ContainerRequest {
447+
cpu_quota: Some(cpu_quota.into()),
448+
..container_req
449+
}
450+
}
451+
452+
fn with_cpu_realtime_period(self, cpu_realtime_period: impl Into<i64>) -> ContainerRequest<I> {
453+
let container_req = self.into();
454+
ContainerRequest {
455+
cpu_realtime_period: Some(cpu_realtime_period.into()),
456+
..container_req
457+
}
458+
}
459+
460+
fn with_cpu_realtime_runtime(
461+
self,
462+
cpu_realtime_runtime: impl Into<i64>,
463+
) -> ContainerRequest<I> {
464+
let container_req = self.into();
465+
ContainerRequest {
466+
cpu_realtime_runtime: Some(cpu_realtime_runtime.into()),
467+
..container_req
468+
}
469+
}
470+
471+
fn with_cpuset_cpus(self, cpuset_cpus: impl Into<String>) -> ContainerRequest<I> {
472+
let container_req = self.into();
473+
ContainerRequest {
474+
cpuset_cpus: Some(cpuset_cpus.into()),
475+
..container_req
476+
}
477+
}
478+
479+
fn with_memory(self, bytes: i64) -> ContainerRequest<I> {
480+
let container_req = self.into();
481+
ContainerRequest {
482+
memory: Some(bytes),
483+
..container_req
484+
}
485+
}
486+
487+
fn with_memory_reservation(self, bytes: i64) -> ContainerRequest<I> {
488+
let container_req = self.into();
489+
ContainerRequest {
490+
memory_reservation: Some(bytes),
491+
..container_req
492+
}
493+
}
494+
495+
fn with_memory_swap(self, bytes: i64) -> ContainerRequest<I> {
496+
let container_req = self.into();
497+
ContainerRequest {
498+
memory_swap: Some(bytes),
499+
..container_req
500+
}
501+
}
502+
503+
fn with_memory_swappiness(self, swappiness: i64) -> ContainerRequest<I> {
504+
let container_req = self.into();
505+
ContainerRequest {
506+
memory_swappiness: Some(swappiness),
507+
..container_req
508+
}
509+
}
510+
511+
fn with_oom_kill_disable(self, disable: bool) -> ContainerRequest<I> {
512+
let container_req = self.into();
513+
ContainerRequest {
514+
oom_kill_disable: Some(disable),
515+
..container_req
516+
}
517+
}
518+
519+
fn with_pids_limit(self, limit: i64) -> ContainerRequest<I> {
381520
let container_req = self.into();
382521
ContainerRequest {
383-
host_config: Some(host_config.into()),
522+
pids_limit: Some(limit),
384523
..container_req
385524
}
386525
}

0 commit comments

Comments
 (0)