forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.tcl
executable file
·543 lines (485 loc) · 14.7 KB
/
regression.tcl
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# OpenSTA, Static Timing Analyzer
# Copyright (c) 2024, Parallax Software, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# regression -help | [-threads threads] [-valgrind] [-report_stats] test1 [test2...]
proc regression_main {} {
setup
parse_args
run_tests
show_summary
exit [found_errors]
}
proc setup {} {
global result_dir diff_file failure_file errors
global use_valgrind valgrind_shared_lib_failure
global report_stats
set use_valgrind 0
set report_stats 0
if { !([file exists $result_dir] && [file isdirectory $result_dir]) } {
file mkdir $result_dir
}
file delete $diff_file
file delete $failure_file
set errors(error) 0
set errors(memory) 0
set errors(leak) 0
set errors(fail) 0
set errors(no_cmd) 0
set errors(no_ok) 0
set valgrind_shared_lib_failure 0
}
proc parse_args {} {
global argv app_options tests test_groups cmd_paths
global use_valgrind
global result_dir tests
global report_stats
while { $argv != {} } {
set arg [lindex $argv 0]
if { $arg == "help" || $arg == "-help" } {
puts {Usage: regression [-help] [-threads threads] [-valgrind] [-report_stats] tests...}
puts " -threads max|integer - number of threads to use"
puts " -valgrind - run valgrind (linux memory checker)"
puts " -report_stats - report run time and memory"
puts " Wildcarding for test names is supported (enclose in \"'s)"
puts " Tests are: all, fast, med, slow, or a test group or test name"
puts ""
puts " If 'limit coredumpsize unlimited' corefiles are saved in $result_dir/test.core"
exit
} elseif { $arg == "-threads" } {
set threads [lindex $argv 1]
if { !([string is integer $threads] || $threads == "max") } {
puts "Error: -threads arg $threads is not an integer or max."
exit 0
}
lappend app_options "-threads"
lappend app_options $threads
set argv [lrange $argv 2 end]
} elseif { $arg == "-valgrind" } {
set use_valgrind 1
set argv [lrange $argv 1 end]
} elseif { $arg == "-report_stats" } {
set report_stats 1
set argv [lrange $argv 1 end]
} else {
break
}
}
if { $argv == {} } {
# Default is to run fast tests.
set tests [group_tests fast]
} else {
set tests [expand_tests $argv]
}
}
proc expand_tests { argv } {
global test_groups errors
set tests {}
foreach arg $argv {
if { [info exists test_groups($arg)] } {
set tests [concat $tests $test_groups($arg)]
} elseif { [string first "*" $arg] != -1 \
|| [string first "?" $arg] != -1 } {
# Find wildcard matches.
foreach test [group_tests "all"] {
if [string match $arg $test] {
lappend tests $test
}
}
} elseif { [lsearch [group_tests "all"] $arg] != -1 } {
lappend tests $arg
} else {
puts "Error: test $arg not found."
incr errors(no_cmd)
}
}
return $tests
}
proc run_tests {} {
global tests errors app_path
foreach test $tests {
run_test $test
}
# Macos debug info generated by valgrind.
file delete -force "$app_path.dSYM"
}
proc run_test { test } {
global result_dir diff_file errors diff_options report_stats
set cmd_file [test_cmd_file $test]
if [file exists $cmd_file] {
set ok_file [test_ok_file $test]
set log_file [test_log_file $test]
foreach file [glob -nocomplain [file join $result_dir $test.*]] {
file delete -force $file
}
puts -nonewline $test
flush stdout
set test_errors [run_test_app $test $cmd_file $log_file]
if { [lindex $test_errors 0] == "ERROR" } {
puts " *ERROR* [lrange $test_errors 1 end]"
append_failure $test
incr errors(error)
# For some reason seg faults aren't echoed in the log - add them.
if { [llength $test_errors] > 1 && [file exists $log_file] } {
set log_ch [open $log_file "a"]
puts $log_ch $test_errors
close $log_ch
}
# Report partial log diff anyway.
if [file exists $ok_file] {
catch [concat exec diff $diff_options $ok_file $log_file \
>> $diff_file]
}
} else {
set error_msg ""
if { [lsearch $test_errors "MEMORY"] != -1 } {
append error_msg " *MEMORY*"
append_failure $test
incr errors(memory)
}
if { [lsearch $test_errors "LEAK"] != -1 } {
append error_msg " *LEAK*"
append_failure $test
incr errors(leak)
}
if { $report_stats } {
append error_msg " [test_stats_summary $test]"
}
if [file exists $ok_file] {
# Filter dos '/r's from log file.
set tmp_file [file join $result_dir $test.tmp]
exec tr -d "\r" < $log_file > $tmp_file
file rename -force $tmp_file $log_file
if [catch [concat exec diff $diff_options $ok_file $log_file \
>> $diff_file]] {
puts " *FAIL*$error_msg"
append_failure $test
incr errors(fail)
} else {
puts " pass$error_msg"
}
} else {
puts " *NO OK FILE*"
append_failure $test
incr errors(no_ok)
}
}
} else {
puts "$test *NO CMD FILE*"
incr errors(no_cmd)
}
}
proc test_stats { test } {
if { ![catch {open [test_stats_file $test] r} stream] } {
gets $stream line1
close $stream
return $line1
} else {
return {}
}
}
proc test_stats_summary { test } {
set stats [test_stats $test]
set elapsed_time [lindex $stats 0]
set user_time [lindex $stats 1]
set memory [lindex $stats 2]
if { [string is double $elapsed_time] } {
set elapsed [format "%.1fe" $elapsed_time]
} else {
set elapsed "?"
}
if { [string is double $user_time] } {
set user [format "%.1fu" $user_time]
} else {
set user "?"
}
if { [string is double $memory] } {
set mem [format "%.0fmb" [expr $memory * 1e-6]]
} else {
set mem "?"
}
return "$elapsed $user $mem"
}
proc append_failure { test } {
global failure_file
set fail_ch [open $failure_file "a"]
puts $fail_ch $test
close $fail_ch
}
# Return error.
proc run_test_app { test cmd_file log_file } {
global app_path errorCode use_valgrind
if { $use_valgrind } {
return [run_test_valgrind $test $cmd_file $log_file]
} else {
return [run_test_plain $test $cmd_file $log_file]
}
}
proc run_test_plain { test cmd_file log_file } {
global app_path app_options result_dir errorCode
global report_stats
global test_expect_eror
if { ![file exists $app_path] } {
return "ERROR $app_path not found."
} elseif { ![file executable $app_path] } {
return "ERROR $app_path is not executable."
} else {
set run_file [test_run_file $test]
set run_stream [open $run_file "w"]
puts $run_stream "cd [file dirname $cmd_file]"
puts $run_stream "source [file tail $cmd_file]"
if { $report_stats } {
set stat_file [file normalize [test_stats_file $test]]
puts $run_stream "sta::write_stats $stat_file"
}
close $run_stream
if { [catch [concat exec $app_path $app_options $run_file >& $log_file]] \
&& ![info exists test_expect_eror($test)] } {
set signal [lindex $errorCode 2]
set error [lindex $errorCode 3]
# Error strings are not consistent across platforms but signal
# names are.
if { $signal == "SIGSEGV" } {
# Save corefiles to regression results directory.
set pid [lindex $errorCode 1]
set sys_corefile [test_sys_core_file $test $pid]
if { [file exists $sys_corefile] } {
file copy $sys_corefile [test_core_file $test]
}
}
cleanse_logfile $test $log_file
return "ERROR $error"
}
cleanse_logfile $test $log_file
return ""
}
}
proc run_test_valgrind { test cmd_file log_file } {
global app_path app_options valgrind_options result_dir errorCode
set vg_cmd_file [test_valgrind_cmd_file $test]
set vg_stream [open $vg_cmd_file "w"]
puts $vg_stream "cd [file dirname $cmd_file]"
puts $vg_stream "source [file tail $cmd_file]"
puts $vg_stream "sta::delete_all_memory"
close $vg_stream
set cmd [concat exec valgrind $valgrind_options \
$app_path $app_options $vg_cmd_file >& $log_file]
set error_msg ""
if { [catch $cmd] } {
set error_msg "ERROR [lindex $errorCode 3]"
}
file delete $vg_cmd_file
cleanse_logfile $test $log_file
lappend error_msg [cleanse_valgrind_logfile $test $log_file]
return $error_msg
}
# Error messages can be found in "valgrind/memcheck/mc_errcontext.c".
#
# "Conditional jump or move depends on uninitialised value(s)"
# "%s contains unaddressable byte(s)"
# "%s contains uninitialised or unaddressable byte(s)"
# "Use of uninitialised value of size %d"
# "Invalid read of size %d"
# "Syscall param %s contains uninitialised or unaddressable byte(s)"
# "Unaddressable byte(s) found during client check request"
# "Uninitialised or unaddressable byte(s) found during client check request"
# "Invalid free() / delete / delete[]"
# "Mismatched free() / delete / delete []"
set valgrind_mem_regexp "(depends on uninitialised value)|(contains unaddressable)|(contains uninitialised)|(Use of uninitialised value)|(Invalid read)|(Unaddressable byte)|(Uninitialised or unaddressable)|(Invalid free)|(Mismatched free)"
# "%d bytes in %d blocks are definitely lost in loss record %d of %d"
# "%d bytes in %d blocks are possibly lost in loss record %d of %d"
#set valgrind_leak_regexp "blocks are (possibly|definitely) lost"
set valgrind_leak_regexp "blocks are definitely lost"
# Valgrind fails on executables using shared libraries.
set valgrind_shared_lib_failure_regexp "No malloc'd blocks -- no leaks are possible"
# Scan the log file to separate valgrind notifications and check for
# valgrind errors.
proc cleanse_valgrind_logfile { test log_file } {
global valgrind_mem_regexp valgrind_leak_regexp
global valgrind_shared_lib_failure_regexp
global valgrind_shared_lib_failure
set tmp_file [test_tmp_file $test]
set valgrind_log_file [test_valgrind_file $test]
file copy -force $log_file $tmp_file
set tmp [open $tmp_file "r"]
set log [open $log_file "w"]
set valgrind [open $valgrind_log_file "w"]
set leaks 0
set mem_errors 0
gets $tmp line
while { ![eof $tmp] } {
if {[regexp "^==" $line]} {
puts $valgrind $line
if {[regexp $valgrind_leak_regexp $line]} {
set leaks 1
}
if {[regexp $valgrind_mem_regexp $line]} {
set mem_errors 1
}
if {[regexp $valgrind_shared_lib_failure_regexp $line]} {
set valgrind_shared_lib_failure 1
}
} elseif {[regexp {^--[0-9]+} $line]} {
# Valgrind notification line.
} else {
puts $log $line
}
gets $tmp line
}
close $log
close $tmp
close $valgrind
file delete $tmp_file
set errors {}
if { $mem_errors } {
lappend errors "MEMORY"
}
if { $leaks } {
lappend errors "LEAK"
}
return $errors
}
################################################################
proc show_summary {} {
global errors tests diff_file result_dir valgrind_shared_lib_failure
global app_path app
puts "------------------------------------------------------"
set test_count [llength $tests]
if { [found_errors] } {
if { $errors(error) != 0 } {
puts "Errored $errors(error)/$test_count"
}
if { $errors(fail) != 0 } {
puts "Failed $errors(fail)/$test_count"
}
if { $errors(leak) != 0 } {
puts "Memory leaks in $errors(leak)/$test_count"
}
if { $errors(memory) != 0 } {
puts "Memory corruption in $errors(memory)/$test_count"
}
if { $errors(no_ok) != 0 } {
puts "No ok file for $errors(no_ok)/$test_count"
}
if { $errors(no_cmd) != 0 } {
puts "No cmd tcl file for $errors(no_cmd)/$test_count"
}
if { $errors(fail) != 0 } {
puts "See $diff_file for differences"
}
} else {
puts "Passed $test_count"
}
if { $valgrind_shared_lib_failure } {
puts "WARNING: valgrind failed because the executable is not statically linked."
}
puts "See $result_dir for log files"
}
proc found_errors {} {
global errors
return [expr $errors(error) != 0 || $errors(fail) != 0 \
|| $errors(no_cmd) != 0 || $errors(no_ok) != 0 \
|| $errors(memory) != 0 || $errors(leak) != 0]
}
################################################################
proc save_ok_main {} {
global argv
if { $argv == "help" || $argv == "-help" } {
puts {Usage: save_ok [failures] test1 [test2]...}
} elseif { $argv == "failures" } {
global failure_file
if [file exists $failure_file] {
set fail_ch [open $failure_file "r"]
while { ! [eof $fail_ch] } {
set test [gets $fail_ch]
if { $test != "" } {
save_ok $test
}
}
close $fail_ch
}
} else {
foreach test $argv {
save_ok $test
}
}
}
proc save_ok { test } {
if { [lsearch [group_tests "all"] $test] == -1 } {
puts "Error: test $test not found."
} else {
set ok_file [test_ok_file $test]
set log_file [test_log_file $test]
if { ! [file exists $log_file] } {
puts "Error: log file $log_file not found."
} else {
file copy -force $log_file $ok_file
}
}
}
################################################################
proc test_cmd_dir { test } {
global cmd_dirs
if {[info exists cmd_dirs($test)]} {
return $cmd_dirs($test)
} else {
return ""
}
}
proc test_cmd_file { test } {
return [file join [test_cmd_dir $test] "$test.tcl"]
}
proc test_ok_file { test } {
global test_dir
return [file join $test_dir "$test.ok"]
}
proc test_log_file { test } {
global result_dir
return [file join $result_dir "$test.log"]
}
proc test_run_file { test } {
global result_dir
return [file join $result_dir $test.run]
}
proc test_tmp_file { test } {
global result_dir
return [file join $result_dir $test.tmp]
}
proc test_valgrind_cmd_file { test } {
global result_dir
return [file join $result_dir $test.vg_cmd]
}
proc test_valgrind_file { test } {
global result_dir
return [file join $result_dir $test.valgrind]
}
proc test_stats_file { test } {
global result_dir
return [file join $result_dir "$test.stats"]
}
proc test_core_file { test } {
global result_dir
return [file join $result_dir $test.core]
}
proc test_sys_core_file { test pid } {
global cmd_dirs
# macos
# return [file join "/cores" "core.$pid"]
# Suse
return [file join [test_cmd_dir $test] "core"]
}
################################################################
# Local Variables:
# mode:tcl
# End: