Skip to content

Commit 502732b

Browse files
committed
Now we have the same values as free! May be an issue with used though.
1 parent a0c35d1 commit 502732b

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/freer/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use prettytable::format::FormatBuilder;
99

1010
fn main () {
1111
// Build the minfo
12-
let minfo = match meminfo::MeminfoStatus::new() {
12+
let minfo = match meminfo::Meminfo::new() {
1313
Ok(minfo) => minfo,
1414
Err(err) => { println!("ERROR, {:?}", err); return },
1515
};
16-
// make a debug option that prints this nicely.
17-
//println!("{:?}", minfo);
16+
// println!("{:?}", minfo);
1817
// Make it look like this :)
1918
// total used free shared buff/cache available
2019
// Mem: 12202716 1666600 957368 401652 9578748 9989056
@@ -23,18 +22,17 @@ fn main () {
2322
// Start building the table
2423
let mut table = Table::new();
2524
// Need to calculate used from other things
26-
table.add_row(row!["Mem:", minfo.memtotal, 0, minfo.memfree]);
25+
table.add_row(row!["Mem:", minfo.memtotal, minfo.mainused, minfo.memfree, minfo.shmem, minfo.maincached, minfo.memavailable]);
26+
table.add_row(row!["Swap:", minfo.swaptotal, minfo.mainswapused, minfo.swapfree]);
2727
// Make a format for it
2828
let format = FormatBuilder::new()
2929
.column_separator(' ')
30-
.padding(0, 12)
30+
.padding(0, 3)
3131
.build();
3232
table.set_format(format);
33-
table.set_titles(row!["", "total", "used", "free"]);
33+
table.set_titles(row!["", "total", "used", "free", "shared", "buff/cache", "available"]);
3434
table.printstd();
3535

36-
// Now push data into it and display
37-
3836

3937
}
4038

src/procrs/meminfo/mod.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl From<io::Error> for MeminfoError {
2626
}
2727

2828
#[derive(Debug)]
29-
pub struct MeminfoStatus {
29+
pub struct Meminfo {
3030
pub memtotal: u64,
3131
pub memfree: u64,
3232
pub memavailable: u64,
@@ -71,14 +71,17 @@ pub struct MeminfoStatus {
7171
pub directmap4k: u64,
7272
pub directmap2m: u64,
7373
pub directmap1g: u64,
74+
pub mainused: u64,
75+
pub maincached: u64,
76+
pub mainswapused: u64,
7477
}
7578

7679

7780
/// Parses the contents of /proc/meminfo into a new Meminfo structure
7881
///
7982
/// # Examples
8083
81-
impl MeminfoStatus {
84+
impl Meminfo {
8285
pub fn new() -> Result<Self, MeminfoError> {
8386
// Create an interim hashmap
8487
// Read the file?
@@ -88,7 +91,26 @@ impl MeminfoStatus {
8891
let lines = try!(io::BufReader::new(minfo_file)
8992
.lines() // We have a Lines of many Result<&str>
9093
.collect::<Result<Vec<_>, _>>()); // This line makes Result<vec<&str>> Or result<err>
91-
let hmap = try!(lines.iter().map(|line| Self::parse_line(line)).collect::<Result<HashMap<_, _>, _>>() );
94+
let mut hmap = try!(lines.iter().map(|line| Self::parse_line(line)).collect::<Result<HashMap<_, _>, _>>() );
95+
// Calculate some of the other values
96+
// kb_main_used = kb_main_total - kb_main_free - kb_main_cached - kb_main_buffe
97+
let total = hmap.get("MemTotal").unwrap().clone();
98+
let free = hmap.get("MemFree").unwrap().clone();
99+
let cached = hmap.get("Cached").unwrap().clone();
100+
let buffer = hmap.get("Buffers").unwrap().clone();
101+
let used = total - free - cached - buffer;
102+
hmap.insert("MainUsed".to_owned(), used);
103+
104+
// kb_main_cached = kb_page_cache + kb_slab
105+
let page_cache = hmap.get("Cached").unwrap().clone();
106+
let slab = hmap.get("Slab").unwrap().clone();
107+
hmap.insert("MainCached".to_owned(), (page_cache + slab) );
108+
109+
// kb_swap_used = kb_swap_total - kb_swap_free
110+
let swap_total = hmap.get("SwapTotal").unwrap().clone();
111+
let swap_free = hmap.get("SwapFree").unwrap().clone();
112+
hmap.insert("MainSwapUsed".to_owned(), (swap_total - swap_free));
113+
92114
// Populate the results
93115
Self::build_minfo(hmap)
94116
}
@@ -104,9 +126,9 @@ impl MeminfoStatus {
104126
}
105127

106128
//This then takes the values out and puts them into an minfo
107-
fn build_minfo(hmap: HashMap<String, u64>) -> Result<MeminfoStatus, MeminfoError> {
129+
fn build_minfo(hmap: HashMap<String, u64>) -> Result<Meminfo, MeminfoError> {
108130
// REALLY REALLY improve this handling of Option types ...
109-
let minfo = MeminfoStatus {
131+
let minfo = Meminfo {
110132
memtotal: hmap.get("MemTotal").unwrap().clone(),
111133
memfree: hmap.get("MemFree").unwrap().clone(),
112134
memavailable: hmap.get("MemAvailable").unwrap().clone(),
@@ -151,14 +173,17 @@ impl MeminfoStatus {
151173
directmap4k: hmap.get("DirectMap4k").unwrap().clone(),
152174
directmap2m: hmap.get("DirectMap2M").unwrap().clone(),
153175
directmap1g: hmap.get("DirectMap1G").unwrap().clone(),
176+
mainused: hmap.get("MainUsed").unwrap().clone(),
177+
maincached: hmap.get("MainCached").unwrap().clone(),
178+
mainswapused: hmap.get("MainSwapUsed").unwrap().clone(),
154179
};
155180
Ok(minfo)
156181
}
157182

158183
}
159184

160185

161-
impl fmt::Display for MeminfoStatus {
186+
impl fmt::Display for Meminfo {
162187
// make a display method to dump the whole struct
163188
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164189
// This won't be nice for all the values we have ...

0 commit comments

Comments
 (0)