-
Notifications
You must be signed in to change notification settings - Fork 6
/
DIRACbenchmark.py
executable file
·242 lines (188 loc) · 6.83 KB
/
DIRACbenchmark.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
#!/usr/bin/python
########################################################################
# File : DIRACbenchmark.py
# Author : Andrew McNab
########################################################################
""" DIRAC Benchmark 2012 by Ricardo Graciani, and wrapper functions to
run multiple instances in parallel by Andrew McNab.
This file (DIRACbenchmark.py) is intended to be the ultimate upstream
shared by different users of the DIRAC Benchmark 2012 (DB12). The
canonical version can be found at https://github.com/?????
This was adapted by Brian Bockelman for CMS and turned into a
glideinWMS validation script.
"""
import os
import sys
import random
import urllib
import multiprocessing
import traceback
from export_siteconf_info import get_glidein_config, add_condor_config_var
version = '0.1 DB12'
def singleDiracBenchmark( iterations = 1 ):
""" Get Normalized Power of one CPU in DIRAC Benchmark 2012 units (DB12)
"""
# This number of iterations corresponds to 1kHS2k.seconds, i.e. 250 HS06 seconds
n = int( 1000 * 1000 * 12.5 )
calib = 250.0
m = long( 0 )
m2 = long( 0 )
p = 0
p2 = 0
# Do one iteration extra to allow CPUs with variable speed (we ignore zeroth iteration)
for i in range( iterations + 1 ):
if i == 1:
start = os.times()
# Now the iterations
for _j in xrange( n ):
t = random.normalvariate( 10, 1 )
m += t
m2 += t * t
p += t
p2 += t * t
end = os.times()
cput = sum( end[:4] ) - sum( start[:4] )
wall = end[4] - start[4]
if not cput:
return None
# Return DIRAC-compatible values
return { 'CPU' : cput, 'WALL' : wall, 'NORM' : calib * iterations / wall, 'UNIT' : 'DB12' }
def getCpuModel():
with open("/proc/cpuinfo") as fp:
for line in fp:
if line.startswith("model name"):
return line.split(":", 1)[-1].strip()
return None
def singleDiracBenchmarkProcess( resultObject, iterations = 1 ):
""" Run singleDiracBenchmark() in a multiprocessing friendly way
"""
benchmarkResult = singleDiracBenchmark( iterations )
if not benchmarkResult or 'NORM' not in benchmarkResult:
return None
# This makes it easy to use with multiprocessing.Process
resultObject.value = benchmarkResult['NORM']
def multipleDiracBenchmark( instances = 1, iterations = 1 ):
""" Run multiple instances of the DIRAC Benchmark in parallel
"""
processes = []
results = []
# Set up all the subprocesses
for i in range( instances ):
results.append( multiprocessing.Value('d', 0.0) )
processes.append( multiprocessing.Process( target = singleDiracBenchmarkProcess, args = ( results[i], iterations ) ) )
# Start them all off at the same time
for p in processes:
p.start()
# Wait for them all to finish
for p in processes:
p.join()
raw = [ result.value for result in results ]
# Return the list of raw results, and the sum and mean of the list
return { 'raw' : raw, 'sum' : sum(raw), 'mean' : sum(raw)/len(raw) }
def wholenodeDiracBenchmark( instances = None, iterations = 1 ):
""" Run as many instances as needed to occupy the whole machine
"""
# Try $MACHINEFEATURES first if not given by caller
if not instances and 'MACHINEFEATURES' in os.environ:
try:
instances = int( urllib2.urlopen( os.environ['MACHINEFEATURES'] + '/total_cpu' ).read() )
except:
pass
# If not given by caller or $MACHINEFEATURES/total_cpu then just count CPUs
if not instances:
try:
instances = multiprocessing.cpu_count()
except:
instances = 1
return multipleDiracBenchmark( instances = instances, iterations = iterations )
def readAdValues(attrs, adname, castInt=False):
"""
A very simple parser for the ads available at runtime. Returns
a dictionary containing
- attrs: A list of string keys to look for.
- adname: Which ad to parse; "job" for the $_CONDOR_JOB_AD or
"machine" for $_CONDOR_MACHINE_AD
- castInt: Set to True to force the values to be integer literals.
Otherwise, this will return the values as a string representation
of the ClassAd expression.
Note this is not a ClassAd parser - will not handle new-style ads
or any expressions.
Will return a dictionary containing the key/value pairs that were
present in the ad and parseable.
On error, returns an empty dictionary.
"""
retval = {}
adfile = None
if adname == 'job':
adfile = os.environ.get("_CONDOR_JOB_AD")
elif adname == 'machine':
adfile = os.environ.get("_CONDOR_MACHINE_AD")
else:
print("Invalid ad name requested for parsing: %s" % adname)
return retval
if not adfile:
print("%s adfile is not set in environment." % adname)
return retval
attrs = [i.lower() for i in attrs]
try:
with open(adfile) as fd:
for line in fd:
info = line.strip().split("=", 1)
if len(info) != 2:
continue
attr = info[0].strip().lower()
if attr in attrs:
val = info[1].strip()
if castInt:
try:
retval[attr] = int(val)
except ValueError:
print("Error parsing %s's %s value: %s", (adname, attr, val))
else:
retval[attr] = val
except IOError:
print("Error opening %s ad:" % adname)
print(traceback.format_exc())
return {}
return retval
def jobslotDiracBenchmark( instances = None, iterations = 1 ):
""" Run as many instances as needed to occupy the job slot
"""
if not instances:
adValues = readAdValues(['cpus'], 'machine', castInt=True)
instances = adValues.setdefault('cpus', 1)
return multipleDiracBenchmark( instances = instances, iterations = iterations )
def main():
try:
glidein_config = get_glidein_config()
except:
glidein_config = {'GLIDEIN_CPUS': 0}
try:
model = getCpuModel()
except:
print "Failed to lookup CPU model"
traceback.print_exc()
model = None
try:
cpus = int(glidein_config['GLIDEIN_CPUS'])
except:
cpus = None
result = jobslotDiracBenchmark(instances=cpus)
print "Result from DIRAC benchmark:", result
if model:
print "Detected CPU model '%s'" % model
if not glidein_config or not cpus:
return
if 'mean' in result:
add_condor_config_var(glidein_config, name="DIRACBenchmark", kind="C", value=str(result['mean']))
add_condor_config_var(glidein_config, name="CPUModel", kind="S", value=str(model))
#
# If we run as a command
#
if __name__ == "__main__":
try:
main()
except:
# Always succeed - this is advisory info.
traceback.print_exc()
sys.exit(0)