Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -199,9 +199,16 @@ async def get_stockfish_engine():
|
|
| 199 |
|
| 200 |
|
| 201 |
async def _clear_engine_hash(engine) -> None:
|
| 202 |
-
"""
|
| 203 |
try:
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
await asyncio.wait_for(engine.ping(), timeout=5.0)
|
| 206 |
except Exception as e:
|
| 207 |
print(f"[WARNING] Failed to clear engine hash: {e}")
|
|
@@ -383,16 +390,31 @@ async def health_ready():
|
|
| 383 |
|
| 384 |
@app.get("/ram")
|
| 385 |
def ram_usage():
|
| 386 |
-
"""Monitor RAM usage
|
| 387 |
process = psutil.Process(os.getpid())
|
| 388 |
mem = process.memory_info()
|
| 389 |
mem_mb = mem.rss / 1024 / 1024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
return {
|
| 391 |
"rss_mb": round(mem_mb, 2),
|
|
|
|
|
|
|
|
|
|
| 392 |
"vms_mb": round(mem.vms / 1024 / 1024, 2),
|
| 393 |
"threshold_mb": _RAM_CLEANUP_THRESHOLD_MB,
|
| 394 |
"cleanup_interval_sec": _RAM_CLEANUP_INTERVAL_SEC,
|
| 395 |
-
"status": "high" if
|
| 396 |
"active_rooms": len(manager.active_connections),
|
| 397 |
"active_connections": sum(len(v) for v in manager.active_connections.values()),
|
| 398 |
}
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
async def _clear_engine_hash(engine) -> None:
|
| 202 |
+
"""Best-effort clear of transposition table/internal game state."""
|
| 203 |
try:
|
| 204 |
+
# Preferred path for Stockfish-family engines.
|
| 205 |
+
await engine.configure({"Clear Hash": True})
|
| 206 |
+
except Exception:
|
| 207 |
+
pass
|
| 208 |
+
try:
|
| 209 |
+
# Also reset game state so the engine does not keep game-history context.
|
| 210 |
+
if hasattr(engine, "protocol") and hasattr(engine.protocol, "send_line"):
|
| 211 |
+
engine.protocol.send_line("ucinewgame")
|
| 212 |
await asyncio.wait_for(engine.ping(), timeout=5.0)
|
| 213 |
except Exception as e:
|
| 214 |
print(f"[WARNING] Failed to clear engine hash: {e}")
|
|
|
|
| 390 |
|
| 391 |
@app.get("/ram")
|
| 392 |
def ram_usage():
|
| 393 |
+
"""Monitor RAM usage for API process + child engine processes."""
|
| 394 |
process = psutil.Process(os.getpid())
|
| 395 |
mem = process.memory_info()
|
| 396 |
mem_mb = mem.rss / 1024 / 1024
|
| 397 |
+
child_rss_mb = 0.0
|
| 398 |
+
child_count = 0
|
| 399 |
+
try:
|
| 400 |
+
for child in process.children(recursive=True):
|
| 401 |
+
try:
|
| 402 |
+
child_rss_mb += child.memory_info().rss / 1024 / 1024
|
| 403 |
+
child_count += 1
|
| 404 |
+
except Exception:
|
| 405 |
+
pass
|
| 406 |
+
except Exception:
|
| 407 |
+
pass
|
| 408 |
+
total_mb = mem_mb + child_rss_mb
|
| 409 |
return {
|
| 410 |
"rss_mb": round(mem_mb, 2),
|
| 411 |
+
"child_rss_mb": round(child_rss_mb, 2),
|
| 412 |
+
"total_process_tree_rss_mb": round(total_mb, 2),
|
| 413 |
+
"child_process_count": child_count,
|
| 414 |
"vms_mb": round(mem.vms / 1024 / 1024, 2),
|
| 415 |
"threshold_mb": _RAM_CLEANUP_THRESHOLD_MB,
|
| 416 |
"cleanup_interval_sec": _RAM_CLEANUP_INTERVAL_SEC,
|
| 417 |
+
"status": "high" if total_mb > _RAM_CLEANUP_THRESHOLD_MB else "ok",
|
| 418 |
"active_rooms": len(manager.active_connections),
|
| 419 |
"active_connections": sum(len(v) for v in manager.active_connections.values()),
|
| 420 |
}
|