-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
swap.rs
149 lines (122 loc) 路 4.04 KB
/
swap.rs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::str::FromStr;
use heim_common::prelude::*;
use heim_common::units::{information, Information};
use heim_runtime::fs;
static PROC_VMSTAT: &str = "/proc/vmstat";
static PROC_MEMINFO: &str = "/proc/meminfo";
#[derive(Debug, Default, Clone)]
pub struct VmStat {
swap_in: Option<Information>, // pswpin
swap_out: Option<Information>, // pswpout
}
impl FromStr for VmStat {
type Err = Error;
fn from_str(vmstat: &str) -> Result<Self> {
let mut stat = VmStat::default();
for line in vmstat.lines() {
let first_bytes = &line.as_bytes()[..2];
if first_bytes != b"ps" {
continue;
}
let mut parts = line.splitn(2, ' ');
let field = match parts.next() {
Some("pswpin") => &mut stat.swap_in,
Some("pswpout") => &mut stat.swap_out,
_ => continue,
};
match parts.next() {
Some(value) => {
*field = {
let bytes = match value.trim_start().splitn(2, ' ').next() {
Some(kbytes) => {
// Values are expressed in 4 kilo bytes, we want bytes instead.
// Source: psutil
let value = kbytes.parse::<u64>()?;
Information::new::<information::kilobyte>(4 * value)
}
None => continue,
};
Some(bytes)
}
}
None => continue,
}
}
Ok(stat)
}
}
#[derive(Debug, Clone)]
pub struct Swap {
total: Information, // SwapTotal
free: Information, // SwapFree
vm_stat: VmStat,
}
impl Swap {
pub fn total(&self) -> Information {
self.total
}
pub fn used(&self) -> Information {
self.total - self.free
}
pub fn free(&self) -> Information {
self.free
}
pub fn sin(&self) -> Option<Information> {
self.vm_stat.swap_in
}
pub fn sout(&self) -> Option<Information> {
self.vm_stat.swap_out
}
pub fn parse_str(meminfo: &str, vm_stat: VmStat) -> Result<Self> {
let mut swap = Swap {
total: Information::new::<information::byte>(0),
free: Information::new::<information::byte>(0),
vm_stat,
};
let mut matched_lines = 0u8;
for line in meminfo.lines() {
// If line does not starts with "Sw" we do not need that key at all
let first_bytes = &line.as_bytes()[..2];
if first_bytes != b"Sw" {
continue;
}
let mut parts = line.splitn(2, ':');
let field = match parts.next() {
Some("SwapTotal") => &mut swap.total,
Some("SwapFree") => &mut swap.free,
_ => continue,
};
match parts.next() {
Some(value) => {
*field = {
let bytes = match value.trim_start().splitn(2, ' ').next() {
Some(kbytes) => {
let value = kbytes.parse::<u64>()?;
Information::new::<information::kilobyte>(value)
}
None => continue,
};
matched_lines += 1;
bytes
}
}
None => continue,
}
if matched_lines == 2 {
return Ok(swap);
}
}
Err(Error::missing_entity("<unknown>"))
}
}
async fn vm_stat() -> Result<VmStat> {
fs::read_into(PROC_VMSTAT).await
}
pub async fn swap() -> Result<Swap> {
let (meminfo, vm_stat) = future::try_join(
fs::read_to_string(PROC_MEMINFO).map_err(Into::into),
vm_stat(),
)
.await?;
Swap::parse_str(&meminfo, vm_stat)
}