Skip to content

Commit 2149e1c

Browse files
ordovicialevex
authored andcommitted
Run rustfmt
1 parent 5660656 commit 2149e1c

21 files changed

+1203
-732
lines changed

src/blkio.rs

Lines changed: 296 additions & 195 deletions
Large diffs are not rendered by default.

src/cgroup.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
//! This module handles cgroup operations. Start here!
22
3-
use {CgroupError, CgroupPid, Resources, ControllIdentifier, Controller, Hierarchy, Subsystem};
3+
use {CgroupError, CgroupPid, ControllIdentifier, Controller, Hierarchy, Resources, Subsystem};
44

55
use std::convert::From;
66

7-
87
/// A control group is the central structure to this crate.
98
///
109
///
1110
/// # What are control groups?
1211
///
13-
/// Lifting over from the Linux kernel sources:
12+
/// Lifting over from the Linux kernel sources:
1413
///
1514
/// > Control Groups provide a mechanism for aggregating/partitioning sets of
1615
/// > tasks, and all their future children, into hierarchical groups with
@@ -26,7 +25,6 @@ pub struct Cgroup<'b> {
2625
}
2726

2827
impl<'b> Cgroup<'b> {
29-
3028
/// Create this control group.
3129
fn create(&self) {
3230
for subsystem in &self.subsystems {
@@ -55,8 +53,11 @@ impl<'b> Cgroup<'b> {
5553
/// destroyed.
5654
pub fn load(hier: &Hierarchy, path: String) -> Cgroup {
5755
let mut subsystems = hier.subsystems();
58-
if path != "" {
59-
subsystems = subsystems.into_iter().map(|x| x.enter(&path)).collect::<Vec<_>>();
56+
if path != "" {
57+
subsystems = subsystems
58+
.into_iter()
59+
.map(|x| x.enter(&path))
60+
.collect::<Vec<_>>();
6061
}
6162

6263
let cg = Cgroup {
@@ -79,28 +80,28 @@ impl<'b> Cgroup<'b> {
7980
/// actually removed, and remove the descendants first if not. In the future, this behavior
8081
/// will change.
8182
pub fn delete(self) {
82-
self.subsystems.into_iter().for_each(|sub| {
83-
match sub {
84-
Subsystem::Pid(pidc) => pidc.delete(),
85-
Subsystem::Mem(c) => c.delete(),
86-
Subsystem::CpuSet(c) => c.delete(),
87-
Subsystem::CpuAcct(c) => c.delete(),
88-
Subsystem::Cpu(c) => c.delete(),
89-
Subsystem::Devices(c) => c.delete(),
90-
Subsystem::Freezer(c) => c.delete(),
91-
Subsystem::NetCls(c) => c.delete(),
92-
Subsystem::BlkIo(c) => c.delete(),
93-
Subsystem::PerfEvent(c) => c.delete(),
94-
Subsystem::NetPrio(c) => c.delete(),
95-
Subsystem::HugeTlb(c) => c.delete(),
96-
Subsystem::Rdma(c) => c.delete(),
97-
}
83+
self.subsystems.into_iter().for_each(|sub| match sub {
84+
Subsystem::Pid(pidc) => pidc.delete(),
85+
Subsystem::Mem(c) => c.delete(),
86+
Subsystem::CpuSet(c) => c.delete(),
87+
Subsystem::CpuAcct(c) => c.delete(),
88+
Subsystem::Cpu(c) => c.delete(),
89+
Subsystem::Devices(c) => c.delete(),
90+
Subsystem::Freezer(c) => c.delete(),
91+
Subsystem::NetCls(c) => c.delete(),
92+
Subsystem::BlkIo(c) => c.delete(),
93+
Subsystem::PerfEvent(c) => c.delete(),
94+
Subsystem::NetPrio(c) => c.delete(),
95+
Subsystem::HugeTlb(c) => c.delete(),
96+
Subsystem::Rdma(c) => c.delete(),
9897
});
9998
}
10099

101100
/// Apply a set of resource limits to the control group.
102101
pub fn apply(&self, res: &Resources) -> Result<(), CgroupError> {
103-
self.subsystems.iter().try_fold((), |_, e| e.to_controller().apply(res))
102+
self.subsystems
103+
.iter()
104+
.try_fold((), |_, e| e.to_controller().apply(res))
104105
}
105106

106107
/// Retrieve a container based on type inference.
@@ -114,8 +115,9 @@ impl<'b> Cgroup<'b> {
114115
/// .expect("No cpu controller attached!");
115116
/// ```
116117
pub fn controller_of<'a, T>(self: &'a Self) -> Option<&'a T>
117-
where &'a T: From<&'a Subsystem>,
118-
T: Controller + ControllIdentifier,
118+
where
119+
&'a T: From<&'a Subsystem>,
120+
T: Controller + ControllIdentifier,
119121
{
120122
for i in &self.subsystems {
121123
if i.to_controller().control_type() == T::controller_type() {
@@ -139,19 +141,25 @@ impl<'b> Cgroup<'b> {
139141

140142
/// Attach a task to the control group.
141143
pub fn add_task(&self, pid: CgroupPid) -> Result<(), CgroupError> {
142-
self.subsystems().iter().try_for_each(|sub| sub.to_controller().add_task(&pid))
144+
self.subsystems()
145+
.iter()
146+
.try_for_each(|sub| sub.to_controller().add_task(&pid))
143147
}
144148

145149
/// Returns an Iterator that can be used to iterate over the tasks that are currently in the
146150
/// control group.
147151
pub fn tasks(&self) -> Vec<CgroupPid> {
148152
/* Collect the tasks from all subsystems */
149-
let mut v = self.subsystems().iter()
153+
let mut v = self
154+
.subsystems()
155+
.iter()
150156
.map(|x| x.to_controller().tasks())
151-
.fold(vec![], |mut acc, mut x| { acc.append(&mut x); acc });
157+
.fold(vec![], |mut acc, mut x| {
158+
acc.append(&mut x);
159+
acc
160+
});
152161
v.sort();
153162
v.dedup();
154163
v
155164
}
156-
157165
}

src/cpu.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
//! This module contains the implementation of the `cpu` cgroup subsystem.
2-
//!
2+
//!
33
//! See the Kernel's documentation for more information about this subsystem, found at:
44
//! [Documentation/scheduler/sched-design-CFS.txt](https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt)
55
//! paragraph 7 ("GROUP SCHEDULER EXTENSIONS TO CFS").
6-
use std::path::PathBuf;
7-
use std::io::{Read, Write};
86
use std::fs::File;
7+
use std::io::{Read, Write};
8+
use std::path::PathBuf;
99

10-
use {CgroupError, CpuResources, Controllers, Controller, Resources, ControllIdentifier, Subsystem};
10+
use {
11+
CgroupError, ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
12+
};
1113

1214
/// A controller that allows controlling the `cpu` subsystem of a Cgroup.
13-
///
15+
///
1416
/// In essence, it allows gathering information about how much the tasks inside the control group
1517
/// are using the CPU and creating rules that limit their usage. Note that this crate does not yet
1618
/// support managing realtime tasks.
1719
#[derive(Debug, Clone)]
18-
pub struct CpuController{
20+
pub struct CpuController {
1921
base: PathBuf,
2022
path: PathBuf,
2123
}
@@ -30,10 +32,18 @@ pub struct Cpu {
3032
}
3133

3234
impl Controller for CpuController {
33-
fn control_type(&self) -> Controllers { Controllers::Cpu}
34-
fn get_path(&self) -> &PathBuf { &self.path }
35-
fn get_path_mut(&mut self) -> &mut PathBuf { &mut self.path }
36-
fn get_base(&self) -> &PathBuf { &self.base }
35+
fn control_type(&self) -> Controllers {
36+
Controllers::Cpu
37+
}
38+
fn get_path(&self) -> &PathBuf {
39+
&self.path
40+
}
41+
fn get_path_mut(&mut self) -> &mut PathBuf {
42+
&mut self.path
43+
}
44+
fn get_base(&self) -> &PathBuf {
45+
&self.base
46+
}
3747

3848
fn apply(&self, res: &Resources) -> Result<(), CgroupError> {
3949
/* get the resources that apply to this controller */
@@ -74,7 +84,7 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
7484
_ => {
7585
assert_eq!(1, 0);
7686
::std::mem::uninitialized()
77-
},
87+
}
7888
}
7989
}
8090
}
@@ -102,42 +112,46 @@ impl CpuController {
102112
/// Returns CPU time statistics based on the processes in the control group.
103113
pub fn cpu(&self) -> Cpu {
104114
Cpu {
105-
stat: self.open_path("cpu.stat", false).and_then(|mut file| {
106-
let mut s = String::new();
107-
let res = file.read_to_string(&mut s);
108-
match res {
109-
Ok(_) => Ok(s),
110-
Err(e) => Err(CgroupError::ReadError(e)),
111-
}
112-
}).unwrap_or("".to_string()),
115+
stat: self
116+
.open_path("cpu.stat", false)
117+
.and_then(|mut file| {
118+
let mut s = String::new();
119+
let res = file.read_to_string(&mut s);
120+
match res {
121+
Ok(_) => Ok(s),
122+
Err(e) => Err(CgroupError::ReadError(e)),
123+
}
124+
}).unwrap_or("".to_string()),
113125
}
114126
}
115127

116128
/// Configures the CPU bandwidth (in relative relation to other control groups and this control
117129
/// group's parent).
118-
///
130+
///
119131
/// For example, setting control group `A`'s `shares` to `100`, and control group `B`'s
120132
/// `shares` to `200` ensures that control group `B` receives twice as much as CPU bandwidth.
121133
/// (Assuming both `A` and `B` are of the same parent)
122134
pub fn set_shares(&self, shares: u64) -> Result<(), CgroupError> {
123135
self.open_path("cpu.shares", true).and_then(|mut file| {
124-
file.write_all(shares.to_string().as_ref()).map_err(CgroupError::WriteError)
136+
file.write_all(shares.to_string().as_ref())
137+
.map_err(CgroupError::WriteError)
125138
})
126139
}
127140

128141
/// Retrieve the CPU bandwidth that this control group (relative to other control groups and
129142
/// this control group's parent) can use.
130143
pub fn shares(&self) -> Result<u64, CgroupError> {
131-
self.open_path("cpu.shares", false)
132-
.and_then(read_u64_from)
144+
self.open_path("cpu.shares", false).and_then(read_u64_from)
133145
}
134146

135147
/// Specify a period (when using the CFS scheduler) of time in microseconds for how often this
136148
/// control group's access to the CPU should be reallocated.
137149
pub fn set_cfs_period(&self, us: u64) -> Result<(), CgroupError> {
138-
self.open_path("cpu.cfs_period_us", true).and_then(|mut file| {
139-
file.write_all(us.to_string().as_ref()).map_err(CgroupError::WriteError)
140-
})
150+
self.open_path("cpu.cfs_period_us", true)
151+
.and_then(|mut file| {
152+
file.write_all(us.to_string().as_ref())
153+
.map_err(CgroupError::WriteError)
154+
})
141155
}
142156

143157
/// Retrieve the period of time of how often this cgroup's access to the CPU should be
@@ -150,11 +164,13 @@ impl CpuController {
150164
/// Specify a quota (when using the CFS scheduler) of time in microseconds for which all tasks
151165
/// in this control group can run during one period (see: `set_cfs_period()`).
152166
pub fn set_cfs_quota(&self, us: u64) -> Result<(), CgroupError> {
153-
self.open_path("cpu.cfs_quota_us", true).and_then(|mut file| {
154-
file.write_all(us.to_string().as_ref()).map_err(CgroupError::WriteError)
155-
})
167+
self.open_path("cpu.cfs_quota_us", true)
168+
.and_then(|mut file| {
169+
file.write_all(us.to_string().as_ref())
170+
.map_err(CgroupError::WriteError)
171+
})
156172
}
157-
173+
158174
/// Retrieve the quota of time for which all tasks in this cgroup can run during one period, in
159175
/// microseconds.
160176
pub fn cfs_quota(&self) -> Result<u64, CgroupError> {

src/cpuacct.rs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! This module contains the implementation of the `cpuacct` cgroup subsystem.
2-
//!
2+
//!
33
//! See the Kernel's documentation for more information about this subsystem, found at:
44
//! [Documentation/cgroup-v1/cpuacct.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt)
5-
use std::path::PathBuf;
6-
use std::io::{Read, Write};
75
use std::fs::File;
6+
use std::io::{Read, Write};
7+
use std::path::PathBuf;
88

9-
use {CgroupError, Controllers, Resources, Subsystem, ControllIdentifier, Controller};
9+
use {CgroupError, ControllIdentifier, Controller, Controllers, Resources, Subsystem};
1010

1111
/// A controller that allows controlling the `cpuacct` subsystem of a Cgroup.
1212
///
@@ -50,10 +50,18 @@ pub struct CpuAcct {
5050
}
5151

5252
impl Controller for CpuAcctController {
53-
fn control_type(&self) -> Controllers { Controllers::CpuAcct }
54-
fn get_path(&self) -> &PathBuf { &self.path }
55-
fn get_path_mut(&mut self) -> &mut PathBuf { &mut self.path }
56-
fn get_base(&self) -> &PathBuf { &self.base }
53+
fn control_type(&self) -> Controllers {
54+
Controllers::CpuAcct
55+
}
56+
fn get_path(&self) -> &PathBuf {
57+
&self.path
58+
}
59+
fn get_path_mut(&mut self) -> &mut PathBuf {
60+
&mut self.path
61+
}
62+
fn get_base(&self) -> &PathBuf {
63+
&self.base
64+
}
5765

5866
fn apply(&self, _res: &Resources) -> Result<(), CgroupError> {
5967
Ok(())
@@ -74,7 +82,7 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
7482
_ => {
7583
assert_eq!(1, 0);
7684
::std::mem::uninitialized()
77-
},
85+
}
7886
}
7987
}
8088
}
@@ -101,7 +109,6 @@ fn read_string_from(mut file: File) -> Result<String, CgroupError> {
101109
}
102110

103111
impl CpuAcctController {
104-
105112
/// Contructs a new `CpuAcctController` with `oroot` serving as the root of the control group.
106113
pub fn new(oroot: PathBuf) -> Self {
107114
let mut root = oroot;
@@ -115,32 +122,44 @@ impl CpuAcctController {
115122
/// Gathers the statistics that are available in the control group into a `CpuAcct` structure.
116123
pub fn cpuacct(&self) -> CpuAcct {
117124
CpuAcct {
118-
stat: self.open_path("cpuacct.stat", false)
119-
.and_then(|file| read_string_from(file)).unwrap_or("".to_string()),
120-
usage: self.open_path("cpuacct.usage", false)
121-
.and_then(|file| read_u64_from(file))
122-
.unwrap_or(0),
123-
usage_all: self.open_path("cpuacct.usage_all", false)
124-
.and_then(|file| read_string_from(file)).unwrap_or("".to_string()),
125-
usage_percpu: self.open_path("cpuacct.usage_percpu", false)
126-
.and_then(|file| read_string_from(file)).unwrap_or("".to_string()),
127-
usage_percpu_sys: self.open_path("cpuacct.usage_percpu_sys", false)
128-
.and_then(|file| read_string_from(file)).unwrap_or("".to_string()),
129-
usage_percpu_user: self.open_path("cpuacct.usage_percpu_user", false)
130-
.and_then(|file| read_string_from(file)).unwrap_or("".to_string()),
131-
usage_sys: self.open_path("cpuacct.usage_sys", false)
132-
.and_then(|file| read_u64_from(file))
133-
.unwrap_or(0),
134-
usage_user: self.open_path("cpuacct.usage_user", false)
135-
.and_then(|file| read_u64_from(file))
136-
.unwrap_or(0),
125+
stat: self
126+
.open_path("cpuacct.stat", false)
127+
.and_then(|file| read_string_from(file))
128+
.unwrap_or("".to_string()),
129+
usage: self
130+
.open_path("cpuacct.usage", false)
131+
.and_then(|file| read_u64_from(file))
132+
.unwrap_or(0),
133+
usage_all: self
134+
.open_path("cpuacct.usage_all", false)
135+
.and_then(|file| read_string_from(file))
136+
.unwrap_or("".to_string()),
137+
usage_percpu: self
138+
.open_path("cpuacct.usage_percpu", false)
139+
.and_then(|file| read_string_from(file))
140+
.unwrap_or("".to_string()),
141+
usage_percpu_sys: self
142+
.open_path("cpuacct.usage_percpu_sys", false)
143+
.and_then(|file| read_string_from(file))
144+
.unwrap_or("".to_string()),
145+
usage_percpu_user: self
146+
.open_path("cpuacct.usage_percpu_user", false)
147+
.and_then(|file| read_string_from(file))
148+
.unwrap_or("".to_string()),
149+
usage_sys: self
150+
.open_path("cpuacct.usage_sys", false)
151+
.and_then(|file| read_u64_from(file))
152+
.unwrap_or(0),
153+
usage_user: self
154+
.open_path("cpuacct.usage_user", false)
155+
.and_then(|file| read_u64_from(file))
156+
.unwrap_or(0),
137157
}
138158
}
139159

140160
/// Reset the statistics the kernel has gathered about the control group.
141161
pub fn reset(&self) -> Result<(), CgroupError> {
142-
self.open_path("cpuacct.usage", true).and_then(|mut file| {
143-
file.write_all(b"0").map_err(CgroupError::WriteError)
144-
})
162+
self.open_path("cpuacct.usage", true)
163+
.and_then(|mut file| file.write_all(b"0").map_err(CgroupError::WriteError))
145164
}
146165
}

0 commit comments

Comments
 (0)