Allow specifying the name for the newly split basic block
[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   $BuildError = "<h3><font color='red'>Build error: compilation " .
202                 "<a href=\"$DATE-Build-Log.txt\">aborted</a></font></h3>";
203 }
204
205 #
206 # Get warnings from the build
207 #
208 my @Warn = split "\n", `egrep 'warning:|Entering dir' $Prefix-Build-Log.txt`;
209 my @Warnings;
210 my $CurDir = "";
211
212 foreach $Warning (@Warn) {
213   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
214     $CurDir = $1;                 # Keep track of directory warning is in...
215     if ($CurDir =~ m#$BuildDir/llvm/(.*)#) { # Remove buildir prefix if included
216       $CurDir = $1;
217     }
218   } else {
219     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
220   }
221 }
222 my $WarningsFile =  join "\n", @Warnings;
223 my $WarningsList = AddPreTag $WarningsFile;
224 $WarningsFile =~ s/:[0-9]+:/::/g;
225
226 # Emit the warnings file, so we can diff...
227 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
228 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
229 $WarningsAdded = AddPreTag $WarningsAdded;
230 $WarningsRemoved = AddPreTag $WarningsRemoved;
231
232
233 #
234 # Get some statistics about CVS commits over the current day...
235 #
236 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
237 #print join "\n", @CVSHistory; print "\n";
238
239 # Extract some information from the CVS history... use a hash so no duplicate
240 # stuff is stored.
241 my (%AddedFiles, %ModifiedFiles, %RemovedFiles, %UsersCommitted, %UsersUpdated);
242
243 my $DateRE = "[-:0-9 ]+\\+[0-9]+";
244
245 # Loop over every record from the CVS history, filling in the hashes.
246 foreach $File (@CVSHistory) {
247   my ($Type, $Date, $UID, $Rev, $Filename);
248   if ($File =~ /([AMRUGC]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)/) {
249     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
250   } elsif ($File =~ /([W]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+)/) {
251     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
252   } elsif ($File =~ /([O]) ($DateRE) ([^ ]+) +([^ ]+)/) {
253     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "$4/");
254   } else {
255     print "UNMATCHABLE: $File\n";
256   }
257   # print "$File\nTy = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
258
259   if ($Filename =~ /^llvm/) {
260     if ($Type eq 'M') {        # Modified
261       $ModifiedFiles{$Filename} = 1;
262       $UsersCommitted{$UID} = 1;
263     } elsif ($Type eq 'A') {   # Added
264       $AddedFiles{$Filename} = 1;
265       $UsersCommitted{$UID} = 1;
266     } elsif ($Type eq 'R') {   # Removed
267       $RemovedFiles{$Filename} = 1;
268       $UsersCommitted{$UID} = 1;
269     } else {
270       $UsersUpdated{$UID} = 1;
271     }
272   }
273 }
274
275 my $UserCommitList = join "\n", keys %UsersCommitted;
276 my $UserUpdateList = join "\n", keys %UsersUpdated;
277 my $AddedFilesList = AddPreTag join "\n", sort keys %AddedFiles;
278 my $ModifiedFilesList = AddPreTag join "\n", sort keys %ModifiedFiles;
279 my $RemovedFilesList = AddPreTag join "\n", sort keys %RemovedFiles;
280
281 my $TestError = 1;
282 my $SingleSourceProgramsTable;
283 my $MultiSourceProgramsTable;
284 my $ExternalProgramsTable;
285
286
287 sub TestDirectory {
288   my $SubDir = shift;
289
290   chdir "test/Programs/$SubDir" or
291     die "Could not change into test/Programs/$SubDir testdir!";
292
293   # Run the programs tests... creating a report.nightly.html file
294   if (!$NOTEST) {
295     system "gmake $MAKEOPTS report.nightly.html TEST=nightly "
296          . "> $Prefix-$SubDir-ProgramTest.txt 2>&1";
297   } else {
298     system "gunzip $Prefix-$SubDir-ProgramTest.txt.gz";
299   }
300
301   my $ProgramsTable;
302   if (`grep '^gmake[^:]: .*Error' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0){
303     $TestError = 1;
304     $ProgramsTable = "<font color=white><h2>Error running tests!</h2></font>";
305   } elsif (`grep '^gmake[^:]: .*No rule to make target' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0) {
306     $TestError = 1;
307     $ProgramsTable =
308       "<font color=white><h2>Makefile error running tests!</h2></font>";
309   } else {
310     $TestError = 0;
311     $ProgramsTable = ReadFile "report.nightly.html";
312
313     #
314     # Create a list of the tests which were run...
315     #
316     system "egrep 'TEST-(PASS|FAIL)' < $Prefix-$SubDir-ProgramTest.txt "
317          . "| sort > $Prefix-$SubDir-Tests.txt";
318   }
319
320   # Compress the test output
321   system "gzip -f $Prefix-$SubDir-ProgramTest.txt";
322   chdir "../../.." or die "Cannot return to parent directory!";
323   return $ProgramsTable;
324 }
325
326 # If we build the tree successfully, run the nightly programs tests...
327 if ($BuildError eq "") {
328   $SingleSourceProgramsTable = TestDirectory("SingleSource");
329   $MultiSourceProgramsTable = TestDirectory("MultiSource");
330   $ExternalProgramsTable = TestDirectory("External");
331   system "cat $Prefix-SingleSource-Tests.txt $Prefix-MultiSource-Tests.txt ".
332          " $Prefix-External-Tests.txt | sort > $Prefix-Tests.txt";
333 }
334
335 my ($TestsAdded, $TestsRemoved, $TestsFixed, $TestsBroken) = ("","","","");
336
337 if ($TestError) {
338   $TestsAdded   = "<b>error testing</b><br>";
339   $TestsRemoved = "<b>error testing</b><br>";
340   $TestsFixed   = "<b>error testing</b><br>";
341   $TestsBroken  = "<b>error testing</b><br>";
342 } else {
343   my ($RTestsAdded, $RTestsRemoved) = DiffFiles "-Tests.txt";
344
345   my @RawTestsAddedArray = split '\n', $RTestsAdded;
346   my @RawTestsRemovedArray = split '\n', $RTestsRemoved;
347
348   my %OldTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
349     @RawTestsRemovedArray;
350   my %NewTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
351     @RawTestsAddedArray;
352
353   foreach $Test (keys %NewTests) {
354     if (!exists $OldTests{$Test}) {  # TestAdded if in New but not old
355       $TestsAdded = "$TestsAdded$Test\n";
356     } else {
357       if ($OldTests{$Test} =~ /TEST-PASS/) {  # Was the old one a pass?
358         $TestsBroken = "$TestsBroken$Test\n";  # New one must be a failure
359       } else {
360         $TestsFixed = "$TestsFixed$Test\n";    # No, new one is a pass.
361       }
362     }
363   }
364   foreach $Test (keys %OldTests) {  # TestRemoved if in Old but not New
365     $TestsRemoved = "$TestsRemoved$Test\n" if (!exists $NewTests{$Test});
366   }
367
368   $TestsAdded   = AddPreTag $TestsAdded;
369   $TestsRemoved = AddPreTag $TestsRemoved;
370   $TestsFixed   = AddPreTag $TestsFixed;
371   $TestsBroken  = AddPreTag $TestsBroken;
372 }
373
374 # If we built the tree successfully, runs of the Olden suite with
375 # LARGE_PROBLEM_SIZE on so that we can get some "running" statistics.
376 if ($BuildError eq "") {
377   my ($NATTime, $CBETime, $LLCTime, $JITTime, $OptTime, $BytecodeSize,
378       $MachCodeSize) = ("","","","","","","");
379   if (!$NORUNNINGTESTS) {
380     chdir "test/Programs/MultiSource/Olden" or die "Olden tests moved?";
381
382     # Clean out previous results...
383     system "gmake $MAKEOPTS clean > /dev/null 2>&1";
384
385     # Run the nightly test in this directory, with LARGE_PROBLEM_SIZE enabled!
386     system "gmake $MAKEOPTS report.nightly.raw.out TEST=nightly " .
387            " LARGE_PROBLEM_SIZE=1 > /dev/null 2>&1";
388     system "cp report.nightly.raw.out $Prefix-Olden-tests.txt";
389   } else {
390     system "gunzip $Prefix-Olden-tests.txt.gz";
391   }
392
393   # Now we know we have $Prefix-Olden-tests.txt as the raw output file.  Split
394   # it up into records and read the useful information.
395   my @Records = split />>> ========= /, ReadFile "$Prefix-Olden-tests.txt";
396   shift @Records;  # Delete the first (garbage) record
397
398   # Loop over all of the records, summarizing them into rows for the running
399   # totals file.
400   my $WallTimeRE = "[A-Za-z0-9.: ]+\\(([0-9.]+) wall clock";
401   foreach $Rec (@Records) {
402     my $rNATTime = GetRegex 'TEST-RESULT-nat-time: real\s*([.0-9m]+)', $Rec;
403     my $rCBETime = GetRegex 'TEST-RESULT-cbe-time: real\s*([.0-9m]+)', $Rec;
404     my $rLLCTime = GetRegex 'TEST-RESULT-llc-time: real\s*([.0-9m]+)', $Rec;
405     my $rJITTime = GetRegex 'TEST-RESULT-jit-time: real\s*([.0-9m]+)', $Rec;
406     my $rOptTime = GetRegex "TEST-RESULT-compile: $WallTimeRE", $Rec;
407     my $rBytecodeSize = GetRegex 'TEST-RESULT-compile: *([0-9]+)', $Rec;
408     my $rMachCodeSize = GetRegex 'TEST-RESULT-jit-machcode: *([0-9]+).*bytes of machine code', $Rec;
409
410     $NATTime .= " " . FormatTime($rNATTime);
411     $CBETime .= " " . FormatTime($rCBETime);
412     $LLCTime .= " " . FormatTime($rLLCTime);
413     $JITTime .= " " . FormatTime($rJITTime);
414     $OptTime .= " $rOptTime";
415     $BytecodeSize .= " $rBytecodeSize";
416     $MachCodeSize .= " $rMachCodeSize";
417   }
418
419   # Now that we have all of the numbers we want, add them to the running totals
420   # files.
421   AddRecord($NATTime, "running_Olden_nat_time.txt");
422   AddRecord($CBETime, "running_Olden_cbe_time.txt");
423   AddRecord($LLCTime, "running_Olden_llc_time.txt");
424   AddRecord($JITTime, "running_Olden_jit_time.txt");
425   AddRecord($OptTime, "running_Olden_opt_time.txt");
426   AddRecord($BytecodeSize, "running_Olden_bytecode.txt");
427   AddRecord($MachCodeSize, "running_Olden_machcode.txt");
428
429   system "gzip -f $Prefix-Olden-tests.txt";
430 }
431
432
433
434
435 #
436 # Get a list of the previous days that we can link to...
437 #
438 my @PrevDays = map {s/.html//; $_} GetDir ".html";
439
440 splice @PrevDays, 20;  # Trim down list to something reasonable...
441
442 my $PrevDaysList =     # Format list for sidebar
443   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
444
445 #
446 # Start outputing files into the web directory
447 #
448 chdir $WebDir or die "Could not change into web directory!";
449
450 # Add information to the files which accumulate information for graphs...
451 AddRecord($LOC, "running_loc.txt");
452 AddRecord($BuildTime, "running_build_time.txt");
453
454 #
455 # Rebuild the graphs now...
456 #
457 system "/usr/dcs/software/supported/bin/gnuplot " .
458        "$BuildDir/llvm/utils/NightlyTest.gnuplot";
459
460 #
461 # Remove the cvs tree...
462 #
463 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
464
465
466
467 #
468 # Print out information...
469 #
470 if (0) {
471   print "DateString: $DateString\n";
472   print "CVS Checkout: $CVSCheckoutTime seconds\n";
473   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
474
475   print "Build Time: $BuildTime seconds\n";
476   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
477
478   print "WARNINGS:\n  $WarningsList\n";
479
480
481   print "Users committed: $UserCommitList\n";
482   print "Added Files: \n  $AddedFilesList\n";
483   print "Modified Files: \n  $ModifiedFilesList\n";
484   print "Removed Files: \n  $RemovedFilesList\n";
485
486   print "Previous Days =\n  $PrevDaysList\n";
487 }
488
489
490 #
491 # Output the files...
492 #
493
494 # Main HTML file...
495 my $Output;
496 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
497 WriteFile "$DATE.html", $Output;
498
499 # Change the index.html symlink...
500 system "ln -sf $DATE.html index.html";
501
502 sub AddRecord {
503   my ($Val, $Filename) = @_;
504   my @Records;
505   if (open FILE, "$WebDir/$Filename") {
506     @Records = grep !/$DATE/, split "\n", <FILE>;
507     close FILE;
508   }
509   push @Records, "$DATE: $Val";
510   WriteFile "$WebDir/$Filename", (join "\n", @Records) . "\n";
511   return @Records;
512 }