add basic v2 cpu/memory functions

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu
2020-09-02 22:03:11 +08:00
parent 704db324ae
commit c623dc3fba
7 changed files with 256 additions and 55 deletions

View File

@@ -149,7 +149,12 @@ impl CpuController {
/// `shares` to `200` ensures that control group `B` receives twice as much as CPU bandwidth.
/// (Assuming both `A` and `B` are of the same parent)
pub fn set_shares(&self, shares: u64) -> Result<()> {
self.open_path("cpu.shares", true).and_then(|mut file| {
let mut file = "cpu.shares";
if self.v2 {
file = "cpu.weight";
}
// NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
self.open_path(file, true).and_then(|mut file| {
file.write_all(shares.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
@@ -194,4 +199,44 @@ impl CpuController {
self.open_path("cpu.cfs_quota_us", false)
.and_then(read_u64_from)
}
pub fn set_cfs_quota_and_period(&self, quota: u64, period: u64) -> Result<()> {
if !self.v2 {
self.set_cfs_quota(quota)?;
return self.set_cfs_period(period);
}
let mut line = "max".to_string();
if quota > 0 {
line = quota.to_string();
}
let mut p = period;
if period == 0 {
// This default value is documented in
// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
p = 100000
}
line = format!("{} {}", line, p);
self.open_path("cpu.max", true)
.and_then(|mut file| {
file.write_all(line.as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_rt_runtime(&self, us: i64) -> Result<()> {
self.open_path("cpu.rt_runtime_us", true)
.and_then(|mut file| {
file.write_all(us.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn set_rt_period_us(&self, us: u64) -> Result<()> {
self.open_path("cpu.rt_period_us", true)
.and_then(|mut file| {
file.write_all(us.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
}