-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun_regression.py
More file actions
executable file
·438 lines (359 loc) · 14.9 KB
/
Run_regression.py
File metadata and controls
executable file
·438 lines (359 loc) · 14.9 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/python3
# Copyright (c) 2018-2019 Bluespec, Inc.
# See LICENSE for license details
import sys
import os
import stat
import subprocess
import argparse
import multiprocessing
# ================================================================
# DEBUGGING ONLY: This exclude list allows skipping some specific test
exclude_list = []
n_workers_max = 4
# ================================================================
def main (argv = None):
# Command-line parsing
parser = argparse.ArgumentParser (
formatter_class=argparse.RawDescriptionHelpFormatter
, description='Runs a regression of the ISA tests'
, epilog = """
The following run-time directories and logs are created:
isa-logs/fail: Contains simulation logs of all failing tests
isa-logs/pass: Contains simulation logs of all passing tests
worker*: temporary simulation directories for each worker process\n"""
)
parser.add_argument (
'--simulator'
, action="store"
, dest='sim_path'
, help='The simulator executable'
, default="simulator.exe"
)
parser.add_argument (
'--tools'
, action="store"
, dest='tool_path'
, help='Path to tools like elf_to_hex'
, default="."
)
parser.add_argument (
'--tests'
, action="store"
, dest='test_path'
, help="Path to the ISA tests elf files"
, default="."
)
parser.add_argument (
'--logs'
, action="store"
, dest='logs_path'
, help="Where should the logs be dumped?"
, default="."
)
parser.add_argument (
'--arch'
, action="store"
, dest='arch_string'
, help="RV architecture string and implied test families"
, default="rv32i"
)
parser.add_argument (
'--verbosity'
, type=int
, action="store"
, dest='verbosity'
, help="""Verbosity of simulation output
(0: quiet, 1: instruction trace, 2: detailed output)"""
, default=0
)
parser.add_argument (
'--num-jobs'
, type=int
, action="store"
, dest='workers'
, help="Number of concurrent workers"
, default=int (multiprocessing.cpu_count () // 3)
)
args = parser.parse_args()
# Simulation executable
args_dict = {'sim_path' : os.path.abspath (os.path.normpath (args.sim_path))}
args_dict ['tool_path'] = os.path.abspath (os.path.normpath (args.tool_path))
args_dict ['elfs_path'] = os.path.abspath (os.path.normpath (args.test_path))
args_dict ['logs_path'] = os.path.abspath (os.path.normpath (args.logs_path))
pass_path = os.path.join (args_dict['logs_path'], 'pass')
fail_path = os.path.join (args_dict['logs_path'], 'fail')
for path in (args_dict['logs_path'], pass_path, fail_path) :
if not os.path.isdir (path) :
print ("Creating dir: " + path)
os.mkdir (path)
# Architecture string and implied ISA test families
args_dict ['arch_string'] = extract_arch_string (args.arch_string)
test_families = select_test_families (args_dict['arch_string'])
print ("Testing the following families of ISA tests")
for tf in test_families:
print (" " + tf)
args_dict ['test_families'] = test_families
args_dict ['verbosity'] = args.verbosity
n_workers = args.workers
sys.stdout.write ("Using {0} worker processes\n".format (n_workers))
# End of command-line arg processing
# ================================================================
# elf_to_hex executable
elf_to_hex_exe = os.path.join (args_dict['tool_path'], "elf_to_hex")
if (not os.path.exists (elf_to_hex_exe)):
sys.stderr.write ("ERROR: elf_to_hex executable does not exist?\n")
sys.stderr.write (" at {0}\n".format (elf_to_hex_exe))
sys.stdout.write ("\n")
return 1
args_dict ['elf_to_hex_exe'] = elf_to_hex_exe
sys.stdout.write ("Parameters:\n")
for key in iter (args_dict):
sys.stdout.write (" {0:<16}: {1}\n".format (key, args_dict [key]))
def fn_filter_regular_file (level, filename):
(dirname, basename) = os.path.split (filename)
# Ignore filename if has any extension (heuristic that it's not an ELF file)
if "." in basename: return False
# TEMPORARY FILTER WHILE DEBUGGING:
if basename in exclude_list:
sys.stdout.write ("WARNING: TEMPORARY FILTER IN EFFECT; REMOVE AFTER DEBUGGING\n")
sys.stdout.write (" This test is in exclude_list: {0}\n".format (basename))
return False
# Ignore filename if does not match test_families
for x in args_dict ['test_families']:
if basename.find (x) != -1: return True
return False
def fn_filter_dir (level, filename):
return True
# Traverse the elfs_path and collect filenames of relevant isa tests
filenames = traverse (fn_filter_dir, fn_filter_regular_file, 0, args_dict['elfs_path'])
n_tests = len (filenames)
# Print the filenames of the elf tests
try : test_list = open ('isa-tests.lst', 'w')
except :
logging.error ("Unable to open file 'isa-tests.lst' for writing")
sys.exit (1)
for f in filenames : test_list.write ('%s\n' % f)
test_list.close ()
sys.stdout.write ("{0} relevant isa tests found under {1}\n".format (n_tests, args_dict['elfs_path']))
if n_tests == 0: return 0
args_dict ['filenames'] = filenames
args_dict ['n_tests'] = n_tests
# Create a shared counter to index into the list of filenames
index = multiprocessing.Value ('L', 0) # Unsigned long (4 bytes)
args_dict ['index'] = index
# Create a shared array for each worker's (n_executed, n_passed) results
results = multiprocessing.Array ('L', [ 0 for j in range (2 * n_workers) ])
args_dict ['results'] = results
# Create n workers
sys.stdout.write ("Creating {0} workers (sub-processes)\n".format (n_workers))
workers = [multiprocessing.Process (target = do_worker,
args = (w, args_dict))
for w in range (n_workers)]
# Start the workers
for worker in workers: worker.start ()
# Wait for all workers to finish
for worker in workers: worker.join ()
# Collect all results
num_executed = 0
num_passed = 0
with results.get_lock ():
for w in range (n_workers):
n_e = results [2 * w]
n_p = results [2 * w + 1]
sys.stdout.write ("Worker {0} executed {1} tests, of which {2} passed\n"
.format (w, n_e, n_p))
num_executed = num_executed + n_e
num_passed = num_passed + n_p
# Write final statistics
sys.stdout.write ("Total tests: {0} tests\n".format (n_tests))
sys.stdout.write ("Executed: {0} tests\n".format (num_executed))
sys.stdout.write ("PASS: {0} tests\n".format (num_passed))
sys.stdout.write ("FAIL: {0} tests\n".format (num_executed - num_passed))
# Set nonzero exit code unless all tests passed
return 0
# ================================================================
# Extract the architecture string (e.g., RV64AIMSU) from the string s
def extract_arch_string (s):
s1 = s.upper()
j_rv32 = s1.find ("RV32")
j_rv64 = s1.find ("RV64")
if (j_rv32 >= 0):
j = j_rv32
elif (j_rv64 >= 0):
j = j_rv64
else:
sys.stderr.write ("ERROR: cannot find architecture string beginning with RV32 or RV64 in: \n")
sys.stderr.write (" '" + s + "'\n")
sys.exit (1)
k = j + 4
rv = s1 [j:k]
extns = ""
while (k < len (s)):
ch = s [k]
if (ch < "A") or (ch > "Z"): break
extns = extns + s [k]
k = k + 1
arch = rv + extns
return arch
# ================================================================
# Select ISA test families based on provided arch string
def select_test_families (arch):
arch = arch.lower ()
families = []
if arch.find ("32") != -1:
rv = 32
families = ["rv32ui-p", "rv32mi-p"]
else:
rv = 64
families = ["rv64ui-p", "rv64mi-p"]
if (arch.find ("s") != -1):
s = True
if rv == 32:
families.extend (["rv32ui-v", "rv32si-p"])
else:
families.extend (["rv64ui-v", "rv64si-p"])
else:
s = False
def add_family (extension):
if (arch.find (extension) != -1):
if rv == 32:
families.append ("rv32u" + extension + "-p")
if s:
families.append ("rv32u" + extension + "-v")
else:
families.append ("rv64u" + extension + "-p")
if s:
families.append ("rv64u" + extension + "-v")
add_family ("m")
add_family ("a")
add_family ("f")
add_family ("d")
add_family ("c")
return families
# ================================================================
# Recursively traverse the dir tree below path and collect filenames
# that pass the given filter functions
def traverse (fn_filter_dir, fn_filter_regular_file, level, path):
st = os.stat (path)
is_dir = stat.S_ISDIR (st.st_mode)
is_regular = stat.S_ISREG (st.st_mode)
if is_dir and fn_filter_dir (level, path):
files = []
for entry in os.listdir (path):
path1 = os.path.join (path, entry)
files.extend (traverse (fn_filter_dir, fn_filter_regular_file, level + 1, path1))
return files
elif is_regular and fn_filter_regular_file (level, path):
return [path]
else:
return []
# ================================================================
# For each ELF file, execute it in the RISC-V simulator
def do_worker (worker_num, args_dict):
tmpdir = "./worker_" + "{0}".format (worker_num)
if not os.path.exists (tmpdir):
os.mkdir (tmpdir)
elif not os.path.isdir (tmpdir):
sys.stdout.write ("ERROR: Worker {0}: {1} exists but is not a dir".format (worker_num, tmpdir))
return
os.chdir (tmpdir)
sys.stdout.write ("Worker {0} using dir: {1}\n".format (worker_num, tmpdir))
n_tests = args_dict ['n_tests']
filenames = args_dict ['filenames']
index = args_dict ['index']
results = args_dict ['results']
num_executed = 0
num_passed = 0
while True:
# Get a unique index into the filenames, and get the filename
with index.get_lock():
my_index = index.value
index.value = my_index + 1
if my_index >= n_tests:
# All done
with results.get_lock():
results [2 * worker_num] = num_executed
results [2 * worker_num + 1] = num_passed
return
filename = filenames [my_index]
(message, passed) = do_isa_test (worker_num, args_dict, filename)
num_executed = num_executed + 1
if passed:
num_passed = num_passed + 1
pass_fail = "PASS"
else:
pass_fail = "FAIL"
message = message + ("Worker {0}: Test: {1} {2} [So far: total {3}, executed {4}, PASS {5}, FAIL {6}]\n"
.format (worker_num,
os.path.basename (filename),
pass_fail,
n_tests,
num_executed,
num_passed,
num_executed - num_passed))
sys.stdout.write (message)
# ================================================================
# For each ELF file, execute it in the RISC-V simulator
def do_isa_test (worker_no, args_dict, full_filename):
message = ""
(dirname, basename) = os.path.split (full_filename)
# Construct the commands for sub-process execution
command1 = [args_dict ['elf_to_hex_exe'], full_filename, "Mem.hex"]
command2 = [args_dict ['sim_path'], "+tohost", "+jtag_port=666{0}".format(worker_no)]
command2.append ("+vpi_port=777{0}".format(worker_no))
if (args_dict ['verbosity'] == 1): command2.append ("+v1")
elif (args_dict ['verbosity'] == 2): command2.append ("+v2")
message = message + " Exec:"
for x in command1:
message = message + (" {0}".format (x))
message = message + "\n"
message = message + (" Exec:")
for x in command2:
message = message + (" {0}".format (x))
message = message + ("\n")
# Run command as a sub-process
completed_process1 = run_command (command1)
completed_process2 = run_command (command2)
passed = completed_process2.stdout.find ("PASS") != -1
# Save stdouts in log file
log_filename = os.path.join (
args_dict ['logs_path'], "pass" if passed else "fail", basename + ".log")
message = message + (" Writing log: {0}\n".format (log_filename))
fd = open (log_filename, 'w')
fd.write (completed_process1.stdout)
fd.write (completed_process2.stdout)
fd.close ()
# If Tandem Verification trace file was created, save it as well
if os.path.exists ("./trace_out.dat"):
trace_filename = log_filename.rsplit('.', 1)[0] + ".trace_data"
os.rename ("./trace_out.dat", trace_filename)
message = message + (" Trace output saved in: {0}\n".format (trace_filename))
return (message, passed)
# ================================================================
# This is a wrapper around 'subprocess.run' because of an annoying
# incompatible change in moving from Python 3.5 to 3.6
def run_command (command):
python_minor_version = sys.version_info [1]
if python_minor_version < 6:
# Python 3.5 and earlier
result = subprocess.run (args = command,
timeout = 30,
bufsize = 0,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
universal_newlines = True)
else:
# Python 3.6 and later
result = subprocess.run (args = command,
timeout = 30,
bufsize = 0,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
encoding='utf-8')
return result
# ================================================================
# For non-interactive invocations, call main() and use its return value
# as the exit code.
if __name__ == '__main__':
sys.exit (main (sys.argv))