Skip to content

Commit 08eeafa

Browse files
committed
manager: Add devices cgroup stats
The stats allow users to retrieve device whitelist from the cgroupfs. Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
1 parent 60b2966 commit 08eeafa

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/manager/fs.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use crate::manager::error::Error;
3232
use crate::manager::{conv, Manager, Result};
3333
use crate::stats::{
3434
BlkioCgroupStats, BlkioStat, CpuAcctStats, CpuCgroupStats, CpuThrottlingStats,
35-
HugeTlbCgroupStats, HugeTlbStat, MemoryCgroupStats, MemoryStats, PidsCgroupStats,
35+
DeviceCgroupStat, DevicesCgroupStats, HugeTlbCgroupStats, HugeTlbStat, MemoryCgroupStats,
36+
MemoryStats, PidsCgroupStats,
3637
};
3738
use crate::{CgroupPid, CgroupStats, FreezerState};
3839

@@ -727,6 +728,41 @@ impl FsManager {
727728
})
728729
.collect()
729730
}
731+
732+
fn devices_cgroup_stats(&self) -> DevicesCgroupStats {
733+
let controller: &DevicesController = match self.controller() {
734+
Ok(controller) => controller,
735+
Err(_) => return DevicesCgroupStats::default(),
736+
};
737+
738+
let list = controller
739+
.allowed_devices()
740+
.map(|devs| {
741+
devs.iter()
742+
.map(|dev| DeviceCgroupStat {
743+
dev_type: dev.devtype.to_char().to_string(),
744+
major: dev.major,
745+
minor: dev.minor,
746+
access: {
747+
let mut access = String::new();
748+
if dev.access.contains(&DevicePermissions::Read) {
749+
access.push('r');
750+
}
751+
if dev.access.contains(&DevicePermissions::Write) {
752+
access.push('w');
753+
}
754+
if dev.access.contains(&DevicePermissions::MkNod) {
755+
access.push('m');
756+
}
757+
access
758+
},
759+
})
760+
.collect::<Vec<_>>()
761+
})
762+
.unwrap_or_default();
763+
764+
DevicesCgroupStats { list }
765+
}
730766
}
731767

732768
impl Manager for FsManager {
@@ -862,6 +898,7 @@ impl Manager for FsManager {
862898
pids: self.pids_cgroup_stats(),
863899
blkio: self.blkio_cgroup_stats(),
864900
hugetlb: self.huge_tlb_cgroup_stats(),
901+
devices: self.devices_cgroup_stats(),
865902
}
866903
}
867904

src/stats.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct CgroupStats {
1313
pub pids: PidsCgroupStats,
1414
pub blkio: BlkioCgroupStats,
1515
pub hugetlb: HugeTlbCgroupStats,
16+
pub devices: DevicesCgroupStats,
1617
}
1718

1819
#[derive(Debug, Default)]
@@ -154,3 +155,16 @@ pub struct HugeTlbStat {
154155
pub max_usage: u64,
155156
pub fail_cnt: u64,
156157
}
158+
159+
#[derive(Debug, Default)]
160+
pub struct DeviceCgroupStat {
161+
pub dev_type: String,
162+
pub major: i64,
163+
pub minor: i64,
164+
pub access: String,
165+
}
166+
167+
#[derive(Debug, Default)]
168+
pub struct DevicesCgroupStats {
169+
pub list: Vec<DeviceCgroupStat>,
170+
}

0 commit comments

Comments
 (0)