Add CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices#567
Merged
Conversation
…io-app#566) - Add trackio/cpu.py with CpuMonitor, collect_cpu_metrics(), cpu_available(), log_cpu() - Collect: CPU utilization (global + per-core), frequency, core count, RAM, swap, disk I/O rates (MB/s + IOPS), network I/O rates (MB/s), temperatures, battery - Auto-start CpuMonitor when psutil is available (skipped on Apple Silicon where AppleGpuMonitor already covers CPU/RAM to avoid duplicates) - Expose trackio.log_cpu() and auto_log_cpu / cpu_log_interval params in trackio.init() - Add psutil to base dependencies - Add [cpu] optional extra for explicit psutil install - Update SystemMetrics.svelte empty-state to mention cpu monitoring - Add 17 unit tests in tests/unit/test_cpu.py - Add examples/test_cpu_metrics.py Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Author
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class CPU/RAM (and related disk/network/sensors) system metrics monitoring via psutil, wiring it into trackio.init()/Run lifecycle and exposing a public trackio.log_cpu() API, plus frontend guidance and unit tests.
Changes:
- Introduces
trackio/cpu.pywithcollect_cpu_metrics(),CpuMonitor, andlog_cpu(). - Extends
Runandtrackio.init()to supportauto_log_cpu+cpu_log_interval, starting/stopping the monitor alongside GPU monitoring. - Updates packaging, frontend empty-state instructions, example script, and adds unit tests for CPU metrics.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
uv.lock |
Updates the lockfile to include the new dependency set (incl. psutil). |
trackio/run.py |
Starts/stops a new CpuMonitor when auto_log_cpu is enabled. |
trackio/frontend/src/pages/SystemMetrics.svelte |
Updates system-metrics empty-state instructions to mention CPU logging. |
trackio/cpu.py |
New CPU/RAM/disk/network/sensor metrics collection + background monitor + log_cpu(). |
trackio/__init__.py |
Exposes trackio.log_cpu() and adds auto_log_cpu/cpu_log_interval to init(). |
tests/unit/test_cpu.py |
Adds unit tests for CPU metric collection and monitor behavior. |
pyproject.toml |
Adds psutil dependency and introduces a cpu extra. |
examples/test_cpu_metrics.py |
Adds a runnable example demonstrating CPU auto-logging. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+14
to
+28
| def _ensure_psutil(): | ||
| global PSUTIL_AVAILABLE, psutil | ||
| if PSUTIL_AVAILABLE: | ||
| return psutil | ||
| try: | ||
| import psutil as _psutil | ||
|
|
||
| psutil = _psutil | ||
| PSUTIL_AVAILABLE = True | ||
| return psutil | ||
| except ImportError: | ||
| raise ImportError( | ||
| "psutil is required for CPU and RAM monitoring. " | ||
| "Install it with: pip install psutil" | ||
| ) |
Comment on lines
+73
to
+84
| try: | ||
| cpu_percent = psutil.cpu_percent(interval=0.1, percpu=False) | ||
| metrics["cpu/utilization"] = cpu_percent | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| per_core = psutil.cpu_percent(interval=None, percpu=True) | ||
| for i, pct in enumerate(per_core): | ||
| metrics[f"cpu/{i}/utilization"] = pct | ||
| except Exception: | ||
| pass |
Comment on lines
+233
to
+235
| while not self._stop_flag.is_set(): | ||
| self._stop_flag.wait(timeout=self._interval) | ||
| try: |
Comment on lines
+55
to
+61
| prev_disk_counters: Previous disk I/O counters for computing read/write rates. | ||
| If None, only cumulative values are reported. | ||
| prev_net_counters: Previous network I/O counters for computing send/recv rates. | ||
| If None, only cumulative values are reported. | ||
| elapsed: Seconds since prev_disk_counters and prev_net_counters were captured. | ||
| Required when prev_disk_counters or prev_net_counters are provided. | ||
|
|
Comment on lines
513
to
516
| <p><strong>Setup:</strong></p> | ||
| <ul> | ||
| <li><strong>System metrics:</strong> <code>pip install trackio[cpu]</code> (requires <code>psutil</code>)</li> | ||
| <li><strong>NVIDIA GPU:</strong> <code>pip install trackio[gpu]</code> (requires <code>nvidia-ml-py</code>)</li> |
Comment on lines
+56
to
+58
| cpu = [ | ||
| "psutil>=5.9.0", | ||
| ] |
- Use _psutil_lock in _ensure_psutil() to prevent race conditions on concurrent init - Single psutil.cpu_percent(percpu=True) call for consistent global/per-core values - Check stop flag after wait() in _monitor_loop() for deterministic shutdown - Fix elapsed docstring to accurately describe optional/fallback behavior - Update SystemMetrics empty-state: psutil is a core dep, no extra install needed - Remove redundant [cpu] extra from pyproject.toml (psutil already in core dependencies)
Contributor
🪼 branch checks and previews
|
Contributor
🦄 change detectedThis Pull Request includes changes to the following packages.
|
🪼 branch checks and previews
Install Trackio from this PR (includes built frontend) pip install "https://huggingface.co/buckets/trackio/trackio-wheels/resolve/ef0aade17930096e1cbfb0420378feb2df50b7db/trackio-0.26.0-py3-none-any.whl" |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Comment on lines
+146
to
+147
| metrics["disk/read_bytes"] = disk.read_bytes / (1024**3) | ||
| metrics["disk/write_bytes"] = disk.write_bytes / (1024**3) |
Comment on lines
+166
to
+167
| metrics["network/sent_bytes"] = net.bytes_sent / (1024**3) | ||
| metrics["network/recv_bytes"] = net.bytes_recv / (1024**3) |
Comment on lines
113
to
116
| from trackio.cpu import collect_cpu_metrics | ||
|
|
||
| metrics = {} | ||
|
|
||
| try: | ||
| cpu_percent = psutil.cpu_percent(interval=0.1, percpu=False) | ||
| metrics["cpu/utilization"] = cpu_percent | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| cpu_percents = psutil.cpu_percent(interval=0.1, percpu=True) | ||
| for i, percent in enumerate(cpu_percents): | ||
| metrics[f"cpu/{i}/utilization"] = percent | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| cpu_freq = psutil.cpu_freq() | ||
| if cpu_freq: | ||
| metrics["cpu/frequency"] = cpu_freq.current | ||
| if cpu_freq.max > 0: | ||
| metrics["cpu/frequency_max"] = cpu_freq.max | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| mem = psutil.virtual_memory() | ||
| metrics["memory/used"] = mem.used / (1024**3) | ||
| metrics["memory/total"] = mem.total / (1024**3) | ||
| metrics["memory/available"] = mem.available / (1024**3) | ||
| metrics["memory/percent"] = mem.percent | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| swap = psutil.swap_memory() | ||
| metrics["swap/used"] = swap.used / (1024**3) | ||
| metrics["swap/total"] = swap.total / (1024**3) | ||
| metrics["swap/percent"] = swap.percent | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| sensors_temps = psutil.sensors_temperatures() | ||
| if sensors_temps: | ||
| for name, entries in sensors_temps.items(): | ||
| for i, entry in enumerate(entries): | ||
| label = entry.label or f"{name}_{i}" | ||
| metrics[f"temp/{label}"] = entry.current | ||
| except Exception: | ||
| pass | ||
| metrics = collect_cpu_metrics(include_static=False) | ||
|
|
Comment on lines
300
to
304
| auto_log_gpu: bool | None = None, | ||
| gpu_log_interval: float = 10.0, | ||
| auto_log_cpu: bool = False, | ||
| cpu_log_interval: float = 10.0, | ||
| webhook_url: str | None = None, |
Comment on lines
+243
to
+247
| metrics = collect_cpu_metrics( | ||
| prev_disk_counters=self._last_disk_counters, | ||
| prev_net_counters=self._last_net_counters, | ||
| elapsed=elapsed, | ||
| include_static=False, |
Comment on lines
+55
to
+57
| cpu = [ | ||
| "psutil>=5.9.0", | ||
| ] |
Merged
Contributor
Author
|
Thanks @abidlabs for the review and the modifications! Happy to contribute |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #566
Changes
Adds optional cross-platform CPU/system metrics logging via
psutil.trackio/cpu.pymodule with:trackio.log_cpu()trackio.init()parameters:auto_log_cpu=Falsecpu_log_interval=10.0psutilis available through the optional extra:pip install trackio[cpu].trackio.log_cpu().Testing