-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kaps: add memory and cpu cgroups support
Signed-off-by: Hugo Amalric <[email protected]>
- Loading branch information
1 parent
716f943
commit 853b3b0
Showing
7 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
/*pub struct Cgroups { | ||
cgroup: Cgroup, | ||
resources: Resources, | ||
} | ||
impl Cgroups { | ||
pub fn new() -> Self { | ||
} | ||
pub fn set_cpus(&mut self, cpus: u64) { | ||
self.resources | ||
.cpu | ||
.attrs | ||
.insert("cgroup.procs".to_string(), cpus.to_string()); | ||
self.cgroup.apply(&self.resources).unwrap(); | ||
} | ||
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use anyhow::Result; | ||
use controlgroup::{ | ||
v1::{cpu, Cgroup, CgroupPath, SubsystemKind}, | ||
Pid, | ||
}; | ||
use oci_spec::runtime::LinuxCpu; | ||
use std::path::PathBuf; | ||
|
||
pub struct Cpu { | ||
cpu_cgroup: cpu::Subsystem, | ||
} | ||
|
||
impl Cpu { | ||
pub fn new() -> Self { | ||
// Define and create a new cgroup controlled by the CPU subsystem. | ||
let mut cpu_cgroup = | ||
cpu::Subsystem::new(CgroupPath::new(SubsystemKind::Cpu, PathBuf::from("kaps"))); | ||
cpu_cgroup.create().unwrap(); | ||
Cpu { cpu_cgroup } | ||
} | ||
|
||
pub fn apply(&mut self, cpu: &LinuxCpu) -> Result<()> { | ||
if let Some(cpu_shares) = cpu.shares() { | ||
if cpu_shares != 0 { | ||
let _ = &self.cpu_cgroup.set_shares(cpu_shares); | ||
} | ||
} | ||
|
||
if let Some(cpu_period) = cpu.period() { | ||
if cpu_period != 0 { | ||
let _ = &self.cpu_cgroup.set_cfs_period_us(cpu_period); | ||
} | ||
} | ||
|
||
if let Some(cpu_quota) = cpu.quota() { | ||
if cpu_quota != 0 { | ||
let _ = &self.cpu_cgroup.set_cfs_quota_us(cpu_quota); | ||
} | ||
} | ||
|
||
if let Some(rt_runtime) = cpu.realtime_runtime() { | ||
if rt_runtime != 0 { | ||
let _ = &self.cpu_cgroup.set_rt_runtime_us(rt_runtime); | ||
} | ||
} | ||
|
||
if let Some(rt_period) = cpu.realtime_period() { | ||
if rt_period != 0 { | ||
let _ = &self.cpu_cgroup.set_rt_period_us(rt_period); | ||
} | ||
} | ||
|
||
// Attach the self process to the cgroup. | ||
let pid = Pid::from(std::process::id()); | ||
self.cpu_cgroup.add_task(pid).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn delete(&mut self) -> Result<()> { | ||
// Removing self process from the cgroup | ||
let pid = Pid::from(std::process::id()); | ||
self.cpu_cgroup.remove_task(pid)?; | ||
// and deleting the cgroup. | ||
self.cpu_cgroup.delete()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use anyhow::Result; | ||
use controlgroup::{ | ||
v1::{memory, Cgroup, CgroupPath, SubsystemKind}, | ||
Pid, | ||
}; | ||
use oci_spec::runtime::LinuxMemory; | ||
use std::path::PathBuf; | ||
|
||
pub struct Memory { | ||
memory_cgroup: memory::Subsystem, | ||
} | ||
|
||
impl Memory { | ||
pub fn new() -> Self { | ||
// Define and create a new cgroup controlled by the Memory subsystem. | ||
let mut memory_cgroup = memory::Subsystem::new(CgroupPath::new( | ||
SubsystemKind::Memory, | ||
PathBuf::from("kaps"), | ||
)); | ||
memory_cgroup.create().unwrap(); | ||
Memory { memory_cgroup } | ||
} | ||
|
||
pub fn apply(&mut self, memory: &LinuxMemory) -> Result<()> { | ||
if let Some(limit) = memory.limit() { | ||
if limit != 0 { | ||
let _ = &self.memory_cgroup.set_limit_in_bytes(limit); | ||
} | ||
} | ||
|
||
if let Some(swappiness) = memory.swappiness() { | ||
if swappiness != 0 { | ||
let _ = &self.memory_cgroup.set_swappiness(swappiness); | ||
} | ||
} | ||
|
||
if let Some(kernel) = memory.kernel() { | ||
if kernel != 0 { | ||
let _ = &self.memory_cgroup.set_kmem_limit_in_bytes(kernel); | ||
} | ||
} | ||
|
||
if let Some(kernel_tcp) = memory.kernel_tcp() { | ||
if kernel_tcp != 0 { | ||
let _ = &self.memory_cgroup.set_kmem_tcp_limit_in_bytes(kernel_tcp); | ||
} | ||
} | ||
|
||
if let Some(reservation) = memory.reservation() { | ||
if reservation != 0 { | ||
let _ = &self.memory_cgroup.set_soft_limit_in_bytes(reservation); | ||
} | ||
} | ||
|
||
if let Some(disable_oom_killer) = memory.disable_oom_killer() { | ||
let _ = &self.memory_cgroup.disable_oom_killer(disable_oom_killer); | ||
} | ||
|
||
// Attach the self process to the cgroup. | ||
let pid = Pid::from(std::process::id()); | ||
self.memory_cgroup.add_task(pid).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn delete(&mut self) -> Result<()> { | ||
// Removing self process from the cgroup | ||
let pid = Pid::from(std::process::id()); | ||
self.memory_cgroup.remove_task(pid)?; | ||
// and deleting the cgroup. | ||
self.memory_cgroup.delete()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters