Server: the sampler's and the HIP probe's files are closed after reading, so the ResourceWarning lines of every run are gone; no answer changes (#4654)

This commit is contained in:
Niko1221 committed 2026-10-31 12:58:46 +01:00
1 parent c9efd49202
commit 5c0259e20c
3 files changed
+63 -8

No files matched your search

+4 -2
View File
@@ -3445,7 +3445,8 @@ def hip_speed_scores(indices: list[int], root: str = "/sys/class/kfd/kfd/topolog
cards = []
for n in nodes:
try:
props = dict(line.split() for line in open(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
with open(os.path.join(root, str(n), "properties")) as fh:
props = dict(line.split() for line in fh if len(line.split()) == 2)
simd, clk = int(props.get("simd_count", 0)), int(props.get("max_engine_clk_fcompute", 0))
except (OSError, ValueError):
continue
@@ -3482,7 +3483,8 @@ def hip_gpu_archs(root: str = "/sys/class/kfd/kfd/topology/nodes") -> dict[int,
out: dict[int, str] = {}
for n in nodes:
try:
props = dict(line.split() for line in open(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
with open(os.path.join(root, str(n), "properties")) as fh:
props = dict(line.split() for line in fh if len(line.split()) == 2)
ver, simd = int(props.get("gfx_target_version", 0)), int(props.get("simd_count", 0))
except (OSError, ValueError):
continue
+10 -6
View File
@@ -455,7 +455,8 @@ def _cpu_topology(root: str = "/sys/devices/system"):
packages = set()
for f in glob.glob(os.path.join(root, "cpu", "cpu[0-9]*", "topology", "physical_package_id")):
try:
packages.add(open(f, encoding="utf-8").read().strip())
with open(f, encoding="utf-8") as fh:
packages.add(fh.read().strip())
except OSError:
continue
nodes = len(glob.glob(os.path.join(root, "node", "node[0-9]*")))
@@ -473,9 +474,10 @@ def _cpu_name():
except OSError:
pass
elif os.path.exists("/proc/cpuinfo"):
for line in open("/proc/cpuinfo", encoding="utf-8", errors="replace"):
if line.startswith("model name"):
return line.split(":", 1)[1].strip()
with open("/proc/cpuinfo", encoding="utf-8", errors="replace") as fh:
for line in fh:
if line.startswith("model name"):
return line.split(":", 1)[1].strip()
return platform.processor() or None
@@ -492,7 +494,8 @@ class _CpuRamFallback:
return idle.value, kern.value + user.value # kernel time includes idle
return None
try:
f = [int(x) for x in open("/proc/stat").readline().split()[1:]]
with open("/proc/stat") as fh:
f = [int(x) for x in fh.readline().split()[1:]]
return f[3] + f[4], sum(f)
except (OSError, ValueError):
return None
@@ -519,7 +522,8 @@ class _CpuRamFallback:
return m.ullTotalPhys - m.ullAvailPhys, m.ullTotalPhys
return None, None
try:
info = dict(line.split(":", 1) for line in open("/proc/meminfo"))
with open("/proc/meminfo") as fh:
info = dict(line.split(":", 1) for line in fh)
total = int(info["MemTotal"].split()[0]) * 1024
avail = int(info["MemAvailable"].split()[0]) * 1024
return total - avail, total
+49
View File
@@ -0,0 +1,49 @@
"""#4654: the sampler's and the HIP probe's files are closed (no ResourceWarning lines on every run).
python -m unittest serve.test_resource_warnings
"""
import gc
import os
import sys
import tempfile
import unittest
import warnings
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from serve import server, telemetry # noqa: E402
@unittest.skipUnless(sys.platform.startswith("linux"), "reads /proc and /sys")
class Closed(unittest.TestCase):
def quiet(self, call):
with warnings.catch_warnings(record=True) as seen:
warnings.simplefilter("always", ResourceWarning)
call()
gc.collect()
return [str(w.message) for w in seen if issubclass(w.category, ResourceWarning)]
def test_the_cpu_and_ram_sampler_closes_its_files(self):
def run():
fallback = telemetry._CpuRamFallback()
fallback.cpu()
fallback.ram()
telemetry._cpu_name()
self.assertEqual(self.quiet(run), [])
def test_the_cpu_topology_closes_its_files(self):
self.assertEqual(self.quiet(lambda: telemetry._cpu_topology()), [])
def test_the_hip_probes_close_their_files(self):
with tempfile.TemporaryDirectory() as root:
node = Path(root) / "0"
node.mkdir()
(node / "properties").write_text("simd_count 256\nmax_engine_clk_fcompute 2000\ngfx_target_version 110501\n")
self.assertEqual(self.quiet(lambda: server.hip_speed_scores([0], root)), [])
self.assertEqual(self.quiet(lambda: server.hip_gpu_archs(root)), [])
self.assertEqual(server.hip_gpu_archs(root), {0: "gfx1151"})
if __name__ == "__main__":
unittest.main()