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
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
#[derive(Debug)]
pub struct IoLoad {
pub read_io: f64,
pub read_merges: f64,
pub read_sectors: f64,
pub read_ticks: f64,
pub write_io: f64,
pub write_merges: f64,
pub write_sectors: f64,
pub write_ticks: f64,
pub in_flight: f64,
pub io_ticks: f64,
pub time_in_queue: f64,
pub discard_io: Option<f64>,
pub discard_merged: Option<f64>,
pub discard_sectors: Option<f64>,
pub discard_ticks: Option<f64>,
}
impl IoLoad {
#[cfg(not(unix))]
pub fn snapshot() -> HashMap<String, NICLoad> {
HashMap::new()
}
#[cfg(unix)]
pub fn snapshot() -> HashMap<String, IoLoad> {
let mut result = HashMap::new();
if let Ok(dir) = std::fs::read_dir("/sys/block/") {
for entry in dir.flatten() {
let stat = entry.path().join("stat");
let mut s = String::new();
if File::open(stat)
.and_then(|mut f| f.read_to_string(&mut s))
.is_err()
{
continue;
}
let parts = s
.split_whitespace()
.map(|w| w.parse().unwrap_or_default())
.collect::<Vec<f64>>();
if parts.len() < 11 {
continue;
}
let load = IoLoad {
read_io: parts[0],
read_merges: parts[1],
read_sectors: parts[2],
read_ticks: parts[3],
write_io: parts[4],
write_merges: parts[5],
write_sectors: parts[6],
write_ticks: parts[7],
in_flight: parts[8],
io_ticks: parts[9],
time_in_queue: parts[10],
discard_io: parts.get(11).cloned(),
discard_merged: parts.get(12).cloned(),
discard_sectors: parts.get(13).cloned(),
discard_ticks: parts.get(14).cloned(),
};
result.insert(format!("{:?}", entry.file_name()), load);
}
}
result
}
}