Debian Codegraph @

easel/devkit/sqc

raw
1 #! /usr/bin/perl
2
3 # sqc ::
4 # quality control script for exercising code, regression testing,
5 # and benchmarking.
6 #
7 # Usage: sqc [options] <level> <command file> <top_builddir> <top_srcdir> \
8 # [<old_top_builddir> <old_top_srcdir>]
9 #
10 # level - an integer >= 0. Higher = more testing, more time.
11 # command file - an sqc command file. See below for format.
12 # top_builddir - top of the build directory - where executables will
13 # be searched for. In .sqc command files, any path
14 # @foo@ is looked for in ${top_builddir}/foo.
15 # top_srcdir - top of the src directory - where scripts and datafiles
16 # will be searched for. In .sqc command files, any
17 # path !foo! is looked for in ${top_srcdir}/foo.
18 # [The other two arguments are optional, and are only used if the
19 # command file contains regression tests:]
20 # old_top_builddir - top of the build tree for an old version of
21 # the same software distribution, for regression tests
22 # old_top_srcdir - top of the src tree for an old version for regression.
23 #
24 #
25 # Available options:
26 # -e - exit with status 1 when finished if any test failed, while
27 # still running all the tests. (Default is for the script to
28 # exit with status 0, having 'succeeded' by reporting any
29 # failures.) This option may also be set by setting env
30 # variable SQC_NONZERO_EXIT nonzero.
31 #
32 # -v - print verbose output, including each command
33 # after various substitutions.
34 #
35 # -x <n> - only run test number <n>, where <n> starts from 1.
36 # Useful when debugging individual failed tests - saves
37 # having to rerun the whole testsuite.
38 #
39 # -V - Run 'valgrind' test types. Caller asserts that valgrind
40 # is installed on this system. This option may also be
41 # set by setting environment variable SQC_VALGRIND nonzero.
42 #
43 # -M - Run 'mpi' test types. Caller asserts that mpiexec is
44 # installed on this system. This option may also be set
45 # by setting env variable SQC_MPI nonzero.
46 # Examples:
47 # % sqc 2 exercises.sqc .. ..
48 #
49 # (Allowing -M, -V to also be set in the environment simplifies life when
50 # we're calling sqc via a 'make check', where we don't really have control
51 # over the cmdline options, but we can control our system environment.)
52 #
53 # For each (non-prep) test in the command file, a one-line summary
54 # of the result is printed. The format of this line is:
55 # <testtype> <test #> [<test name>]... <status>
56 # where <testtype> is exercise, valgrind, mpi, regression, benchmark, or
57 # fail; <test #> is a counter, separate for each type; <test name> is
58 # the one-word name for this test; and <status> is the result of the
59 # test. The format for <status> is described later.
60 #
61 # Format of sqc file:
62 # Blank lines are ignored. Lines beginning with # are comments and
63 # are ignored. All other lines have format:
64 # <level> <type> <name> <command>
65 #
66 # level: an integer >= 0. If the sqc level is less than this test's
67 # level, the command is skipped. This allows quick, less
68 # extensive tests and long, extensive tests to be configured
69 # in one command file.
70 #
71 # type: One of the following keywords:
72 # prep, exercise, valgrind, mpi, regression, benchmark, fail
73 # See below for description of each.
74 #
75 # name: One word, <=20 characters, naming this test.
76 # Makes it easier to track down a failed test.
77 # sqc does not verify that names are unique, but it's
78 # a good idea.
79 #
80 # command: Command template to run. (remainder of line, usually
81 # more than one token/word).
82 #
83 # A command template is subjected to three types of
84 # filename substitution (@@ = executables in the build tree;
85 # !! = scripts and data in the source tree; %% = temp files
86 # managed by sqc).
87 #
88 # No token may contain the string REGRESSION; this is
89 # reserved for regression tests (see below).
90 #
91 # valgrind prefixing, regression filename substitution,
92 # path substitution, and output redirection:
93 #
94 # @@ path substitution for binaries (in build tree):
95 # Tokens enclosed in @@ in the template are interpreted
96 # as executables to be found in the build tree. The token is
97 # prefixed by ${top_builddir}.
98 #
99 # With no token at all (@@), the ${top_builddir} is
100 # substituted; this special case is used to pass the
101 # ${top_builddir} as an argument into integrated test
102 # scripts that sqc is running. This leads to an
103 # idiomatic incantation for integrated tests:
104 # !testsuite/ixx-test.pl! @@ !! %TMPPFX%
105 # by which the builddir, srcdir, and a tempfile prefix are
106 # passed along to a script, which is responsible for
107 # printing "ok" and returning 0 on success, and die()'ing
108 # on any failure.
109 #
110 # !! path substitution for scripts and data (in src tree):
111 # Tokens enclosed in !! in a command template are
112 # interpreted as scripts or datafiles to be found in the
113 # source tree. The token is prefixed by ${top_srcdir}.
114 #
115 # As above, with no token at all (!!), the ${top_srcdir} is
116 # substituted.
117
118 # %% tempfile substitution for output generated during sqc:
119 # Tokens enclosed in %% are interpreted as a tempfile to
120 # be created during the sqc run. sqc is responsible for
121 # deleting the temp file. Each token in an sqc command
122 # file maps uniquely to the same file name throughout an
123 # sqc process, so the same token may be used in more than
124 # one command; for example, a "prep" command can create a
125 # file that subsequent "exercise" commands need as input).
126 #
127 # It is a common idiom to pass a tempfile name into a test
128 # script, where the script then uses the name as a unique
129 # prefix to create a number of temp files with different
130 # suffixes, a la %TMPFILE%.1, %TMPFILE%.2, etc. Such a
131 # script cleans up the suffixed temp files it creates; sqc
132 # cleans up the original %TMPFILE%.
133 #
134 # output redirection:
135 # ">/dev/null 2> <TMPFILE>" is appended to the command
136 # template, to redirect all output away, and save
137 # STDERR diagnostics to a <TMPFILE> (for instance, valgrind
138 # report goes to this file).
139 # If the command template already includes a ">", only
140 # the STDERR redirection is done; it is assumed that the command
141 # is deliberately keeping its output (probably in its own tmpfile).
142 # If redirection is done explicitly in the command template,
143 # the template is responsible for stdout. sqc always handles
144 # stderr itself.
145 #
146 # regression filename substitution: ["regression" test types only]
147 # In a "regression" test, the same command is run twice;
148 # once under a "new" build and once under an "old" build
149 # of the same software distribution. Any tempfile tokens in
150 # the command template that start with REGRESSION (such as
151 # %REGRESSION.OUT1%) are considered to be outputs that
152 # should be absolutely identical between the old and new
153 # software versions. These are compared by "diff", and
154 # the regression test fails if the diff isn't clean.
155 #
156 #
157 # Types of tests:
158 #
159 # prep: Creates tmp files that other tests will need.
160 # If a prep command fails for any reason with nonzero
161 # exit status, sqc dies at that point.
162 # prep commands create no output lines in the sqc report.
163 #
164 # exercise: Run a command that is expected to succeed with zero
165 # exit status.
166 # Return status is tested; if nonzero, a failure is
167 # recorded. Crashes versus clean failures are reported
168 # differently in the output message.
169 # The format for the status in the output line is:
170 # ok.
171 # FAILED [command failed]
172 # FAILED [crash!]
173 #
174 # fail: Run a command that is expected to exit cleanly with
175 # *nonzero* exit status (for example, testing that a program
176 # successfully detects bad input, rather than crashing).
177 # Possible results for output line status are:
178 # ok.
179 # FAILED to fail
180 # FAILED [crash!]
181 #
182 # valgrind: Check for memory problems and leaks.
183 # Like exercise, but the command is run with "valgrind
184 # --error-exitcode=99" prepended, so valgrind runs the
185 # command.
186 #
187 # Return status is tested; 99 indicates an error detected
188 # by valgrind, other nonzero codes indicate a failure code
189 # reported by the application. The stderr that sqc
190 # captured is examined for valgrind leak reports. The
191 # format for the status in the output line is:
192 # ok.
193 # FAILED [valgrind reports error(s)]
194 # FAILED [valgrind reports leak(s)]
195 # FAILED [command failed]
196 # FAILED [crash!]
197 #
198 # Valgrind tests are only run if -V cmdline option is used,
199 # or if SQC_VALGRIND environment variable is set nonzero.
200 #
201 # mpi: An MPI-specific exercise.
202 #
203 # The command includes 'mpiexec'; for example,
204 # "mpiexec -n 2 ./my_mpi_unit_test"
205 #
206 # MPI exercises are only run if the -M cmdline option is used,
207 # or if SQC_MPI environment variable is set nonzero.
208 #
209 # Output line format is the same as for an exercise.
210 #
211 # regression: The same command template is processed and run twice;
212 # once under a "new" build (<top_builddir> and <top_srcdir>)
213 # and once under an "old" build of the same software
214 # distribution (<old_top_builddir> and <old_top_srcdir>).
215 #
216 # Any tempfile tokens in the command template that start
217 # with REGRESSION (such as %REGRESSION.OUT1%) are
218 # considered to be outputs that should be absolutely
219 # identical between the old and new software
220 # versions. These are compared by "diff", and the
221 # regression test fails if the diff isn't clean.
222 #
223 # Possible outputs:
224 # ok.
225 # FAILED [regressions differ]
226 # FAILED [new command failed]
227 # FAILED [old command failed]
228 # FAILED [new crashed!]
229 # FAILED [old crashed!]
230 #
231 # benchmark: Runs a command and measures how long it takes.
232 # The output status field is the user CPU time in seconds.
233 # Like a prep command, if a benchmark command fails, sqc
234 # dies immediately at that point.
235 #
236 #
237 ################################################################
238 # SRE, Tue Aug 6 11:16:39 2002
239 # SVN $Id: sqc 1796 2007-01-03 22:36:44Z eddys $
240
241 use Getopt::Std;
242
243 # Parse our command line options
244 #
245 getopts('evx:VM');
246 if ($opt_e) { $do_nonzero_exit = 1; }
247 if ($opt_v) { $verbose = 1; }
248 if ($opt_x) { $only_do_test_num = $opt_x; }
249 if ($opt_V) { $do_valgrind = 1; }
250 if ($opt_M) { $do_mpi = 1; }
251
252 # Parse our environment
253 #
254 if ($ENV{"SQC_VALGRIND"} != 0) { $do_valgrind = 1; }
255 if ($ENV{"SQC_MPI"} != 0) { $do_mpi = 1; }
256 if ($ENV{"SQC_NONZERO_EXIT"} != 0) { $do_nonzero_exit = 1; }
257
258 # Check software dependencies.
259 # Valgrind tests require valgrind; MPI tests require MPI. (Duh.)
260 # Some integrated tests use python3.
261 #
262 if ($do_valgrind)
263 {
264 $output = `valgrind --version 2>&1`;
265 if ($?) { die "couldn't run valgrind. Don't set -V cmdline option, don't set SQC_VALGRIND env variable.\n"; }
266 }
267 if ($do_mpi)
268 {
269 $output = `mpiexec -V 2>&1`;
270 if ($?) { die "couldn't run mpiexec. Don't set -M cmdline option, don't set SQC_MPI env variable.\n"; }
271 }
272
273 $output = `python3 --version 2>&1`;
274 if ($?) { die "Unable to run the test suite. Some tests require python3\n\n"; }
275
276
277
278 # Parse command line arguments
279 #
280 if ($#ARGV == 3)
281 {
282 $setlevel = shift;
283 $commandfile = shift;
284 $top_builddir = shift;
285 $top_srcdir = shift;
286 undef($old_builddir);
287 undef($old_srcdir);
288 }
289 elsif ($#ARGV == 5)
290 {
291 $setlevel = shift;
292 $commandfile = shift;
293 $top_builddir = shift;
294 $top_srcdir = shift;
295 $old_builddir = shift;
296 $old_srcdir = shift;
297 }
298 else
299 { die "Usage: sqc [options] <level> <commandfile> <top_builddir> <top_srcdir> [<old_top_builddir> <old_top_srcdir>]\n"; }
300
301 $tmp = &tempname;
302 $| = 1;
303
304 print "sqc: running $commandfile.\n" if $verbose;
305
306 open(COMMANDS, $commandfile) || die;
307 $nbench = $ntest = $badtest = 0;
308 $tot_benchtime_cpu = $tot_benchtime_wall = 0.;
309 $linenum = 0;
310
311 COMMAND:
312 while (<COMMANDS>) {
313 $linenum++;
314 if (/^\#/) { next; }
315 if (/^\s*$/) { next; }
316
317 chomp;
318 ($testlevel, $testtype, $testname, $cmdtemplate) = split(' ', $_, 4);
319
320 # Skip test is it's harder than our set level.
321 if ($setlevel < $testlevel) { next COMMAND; }
322
323 # Make sure it's a valid test type;
324 # print the first part of the output line.
325 #
326 if ($testtype eq "exercise" ||
327 $testtype eq "valgrind" ||
328 $testtype eq "mpi" ||
329 $testtype eq "regression" ||
330 $testtype eq "fail" ||
331 $testtype eq "benchmark")
332 {
333 # In verbose mode, if any test calls for valgrind but we're not set up to run
334 # valgrind, issue one warning. Ditto for mpi tests. It's easy to forget
335 # the need for -V/SQC_VALGRIND, or -M/SQC_MPI.
336 if ($verbose && $testtype eq "valgrind" && ! do_valgrind && $valgrind_ntests == 0)
337 { print ("[WARNING: valgrind commands not running: use -V or export SQC_VALGRIND=1 to enable valgrind tests]"); }
338 if ($verbose && $testtype eq "mpi" && ! do_mpi && $mpi_ntests == 0)
339 { print ("[WARNING: mpi tests present but not run: use -M or export SQC_MPI=1 to enable mpi tests]"); }
340
341 if ($testtype eq "valgrind") { $valgrind_ntests++; if (! $do_valgrind) { next COMMAND; } }
342 if ($testtype eq "mpi") { $mpi_ntests++; if (! $do_mpi) { next COMMAND; } }
343
344
345
346 $ntest++;
347 if ($only_do_test_num && $only_do_test_num != $ntest) { next COMMAND; }
348 printf(" %10s %4d [%21s] ... ", $testtype, $ntest, $testname);
349 }
350 elsif ($testtype ne "prep")
351 {
352 die "No such test type $testtype at line $linenum of command file\n";
353 }
354
355 print "sqc: evaluating line: $_\n" if $verbose;
356
357 # Filename substitution.
358 $cmd = $cmdtemplate;
359
360 ($status, $cmd) = &build_substitution($tmp, $cmd, $top_builddir);
361 next COMMAND if &check_status($status, $testname, $testtype, "FAILED [@@ substitution]");
362
363 ($status, $cmd) = &src_substitution($tmp, $cmd, $top_srcdir);
364 next COMMAND if &check_status($status, $testname, $testtype, "FAILED [!! substitution]");
365
366 $cmd = &tempfile_substitution($tmp, $cmd);
367
368 print "sqc: after filename subst, cmd is: $cmd\n" if $verbose;
369
370 # Valgrind prefixing.
371 $cmd = "valgrind --error-exitcode=99 $cmd" if ($testtype eq "valgrind");
372
373 # Regression substitutions, $cmd splits into $cmd (new) and $cmd2 (old)
374 if ($testtype eq "regression") {
375 $cmd2 = $cmdtemplate;
376
377 die ("FAILED;\nno <old_builddir> argument on command line; can't run regression tests\n") if (! defined($old_builddir));
378 die ("FAILED;\nno <old_srcdir> argument on command line; can't run regression tests\n") if (! defined($old_srcdir));
379
380 ($status, $cmd2) = &build_substitution($tmp, $cmd2, $old_builddir);
381 next COMMAND if &check_status($status, $testname, $testtype, "FAILED [@@ substitution]");
382
383 ($status, $cmd2) = &src_substitution($tmp, $cmd2, $old_srcdir);
384 next COMMAND if &check_status($status, $testname, $testtype, "FAILED [!! substitution]");
385
386 $cmd2 = &tempfile_substitution($tmp, $cmd2);
387
388 ($nregressions, $cmd, $cmd2) = &regression_substitution($tmp, $cmd, $cmd2);
389
390 print "sqc: after filename subst, regressed new cmd is: $cmd\n" if $verbose;
391 print "sqc: after filename subst, regressed old cmd is: $cmd2\n" if $verbose;
392 }
393
394 # Output redirection substitution.
395 # stdout is sent to /dev/null unless command already is handling it.
396 # stderr is saved in a tmp file.
397 # (stderr from the old cmd2 of a regression test is sent to /dev/null)
398 #
399 if ($cmd !~ />/) {
400 $cmd = "$cmd > /dev/null";
401 $cmd2 = "$cmd2 > /dev/null" if ($testtype eq "regression");
402 }
403 $cmd = "$cmd 2> $tmp.stderr";
404 $cmd2 = "$cmd2 2> /dev/null" if ($testtype eq "regression");
405 print "sqc: after output subst, cmd is: $cmd\n" if $verbose;
406
407 # Run the commands;
408 # collect exit status in status1;
409 # additionally, for regression test, old executable's status is in status2.
410 #
411 # If our command fails "cleanly" it has an exit code of 1 by Easel convention.
412 # Because of the way Perl handles status codes, this becomes 1<<8 = 256.
413 # All other nonzero codes are called "crashes" because they probably are.
414 #
415 # The `(true; $cmd)` stuff is because a segfault gets reported by shell,
416 # not the process itself; we want to redirect that output too.
417 $startwall = time;
418 $startcpu = (times)[2];
419 print "sqc: running cmd: $cmd\n" if $verbose;
420 `(true; $cmd) 2> /dev/null > /dev/null`;
421 $status1 = $?;
422 print "sqc: return status $status1\n" if $verbose;
423 if ($testtype eq "regression") {
424 `(true; $cmd2) 2> /dev/null > /dev/null`;
425 $status2 = $?;
426 }
427 $stopwall = time;
428 $stopcpu = (times)[2];
429
430 # Deal with exit status and output.
431 if ($testtype eq "prep")
432 {
433 if ($status1 != 0) { die "prep command [$testname] at line $linenum failed with status $status1\n"; }
434 }
435
436 elsif ($testtype eq "exercise" || $testtype eq "mpi")
437 {
438 if (($status1>>8) == 1) { print "FAILED [command failed]\n"; $badtest++; }
439 elsif ( $status1 != 0) { print "FAILED [crash!]\n"; $badtest++; }
440 else { print "ok.\n"; }
441 }
442
443 elsif ($testtype eq "valgrind")
444 {
445 ($has_valgrind, $has_leak) = &check_valgrind_status("$tmp.stderr");
446 if ($has_valgrind == 0) { print "FAILED [no valgrind output?]\n"; $badtest++; }
447 elsif (($status1>>8) == 99) { print "FAILED [valgrind reports error(s)]\n"; $badtest++; }
448 elsif (($status1>>8) == 1) { print "FAILED [command failed]\n"; $badtest++; }
449 elsif ( $status1 != 0) { print "FAILED [crash!]\n"; $badtest++; }
450 elsif ($has_leak) { print "FAILED [valgrind reports leak(s)]\n"; $badtest++; }
451 else { print "ok.\n"; }
452 # Important to check for leaks last above. On app failure, we're allowed to return a nonzero
453 # exit code without scrupulously free'ing everything.
454 }
455
456
457 elsif ($testtype eq "regression")
458 {
459 if (($status1>>8) == 1) { print "FAILED [new command failed]\n"; $badtest++; }
460 elsif (($status2>>8) == 1) { print "FAILED [old command failed]\n"; $badtest++; }
461 elsif ( $status1 != 0) { print "FAILED [new crashed!]\n"; $badtest++; }
462 elsif ( $status2 != 0) { print "FAILED [old crashed!]\n"; $badtest++; }
463 else
464 {
465 $nregress_failed = 0;
466 for ($i = 0; $i < $nregressions; $i++)
467 {
468 system("diff $tmp.REGRESSION.old.$i $tmp.REGRESSION.new.$i > /dev/null");
469 if ($? != 0) { $nregress_failed++; }
470 }
471 if ($nregress_failed > 0) { print "FAILED [regressions differ]\n"; $badtest++; }
472 else { print "ok.\n"; }
473 }
474 }
475
476 elsif ($testtype eq "benchmark")
477 {
478 if ($status1 != 0) {
479 die "benchmark at line $linenum failed with status $status1\n";
480 }
481 $cpu_elapsed = $stopcpu - $startcpu;
482 $wall_elapsed = $stopwall - $startwall;
483 printf "%6.1f cpu %4d wall\n", $cpu_elapsed, $wall_elapsed;
484 $tot_benchtime_cpu += $cpu_elapsed;
485 $tot_benchtime_wall += $wall_elapsed;
486 $nbench++;
487 }
488
489 elsif ($testtype eq "fail")
490 {
491 if (($status1>>8) == 0) { print "FAILED [0 status]\n"; $badtest++; }
492 elsif (($status1>>8) != 1) { print "FAILED [crash!]\n"; $badtest++; }
493 else { print "ok.\n"; }
494 }
495
496 last if ($only_do_test_num && $ntest == $only_do_test_num );
497
498 }
499
500 # Summarize output.
501 if ($badtest > 0) { print "\n$badtest of $ntest exercises at level <= $setlevel FAILED.\n"; }
502 else { print "\nAll $ntest exercises at level <= $setlevel passed.\n"; }
503
504 if ($nbench > 0) {
505 printf "\nTotal of %d benchmarks: %.1f cpu %d wall\n",
506 $nbench, $tot_benchtime_cpu, $tot_benchtime_wall;
507 }
508
509
510 # Print info on system, date, etc.
511 #
512 print "\n\nSystem information:\n";
513 print `date`;
514 print `uname -a`;
515
516
517 # Clean up and exit
518 #
519 foreach $tmpfile (keys(%used_tmpfile)) {
520 unlink $tmpfile if -e $tmpfile;
521 }
522 unlink $tmp if -e $tmp;
523 unlink "$tmp.stderr" if -e "$tmp.stderr";
524
525 if ($do_nonzero_exit && $badtest > 0) { exit 1; } else { exit 0; }
526
527
528
529
530
531 sub check_status
532 {
533 my ($status, $testname, $testtype, $errmsg) = @_;
534 if ($status != 0)
535 {
536 if ($testtype eq "prep") { die "fatal: prep command [$testname]: $errmsg\n"; }
537 print $errmsg . "\n";
538 $badtest++;
539 }
540 ($status);
541 }
542
543
544 # build_substitution(tmppfx, cmd, builddir)
545 #
546 # Perform @@ substitutions for builddir;
547 # check that token exists and is executable.
548 #
549 # Return (status, cmd).
550 # status != 0 means token doesn't exist or isn't executable.
551 #
552 sub build_substitution
553 {
554 my ($tmp, $cmd, $builddir) = @_;
555 my ($status, $token, $newname);
556
557 $status = 0;
558 $cmd =~ s/\@\@/$builddir/g; # special case of "@@" replacement
559 while ($cmd =~ /\@(\S+?)\@/)
560 {
561 $token = $1;
562 $newname = "$builddir/$token";
563 if (! -x $newname) { $status = 1; }
564 $cmd =~ s/\@$token\@/$newname/g;
565 }
566 ($status, $cmd);
567 }
568
569
570
571 # src_substitution(tmppfx, cmd, srcdir)
572 #
573 # Perform !! substitutions for srcdir;
574 # check that token exists.
575 #
576 # Return (status, cmd).
577 # status != 0 means token didn't exist.
578 #
579 sub src_substitution
580 {
581 my ($tmp, $cmd, $srcdir) = @_;
582 my ($status, $token, $newname);
583
584 $status = 0;
585 $cmd =~ s/\!\!/$srcdir/g; # special case of "!!" replacement
586 while ($cmd =~ /\!(\S+?)\!/)
587 {
588 $token = $1;
589 $newname = "$srcdir/$token";
590 if (! -e $newname) { $status = 1; }
591 $cmd =~ s/\!$token\!/$newname/g;
592 }
593 ($status, $cmd);
594 }
595
596
597 # tempfile_substitution(tmpprefix, command_template)
598 #
599 # Uses a global, %used_tmpfile, which is a hash
600 # that is TRUE for each tmpfile names that we'll
601 # try to delete upon exit.
602 sub tempfile_substitution
603 {
604 my ($tmp, $com) = @_;
605 my ($token, $newname);
606
607 while ($com =~ /%(\S+?)%/ && $1 !~ /^REGRESSION/) {
608 $token = $1;
609 $newname = "$tmp.$token";
610 $com =~ s/%$token%/$newname/g;
611 $used_tmpfile{$newname} = 1;
612 }
613 return $com;
614 }
615
616 # regression_substitution(tmpprefix, new_command, old_command)
617 #
618 # Any tempfile token that starts with REGRESSION is
619 # now substituted by <tmpppfx>.REGRESSION.{new,old}.{n},
620 # where {n} is an index {0..n-1} for <n> regression
621 # tmpfiles in the command template (usually 1).
622 #
623 # Unlike normal tmpfile substitution, these constructed names
624 # ignore the exact %token% used by the command template;
625 # but there is no possibility of name clash, because the
626 # entire token namespace prefixed by REGRESSION is reserved.
627 #
628 # The reason to do it this way is that now the caller can
629 # enumerate (and diff) all regression files just by knowing <n>.
630 #
631 # Returns (<n>, <cmd1>, <cmd2>).
632 #
633 sub regression_substitution
634 {
635 my ($tmp, $cmd1, $cmd2) = @_;
636 my ($token, $newname1, $newname2, $n);
637
638 $n = 0;
639 while ($cmd1 =~ /%(REGRESSION\S*?)%/)
640 {
641 $token = $1;
642 $newname1 = "$tmp.REGRESSION.new.$n";
643 $newname2 = "$tmp.REGRESSION.old.$n";
644 $cmd1 =~ s/%$token%/$newname1/g;
645 $cmd2 =~ s/%$token%/$newname2/g;
646 $used_tmpfile{$newname1} = 1;
647 $used_tmpfile{$newname2} = 1;
648 $n++;
649 }
650 return ($n, $cmd1, $cmd2);
651 }
652
653
654 # Function: check_valgrind_status
655 #
656 # Look at a file containing stderr from an executed command.
657 # Find valgrind report, and look for a memory leak report.
658 #
659 # You'll see below that code for checking "still reachable" has been
660 # commented out. System libraries may do harmless global allocations
661 # without deallocating, and these show up as "still reachable", yet
662 # are outside our control. This is especially noticeable on Mac OS/X.
663 #
664 # Return ($has_valgrind, $has_leak):
665 # $has_valgrind: 1 if report is present; else 0
666 # $has_leak: 1 if valgrind report shows an unsuppressed memory leak.
667 #
668 # If $file isn't present at all, returns (0,0).
669 #
670 sub check_valgrind_status
671 {
672 my ($file) = @_;
673 my ($has_valgrind, $has_leak);
674
675 open(VALGRIND, "$file") || return (0,0);
676 $has_valgrind = $has_leak = 0;
677 while (<VALGRIND>) {
678 if (/^==\d+== Memcheck/) { $has_valgrind = 1; }
679 if (/^==\d+== definitely lost: (\d+)/ && $1 != 0) { $has_leak = 1; }
680 if (/^==\d+== indirectly lost: (\d+)/ && $1 != 0) { $has_leak = 1; }
681 if (/^==\d+== possibly lost: (\d+)/ && $1 != 0) { $has_leak = 1; }
682 # if (/^==\d+== still reachable: (\d+)/ && $1 != 0) { $has_leak = 1; }
683 }
684 close VALGRIND;
685
686 if (! $has_valgrind) { $has_leak = 0; }
687
688 return ($has_valgrind, $has_leak);
689 }
690
691 # Function: tempname
692 #
693 # Returns a unique temporary filename.
694 #
695 # Should be robust. Uses the pid as part of the temp name
696 # to prevent other processes from clashing. A two-letter
697 # code is also added, so a given process can request
698 # up to 676 temp file names (26*26). An "esltmp" code is
699 # also added to distinguish these temp files from those
700 # made by other programs.
701 #
702 # Returns nothing if it fails to get a temp file name.
703 #
704 # If TMPDIR is set, that directory is prepended to the
705 # name.
706 #
707 sub tempname {
708 my ($dir, $name, $suffix);
709 if ($TMPDIR) { $dir = $TMPDIR."/"; } else {$dir = "";}
710
711 foreach $suffix ("aa".."zz") {
712 $name = "$dir"."esltmp".$suffix.$$;
713 if (! (-e $name)) {
714 open(TMP,">$name") || die; # Touch it to reserve it.
715 close(TMP);
716 return "$name";
717 }
718 }
719 }
720
721