all-smi 0.24.2

Command-line utility for monitoring GPU hardware. It provides a real-time view of GPU utilization, memory usage, temperature, power consumption, and other metrics.
Documentation
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use async_trait::async_trait;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
use sysinfo::Disks;
use tokio::sync::{Mutex, RwLock};
use tokio::time::timeout;

/// Type alias for the process cache using std::sync::RwLock for synchronous access
type ProcessCache = std::sync::RwLock<HashMap<u32, ProcessInfo>>;

use crate::app_state::AppState;
#[cfg(target_os = "linux")]
use crate::device::platform_detection::has_tenstorrent;
use crate::device::{
    ChassisInfo, ChassisReader, CpuInfo, CpuReader, GpuInfo, GpuReader, MemoryInfo, MemoryReader,
    MigGpuInfo, ProcessInfo, VgpuHostInfo, create_chassis_reader, get_cpu_readers, get_gpu_readers,
    get_memory_readers, get_nvml_status_message,
    platform_detection::has_nvidia,
    process_list::{merge_gpu_processes, update_process_cache},
};

#[cfg(target_os = "linux")]
use crate::device::get_tenstorrent_status_message;
#[cfg(target_os = "linux")]
use crate::device::get_tpu_status_message;
#[cfg(target_os = "linux")]
use crate::device::platform_detection::has_google_tpu;
use crate::storage::info::StorageInfo;
use crate::utils::{filter_docker_aware_disks, get_hostname, with_global_system};

use super::aggregator::DataAggregator;
use super::strategy::{
    CollectionConfig, CollectionData, CollectionError, CollectionResult, DataCollectionStrategy,
};

/// Maximum number of processes to keep after collection.
/// Processes are sorted by CPU usage (descending) and truncated to this limit
/// to reduce CPU overhead from tracking thousands of processes.
const MAX_DISPLAY_PROCESSES: usize = 500;

/// How often to do a full process refresh to discover new high-CPU processes.
/// Every N cycles, we refresh all processes; otherwise, we only refresh tracked PIDs.
const FULL_REFRESH_INTERVAL: u32 = 5;

/// Inject aggregated GPU power into chassis info when not already set.
fn inject_gpu_power(chassis_info: Vec<ChassisInfo>, gpu_info: &[GpuInfo]) -> Vec<ChassisInfo> {
    chassis_info
        .into_iter()
        .map(|mut ci| {
            if ci.total_power_watts.is_none() {
                let total: f64 = gpu_info.iter().map(|g| g.power_consumption).sum();
                if total > 0.0 {
                    ci.total_power_watts = Some(total);
                }
            }
            ci
        })
        .collect()
}

pub struct LocalCollector {
    gpu_readers: Arc<RwLock<Vec<Box<dyn GpuReader>>>>,
    cpu_readers: Arc<RwLock<Vec<Box<dyn CpuReader>>>>,
    memory_readers: Arc<RwLock<Vec<Box<dyn MemoryReader>>>>,
    chassis_reader: Arc<RwLock<Option<Box<dyn ChassisReader>>>>,
    aggregator: DataAggregator,
    initialized: Arc<Mutex<bool>>,
    /// PIDs of processes from the previous collection cycle (top N by CPU usage).
    /// Used for selective process refresh to reduce CPU overhead.
    tracked_pids: Arc<RwLock<Vec<sysinfo::Pid>>>,
    /// Counter for refresh cycles; every FULL_REFRESH_INTERVAL cycles we do a full refresh.
    refresh_cycle: Arc<AtomicU32>,
    /// Cache of ProcessInfo objects by PID to reduce memory allocation overhead.
    /// On each collection, existing objects are updated in place rather than reallocated.
    /// Uses std::sync::RwLock for synchronous access within with_global_system closure.
    process_cache: Arc<ProcessCache>,
}

impl LocalCollector {
    pub fn new() -> Self {
        Self {
            gpu_readers: Arc::new(RwLock::new(Vec::new())),
            cpu_readers: Arc::new(RwLock::new(Vec::new())),
            memory_readers: Arc::new(RwLock::new(Vec::new())),
            chassis_reader: Arc::new(RwLock::new(None)),
            aggregator: DataAggregator::new(),
            initialized: Arc::new(Mutex::new(false)),
            tracked_pids: Arc::new(RwLock::new(Vec::new())),
            refresh_cycle: Arc::new(AtomicU32::new(0)),
            process_cache: Arc::new(std::sync::RwLock::new(HashMap::with_capacity(
                MAX_DISPLAY_PROCESSES,
            ))),
        }
    }

    async fn initialize_readers(&self, app_state: Arc<Mutex<AppState>>) {
        // Use timeout to prevent deadlock
        let initialized_result = timeout(Duration::from_secs(5), self.initialized.lock()).await;

        let mut initialized = match initialized_result {
            Ok(lock) => lock,
            Err(_) => {
                eprintln!("Warning: Timeout acquiring initialized lock");
                return;
            }
        };

        if *initialized {
            return;
        }

        // Add startup status with timeout
        {
            let state_result = timeout(Duration::from_secs(2), app_state.lock()).await;

            if let Ok(mut state) = state_result {
                state
                    .startup_status_lines
                    .push("✓ Initializing GPU readers...".to_string());
            }
        }

        let gpu_readers = get_gpu_readers();

        // Add startup status
        {
            let mut state = app_state.lock().await;
            state
                .startup_status_lines
                .push("✓ Initializing CPU readers...".to_string());
        }

        let cpu_readers = get_cpu_readers();

        // Add startup status
        {
            let mut state = app_state.lock().await;
            state
                .startup_status_lines
                .push("✓ Initializing memory readers...".to_string());
        }

        let memory_readers = get_memory_readers();

        // Create chassis reader
        let chassis_reader = create_chassis_reader();

        // Store the readers in self using RwLock with timeout
        {
            match timeout(Duration::from_secs(2), self.gpu_readers.write()).await {
                Ok(mut gpu_lock) => {
                    *gpu_lock = gpu_readers;
                }
                _ => {
                    eprintln!("Warning: Timeout acquiring GPU readers lock");
                }
            }
        }
        {
            match timeout(Duration::from_secs(2), self.cpu_readers.write()).await {
                Ok(mut cpu_lock) => {
                    *cpu_lock = cpu_readers;
                }
                _ => {
                    eprintln!("Warning: Timeout acquiring CPU readers lock");
                }
            }
        }
        {
            match timeout(Duration::from_secs(2), self.memory_readers.write()).await {
                Ok(mut mem_lock) => {
                    *mem_lock = memory_readers;
                }
                _ => {
                    eprintln!("Warning: Timeout acquiring memory readers lock");
                }
            }
        }
        {
            match timeout(Duration::from_secs(2), self.chassis_reader.write()).await {
                Ok(mut chassis_lock) => {
                    *chassis_lock = Some(chassis_reader);
                }
                _ => {
                    eprintln!("Warning: Timeout acquiring chassis reader lock");
                }
            }
        }

        *initialized = true;
    }

    async fn collect_parallel_first_iteration(
        &self,
        app_state: Arc<Mutex<AppState>>,
    ) -> CollectionData {
        use tokio::sync::mpsc;
        use tokio::task;

        // Add initial startup status
        {
            let mut state = app_state.lock().await;
            state
                .startup_status_lines
                .push("â—‹ Collecting GPU information...".to_string());
            state
                .startup_status_lines
                .push("â—‹ Collecting CPU information...".to_string());
            state
                .startup_status_lines
                .push("â—‹ Collecting memory information...".to_string());
            state
                .startup_status_lines
                .push("â—‹ Collecting process information...".to_string());
            state
                .startup_status_lines
                .push("â—‹ Collecting storage information...".to_string());
        }

        // Create channel for status updates
        let (status_tx, mut status_rx) = mpsc::channel(10);
        let app_state_clone = Arc::clone(&app_state);

        // Spawn task to handle status updates
        let status_handler = task::spawn(async move {
            while let Some((index, message)) = status_rx.recv().await {
                let mut state = app_state_clone.lock().await;
                if index < state.startup_status_lines.len() {
                    state.startup_status_lines[3 + index] = message;
                }
            }
        });

        // Run all collections in parallel with status updates
        // Use Arc references instead of cloning the entire Arc<RwLock<_>>
        let gpu_readers_1 = Arc::clone(&self.gpu_readers);
        let gpu_readers_2 = Arc::clone(&self.gpu_readers);
        let gpu_readers_vgpu = Arc::clone(&self.gpu_readers);
        let gpu_readers_mig = Arc::clone(&self.gpu_readers);
        let cpu_readers = Arc::clone(&self.cpu_readers);
        let memory_readers = Arc::clone(&self.memory_readers);
        let chassis_reader = Arc::clone(&self.chassis_reader);
        let process_cache = Arc::clone(&self.process_cache);

        let (
            all_gpu_info,
            all_cpu_info,
            all_memory_info,
            gpu_processes_result,
            all_processes,
            all_storage_info,
            all_chassis_info,
            all_vgpu_info,
            all_mig_info,
        ) = {
            let status_tx_gpu = status_tx.clone();
            let status_tx_cpu = status_tx.clone();
            let status_tx_mem = status_tx.clone();
            let status_tx_proc = status_tx.clone();
            let status_tx_storage = status_tx.clone();

            tokio::join!(
                // GPU info collection
                async move {
                    let readers = gpu_readers_1.read().await;
                    let info: Vec<GpuInfo> = readers
                        .iter()
                        .flat_map(|reader| reader.get_gpu_info())
                        .collect();
                    let _ = status_tx_gpu
                        .send((0, "✓ GPU information collected".to_string()))
                        .await;
                    info
                },
                // CPU info collection
                async move {
                    let readers = cpu_readers.read().await;
                    let info: Vec<CpuInfo> = readers
                        .iter()
                        .flat_map(|reader| reader.get_cpu_info())
                        .collect();
                    let _ = status_tx_cpu
                        .send((1, "✓ CPU information collected".to_string()))
                        .await;
                    info
                },
                // Memory info collection
                async move {
                    let readers = memory_readers.read().await;
                    let info: Vec<MemoryInfo> = readers
                        .iter()
                        .flat_map(|reader| reader.get_memory_info())
                        .collect();
                    let _ = status_tx_mem
                        .send((2, "✓ Memory information collected".to_string()))
                        .await;
                    info
                },
                // GPU process collection (lightweight - raw GPU processes only)
                async move {
                    let readers = gpu_readers_2.read().await;
                    let mut all_gpu_procs = Vec::new();
                    let mut all_gpu_pids = HashSet::new();
                    for reader in readers.iter() {
                        let (procs, pids) = reader.get_gpu_processes();
                        all_gpu_procs.extend(procs);
                        all_gpu_pids.extend(pids);
                    }
                    (all_gpu_procs, all_gpu_pids)
                },
                // Full process collection - use spawn_blocking to avoid blocking tokio runtime
                async move {
                    let all_processes = tokio::task::spawn_blocking(move || {
                        with_global_system(|system| {
                            use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, UpdateKind};
                            // OPTIMIZATION: Only refresh fields we actually need
                            // - CPU usage for cpu_percent
                            // - Memory for memory_percent/memory_rss/memory_vms
                            // - User only if not already set (avoid repeated lookups)
                            // This is much cheaper than everything() which includes disk I/O, etc.
                            let refresh_kind = ProcessRefreshKind::nothing()
                                .with_cpu()
                                .with_memory()
                                .with_user(UpdateKind::OnlyIfNotSet);
                            system.refresh_processes_specifics(
                                ProcessesToUpdate::All,
                                true,
                                refresh_kind,
                            );
                            system.refresh_memory();

                            // OPTIMIZATION: Initialize process cache on first iteration
                            // This populates the cache with all current processes
                            let gpu_pids: HashSet<u32> = HashSet::new();
                            let mut cache = process_cache.write().unwrap();
                            update_process_cache(system, &gpu_pids, &mut cache)
                        })
                    })
                    .await
                    .unwrap_or_default();
                    let _ = status_tx_proc
                        .send((3, "✓ Process information collected".to_string()))
                        .await;
                    all_processes
                },
                // Storage collection
                async move {
                    let storage_info = Self::collect_storage_info();
                    let _ = status_tx_storage
                        .send((4, "✓ Storage information collected".to_string()))
                        .await;
                    storage_info
                },
                // Chassis info collection
                async move {
                    let reader = chassis_reader.read().await;
                    let info: Vec<ChassisInfo> = reader
                        .as_ref()
                        .and_then(|r| r.get_chassis_info())
                        .into_iter()
                        .collect();
                    info
                },
                // vGPU info collection (only NVIDIA readers produce data; others
                // return an empty vector via the default trait implementation).
                async move {
                    let readers = gpu_readers_vgpu.read().await;
                    let info: Vec<VgpuHostInfo> = readers
                        .iter()
                        .flat_map(|reader| reader.get_vgpu_info())
                        .collect();
                    info
                },
                // MIG info collection (only NVIDIA readers with MIG enabled
                // GPUs produce data; everyone else returns an empty vector
                // via the default trait implementation).
                async move {
                    let readers = gpu_readers_mig.read().await;
                    let info: Vec<MigGpuInfo> = readers
                        .iter()
                        .flat_map(|reader| reader.get_mig_info())
                        .collect();
                    info
                }
            )
        };

        // Close the channel and wait for status handler to finish
        drop(status_tx);
        let _ = status_handler.await;

        // Merge raw GPU processes into main process list
        let (gpu_processes, _gpu_pids) = gpu_processes_result;
        let mut all_processes_merged = merge_gpu_processes(all_processes, gpu_processes);

        // Sort by CPU usage descending and limit to top MAX_DISPLAY_PROCESSES
        all_processes_merged.sort_by(|a, b| {
            b.cpu_percent
                .partial_cmp(&a.cpu_percent)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        if all_processes_merged.len() > MAX_DISPLAY_PROCESSES {
            all_processes_merged.truncate(MAX_DISPLAY_PROCESSES);
        }

        // Initialize tracked PIDs for selective refresh in subsequent cycles
        let new_tracked_pids: Vec<sysinfo::Pid> = all_processes_merged
            .iter()
            .map(|p| sysinfo::Pid::from_u32(p.pid))
            .collect();
        *self.tracked_pids.write().await = new_tracked_pids;

        // Reset refresh cycle counter (first iteration counts as cycle 0)
        self.refresh_cycle.store(1, Ordering::Relaxed);

        // Inject aggregated GPU power into chassis info
        let all_chassis_info = inject_gpu_power(all_chassis_info, &all_gpu_info);

        CollectionData {
            gpu_info: all_gpu_info,
            cpu_info: all_cpu_info,
            memory_info: all_memory_info,
            process_info: all_processes_merged,
            storage_info: all_storage_info,
            chassis_info: all_chassis_info,
            vgpu_info: all_vgpu_info,
            mig_info: all_mig_info,
            connection_statuses: Vec::new(),
            // Local mode has no cluster-wide Users tab (issue #189).
            remote_process_info: Vec::new(),
        }
    }

    async fn collect_sequential(&self) -> CollectionData {
        let gpu_readers = self.gpu_readers.read().await;
        let all_gpu_info: Vec<GpuInfo> = gpu_readers
            .iter()
            .flat_map(|reader| reader.get_gpu_info())
            .collect();

        let cpu_readers = self.cpu_readers.read().await;
        let all_cpu_info: Vec<CpuInfo> = cpu_readers
            .iter()
            .flat_map(|reader| reader.get_cpu_info())
            .collect();

        let memory_readers = self.memory_readers.read().await;
        let all_memory_info: Vec<MemoryInfo> = memory_readers
            .iter()
            .flat_map(|reader| reader.get_memory_info())
            .collect();

        let mut gpu_processes = Vec::new();
        let mut gpu_pids = HashSet::new();
        for reader in gpu_readers.iter() {
            let (procs, pids) = reader.get_gpu_processes();
            gpu_processes.extend(procs);
            gpu_pids.extend(pids);
        }

        // vGPU info collection — performed on the same readers set so it
        // shares NVML handle caching with the main GPU collection.
        let all_vgpu_info: Vec<VgpuHostInfo> = gpu_readers
            .iter()
            .flat_map(|reader| reader.get_vgpu_info())
            .collect();

        // MIG info collection — same locality benefits as vGPU above.
        let all_mig_info: Vec<MigGpuInfo> = gpu_readers
            .iter()
            .flat_map(|reader| reader.get_mig_info())
            .collect();

        // Determine if we should do a full refresh or selective refresh
        let cycle = self.refresh_cycle.fetch_add(1, Ordering::Relaxed);
        let do_full_refresh = cycle.is_multiple_of(FULL_REFRESH_INTERVAL);

        // Read tracked PIDs for selective refresh (outside the closure)
        let tracked_pids_for_refresh: Vec<sysinfo::Pid> = if do_full_refresh {
            Vec::new() // Not needed for full refresh
        } else {
            self.tracked_pids.read().await.clone()
        };
        let process_cache = Arc::clone(&self.process_cache);
        let all_processes = with_global_system(|system| {
            use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, UpdateKind};
            // OPTIMIZATION: Only refresh fields we actually need
            // - CPU usage for cpu_percent
            // - Memory for memory_percent/memory_rss/memory_vms
            // - User only if not already set (avoid repeated lookups)
            let refresh_kind = ProcessRefreshKind::nothing()
                .with_cpu()
                .with_memory()
                .with_user(UpdateKind::OnlyIfNotSet);

            // OPTIMIZATION: Selective process refresh
            // Full refresh every N cycles to discover new high-CPU processes;
            // otherwise only refresh tracked PIDs to significantly reduce CPU usage.
            if do_full_refresh || tracked_pids_for_refresh.is_empty() {
                system.refresh_processes_specifics(ProcessesToUpdate::All, true, refresh_kind);
            } else {
                system.refresh_processes_specifics(
                    ProcessesToUpdate::Some(&tracked_pids_for_refresh),
                    true,
                    refresh_kind,
                );
            }
            system.refresh_memory();

            // OPTIMIZATION: Use process cache to reduce memory allocation overhead
            // Instead of creating new ProcessInfo objects every cycle, we update
            // existing cached objects and only allocate for new processes.
            let mut cache = process_cache.write().unwrap();
            update_process_cache(system, &gpu_pids, &mut cache)
        });
        let mut all_processes = merge_gpu_processes(all_processes, gpu_processes);

        // Sort by CPU usage descending and limit to top MAX_DISPLAY_PROCESSES
        all_processes.sort_by(|a, b| {
            b.cpu_percent
                .partial_cmp(&a.cpu_percent)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        if all_processes.len() > MAX_DISPLAY_PROCESSES {
            all_processes.truncate(MAX_DISPLAY_PROCESSES);
        }

        // Update tracked PIDs for next cycle (after truncation to top N)
        let new_tracked_pids: Vec<sysinfo::Pid> = all_processes
            .iter()
            .map(|p| sysinfo::Pid::from_u32(p.pid))
            .collect();
        *self.tracked_pids.write().await = new_tracked_pids;

        let all_storage_info = Self::collect_storage_info();

        // Collect chassis info
        let chassis_reader = self.chassis_reader.read().await;
        let all_chassis_info: Vec<ChassisInfo> = chassis_reader
            .as_ref()
            .and_then(|r| r.get_chassis_info())
            .into_iter()
            .collect();

        // Inject aggregated GPU power into chassis info
        let all_chassis_info = inject_gpu_power(all_chassis_info, &all_gpu_info);

        CollectionData {
            gpu_info: all_gpu_info,
            cpu_info: all_cpu_info,
            memory_info: all_memory_info,
            process_info: all_processes,
            storage_info: all_storage_info,
            chassis_info: all_chassis_info,
            vgpu_info: all_vgpu_info,
            mig_info: all_mig_info,
            connection_statuses: Vec::new(),
            // Local mode has no cluster-wide Users tab (issue #189).
            remote_process_info: Vec::new(),
        }
    }

    fn collect_storage_info() -> Vec<StorageInfo> {
        let mut all_storage_info = Vec::new();
        let disks = Disks::new_with_refreshed_list();
        let hostname = get_hostname();

        let mut filtered_disks = filter_docker_aware_disks(&disks);
        filtered_disks.sort_by(|a, b| {
            a.mount_point()
                .to_string_lossy()
                .cmp(&b.mount_point().to_string_lossy())
        });

        for (index, disk) in filtered_disks.iter().enumerate() {
            let mount_point_str = disk.mount_point().to_string_lossy();
            all_storage_info.push(StorageInfo {
                mount_point: mount_point_str.to_string(),
                total_bytes: disk.total_space(),
                available_bytes: disk.available_space(),
                host_id: hostname.clone(),
                hostname: hostname.clone(),
                index: index as u32,
            });
        }

        all_storage_info
    }

    fn update_notifications(state: &mut AppState) {
        // Update notifications (remove expired ones)
        state.notifications.update();

        // Only check NVML status if we're trying to monitor NVIDIA devices
        if has_nvidia()
            && let Some(nvml_message) = get_nvml_status_message()
            && !state.nvml_notification_shown
        {
            if let Err(e) = state.notifications.warning(nvml_message) {
                eprintln!("Failed to show NVML notification: {e}");
            }
            state.nvml_notification_shown = true;
        }

        // Only check Tenstorrent status if we're trying to monitor Tenstorrent devices
        #[cfg(target_os = "linux")]
        if has_tenstorrent()
            && let Some(tt_message) = get_tenstorrent_status_message()
            && !state.tenstorrent_notification_shown
        {
            if let Err(e) = state.notifications.warning(tt_message) {
                eprintln!("Failed to show Tenstorrent notification: {e}");
            }
            state.tenstorrent_notification_shown = true;
        }

        // Google TPU status (Initializing / Failed)
        #[cfg(target_os = "linux")]
        if has_google_tpu()
            && let Some(msg) = get_tpu_status_message()
        {
            // If initializing, allow repeated updates (it will be "Initializing...")
            // If failed, show error once.
            if msg.contains("Initializing") {
                let _ = state.notifications.status(msg);
            } else if (msg.contains("failed") || msg.contains("error"))
                && !state.tpu_notification_shown
            {
                let _ = state.notifications.error(msg);
                state.tpu_notification_shown = true;
            }
        }
    }

    fn update_tabs(state: &mut AppState) {
        let mut host_ids: Vec<String> = state
            .gpu_info
            .iter()
            .map(|info| info.host_id.clone())
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();

        // If no GPU info available, use the local hostname
        if host_ids.is_empty() {
            host_ids.push(get_hostname());
        }

        host_ids.sort();

        // Always create "All" tab for consistent UI behavior
        let mut tabs = vec!["All".to_string()];
        tabs.extend(host_ids);

        state.tabs = tabs;
    }
}

#[async_trait]
impl DataCollectionStrategy for LocalCollector {
    async fn collect(&self, config: &CollectionConfig) -> CollectionResult {
        if config.first_iteration {
            // For first iteration, we need app_state for status updates
            // This is a limitation that needs to be addressed in the refactor
            // For now, return an error indicating initialization is needed
            return Err(CollectionError::Other(
                "First iteration requires app_state initialization".to_string(),
            ));
        }

        Ok(self.collect_sequential().await)
    }

    async fn update_state(
        &self,
        app_state: Arc<Mutex<AppState>>,
        data: CollectionData,
        _config: &CollectionConfig,
    ) {
        // Check if we need to initialize readers
        if !*self.initialized.lock().await {
            self.initialize_readers(app_state.clone()).await;
        }

        let mut state = app_state.lock().await;

        // Update GPU info with UUID matching
        if state.gpu_info.is_empty() {
            state.gpu_info = data.gpu_info;
        } else {
            for new_info in data.gpu_info {
                if let Some(old_info) = state
                    .gpu_info
                    .iter_mut()
                    .find(|info| info.uuid == new_info.uuid)
                {
                    *old_info = new_info;
                }
            }
        }

        state.cpu_info = data.cpu_info;
        state.memory_info = data.memory_info;

        // Sort processes based on current criteria
        let mut sorted_processes = data.process_info;
        sorted_processes.sort_by(|a, b| {
            state
                .sort_criteria
                .sort_processes(a, b, state.sort_direction)
        });
        state.process_info = sorted_processes;

        state.storage_info = data.storage_info;
        state.chassis_info = data.chassis_info;
        state.vgpu_info = data.vgpu_info;
        state.mig_info = data.mig_info;

        // Mark data as changed to trigger UI update AND invalidate
        // collector-keyed caches (e.g. Users-tab aggregation).
        state.mark_collector_data_changed();

        // Update notifications
        Self::update_notifications(&mut state);

        // Update utilization history
        self.aggregator.update_utilization_history(&mut state);

        // Feed power samples into the energy integrator (issue #191).
        // Must run AFTER the new GPU / CPU / chassis info has been
        // written to state.
        self.aggregator.update_energy_counters(&mut state);

        // Update tabs
        Self::update_tabs(&mut state);

        // Always clear loading state in local mode after first iteration
        state.loading = false;
    }

    fn strategy_type(&self) -> &str {
        "local"
    }

    async fn is_ready(&self) -> bool {
        *self.initialized.lock().await
    }
}

impl LocalCollector {
    pub async fn collect_with_app_state(
        &self,
        app_state: Arc<Mutex<AppState>>,
        config: &CollectionConfig,
    ) -> CollectionResult {
        if !*self.initialized.lock().await {
            self.initialize_readers(app_state.clone()).await;
        }

        if config.first_iteration {
            Ok(self.collect_parallel_first_iteration(app_state).await)
        } else {
            Ok(self.collect_sequential().await)
        }
    }
}