Merge remote-tracking branch 'remotes/origin/main' into dev/rebase-v0.16.0
This commit is contained in:
commit
15099119dd
1 file changed
+26
-31
@@ -306,42 +306,37 @@ def in_container() -> bool:
|
|||||||
return any(m in cg for m in markers)
|
return any(m in cg for m in markers)
|
||||||
|
|
||||||
|
|
||||||
def _nvml_pid_matches() -> bool | None:
|
def has_pid_host() -> 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:
|
Returns:
|
||||||
True -> PID namespaces match (single NSpid entry)
|
True -> very likely running with --pid=host (host PID namespace)
|
||||||
False -> PID namespaces differ (multiple NSpid entries)
|
False -> very likely isolated PID namespace (default)
|
||||||
None -> cannot determine
|
None -> cannot determine
|
||||||
"""
|
"""
|
||||||
status = _read_text("/proc/self/status")
|
# Strong signal: in host pid namespace, PID 2 is usually kthreadd
|
||||||
if status is None:
|
comm2 = _read_text("/proc/2/comm")
|
||||||
return None
|
if comm2 is not None:
|
||||||
for line in status.splitlines():
|
comm2 = comm2.strip()
|
||||||
if line.startswith("NSpid:"):
|
if comm2 == "kthreadd":
|
||||||
pids = line.split()[1:]
|
return True
|
||||||
# Single PID means container PID == host PID
|
# If PID 2 exists and is NOT kthreadd, we're almost certainly not in host pid ns
|
||||||
return len(pids) == 1
|
return False
|
||||||
return None
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
def detect_pid_host() -> bool:
|
def detect_pid_host() -> bool:
|
||||||
"""Check if os.getpid() matches the PID that NVML will report.
|
ic = in_container()
|
||||||
|
if not ic:
|
||||||
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 True
|
||||||
|
|
||||||
result = _nvml_pid_matches()
|
return has_pid_host()
|
||||||
if result is not None:
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Cannot determine — assume mismatch to be safe.
|
|
||||||
return False
|
|
||||||
Reference in new issue
Block a user