interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / testrunner.tcl

2299 lines · 67574 bytes Raw Blame History
1#!/bin/sh
2# Script to runs tests for SQLite. Run with option "help" for more info. \
3exec tclsh "$0" "$@"
4
5set dir [pwd]
6set testdir [file normalize [file dirname $argv0]]
7set saved $argv
8set argv [list]
9source [file join $testdir testrunner_data.tcl]
10
11# Estimated amount of work required by displaytype, relative to 'tcl'
12#
13set estwork(tcl) 1
14set estwork(fuzz) 22
15set estwork(bld) 66
16set estwork(make) 102
17
18set estworkfile [file join $testdir testrunner_estwork.tcl]
19if {[file readable $estworkfile]} {
20 source $estworkfile
21}
22source [file join $testdir permutations.test]
23set argv $saved
24cd $dir
25
26# This script requires an interpreter that supports [package require sqlite3]
27# to run. If this is not such an intepreter, see if there is a [testfixture]
28# in the current directory. If so, run the command using it. If not,
29# recommend that the user build one.
30#
31proc find_interpreter {} {
32 global dir
33 set interpreter [file tail [info nameofexec]]
34 set rc [catch { package require sqlite3 }]
35 if {$rc} {
36 if {[file readable pkgIndex.tcl] && [catch {source pkgIndex.tcl}]==0} {
37 # Since $dir is defined to be the [pwd], if the "source pkgIndex.tcl"
38 # worked, that should have enabled us to use the locally built
39 # copy of the TCL extension.
40 set rc [catch { package require sqlite3 }]
41 if {!$rc} {
42 puts "Using the locally built copy of the \"sqlite3\" tcl extension"
43 } else {
44 puts "The locally built copy of the \"sqlite3\" tcl extension does work..."
45 }
46 }
47 }
48 if {$rc} {
49 if { [string match -nocase testfixture* $interpreter]==0
50 && [file executable ./testfixture]
51 } {
52 puts "Failed to find tcl package sqlite3. Restarting with ./testfixture.."
53 set status [catch {
54 exec [trd_get_bin_name testfixture] [info script] {*}$::argv >@ stdout
55 } msg]
56 exit $status
57 }
58 }
59 if {$rc} {
60 puts "Cannot find tcl package \"sqlite3\": Trying to build it now..."
61 if {$::tcl_platform(platform) eq "windows"} {
62 set bat [open make-tcl-extension.bat w]
63 puts $bat "nmake /f Makefile.msc tclextension"
64 close $bat
65 catch {exec -ignorestderr -- make-tcl-extension.bat}
66 } else {
67 catch {exec make tclextension}
68 }
69 if {[file readable pkgIndex.tcl] && [catch {source pkgIndex.tcl}]==0} {
70 set rc [catch { package require sqlite3 }]
71 }
72 if {$rc==0} {
73 puts "The SQLite tcl extension was successfully built and loaded."
74 puts "Run \"make tclextension-install\" to avoid having to rebuild\
75 it in the future."
76 } else {
77 puts "Unable to build the SQLite tcl extension"
78 }
79 }
80 if {$rc} {
81 puts stderr "Cannot find a working instance of the SQLite tcl extension."
82 puts stderr "Run \"make tclextension\" or \"make testfixture\" and\
83 try again..."
84 exit 1
85 }
86}
87find_interpreter
88
89# Usually this script is run by [testfixture]. But it can also be run
90# by a regular [tclsh]. For these cases, emulate the [clock_milliseconds]
91# command.
92if {[info commands clock_milliseconds]==""} {
93 proc clock_milliseconds {} {
94 clock milliseconds
95 }
96}
97
98# Ensure DEBUGINFOD_URLS is unset. Leaving it set causes some sanitizer
99# test cases to run slowly on Ubuntu.
100#
101unset -nocomplain ::env(DEBUGINFOD_URLS)
102
103#-------------------------------------------------------------------------
104# Usage:
105#
106proc usage {} {
107 set a0 [file tail $::argv0]
108
109 puts [string trim [subst -nocommands {
110Usage:
111 $a0 ?SWITCHES? ?PERMUTATION? ?PATTERNS?
112 $a0 PERMUTATION FILE
113 $a0 clean
114 $a0 errors ?-v|--verbose? ?-s|--summary? ?PATTERN?
115 $a0 estwork
116 $a0 halt
117 $a0 help
118 $a0 joblist ?PATTERN?
119 $a0 njob ?NJOB?
120 $a0 retest
121 $a0 make CONFIG TARGETS
122 $a0 script ?-msvc? CONFIG
123 $a0 status ?-d SECS? ?--cls?
124
125 where SWITCHES are:
126 --buildonly Build test exes but do not run tests
127 --cases DISPLAYNAME Only run test that match DISPLAYNAME
128 --config CONFIGS Only use configs on comma-separate list CONFIGS
129 --dryrun Write what would have happened to testrunner.log
130 --explain Write summary to stdout
131 --fuzzdb FILENAME Additional external fuzzcheck database
132 --jobs NUM Run tests using NUM separate processes
133 --omit CONFIGS Omit configs on comma-separated list CONFIGS
134 --status Show the full "status" report while running
135 --stop-on-coredump Stop running if any test segfaults
136 --stop-on-error Stop running after any reported error
137 --zipvfs ZIPVFSDIR ZIPVFS source directory
138
139Special values for PERMUTATION include:
140
141 list - show allowed PERMUTATION arguments.
142 mdevtest - tests recommended prior to normal development check-ins.
143 devtest - alias for "mdevtest"
144 release - full release test with various builds.
145 c - tests in test/c directory only.
146 sdevtest - like mdevtest but using ASAN and UBSAN.
147 all - all tcl test scripts, plus a subset of test scripts rerun
148 with various permutations.
149 full - all tcl test scripts.
150 veryquick - a fast subset of the tcl test scripts. This is the default.
151
152The interpreter that runs this script can be an ordinary "tclsh" as long
153as "package require sqlite3" works, or it can be "testfixture".
154
155If no PATTERN arguments are present, all tests specified by the PERMUTATION
156are run. Otherwise, each pattern is interpreted as a glob pattern. Only
157those tcl tests for which the final component of the filename matches at
158least one specified pattern are run. The glob wildcard '*' is prepended
159to the pattern if it does not start with '^' and appended to every
160pattern that does not end with '$'. If any PATTERN argument begins with "~",
161then it is an anti-pattern. When PATTERN arguments are present, tests are
162only run if they match one or more patterns and match no anti-patterns.
163
164If a PERMUTATION is specified and is followed by the path to a Tcl script
165instead of a list of patterns, then that single Tcl test script is run
166with the specified permutation.
167
168The "status" and "njob" commands are designed to be run from the same
169directory as a running testrunner.tcl script. The "status" command prints
170a report describing the current state and progress of the tests. Use
171the "-d N" option to have the status display clear the screen and repeat
172every N seconds. The "njob" command may be used to query or modify the
173number of sub-processes the test script uses to run tests.
174
175The "halt" command modifies the database so that all tasks are marked
176as complete. Testing will halt when all tests currently running complete.
177
178The "clean" command removes files and directories created by a prior
179invocation of testrunner.tcl.
180
181The "make" command runs "make" configured for the specific CONFIG.
182
183The "script" command outputs the script used to build a configuration.
184Add the "-msvc" option for a Windows-compatible script. For a list of
185available configurations enter "$a0 script help".
186
187The "errors" commands shows the output of tests that failed in the
188most recent run. Complete output is shown if the -v or --verbose options
189are used. Otherwise, an attempt is made to minimize the output to show
190only the parts that contain the error messages. The --summary option just
191shows the jobs that failed. If PATTERN are provided, the error information
192is only provided for jobs that match PATTERN.
193
194The "retest" command reruns tests that failed or were never completed
195by the previous invocation of testrunner.tcl.
196
197Full documentation here: https://sqlite.org/src/doc/trunk/doc/testrunner.md
198 }]]
199
200 exit 1
201}
202#-------------------------------------------------------------------------
203
204#-------------------------------------------------------------------------
205# Try to estimate a the number of processes to use.
206#
207# Command [guess_number_of_cores] attempts to glean the number of logical
208# cores. Command [default_njob] returns the default value for the --jobs
209# switch.
210#
211proc guess_number_of_cores {} {
212 if {[catch {number_of_cores} ret]} {
213 set ret 4
214 if {$::tcl_platform(platform) eq "windows"} {
215 catch { set ret $::env(NUMBER_OF_PROCESSORS) }
216 } else {
217 if {$::tcl_platform(os)=="Darwin"} {
218 set cmd "sysctl -n hw.logicalcpu"
219 } else {
220 set cmd "nproc"
221 }
222 catch {
223 set fd [open "|$cmd" r]
224 set ret [gets $fd]
225 close $fd
226 set ret [expr $ret]
227 }
228 }
229 }
230 return $ret
231}
232
233proc default_njob {} {
234 global env
235 if {[info exists env(NJOB)] && $env(NJOB)>=1} {
236 return $env(NJOB)
237 }
238 set nCore [guess_number_of_cores]
239 if {$nCore<=2} {
240 set nHelper 1
241 } else {
242 set nHelper [expr int($nCore*0.8)]
243 if {$nHelper>20} {set nHelper 20}
244 }
245 return $nHelper
246}
247
248# Return true if the current test is using Fil-C. False otherwise.
249#
250proc is_fil_cc {} {
251 set ret 0
252 if {[catch { exec $::env(CC) --version } msg]==0
253 && [string first Fil-C $msg]>=0
254 } {
255 set ret 1
256 }
257 return $ret
258}
259
260# Return the default value for the -nbuild switch. This value limits the
261# number of build jobs that may be run concurrently. Usually there is
262# no special limit for this, but with Fil-C the compiler has been observed
263# using up to 8GB of memory to build sqlite3.c, so in this case the default
264# limit is two concurrent builds.
265#
266proc default_nbuild {} {
267 set ret -1
268 if {$::TRG(fil-cc)} {
269 set ret 2
270 }
271 return $ret
272}
273
274#-------------------------------------------------------------------------
275
276#-------------------------------------------------------------------------
277# Setup various default values in the global TRG() array.
278#
279set TRG(dbname) [file normalize testrunner.db]
280set TRG(logname) [file normalize testrunner.log]
281set TRG(build.logname) [file normalize testrunner_build.log]
282set TRG(info_script) [file normalize [info script]]
283set TRG(timeout) 10000 ;# Default busy-timeout for testrunner.db
284set TRG(fil-cc) [is_fil_cc] ;# True for fil-cc on Linux */
285set TRG(nJob) [default_njob] ;# Default number of helper processes
286set TRG(nBuild) [default_nbuild] ;# Default number of max build processes
287set TRG(patternlist) [list]
288set TRG(cmdline) $argv
289set TRG(reporttime) 2000
290set TRG(fuzztest) 0 ;# is the fuzztest option present.
291set TRG(zipvfs) "" ;# -zipvfs option, if any
292set TRG(buildonly) 0 ;# True if --buildonly option
293set TRG(config) {} ;# Only build the named configurations
294set TRG(omitconfig) {} ;# Do not build these configurations
295set TRG(dryrun) 0 ;# True if --dryrun option
296set TRG(explain) 0 ;# True for the --explain option
297set TRG(stopOnError) 0 ;# Stop running at first failure
298set TRG(stopOnCore) 0 ;# Stop on a core-dump
299set TRG(fullstatus) 0 ;# Full "status" report while running
300set TRG(case) {} ;# Only run cases matching this GLOB pattern
301
302switch -nocase -glob -- $tcl_platform(os) {
303 *darwin* {
304 set TRG(platform) osx
305 set TRG(make) make.sh
306 set TRG(makecmd) "bash make.sh"
307 set TRG(testfixture) testfixture
308 set TRG(shell) sqlite3
309 set TRG(run) run.sh
310 set TRG(runcmd) "bash run.sh"
311 }
312 *linux* - MSYS_NT* - MINGW64_NT* - MINGW32_NT* -
313 *haiku* {
314 set TRG(platform) linux
315 set TRG(make) make.sh
316 set TRG(makecmd) "bash make.sh"
317 set TRG(testfixture) testfixture
318 set TRG(shell) sqlite3
319 set TRG(run) run.sh
320 set TRG(runcmd) "bash run.sh"
321 }
322 *freebsd* -
323 *netbsd* -
324 *dragonfly* -
325 *openbsd* {
326 set TRG(platform) linux
327 set TRG(make) make.sh
328 set TRG(makecmd) "sh make.sh"
329 set TRG(testfixture) testfixture
330 set TRG(shell) sqlite3
331 set TRG(run) run.sh
332 set TRG(runcmd) "sh run.sh"
333 }
334 *win* {
335 set TRG(platform) win
336 set TRG(make) make.bat
337 set TRG(makecmd) "call make.bat"
338 set TRG(testfixture) testfixture.exe
339 set TRG(shell) sqlite3.exe
340 set TRG(run) run.bat
341 set TRG(runcmd) "run.bat"
342 if {"unix" eq $tcl_platform(platform)} {
343 # Presumably cygwin. This block gets testrunner.tcl started on
344 # Cygwin but then downstream tests all fail, at least in part
345 # because of the discrepancies in build target names which need
346 # .exe on cygwin but not on other Unix-like platforms.
347 set TRG(platform) cygwin
348 set TRG(make) make.sh
349 set TRG(makecmd) "bash make.sh"
350 set TRG(testfixture) testfixture.exe
351 set TRG(shell) sqlite3.exe
352 set TRG(run) run.sh
353 set TRG(runcmd) "bash run.sh"
354 }
355 }
356 default {
357 puts "tcl_platform(os)=$::tcl_platform(os)"
358 error "cannot determine platform!"
359 }
360}
361set TRG(testfixture-fullpath) [file join $dir $TRG(testfixture)]
362set TRG(shell-fullpath) [file join $dir $TRG(shell)]
363set TRG(interp) [info nameofexec]
364#-------------------------------------------------------------------------
365
366#-------------------------------------------------------------------------
367# The database schema used by the testrunner.db database.
368#
369set TRG(schema) {
370 DROP TABLE IF EXISTS jobs;
371 DROP TABLE IF EXISTS config;
372
373 /*
374 ** This table contains one row for each job that testrunner.tcl must run
375 ** before the entire test run is finished.
376 **
377 ** jobid:
378 ** Unique identifier for each job. Must be a +ve non-zero number.
379 **
380 ** displaytype:
381 ** 3 or 4 letter mnemonic for the class of tests this belongs to e.g.
382 ** "fuzz", "tcl", "make" etc.
383 **
384 ** displayname:
385 ** Name/description of job. For display purposes.
386 **
387 ** build:
388 ** If the job requires a make.bat/make.sh make wrapper (i.e. to build
389 ** something), the name of the build configuration it uses. See
390 ** testrunner_data.tcl for a list of build configs. e.g. "Win32-MemDebug".
391 **
392 ** dirname:
393 ** If the job should use a well-known directory name for its
394 ** sub-directory instead of an anonymous "testdir[1234...]" sub-dir
395 ** that is deleted after the job is finished.
396 **
397 ** cmd:
398 ** Bash or batch script to run the job.
399 **
400 ** depid:
401 ** The jobid value of a job that this job depends on. This job may not
402 ** be run before its depid job has finished successfully.
403 **
404 ** priority:
405 ** Higher values run first. Sometimes.
406 */
407 CREATE TABLE jobs(
408 /* Fields populated when db is initialized */
409 jobid INTEGER PRIMARY KEY, -- id to identify job
410 displaytype TEXT NOT NULL, -- Type of test (for one line report)
411 displayname TEXT NOT NULL, -- Human readable job name
412 build TEXT NOT NULL DEFAULT '', -- make.sh/make.bat file request, if any
413 dirname TEXT NOT NULL DEFAULT '', -- directory name, if required
414 cmd TEXT NOT NULL, -- shell command to run
415 depid INTEGER, -- identifier of dependency (or '')
416 priority INTEGER NOT NULL, -- higher priority jobs may run earlier
417
418 /* Fields updated as jobs run */
419 starttime INTEGER, -- Start time (milliseconds since 1970)
420 endtime INTEGER, -- End time
421 span INTEGER, -- Total run-time in milliseconds
422 estwork INTEGER, -- Estimated amount of work
423 estkey TEXT, -- Key used to compute estwork
424 state TEXT CHECK( state IN ('','ready','running','done','failed','omit','halt') ),
425 ntest INT, -- Number of test cases run
426 nerr INT, -- Number of errors reported
427 svers TEXT, -- Reported SQLite version
428 pltfm TEXT, -- Host platform reported
429 output TEXT, -- test output
430 cwd TEXT -- working directory for test
431 );
432
433 CREATE TABLE config(
434 name TEXT COLLATE nocase PRIMARY KEY,
435 value
436 ) WITHOUT ROWID;
437
438 CREATE INDEX i1 ON jobs(state, priority);
439 CREATE INDEX i2 ON jobs(depid);
440}
441#-------------------------------------------------------------------------
442
443#--------------------------------------------------------------------------
444# Check if this script is being invoked to run a single file. If so,
445# run it.
446#
447if {[llength $argv]==2
448 && ([lindex $argv 0]=="" || [info exists ::testspec([lindex $argv 0])])
449 && [file exists [lindex $argv 1]]
450} {
451 set permutation [lindex $argv 0]
452 set script [file normalize [lindex $argv 1]]
453 set ::argv [list]
454
455 set testdir [file dirname $argv0]
456 source $::testdir/tester.tcl
457
458 if {$permutation=="full"} {
459
460 unset -nocomplain ::G(isquick)
461 reset_db
462
463 } elseif {$permutation!="default" && $permutation!=""} {
464
465 if {[info exists ::testspec($permutation)]==0} {
466 error "no such permutation: $permutation"
467 }
468
469 array set O $::testspec($permutation)
470 set ::G(perm:name) $permutation
471 set ::G(perm:prefix) $O(-prefix)
472 set ::G(isquick) 1
473 set ::G(perm:dbconfig) $O(-dbconfig)
474 set ::G(perm:presql) $O(-presql)
475
476 rename finish_test helper_finish_test
477 proc finish_test {} "
478 uplevel {
479 $O(-shutdown)
480 }
481 helper_finish_test
482 "
483
484 eval $O(-initialize)
485 }
486
487 reset_db
488 source $script
489 exit
490}
491#--------------------------------------------------------------------------
492
493#--------------------------------------------------------------------------
494# Check if this is the "njob" command:
495#
496if {([llength $argv]==2 || [llength $argv]==1)
497 && [string compare -nocase njob [lindex $argv 0]]==0
498} {
499 sqlite3 mydb $TRG(dbname)
500 if {[llength $argv]==2} {
501 set param [lindex $argv 1]
502 if {[string is integer $param]==0 || $param<0 || $param>128} {
503 puts stderr "parameter must be an integer between 0 and 128"
504 exit 1
505 }
506
507 mydb eval { REPLACE INTO config VALUES('njob', $param); }
508 }
509 set res [mydb one { SELECT value FROM config WHERE name='njob' }]
510 mydb close
511 puts "$res"
512 exit
513}
514#--------------------------------------------------------------------------
515
516#--------------------------------------------------------------------------
517# Check if this is the "clean" command:
518#
519if {([llength $argv]==2 || [llength $argv]==1)
520 && [string compare -nocase clean [lindex $argv 0]]==0
521} {
522 set pattern {_(fuzzcheck|sessionfuzz|sqlite3|testfixture)}
523 foreach f [glob testrun_*] {
524 if {[file isdir $f] && [regexp $pattern $f]} {
525 file delete -force $f
526 }
527 }
528 foreach f [glob testdir*] {
529 if {[file isdir $f] && [regexp {^testdir[0-9]+$} $f]} {
530 file delete -force $f
531 }
532 }
533 foreach f [glob testrunner.db*] {
534 file delete -force $f
535 }
536 file delete -force testrunner.log
537 exit
538}
539
540#--------------------------------------------------------------------------
541
542#--------------------------------------------------------------------------
543# Check if this is the "halt" command:
544#
545if {[llength $argv]==1
546 && [string compare -nocase halt [lindex $argv 0]]==0
547} {
548 sqlite3 mydb $TRG(dbname)
549 mydb eval {UPDATE jobs SET state='halt' WHERE state IN ('ready','')}
550 mydb close
551 exit
552}
553#--------------------------------------------------------------------------
554
555#--------------------------------------------------------------------------
556# Check if this is the "estwork" command:
557#
558# Generate (on standard output) a set of estwork() values based on the lastest
559# test case, that can be used to replace the test/testrunner_estwork.tcl file.
560#
561if {[llength $argv]==1
562 && [string compare -nocase estwork [lindex $argv 0]]==0
563} {
564 sqlite3 mydb $TRG(dbname)
565 set njob [mydb one {SELECT count(*) FROM jobs WHERE state='done'}]
566 if {$njob<1000} {
567 puts "Too few completed jobs to do a work estimate."
568 puts "Have $njob but not need at least 1000."
569 mydb close
570 exit 1
571 }
572 set badjobs [mydb one {SELECT count(*) FROM jobs WHERE state<>'done'}]
573 if {$badjobs} {
574 puts "Database contains $badjobs incomplete jobs."
575 mydb close
576 exit 1
577 }
578 set half [mydb one {SELECT count(*)/2 FROM jobs WHERE displaytype='tcl'}]
579 set scale [mydb one {SELECT span FROM jobs WHERE displaytype='tcl'
580 ORDER BY span LIMIT 1 OFFSET $half}]
581 mydb eval {
582 SELECT estkey, CAST(avg(span)/$scale AS INT) AS cost
583 FROM jobs
584 GROUP BY estkey
585 HAVING cost>=2
586 } {
587 set estwork($estkey) $cost
588 }
589 set avgtcl [mydb one {SELECT avg(span) FROM jobs WHERE displaytype='tcl'}]
590 set estwork(tcl) 1
591 foreach type {bld fuzz make} {
592 set avg [mydb one {SELECT avg(span) FROM jobs WHERE displaytype=$type}]
593 if {$avg!=""} {
594 set estwork($type) [expr {int($avg/$avgtcl)}]
595 }
596 }
597 mydb close
598 puts "# Estimated relative cost of various jobs, based on the \"estkey\" field."
599 puts "# Computed by the \"test/testrunner.tcl estwork\" command."
600 puts "#"
601 foreach key [lsort [array names estwork]] {
602 puts "set [list estwork($key)] $estwork($key)"
603 }
604 exit
605}
606#--------------------------------------------------------------------------
607
608#--------------------------------------------------------------------------
609# Check if this is the "help" command:
610#
611if {[string compare -nocase help [lindex $argv 0]]==0} {
612 usage
613}
614#--------------------------------------------------------------------------
615
616#--------------------------------------------------------------------------
617# Check if this is the "script" command:
618#
619if {[string compare -nocase script [lindex $argv 0]]==0} {
620 if {[llength $argv]!=2 && !([llength $argv]==3&&[lindex $argv 1]=="-msvc")} {
621 usage
622 }
623
624 set bMsvc [expr ([llength $argv]==3)]
625 set config [lindex $argv [expr [llength $argv]-1]]
626
627 puts [trd_buildscript $config [file dirname $testdir] $bMsvc]
628 exit
629}
630
631#--------------------------------------------------------------------------
632# Check if this is the "make" command. Example:
633#
634# test/testrunner.tcl make Debug-Two clean testfixture
635# \_______/ \_______________/
636# Configuration ----------^ ^---------- Arguments to "make"
637#
638# This works by running the equivalent of "testrunner.tcl script" to generate
639# the approprate shell script or BAT file, then invoking that script. The
640# generated script is usually deleted automatically, but that can be suppressed
641# using the --keep option.
642#
643if {[string compare -nocase "make" [lindex $argv 0]]==0} {
644 set bKeep 0
645 set bDryRun 0
646 set Config {}
647 set MakeArgs [list]
648 for {set i 1} {$i<[llength $argv]} {incr i} {
649 set arg [lindex $argv $i]
650 if {$arg eq "-keep" || $arg eq "--keep"} {
651 set bKeep 1
652 continue
653 }
654 if {[regexp {^--?dry-?run$} $arg] || [regexp {^--?n$} $arg]} {
655 set bDryRun 1
656 continue
657 }
658 if {$Config eq ""} {
659 if {![info exists ::trd::build($arg)]} {
660 puts stderr "No such configuration: $arg"
661 puts stderr "Should be one of: [lsort [array names ::trd::build]]"
662 exit 1
663 }
664 set Config $arg
665 continue
666 }
667 lappend MakeArgs $arg
668 }
669 if {$Config eq ""} {
670 puts stderr "Missing configuration name"
671 puts stderr "Run \"$argv0 help\" for help"
672 exit 1
673 }
674 set scriptname testrunner-[expr {int(rand()*1000000)}][clock seconds]
675 if {$tcl_platform(platform) eq "windows"} {
676 set bMsvc 1
677 append scriptname .bat
678 } else {
679 set bMsvc 0
680 append scriptname .sh
681 }
682 if {$bDryRun} {
683 puts "Script \"$scriptname\" would have been:"
684 puts [trd_buildscript $Config [file dirname $testdir] $bMsvc $MakeArgs]
685 exit 0
686 }
687 set fd [open $scriptname w]
688 puts $fd [trd_buildscript $Config [file dirname $testdir] $bMsvc $MakeArgs]
689 close $fd
690 if {$bMsvc} {
691 set rc [catch {
692 exec $scriptname >@stdout
693 } msg]
694 set rc 0
695 } else {
696 set rc [catch {
697 exec sh $scriptname >@stdout 2>@stderr
698 } msg]
699 }
700 if {$bKeep} {
701 puts "script retained in \"$scriptname\"
702 } else {
703 file delete -force $scriptname
704 }
705 if {$rc} {
706 puts stderr "make failed: $msg"
707 exit 1
708 }
709 exit
710}
711
712# Compute an elapse time string MM:SS or HH:MM:SS based on the
713# number of milliseconds in the argument.
714#
715proc elapsetime {ms} {
716 if {$ms==""} {set ms 0}
717 set s [expr {int(($ms+500.0)*0.001)}]
718 set hr [expr {$s/3600}]
719 set mn [expr {($s/60)%60}]
720 set sc [expr {$s%60}]
721 if {$hr>0} {
722 return [format %02d:%02d:%02d $hr $mn $sc]
723 } else {
724 return [format %02d:%02d $mn $sc]
725 }
726}
727
728# Helper routine for show_status
729#
730proc display_job {jobdict {tm ""}} {
731 array set job $jobdict
732 if {[string length $job(displayname)]>65} {
733 set dfname [format %.65s... $job(displayname)]
734 } else {
735 set dfname [format %-68s $job(displayname)]
736 }
737 set dtm ""
738 if {$tm!=""} {
739 set dtm [expr {$tm-$job(starttime)}]
740 set dtm [format %8s [elapsetime $dtm]]
741 } else {
742 set dtm [format %8s ""]
743 }
744 puts " $dfname $dtm"
745}
746
747# This procedure shows the "status" page. It uses the database
748# connect passed in as the "db" parameter. If the "cls" parameter
749# is true, then VT100 escape codes are used to format the display.
750#
751proc show_status {db cls} {
752 global TRG
753 $db eval BEGIN
754 if {[catch {
755 set cmdline [$db one { SELECT value FROM config WHERE name='cmdline' }]
756 set nJob [$db one { SELECT value FROM config WHERE name='njob' }]
757 } msg]} {
758 if {$cls} {puts "\033\[H\033\[2J"}
759 puts "Cannot read database: $TRG(dbname)"
760 return
761 }
762 set now [clock_milliseconds]
763 set tm [$db one {
764 SELECT
765 COALESCE((SELECT value FROM config WHERE name='end'), $now) -
766 (SELECT value FROM config WHERE name='start')
767 }]
768
769 set totalw 0
770 foreach s {"" ready running done failed omit} { set S($s) 0; set W($s) 0; }
771 set workpending 0
772 $db eval {
773 SELECT state, count(*) AS cnt, sum(estwork) AS ew FROM jobs GROUP BY 1
774 } {
775 incr S($state) $cnt
776 incr W($state) $ew
777 incr totalw $ew
778 }
779 set nt 0
780 set ne 0
781 $db eval {
782 SELECT sum(ntest) AS nt, sum(nerr) AS ne FROM jobs HAVING nt>0
783 } break
784 set fin [expr $W(done)+$W(failed)+$W(omit)]
785 if {$cmdline!=""} {set cmdline " $cmdline"}
786
787 if {$cls} {
788 # Move the cursor to the top-left corner. Each iteration will simply
789 # overwrite.
790 puts -nonewline "\033\[H"
791 flush stdout
792 }
793 puts [format %-79.79s "Command: \[testrunner.tcl$cmdline\]"]
794 puts [format %-79.79s "Summary: [elapsetime $tm], $fin/$totalw jobs,\
795 $ne errors, $nt tests"]
796
797 set srcdir [file dirname [file dirname $TRG(info_script)]]
798 set line "Running: $S(running) (max: $nJob)"
799 if {$S(running)>0 && [set pct [expr {int(($fin*100.0)/$totalw)}]]>=4} {
800 set tmleft [expr {($tm/double($fin))*($totalw-$fin)}]
801 if {$tmleft<0.02*$tm} {
802 set tmleft [expr {$tm*0.02}]
803 }
804 set etc " ETC [elapsetime $tmleft]"
805 if {[string length $line]+[string length $etc]<80} {
806 append line $etc
807 }
808 # append line " $pct%"
809 }
810 puts [format %-79.79s $line]
811 if {$S(running)>0} {
812 $db eval {
813 SELECT * FROM jobs WHERE state='running' ORDER BY starttime
814 } job {
815 display_job [array get job] $now
816 }
817 }
818 if {$S(failed)>0} {
819 # $toshow is the number of failures to report. In $cls mode,
820 # status tries to limit the number of failure reported so that
821 # the status display does not overflow a 24-line terminal. It will
822 # always show at least the most recent 4 failures, even if an overflow
823 # is needed. No limit is imposed for a status within $cls.
824 #
825 if {$cls && $S(failed)>18-$S(running)} {
826 set toshow [expr {18-$S(running)}]
827 if {$toshow<4} {set toshow 4}
828 set shown " (must recent $toshow shown)"
829 } else {
830 set toshow $S(failed)
831 set shown ""
832 }
833 puts [format %-79s "Failed: $S(failed) $shown"]
834 $db eval {
835 SELECT * FROM jobs WHERE state='failed'
836 ORDER BY endtime DESC LIMIT $toshow
837 } job {
838 display_job [array get job]
839 }
840 set nOmit [$db one {SELECT count(*) FROM jobs WHERE state='omit'}]
841 if {$nOmit} {
842 puts [format %-79s " ... $nOmit jobs omitted due to failures"]
843 }
844 }
845 if {$cls} {
846 # Clear everything else to the bottom of the screen
847 puts -nonewline "\033\[0J"
848 flush stdout
849 }
850 $db eval COMMIT
851}
852
853
854
855#--------------------------------------------------------------------------
856# Check if this is the "status" command:
857#
858if {[llength $argv]>=1
859 && [string compare -nocase status [lindex $argv 0]]==0
860} {
861 set delay 0
862 set cls 0
863 for {set ii 1} {$ii<[llength $argv]} {incr ii} {
864 set a0 [lindex $argv $ii]
865 if {$a0=="-d" && $ii+1<[llength $argv]} {
866 incr ii
867 set delay [lindex $argv $ii]
868 if {![string is integer -strict $delay]} {
869 puts "Argument to -d should be an integer"
870 exit 1
871 }
872 } elseif {$a0=="-cls" || $a0=="--cls"} {
873 set cls 1
874 } else {
875 puts "unknown option: \"$a0\""
876 exit 1
877 }
878 }
879
880 set once 1
881 while {![file readable $TRG(dbname)]} {
882 if {$delay==0} {
883 puts "Database missing: $TRG(dbname)"
884 exit
885 }
886 if {$once} {
887 set once 0
888 puts "Waiting for testing to start...."
889 flush stdout
890 }
891 after [expr {$delay*1000}]
892 }
893 sqlite3 mydb $TRG(dbname)
894 mydb timeout 2000
895
896 # Clear the whole screen initially.
897 #
898 if {$delay>0 || $cls} {puts -nonewline "\033\[2J"}
899
900 while {1} {
901 show_status mydb [expr {$delay>0 || $cls}]
902 if {$delay<=0} break
903 after [expr {$delay*1000}]
904 }
905 mydb close
906 exit
907}
908
909#--------------------------------------------------------------------------
910# Check if this is the "joblist" command:
911#
912if {[llength $argv]>=1
913 && [string compare -nocase "joblist" [lindex $argv 0]]==0
914} {
915 set pattern {}
916 for {set ii 1} {$ii<[llength $argv]} {incr ii} {
917 set a0 [lindex $argv $ii]
918 if {$pattern==""} {
919 set pattern [string trim $a0 *]
920 } else {
921 puts "unknown option: \"$a0\""
922 exit 1
923 }
924 }
925 set SQL {SELECT displaytype, displayname, state FROM jobs}
926 if {$pattern!=""} {
927 regsub -all {[^a-zA-Z0-9*.-/]} $pattern ? pattern
928 set pattern [string tolower $pattern]
929 append SQL \
930 " WHERE lower(concat(state,' ',displaytype,' ',displayname)) GLOB '*$pattern*'"
931 }
932 append SQL " ORDER BY starttime"
933
934 if {![file readable $TRG(dbname)]} {
935 puts "Database missing: $TRG(dbname)"
936 exit
937 }
938 sqlite3 mydb $TRG(dbname)
939 mydb timeout 2000
940
941 mydb eval $SQL {
942 set label UNKNOWN
943 switch -- $state {
944 ready {set label READY}
945 done {set label DONE}
946 failed {set label FAILED}
947 omit {set label OMIT}
948 running {set label RUNNING}
949 }
950 puts [format {%-7s %-5s %s} $label $displaytype $displayname]
951 }
952 mydb close
953 exit
954}
955
956# Scan the output of all jobs looking for the summary lines that
957# report the number of test cases and the number of errors.
958# Aggregate these numbers and return them.
959#
960proc aggregate_test_counts {db} {
961 set ne 0
962 set nt 0
963 $db eval {SELECT sum(nerr) AS ne, sum(ntest) as nt FROM jobs} break
964 return [list $ne $nt]
965}
966
967#--------------------------------------------------------------------------
968# Check if this is the "errors" command:
969#
970if {[llength $argv]>=1
971 && ([string compare -nocase errors [lindex $argv 0]]==0 ||
972 [string match err* [lindex $argv 0]]==1)
973} {
974 set verbose 0
975 set pattern {}
976 set summary 0
977 for {set ii 1} {$ii<[llength $argv]} {incr ii} {
978 set a0 [lindex $argv $ii]
979 if {$a0=="-v" || $a0=="--verbose" || $a0=="-verbose"} {
980 set verbose 1
981 } elseif {$a0=="-s" || $a0=="--summary" || $a0=="-summary"} {
982 set summary 1
983 } elseif {$pattern==""} {
984 set pattern *[string trim $a0 *]*
985 } else {
986 puts "unknown option: \"$a0\"". Use --help for more info."
987 exit 1
988 }
989 }
990 set cnt 0
991 sqlite3 mydb $TRG(dbname)
992 mydb timeout 5000
993 if {$summary} {
994 set sql "SELECT displayname FROM jobs WHERE state='failed'"
995 } else {
996 set sql "SELECT displaytype, displayname, output FROM jobs \
997 WHERE state='failed'"
998 }
999 if {$pattern!=""} {
1000 regsub -all {[^a-zA-Z0-9*/ ?]} $pattern . pattern
1001 append sql " AND displayname GLOB '$pattern'"
1002 }
1003 mydb eval $sql {
1004 if {$summary} {
1005 puts "FAILED: $displayname"
1006 continue
1007 }
1008 puts "**** $displayname ****"
1009 if {$verbose || $displaytype!="tcl"} {
1010 puts $output
1011 } else {
1012 foreach line [split $output \n] {
1013 if {[string match {!*} $line] || [string match *failed* $line]} {
1014 puts $line
1015 }
1016 }
1017 }
1018 incr cnt
1019 }
1020 if {$pattern==""} {
1021 set summary [aggregate_test_counts mydb]
1022 mydb close
1023 puts "Total [lindex $summary 0] errors out of [lindex $summary 1] tests"
1024 } else {
1025 mydb close
1026 }
1027 exit
1028}
1029
1030#-------------------------------------------------------------------------
1031# Parse the command line.
1032#
1033for {set ii 0} {$ii < [llength $argv]} {incr ii} {
1034 set isLast [expr $ii==([llength $argv]-1)]
1035 set a [lindex $argv $ii]
1036 set n [string length $a]
1037
1038 if {[string range $a 0 0]=="-"} {
1039 if {($n>2 && [string match "$a*" --jobs]) || $a=="-j"} {
1040 incr ii
1041 set TRG(nJob) [lindex $argv $ii]
1042 if {$isLast} { usage }
1043 } elseif {($n>2 && [string match "$a*" --zipvfs]) || $a=="-z"} {
1044 incr ii
1045 set TRG(zipvfs) [file normalize [lindex $argv $ii]]
1046 if {$isLast} { usage }
1047 } elseif {($n>2 && [string match "$a*" --buildonly]) || $a=="-b"} {
1048 set TRG(buildonly) 1
1049 } elseif {($n>2 && [string match "$a*" --config]) || $a=="-c"} {
1050 incr ii
1051 set TRG(config) [lindex $argv $ii]
1052 } elseif {($n>2 && [string match "$a*" --dryrun]) || $a=="-d"} {
1053 set TRG(dryrun) 1
1054 } elseif {($n>2 && [string match "$a*" --explain]) || $a=="-e"} {
1055 set TRG(explain) 1
1056 } elseif {$n>2 && [string match "$a*" --omit]} {
1057 incr ii
1058 set TRG(omitconfig) [lindex $argv $ii]
1059 } elseif {$n>2 && [string match "$a*" --cases]} {
1060 incr ii
1061 set TRG(case) [lindex $argv $ii]
1062 } elseif {$n>2 && [string match "$a*" --fuzzdb]} {
1063 incr ii
1064 set env(FUZZDB) [lindex $argv $ii]
1065 } elseif {[string match "$a*" --stop-on-error]} {
1066 set TRG(stopOnError) 1
1067 } elseif {[string match "$a*" --stop-on-coredump]} {
1068 set TRG(stopOnCore) 1
1069 } elseif {[string match "$a*" --status]} {
1070 if {$tcl_platform(platform) eq "windows"} {
1071 puts stdout \
1072"The --status option is not available on Windows. A suggested work-around"
1073 puts stdout \
1074"is to run the following command in a separate window:\n"
1075 puts stdout " [info nameofexe] $argv0 status -d 2\n"
1076 } else {
1077 set TRG(fullstatus) 1
1078 }
1079 } elseif {($n>2 && [string match "$a*" --nbuild]) || $a=="-nb"} {
1080 incr ii
1081 set TRG(nBuild) [lindex $argv $ii]
1082 if {$isLast} { usage }
1083 } else {
1084 usage
1085 }
1086 } else {
1087 lappend TRG(patternlist) [string map {% *} $a]
1088 }
1089}
1090set argv [list]
1091
1092# This script runs individual tests - tcl scripts or [make xyz] commands -
1093# in directories named "testdir$N", where $N is an integer. This variable
1094# contains a list of integers indicating the directories in use.
1095#
1096# This variable is accessed only via the following commands:
1097#
1098# dirs_nHelper
1099# Return the number of entries currently in the list.
1100#
1101# dirs_freeDir IDIR
1102# Remove value IDIR from the list. It is an error if it is not present.
1103#
1104# dirs_allocDir
1105# Select a value that is not already in the list. Add it to the list
1106# and return it.
1107#
1108set TRG(dirs_in_use) [list]
1109
1110proc dirs_nHelper {} {
1111 global TRG
1112 llength $TRG(dirs_in_use)
1113}
1114proc dirs_freeDir {iDir} {
1115 global TRG
1116 set out [list]
1117 foreach d $TRG(dirs_in_use) {
1118 if {$iDir!=$d} { lappend out $d }
1119 }
1120 if {[llength $out]!=[llength $TRG(dirs_in_use)]-1} {
1121 error "dirs_freeDir could not find $iDir"
1122 }
1123 set TRG(dirs_in_use) $out
1124}
1125proc dirs_allocDir {} {
1126 global TRG
1127 array set inuse [list]
1128 foreach d $TRG(dirs_in_use) {
1129 set inuse($d) 1
1130 }
1131 for {set iRet 0} {[info exists inuse($iRet)]} {incr iRet} { }
1132 lappend TRG(dirs_in_use) $iRet
1133 return $iRet
1134}
1135
1136# Check that directory $dir exists. If it does not, create it. If
1137# it does, delete its contents.
1138#
1139proc create_or_clear_dir {dir} {
1140 set dir [file normalize $dir]
1141 catch { file mkdir $dir }
1142 foreach f [glob -nocomplain [file join $dir *]] {
1143 catch { file delete -force $f }
1144 }
1145}
1146
1147proc build_to_dirname {bname} {
1148 set fold [string tolower [string map {- _} $bname]]
1149 return "testrunner_build_$fold"
1150}
1151
1152#-------------------------------------------------------------------------
1153
1154proc r_write_db {tcl} {
1155 trdb eval { BEGIN EXCLUSIVE }
1156 uplevel $tcl
1157 trdb eval { COMMIT }
1158}
1159
1160# Obtain a new job to be run by worker $iJob (an integer). A job is
1161# returned as a serialized array containing all fields from the "jobs"
1162# table.
1163#
1164proc r_get_next_job {iJob} {
1165 global T
1166 global TRG
1167 set orderby ""
1168
1169 r_write_db {
1170 # If the TRG(nBuild) variable is greater than 0, count how many builds
1171 # are running already. If there are $TRG(nBuild) or more, ensure that
1172 # something other than a build is selected to run next.
1173 #
1174 if {$TRG(nBuild)>0} {
1175 set n [trdb one {
1176 SELECT count(*) FROM jobs WHERE state='running' AND displaytype='bld'
1177 }]
1178 if {$n>=$TRG(nBuild)} {
1179 set orderby "AND displaytype!='bld' "
1180 }
1181 }
1182
1183 if {($iJob%2)} {
1184 append orderby "ORDER BY priority ASC"
1185 } else {
1186 append orderby "ORDER BY priority DESC"
1187 }
1188
1189 set ret [list]
1190
1191 set query "
1192 SELECT * FROM jobs AS j WHERE state='ready' $orderby LIMIT 1
1193 "
1194 trdb eval $query job {
1195 set tm [clock_milliseconds]
1196 set T($iJob) $tm
1197 set jobid $job(jobid)
1198
1199 set cwd $job(dirname)
1200 if {$cwd==""} {
1201 set cwd [dirname $iJob]
1202 }
1203
1204 trdb eval {
1205 UPDATE jobs
1206 SET starttime=$tm, state='running', cwd=$cwd
1207 WHERE jobid=$jobid
1208 }
1209
1210 set ret [array get job]
1211 }
1212 }
1213
1214 return $ret
1215}
1216
1217# Usage:
1218#
1219# add_job OPTION ARG OPTION ARG...
1220#
1221# where available OPTIONS are:
1222#
1223# -displaytype
1224# -displayname
1225# -build
1226# -dirname
1227# -cmd
1228# -depid
1229# -priority
1230#
1231# Returns the jobid value for the new job.
1232#
1233proc add_job {args} {
1234 global estwork
1235
1236 set options {
1237 -displaytype -displayname -build -dirname
1238 -cmd -depid -priority
1239 }
1240
1241 # Set default values of options.
1242 set A(-dirname) ""
1243 set A(-depid) ""
1244 set A(-priority) 0
1245 set A(-build) ""
1246
1247 array set A $args
1248
1249 # Check all required options are present. And that no extras are present.
1250 foreach o $options {
1251 if {[info exists A($o)]==0} { error "missing required option $o" }
1252 }
1253 foreach o [array names A] {
1254 if {[lsearch -exact $options $o]<0} { error "unrecognized option: $o" }
1255 }
1256
1257 set state ""
1258 if {$A(-depid)==""} { set state ready }
1259 set type $A(-displaytype)
1260 set displayname $A(-displayname)
1261 switch $type {
1262 tcl {
1263 set ek [file tail [lindex $displayname end]]
1264 }
1265 bld {
1266 set ek [lindex $displayname end]
1267 }
1268 fuzz {
1269 set ek [lrange $displayname 1 2]
1270 }
1271 make {
1272 set ek [lindex $displayname end]
1273 }
1274 }
1275 if {[info exists estwork($ek)]} {
1276 set ew $estwork($ek)
1277 } else {
1278 set ew $estwork($type)
1279 }
1280
1281 trdb eval {
1282 INSERT INTO jobs(
1283 displaytype, displayname, build, dirname, cmd, depid, priority,
1284 estwork, estkey, state
1285 ) VALUES (
1286 $type,
1287 $A(-displayname),
1288 $A(-build),
1289 $A(-dirname),
1290 $A(-cmd),
1291 $A(-depid),
1292 $A(-priority),
1293 $ew,
1294 $ek,
1295 $state
1296 )
1297 }
1298
1299 trdb last_insert_rowid
1300}
1301
1302# Look to see if $jobcmd matches any of the glob patterns given in
1303# $patternlist. Return true if there is a match. Return false
1304# if no match is seen.
1305#
1306# An empty patternlist matches everything
1307#
1308# Entries of patternlist that begin with "~" mean "match anything that
1309# does not match the following pattern". For example, a patternlist
1310# of {fuzzcheck ~san} will match "fuzzcheck" but not "fuzzcheck-asan".
1311#
1312proc job_matches_any_pattern {patternlist jobcmd} {
1313 set bMatch 0
1314 set bMiss 0
1315 if {[llength $patternlist]==0} {return 1}
1316 foreach p $patternlist {
1317 if {[string index $p 0] eq "~"} {
1318 set p [string range $p 1 end]
1319 set not 1
1320 } else {
1321 set not 0
1322 }
1323 set p [string trim $p *]
1324 if {[string index $p 0]=="^"} {
1325 set p [string range $p 1 end]
1326 } else {
1327 set p "*$p"
1328 }
1329 if {[string index $p end]=="\$"} {
1330 set p [string range $p 0 end-1]
1331 } else {
1332 set p "$p*"
1333 }
1334 if {$not} {
1335 if {[string match $p $jobcmd]} {return 0}
1336 } else {
1337 if {[string match $p $jobcmd]} {
1338 set bMatch 1
1339 } else {
1340 set bMiss 1
1341 }
1342 }
1343 }
1344 if {!$bMiss} {
1345 set bMatch 1
1346 }
1347 return $bMatch
1348}
1349
1350
1351# Argument $build is either an empty string, or else a list of length 3
1352# describing the job to build testfixture. In the usual form:
1353#
1354# {ID DIRNAME DISPLAYNAME}
1355#
1356# e.g
1357#
1358# {1 /home/user/sqlite/test/testrun_xyz All-Debug}
1359#
1360proc add_tcl_jobs {build config patternlist {shelldepid ""}} {
1361 global TRG
1362 set ntcljob 0
1363
1364 set topdir [file dirname $::testdir]
1365 set testrunner_tcl [file normalize [info script]]
1366
1367 if {$build==""} {
1368 set testfixture $TRG(interp)
1369 } else {
1370 set testfixture [file join [lindex $build 1] $TRG(testfixture)]
1371 }
1372 if {[lindex $build 2]=="Valgrind"} {
1373 set setvar "export OMIT_MISUSE=1\n"
1374 set testfixture "${setvar}valgrind -v --error-exitcode=1 $testfixture"
1375 }
1376
1377 # The ::testspec array is populated by permutations.test
1378 foreach f [dict get $::testspec($config) -files] {
1379
1380 if {[file pathtype $f]!="absolute"} { set f [file join $::testdir $f] }
1381 set f [file normalize $f]
1382
1383 set displayname [string map [list $topdir/ {}] $f]
1384 if {$config=="full" || $config=="veryquick"} {
1385 set cmd "$testfixture $f"
1386 } else {
1387 set cmd "$testfixture $testrunner_tcl $config $f"
1388 set displayname "config=$config $displayname"
1389 }
1390 if {$build!=""} {
1391 set displayname "[lindex $build 2] $displayname"
1392 }
1393
1394 if {![job_matches_any_pattern $patternlist $displayname]} {
1395 continue
1396 }
1397
1398 set lProp [trd_test_script_properties $f]
1399 set priority 0
1400 if {[lsearch $lProp slow]>=0} { set priority 2 }
1401 if {[lsearch $lProp superslow]>=0} { set priority 4 }
1402
1403 set depid [lindex $build 0]
1404 if {$shelldepid!="" && [lsearch $lProp shell]>=0} { set depid $shelldepid }
1405
1406 incr ntcljob
1407 add_job \
1408 -displaytype tcl \
1409 -displayname $displayname \
1410 -cmd $cmd \
1411 -depid $depid \
1412 -priority $priority
1413
1414 }
1415}
1416
1417# Add one job for each *.sql script in the test directory. Each job runs
1418# the script using the CLI: "sqlite3 FILENAME".
1419#
1420# The $build argument is as for add_tcl_jobs. If it is an empty string,
1421# use the CLI in the current directory, trying to build it if it is missing.
1422# Otherwise, use the CLI from that build, and depend on the placeholder job
1423# "SHELL", which the caller must replace with a job that builds the CLI.
1424#
1425proc add_sql_jobs {build patternlist} {
1426 global TRG
1427
1428 set topdir [file dirname $::testdir]
1429 set depid ""
1430 if {$build==""} {
1431 set cli $TRG(shell-fullpath)
1432 } else {
1433 set cli [file join [lindex $build 1] $TRG(shell)]
1434 set depid SHELL
1435 }
1436 if {[lindex $build 2]=="Valgrind"} {
1437 set cli "valgrind -v --error-exitcode=1 $cli"
1438 }
1439
1440 foreach f [lsort [glob -nocomplain -directory $::testdir *.sql]] {
1441 set f [file normalize $f]
1442 set displayname [string map [list $topdir/ {}] $f]
1443 if {$build!=""} {
1444 set displayname "[lindex $build 2] $displayname"
1445 }
1446
1447 if {![job_matches_any_pattern $patternlist $displayname]} {
1448 continue
1449 }
1450
1451 if {$build=="" && ![file executable $TRG(shell-fullpath)]} {
1452 puts "make $TRG(shell)"
1453 catch {exec make $TRG(shell) >@stdout 2>@stderr}
1454 if {![file executable $TRG(shell-fullpath)]} {
1455 puts "Skipping *.sql tests because there is no $TRG(shell) to run them."
1456 return
1457 }
1458 }
1459
1460 add_job \
1461 -displaytype tcl \
1462 -displayname $displayname \
1463 -cmd "$cli --noinit $f .q" \
1464 -depid $depid
1465 }
1466}
1467
1468proc add_build_job {buildname target {postcmd ""} {depid ""}} {
1469 global TRG
1470
1471 set dirname "[string tolower [string map {- _} $buildname]]_$target"
1472 regsub {\.exe$} $dirname {} dirname
1473 regsub {\.l?o$} $dirname {obj} dirname
1474 set dirname "testrun_$dirname"
1475
1476 set cmd "$TRG(makecmd) $target"
1477 if {$postcmd!=""} {
1478 append cmd "\n"
1479 append cmd $postcmd
1480 }
1481
1482 set id [add_job \
1483 -displaytype bld \
1484 -displayname "Build $buildname ($target)" \
1485 -dirname $dirname \
1486 -build $buildname \
1487 -cmd $cmd \
1488 -depid $depid \
1489 -priority 3
1490 ]
1491
1492 list $id [file normalize $dirname] $buildname
1493}
1494
1495# Add jobs to build and run all the *.c files in $testdir/c/ for build
1496# configuration $buildname.
1497#
1498proc add_c_jobs {buildname patternlist} {
1499 global TRG
1500
1501 set dir [file join $::testdir c]
1502
1503 # One job to build the sqlite3.o file for this configuration. Each
1504 # individual "c" job will copy this sqlite3.o into its working directory
1505 # so that it doesn't have to build it separately every time.
1506 #
1507 set obj sqlite3.o
1508 if {$TRG(platform)=="win"} { set obj sqlite3.lo }
1509 set B [add_build_job $buildname $obj]
1510 foreach {bldid blddir dummy} $B {}
1511
1512 # One job for each C file.
1513 #
1514 foreach f [glob -nocomplain $dir/*.c] {
1515 set prg [string range [file tail $f] 0 end-2]
1516
1517 set cmd ""
1518 if {$TRG(platform)=="win"} {
1519 foreach cp {sqlite3.lo *.h *.c} {
1520 append cmd "copy [file nativename [file join $blddir $cp]] .\n"
1521 }
1522 append cmd "SET AUXTEST=$prg\n"
1523 set prg "${prg}.exe"
1524 append cmd "$TRG(makecmd) $prg\n"
1525 append cmd ".\\$prg\n"
1526 } else {
1527 set cmd "set -e\n"
1528 foreach cp {sqlite3.c sqlite3.h sqlite3.o .target_source src-verify} {
1529 append cmd "cp [file join $blddir $cp] .\n"
1530 }
1531 append cmd "AUXTEST=$prg $TRG(makecmd) $prg\n"
1532 append cmd "./$prg\n"
1533 }
1534 if {![job_matches_any_pattern $patternlist "$prg ($buildname)"]} {
1535 continue
1536 }
1537
1538 set id [add_job \
1539 -displaytype tcl \
1540 -displayname "$prg ($buildname)" \
1541 -build $buildname \
1542 -cmd $cmd \
1543 -depid $bldid \
1544 -priority 3
1545 ]
1546 }
1547}
1548
1549proc add_shell_build_job {buildname dirname depid} {
1550 global TRG
1551
1552 if {$TRG(platform)=="win"} {
1553 set path [string map {/ \\} "$dirname/"]
1554 set copycmd "xcopy $TRG(shell) $path"
1555 } else {
1556 set copycmd "cp $TRG(shell) $dirname/"
1557 }
1558
1559 return [
1560 add_build_job $buildname $TRG(shell) $copycmd $depid
1561 ]
1562}
1563
1564
1565proc add_make_job {bld target} {
1566 global TRG
1567
1568 if {$TRG(platform)=="win"} {
1569 set path [string map {/ \\} [lindex $bld 1]]
1570 set cmd "xcopy /S $path\\* ."
1571 } else {
1572 set cmd "cp -r [lindex $bld 1]/* ."
1573 }
1574 append cmd "\n$TRG(makecmd) $target"
1575
1576 add_job \
1577 -displaytype make \
1578 -displayname "[lindex $bld 2] make $target" \
1579 -cmd $cmd \
1580 -depid [lindex $bld 0] \
1581 -priority 1
1582}
1583
1584proc add_fuzztest_jobs {buildname patternlist} {
1585 global env TRG
1586 # puts buildname=$buildname
1587
1588 foreach {interpreter scripts} [trd_fuzztest_data $buildname $TRG(fil-cc)] {
1589 set bldDone 0
1590 set subcmd [lrange $interpreter 1 end]
1591 set interpreter [lindex $interpreter 0]
1592
1593 # For fuzzcheck-asan and fuzzcheck-ubsan, break up some
1594 # fuzzdata files into multiple slices, for improved
1595 # concurrency.
1596 #
1597 if {[string match *fuzzcheck-*san $interpreter]} {
1598 set newscripts {}
1599 foreach s $scripts {
1600 if {[string match {*fuzzdata[12].db} $s]
1601 && ![string match slice $s]} {
1602 set N 6
1603 for {set i 0} {$i<$N} {incr i} {
1604 lappend newscripts [list --slice $i $N $s]
1605 }
1606 } else {
1607 lappend newscripts $s
1608 }
1609 }
1610 set scripts $newscripts
1611 }
1612
1613 if {[string match fuzzcheck* $interpreter]
1614 && [info exists env(FUZZDB)]
1615 && [file readable $env(FUZZDB)]
1616 && $buildname ne "Windows-Win32Heap"
1617 && $buildname ne "Windows-Memdebug"
1618 } {
1619 set TRG(FUZZDB) $env(FUZZDB)
1620 set fname [file normalize $env(FUZZDB)]
1621 set N [expr {([file size $fname]+4999999)/5000000}]
1622 for {set i 0} {$i<$N} {incr i} {
1623 lappend scripts [list --slice $i $N $fname]
1624 }
1625 }
1626
1627 foreach s $scripts {
1628
1629 # Fuzz data files fuzzdata1.db and fuzzdata2.db are larger than
1630 # the others. So ensure that these are run as a higher priority.
1631 if {[llength $s]==1} {
1632 set tail [file tail $s]
1633 } else {
1634 set fname [lindex $s end]
1635 set tail [lrange $s 0 end-1]
1636 lappend tail [file tail $fname]
1637 }
1638 if {![job_matches_any_pattern $patternlist "$buildname $interpreter $tail"]} {
1639 continue
1640 }
1641 if {!$bldDone} {
1642 set bld [add_build_job $buildname $interpreter]
1643 foreach {depid dirname displayname} $bld {}
1644 set bldDone 1
1645 }
1646 if {[string match ?-slice* $tail]} {
1647 set priority 15
1648 } elseif {$tail=="fuzzdata1.db"
1649 || $tail=="fuzzdata2.db"
1650 || $tail=="fuzzdata8.db"} {
1651 set priority 5
1652 } else {
1653 set priority 1
1654 }
1655 add_job \
1656 -displaytype fuzz \
1657 -displayname "$buildname $interpreter $tail" \
1658 -depid $depid \
1659 -cmd "[file join $dirname $interpreter] $subcmd $s" \
1660 -priority $priority
1661 }
1662 }
1663}
1664
1665proc add_zipvfs_jobs {} {
1666 global TRG
1667 source [file join $TRG(zipvfs) test zipvfs_testrunner.tcl]
1668
1669 set bld [add_build_job Zipvfs $TRG(testfixture)]
1670 foreach s [zipvfs_testrunner_files] {
1671 set cmd "[file join [lindex $bld 1] $TRG(testfixture)] $s"
1672 add_job \
1673 -displaytype tcl \
1674 -displayname "Zipvfs [file tail $s]" \
1675 -cmd $cmd \
1676 -depid [lindex $bld 0]
1677 }
1678
1679 set ::env(SQLITE_TEST_DIR) $::testdir
1680}
1681
1682# Used to add jobs for "mdevtest" and "sdevtest".
1683#
1684proc add_devtest_jobs {lBld patternlist} {
1685 global TRG
1686
1687 foreach b $lBld {
1688 set bld [add_build_job $b $TRG(testfixture)]
1689 add_tcl_jobs $bld veryquick $patternlist SHELL
1690 add_sql_jobs $bld $patternlist
1691 add_fuzztest_jobs $b $patternlist
1692
1693 if {[trdb one "SELECT EXISTS (SELECT 1 FROM jobs WHERE depid='SHELL')"]} {
1694 set sbld [add_shell_build_job $b [lindex $bld 1] [lindex $bld 0]]
1695 set sbldid [lindex $sbld 0]
1696 trdb eval {
1697 UPDATE jobs SET depid=$sbldid WHERE depid='SHELL'
1698 }
1699 }
1700
1701 }
1702}
1703
1704# Check to ensure that TRG(interp) is a full-blown "testfixture" and
1705# not just a "tclsh".
1706#
1707# The value of TRG(interp) defaults to whatever interpreter is running
1708# this script, which might be either tclsh or testfixture. If tclsh is
1709# running this script, change $TRG(interp) to be an instance of testfixture.
1710# If no testfixture exists in the directory from which this script is run,
1711# attempt to build one.
1712#
1713# Do not return unless $TRG(interp) is a valid testfixture. If unable
1714# to find and/or construct one, abort with an error message.
1715#
1716proc must_be_testfixture {} {
1717 global TRG
1718 if {[lsearch [info commands] sqlite3_soft_heap_limit]<0} {
1719 if {![file exec $TRG(testfixture-fullpath)]} {
1720 puts "make testfixture"
1721 catch {exec make testfixture >@stdout 2>@stderr}
1722 }
1723 if {![file exec $TRG(testfixture-fullpath)]} {
1724 puts "Requires testfixture, and I was unable to build it."
1725 exit 1
1726 }
1727 set TRG(interp) $TRG(testfixture-fullpath)
1728 }
1729}
1730
1731proc add_jobs_from_cmdline {patternlist} {
1732 global TRG
1733
1734 if {$TRG(zipvfs)!=""} {
1735 add_zipvfs_jobs
1736 if {[llength $patternlist]==0} return
1737 }
1738
1739 if {[llength $patternlist]==0} {
1740 set patternlist [list veryquick]
1741 }
1742
1743 set first [lindex $patternlist 0]
1744 switch -- $first {
1745 all {
1746 must_be_testfixture
1747 set patternlist [lrange $patternlist 1 end]
1748 set clist [trd_all_configs]
1749 foreach c $clist {
1750 add_tcl_jobs "" $c $patternlist
1751 }
1752 add_sql_jobs "" $patternlist
1753 }
1754
1755 devtest -
1756 mdevtest {
1757 set config_set {
1758 All-O0
1759 All-Debug
1760 }
1761 add_devtest_jobs $config_set [lrange $patternlist 1 end]
1762 }
1763
1764 sdevtest {
1765 set config_set {
1766 All-Sanitize
1767 All-Debug
1768 }
1769 add_devtest_jobs $config_set [lrange $patternlist 1 end]
1770 }
1771
1772 release {
1773 set patternlist [lrange $patternlist 1 end]
1774 foreach b [trd_builds $TRG(platform)] {
1775 if {$TRG(config)!="" && ![regexp "\\y$b\\y" $TRG(config)]} continue
1776 if {[regexp "\\y$b\\y" $TRG(omitconfig)]} continue
1777 set bld [add_build_job $b $TRG(testfixture)]
1778 foreach c [trd_configs $TRG(platform) $b] {
1779 add_tcl_jobs $bld $c $patternlist SHELL
1780 }
1781 add_sql_jobs $bld $patternlist
1782
1783 foreach e [trd_extras $TRG(platform) $b] {
1784 if {$e=="fuzztest"} {
1785 add_fuzztest_jobs $b $patternlist
1786 } elseif {[job_matches_any_pattern $patternlist $e]} {
1787 add_make_job $bld $e
1788 }
1789 }
1790
1791 if {[trdb one "SELECT EXISTS(SELECT 1
1792 FROM jobs WHERE depid='SHELL')"]} {
1793 set sbld [add_shell_build_job $b [lindex $bld 1] [lindex $bld 0]]
1794 set sbldid [lindex $sbld 0]
1795 trdb eval {
1796 UPDATE jobs SET depid=$sbldid WHERE depid='SHELL'
1797 }
1798 }
1799
1800 add_c_jobs $b $patternlist
1801 }
1802 }
1803
1804 c {
1805 set patternlist [lrange $patternlist 1 end]
1806 foreach b [trd_builds $TRG(platform)] {
1807 add_c_jobs $b $patternlist
1808 }
1809 }
1810
1811 list {
1812 set allperm [array names ::testspec]
1813 lappend allperm all devtest mdevtest sdevtest release list
1814 puts "Allowed values for the PERMUTATION argument: [lsort $allperm]"
1815 exit 0
1816 }
1817
1818 retest {
1819 # no-op
1820 }
1821
1822 default {
1823 must_be_testfixture
1824 if {[info exists ::testspec($first)]} {
1825 add_tcl_jobs "" $first [lrange $patternlist 1 end]
1826 if {$first=="veryquick" || $first=="full"} {
1827 add_sql_jobs "" [lrange $patternlist 1 end]
1828 }
1829 } else {
1830 add_tcl_jobs "" full $patternlist
1831 add_sql_jobs "" $patternlist
1832 }
1833 }
1834 }
1835
1836 # If the "--case DISPLAYNAME" option appears on the command-line, mark
1837 # all tests other than DISPLAYNAME as 'omit'.
1838 #
1839 if {[info exists TRG(case)] && $TRG(case) ne ""} {
1840 set jid [trdb one {
1841 SELECT jobid FROM jobs WHERE displayname GLOB $TRG(case)
1842 }]
1843 if {$jid eq ""} {
1844 puts "ERROR: No jobs match \"$TRG(case)\"."
1845 puts "The argument to --cases must GLOB match the jobs.displayname column"
1846 puts "of the testrunner.db database."
1847 trdb eval {UPDATE jobs SET state='omit'}
1848 } else {
1849 trdb eval {
1850 WITH RECURSIVE keepers(jid,did) AS (
1851 SELECT jobid,depid FROM jobs
1852 WHERE displayname GLOB $TRG(case)
1853 UNION
1854 SELECT jobid,depid FROM jobs, keepers WHERE jobid=did
1855 )
1856 DELETE FROM jobs WHERE jobid NOT IN (SELECT jid FROM keepers);
1857 }
1858 }
1859 }
1860}
1861
1862# Initializer, or reinitialize, the testrunner.db database file.
1863#
1864proc make_new_testset {} {
1865 global TRG
1866
1867 trdb eval {PRAGMA journal_mode=WAL;}
1868 r_write_db {
1869 trdb eval $TRG(schema)
1870 set nJob $TRG(nJob)
1871 set cmdline $TRG(cmdline)
1872 set tm [clock_milliseconds]
1873 trdb eval { REPLACE INTO config VALUES('njob', $nJob ); }
1874 trdb eval { REPLACE INTO config VALUES('cmdline', $cmdline ); }
1875 trdb eval { REPLACE INTO config VALUES('start', $tm ); }
1876
1877 add_jobs_from_cmdline $TRG(patternlist)
1878
1879 }
1880}
1881
1882proc mark_job_as_finished {jobid output state endtm} {
1883 set ntest 1
1884 set nerr 0
1885 if {$endtm>0} {
1886 set re {\y(\d+) errors out of (\d+) tests( on [^\n]+\n)?}
1887 if {[regexp $re $output all a b pltfm]} {
1888 set nerr $a
1889 set ntest $b
1890 } elseif {[regexp {\y(\d+) tests? run with (\d+) errors?\y} $output \
1891 all b a]} {
1892 # The summary line written by the CLI for *.sql test scripts
1893 set nerr $a
1894 set ntest $b
1895 }
1896 regexp {\ySQLite \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [0-9a-fA-F]+} \
1897 $output svers
1898 }
1899 r_write_db {
1900 if {$state=="failed"} {
1901 set childstate omit
1902 if {$nerr<=0} {set nerr 1}
1903 } else {
1904 set childstate ready
1905 }
1906 if {[info exists pltfm]} {set pltfm [string trim $pltfm]}
1907 trdb eval {
1908 UPDATE jobs
1909 SET output=$output, state=$state, endtime=$endtm, span=$endtm-starttime,
1910 ntest=$ntest, nerr=$nerr, svers=$svers, pltfm=$pltfm
1911 WHERE jobid=$jobid;
1912 UPDATE jobs SET state=$childstate
1913 WHERE depid=$jobid AND state!='halt' AND state!='done';
1914 UPDATE config SET value=value+$nerr WHERE name='nfail';
1915 UPDATE config SET value=value+$ntest WHERE name='ntest';
1916 }
1917 }
1918}
1919
1920# We want to stop running tests. Do this in an orderly way that
1921# avoids confusing Windows. (On unix we just invoke "exit 1".)
1922#
1923proc orderly_shutdown {} {
1924 global tcl_platform
1925 if {$tcl_platform(platform) ne "windows"} {
1926 # Non-Windows platform can just call exit and the OS stops everything
1927 # for us automatically.
1928 exit 1
1929 }
1930
1931 # Only windows reaches this point. Mark pending jobs as 'halt' to prevent
1932 # them from being scheduled. Let running jobs continue to completion.
1933 #
1934 set n [trdb one {SELECT count(*) FROM jobs WHERE state='running'}]
1935 trdb eval {UPDATE jobs SET state='halt' WHERE state in ('ready','')}
1936 if {$n>1} {
1937 incr n -1
1938 puts "Deferred shutdown: Waiting on $n running tasks to complete."
1939 }
1940}
1941
1942
1943proc script_input_ready {fd iJob jobid} {
1944 global TRG
1945 global O
1946 global T
1947
1948 if {[eof $fd]} {
1949 trdb eval { SELECT * FROM jobs WHERE jobid=$jobid } job {}
1950
1951 # If this job specified a directory name, then delete the run.sh/run.bat
1952 # file from it before continuing. This is because the contents of this
1953 # directory might be copied by some other job, and we don't want to copy
1954 # the run.sh file in this case.
1955 if {$job(dirname)!=""} {
1956 file delete -force [file join $job(dirname) $TRG(run)]
1957 }
1958
1959 set ::done 1
1960 fconfigure $fd -blocking 1
1961 set state "done"
1962 set rc [catch { close $fd } msg]
1963 if {$rc} {
1964 puts [format %-79.79s "FAILED: $job(displayname) ($iJob)"]
1965 set state "failed"
1966 if {$TRG(stopOnError)} {
1967 puts "Stopping due to errors in $job(displayname)..."
1968 # puts "OUTPUT: $O($iJob)"
1969 orderly_shutdown
1970 }
1971 if {$TRG(stopOnCore) && [string first {core dumped} $O($iJob)]>0} {
1972 puts "Stopping due to core dump in $job(displayname)..."
1973 # puts "OUTPUT: $O($iJob)"
1974 orderly_shutdown
1975 }
1976 }
1977
1978 set tm [clock_milliseconds]
1979 set jobtm [expr {$tm - $job(starttime)}]
1980
1981 puts $TRG(log) "### $job(displayname) ${jobtm}ms ($state)"
1982 puts $TRG(log) [string trim $O($iJob)]
1983
1984 mark_job_as_finished $jobid $O($iJob) $state $tm
1985
1986 dirs_freeDir $iJob
1987 launch_some_jobs
1988 incr ::wakeup
1989 } else {
1990 set rc [catch { gets $fd line } res]
1991 if {$rc} {
1992 puts "ERROR $res"
1993 }
1994 if {$res>=0} {
1995 append O($iJob) "$line\n"
1996 }
1997 }
1998}
1999
2000proc dirname {ii} {
2001 return "testdir$ii"
2002}
2003
2004proc launch_another_job {iJob} {
2005 global TRG
2006 global O
2007 global T
2008
2009 set testfixture $TRG(interp)
2010 set script $TRG(info_script)
2011
2012 set O($iJob) ""
2013
2014 set jobdict [r_get_next_job $iJob]
2015 if {$jobdict==""} { return 0 }
2016 array set job $jobdict
2017
2018 set dir $job(dirname)
2019 if {$dir==""} { set dir [dirname $iJob] }
2020 create_or_clear_dir $dir
2021
2022 if {$job(build)!=""} {
2023 set srcdir [file dirname $::testdir]
2024 if {$job(build)=="Zipvfs"} {
2025 set script [zipvfs_testrunner_script]
2026 } else {
2027 set bWin [expr {$TRG(platform)=="win"}]
2028 set script [trd_buildscript $job(build) $srcdir $bWin]
2029 }
2030 set fd [open [file join $dir $TRG(make)] w]
2031 puts $fd $script
2032 close $fd
2033 }
2034
2035 # Add a batch/shell file command to set the directory used for temp
2036 # files to the test's working directory. Otherwise, tests that use
2037 # large numbers of temp files (e.g. zipvfs), might generate temp
2038 # filename collisions.
2039 if {$TRG(platform)=="win"} {
2040 set set_tmp_dir "SET SQLITE_TMPDIR=[file normalize $dir]"
2041 } else {
2042 set set_tmp_dir "export SQLITE_TMPDIR=\"[file normalize $dir]\""
2043 }
2044
2045 if { $TRG(dryrun) } {
2046
2047 mark_job_as_finished $job(jobid) "" done 0
2048 dirs_freeDir $iJob
2049 if {$job(build)!=""} {
2050 puts $TRG(log) "(cd $dir ; $job(cmd) )"
2051 } else {
2052 puts $TRG(log) "$job(cmd)"
2053 }
2054
2055 } else {
2056 set pwd [pwd]
2057 cd $dir
2058 set fd [open $TRG(run) w]
2059 puts $fd $set_tmp_dir
2060 puts $fd $job(cmd)
2061 close $fd
2062 set fd [open "|$TRG(runcmd) 2>@1" r]
2063 cd $pwd
2064
2065 fconfigure $fd -blocking false -translation binary
2066 fileevent $fd readable [list script_input_ready $fd $iJob $job(jobid)]
2067 }
2068
2069 return 1
2070}
2071
2072# Show the testing progress report
2073#
2074proc progress_report {} {
2075 global TRG
2076
2077 if {$TRG(fullstatus)} {
2078 if {$::tcl_platform(platform) eq "windows"} {
2079 exec [info nameofexe] $::argv0 status --cls
2080 } else {
2081 show_status trdb 1
2082 }
2083 } else {
2084 set tmms [expr [clock_milliseconds] - $TRG(starttime)]
2085 set tm [format "%d" [expr int($tmms/1000.0 + 0.5)]]
2086
2087 set wtotal 0
2088 set wdone 0
2089 r_write_db {
2090 trdb eval {
2091 SELECT displaytype, state, count(*) AS cnt, sum(estwork) AS ew
2092 FROM jobs
2093 GROUP BY 1, 2
2094 } {
2095 set v($state,$displaytype) $cnt
2096 incr t($displaytype) $cnt
2097 incr wtotal $ew
2098 if {$state=="done" || $state=="failed" || $state=="omit"} {
2099 incr wdone $ew
2100 }
2101 }
2102 }
2103
2104 set text ""
2105 foreach j [lsort [array names t]] {
2106 foreach k {done failed running} { incr v($k,$j) 0 }
2107 set fin [expr $v(done,$j) + $v(failed,$j)]
2108 lappend text "${j}($fin/$t($j))"
2109 if {$v(failed,$j)>0} {
2110 lappend text "f$v(failed,$j)"
2111 }
2112 if {$v(running,$j)>0} {
2113 lappend text "r$v(running,$j)"
2114 }
2115 }
2116 set report "[elapsetime $tmms] [join $text { }]"
2117 if {$wdone>0 && [set pct [expr {int(($wdone*100.0)/$wtotal)}]]>=4} {
2118 set tmleft [expr {($tmms/double($wdone))*($wtotal-$wdone)}]
2119 set etc " ETC [elapsetime $tmleft]"
2120 if {[string length $report]+[string length $etc]<80} {
2121 append report $etc
2122 }
2123 # append report " $pct%"
2124 }
2125 puts -nonewline [format %-79.79s $report]\r
2126 flush stdout
2127 }
2128 after $TRG(reporttime) progress_report
2129}
2130
2131proc launch_some_jobs {} {
2132 global TRG
2133 set nJob [trdb one { SELECT value FROM config WHERE name='njob' }]
2134
2135 while {[dirs_nHelper]<$nJob} {
2136 set iDir [dirs_allocDir]
2137 if {0==[launch_another_job $iDir]} {
2138 dirs_freeDir $iDir
2139 break;
2140 }
2141 }
2142}
2143
2144proc run_testset {} {
2145 global TRG
2146 set ii 0
2147
2148 set TRG(starttime) [clock_milliseconds]
2149 set TRG(log) [open $TRG(logname) w]
2150
2151 launch_some_jobs
2152
2153 if {$TRG(fullstatus)} {puts "\033\[2J"}
2154 progress_report
2155 while {[dirs_nHelper]>0} {
2156 after 500 {incr ::wakeup}
2157 vwait ::wakeup
2158 }
2159 close $TRG(log)
2160 progress_report
2161 puts ""
2162
2163 r_write_db {
2164 set tm [clock_milliseconds]
2165 trdb eval { REPLACE INTO config VALUES('end', $tm ); }
2166 set nErr [trdb one {SELECT count(*) FROM jobs WHERE state='failed'}]
2167 if {$nErr>0} {
2168 puts "$nErr failures:"
2169 trdb eval {
2170 SELECT displayname FROM jobs WHERE state='failed'
2171 } {
2172 puts "FAILED: $displayname"
2173 }
2174 }
2175 set nOmit [trdb one {SELECT count(*) FROM jobs WHERE state='omit'}]
2176 if {$nOmit>0} {
2177 puts "$nOmit jobs skipped due to prior failures"
2178 }
2179 }
2180
2181 puts "Test database is $TRG(dbname)"
2182 puts "Test log is $TRG(logname)"
2183 if {[info exists TRG(FUZZDB)]} {
2184 puts "Extra fuzzcheck data taken from $TRG(FUZZDB)"
2185 }
2186 trdb eval {
2187 SELECT sum(ntest) AS totaltest,
2188 sum(nerr) AS totalerr
2189 FROM jobs
2190 } break
2191 trdb eval {
2192 SELECT max(endtime)-min(starttime) AS totaltime
2193 FROM jobs WHERE endtime>0
2194 } break;
2195 set et [elapsetime $totaltime]
2196 set pltfm {}
2197 trdb eval {
2198 SELECT pltfm, count(*) FROM jobs WHERE pltfm IS NOT NULL
2199 ORDER BY 2 DESC LIMIT 1
2200 } break
2201 if {$totalerr==""} {set totalerr 0}
2202 if {$totaltest==""} {set totaltest 0}
2203 puts "$totalerr errors out of $totaltest tests in $et $pltfm"
2204 trdb eval {
2205 SELECT DISTINCT substr(svers,1,79) as v1 FROM jobs WHERE svers IS NOT NULL
2206 } {puts $v1}
2207
2208 return [expr {$nErr>0}]
2209}
2210
2211# If the argument is "retest", simply rerun all tests from the previous
2212# run that are marked as one of "ready", "running", "failed", or "omit"
2213# plus redo any build of dependencies those tests.
2214#
2215proc handle_retest {} {
2216 set cnt 0
2217 if {[catch {trdb exists {SELECT jobid FROM jobs}} cnt] || $cnt==0} {
2218 puts "No test available to rerun"
2219 exit 1
2220 }
2221 trdb eval {UPDATE jobs SET state='ready'
2222 WHERE state IN ('running','failed','omit')}
2223 for {set kk 0} {$kk<2} {incr kk} {
2224 trdb eval {
2225 UPDATE jobs SET state='ready'
2226 WHERE jobid IN (SELECT depid FROM jobs WHERE state='ready');
2227 UPDATE jobs SET state='' WHERE state='ready' AND depid<>'';
2228 }
2229 }
2230}
2231
2232# Handle the --buildonly option, if it was specified.
2233#
2234proc handle_buildonly {} {
2235 global TRG
2236 if {$TRG(buildonly)} {
2237 r_write_db {
2238 trdb eval { DELETE FROM jobs WHERE displaytype!='bld' }
2239 }
2240 }
2241}
2242
2243# Handle the --explain option. Provide a human-readable
2244# explanation of all the tests that are in the trdb database jobs
2245# table.
2246#
2247proc explain_layer {indent depid} {
2248 global TRG
2249 if {$TRG(buildonly)} {
2250 set showtests 0
2251 } else {
2252 set showtests 1
2253 }
2254 trdb eval {SELECT jobid, displayname, displaytype, dirname
2255 FROM jobs WHERE depid=$depid ORDER BY displayname} {
2256 if {$displaytype=="bld"} {
2257 puts "${indent}$displayname in $dirname"
2258 explain_layer "${indent} " $jobid
2259 } elseif {$showtests} {
2260 puts "${indent}$displayname"
2261 }
2262 }
2263}
2264proc explain_tests {} {
2265 explain_layer "" ""
2266}
2267
2268set exit_status 0
2269sqlite3 trdb $TRG(dbname)
2270trdb timeout $TRG(timeout)
2271if {[llength $TRG(patternlist)]==1 && $TRG(patternlist) eq "retest"} {
2272 set tm 0
2273 handle_retest
2274} else {
2275 set tm [lindex [time { make_new_testset }] 0]
2276 if {[llength $TRG(patternlist)]>0} {
2277 r_write_db {
2278 trdb eval {
2279 DELETE FROM jobs
2280 WHERE displaytype='bld'
2281 AND NOT EXISTS(SELECT 1 FROM jobs chld WHERE chld.depid=jobs.jobid)
2282 }
2283 }
2284 }
2285}
2286if {$TRG(explain)} {
2287 explain_tests
2288} else {
2289 if {$TRG(nJob)>1} {
2290 puts "splitting work across $TRG(nJob) cores"
2291 }
2292 if {$tm>0} {
2293 puts "built testset in [expr $tm/1000]ms.."
2294 }
2295 handle_buildonly
2296 set exit_status [run_testset]
2297}
2298trdb close
2299exit $exit_status