-
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.
Signed-off-by: Hugo Amalric <[email protected]>
- Loading branch information
1 parent
716f943
commit b171310
Showing
6 changed files
with
118 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,29 @@ | ||
use cgroups_rs::Cgroup; | ||
use cgroups_rs::Resources; | ||
|
||
#[derive(Default, Clone)] | ||
pub struct Cgroups { | ||
cgroup: Cgroup, | ||
resources: Resources, | ||
} | ||
|
||
impl Cgroups { | ||
pub fn new() -> Self { | ||
let h = cgroups_rs::hierarchies::auto(); | ||
let cgroup: Cgroup = Cgroup::new(h, "kaps"); | ||
let resources = cgroups_rs::Resources::default(); | ||
|
||
Cgroups { | ||
cgroup, | ||
resources, | ||
} | ||
} | ||
|
||
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,59 @@ | ||
use oci_spec::runtime::LinuxCpu; | ||
use anyhow::{Result}; | ||
|
||
const CGROUP_CPU_SHARES: &str = "cpu.shares"; | ||
const CGROUP_CPU_QUOTA: &str = "cpu.cfs_quota_us"; | ||
const CGROUP_CPU_PERIOD: &str = "cpu.cfs_period_us"; | ||
const CGROUP_CPU_BURST: &str = "cpu.cfs_burst_us"; | ||
const CGROUP_CPU_RT_RUNTIME: &str = "cpu.rt_runtime_us"; | ||
const CGROUP_CPU_RT_PERIOD: &str = "cpu.rt_period_us"; | ||
const CGROUP_CPU_STAT: &str = "cpu.stat"; | ||
const CGROUP_CPU_PROCS: &str = "cgroup.procs"; | ||
|
||
pub struct Cpu {} | ||
|
||
impl Cpu { | ||
fn apply(cgroups: &Cgroups, cpu: &LinuxCpu) -> Result<()> { | ||
if let Some(cpu_shares) = cpu.shares() { | ||
if cpu_shares != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_SHARES.to_string(), cpu_shares.to_string()); | ||
} | ||
} | ||
|
||
if let Some(cpu_period) = cpu.period() { | ||
if cpu_period != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_PERIOD.to_string(), cpu_period.to_string()); | ||
} | ||
} | ||
|
||
if let Some(cpu_quota) = cpu.quota() { | ||
if cpu_quota != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_QUOTA.to_string(), cpu_quota.to_string()); | ||
} | ||
} | ||
|
||
if let Some(cpu_burst) = cpu.burst() { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_BURST.to_string(), cpu_burst.to_string()); | ||
} | ||
|
||
if let Some(rt_runtime) = cpu.realtime_runtime() { | ||
if rt_runtime != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_RT_RUNTIME.to_string(), rt_runtime.to_string()); | ||
} | ||
} | ||
|
||
if let Some(rt_period) = cpu.realtime_period() { | ||
if rt_period != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_RT_PERIOD.to_string(), rt_period.to_string()); | ||
} | ||
} | ||
|
||
if let Some(cpus) = cpu.cpus() { | ||
if cpus != 0 { | ||
cgroups.resources.cpu.attrs.insert(CGROUP_CPU_PROCS.to_string(), cpus.to_string()); | ||
} | ||
} | ||
|
||
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