Skip to content

Commit 9fe33e4

Browse files
committed
feat(cgroup): add memory extension methods
1 parent 6505e74 commit 9fe33e4

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/metrics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ pub enum Verdict {
1313
IdleTimeLimitExceeded,
1414
}
1515

16+
#[derive(Debug)]
1617
pub struct Metrics {
1718
pub verdict: Verdict,
1819
pub run_time: Duration,
20+
pub memory_usage: Byte,
1921
pub stdout: String,
2022
pub stderr: String,
21-
pub memory: Byte,
2223
}

src/sandbox/cgroup.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use std::time::Duration;
22

3-
use cgroups_rs::fs::{Cgroup, cpu::CpuController};
3+
use byte_unit::Byte;
4+
use cgroups_rs::fs::{Cgroup, cpu::CpuController, memory::MemController};
45

56
const CPU_USAGE_PREFIX: &str = "usage_usec ";
67

78
pub trait CgroupExt {
89
fn get_cpu_time(&self) -> Duration;
10+
fn get_memory_usage(&self) -> Byte;
11+
fn get_memory_limit(&self) -> Byte;
912
}
1013

1114
impl CgroupExt for Cgroup {
@@ -22,4 +25,20 @@ impl CgroupExt for Cgroup {
2225
let usage = usage.parse().unwrap();
2326
Duration::from_micros(usage)
2427
}
28+
29+
fn get_memory_usage(&self) -> Byte {
30+
// SAFETY: there must be memory controller for cgroup v2
31+
let memory_controller: &MemController = self.controller_of().unwrap();
32+
let stats = memory_controller.memory_stat();
33+
34+
Byte::from_u64(stats.usage_in_bytes)
35+
}
36+
37+
fn get_memory_limit(&self) -> Byte {
38+
// SAFETY: there must be memory controller for cgroup v2
39+
let memory_controller: &MemController = self.controller_of().unwrap();
40+
let stats = memory_controller.memory_stat();
41+
42+
Byte::from_u64(stats.limit_in_bytes.max(0) as u64)
43+
}
2544
}

0 commit comments

Comments
 (0)