Skip to content

Add CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices#567

Merged
abidlabs merged 17 commits into
gradio-app:mainfrom
yannsadowski:fix-issue-566
Jun 10, 2026
Merged

Add CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices#567
abidlabs merged 17 commits into
gradio-app:mainfrom
yannsadowski:fix-issue-566

Conversation

@yannsadowski

@yannsadowski yannsadowski commented May 27, 2026

Copy link
Copy Markdown
Contributor

Closes #566

Changes

Adds optional cross-platform CPU/system metrics logging via psutil.

  • New trackio/cpu.py module with:
    • CPU utilization, per-core utilization, frequency, and core counts
    • memory and swap usage
    • disk I/O rates and cumulative byte counters
    • network I/O rates and cumulative byte counters
    • temperatures and battery status when available
  • New public API: trackio.log_cpu()
  • New trackio.init() parameters:
    • auto_log_cpu=False
    • cpu_log_interval=10.0
  • CPU/system metrics are opt-in to avoid surprising background logging.
  • psutil is available through the optional extra: pip install trackio[cpu].
  • Apple Silicon metrics now reuse the shared CPU/system metric collector while preserving static CPU metrics.
  • System Metrics empty-state copy now mentions CPU/system metrics and trackio.log_cpu().

Testing

pytest tests/unit/test_cpu.py tests/unit/test_gpu.py -q
cd trackio/frontend && npm run build

…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>
@yannsadowski

Copy link
Copy Markdown
Contributor Author
image image image

@abidlabs abidlabs requested a review from Copilot June 2, 2026 19:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with collect_cpu_metrics(), CpuMonitor, and log_cpu().
  • Extends Run and trackio.init() to support auto_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 thread trackio/cpu.py Outdated
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 thread trackio/cpu.py
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 thread trackio/cpu.py
Comment on lines +233 to +235
while not self._stop_flag.is_set():
self._stop_flag.wait(timeout=self._interval)
try:
Comment thread trackio/cpu.py
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 thread pyproject.toml
Comment on lines +56 to +58
cpu = [
"psutil>=5.9.0",
]
yannsadowski and others added 8 commits June 3, 2026 17:17
- 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)
@abidlabs abidlabs changed the title feat: add CPU, RAM, disk, network and sensor metrics monitoring dd CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices Jun 10, 2026
@abidlabs abidlabs changed the title dd CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices Add CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices Jun 10, 2026
@abidlabs abidlabs requested a review from Copilot June 10, 2026 04:07
@gradio-pr-bot

gradio-pr-bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

🪼 branch checks and previews

Name Status URL
🦄 Changes detected! Details

@gradio-pr-bot

Copy link
Copy Markdown
Contributor

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
trackio minor

  • Add CPU, RAM, disk, network and sensor metrics monitoring for non-Apple devices

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Jun 10, 2026

Copy link
Copy Markdown

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview

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"

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 9 changed files in this pull request and generated 6 comments.

Comment thread trackio/cpu.py Outdated
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 thread trackio/cpu.py Outdated
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 thread trackio/apple_gpu.py Outdated
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 thread trackio/__init__.py
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 thread trackio/cpu.py Outdated
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 thread pyproject.toml
Comment on lines +55 to +57
cpu = [
"psutil>=5.9.0",
]

@abidlabs abidlabs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @yannsadowski!

@abidlabs abidlabs merged commit 8c0ce21 into gradio-app:main Jun 10, 2026
@yannsadowski

Copy link
Copy Markdown
Contributor Author

Thanks @abidlabs for the review and the modifications! Happy to contribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CPU and RAM metrics to system monitoring

5 participants