Skip to content

Commit eb3e37a

Browse files
authored
Merge pull request #132 from aa624545345/collect-cgroup-path
Correct path of cgroup when running a container in a container
2 parents 69ef63a + 4005ad8 commit eb3e37a

File tree

17 files changed

+147
-82
lines changed

17 files changed

+147
-82
lines changed

src/blkio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController {
430430

431431
impl BlkIoController {
432432
/// Constructs a new `BlkIoController` with `root` serving as the root of the control group.
433-
pub fn new(root: PathBuf, v2: bool) -> Self {
433+
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
434434
Self {
435-
base: root.clone(),
436-
path: root,
435+
base: root,
436+
path: point,
437437
v2,
438438
}
439439
}

src/cgroup.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use crate::error::ErrorKind::*;
1010
use crate::error::*;
1111

12+
use crate::hierarchies::V1;
1213
use crate::{CgroupPid, ControllIdentifier, Controller, Hierarchy, Resources, Subsystem};
1314

1415
use std::collections::HashMap;
@@ -588,6 +589,33 @@ pub fn get_cgroups_relative_paths_by_pid(pid: u32) -> Result<HashMap<String, Str
588589
get_cgroups_relative_paths_by_path(path)
589590
}
590591

592+
fn get_cgroup_destination(mut mount_root: String, pidpath: String) -> String {
593+
if mount_root == "/" {
594+
mount_root = String::from("");
595+
}
596+
pidpath.trim_start_matches(&mount_root).to_string()
597+
}
598+
599+
pub fn existing_path(paths: HashMap<String, String>) -> Result<HashMap<String, String>> {
600+
let mount_roots_v1 = V1::new();
601+
let mut mount_roots_subsystems_map = HashMap::new();
602+
603+
for s in mount_roots_v1.subsystems().iter() {
604+
let controller_name = s.controller_name();
605+
let path_from_cgroup = paths
606+
.get(&controller_name)
607+
.ok_or(Error::new(Common(format!(
608+
"controller {} found in mountinfo, but not found in cgroup.",
609+
controller_name
610+
))))?;
611+
let path_from_mountinfo = s.to_controller().base().to_string_lossy().to_string();
612+
613+
let des_path = get_cgroup_destination(path_from_mountinfo, path_from_cgroup.to_owned());
614+
mount_roots_subsystems_map.insert(controller_name, des_path);
615+
}
616+
Ok(mount_roots_subsystems_map)
617+
}
618+
591619
fn get_cgroups_relative_paths_by_path(path: String) -> Result<HashMap<String, String>> {
592620
let mut m = HashMap::new();
593621
let content =

src/cpu.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ impl ControllerInternal for CpuController {
5959
fn get_path(&self) -> &PathBuf {
6060
&self.path
6161
}
62-
6362
fn get_path_mut(&mut self) -> &mut PathBuf {
6463
&mut self.path
6564
}
@@ -113,10 +112,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
113112

114113
impl CpuController {
115114
/// Contructs a new `CpuController` with `root` serving as the root of the control group.
116-
pub fn new(root: PathBuf, v2: bool) -> Self {
115+
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
117116
Self {
118-
base: root.clone(),
119-
path: root,
117+
base: root,
118+
path: point,
120119
v2,
121120
}
122121
}

src/cpuacct.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
100100

101101
impl CpuAcctController {
102102
/// Contructs a new `CpuAcctController` with `root` serving as the root of the control group.
103-
pub fn new(root: PathBuf) -> Self {
103+
pub fn new(point: PathBuf, root: PathBuf) -> Self {
104104
Self {
105-
base: root.clone(),
106-
path: root,
105+
base: root,
106+
path: point,
107107
}
108108
}
109109

src/cpuset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
254254

255255
impl CpuSetController {
256256
/// Contructs a new `CpuSetController` with `root` serving as the root of the control group.
257-
pub fn new(root: PathBuf, v2: bool) -> Self {
257+
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
258258
Self {
259-
base: root.clone(),
260-
path: root,
259+
base: root,
260+
path: point,
261261
v2,
262262
}
263263
}

src/devices.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ impl<'a> From<&'a Subsystem> for &'a DevicesController {
204204

205205
impl DevicesController {
206206
/// Constructs a new `DevicesController` with `root` serving as the root of the control group.
207-
pub fn new(root: PathBuf) -> Self {
207+
pub fn new(point: PathBuf, root: PathBuf) -> Self {
208208
Self {
209-
base: root.clone(),
210-
path: root,
209+
base: root,
210+
path: point,
211211
}
212212
}
213213

src/freezer.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ impl<'a> From<&'a Subsystem> for &'a FreezerController {
8484

8585
impl FreezerController {
8686
/// Contructs a new `FreezerController` with `root` serving as the root of the control group.
87-
pub fn new(root: PathBuf, v2: bool) -> Self {
87+
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
8888
Self {
89-
base: root.clone(),
90-
path: root,
89+
base: root,
90+
path: point,
9191
v2,
9292
}
9393
}
94-
9594
/// Freezes the processes in the control group.
9695
pub fn freeze(&self) -> Result<()> {
9796
let mut file_name = "freezer.state";

src/hierarchies.rs

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use crate::cgroup::Cgroup;
3737
/// See `proc(5)` for format details.
3838
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
3939
pub struct Mountinfo {
40+
/// Mount root directory of the file system.
41+
pub mount_root: PathBuf,
4042
/// Mount pathname relative to the process's root.
4143
pub mount_point: PathBuf,
4244
/// Filesystem type (main type with optional sub-type).
@@ -57,6 +59,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
5759
return None;
5860
}
5961
let mount_point = PathBuf::from(s0_values[4]);
62+
let mount_root = PathBuf::from(s0_values[3]);
6063
let fs_type_values: Vec<_> = s1_values[0].trim().split('.').collect();
6164
let fs_type = match fs_type_values.len() {
6265
1 => (fs_type_values[0].to_string(), None),
@@ -69,6 +72,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
6972

7073
let super_opts: Vec<String> = s1_values[2].trim().split(',').map(String::from).collect();
7174
Some(Mountinfo {
75+
mount_root,
7276
mount_point,
7377
fs_type,
7478
super_opts,
@@ -123,47 +127,53 @@ impl Hierarchy for V1 {
123127
// The cgroup writeback feature requires cooperation between memcgs and blkcgs
124128
// To avoid exceptions, we should add_task for blkcg before memcg(push BlkIo before Mem)
125129
// For more Information: https://www.alibabacloud.com/help/doc-detail/155509.htm
126-
if let Some(root) = self.get_mount_point(Controllers::BlkIo) {
127-
subs.push(Subsystem::BlkIo(BlkIoController::new(root, false)));
130+
if let Some((point, root)) = self.get_mount_point(Controllers::BlkIo) {
131+
subs.push(Subsystem::BlkIo(BlkIoController::new(point, root, false)));
128132
}
129-
if let Some(root) = self.get_mount_point(Controllers::Mem) {
130-
subs.push(Subsystem::Mem(MemController::new(root, false)));
133+
if let Some((point, root)) = self.get_mount_point(Controllers::Mem) {
134+
subs.push(Subsystem::Mem(MemController::new(point, root, false)));
131135
}
132-
if let Some(root) = self.get_mount_point(Controllers::Pids) {
133-
subs.push(Subsystem::Pid(PidController::new(root, false)));
136+
if let Some((point, root)) = self.get_mount_point(Controllers::Pids) {
137+
subs.push(Subsystem::Pid(PidController::new(point, root, false)));
134138
}
135-
if let Some(root) = self.get_mount_point(Controllers::CpuSet) {
136-
subs.push(Subsystem::CpuSet(CpuSetController::new(root, false)));
139+
if let Some((point, root)) = self.get_mount_point(Controllers::CpuSet) {
140+
subs.push(Subsystem::CpuSet(CpuSetController::new(point, root, false)));
137141
}
138-
if let Some(root) = self.get_mount_point(Controllers::CpuAcct) {
139-
subs.push(Subsystem::CpuAcct(CpuAcctController::new(root)));
142+
if let Some((point, root)) = self.get_mount_point(Controllers::CpuAcct) {
143+
subs.push(Subsystem::CpuAcct(CpuAcctController::new(point, root)));
140144
}
141-
if let Some(root) = self.get_mount_point(Controllers::Cpu) {
142-
subs.push(Subsystem::Cpu(CpuController::new(root, false)));
145+
if let Some((point, root)) = self.get_mount_point(Controllers::Cpu) {
146+
subs.push(Subsystem::Cpu(CpuController::new(point, root, false)));
143147
}
144-
if let Some(root) = self.get_mount_point(Controllers::Devices) {
145-
subs.push(Subsystem::Devices(DevicesController::new(root)));
148+
if let Some((point, root)) = self.get_mount_point(Controllers::Devices) {
149+
subs.push(Subsystem::Devices(DevicesController::new(point, root)));
146150
}
147-
if let Some(root) = self.get_mount_point(Controllers::Freezer) {
148-
subs.push(Subsystem::Freezer(FreezerController::new(root, false)));
151+
if let Some((point, root)) = self.get_mount_point(Controllers::Freezer) {
152+
subs.push(Subsystem::Freezer(FreezerController::new(
153+
point, root, false,
154+
)));
149155
}
150-
if let Some(root) = self.get_mount_point(Controllers::NetCls) {
151-
subs.push(Subsystem::NetCls(NetClsController::new(root)));
156+
if let Some((point, root)) = self.get_mount_point(Controllers::NetCls) {
157+
subs.push(Subsystem::NetCls(NetClsController::new(point, root)));
152158
}
153-
if let Some(root) = self.get_mount_point(Controllers::PerfEvent) {
154-
subs.push(Subsystem::PerfEvent(PerfEventController::new(root)));
159+
if let Some((point, root)) = self.get_mount_point(Controllers::PerfEvent) {
160+
subs.push(Subsystem::PerfEvent(PerfEventController::new(point, root)));
155161
}
156-
if let Some(root) = self.get_mount_point(Controllers::NetPrio) {
157-
subs.push(Subsystem::NetPrio(NetPrioController::new(root)));
162+
if let Some((point, root)) = self.get_mount_point(Controllers::NetPrio) {
163+
subs.push(Subsystem::NetPrio(NetPrioController::new(point, root)));
158164
}
159-
if let Some(root) = self.get_mount_point(Controllers::HugeTlb) {
160-
subs.push(Subsystem::HugeTlb(HugeTlbController::new(root, false)));
165+
if let Some((point, root)) = self.get_mount_point(Controllers::HugeTlb) {
166+
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
167+
point, root, false,
168+
)));
161169
}
162-
if let Some(root) = self.get_mount_point(Controllers::Rdma) {
163-
subs.push(Subsystem::Rdma(RdmaController::new(root)));
170+
if let Some((point, root)) = self.get_mount_point(Controllers::Rdma) {
171+
subs.push(Subsystem::Rdma(RdmaController::new(point, root)));
164172
}
165-
if let Some(root) = self.get_mount_point(Controllers::Systemd) {
166-
subs.push(Subsystem::Systemd(SystemdController::new(root, false)));
173+
if let Some((point, root)) = self.get_mount_point(Controllers::Systemd) {
174+
subs.push(Subsystem::Systemd(SystemdController::new(
175+
point, root, false,
176+
)));
167177
}
168178

169179
subs
@@ -218,29 +228,51 @@ impl Hierarchy for V2 {
218228
for s in controller_list {
219229
match s {
220230
"cpu" => {
221-
subs.push(Subsystem::Cpu(CpuController::new(self.root(), true)));
231+
subs.push(Subsystem::Cpu(CpuController::new(
232+
self.root(),
233+
PathBuf::from(""),
234+
true,
235+
)));
222236
}
223237
"io" => {
224-
subs.push(Subsystem::BlkIo(BlkIoController::new(self.root(), true)));
238+
subs.push(Subsystem::BlkIo(BlkIoController::new(
239+
self.root(),
240+
PathBuf::from(""),
241+
true,
242+
)));
225243
}
226244
"cpuset" => {
227-
subs.push(Subsystem::CpuSet(CpuSetController::new(self.root(), true)));
245+
subs.push(Subsystem::CpuSet(CpuSetController::new(
246+
self.root(),
247+
PathBuf::from(""),
248+
true,
249+
)));
228250
}
229251
"memory" => {
230-
subs.push(Subsystem::Mem(MemController::new(self.root(), true)));
252+
subs.push(Subsystem::Mem(MemController::new(
253+
self.root(),
254+
PathBuf::from(""),
255+
true,
256+
)));
231257
}
232258
"pids" => {
233-
subs.push(Subsystem::Pid(PidController::new(self.root(), true)));
259+
subs.push(Subsystem::Pid(PidController::new(
260+
self.root(),
261+
PathBuf::from(""),
262+
true,
263+
)));
234264
}
235265
"freezer" => {
236266
subs.push(Subsystem::Freezer(FreezerController::new(
237267
self.root(),
268+
PathBuf::from(""),
238269
true,
239270
)));
240271
}
241272
"hugetlb" => {
242273
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
243274
self.root(),
275+
PathBuf::from(""),
244276
true,
245277
)));
246278
}
@@ -275,10 +307,10 @@ impl V1 {
275307
}
276308
}
277309

278-
pub fn get_mount_point(&self, controller: Controllers) -> Option<PathBuf> {
310+
pub fn get_mount_point(&self, controller: Controllers) -> Option<(PathBuf, PathBuf)> {
279311
self.mountinfo.iter().find_map(|m| {
280312
if m.fs_type.0 == "cgroup" && m.super_opts.contains(&controller.to_string()) {
281-
return Some(m.mount_point.clone());
313+
return Some((m.mount_point.to_owned(), m.mount_root.to_owned()));
282314
}
283315
None
284316
})
@@ -337,19 +369,19 @@ mod tests {
337369
fn test_parse_mount() {
338370
let mountinfo = vec![
339371
("29 26 0:26 / /sys/fs/cgroup/cpuset,cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,cpuset,cpu,cpuacct",
340-
Mountinfo{mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
372+
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
341373
"rw".to_string(),
342374
"cpuset".to_string(),
343375
"cpu".to_string(),
344376
"cpuacct".to_string(),
345377
]}),
346378
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs shm rw,size=65536k",
347-
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
379+
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
348380
"rw".to_string(),
349381
"size=65536k".to_string(),
350382
]}),
351383
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs.123 shm rw,size=65536k",
352-
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
384+
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
353385
"rw".to_string(),
354386
"size=65536k".to_string(),
355387
]}),

src/hugetlb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController {
8888

8989
impl HugeTlbController {
9090
/// Constructs a new `HugeTlbController` with `root` serving as the root of the control group.
91-
pub fn new(root: PathBuf, v2: bool) -> Self {
91+
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
9292
let sizes = get_hugepage_sizes();
9393
Self {
94-
base: root.clone(),
95-
path: root,
94+
base: root,
95+
path: point,
9696
sizes,
9797
v2,
9898
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ pub trait Controller {
255255
/// The file system path to the controller.
256256
fn path(&self) -> &Path;
257257

258+
/// Root path of the file system to the controller.
259+
fn base(&self) -> &Path;
260+
258261
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
259262
/// kernel the information.
260263
fn apply(&self, res: &Resources) -> Result<()>;
@@ -307,6 +310,10 @@ where
307310
self.get_path()
308311
}
309312

313+
fn base(&self) -> &Path {
314+
self.get_base()
315+
}
316+
310317
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
311318
/// kernel the information.
312319
fn apply(&self, res: &Resources) -> Result<()> {

0 commit comments

Comments
 (0)