| 1 | //! Time series data. |
| 2 | |
| 3 | use std::{ |
| 4 | cmp::Ordering, |
| 5 | time::{Duration, Instant}, |
| 6 | vec::Vec, |
| 7 | }; |
| 8 | |
| 9 | use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; |
| 10 | use timeless::data::ChunkedData; |
| 11 | |
| 12 | use crate::{ |
| 13 | app::{AppConfigFields, DataFilters, filter::Filter, layout_manager::UsedWidgets}, |
| 14 | collection::Data, |
| 15 | widgets::DiskWidgetData, |
| 16 | }; |
| 17 | |
| 18 | /// Values corresponding to a time slice. |
| 19 | pub type Values = ChunkedData<f64>; |
| 20 | |
| 21 | /// Represents time series data in a chunked, deduped manner. |
| 22 | /// |
| 23 | /// Properties: |
| 24 | /// - Time in this manner is represented in a reverse-offset fashion from the |
| 25 | /// current time. |
| 26 | /// - All data is stored in SoA fashion. |
| 27 | /// - Values are stored in a chunked format, which facilitates gaps in data |
| 28 | /// collection if needed. |
| 29 | /// - Additional metadata is stored to make data pruning over time easy. |
| 30 | #[derive(Clone, Debug, Default)] |
| 31 | pub struct TimeSeriesData { |
| 32 | /// Time values. |
| 33 | /// |
| 34 | /// TODO: (points_rework_v1) Either store millisecond-level only or offsets |
| 35 | /// only. |
| 36 | pub time: Vec<Instant>, |
| 37 | |
| 38 | /// Network RX data. |
| 39 | pub rx: Values, |
| 40 | |
| 41 | /// Network TX data. |
| 42 | pub tx: Values, |
| 43 | |
| 44 | /// CPU data. |
| 45 | pub cpu: Vec<Values>, |
| 46 | |
| 47 | /// RAM memory data. |
| 48 | pub ram: Values, |
| 49 | |
| 50 | /// Swap data. |
| 51 | pub swap: Values, |
| 52 | |
| 53 | #[cfg(not(target_os = "windows"))] |
| 54 | /// Cache data. |
| 55 | pub cache_mem: Values, |
| 56 | |
| 57 | #[cfg(feature = "zfs")] |
| 58 | /// Arc data. |
| 59 | pub arc_mem: Values, |
| 60 | |
| 61 | #[cfg(feature = "gpu")] |
| 62 | /// GPU memory data. |
| 63 | pub gpu_mem: HashMap<String, Values>, |
| 64 | |
| 65 | /// Temperature data. |
| 66 | /// |
| 67 | /// TODO: Maybe make this use TypedTemperature? |
| 68 | pub temperature: HashMap<String, ChunkedData<f32>>, |
| 69 | |
| 70 | /// Disk I/O read rate data, keyed by device name. |
| 71 | pub disk_io_read: HashMap<String, ChunkedData<f64>>, |
| 72 | |
| 73 | /// Disk I/O write rate data, keyed by device name. |
| 74 | pub disk_io_write: HashMap<String, ChunkedData<f64>>, |
| 75 | } |
| 76 | |
| 77 | impl TimeSeriesData { |
| 78 | /// Add a new data point. |
| 79 | pub fn add( |
| 80 | &mut self, data: &Data, used_widgets: &UsedWidgets, settings: &AppConfigFields, |
| 81 | filters: &DataFilters, |
| 82 | ) { |
| 83 | self.time.push(data.collection_time); |
| 84 | |
| 85 | if let Some(network) = &data.network { |
| 86 | self.rx.push(network.rx as f64); |
| 87 | self.tx.push(network.tx as f64); |
| 88 | } else { |
| 89 | self.rx.insert_break(); |
| 90 | self.tx.insert_break(); |
| 91 | } |
| 92 | |
| 93 | if let Some(cpu) = &data.cpu { |
| 94 | match self.cpu.len().cmp(&cpu.len()) { |
| 95 | Ordering::Less => { |
| 96 | let diff = cpu.len() - self.cpu.len(); |
| 97 | self.cpu.reserve_exact(diff); |
| 98 | |
| 99 | for _ in 0..diff { |
| 100 | self.cpu.push(Default::default()); |
| 101 | } |
| 102 | } |
| 103 | Ordering::Greater => { |
| 104 | let diff = self.cpu.len() - cpu.len(); |
| 105 | let offset = self.cpu.len() - diff; |
| 106 | |
| 107 | for curr in &mut self.cpu[offset..] { |
| 108 | curr.insert_break(); |
| 109 | } |
| 110 | } |
| 111 | Ordering::Equal => {} |
| 112 | } |
| 113 | |
| 114 | for (curr, new_data) in self.cpu.iter_mut().zip(cpu.iter()) { |
| 115 | curr.push(new_data.usage.into()); |
| 116 | } |
| 117 | } else { |
| 118 | for c in &mut self.cpu { |
| 119 | c.insert_break(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if let Some(memory) = &data.memory { |
| 124 | self.ram.push(memory.percentage()); |
| 125 | } else { |
| 126 | self.ram.insert_break(); |
| 127 | } |
| 128 | |
| 129 | if let Some(swap) = &data.swap { |
| 130 | self.swap.push(swap.percentage()); |
| 131 | } else { |
| 132 | self.swap.insert_break(); |
| 133 | } |
| 134 | |
| 135 | #[cfg(not(target_os = "windows"))] |
| 136 | { |
| 137 | if let Some(cache) = &data.cache { |
| 138 | self.cache_mem.push(cache.percentage()); |
| 139 | } else { |
| 140 | self.cache_mem.insert_break(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #[cfg(feature = "zfs")] |
| 145 | { |
| 146 | if let Some(arc) = &data.arc { |
| 147 | self.arc_mem.push(arc.percentage()); |
| 148 | } else { |
| 149 | self.arc_mem.insert_break(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #[cfg(feature = "gpu")] |
| 154 | { |
| 155 | if let Some(gpu) = &data.gpu { |
| 156 | let mut not_visited = self |
| 157 | .gpu_mem |
| 158 | .keys() |
| 159 | .map(String::to_owned) |
| 160 | .collect::<HashSet<_>>(); |
| 161 | |
| 162 | for (name, new_data) in gpu { |
| 163 | not_visited.remove(name); |
| 164 | |
| 165 | if !self.gpu_mem.contains_key(name) { |
| 166 | self.gpu_mem |
| 167 | .insert(name.to_string(), ChunkedData::default()); |
| 168 | } |
| 169 | |
| 170 | let curr = self |
| 171 | .gpu_mem |
| 172 | .get_mut(name) |
| 173 | .expect("entry must exist as it was created above"); |
| 174 | curr.push(new_data.percentage()); |
| 175 | } |
| 176 | |
| 177 | for nv in not_visited { |
| 178 | if let Some(entry) = self.gpu_mem.get_mut(&nv) { |
| 179 | entry.insert_break(); |
| 180 | } |
| 181 | } |
| 182 | } else { |
| 183 | for g in self.gpu_mem.values_mut() { |
| 184 | g.insert_break(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if used_widgets.use_temp_graph { |
| 190 | if let Some(temperature_sensors) = &data.temperature_sensors { |
| 191 | let mut not_visited = self |
| 192 | .temperature |
| 193 | .keys() |
| 194 | .map(String::to_owned) |
| 195 | .collect::<HashSet<_>>(); |
| 196 | |
| 197 | for sensor_data in temperature_sensors { |
| 198 | if !Filter::optional_should_keep(&filters.temp_graph_filter, &sensor_data.name) |
| 199 | { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if let Some(temperature) = sensor_data.temperature { |
| 204 | not_visited.remove(&sensor_data.name); |
| 205 | |
| 206 | if !self.temperature.contains_key(&sensor_data.name) { |
| 207 | self.temperature |
| 208 | .insert(sensor_data.name.clone(), ChunkedData::default()); |
| 209 | } |
| 210 | |
| 211 | let curr = self |
| 212 | .temperature |
| 213 | .get_mut(&sensor_data.name) |
| 214 | .expect("entry must exist as it was created above"); |
| 215 | |
| 216 | let converted_temperature = settings |
| 217 | .temperature_type |
| 218 | .convert_temp_unit_float(temperature); |
| 219 | curr.push(converted_temperature); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | for nv in not_visited { |
| 224 | if let Some(entry) = self.temperature.get_mut(&nv) { |
| 225 | entry.insert_break(); |
| 226 | } |
| 227 | } |
| 228 | } else { |
| 229 | for g in self.temperature.values_mut() { |
| 230 | g.insert_break(); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// Update disk I/O time series using already-calculated rates from |
| 237 | /// [`DiskWidgetData`]. Devices not present in `disks` get a break inserted |
| 238 | /// so the legend keeps them around for the window's remaining lifetime. |
| 239 | pub fn update_disk_io( |
| 240 | &mut self, disks: &[DiskWidgetData], filter: &Option<Filter>, show_unmounted: bool, |
| 241 | ) { |
| 242 | #[cfg(not(target_os = "linux"))] |
| 243 | let _ = show_unmounted; |
| 244 | |
| 245 | let mut not_visited: HashSet<String> = self.disk_io_read.keys().cloned().collect(); |
| 246 | |
| 247 | for disk in disks { |
| 248 | #[cfg(target_os = "linux")] |
| 249 | if !show_unmounted && disk.mount_point.is_empty() { |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if !Filter::optional_should_keep(filter, &disk.name) { |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | not_visited.remove(&disk.name); |
| 258 | |
| 259 | let read_entry = self.disk_io_read.entry(disk.name.clone()).or_default(); |
| 260 | match disk.io_read_rate_bytes { |
| 261 | Some(v) => read_entry.push(v as f64), |
| 262 | None => read_entry.insert_break(), |
| 263 | } |
| 264 | |
| 265 | let write_entry = self.disk_io_write.entry(disk.name.clone()).or_default(); |
| 266 | match disk.io_write_rate_bytes { |
| 267 | Some(v) => write_entry.push(v as f64), |
| 268 | None => write_entry.insert_break(), |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | for name in not_visited { |
| 273 | if let Some(entry) = self.disk_io_read.get_mut(&name) { |
| 274 | entry.insert_break(); |
| 275 | } |
| 276 | if let Some(entry) = self.disk_io_write.get_mut(&name) { |
| 277 | entry.insert_break(); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /// Prune any data older than the given duration. |
| 283 | pub fn prune(&mut self, max_age: Duration) { |
| 284 | if self.time.is_empty() { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | let now = Instant::now(); |
| 289 | let end = { |
| 290 | let partition_point = self |
| 291 | .time |
| 292 | .partition_point(|then| now.duration_since(*then) > max_age); |
| 293 | |
| 294 | // Partition point returns the first index that does not match the predicate, so |
| 295 | // minus one. |
| 296 | if partition_point > 0 { |
| 297 | partition_point - 1 |
| 298 | } else { |
| 299 | // If the partition point was 0, then it means all values are too new to be |
| 300 | // pruned. |
| 301 | // crate::info!("Skipping prune."); |
| 302 | return; |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | // crate::info!("Pruning up to index {end}."); |
| 307 | |
| 308 | // Note that end here is _inclusive_. |
| 309 | self.time.drain(0..=end); |
| 310 | self.time.shrink_to_fit(); |
| 311 | |
| 312 | let _ = self.rx.prune_and_shrink_to_fit(end); |
| 313 | let _ = self.tx.prune_and_shrink_to_fit(end); |
| 314 | |
| 315 | for cpu in &mut self.cpu { |
| 316 | let _ = cpu.prune_and_shrink_to_fit(end); |
| 317 | } |
| 318 | |
| 319 | let _ = self.ram.prune_and_shrink_to_fit(end); |
| 320 | let _ = self.swap.prune_and_shrink_to_fit(end); |
| 321 | |
| 322 | #[cfg(not(target_os = "windows"))] |
| 323 | let _ = self.cache_mem.prune_and_shrink_to_fit(end); |
| 324 | |
| 325 | #[cfg(feature = "zfs")] |
| 326 | let _ = self.arc_mem.prune_and_shrink_to_fit(end); |
| 327 | |
| 328 | #[cfg(feature = "gpu")] |
| 329 | { |
| 330 | self.gpu_mem.retain(|_, gpu| { |
| 331 | let _ = gpu.prune(end); |
| 332 | |
| 333 | // Remove the entry if it is empty. We can always add it again later. |
| 334 | if gpu.no_elements() { |
| 335 | false |
| 336 | } else { |
| 337 | gpu.shrink_to_fit(); |
| 338 | true |
| 339 | } |
| 340 | }); |
| 341 | } |
| 342 | |
| 343 | self.temperature.retain(|_, data| { |
| 344 | let _ = data.prune(end); |
| 345 | |
| 346 | // Remove the entry if it is empty. We can always add it again later. |
| 347 | if data.no_elements() { |
| 348 | false |
| 349 | } else { |
| 350 | data.shrink_to_fit(); |
| 351 | true |
| 352 | } |
| 353 | }); |
| 354 | |
| 355 | self.disk_io_read.retain(|_, data| { |
| 356 | let _ = data.prune(end); |
| 357 | |
| 358 | if data.no_elements() { |
| 359 | false |
| 360 | } else { |
| 361 | data.shrink_to_fit(); |
| 362 | true |
| 363 | } |
| 364 | }); |
| 365 | |
| 366 | self.disk_io_write.retain(|_, data| { |
| 367 | let _ = data.prune(end); |
| 368 | |
| 369 | if data.no_elements() { |
| 370 | false |
| 371 | } else { |
| 372 | data.shrink_to_fit(); |
| 373 | true |
| 374 | } |
| 375 | }); |
| 376 | } |
| 377 | } |