forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
178 lines (153 loc) · 5.93 KB
/
Copy pathmonitor.py
File metadata and controls
178 lines (153 loc) · 5.93 KB
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
"""v0.44.0 Part A — `soup monitor` GPU live-monitor command.
Renders a Rich panel with one row per detected GPU: Util / Temp / VRAM /
Power. Polls `nvidia-smi` (Linux/Windows/CUDA) at the configured refresh
rate, or `powermetrics` on Apple Silicon.
"""
from __future__ import annotations
import time
import typer
from rich.console import Console
from rich.live import Live
from rich.markup import escape
from rich.panel import Panel
from rich.table import Table
from soup_cli.utils.gpu_monitor import (
GpuSample,
PowermetricsStatus,
detect_apple_silicon,
query_nvidia_smi,
query_powermetrics,
)
console = Console()
def _format_pct(value: float | None) -> str:
return "—" if value is None else f"{value:5.1f}%"
def _format_mb(value: float | None) -> str:
return "—" if value is None else f"{value:7.0f} MB"
def _format_temp(value: float | None) -> str:
return "—" if value is None else f"{value:4.0f}°C"
def _format_power(value: float | None) -> str:
return "—" if value is None else f"{value:5.1f} W"
def _build_table(samples: list[GpuSample]) -> Table:
table = Table(show_header=True, header_style="bold cyan")
table.add_column("GPU", justify="right")
table.add_column("Name", overflow="fold")
table.add_column("Util", justify="right")
table.add_column("Mem Util", justify="right")
table.add_column("VRAM Used", justify="right")
table.add_column("VRAM Total", justify="right")
table.add_column("Temp", justify="right")
table.add_column("Power", justify="right")
for sample in samples:
table.add_row(
str(sample.index),
escape(sample.name),
_format_pct(sample.util_gpu_pct),
_format_pct(sample.util_mem_pct),
_format_mb(sample.mem_used_mb),
_format_mb(sample.mem_total_mb),
_format_temp(sample.temp_c),
_format_power(sample.power_w),
)
return table
def _powermetrics_error(status: PowermetricsStatus) -> str:
if status is PowermetricsStatus.PERMISSION_DENIED:
return (
"[yellow]Apple GPU metrics require permission to run "
"`/usr/bin/powermetrics`.[/]\n"
"Run [bold]sudo -v[/] in a terminal, then rerun `soup monitor`. "
"Soup uses `sudo -n`: it never prompts for or reads your password."
)
if status is PowermetricsStatus.UNAVAILABLE:
return (
"[yellow]`/usr/bin/powermetrics` is unavailable on this Mac. "
"Use Activity Monitor → Window → GPU History.[/]"
)
if status is PowermetricsStatus.INVALID_OUTPUT:
return (
"[yellow]`powermetrics` returned no usable Apple GPU sample. "
"Its plist schema may not expose `gpu_power` on this Mac.[/]"
)
return "[yellow]`powermetrics` failed or timed out while reading Apple GPU metrics.[/]"
def _monitor_apple_silicon(refresh: float, once: bool) -> None:
title = "Soup GPU Monitor — Apple Silicon"
result = query_powermetrics(refresh)
if result.status is not PowermetricsStatus.OK:
console.print(_powermetrics_error(result.status))
raise typer.Exit(code=1)
samples = list(result.samples)
if once or not samples:
console.print(Panel(_build_table(samples), title=title))
return
with Live(
Panel(_build_table(samples), title=title),
refresh_per_second=max(1.0, 1.0 / refresh),
screen=False,
console=console,
) as live:
try:
while True:
# powermetrics itself blocks for the requested sample window;
# sleeping here as well would double the refresh interval.
fresh = query_powermetrics(refresh)
if fresh.status is not PowermetricsStatus.OK:
live.update(Panel(_powermetrics_error(fresh.status), title=title))
# Failure paths can return immediately; avoid a busy loop.
time.sleep(refresh)
continue
live.update(Panel(_build_table(list(fresh.samples)), title=title))
except KeyboardInterrupt:
console.print("[dim]exit[/]")
def monitor(
refresh: float = typer.Option(
2.0,
"--refresh",
"-r",
help="Refresh interval in seconds (0.25 to 30).",
),
once: bool = typer.Option(
False,
"--once",
help="Print one snapshot and exit (skip the live panel).",
),
) -> None:
"""Live GPU monitor: Util / Temp / VRAM / Power per GPU.
Uses nvidia-smi on NVIDIA systems and powermetrics on Apple Silicon.
"""
if not (0.25 <= refresh <= 30.0):
console.print("[red]--refresh must be in [0.25, 30][/]")
raise typer.Exit(code=2)
if detect_apple_silicon():
_monitor_apple_silicon(refresh, once)
return
ok, samples = query_nvidia_smi()
if not ok:
console.print(
"[yellow]nvidia-smi not found or returned non-zero. "
"Install NVIDIA drivers + CUDA toolkit, or run on a GPU host.[/]"
)
raise typer.Exit(code=1)
if once or not samples:
console.print(Panel(_build_table(samples), title="Soup GPU Monitor"))
return
with Live(
Panel(_build_table(samples), title="Soup GPU Monitor"),
refresh_per_second=max(1.0, 1.0 / refresh),
screen=False,
) as live:
try:
while True:
time.sleep(refresh)
ok, fresh = query_nvidia_smi()
if not ok:
live.update(
Panel(
"[yellow]nvidia-smi unavailable[/]",
title="Soup GPU Monitor",
)
)
continue
live.update(
Panel(_build_table(fresh), title="Soup GPU Monitor")
)
except KeyboardInterrupt:
console.print("[dim]exit[/]")