This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Node.py
294 lines (236 loc) · 11.2 KB
/
Node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import string
import os
import re
import commands
from pUtil import tolog, readpar
from FileHandling import getMaxWorkDirSize
class Node:
""" worker node information """
def __init__(self):
self.cpu = 0
self.nodename = None
self.mem = 0
self.disk = 0
self.numberOfCores = self.setNumberOfCores()
self.benchmarks = None
# set the batch job and machine features
self.collectMachineFeatures()
def setNodeName(self, nm):
self.nodename = nm
def displayNodeInfo(self):
tolog("CPU: %0.2f, memory: %0.2f, disk space: %0.2f" % (self.cpu, self.mem, self.disk))
def isAVirtualMachine(self):
""" Are we running in a virtual machine? """
status = False
# if we are running inside a VM, then linux will put 'hypervisor' in cpuinfo
with open("/proc/cpuinfo", "r") as fd:
lines = fd.readlines()
for line in lines:
if "hypervisor" in line:
status = True
break
return status
def collectCoreInfo(self):
""" Collect information about cores and threads from lscpu """
# Thread(s) per core: 1
# Core(s) per socket: 1
# Socket(s): 8
threads_per_core = 0
cores_per_socket = 0
sockets = 0
threads_pattern = re.compile(r"Thread\(s\)\ per\ core\:.+(\d+)+")
cores_pattern = re.compile(r"Core\(s\)\ per\ socket\:.+(\d+)+")
sockets_pattern = re.compile(r"Socket\(s\)\:.+(\d+)+")
lscpu = os.popen('lscpu')
if lscpu:
lines = lscpu.readlines()
lscpu.close()
if lines:
for line in lines:
threads_found = re.findall(threads_pattern, line)
if len(threads_found) > 0:
tolog("Found %s thread(s) per core" % (threads_found[0]))
threads_per_core = int(threads_found[0])
cores_found = re.findall(cores_pattern, line)
if len(cores_found) > 0:
tolog("Found %s core(s) per socket" % (cores_found[0]))
cores_per_socket = int(cores_found[0])
sockets_found = re.findall(sockets_pattern, line)
if len(sockets_found) > 0:
tolog("Found %s socket(s)" % (sockets_found[0]))
sockets = int(sockets_found[0])
else:
tolog("No info from lscpu")
else:
tolog("Failed to open lscpu (cannot report core info with benchmarks)")
return threads_per_core, cores_per_socket, sockets
def collectWNInfo(self, diskpath):
""" collect node information (cpu, memory and disk space) """
with open("/proc/meminfo", "r") as fd:
mems = fd.readline()
while mems:
if mems.upper().find("MEMTOTAL") != -1:
self.mem = float(mems.split()[1])/1024
break
mems = fd.readline()
with open("/proc/cpuinfo", "r") as fd:
lines = fd.readlines()
for line in lines:
if not string.find(line, "cpu MHz"):
self.cpu = float(line.split(":")[1])
break
diskpipe = os.popen("df -mP %s" % (diskpath)) # -m = MB
disks = diskpipe.read()
if not diskpipe.close():
self.disk = float(disks.splitlines()[1].split()[3])
return self.mem, self.cpu, self.disk
def getNumberOfCores(self):
""" Report the number of cores """
return self.numberOfCores
def setNumberOfCores(self, nCores=0):
""" Get the number of cores """
# Note: this is usually as specified in the job description, ie the requested number of codes, not the
# physical number of cores on the WN
# Note also that this method will be called when the node object is created - before the job description
# is downloaded, to get a default initial value. The method should be called once the job has been downloaded.
# Since pilot version 69.2, the pilot will only set the number of cores if known by ATHENA_PROC_NUMBER
# If nCores is not specified, try to get it from the environment
if nCores == 0:
if 'ATHENA_PROC_NUMBER' in os.environ:
try:
nCores = int(os.environ['ATHENA_PROC_NUMBER'])
except:
nCores = 1
else:
nCores = 1
self.numberOfCores = nCores
def getNumberOfCoresFromEnvironment(self):
""" Get the number of cores from the environment """
nCores = None
if 'ATHENA_PROC_NUMBER' in os.environ:
try:
nCores = int(os.environ['ATHENA_PROC_NUMBER'])
except:
nCores = None
return nCores
def readValue(self, path):
""" Read value from file """
value = ""
try:
f = open(path, "r")
except IOError, e:
tolog("!!WARNING!!2344!! Failed to open file: %s" % (e))
else:
value = f.read()
f.close()
# protect against trailing new line
if value.endswith("\n"):
value = value[:-1]
return value
def setDataMember(self, directory, name):
""" Set a batch job or machine features data member """
data_member_value = ""
path = os.path.join(directory, name)
if os.path.exists(path):
data_member_value = self.readValue(path)
return data_member_value
def dumpValue(self, name, value):
""" Print a value if not empty """
if value != "":
tolog("%s = %s" % (name, value))
else:
tolog("%s was not set by the batch system" % (name))
def collectMachineFeatures(self):
""" Collect the machine and job features from /etc/machinefeatures and $JOBFEATURES """
# treat float and int values as strings
self.hs06 = ""
self.shutdownTime = ""
self.jobSlots = ""
self.physCores = ""
self.logCores = ""
self.cpuFactorLrms = ""
self.cpuLimitSecsLrms = ""
self.cpuLimitSecs = ""
self.wallLimitSecsLrms = ""
self.wallLimitSecs = ""
self.diskLimitGB = ""
self.jobStartSecs = ""
self.memLimitMB = ""
self.allocatedCPU = ""
tolog("Collecting machine features")
if os.environ.has_key('MACHINEFEATURES'):
MACHINEFEATURES = os.environ['MACHINEFEATURES']
self.hs06 = self.setDataMember(MACHINEFEATURES, "hs06")
self.shutdownTime = self.setDataMember(MACHINEFEATURES, "shutdowntime")
self.jobSlots = self.setDataMember(MACHINEFEATURES, "jobslots")
self.physCores = self.setDataMember(MACHINEFEATURES, "phys_cores")
self.logCores = self.setDataMember(MACHINEFEATURES, "log_cores")
tolog("Machine features:")
self.dumpValue("hs06", self.hs06)
self.dumpValue("shutdowntime", self.shutdownTime)
self.dumpValue("jobslots", self.jobSlots)
self.dumpValue("phys_cores", self.physCores)
self.dumpValue("log_cores", self.logCores)
else:
tolog("$MACHINEFEATURES not defined locally")
if os.environ.has_key('JOBFEATURES'):
JOBFEATURES = os.environ['JOBFEATURES']
self.cpuFactorLrms = self.setDataMember(JOBFEATURES, "cpufactor_lrms")
self.cpuLimitSecsLrms = self.setDataMember(JOBFEATURES, "cpu_limit_secs_lrms")
self.cpuLimitSecs = self.setDataMember(JOBFEATURES, "cpu_limit_secs")
self.wallLimitSecsLrms = self.setDataMember(JOBFEATURES, "wall_limit_secs_lrms")
self.wallLimitSecs = self.setDataMember(JOBFEATURES, "wall_limit_secs")
self.diskLimitGB = self.setDataMember(JOBFEATURES, "disk_limit_GB")
self.jobStartSecs = self.setDataMember(JOBFEATURES, "jobstart_secs")
self.memLimitMB = self.setDataMember(JOBFEATURES, "mem_limit_MB")
self.allocatedCPU = self.setDataMember(JOBFEATURES, "allocated_CPU")
tolog("Batch system job features:")
self.dumpValue("cpufactor_lrms", self.cpuFactorLrms)
self.dumpValue("cpu_limit_secs_lrms", self.cpuLimitSecsLrms)
self.dumpValue("cpu_limit_secs", self.cpuLimitSecs)
self.dumpValue("wall_limit_secs_lrms", self.wallLimitSecsLrms)
self.dumpValue("wall_limit_secs", self.wallLimitSecs)
self.dumpValue("disk_limit_GB", self.diskLimitGB)
self.dumpValue("jobstart_secs", self.jobStartSecs)
self.dumpValue("mem_limit_MB", self.memLimitMB)
self.dumpValue("allocated_CPU", self.allocatedCPU)
else:
tolog("$JOBFEATURES not defined locally")
cmd = "hostname -i"
tolog("Executing command: %s" % (cmd))
out = commands.getoutput(cmd)
tolog("IP number of worker node: %s" % (out))
def addFieldToJobMetrics(self, name, value):
""" Add a substring field to the job metrics string """
jobMetrics = ""
if value != "":
jobMetrics += "%s=%s " % (name, value)
return jobMetrics
def addToJobMetrics(self, jobResult, path, jobId):
""" Add the batch job and machine features to the job metrics """
jobMetrics = ""
# jobMetrics += self.addFieldToJobMetrics("hs06", self.hs06)
# jobMetrics += self.addFieldToJobMetrics("shutdowntime", self.shutdownTime)
# jobMetrics += self.addFieldToJobMetrics("jobslots", self.jobSlots)
# jobMetrics += self.addFieldToJobMetrics("phys_cores", self.physCores)
# jobMetrics += self.addFieldToJobMetrics("log_cores", self.logCores)
# jobMetrics += self.addFieldToJobMetrics("cpufactor_lrms", self.cpuFactorLrms)
# jobMetrics += self.addFieldToJobMetrics("cpu_limit_secs_lrms", self.cpuLimitSecsLrms)
# jobMetrics += self.addFieldToJobMetrics("cpu_limit_secs", self.cpuLimitSecs)
# jobMetrics += self.addFieldToJobMetrics("wall_limit_secs_lrms", self.wallLimitSecsLrms)
# jobMetrics += self.addFieldToJobMetrics("wall_limit_secs", self.wallLimitSecs)
# jobMetrics += self.addFieldToJobMetrics("disk_limit_GB", self.diskLimitGB)
# jobMetrics += self.addFieldToJobMetrics("jobstart_secs", self.jobStartSecs)
# jobMetrics += self.addFieldToJobMetrics("mem_limit_MB", self.memLimitMB)
# jobMetrics += self.addFieldToJobMetrics("allocated_CPU", self.allocatedCPU)
# Get the max disk space used by the payload (at the end of a job)
if jobResult == "finished" or jobResult == "failed" or jobResult == "holding":
max_space = getMaxWorkDirSize(path, jobId)
if max_space > 0L:
jobMetrics += self.addFieldToJobMetrics("workDirSize", max_space)
else:
tolog("Will not add max space = %d to job metrics" % (max_space))
return jobMetrics
def getBenchmarkDictionary(self):
""" Return the benchmark dictionary """
return self.benchmarks