1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright ⓒ 2019-2020 Lewis Belcher
// Licensed under the MIT license (see LICENSE or <http://opensource.org/licenses/MIT>).
// All files in the project carrying such notice may not be copied, modified, or
// distributed except according to those terms

use crate::blocks::{Block, Configure, Message, Sender};
use crate::{ema, utils};
use regex::Regex;
use serde::Deserialize;
use std::thread;

const MEMPATH: &str = "/proc/meminfo";
const PATTERN: &str = r"(?s)MemTotal:\s+(\d+).+MemFree:\s+(\d+)";

#[derive(Configure, Deserialize)]
pub struct Memory {
	#[serde(default = "default_name")]
	name: String,
	#[serde(default = "default_period")]
	period: f32,
	#[serde(default = "default_alpha")]
	alpha: f32,
}

fn default_name() -> String {
	"memory".to_string()
}

fn default_period() -> f32 {
	1.0
}

fn default_alpha() -> f32 {
	0.5
}

impl Sender for Memory {
	fn add_sender(&self, channel: crossbeam_channel::Sender<Message>) {
		let name = self.get_name();
		let monitor = utils::monitor_file(MEMPATH.to_string(), self.period);
		let mut mem = ema::Ema::new(self.alpha);
		let mut block = Block::new(name.clone(), true);

		thread::spawn(move || {
			for text in monitor {
				let perc = get_mem_percentage(match_mem_stats(&text));
				block.full_text = Some(format!(" {:.1}%", mem.push(perc) * 100.0));
				channel.send((name.clone(), block.to_string())).unwrap();
			}
		});
	}
}

#[derive(Debug, PartialEq)]
struct MemStats {
	total: f64,
	free: f64,
}

fn match_mem_stats(s: &str) -> MemStats {
	lazy_static! {
		static ref RE: Regex = Regex::new(PATTERN).unwrap();
	}
	let caps = RE.captures(s).unwrap();
	MemStats {
		total: caps.get(1).unwrap().as_str().parse().unwrap(),
		free: caps.get(2).unwrap().as_str().parse().unwrap(),
	}
}

fn get_mem_percentage(mem: MemStats) -> f32 {
	1.0 - mem.free as f32 / mem.total as f32
}

#[cfg(test)]
mod tests {
	use super::*;
	const MEMFILE: &str = "MemTotal:  16134372 kB\nMemFree:  2757408 kB\n";

	#[test]
	fn regex_matches() {
		let mem = match_mem_stats(&MEMFILE);
		assert_eq!(
			mem,
			MemStats {
				total: 16134372.0,
				free: 2757408.0
			}
		);
	}
}