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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Real system metrics collection from /proc filesystem.
use std::time::Instant;
use super::hardware::{DiskMetrics, MemoryBreakdown, NetworkMetrics};
use super::CbtopApp;
impl CbtopApp {
/// Read memory breakdown from /proc/meminfo (PMAT-012 UI-04)
pub(super) fn read_memory() -> MemoryBreakdown {
#[cfg(target_os = "linux")]
{
if let Ok(contents) = std::fs::read_to_string("/proc/meminfo") {
let mut mem = MemoryBreakdown::default();
for line in contents.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
let value: u64 = parts[1].parse().unwrap_or(0);
match parts[0] {
"MemTotal:" => mem.total_kb = value,
"MemAvailable:" => mem.available_kb = value,
"Buffers:" => mem.buffers_kb = value,
"Cached:" => mem.cached_kb = value,
_ => {}
}
}
}
mem.used_kb = mem.total_kb.saturating_sub(mem.available_kb);
return mem;
}
}
MemoryBreakdown::default()
}
/// Read network metrics from /proc/net/dev (PMAT-012 UI-07 P2)
pub(super) fn read_network(&mut self) -> NetworkMetrics {
#[cfg(target_os = "linux")]
{
if let Ok(contents) = std::fs::read_to_string("/proc/net/dev") {
let mut total_rx: u64 = 0;
let mut total_tx: u64 = 0;
for line in contents.lines().skip(2) {
// Skip header lines
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 10 {
let iface = parts[0].trim_end_matches(':');
// Skip loopback
if iface == "lo" {
continue;
}
let rx: u64 = parts[1].parse().unwrap_or(0);
let tx: u64 = parts[9].parse().unwrap_or(0);
total_rx += rx;
total_tx += tx;
}
}
let now = Instant::now();
let (rx_rate, tx_rate) =
if let Some((prev_rx, prev_tx, prev_time)) = self.last_network_stat {
let elapsed = now.duration_since(prev_time).as_secs_f64();
if elapsed > 0.0 {
let rx_delta = total_rx.saturating_sub(prev_rx) as f64;
let tx_delta = total_tx.saturating_sub(prev_tx) as f64;
(rx_delta / elapsed, tx_delta / elapsed)
} else {
(0.0, 0.0)
}
} else {
(0.0, 0.0)
};
self.last_network_stat = Some((total_rx, total_tx, now));
return NetworkMetrics {
rx_bytes: total_rx,
tx_bytes: total_tx,
rx_rate,
tx_rate,
};
}
}
NetworkMetrics::default()
}
/// Read disk metrics using statvfs (PMAT-012 UI-08 P2)
pub(super) fn read_disks() -> Vec<DiskMetrics> {
let mut disks = Vec::new();
#[cfg(target_os = "linux")]
{
// Read mounts from /proc/mounts and get stats for common ones
if let Ok(contents) = std::fs::read_to_string("/proc/mounts") {
for line in contents.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
let mount = parts[1];
let fstype = parts.get(2).unwrap_or(&"");
// Only include real filesystems on common mounts
if !matches!(*fstype, "ext4" | "xfs" | "btrfs" | "zfs" | "ntfs" | "vfat") {
continue;
}
// Skip if not a standard mount
if !mount.starts_with("/home") && mount != "/" && !mount.starts_with("/mnt")
{
continue;
}
// Use nix or libc statvfs
#[cfg(unix)]
{
use std::ffi::CString;
use std::mem::MaybeUninit;
if let Ok(c_path) = CString::new(mount) {
let mut stat = MaybeUninit::<libc::statvfs>::uninit();
let result =
unsafe { libc::statvfs(c_path.as_ptr(), stat.as_mut_ptr()) };
if result == 0 {
let stat = unsafe { stat.assume_init() };
let block_size = stat.f_frsize;
let total = stat.f_blocks * block_size;
let available = stat.f_bavail * block_size;
let used = total.saturating_sub(available);
let usage_pct = if total > 0 {
(used as f64 / total as f64) * 100.0
} else {
0.0
};
disks.push(DiskMetrics {
mount: mount.to_string(),
total_bytes: total,
used_bytes: used,
usage_percent: usage_pct,
});
// Limit to 3 disks for display
if disks.len() >= 3 {
break;
}
}
}
}
}
}
}
}
disks
}
/// Read real CPU usage from /proc/stat (aggregate and per-core)
pub(super) fn read_cpu_usage(&mut self) -> f64 {
#[cfg(target_os = "linux")]
{
if let Ok(contents) = std::fs::read_to_string("/proc/stat") {
let mut aggregate_usage = 0.0;
let mut per_core: Vec<f64> = Vec::new();
for line in contents.lines() {
if line.starts_with("cpu") {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 5 {
let user: u64 = parts[1].parse().unwrap_or(0);
let nice: u64 = parts[2].parse().unwrap_or(0);
let system: u64 = parts[3].parse().unwrap_or(0);
let idle: u64 = parts[4].parse().unwrap_or(0);
let total = user + nice + system + idle;
let active = user + nice + system;
if parts[0] == "cpu" {
// Aggregate CPU line
if let Some((prev_active, prev_total)) = self.last_cpu_stat {
let delta_active = active.saturating_sub(prev_active);
let delta_total = total.saturating_sub(prev_total);
if delta_total > 0 {
aggregate_usage =
(delta_active as f64 / delta_total as f64) * 100.0;
}
}
self.last_cpu_stat = Some((active, total));
} else if parts[0].starts_with("cpu") {
// Per-core CPU line (cpu0, cpu1, etc.)
// Calculate instantaneous usage (simplified - no delta tracking per core)
if total > 0 {
per_core.push((active as f64 / total as f64) * 100.0);
}
}
}
}
}
self.load_metrics.per_core_usage = per_core;
return aggregate_usage;
}
}
// Fallback for non-Linux or on error
0.0
}
}