Skip to content

Commit c8ae2d5

Browse files
committed
use bytesize
1 parent c16a7d6 commit c8ae2d5

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ repository = "https://github.com/myfreeweb/systemstat"
1313
libc = "0.2"
1414
chrono = "0.2"
1515
lazy_static = "0.1"
16+
bytesize = "0.1"

examples/info.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ fn main() {
1010
let mounts = sys.mounts().unwrap();
1111
println!("\nMounts:");
1212
for mount in mounts.iter() {
13-
println!("{} ---{}---> {} (available {} of {} bytes)",
14-
mount.fs_mounted_from, mount.fs_type, mount.fs_mounted_on, mount.avail_bytes, mount.total_bytes);
13+
println!("{} ---{}---> {} (available {} of {})",
14+
mount.fs_mounted_from, mount.fs_type, mount.fs_mounted_on, mount.avail, mount.total);
1515
}
1616

1717
let netifs = sys.networks().unwrap();
@@ -21,8 +21,8 @@ fn main() {
2121
}
2222

2323
let mem = sys.memory().unwrap();
24-
println!("\nMemory: {} KiB active, {} KiB inact, {} KiB wired, {} KiB cache, {} KiB free",
25-
mem.active_kb, mem.inactive_kb, mem.wired_kb, mem.cache_kb, mem.free_kb);
24+
println!("\nMemory: {} active, {} inact, {} wired, {} cache, {} free",
25+
mem.active, mem.inactive, mem.wired, mem.cache, mem.free);
2626

2727
let loadavg = sys.load_average().unwrap();
2828
println!("\nLoad average: {} {} {}", loadavg.one, loadavg.five, loadavg.fifteen);

src/data.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use std::io;
66
use std::net::{Ipv4Addr, Ipv6Addr};
7+
pub use bytesize::ByteSize;
78

89
/// A wrapper for a measurement that takes time.
910
///
@@ -52,20 +53,20 @@ pub struct LoadAverage {
5253

5354
#[derive(Debug, Clone)]
5455
pub struct Memory {
55-
pub active_kb: usize,
56-
pub inactive_kb: usize,
57-
pub wired_kb: usize,
58-
pub cache_kb: usize,
59-
pub free_kb: usize,
56+
pub active: ByteSize,
57+
pub inactive: ByteSize,
58+
pub wired: ByteSize,
59+
pub cache: ByteSize,
60+
pub free: ByteSize,
6061
}
6162

6263
#[derive(Debug, Clone)]
6364
pub struct Filesystem {
64-
pub files: u64,
65-
pub free_bytes: u64,
66-
pub avail_bytes: u64,
67-
pub total_bytes: u64,
68-
pub name_max: u64,
65+
pub files: usize,
66+
pub free: ByteSize,
67+
pub avail: ByteSize,
68+
pub total: ByteSize,
69+
pub name_max: usize,
6970
pub fs_type: String,
7071
pub fs_mounted_from: String,
7172
pub fs_mounted_on: String,

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! network interfaces, etc.
33
44
extern crate libc;
5+
extern crate bytesize;
56
#[macro_use] extern crate lazy_static;
67

78
pub mod platform;

src/platform/freebsd.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ impl Platform for PlatformImpl {
9696
let mut cache: usize = 0; sysctl!(V_CACHE_COUNT, &mut cache, mem::size_of::<usize>());
9797
let mut free: usize = 0; sysctl!(V_FREE_COUNT, &mut free, mem::size_of::<usize>());
9898
Ok(Memory {
99-
active_kb: active << *PAGESHIFT,
100-
inactive_kb: inactive << *PAGESHIFT,
101-
wired_kb: wired << *PAGESHIFT,
102-
cache_kb: cache << *PAGESHIFT,
103-
free_kb: free << *PAGESHIFT,
99+
active: ByteSize::kib(active << *PAGESHIFT),
100+
inactive: ByteSize::kib(inactive << *PAGESHIFT),
101+
wired: ByteSize::kib(wired << *PAGESHIFT),
102+
cache: ByteSize::kib(cache << *PAGESHIFT),
103+
free: ByteSize::kib(free << *PAGESHIFT),
104104
})
105105
}
106106

@@ -252,11 +252,11 @@ struct statfs {
252252
impl statfs {
253253
fn to_fs(&self) -> Filesystem {
254254
Filesystem {
255-
files: self.f_files - self.f_ffree as u64,
256-
free_bytes: self.f_bfree as u64 * self.f_bsize,
257-
avail_bytes: self.f_bavail as u64 * self.f_bsize,
258-
total_bytes: self.f_blocks as u64 * self.f_bsize,
259-
name_max: self.f_namemax as u64,
255+
files: self.f_files as usize - self.f_ffree as usize,
256+
free: ByteSize::b(self.f_bfree as usize * self.f_bsize as usize),
257+
avail: ByteSize::b(self.f_bavail as usize * self.f_bsize as usize),
258+
total: ByteSize::b(self.f_blocks as usize * self.f_bsize as usize),
259+
name_max: self.f_namemax as usize,
260260
fs_type: unsafe { ffi::CStr::from_ptr(&self.f_fstypename[0]).to_string_lossy().into_owned() },
261261
fs_mounted_from: unsafe { ffi::CStr::from_ptr(&self.f_mntfromname[0]).to_string_lossy().into_owned() },
262262
fs_mounted_on: unsafe { ffi::CStr::from_ptr(&self.f_mntonname[0]).to_string_lossy().into_owned() },

src/platform/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod tests {
4343
#[test]
4444
fn test_memory() {
4545
let mem = PlatformImpl::new().memory().unwrap();
46-
assert!(mem.free_kb > 100 && mem.active_kb > 100);
46+
assert!(mem.free.as_usize() > 1024 && mem.active.as_usize() > 1024);
4747
}
4848

4949
#[test]

0 commit comments

Comments
 (0)