Skip to content

Commit b0cf6ac

Browse files
committed
fix: use pathlib, and a dict of information
1 parent a62c940 commit b0cf6ac

File tree

1 file changed

+9
-12
lines changed
  • src/DIRAC/WorkloadManagementSystem/JobWrapper

1 file changed

+9
-12
lines changed

src/DIRAC/WorkloadManagementSystem/JobWrapper/Watchdog.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import socket
2525
import getpass
2626
import psutil
27+
from pathlib import Path
2728

2829
from DIRAC import S_OK, S_ERROR, gLogger
2930
from DIRAC.Core.Utilities import Time
@@ -38,7 +39,7 @@
3839
from DIRAC.WorkloadManagementSystem.Client import JobMinorStatus
3940

4041

41-
class Watchdog(object):
42+
class Watchdog:
4243

4344
#############################################################################
4445
def __init__(self, pid, exeThread, spObject, jobCPUTime, memoryLimit=0, processors=1, jobArgs={}):
@@ -968,17 +969,13 @@ def getNodeInformation(self):
968969
result["Memory(kB)"] = int(psutil.virtual_memory()[1] / 1024)
969970
result["LocalAccount"] = getpass.getuser()
970971

971-
if os.path.exists("/proc/cpuinfo"):
972-
with open("/proc/cpuinfo", "r") as cpuinfo:
973-
info = cpuinfo.readlines()
974-
try:
975-
result["ModelName"] = info[4].split(":")[1].replace(" ", "").replace("\n", "")
976-
except IndexError:
977-
pass
978-
try:
979-
result["CacheSize(kB)"] = [x.strip().split(":")[1] for x in info if "cache size" in x][0].strip()
980-
except IndexError:
981-
pass
972+
path = Path("/proc/cpuinfo")
973+
if path.is_file():
974+
# Assume all of the CPUs are the same and only consider the first one
975+
raw_info = path.read_text().split("\n\n")[0]
976+
info = dict(map(str.strip, x.split(":", 1)) for x in raw_info.split("\n"))
977+
result["ModelName"] = info.get("model name", "Unknown")
978+
result["CacheSize(kB)"] = info.get("cache size", "Unknown")
982979

983980
return result
984981

0 commit comments

Comments
 (0)