Skip to content

Commit

Permalink
Merge pull request #320 from utam0k/refactoring/introduce-lifetime-to…
Browse files Browse the repository at this point in the history
…-ControllerOpt

Avoid cloning LinuxResources because it is a large structure.
  • Loading branch information
Furisto authored Sep 22, 2021
2 parents 4bab88d + 8023ff4 commit 9f669d7
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 47 deletions.
6 changes: 3 additions & 3 deletions cgroups/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ pub enum FreezerState {
}

/// ControllerOpt is given all cgroup controller for applying cgroup configuration.
#[derive(Clone, Debug, Default)]
pub struct ControllerOpt {
#[derive(Clone, Debug)]
pub struct ControllerOpt<'a> {
/// Resources contain cgroup information for handling resource constraints for the container.
pub resources: LinuxResources,
pub resources: &'a LinuxResources,
/// Disables the OOM killer for out of memory conditions.
pub disable_oom_killer: bool,
/// Specify an oom_score_adj for container.
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Controller for Blkio {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(blkio) = &controller_opt.resources.block_io {
return Some(blkio);
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub trait Controller {
fn apply(controller_opt: &ControllerOpt, cgroup_root: &Path) -> Result<()>;

/// Checks if the controller needs to handle this request
fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource>;
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource>;
}
2 changes: 1 addition & 1 deletion cgroups/src/v1/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Controller for Cpu {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(cpu) = &controller_opt.resources.cpu {
if cpu.shares.is_some()
|| cpu.period.is_some()
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/cpuacct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Controller for CpuAcct {
Ok(())
}

fn needs_to_handle(_controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(_controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
None
}
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/cpuset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Controller for CpuSet {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(cpuset) = &controller_opt.resources.cpu {
if cpuset.cpus.is_some() || cpuset.mems.is_some() {
return Some(cpuset);
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Controller for Devices {
}

// always needs to be called due to default devices
fn needs_to_handle(_controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(_controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
Some(&())
}
}
Expand Down
19 changes: 11 additions & 8 deletions cgroups/src/v1/freezer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl Controller for Freezer {
Ok(())
}

fn needs_to_handle(controller: &ControllerOpt) -> Option<&Self::Resource> {
if let Some(freezer_state) = &controller.freezer_state {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(freezer_state) = &controller_opt.freezer_state {
return Some(freezer_state);
}

Expand Down Expand Up @@ -190,9 +190,10 @@ mod tests {
let state = FreezerState::Thawed;

let controller_opt = ControllerOpt {
resources: linux_resources,
resources: &linux_resources,
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};

let pid = Pid::from_raw(1000);
Expand Down Expand Up @@ -223,9 +224,10 @@ mod tests {
let state = FreezerState::Frozen;

let controller_opt = ControllerOpt {
resources: linux_resources,
resources: &linux_resources,
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};

let pid = Pid::from_raw(1001);
Expand Down Expand Up @@ -256,9 +258,10 @@ mod tests {
let state = FreezerState::Undefined;

let controller_opt = ControllerOpt {
resources: linux_resources,
resources: &linux_resources,
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};

let pid = Pid::from_raw(1002);
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/hugetlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Controller for HugeTlb {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(hugepage_limits) = controller_opt.resources.hugepage_limits.as_ref() {
if !hugepage_limits.is_empty() {
return controller_opt.resources.hugepage_limits.as_ref();
Expand Down
5 changes: 3 additions & 2 deletions cgroups/src/v1/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ impl CgroupManager for Manager {

fn freeze(&self, state: FreezerState) -> Result<()> {
let controller_opt = ControllerOpt {
resources: Default::default(),
resources: &Default::default(),
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};
Freezer::apply(
&controller_opt,
Expand Down
7 changes: 4 additions & 3 deletions cgroups/src/v1/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Controller for Memory {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(memory) = &controller_opt.resources.memory {
return Some(memory);
}
Expand Down Expand Up @@ -455,9 +455,10 @@ mod tests {
};

let controller_opt = ControllerOpt {
resources: linux_resources,
resources: &linux_resources,
disable_oom_killer,
..Default::default()
oom_score_adj: None,
freezer_state: None,
};

let result = <Memory as Controller>::apply(&controller_opt, &tmp);
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/network_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Controller for NetworkClassifier {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(network) = controller_opt.resources.network.as_ref() {
return Some(network);
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/network_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Controller for NetworkPriority {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(network) = &controller_opt.resources.network {
return Some(network);
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/perf_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Controller for PerfEvent {
Ok(())
}
//no need to handle any case
fn needs_to_handle(_controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(_controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
None
}
}
Expand Down
2 changes: 1 addition & 1 deletion cgroups/src/v1/pids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Controller for Pids {
Ok(())
}

fn needs_to_handle(controller_opt: &ControllerOpt) -> Option<&Self::Resource> {
fn needs_to_handle<'a>(controller_opt: &'a ControllerOpt) -> Option<&'a Self::Resource> {
if let Some(pids) = &controller_opt.resources.pids {
return Some(pids);
}
Expand Down
5 changes: 3 additions & 2 deletions cgroups/src/v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ impl CgroupManager for Manager {

fn freeze(&self, state: FreezerState) -> Result<()> {
let controller_opt = ControllerOpt {
resources: Default::default(),
resources: &Default::default(),
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};
Freezer::apply(&controller_opt, &self.full_path)
}
Expand Down
5 changes: 3 additions & 2 deletions cgroups/src/v2/systemd_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ impl CgroupManager for SystemDCGroupManager {

fn freeze(&self, state: FreezerState) -> Result<()> {
let controller_opt = ControllerOpt {
resources: Default::default(),
resources: &Default::default(),
freezer_state: Some(state),
..Default::default()
oom_score_adj: None,
disable_oom_killer: false,
};
Freezer::apply(&controller_opt, &self.full_path)
}
Expand Down
18 changes: 12 additions & 6 deletions cgroups/src/v2/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ mod tests {
};

let controller_opt = ControllerOpt {
resources,
..Default::default()
resources: &resources,
freezer_state: None,
oom_score_adj: None,
disable_oom_killer: false,
};

// act
Expand Down Expand Up @@ -112,8 +114,10 @@ mod tests {
};

let controller_opt = ControllerOpt {
resources,
..Default::default()
resources: &resources,
freezer_state: None,
oom_score_adj: None,
disable_oom_killer: false,
};

// act
Expand Down Expand Up @@ -143,8 +147,10 @@ mod tests {
};

let controller_opt = ControllerOpt {
resources,
..Default::default()
resources: &resources,
oom_score_adj: None,
disable_oom_killer: false,
freezer_state: None,
};

// act
Expand Down
24 changes: 14 additions & 10 deletions src/container/builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,21 @@ impl<'a> ContainerBuilderImpl<'a> {
log::debug!("init pid is {:?}", init_pid);

if self.rootless.is_none() && linux.resources.is_some() && self.init {
let controller_opt = cgroups::common::ControllerOpt {
resources: linux.resources.clone().unwrap(),
..Default::default()
};
cmanager
.add_task(init_pid)
.context("Failed to add tasks to cgroup manager")?;
if let Some(resources) = linux.resources.as_ref() {
let controller_opt = cgroups::common::ControllerOpt {
resources,
freezer_state: None,
oom_score_adj: None,
disable_oom_killer: false,
};
cmanager
.add_task(init_pid)
.context("Failed to add tasks to cgroup manager")?;

cmanager
.apply(&controller_opt)
.context("Failed to apply resource limits through cgroup")?;
cmanager
.apply(&controller_opt)
.context("Failed to apply resource limits through cgroup")?;
}
}

// if file to write the pid to is specified, write pid of the child
Expand Down

0 comments on commit 9f669d7

Please sign in to comment.