Skip to content

Commit

Permalink
Fix all warnings from the compiler
Browse files Browse the repository at this point in the history
This change simply fixes all the warnings from the latest version of the
compiler. It also removes the (non-test code) unsafe blocks. One of the
warnings I think was actually a bug - a cgroup builder could fail to
apply resource constraints but still return a Cgroup silently. Now a
builder will return an Error correctly in this case - this is an API
change though so requires a mv bump.
  • Loading branch information
campbellC committed Sep 6, 2020
1 parent ed1e816 commit 66b0e32
Show file tree
Hide file tree
Showing 25 changed files with 369 additions and 452 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository = "https://github.com/levex/cgroups-rs"
keywords = ["linux", "cgroup", "containers", "isolation"]
categories = ["os", "api-bindings", "os::unix-apis"]
license = "MIT OR Apache-2.0"
version = "0.1.1-alpha.0"
version = "0.2.1-alpha.0"
authors = ["Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
edition = "2018"

Expand Down
85 changes: 20 additions & 65 deletions src/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;

use crate::error::*;
use crate::error::ErrorKind::*;
use crate::error::*;

use crate::{
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
Expand Down Expand Up @@ -279,7 +279,8 @@ impl ControllerInternal for BlkIoController {

for dev in &res.weight_device {
let _ = self.set_weight_for_device(dev.major, dev.minor, dev.weight as u64);
let _ = self.set_leaf_weight_for_device(dev.major, dev.minor, dev.leaf_weight as u64);
let _ =
self.set_leaf_weight_for_device(dev.major, dev.minor, dev.leaf_weight as u64);
}

for dev in &res.throttle_read_bps_device {
Expand Down Expand Up @@ -309,19 +310,7 @@ impl ControllIdentifier for BlkIoController {
}
}

impl<'a> From<&'a Subsystem> for &'a BlkIoController {
fn from(sub: &'a Subsystem) -> &'a BlkIoController {
unsafe {
match sub {
Subsystem::BlkIo(c) => c,
_ => {
assert_eq!(1, 0);
::std::mem::uninitialized()
}
}
}
}
}
impl_from_subsystem_for_controller!(Subsystem::BlkIo, BlkIoController);

fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
Expand All @@ -334,7 +323,10 @@ fn read_string_from(mut file: File) -> Result<String> {
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string.trim().parse().map_err(|e| Error::with_cause(ParseError, e)),
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
Expand Down Expand Up @@ -588,12 +580,7 @@ impl BlkIoController {
}

/// Same as `set_leaf_weight()`, but settable per each block device.
pub fn set_leaf_weight_for_device(
&self,
major: u64,
minor: u64,
weight: u64,
) -> Result<()> {
pub fn set_leaf_weight_for_device(&self, major: u64, minor: u64, weight: u64) -> Result<()> {
self.open_path("blkio.leaf_weight_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, weight).as_ref())
Expand All @@ -612,12 +599,7 @@ impl BlkIoController {

/// Throttle the bytes per second rate of read operation affecting the block device
/// `major:minor` to `bps`.
pub fn throttle_read_bps_for_device(
&self,
major: u64,
minor: u64,
bps: u64,
) -> Result<()> {
pub fn throttle_read_bps_for_device(&self, major: u64, minor: u64, bps: u64) -> Result<()> {
self.open_path("blkio.throttle.read_bps_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, bps).to_string().as_ref())
Expand All @@ -627,12 +609,7 @@ impl BlkIoController {

/// Throttle the I/O operations per second rate of read operation affecting the block device
/// `major:minor` to `bps`.
pub fn throttle_read_iops_for_device(
&self,
major: u64,
minor: u64,
iops: u64,
) -> Result<()> {
pub fn throttle_read_iops_for_device(&self, major: u64, minor: u64, iops: u64) -> Result<()> {
self.open_path("blkio.throttle.read_iops_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, iops).to_string().as_ref())
Expand All @@ -641,12 +618,7 @@ impl BlkIoController {
}
/// Throttle the bytes per second rate of write operation affecting the block device
/// `major:minor` to `bps`.
pub fn throttle_write_bps_for_device(
&self,
major: u64,
minor: u64,
bps: u64,
) -> Result<()> {
pub fn throttle_write_bps_for_device(&self, major: u64, minor: u64, bps: u64) -> Result<()> {
self.open_path("blkio.throttle.write_bps_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, bps).to_string().as_ref())
Expand All @@ -656,12 +628,7 @@ impl BlkIoController {

/// Throttle the I/O operations per second rate of write operation affecting the block device
/// `major:minor` to `bps`.
pub fn throttle_write_iops_for_device(
&self,
major: u64,
minor: u64,
iops: u64,
) -> Result<()> {
pub fn throttle_write_iops_for_device(&self, major: u64, minor: u64, iops: u64) -> Result<()> {
self.open_path("blkio.throttle.write_iops_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, iops).to_string().as_ref())
Expand All @@ -671,20 +638,14 @@ impl BlkIoController {

/// Set the weight of the control group's tasks.
pub fn set_weight(&self, w: u64) -> Result<()> {
self.open_path("blkio.weight", true)
.and_then(|mut file| {
file.write_all(w.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
self.open_path("blkio.weight", true).and_then(|mut file| {
file.write_all(w.to_string().as_ref())
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}

/// Same as `set_weight()`, but settable per each block device.
pub fn set_weight_for_device(
&self,
major: u64,
minor: u64,
weight: u64,
) -> Result<()> {
pub fn set_weight_for_device(&self, major: u64, minor: u64, weight: u64) -> Result<()> {
self.open_path("blkio.weight_device", true)
.and_then(|mut file| {
file.write_all(format!("{}:{} {}", major, minor, weight).as_ref())
Expand Down Expand Up @@ -755,10 +716,7 @@ Total 61823067136
#[test]
fn test_parse_io_service_total() {
let ok = parse_io_service_total(TEST_VALUE.to_string()).unwrap();
assert_eq!(
ok,
61823067136
);
assert_eq!(ok, 61823067136);
}

#[test]
Expand Down Expand Up @@ -806,10 +764,7 @@ Total 61823067136
]
);
let err = parse_io_service(TEST_WRONG_VALUE.to_string()).unwrap_err();
assert_eq!(
err.kind(),
&ErrorKind::ParseError,
);
assert_eq!(err.kind(), &ErrorKind::ParseError,);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Cgroup<'b> {
subsystems: Vec<Subsystem>,

/// The hierarchy.
hier: &'b Hierarchy,
hier: &'b dyn Hierarchy,
}

impl<'b> Cgroup<'b> {
Expand All @@ -41,7 +41,7 @@ impl<'b> Cgroup<'b> {
///
/// Note that if the handle goes out of scope and is dropped, the control group is _not_
/// destroyed.
pub fn new<P: AsRef<Path>>(hier: &Hierarchy, path: P) -> Cgroup {
pub fn new<P: AsRef<Path>>(hier: &dyn Hierarchy, path: P) -> Cgroup {
let cg = Cgroup::load(hier, path);
cg.create();
cg
Expand All @@ -54,7 +54,7 @@ impl<'b> Cgroup<'b> {
///
/// Note that if the handle goes out of scope and is dropped, the control group is _not_
/// destroyed.
pub fn load<P: AsRef<Path>>(hier: &Hierarchy, path: P) -> Cgroup {
pub fn load<P: AsRef<Path>>(hier: &dyn Hierarchy, path: P) -> Cgroup {
let path = path.as_ref();
let mut subsystems = hier.subsystems();
if path.as_os_str() != "" {
Expand Down
Loading

0 comments on commit 66b0e32

Please sign in to comment.