Spaces:
Running
Running
Download lpu/runtime/process_discovery.py from iparaco/Virtual-LPU: direct link, hf CLI and curl.
- Browser
- Download file 4.42 kB
-
https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/runtime/process_discovery.py
- Command line
-
hf download hf://spaces/iparaco/Virtual-LPU/lpu/runtime/process_discovery.py
-
curl -L -o process_discovery.py https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/runtime/process_discovery.py
4.42 kB
| """Process discovery: READ-ONLY monitoring of running AI processes. | |
| What this does: | |
| - enumerate processes via psutil | |
| - classify python processes (torch/SR evidence from cmdline, optional deep | |
| header scan) WITHOUT modifying anything | |
| - attribute per-process VRAM via NVML's process list (when available) | |
| What this never does: | |
| - inject code into running processes | |
| - alter CUDA contexts | |
| - kill, renice, or otherwise touch foreign processes | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import time | |
| from dataclasses import dataclass, field | |
| from typing import Dict, List, Optional | |
| import psutil | |
| from ..monitoring.gpu_monitor import GPUMonitor | |
| from ..integration.project_detector import classify_cmdline, deep_scan | |
| class DiscoveredProcess: | |
| pid: int | |
| exe: Optional[str] | |
| cmdline: List[str] | |
| cpu_pct: float | |
| mem_mb: float | |
| create_time: float | |
| kind: str | |
| evidence: List[str] = field(default_factory=list) | |
| gpu_vram_mb: Optional[float] = None | |
| entry_script: Optional[str] = None | |
| def summary(self) -> dict: | |
| return { | |
| "pid": self.pid, | |
| "exe": self.exe, | |
| "cmdline": self.cmdline[:10], | |
| "cpu_pct": round(self.cpu_pct, 1), | |
| "mem_mb": round(self.mem_mb, 1), | |
| "age_s": round(time.time() - self.create_time, 0), | |
| "kind": self.kind, | |
| "evidence": self.evidence, | |
| "gpu_vram_mb": self.gpu_vram_mb, | |
| "entry_script": self.entry_script, | |
| } | |
| _PYTHON_NAMES = { | |
| "python", "python3", "pythonw", "python.exe", "python3.exe", "pythonw.exe", | |
| } | |
| def _is_python(proc: psutil.Process) -> bool: | |
| try: | |
| name = (proc.name() or "").lower() | |
| if name in _PYTHON_NAMES or name.startswith("python"): | |
| return True | |
| exe = (proc.exe() or "").lower() | |
| return "python" in exe.split(os.sep)[-1] if exe else False | |
| except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
| return False | |
| class ProcessDiscovery: | |
| def __init__(self, deep_scan: bool = False, log_dir=None, | |
| gpu_monitor: Optional[GPUMonitor] = None): | |
| from ..monitoring.logger import get_logger | |
| self.deep_scan = deep_scan | |
| self.gpu_monitor = gpu_monitor | |
| self._log = get_logger("discovery", log_dir=log_dir, console=False) | |
| def scan(self) -> List[DiscoveredProcess]: | |
| """One read-only sweep. Safe to call at any frequency.""" | |
| vram_by_pid: Dict[int, float] = {} | |
| if self.gpu_monitor is not None: | |
| vram_by_pid = self.gpu_monitor.per_process_vram_mb() | |
| found: List[DiscoveredProcess] = [] | |
| for proc in psutil.process_iter(["pid", "name", "exe", "cmdline", "cpu_percent", | |
| "memory_info", "create_time"]): | |
| try: | |
| if not _is_python(proc): | |
| continue | |
| info = proc.info | |
| cmdline = info.get("cmdline") or [] | |
| if not cmdline: | |
| continue | |
| pinfo = classify_cmdline(cmdline) | |
| if self.deep_scan and pinfo.entry_script and pinfo.kind == "unknown": | |
| deeper = deep_scan(pinfo.entry_script) | |
| if deeper: | |
| pinfo = deeper | |
| gpu_mb = vram_by_pid.get(proc.pid) | |
| interesting = pinfo.kind != "unknown" or gpu_mb is not None | |
| if not interesting: | |
| continue | |
| found.append(DiscoveredProcess( | |
| pid=proc.pid, | |
| exe=info.get("exe"), | |
| cmdline=cmdline, | |
| cpu_pct=info.get("cpu_percent") or 0.0, | |
| mem_mb=(info.get("memory_info").rss / (1024 * 1024)) | |
| if info.get("memory_info") else 0.0, | |
| create_time=info.get("create_time") or time.time(), | |
| kind=pinfo.kind, | |
| evidence=pinfo.evidence, | |
| gpu_vram_mb=gpu_mb, | |
| entry_script=pinfo.entry_script, | |
| )) | |
| except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
| continue | |
| except Exception: # noqa: BLE001 - one bad process never breaks a sweep | |
| continue | |
| found.sort(key=lambda p: -(p.gpu_vram_mb or 0.0)) | |
| return found | |