59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
"""Best-effort scene/resource diagnostics. Never a Gaussian-count admission gate."""
|
|
|
|
import ctypes
|
|
import subprocess
|
|
|
|
|
|
class ScenePreparationError(RuntimeError):
|
|
"""A bounded, operator-readable preparation failure."""
|
|
|
|
|
|
def host_available_bytes():
|
|
class Memory(ctypes.Structure):
|
|
_fields_ = [("length", ctypes.c_ulong), ("load", ctypes.c_ulong)] + [
|
|
(name, ctypes.c_ulonglong) for name in (
|
|
"total", "available", "total_page", "available_page",
|
|
"total_virtual", "available_virtual", "extended")]
|
|
memory = Memory()
|
|
memory.length = ctypes.sizeof(memory)
|
|
if not ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(memory)):
|
|
raise OSError("Memory observation unavailable")
|
|
return memory.available
|
|
|
|
|
|
def capacity_snapshot():
|
|
result = subprocess.run([
|
|
"nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits",
|
|
], capture_output=True, text=True, timeout=10, check=True)
|
|
return dict(gpu_free_bytes=int(result.stdout.splitlines()[0].strip()) * 1024**2,
|
|
host_free_bytes=host_available_bytes())
|
|
|
|
|
|
def scene_diagnostics(count, source_bytes):
|
|
value = dict(schema_version="missioncore.scene-diagnostics/v1",
|
|
gaussian_count=count, source_bytes=source_bytes, authority="observation-only")
|
|
try:
|
|
value.update(capacity_snapshot())
|
|
except Exception as exc:
|
|
value["capacity_unavailable"] = type(exc).__name__
|
|
return value
|
|
|
|
|
|
class RendererMemoryGuard:
|
|
"""A reported allocation failure invalidates the scene, even if Kit keeps running."""
|
|
def __init__(self, log):
|
|
self.log, self.offset, self.tail = log, 0, b""
|
|
|
|
def check(self):
|
|
with self.log.open("rb") as stream:
|
|
stream.seek(self.offset)
|
|
while chunk := stream.read(65536):
|
|
content = self.tail + chunk
|
|
if (b"ERROR_OUT_OF_DEVICE_MEMORY" in content or
|
|
b"Out of GPU memory allocating resource" in content):
|
|
raise ScenePreparationError(
|
|
"Недостаточно видеопамяти для полной сцены выбранной детализации. "
|
|
"Прогон остановлен: корректность изображения не подтверждена.")
|
|
self.tail = content[-128:]
|
|
self.offset = stream.tell()
|