Repository navigation
Expand file tree
/
Copy pathgpu_probe.py
More file actions
executable file
·89 lines (72 loc) · 2.49 KB
/
Copy pathgpu_probe.py
File metadata and controls
executable file
·89 lines (72 loc) · 2.49 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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from collections import defaultdict
GPU_QUERY = [
"nvidia-smi",
"--query-gpu=index,uuid,memory.total,memory.used,utilization.gpu",
"--format=csv,noheader,nounits",
]
APP_QUERY = [
"nvidia-smi",
"--query-compute-apps=gpu_uuid,pid,used_memory",
"--format=csv,noheader,nounits",
]
def run_command(command: list[str]) -> str:
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip() or f"command failed: {' '.join(command)}")
return result.stdout.strip()
def parse_csv_lines(raw: str, expected_fields: int) -> list[list[str]]:
rows: list[list[str]] = []
if not raw:
return rows
for line in raw.splitlines():
parts = [part.strip() for part in line.split(",")]
if len(parts) < expected_fields:
continue
rows.append(parts[:expected_fields])
return rows
def build_probe() -> dict:
gpu_rows = parse_csv_lines(run_command(GPU_QUERY), 5)
app_rows = parse_csv_lines(run_command(APP_QUERY), 3)
apps_by_uuid: dict[str, list[dict]] = defaultdict(list)
for gpu_uuid, pid, used_memory_mb in app_rows:
apps_by_uuid[gpu_uuid].append(
{
"pid": int(pid),
"used_memory_mb": int(used_memory_mb),
}
)
gpus = []
for index, uuid, memory_total_mb, memory_used_mb, utilization_gpu in gpu_rows:
processes = apps_by_uuid.get(uuid, [])
gpus.append(
{
"index": int(index),
"uuid": uuid,
"memory_total_mb": int(memory_total_mb),
"memory_used_mb": int(memory_used_mb),
"memory_free_mb": int(memory_total_mb) - int(memory_used_mb),
"utilization_gpu": int(utilization_gpu),
"process_count": len(processes),
"processes": processes,
}
)
return {"gpus": gpus}
def main() -> int:
parser = argparse.ArgumentParser(description="Probe local GPU state via nvidia-smi.")
parser.parse_args()
try:
payload = build_probe()
except Exception as exc: # noqa: BLE001
print(json.dumps({"error": str(exc)}), file=sys.stderr)
return 1
json.dump(payload, sys.stdout, indent=2)
sys.stdout.write("\n")
return 0
if __name__ == "__main__":
raise SystemExit(main())