Include the new docs directory, whenever it gets added. www is gone
[oota-llvm.git] / utils / NightlyTest.pl
1 #!/usr/dcs/software/supported/bin/perl -w
2 #
3 # Program:  NightlyTest.pl
4 #
5 # Synopsis: Perform a series of tests which are designed to be run nightly.
6 #           This is used to keep track of the status of the LLVM tree, tracking
7 #           regressions and performance changes.  This generates one web page a
8 #           day which can be used to access this information.
9 #
10 # Syntax:   NightlyTest.pl <CVSRootDir> <BuildDir> <WebDir>
11 #
12 use POSIX qw(strftime);
13
14 my $HOME = $ENV{HOME};
15 my $CVSRootDir = "/home/vadve/vadve/Research/DynOpt/CVSRepository";
16 my $BuildDir   = "$HOME/buildtest";
17 my $WebDir     = "$HOME/cvs/testresults-X86";
18
19 # Calculate the date prefix...
20 @TIME = localtime;
21 my $DATE = sprintf "%4d-%02d-%02d", $TIME[5]+1900, $TIME[4]+1, $TIME[3];
22 my $DateString = strftime "%B %d, %Y", localtime;
23
24 sub ReadFile {
25   undef $/;
26   if (open (FILE, $_[0])) {
27     my $Ret = <FILE>;
28     close FILE;
29     return $Ret;
30   } else {
31     print "Could not open file '$_[0]' for reading!";
32     return "";
33   }
34 }
35
36 sub WriteFile {  # (filename, contents)
37   open (FILE, ">$_[0]") or die "Could not open file '$_[0]' for writing!";
38   print FILE $_[1];
39   close FILE;
40 }
41
42 sub GetRegex {   # (Regex with ()'s, value)
43   $_[1] =~ /$_[0]/m;
44   if (defined($1)) {
45     return $1;
46   }
47   return "?";
48 }
49
50 sub AddPreTag {  # Add pre tags around nonempty list, or convert to "none"
51   $_ = shift;
52   if (length) { return "<ul><pre>$_</pre></ul>"; } else { "<b>none</b><br>"; }
53 }
54
55 sub GetDir {
56   my $Suffix = shift;
57   opendir DH, $WebDir;
58   my @Result = reverse sort grep !/$DATE/, grep /[-0-9]+$Suffix/, readdir DH;
59   closedir DH;
60   return @Result;
61 }
62
63 # DiffFiles - Diff the current version of the file against the last version of
64 # the file, reporting things added and removed.  This is used to report, for
65 # example, added and removed warnings.  This returns a pair (added, removed)
66 #
67 sub DiffFiles {
68   my $Suffix = shift;
69   my @Others = GetDir $Suffix;
70   if (@Others == 0) {  # No other files?  We added all entries...
71     return (`cat $WebDir/$DATE$Suffix`, "");
72   }
73   # Diff the files now...
74   my @Diffs = split "\n", `diff $WebDir/$DATE$Suffix $WebDir/$Others[0]`;
75   my $Added   = join "\n", grep /^</, @Diffs;
76   my $Removed = join "\n", grep /^>/, @Diffs;
77   $Added =~ s/^< //gm;
78   $Removed =~ s/^> //gm;
79   return ($Added, $Removed);
80 }
81
82 # FormatTime - Convert a time from 1m23.45 into 83.45
83 sub FormatTime {
84   my $Time = shift;
85   if ($Time =~ m/([0-9]+)m([0-9.]+)/) {
86     $Time = sprintf("%7.4f", $1*60.0+$2);
87   }
88   return $Time;
89 }
90
91
92 # Command line argument settings...
93 my $NOCHECKOUT = 0;
94 my $NOREMOVE   = 0;
95 my $NOTEST     = 0;
96 my $NORUNNINGTESTS = 0;
97 my $MAKEOPTS   = "";
98
99 # Parse arguments...
100 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
101   shift;
102   last if /^--$/;  # Stop processing arguments on --
103
104   # List command line options here...
105   if (/^-nocheckout$/)     { $NOCHECKOUT = 1; next; }
106   if (/^-noremove$/)       { $NOREMOVE   = 1; next; }
107   if (/^-notest$/)         { $NOTEST     = 1; $NORUNNINGTESTS = 1; next; }
108   if (/^-norunningtests$/) { $NORUNNINGTESTS = 1; next; }
109   if (/^-parallel$/)       { $MAKEOPTS   = "-j2 -l3.0"; next; }
110
111   print "Unknown option: $_ : ignoring!\n";
112 }
113
114 die "Must specify 0 or 3 options!" if (@ARGV != 0 and @ARGV != 3);
115
116 if (@ARGV == 3) {
117   $CVSRootDir = $ARGV[0];
118   $BuildDir   = $ARGV[1];
119   $WebDir     = $ARGV[2];
120 }
121
122 my $Template = "$BuildDir/llvm/utils/NightlyTestTemplate.html";
123 my $Prefix = "$WebDir/$DATE";
124
125 if (0) {
126   print "CVS Root = $CVSRootDir\n";
127   print "BuildDir = $BuildDir\n";
128   print "WebDir   = $WebDir\n";
129   print "Prefix   = $Prefix\n";
130 }
131
132
133 #
134 # Create the CVS repository directory
135 #
136 if (!$NOCHECKOUT) {
137   mkdir $BuildDir or die "Could not create CVS checkout directory $BuildDir!";
138 }
139 chdir $BuildDir or die "Could not change to CVS checkout directory $BuildDir!";
140
141
142 #
143 # Check out the llvm tree, saving CVS messages to the cvs log...
144 #
145 system "(time -p cvs -d $CVSRootDir co llvm) > $Prefix-CVS-Log.txt 2>&1"
146   if (!$NOCHECKOUT);
147
148 chdir "llvm" or die "Could not change into llvm directory!";
149
150 system "cvs up -P -d > /dev/null 2>&1" if (!$NOCHECKOUT);
151
152 # Read in the HTML template file...
153 my $TemplateContents = ReadFile $Template;
154
155
156 #
157 # Get some static statistics about the current state of CVS
158 #
159 my $CVSCheckoutTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-CVS-Log.txt`;
160 my $NumFilesInCVS = `grep '^U' $Prefix-CVS-Log.txt | wc -l` + 0;
161 my $NumDirsInCVS  = `grep '^cvs checkout' $Prefix-CVS-Log.txt | wc -l` + 0;
162 $LOC = GetRegex "([0-9]+) +total", `wc -l \`utils/getsrcs.sh\` | grep total`;
163
164 #
165 # Build the entire tree, saving build messages to the build log
166 #
167 if (!$NOCHECKOUT) {
168   system "(time -p ./configure --enable-jit --enable-spec --with-objroot=.) > $Prefix-Build-Log.txt 2>&1";
169
170   # Build the entire tree, capturing the output into $Prefix-Build-Log.txt
171   system "(time -p gmake $MAKEOPTS) >> $Prefix-Build-Log.txt 2>&1";
172 }
173
174
175 sub GetRegexNum {
176   my ($Regex, $Num, $Regex2, $File) = @_;
177   my @Items = split "\n", `grep '$Regex' $File`;
178   return GetRegex $Regex2, $Items[$Num];
179 }
180
181 #
182 # Get some statistics about the build...
183 #
184 my @Linked = split '\n', `grep Linking $Prefix-Build-Log.txt`;
185 my $NumExecutables = scalar(grep(/executable/, @Linked));
186 my $NumLibraries   = scalar(grep(!/executable/, @Linked));
187 my $NumObjects     = `grep '^Compiling' $Prefix-Build-Log.txt | wc -l` + 0;
188
189 my $ConfigTimeU = GetRegexNum "^user", 0, "([0-9.]+)", "$Prefix-Build-Log.txt";
190 my $ConfigTimeS = GetRegexNum "^sys", 0, "([0-9.]+)", "$Prefix-Build-Log.txt";
191 my $ConfigTime  = $ConfigTimeU+$ConfigTimeS;  # ConfigTime = User+System
192 my $ConfigWallTime = GetRegexNum "^real", 0,"([0-9.]+)","$Prefix-Build-Log.txt";
193
194 my $BuildTimeU = GetRegexNum "^user", 1, "([0-9.]+)", "$Prefix-Build-Log.txt";
195 my $BuildTimeS = GetRegexNum "^sys", 1, "([0-9.]+)", "$Prefix-Build-Log.txt";
196 my $BuildTime  = $BuildTimeU+$BuildTimeS;  # BuildTime = User+System
197 my $BuildWallTime = GetRegexNum "^real", 1, "([0-9.]+)","$Prefix-Build-Log.txt";
198
199 my $BuildError = "";
200 if (`grep '^gmake[^:]*: .*Error' $Prefix-Build-Log.txt | wc -l` + 0 ||
201     `grep '^gmake: \*\*\*.*Stop.' $Prefix-Build-Log.txt | wc -l`+0) {
202   $BuildError = "<h3><font color='red'>Build error: compilation " .
203                 "<a href=\"$DATE-Build-Log.txt\">aborted</a></font></h3>";
204 }
205
206 #
207 # Get warnings from the build
208 #
209 my @Warn = split "\n", `egrep 'warning:|Entering dir' $Prefix-Build-Log.txt`;
210 my @Warnings;
211 my $CurDir = "";
212
213 foreach $Warning (@Warn) {
214   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
215     $CurDir = $1;                 # Keep track of directory warning is in...
216     if ($CurDir =~ m#$BuildDir/llvm/(.*)#) { # Remove buildir prefix if included
217       $CurDir = $1;
218     }
219   } else {
220     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
221   }
222 }
223 my $WarningsFile =  join "\n", @Warnings;
224 my $WarningsList = AddPreTag $WarningsFile;
225 $WarningsFile =~ s/:[0-9]+:/::/g;
226
227 # Emit the warnings file, so we can diff...
228 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
229 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
230 $WarningsAdded = AddPreTag $WarningsAdded;
231 $WarningsRemoved = AddPreTag $WarningsRemoved;
232
233
234 #
235 # Get some statistics about CVS commits over the current day...
236 #
237 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
238 #print join "\n", @CVSHistory; print "\n";
239
240 # Extract some information from the CVS history... use a hash so no duplicate
241 # stuff is stored.
242 my (%AddedFiles, %ModifiedFiles, %RemovedFiles, %UsersCommitted, %UsersUpdated);
243
244 my $DateRE = "[-:0-9 ]+\\+[0-9]+";
245
246 # Loop over every record from the CVS history, filling in the hashes.
247 foreach $File (@CVSHistory) {
248   my ($Type, $Date, $UID, $Rev, $Filename);
249   if ($File =~ /([AMRUGC]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)/) {
250     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
251   } elsif ($File =~ /([W]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+)/) {
252     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
253   } elsif ($File =~ /([O]) ($DateRE) ([^ ]+) +([^ ]+)/) {
254     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "$4/");
255   } else {
256     print "UNMATCHABLE: $File\n";
257   }
258   # print "$File\nTy = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
259
260   if ($Filename =~ /^llvm/) {
261     if ($Type eq 'M') {        # Modified
262       $ModifiedFiles{$Filename} = 1;
263       $UsersCommitted{$UID} = 1;
264     } elsif ($Type eq 'A') {   # Added
265       $AddedFiles{$Filename} = 1;
266       $UsersCommitted{$UID} = 1;
267     } elsif ($Type eq 'R') {   # Removed
268       $RemovedFiles{$Filename} = 1;
269       $UsersCommitted{$UID} = 1;
270     } else {
271       $UsersUpdated{$UID} = 1;
272     }
273   }
274 }
275
276 my $UserCommitList = join "\n", keys %UsersCommitted;
277 my $UserUpdateList = join "\n", keys %UsersUpdated;
278 my $AddedFilesList = AddPreTag join "\n", sort keys %AddedFiles;
279 my $ModifiedFilesList = AddPreTag join "\n", sort keys %ModifiedFiles;
280 my $RemovedFilesList = AddPreTag join "\n", sort keys %RemovedFiles;
281
282 my $TestError = 1;
283 my $SingleSourceProgramsTable;
284 my $MultiSourceProgramsTable;
285 my $ExternalProgramsTable;
286
287
288 sub TestDirectory {
289   my $SubDir = shift;
290
291   chdir "test/Programs/$SubDir" or
292     die "Could not change into test/Programs/$SubDir testdir!";
293
294   # Run the programs tests... creating a report.nightly.html file
295   if (!$NOTEST) {
296     system "gmake $MAKEOPTS report.nightly.html TEST=nightly "
297          . "> $Prefix-$SubDir-ProgramTest.txt 2>&1";
298   } else {
299     system "gunzip $Prefix-$SubDir-ProgramTest.txt.gz";
300   }
301
302   my $ProgramsTable;
303   if (`grep '^gmake[^:]: .*Error' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0){
304     $TestError = 1;
305     $ProgramsTable = "<font color=white><h2>Error running tests!</h2></font>";
306   } elsif (`grep '^gmake[^:]: .*No rule to make target' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0) {
307     $TestError = 1;
308     $ProgramsTable =
309       "<font color=white><h2>Makefile error running tests!</h2></font>";
310   } else {
311     $TestError = 0;
312     $ProgramsTable = ReadFile "report.nightly.html";
313
314     #
315     # Create a list of the tests which were run...
316     #
317     system "egrep 'TEST-(PASS|FAIL)' < $Prefix-$SubDir-ProgramTest.txt "
318          . "| sort > $Prefix-$SubDir-Tests.txt";
319   }
320
321   # Compress the test output
322   system "gzip -f $Prefix-$SubDir-ProgramTest.txt";
323   chdir "../../.." or die "Cannot return to parent directory!";
324   return $ProgramsTable;
325 }
326
327 # If we build the tree successfully, run the nightly programs tests...
328 if ($BuildError eq "") {
329   $SingleSourceProgramsTable = TestDirectory("SingleSource");
330   $MultiSourceProgramsTable = TestDirectory("MultiSource");
331   $ExternalProgramsTable = TestDirectory("External");
332   system "cat $Prefix-SingleSource-Tests.txt $Prefix-MultiSource-Tests.txt ".
333          " $Prefix-External-Tests.txt | sort > $Prefix-Tests.txt";
334 }
335
336 my ($TestsAdded, $TestsRemoved, $TestsFixed, $TestsBroken) = ("","","","");
337
338 if ($TestError) {
339   $TestsAdded   = "<b>error testing</b><br>";
340   $TestsRemoved = "<b>error testing</b><br>";
341   $TestsFixed   = "<b>error testing</b><br>";
342   $TestsBroken  = "<b>error testing</b><br>";
343 } else {
344   my ($RTestsAdded, $RTestsRemoved) = DiffFiles "-Tests.txt";
345
346   my @RawTestsAddedArray = split '\n', $RTestsAdded;
347   my @RawTestsRemovedArray = split '\n', $RTestsRemoved;
348
349   my %OldTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
350     @RawTestsRemovedArray;
351   my %NewTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
352     @RawTestsAddedArray;
353
354   foreach $Test (keys %NewTests) {
355     if (!exists $OldTests{$Test}) {  # TestAdded if in New but not old
356       $TestsAdded = "$TestsAdded$Test\n";
357     } else {
358       if ($OldTests{$Test} =~ /TEST-PASS/) {  # Was the old one a pass?
359         $TestsBroken = "$TestsBroken$Test\n";  # New one must be a failure
360       } else {
361         $TestsFixed = "$TestsFixed$Test\n";    # No, new one is a pass.
362       }
363     }
364   }
365   foreach $Test (keys %OldTests) {  # TestRemoved if in Old but not New
366     $TestsRemoved = "$TestsRemoved$Test\n" if (!exists $NewTests{$Test});
367   }
368
369   $TestsAdded   = AddPreTag $TestsAdded;
370   $TestsRemoved = AddPreTag $TestsRemoved;
371   $TestsFixed   = AddPreTag $TestsFixed;
372   $TestsBroken  = AddPreTag $TestsBroken;
373 }
374
375 # If we built the tree successfully, runs of the Olden suite with
376 # LARGE_PROBLEM_SIZE on so that we can get some "running" statistics.
377 if ($BuildError eq "") {
378   my ($NATTime, $CBETime, $LLCTime, $JITTime, $OptTime, $BytecodeSize,
379       $MachCodeSize) = ("","","","","","","");
380   if (!$NORUNNINGTESTS) {
381     chdir "test/Programs/MultiSource/Benchmarks/Olden" or die "Olden tests moved?";
382
383     # Clean out previous results...
384     system "gmake $MAKEOPTS clean > /dev/null 2>&1";
385
386     # Run the nightly test in this directory, with LARGE_PROBLEM_SIZE enabled!
387     system "gmake $MAKEOPTS report.nightly.raw.out TEST=nightly " .
388            " LARGE_PROBLEM_SIZE=1 > /dev/null 2>&1";
389     system "cp report.nightly.raw.out $Prefix-Olden-tests.txt";
390   } else {
391     system "gunzip $Prefix-Olden-tests.txt.gz";
392   }
393
394   # Now we know we have $Prefix-Olden-tests.txt as the raw output file.  Split
395   # it up into records and read the useful information.
396   my @Records = split />>> ========= /, ReadFile "$Prefix-Olden-tests.txt";
397   shift @Records;  # Delete the first (garbage) record
398
399   # Loop over all of the records, summarizing them into rows for the running
400   # totals file.
401   my $WallTimeRE = "[A-Za-z0-9.: ]+\\(([0-9.]+) wall clock";
402   foreach $Rec (@Records) {
403     my $rNATTime = GetRegex 'TEST-RESULT-nat-time: real\s*([.0-9m]+)', $Rec;
404     my $rCBETime = GetRegex 'TEST-RESULT-cbe-time: real\s*([.0-9m]+)', $Rec;
405     my $rLLCTime = GetRegex 'TEST-RESULT-llc-time: real\s*([.0-9m]+)', $Rec;
406     my $rJITTime = GetRegex 'TEST-RESULT-jit-time: real\s*([.0-9m]+)', $Rec;
407     my $rOptTime = GetRegex "TEST-RESULT-compile: $WallTimeRE", $Rec;
408     my $rBytecodeSize = GetRegex 'TEST-RESULT-compile: *([0-9]+)', $Rec;
409     my $rMachCodeSize = GetRegex 'TEST-RESULT-jit-machcode: *([0-9]+).*bytes of machine code', $Rec;
410
411     $NATTime .= " " . FormatTime($rNATTime);
412     $CBETime .= " " . FormatTime($rCBETime);
413     $LLCTime .= " " . FormatTime($rLLCTime);
414     $JITTime .= " " . FormatTime($rJITTime);
415     $OptTime .= " $rOptTime";
416     $BytecodeSize .= " $rBytecodeSize";
417     $MachCodeSize .= " $rMachCodeSize";
418   }
419
420   # Now that we have all of the numbers we want, add them to the running totals
421   # files.
422   AddRecord($NATTime, "running_Olden_nat_time.txt");
423   AddRecord($CBETime, "running_Olden_cbe_time.txt");
424   AddRecord($LLCTime, "running_Olden_llc_time.txt");
425   AddRecord($JITTime, "running_Olden_jit_time.txt");
426   AddRecord($OptTime, "running_Olden_opt_time.txt");
427   AddRecord($BytecodeSize, "running_Olden_bytecode.txt");
428   AddRecord($MachCodeSize, "running_Olden_machcode.txt");
429
430   system "gzip -f $Prefix-Olden-tests.txt";
431 }
432
433
434
435
436 #
437 # Get a list of the previous days that we can link to...
438 #
439 my @PrevDays = map {s/.html//; $_} GetDir ".html";
440
441 splice @PrevDays, 20;  # Trim down list to something reasonable...
442
443 my $PrevDaysList =     # Format list for sidebar
444   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
445
446 #
447 # Start outputing files into the web directory
448 #
449 chdir $WebDir or die "Could not change into web directory!";
450
451 # Add information to the files which accumulate information for graphs...
452 AddRecord($LOC, "running_loc.txt");
453 AddRecord($BuildTime, "running_build_time.txt");
454
455 #
456 # Rebuild the graphs now...
457 #
458 system "/usr/dcs/software/supported/bin/gnuplot " .
459        "$BuildDir/llvm/utils/NightlyTest.gnuplot";
460
461 #
462 # Remove the cvs tree...
463 #
464 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
465
466
467
468 #
469 # Print out information...
470 #
471 if (0) {
472   print "DateString: $DateString\n";
473   print "CVS Checkout: $CVSCheckoutTime seconds\n";
474   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
475
476   print "Build Time: $BuildTime seconds\n";
477   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
478
479   print "WARNINGS:\n  $WarningsList\n";
480
481
482   print "Users committed: $UserCommitList\n";
483   print "Added Files: \n  $AddedFilesList\n";
484   print "Modified Files: \n  $ModifiedFilesList\n";
485   print "Removed Files: \n  $RemovedFilesList\n";
486
487   print "Previous Days =\n  $PrevDaysList\n";
488 }
489
490
491 #
492 # Output the files...
493 #
494
495 # Main HTML file...
496 my $Output;
497 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
498 WriteFile "$DATE.html", $Output;
499
500 # Change the index.html symlink...
501 system "ln -sf $DATE.html index.html";
502
503 sub AddRecord {
504   my ($Val, $Filename) = @_;
505   my @Records;
506   if (open FILE, "$WebDir/$Filename") {
507     @Records = grep !/$DATE/, split "\n", <FILE>;
508     close FILE;
509   }
510   push @Records, "$DATE: $Val";
511   WriteFile "$WebDir/$Filename", (join "\n", @Records) . "\n";
512   return @Records;
513 }