* Made $Template refer to a path relative to the checked-out tree
[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   if (open (FILE, $_[0])) {
26     my $Ret = <FILE>;
27     close FILE;
28     return $Ret;
29   } else {
30     print "Could not open file '$_[0]' for reading!";
31     return "";
32   }
33 }
34
35 sub WriteFile {  # (filename, contents)
36   open (FILE, ">$_[0]") or die "Could not open file '$_[0]' for writing!";
37   print FILE $_[1];
38   close FILE;
39 }
40
41 sub GetRegex {   # (Regex with ()'s, value)
42   $_[1] =~ /$_[0]/m;
43   return $1;
44 }
45
46 sub AddPreTag {  # Add pre tags around nonempty list, or convert to "none"
47   $_ = shift;
48   if (length) { return "<ul><pre>$_</pre></ul>"; } else { "<b>none</b><br>"; }
49 }
50
51 sub GetDir {
52   my $Suffix = shift;
53   opendir DH, $WebDir;
54   my @Result = reverse sort grep !/$DATE/, grep /[-0-9]+$Suffix/, readdir DH;
55   closedir DH;
56   return @Result;
57 }
58
59 # DiffFiles - Diff the current version of the file against the last version of
60 # the file, reporting things added and removed.  This is used to report, for
61 # example, added and removed warnings.  This returns a pair (added, removed)
62 #
63 sub DiffFiles {
64   my $Suffix = shift;
65   my @Others = GetDir $Suffix;
66   if (@Others == 0) {  # No other files?  We added all entries...
67     return (`cat $WebDir/$DATE$Suffix`, "");
68   }
69   # Diff the files now...
70   my @Diffs = split "\n", `diff $WebDir/$DATE$Suffix $WebDir/$Others[0]`;
71   my $Added   = join "\n", grep /^</, @Diffs;
72   my $Removed = join "\n", grep /^>/, @Diffs;
73   $Added =~ s/^< //gm;
74   $Removed =~ s/^> //gm;
75   return ($Added, $Removed);
76 }
77
78
79 # Command line argument settings...
80 my $NOCHECKOUT = 0;
81 my $NOREMOVE   = 0;
82 my $NOTEST     = 0;
83 my $MAKEOPTS   = "";
84
85 # Parse arguments...
86 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
87   shift;
88   last if /^--$/;  # Stop processing arguments on --
89
90   # List command line options here...
91   if (/^-nocheckout$/) { $NOCHECKOUT = 1; next; }
92   if (/^-noremove$/)   { $NOREMOVE   = 1; next; }
93   if (/^-notest$/)     { $NOTEST     = 1; next; }
94   if (/^-parallel$/)   { $MAKEOPTS   = "-j2 -l3.0"; next; }
95
96   print "Unknown option: $_ : ignoring!\n";
97 }
98
99 die "Must specify 0 or 3 options!" if (@ARGV != 0 and @ARGV != 3);
100
101 if (@ARGV == 3) {
102   $CVSRootDir = $ARGV[0];
103   $BuildDir   = $ARGV[1];
104   $WebDir     = $ARGV[2];
105 }
106
107 my $Template = "$BuildDir/llvm/utils/NightlyTestTemplate.html";
108 my $Prefix = "$WebDir/$DATE";
109
110 if (0) {
111   print "CVS Root = $CVSRootDir\n";
112   print "BuildDir = $BuildDir\n";
113   print "WebDir   = $WebDir\n";
114   print "Prefix   = $Prefix\n";
115 }
116
117
118 #
119 # Create the CVS repository directory
120 #
121 if (!$NOCHECKOUT) {
122   mkdir $BuildDir or die "Could not create CVS checkout directory $BuildDir!";
123 }
124 chdir $BuildDir or die "Could not change to CVS checkout directory $BuildDir!";
125
126
127 #
128 # Check out the llvm tree, saving CVS messages to the cvs log...
129 #
130 system "(time -p cvs -d $CVSRootDir co llvm) > $Prefix-CVS-Log.txt 2>&1"
131   if (!$NOCHECKOUT);
132
133 chdir "llvm" or die "Could not change into llvm directory!";
134
135 system "cvs up -P -d > /dev/null 2>&1" if (!$NOCHECKOUT);
136
137 # Read in the HTML template file...
138 undef $/;
139 my $TemplateContents = ReadFile $Template;
140
141
142 #
143 # Get some static statistics about the current state of CVS
144 #
145 my $CVSCheckoutTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-CVS-Log.txt`;
146 my $NumFilesInCVS = `grep '^U' $Prefix-CVS-Log.txt | wc -l` + 0;
147 my $NumDirsInCVS  = `grep '^cvs checkout' $Prefix-CVS-Log.txt | wc -l` + 0;
148 $LOC = GetRegex "([0-9]+) +total", `wc -l \`utils/getsrcs.sh\` | grep total`;
149
150 #
151 # Build the entire tree, saving build messages to the build log
152 #
153 if (!$NOCHECKOUT) {
154   system "(time -p ./configure --enable-jit) > $Prefix-Build-Log.txt 2>&1";
155   # Change the Makefile.config to not strip executables...
156   system "echo 'KEEP_SYMBOLS := 1' >> Makefile.config";
157
158   # Build the entire tree, capturing the output into $Prefix-Build-Log.txt
159   system "(time -p gmake $MAKEOPTS) >> $Prefix-Build-Log.txt 2>&1";
160 }
161
162
163 #
164 # Get some statistics about the build...
165 #
166 my @Linked = split '\n', `grep Linking $Prefix-Build-Log.txt`;
167 my $NumExecutables = scalar(grep(/executable/, @Linked));
168 my $NumLibraries   = scalar(grep(!/executable/, @Linked));
169 my $NumObjects     = `grep '^Compiling' $Prefix-Build-Log.txt | wc -l` + 0;
170 my $BuildTimeU = GetRegex "([0-9.]+)", `grep '^user' $Prefix-Build-Log.txt`;
171 my $BuildTimeS = GetRegex "([0-9.]+)", `grep '^sys' $Prefix-Build-Log.txt`;
172 my $BuildWallTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-Build-Log.txt`;
173 my $BuildTime  = $BuildTimeU+$BuildTimeS;  # BuildTime = User+System
174 my $BuildError = "";
175 if (`grep '^gmake: .*Error' $Prefix-Build-Log.txt | wc -l` + 0) {
176   $BuildError = "<h3>Build error: compilation <a href=\"$DATE-Build-Log.txt\">"
177               . "aborted</a></h3>";
178 }
179
180
181 #
182 # Get warnings from the build
183 #
184 my @Warn = split "\n", `egrep 'warning:|Entering dir' $Prefix-Build-Log.txt`;
185 my @Warnings;
186 my $CurDir = "";
187
188 foreach $Warning (@Warn) {
189   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
190     $CurDir = $1;                 # Keep track of directory warning is in...
191     if ($CurDir =~ m|$BuildDir/llvm/(.*)|) { # Remove buildir prefix if included
192       $CurDir = $1;
193     }
194   } else {
195     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
196   }
197 }
198 my $WarningsFile =  join "\n", @Warnings;
199 my $WarningsList = AddPreTag $WarningsFile;
200 $WarningsFile =~ s/:[0-9]+:/::/g;
201
202 # Emit the warnings file, so we can diff...
203 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
204 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
205 $WarningsAdded = AddPreTag $WarningsAdded;
206 $WarningsRemoved = AddPreTag $WarningsRemoved;
207
208
209 #
210 # Get some statistics about CVS commits over the current day...
211 #
212 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
213 #print join "\n", @CVSHistory; print "\n";
214
215 # Extract some information from the CVS history... use a hash so no duplicate
216 # stuff is stored.
217 my (%AddedFiles, %ModifiedFiles, %RemovedFiles, %UsersCommitted, %UsersUpdated);
218
219 my $DateRE = "[-:0-9 ]+\\+[0-9]+";
220
221 # Loop over every record from the CVS history, filling in the hashes.
222 foreach $File (@CVSHistory) {
223   my ($Type, $Date, $UID, $Rev, $Filename);
224   if ($File =~ /([AMRUGC]) ($DateRE) ([^ ]+) +([0-9\.]+) +([^ ]+) +([^ ]+)/) {
225     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
226   } elsif ($File =~ /([W]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+)/) {
227     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
228   } elsif ($File =~ /([O]) ($DateRE) ([^ ]+) +([^ ]+)/) {
229     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "$4/");
230   } else {
231     print "UNMATCHABLE: $File\n";
232   }
233   # print "$File\nTy = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
234
235   if ($Filename =~ /^llvm/) {
236     if ($Type eq 'M') {        # Modified
237       $ModifiedFiles{$Filename} = 1;
238       $UsersCommitted{$UID} = 1;
239     } elsif ($Type eq 'A') {   # Added
240       $AddedFiles{$Filename} = 1;
241       $UsersCommitted{$UID} = 1;
242     } elsif ($Type eq 'R') {   # Removed
243       $RemovedFiles{$Filename} = 1;
244       $UsersCommitted{$UID} = 1;
245     } else {
246       $UsersUpdated{$UID} = 1;
247     }
248   }
249 }
250
251 my $UserCommitList = join "\n", keys %UsersCommitted;
252 my $UserUpdateList = join "\n", keys %UsersUpdated;
253 my $AddedFilesList = AddPreTag join "\n", keys %AddedFiles;
254 my $ModifiedFilesList = AddPreTag join "\n", keys %ModifiedFiles;
255 my $RemovedFilesList = AddPreTag join "\n", keys %RemovedFiles;
256
257 my $TestError = 1;
258 my $ProgramsTable;
259
260 # If we build the tree successfully, the nightly programs tests...
261 if ($BuildError eq "") {
262   chdir "test/Programs" or die "Could not change into programs testdir!";
263
264   # Run the programs tests... creating a report.nightly.html file
265   if (!$NOTEST) {
266     system "gmake $MAKEOPTS report.nightly.html TEST=nightly "
267          . "RUNTIMELIMIT=300 > $Prefix-ProgramTest.txt 2>&1";
268   } else {
269     system "gunzip $Prefix-ProgramTest.txt.gz";
270   }
271
272   if (`grep '^gmake: .*Error' $Prefix-ProgramTest.txt | wc -l` + 0) {
273     $TestError = 1;
274     $ProgramsTable = "<font color=white><h2>Error running tests!</h2></font>";
275   } elsif (`grep '^gmake: .*No rule to make target' $Prefix-ProgramTest.txt | wc -l` + 0) {
276     $TestError = 1;
277     $ProgramsTable =
278       "<font color=white><h2>Makefile error running tests!</h2></font>";
279   } else {
280     $TestError = 0;
281     $ProgramsTable = ReadFile "report.nightly.html";
282
283     #
284     # Create a list of the tests which were run...
285     #
286     system "egrep 'TEST-(PASS|FAIL)' < $Prefix-ProgramTest.txt "
287          . "| sort > $Prefix-Tests.txt";
288   }
289
290   # Compress the test output
291   system "gzip -f $Prefix-ProgramTest.txt";
292 }
293
294 my ($TestsAdded, $TestsRemoved, $TestsFixed, $TestsBroken) = ("","","","");
295
296 if ($TestError) {
297   $TestsAdded   = "<b>error testing</b><br>";
298   $TestsRemoved = "<b>error testing</b><br>";
299   $TestsFixed   = "<b>error testing</b><br>";
300   $TestsBroken  = "<b>error testing</b><br>";
301 } else {
302   my ($RTestsAdded, $RTestsRemoved) = DiffFiles "-Tests.txt";
303
304   my @RawTestsAddedArray = split '\n', $RTestsAdded;
305   my @RawTestsRemovedArray = split '\n', $RTestsRemoved;
306
307   my %OldTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
308     @RawTestsRemovedArray;
309   my %NewTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
310     @RawTestsAddedArray;
311
312   foreach $Test (keys %NewTests) {
313     if (!exists $OldTests{$Test}) {  # TestAdded if in New but not old
314       $TestsAdded = "$TestsAdded$Test\n";
315     } else {
316       if ($OldTests{$Test} =~ /TEST-PASS/) {  # Was the old one a pass?
317         $TestsBroken = "$TestsBroken$Test\n";  # New one must be a failure
318       } else {
319         $TestsFixed = "$TestsFixed$Test\n";    # No, new one is a pass.
320       }
321     }
322   }
323   foreach $Test (keys %OldTests) {  # TestRemoved if in Old but not New
324     $TestsRemoved = "$TestsRemoved$Test\n" if (!exists $NewTests{$Test});
325   }
326
327   $TestsAdded   = AddPreTag $TestsAdded;
328   $TestsRemoved = AddPreTag $TestsRemoved;
329   $TestsFixed   = AddPreTag $TestsFixed;
330   $TestsBroken  = AddPreTag $TestsBroken;
331 }
332
333 #
334 # Get a list of the previous days that we can link to...
335 #
336 my @PrevDays = map {s/.html//; $_} GetDir ".html";
337
338 splice @PrevDays, 20;  # Trim down list to something reasonable...
339
340 my $PrevDaysList =     # Format list for sidebar
341   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
342
343
344 #
345 # Remove the cvs tree...
346 #
347 chdir $WebDir or die "Could not change into web directory!";
348 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
349
350
351 #
352 # Print out information...
353 #
354 if (0) {
355   print "DateString: $DateString\n";
356   print "CVS Checkout: $CVSCheckoutTime seconds\n";
357   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
358
359   print "Build Time: $BuildTime seconds\n";
360   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
361
362   print "WARNINGS:\n  $WarningsList\n";
363
364
365   print "Users committed: $UserCommitList\n";
366   print "Added Files: \n  $AddedFilesList\n";
367   print "Modified Files: \n  $ModifiedFilesList\n";
368   print "Removed Files: \n  $RemovedFilesList\n";
369
370   print "Previous Days =\n  $PrevDaysList\n";
371 }
372
373
374 #
375 # Output the files...
376 #
377
378 # Main HTML file...
379 my $Output;
380 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
381 WriteFile "$DATE.html", $Output;
382
383 # Change the index.html symlink...
384 system "ln -sf $DATE.html index.html";
385
386 sub AddRecord {
387   my ($Val, $Filename) = @_;
388   my @Records;
389   if (open FILE, $Filename) {
390     @Records = grep !/$DATE/, split "\n", <FILE>;
391     close FILE;
392   }
393   push @Records, "$DATE: $Val";
394   WriteFile $Filename, (join "\n", @Records) . "\n";
395   return @Records;
396 }
397
398 # Add information to the files which accumulate information for graphs...
399 AddRecord($LOC, "running_loc.txt");
400 AddRecord($BuildTime, "running_build_time.txt");