[Feature]: Diffusion sleep to use process level memory calculation (#1276)

Signed-off-by: Divyansh Singhvi <divyanshsinghvi@gmail.com>
Signed-off-by: 汪志鹏 <wangzhipeng628@gmail.com>
Signed-off-by: dsinghvi <divyanshsinghvi@gmail.com>
Co-authored-by: 汪志鹏 <wangzhipeng628@gmail.com>
This commit is contained in:
dsinghviand汪志鹏 authored and GitHub committed 2026-02-12 12:17:54 +00:00
1 parent b16965dfa7
commit c6f314df4f
4 files changed
+121 -58

No files matched your search

+31 -26
View File
@@ -306,37 +306,42 @@ def in_container() -> bool:
return any(m in cg for m in markers)
def has_pid_host() -> bool | None:
"""
def _nvml_pid_matches() -> bool | None:
"""Check if os.getpid() matches what NVML would report via NSpid.
If /proc/self/status has a single NSpid entry, the container PID equals
the host PID, so NVML process-scoped memory tracking works correctly.
Returns:
True -> very likely running with --pid=host (host PID namespace)
False -> very likely isolated PID namespace (default)
True -> PID namespaces match (single NSpid entry)
False -> PID namespaces differ (multiple NSpid entries)
None -> cannot determine
"""
# Strong signal: in host pid namespace, PID 2 is usually kthreadd
comm2 = _read_text("/proc/2/comm")
if comm2 is not None:
comm2 = comm2.strip()
if comm2 == "kthreadd":
return True
# If PID 2 exists and is NOT kthreadd, we're almost certainly not in host pid ns
return False
# Fallback: check for other low-numbered kernel threads (best-effort)
for pid, name in [(3, "rcu_gp"), (4, "rcu_par_gp"), (10, "ksoftirqd/0")]:
comm = _read_text(f"/proc/{pid}/comm")
if comm is not None:
if comm.strip() == name:
return True
else:
return False
return False
status = _read_text("/proc/self/status")
if status is None:
return None
for line in status.splitlines():
if line.startswith("NSpid:"):
pids = line.split()[1:]
# Single PID means container PID == host PID
return len(pids) == 1
return None
def detect_pid_host() -> bool:
ic = in_container()
if not ic:
"""Check if os.getpid() matches the PID that NVML will report.
NVML always reports host-namespace PIDs. If this process lives in a
different PID namespace the match will fail and process-scoped memory
tracking won't work. We detect this via the NSpid field in
/proc/self/status: a single entry means host PID == container PID.
"""
if not in_container():
return True
return has_pid_host()
result = _nvml_pid_matches()
if result is not None:
return result
# Cannot determine — assume mismatch to be safe.
return False