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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
//! This is the main file to house data collection functions.
use std::{collections::HashMap, time::Instant};
use sysinfo::{System, SystemExt};
use crate::app::layout_manager::UsedWidgets;
use futures::join;
pub mod cpu;
pub mod disks;
pub mod mem;
pub mod network;
pub mod processes;
pub mod temperature;
#[derive(Clone, Debug)]
pub struct Data {
pub cpu: cpu::CPUHarvest,
pub memory: mem::MemHarvest,
pub swap: mem::MemHarvest,
pub temperature_sensors: Vec<temperature::TempHarvest>,
pub network: network::NetworkHarvest,
pub list_of_processes: Vec<processes::ProcessHarvest>,
pub disks: Vec<disks::DiskHarvest>,
pub io: disks::IOHarvest,
pub last_collection_time: Instant,
}
impl Default for Data {
fn default() -> Self {
Data {
cpu: cpu::CPUHarvest::default(),
memory: mem::MemHarvest::default(),
swap: mem::MemHarvest::default(),
temperature_sensors: Vec::default(),
list_of_processes: Vec::default(),
disks: Vec::default(),
io: disks::IOHarvest::default(),
network: network::NetworkHarvest::default(),
last_collection_time: Instant::now(),
}
}
}
impl Data {
pub fn first_run_cleanup(&mut self) {
self.io = disks::IOHarvest::default();
self.temperature_sensors = Vec::new();
self.list_of_processes = Vec::new();
self.disks = Vec::new();
self.network.first_run_cleanup();
self.memory = mem::MemHarvest::default();
self.swap = mem::MemHarvest::default();
self.cpu = cpu::CPUHarvest::default();
}
}
pub struct DataCollector {
pub data: Data,
sys: System,
prev_pid_stats: HashMap<String, (f64, Instant)>,
prev_idle: f64,
prev_non_idle: f64,
mem_total_kb: u64,
temperature_type: temperature::TemperatureType,
use_current_cpu_total: bool,
last_collection_time: Instant,
total_rx: u64,
total_tx: u64,
show_average_cpu: bool,
widgets_to_harvest: UsedWidgets,
}
impl Default for DataCollector {
fn default() -> Self {
DataCollector {
data: Data::default(),
sys: System::new_all(),
prev_pid_stats: HashMap::new(),
prev_idle: 0_f64,
prev_non_idle: 0_f64,
mem_total_kb: 0,
temperature_type: temperature::TemperatureType::Celsius,
use_current_cpu_total: false,
last_collection_time: Instant::now(),
total_rx: 0,
total_tx: 0,
show_average_cpu: false,
widgets_to_harvest: UsedWidgets::default(),
}
}
}
impl DataCollector {
pub fn init(&mut self) {
self.mem_total_kb = self.sys.get_total_memory();
futures::executor::block_on(self.update_data());
std::thread::sleep(std::time::Duration::from_millis(250));
self.data.first_run_cleanup();
}
pub fn set_collected_data(&mut self, used_widgets: UsedWidgets) {
self.widgets_to_harvest = used_widgets;
}
pub fn set_temperature_type(&mut self, temperature_type: temperature::TemperatureType) {
self.temperature_type = temperature_type;
}
pub fn set_use_current_cpu_total(&mut self, use_current_cpu_total: bool) {
self.use_current_cpu_total = use_current_cpu_total;
}
pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) {
self.show_average_cpu = show_average_cpu;
}
pub async fn update_data(&mut self) {
if self.widgets_to_harvest.use_cpu {
self.sys.refresh_cpu();
}
if cfg!(not(target_os = "linux")) {
if self.widgets_to_harvest.use_proc {
self.sys.refresh_processes();
}
if self.widgets_to_harvest.use_temp {
self.sys.refresh_components();
}
}
if cfg!(target_os = "windows") && self.widgets_to_harvest.use_net {
self.sys.refresh_networks();
}
let current_instant = std::time::Instant::now();
// CPU
if self.widgets_to_harvest.use_cpu {
self.data.cpu = cpu::get_cpu_data_list(&self.sys, self.show_average_cpu);
}
if self.widgets_to_harvest.use_proc {
// Processes. This is the longest part of the harvesting process... changing this might be
// good in the future. What was tried already:
// * Splitting the internal part into multiple scoped threads (dropped by ~.01 seconds, but upped usage)
if let Ok(process_list) = processes::get_sorted_processes_list(
&self.sys,
&mut self.prev_idle,
&mut self.prev_non_idle,
&mut self.prev_pid_stats,
self.use_current_cpu_total,
self.mem_total_kb,
current_instant,
) {
self.data.list_of_processes = process_list;
}
}
// ASYNC
let network_data_fut = network::get_network_data(
&self.sys,
self.last_collection_time,
&mut self.total_rx,
&mut self.total_tx,
current_instant,
self.widgets_to_harvest.use_net,
);
let mem_data_fut = mem::get_mem_data_list(self.widgets_to_harvest.use_mem);
let swap_data_fut = mem::get_swap_data_list(self.widgets_to_harvest.use_mem);
let disk_data_fut = disks::get_disk_usage_list(self.widgets_to_harvest.use_disk);
let disk_io_usage_fut = disks::get_io_usage_list(false, self.widgets_to_harvest.use_disk);
let temp_data_fut = temperature::get_temperature_data(
&self.sys,
&self.temperature_type,
self.widgets_to_harvest.use_temp,
);
let (net_data, mem_res, swap_res, disk_res, io_res, temp_res) = join!(
network_data_fut,
mem_data_fut,
swap_data_fut,
disk_data_fut,
disk_io_usage_fut,
temp_data_fut
);
// After async
if let Some(net_data) = net_data {
self.data.network = net_data;
self.total_rx = self.data.network.total_rx;
self.total_tx = self.data.network.total_tx;
}
if let Ok(memory) = mem_res {
if let Some(memory) = memory {
self.data.memory = memory;
}
}
if let Ok(swap) = swap_res {
if let Some(swap) = swap {
self.data.swap = swap;
}
}
if let Ok(disks) = disk_res {
if let Some(disks) = disks {
self.data.disks = disks;
}
}
if let Ok(io) = io_res {
if let Some(io) = io {
self.data.io = io;
}
}
if let Ok(temp) = temp_res {
if let Some(temp) = temp {
self.data.temperature_sensors = temp;
}
}
// Update time
self.data.last_collection_time = current_instant;
self.last_collection_time = current_instant;
}
}
|