mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de9625ff57 | ||
|
|
b6b65f79d1 | ||
|
|
547fb08c03 | ||
|
|
ec9f3547ed | ||
|
|
82a6aa491a | ||
|
|
65c36214b7 | ||
|
|
e0d0b8f4bc | ||
|
|
db822470e5 | ||
|
|
362373b3ec | ||
|
|
eadbf53140 | ||
|
|
b3c57840ee | ||
|
|
b10e52d85f | ||
|
|
7d4d4579a3 | ||
|
|
eb3e37a4bc | ||
|
|
4005ad844d | ||
|
|
ef3497646f | ||
|
|
69ef63a0ef | ||
|
|
346844ca72 | ||
|
|
4f1fe13d91 | ||
|
|
17a6c6b842 | ||
|
|
3c4b724433 | ||
|
|
01885adb99 | ||
|
|
ce5f5f638e | ||
|
|
be837166e9 |
@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
|
||||
keywords = ["linux", "cgroup", "containers", "isolation"]
|
||||
categories = ["os", "api-bindings", "os::unix-apis"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
version = "0.3.3"
|
||||
version = "0.3.5"
|
||||
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
||||
edition = "2018"
|
||||
homepage = "https://github.com/kata-containers/cgroups-rs"
|
||||
|
||||
@@ -430,10 +430,10 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController {
|
||||
|
||||
impl BlkIoController {
|
||||
/// Constructs a new `BlkIoController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
use crate::error::ErrorKind::*;
|
||||
use crate::error::*;
|
||||
|
||||
use crate::hierarchies::V1;
|
||||
use crate::{CgroupPid, ControllIdentifier, Controller, Hierarchy, Resources, Subsystem};
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -521,7 +522,7 @@ fn supported_controllers() -> Vec<String> {
|
||||
let ret = fs::read_to_string(p.as_str());
|
||||
ret.unwrap_or_default()
|
||||
.split(' ')
|
||||
.map(|x| x.to_string())
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
@@ -588,19 +589,47 @@ pub fn get_cgroups_relative_paths_by_pid(pid: u32) -> Result<HashMap<String, Str
|
||||
get_cgroups_relative_paths_by_path(path)
|
||||
}
|
||||
|
||||
fn get_cgroup_destination(mut mount_root: String, pidpath: String) -> String {
|
||||
if mount_root == "/" {
|
||||
mount_root = String::from("");
|
||||
}
|
||||
pidpath.trim_start_matches(&mount_root).to_string()
|
||||
}
|
||||
|
||||
pub fn existing_path(paths: HashMap<String, String>) -> Result<HashMap<String, String>> {
|
||||
let mount_roots_v1 = V1::new();
|
||||
let mut mount_roots_subsystems_map = HashMap::new();
|
||||
|
||||
for s in mount_roots_v1.subsystems().iter() {
|
||||
let controller_name = s.controller_name();
|
||||
let path_from_cgroup = paths
|
||||
.get(&controller_name)
|
||||
.ok_or(Error::new(Common(format!(
|
||||
"controller {} found in mountinfo, but not found in cgroup.",
|
||||
controller_name
|
||||
))))?;
|
||||
let path_from_mountinfo = s.to_controller().base().to_string_lossy().to_string();
|
||||
|
||||
let des_path = get_cgroup_destination(path_from_mountinfo, path_from_cgroup.to_owned());
|
||||
mount_roots_subsystems_map.insert(controller_name, des_path);
|
||||
}
|
||||
Ok(mount_roots_subsystems_map)
|
||||
}
|
||||
|
||||
fn get_cgroups_relative_paths_by_path(path: String) -> Result<HashMap<String, String>> {
|
||||
let mut m = HashMap::new();
|
||||
let content =
|
||||
fs::read_to_string(path.clone()).map_err(|e| Error::with_cause(ReadFailed(path), e))?;
|
||||
for l in content.lines() {
|
||||
let fl: Vec<&str> = l.split(':').collect();
|
||||
if fl.len() != 3 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let keys: Vec<&str> = fl[1].split(',').collect();
|
||||
for key in &keys {
|
||||
m.insert(key.to_string(), fl[2].to_string());
|
||||
// cgroup path may have ":" , likes
|
||||
// "2:cpu,cpuacct:/system.slice/containerd.service/test.slice:cri-containerd:96b37a2edf84351487f42039e137427f1812f678850675fac214caf597ee5e4a"
|
||||
for line in content.lines() {
|
||||
if let Some((first_value_part, remaining_path)) =
|
||||
line.split_once(':').unwrap_or_default().1.split_once(':')
|
||||
{
|
||||
let keys: Vec<&str> = first_value_part.split(',').collect();
|
||||
keys.iter().for_each(|key| {
|
||||
m.insert(key.to_string(), remaining_path.to_string());
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(m)
|
||||
|
||||
@@ -59,7 +59,6 @@ impl ControllerInternal for CpuController {
|
||||
fn get_path(&self) -> &PathBuf {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn get_path_mut(&mut self) -> &mut PathBuf {
|
||||
&mut self.path
|
||||
}
|
||||
@@ -113,10 +112,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
|
||||
|
||||
impl CpuController {
|
||||
/// Contructs a new `CpuController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,10 +100,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
|
||||
|
||||
impl CpuAcctController {
|
||||
/// Contructs a new `CpuAcctController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,10 +254,10 @@ fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
|
||||
|
||||
impl CpuSetController {
|
||||
/// Contructs a new `CpuSetController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,10 +204,10 @@ impl<'a> From<&'a Subsystem> for &'a DevicesController {
|
||||
|
||||
impl DevicesController {
|
||||
/// Constructs a new `DevicesController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ impl DevicesController {
|
||||
match res {
|
||||
Ok(_) => {
|
||||
s.lines().fold(Ok(Vec::new()), |acc, line| {
|
||||
let ls = line.to_string().split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::<Vec<String>>();
|
||||
let ls = line.split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::<Vec<String>>();
|
||||
if acc.is_err() || ls.len() != 4 {
|
||||
error!("allowed_devices: acc: {:?}, ls: {:?}", acc, ls);
|
||||
Err(Error::new(ParseError))
|
||||
|
||||
@@ -84,14 +84,13 @@ impl<'a> From<&'a Subsystem> for &'a FreezerController {
|
||||
|
||||
impl FreezerController {
|
||||
/// Contructs a new `FreezerController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
/// Freezes the processes in the control group.
|
||||
pub fn freeze(&self) -> Result<()> {
|
||||
let mut file_name = "freezer.state";
|
||||
|
||||
@@ -5,9 +5,6 @@
|
||||
//
|
||||
|
||||
//! This module represents the various control group hierarchies the Linux kernel supports.
|
||||
//!
|
||||
//! Currently, we only support the cgroupv1 hierarchy, but in the future we will add support for
|
||||
//! the Unified Hierarchy.
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
@@ -37,6 +34,8 @@ use crate::cgroup::Cgroup;
|
||||
/// See `proc(5)` for format details.
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
pub struct Mountinfo {
|
||||
/// Mount root directory of the file system.
|
||||
pub mount_root: PathBuf,
|
||||
/// Mount pathname relative to the process's root.
|
||||
pub mount_point: PathBuf,
|
||||
/// Filesystem type (main type with optional sub-type).
|
||||
@@ -57,6 +56,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
|
||||
return None;
|
||||
}
|
||||
let mount_point = PathBuf::from(s0_values[4]);
|
||||
let mount_root = PathBuf::from(s0_values[3]);
|
||||
let fs_type_values: Vec<_> = s1_values[0].trim().split('.').collect();
|
||||
let fs_type = match fs_type_values.len() {
|
||||
1 => (fs_type_values[0].to_string(), None),
|
||||
@@ -69,6 +69,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
|
||||
|
||||
let super_opts: Vec<String> = s1_values[2].trim().split(',').map(String::from).collect();
|
||||
Some(Mountinfo {
|
||||
mount_root,
|
||||
mount_point,
|
||||
fs_type,
|
||||
super_opts,
|
||||
@@ -123,47 +124,53 @@ impl Hierarchy for V1 {
|
||||
// The cgroup writeback feature requires cooperation between memcgs and blkcgs
|
||||
// To avoid exceptions, we should add_task for blkcg before memcg(push BlkIo before Mem)
|
||||
// For more Information: https://www.alibabacloud.com/help/doc-detail/155509.htm
|
||||
if let Some(root) = self.get_mount_point(Controllers::BlkIo) {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::BlkIo) {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Mem) {
|
||||
subs.push(Subsystem::Mem(MemController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Mem) {
|
||||
subs.push(Subsystem::Mem(MemController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Pids) {
|
||||
subs.push(Subsystem::Pid(PidController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Pids) {
|
||||
subs.push(Subsystem::Pid(PidController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::CpuSet) {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::CpuSet) {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::CpuAcct) {
|
||||
subs.push(Subsystem::CpuAcct(CpuAcctController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::CpuAcct) {
|
||||
subs.push(Subsystem::CpuAcct(CpuAcctController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Cpu) {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Cpu) {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Devices) {
|
||||
subs.push(Subsystem::Devices(DevicesController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Devices) {
|
||||
subs.push(Subsystem::Devices(DevicesController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Freezer) {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Freezer) {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::NetCls) {
|
||||
subs.push(Subsystem::NetCls(NetClsController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::NetCls) {
|
||||
subs.push(Subsystem::NetCls(NetClsController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::PerfEvent) {
|
||||
subs.push(Subsystem::PerfEvent(PerfEventController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::PerfEvent) {
|
||||
subs.push(Subsystem::PerfEvent(PerfEventController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::NetPrio) {
|
||||
subs.push(Subsystem::NetPrio(NetPrioController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::NetPrio) {
|
||||
subs.push(Subsystem::NetPrio(NetPrioController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::HugeTlb) {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::HugeTlb) {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Rdma) {
|
||||
subs.push(Subsystem::Rdma(RdmaController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Rdma) {
|
||||
subs.push(Subsystem::Rdma(RdmaController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Systemd) {
|
||||
subs.push(Subsystem::Systemd(SystemdController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Systemd) {
|
||||
subs.push(Subsystem::Systemd(SystemdController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
|
||||
subs
|
||||
@@ -218,29 +225,51 @@ impl Hierarchy for V2 {
|
||||
for s in controller_list {
|
||||
match s {
|
||||
"cpu" => {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Cpu(CpuController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"io" => {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(self.root(), true)));
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"cpuset" => {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(self.root(), true)));
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"memory" => {
|
||||
subs.push(Subsystem::Mem(MemController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Mem(MemController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"pids" => {
|
||||
subs.push(Subsystem::Pid(PidController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Pid(PidController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"freezer" => {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"hugetlb" => {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
@@ -275,10 +304,10 @@ impl V1 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mount_point(&self, controller: Controllers) -> Option<PathBuf> {
|
||||
pub fn get_mount_point(&self, controller: Controllers) -> Option<(PathBuf, PathBuf)> {
|
||||
self.mountinfo.iter().find_map(|m| {
|
||||
if m.fs_type.0 == "cgroup" && m.super_opts.contains(&controller.to_string()) {
|
||||
return Some(m.mount_point.clone());
|
||||
return Some((m.mount_point.to_owned(), m.mount_root.to_owned()));
|
||||
}
|
||||
None
|
||||
})
|
||||
@@ -309,43 +338,16 @@ impl Default for V2 {
|
||||
|
||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||
|
||||
#[cfg(any(
|
||||
all(target_os = "linux", not(target_env = "musl")),
|
||||
target_os = "android"
|
||||
))]
|
||||
pub fn is_cgroup2_unified_mode() -> bool {
|
||||
use nix::sys::statfs;
|
||||
|
||||
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
||||
let fs_stat = statfs::statfs(path);
|
||||
if fs_stat.is_err() {
|
||||
return false;
|
||||
}
|
||||
let fs_stat = match statfs::statfs(path) {
|
||||
Ok(fs_stat) => fs_stat,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
// FIXME notwork, nix will not compile CGROUP2_SUPER_MAGIC because not(target_env = "musl")
|
||||
fs_stat.unwrap().filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||
}
|
||||
|
||||
pub const INIT_CGROUP_PATHS: &str = "/proc/1/cgroup";
|
||||
|
||||
#[cfg(all(target_os = "linux", target_env = "musl"))]
|
||||
pub fn is_cgroup2_unified_mode() -> bool {
|
||||
let lines = fs::read_to_string(INIT_CGROUP_PATHS);
|
||||
if lines.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
for line in lines.unwrap().lines() {
|
||||
let fields: Vec<&str> = line.split(':').collect();
|
||||
if fields.len() != 3 {
|
||||
continue;
|
||||
}
|
||||
if fields[0] != "0" {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
fs_stat.filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||
}
|
||||
|
||||
pub fn auto() -> Box<dyn Hierarchy> {
|
||||
@@ -364,19 +366,19 @@ mod tests {
|
||||
fn test_parse_mount() {
|
||||
let mountinfo = vec![
|
||||
("29 26 0:26 / /sys/fs/cgroup/cpuset,cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,cpuset,cpu,cpuacct",
|
||||
Mountinfo{mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"cpuset".to_string(),
|
||||
"cpu".to_string(),
|
||||
"cpuacct".to_string(),
|
||||
]}),
|
||||
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs shm rw,size=65536k",
|
||||
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"size=65536k".to_string(),
|
||||
]}),
|
||||
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs.123 shm rw,size=65536k",
|
||||
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"size=65536k".to_string(),
|
||||
]}),
|
||||
|
||||
@@ -88,11 +88,11 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController {
|
||||
|
||||
impl HugeTlbController {
|
||||
/// Constructs a new `HugeTlbController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
let sizes = get_hugepage_sizes();
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
sizes,
|
||||
v2,
|
||||
}
|
||||
|
||||
@@ -231,6 +231,7 @@ mod sealed {
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get(&self, key: &str) -> Result<String> {
|
||||
self.open_path(key, false).and_then(|mut file: File| {
|
||||
let mut string = String::new();
|
||||
@@ -255,6 +256,9 @@ pub trait Controller {
|
||||
/// The file system path to the controller.
|
||||
fn path(&self) -> &Path;
|
||||
|
||||
/// Root path of the file system to the controller.
|
||||
fn base(&self) -> &Path;
|
||||
|
||||
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
|
||||
/// kernel the information.
|
||||
fn apply(&self, res: &Resources) -> Result<()>;
|
||||
@@ -307,6 +311,10 @@ where
|
||||
self.get_path()
|
||||
}
|
||||
|
||||
fn base(&self) -> &Path {
|
||||
self.get_base()
|
||||
}
|
||||
|
||||
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
|
||||
/// kernel the information.
|
||||
fn apply(&self, res: &Resources) -> Result<()> {
|
||||
|
||||
@@ -540,10 +540,10 @@ impl ControllerInternal for MemController {
|
||||
|
||||
impl MemController {
|
||||
/// Contructs a new `MemController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
@@ -573,18 +573,33 @@ impl MemController {
|
||||
|
||||
// for v2
|
||||
pub fn get_mem(&self) -> Result<SetMemory> {
|
||||
let mut m: SetMemory = Default::default();
|
||||
self.get_max_value("memory.high")
|
||||
.map(|x| m.high = Some(x))?;
|
||||
self.get_max_value("memory.low").map(|x| m.low = Some(x))?;
|
||||
self.get_max_value("memory.max").map(|x| m.max = Some(x))?;
|
||||
self.get_max_value("memory.min").map(|x| m.min = Some(x))?;
|
||||
let m = SetMemory {
|
||||
high: self
|
||||
.get_max_value("memory.high")
|
||||
.map_or(Some(MaxValue::default()), Some),
|
||||
low: self
|
||||
.get_max_value("memory.low")
|
||||
.map_or(Some(MaxValue::Value(0)), Some),
|
||||
max: self
|
||||
.get_max_value("memory.max")
|
||||
.map_or(Some(MaxValue::default()), Some),
|
||||
min: self
|
||||
.get_max_value("memory.min")
|
||||
.map_or(Some(MaxValue::Value(0)), Some),
|
||||
};
|
||||
|
||||
Ok(m)
|
||||
}
|
||||
|
||||
fn memory_stat_v2(&self) -> Memory {
|
||||
let set = self.get_mem().unwrap();
|
||||
// NOTE: get_mem() always returns T, but let's
|
||||
// still do `unwrap_or` for safety.
|
||||
let set = self.get_mem().unwrap_or(SetMemory {
|
||||
low: Some(MaxValue::Value(0)),
|
||||
high: Some(MaxValue::default()),
|
||||
max: Some(MaxValue::default()),
|
||||
min: Some(MaxValue::Value(0)),
|
||||
});
|
||||
|
||||
Memory {
|
||||
fail_cnt: 0,
|
||||
@@ -593,7 +608,10 @@ impl MemController {
|
||||
.open_path("memory.current", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
max_usage_in_bytes: 0,
|
||||
max_usage_in_bytes: self
|
||||
.open_path("memory.peak", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
move_charge_at_immigrate: 0,
|
||||
numa_stat: NumaStat::default(),
|
||||
oom_control: OomControl::default(),
|
||||
@@ -727,7 +745,7 @@ impl MemController {
|
||||
.open_path("memory.swap.events", false)
|
||||
.and_then(flat_keyed_to_hashmap)
|
||||
.map(|x| *x.get("fail").unwrap_or(&0) as u64)
|
||||
.unwrap(),
|
||||
.unwrap_or(0),
|
||||
limit_in_bytes: self
|
||||
.open_path("memory.swap.max", false)
|
||||
.and_then(read_i64_from)
|
||||
@@ -736,7 +754,10 @@ impl MemController {
|
||||
.open_path("memory.swap.current", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
max_usage_in_bytes: 0,
|
||||
max_usage_in_bytes: self
|
||||
.open_path("memory.swap.peak", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -844,13 +865,16 @@ impl MemController {
|
||||
/// Set the memory usage limit of the control group, in bytes.
|
||||
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
||||
let mut file_name = "memory.limit_in_bytes";
|
||||
let mut limit_str = limit.to_string();
|
||||
if self.v2 {
|
||||
file_name = "memory.max";
|
||||
if limit == -1 {
|
||||
limit_str = "max".to_string();
|
||||
}
|
||||
}
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
||||
})
|
||||
file.write_all(limit_str.as_ref())
|
||||
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -881,13 +905,16 @@ impl MemController {
|
||||
/// Set the memory+swap limit of the control group, in bytes.
|
||||
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
||||
let mut file_name = "memory.memsw.limit_in_bytes";
|
||||
let mut limit_str = limit.to_string();
|
||||
if self.v2 {
|
||||
file_name = "memory.swap.max";
|
||||
if limit == -1 {
|
||||
limit_str = "max".to_string();
|
||||
}
|
||||
}
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
||||
})
|
||||
file.write_all(limit_str.as_ref())
|
||||
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -76,10 +76,10 @@ impl<'a> From<&'a Subsystem> for &'a NetClsController {
|
||||
|
||||
impl NetClsController {
|
||||
/// Constructs a new `NetClsController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,10 +79,10 @@ impl<'a> From<&'a Subsystem> for &'a NetPrioController {
|
||||
|
||||
impl NetPrioController {
|
||||
/// Constructs a new `NetPrioController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,10 +65,10 @@ impl<'a> From<&'a Subsystem> for &'a PerfEventController {
|
||||
|
||||
impl PerfEventController {
|
||||
/// Constructs a new `PerfEventController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +92,10 @@ impl<'a> From<&'a Subsystem> for &'a PidController {
|
||||
impl PidController {
|
||||
/// Constructors a new `PidController` instance, with `root` serving as the controller's root
|
||||
/// directory.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
11
src/rdma.rs
11
src/rdma.rs
@@ -68,10 +68,10 @@ impl<'a> From<&'a Subsystem> for &'a RdmaController {
|
||||
|
||||
impl RdmaController {
|
||||
/// Constructs a new `RdmaController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +81,11 @@ impl RdmaController {
|
||||
.and_then(read_string_from)
|
||||
}
|
||||
|
||||
/// Returns the max usage of RDMA/IB specific resources.
|
||||
pub fn max(&self) -> Result<String> {
|
||||
self.open_path("rdma.max", false).and_then(read_string_from)
|
||||
}
|
||||
|
||||
/// Set a maximum usage for each RDMA/IB resource.
|
||||
pub fn set_max(&self, max: &str) -> Result<()> {
|
||||
self.open_path("rdma.max", true).and_then(|mut file| {
|
||||
|
||||
@@ -62,10 +62,10 @@ impl<'a> From<&'a Subsystem> for &'a SystemdController {
|
||||
|
||||
impl SystemdController {
|
||||
/// Constructs a new `SystemdController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
_v2: v2,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user