Remove fixme
[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 --enable-spec --with-objroot=.) > $Prefix-Build-Log.txt 2>&1";
155
156   # Build the entire tree, capturing the output into $Prefix-Build-Log.txt
157   system "(time -p gmake $MAKEOPTS) >> $Prefix-Build-Log.txt 2>&1";
158 }
159
160
161 sub GetRegexNum {
162   my ($Regex, $Num, $Regex2, $File) = @_;
163   my @Items = split "\n", `grep '$Regex' $File`;
164   return GetRegex $Regex2, $Items[$Num];
165 }
166
167 #
168 # Get some statistics about the build...
169 #
170 my @Linked = split '\n', `grep Linking $Prefix-Build-Log.txt`;
171 my $NumExecutables = scalar(grep(/executable/, @Linked));
172 my $NumLibraries   = scalar(grep(!/executable/, @Linked));
173 my $NumObjects     = `grep '^Compiling' $Prefix-Build-Log.txt | wc -l` + 0;
174
175 my $ConfigTimeU = GetRegexNum "^user", 0, "([0-9.]+)", "$Prefix-Build-Log.txt";
176 my $ConfigTimeS = GetRegexNum "^sys", 0, "([0-9.]+)", "$Prefix-Build-Log.txt";
177 my $ConfigTime  = $ConfigTimeU+$ConfigTimeS;  # ConfigTime = User+System
178 my $ConfigWallTime = GetRegexNum "^real", 0,"([0-9.]+)","$Prefix-Build-Log.txt";
179
180 my $BuildTimeU = GetRegexNum "^user", 1, "([0-9.]+)", "$Prefix-Build-Log.txt";
181 my $BuildTimeS = GetRegexNum "^sys", 1, "([0-9.]+)", "$Prefix-Build-Log.txt";
182 my $BuildTime  = $BuildTimeU+$BuildTimeS;  # BuildTime = User+System
183 my $BuildWallTime = GetRegexNum "^real", 1, "([0-9.]+)","$Prefix-Build-Log.txt";
184
185 my $BuildError = "";
186 if (`grep '^gmake[^:]*: .*Error' $Prefix-Build-Log.txt | wc -l` + 0) {
187   $BuildError = "<h3>Build error: compilation <a href=\"$DATE-Build-Log.txt\">"
188               . "aborted</a></h3>";
189 }
190
191
192 #
193 # Get warnings from the build
194 #
195 my @Warn = split "\n", `egrep 'warning:|Entering dir' $Prefix-Build-Log.txt`;
196 my @Warnings;
197 my $CurDir = "";
198
199 foreach $Warning (@Warn) {
200   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
201     $CurDir = $1;                 # Keep track of directory warning is in...
202     if ($CurDir =~ m#$BuildDir/llvm/(.*)#) { # Remove buildir prefix if included
203       $CurDir = $1;
204     }
205   } else {
206     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
207   }
208 }
209 my $WarningsFile =  join "\n", @Warnings;
210 my $WarningsList = AddPreTag $WarningsFile;
211 $WarningsFile =~ s/:[0-9]+:/::/g;
212
213 # Emit the warnings file, so we can diff...
214 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
215 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
216 $WarningsAdded = AddPreTag $WarningsAdded;
217 $WarningsRemoved = AddPreTag $WarningsRemoved;
218
219
220 #
221 # Get some statistics about CVS commits over the current day...
222 #
223 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
224 #print join "\n", @CVSHistory; print "\n";
225
226 # Extract some information from the CVS history... use a hash so no duplicate
227 # stuff is stored.
228 my (%AddedFiles, %ModifiedFiles, %RemovedFiles, %UsersCommitted, %UsersUpdated);
229
230 my $DateRE = "[-:0-9 ]+\\+[0-9]+";
231
232 # Loop over every record from the CVS history, filling in the hashes.
233 foreach $File (@CVSHistory) {
234   my ($Type, $Date, $UID, $Rev, $Filename);
235   if ($File =~ /([AMRUGC]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)/) {
236     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
237   } elsif ($File =~ /([W]) ($DateRE) ([^ ]+) +([^ ]+) +([^ ]+)/) {
238     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
239   } elsif ($File =~ /([O]) ($DateRE) ([^ ]+) +([^ ]+)/) {
240     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "$4/");
241   } else {
242     print "UNMATCHABLE: $File\n";
243   }
244   # print "$File\nTy = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
245
246   if ($Filename =~ /^llvm/) {
247     if ($Type eq 'M') {        # Modified
248       $ModifiedFiles{$Filename} = 1;
249       $UsersCommitted{$UID} = 1;
250     } elsif ($Type eq 'A') {   # Added
251       $AddedFiles{$Filename} = 1;
252       $UsersCommitted{$UID} = 1;
253     } elsif ($Type eq 'R') {   # Removed
254       $RemovedFiles{$Filename} = 1;
255       $UsersCommitted{$UID} = 1;
256     } else {
257       $UsersUpdated{$UID} = 1;
258     }
259   }
260 }
261
262 my $UserCommitList = join "\n", keys %UsersCommitted;
263 my $UserUpdateList = join "\n", keys %UsersUpdated;
264 my $AddedFilesList = AddPreTag join "\n", sort keys %AddedFiles;
265 my $ModifiedFilesList = AddPreTag join "\n", sort keys %ModifiedFiles;
266 my $RemovedFilesList = AddPreTag join "\n", sort keys %RemovedFiles;
267
268 my $TestError = 1;
269 my $SingleSourceProgramsTable;
270 my $MultiSourceProgramsTable;
271
272
273 sub TestDirectory {
274   my $SubDir = shift;
275
276   chdir "test/Programs/$SubDir" or
277     die "Could not change into test/Programs/$SubDir testdir!";
278
279   # Run the programs tests... creating a report.nightly.html file
280   if (!$NOTEST) {
281     system "gmake $MAKEOPTS report.nightly.html TEST=nightly "
282          . "RUNTIMELIMIT=300 > $Prefix-$SubDir-ProgramTest.txt 2>&1";
283   } else {
284     system "gunzip $Prefix-$SubDir-ProgramTest.txt.gz";
285   }
286
287   my $ProgramsTable;
288   if (`grep '^gmake: .*Error' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0) {
289     $TestError = 1;
290     $ProgramsTable = "<font color=white><h2>Error running tests!</h2></font>";
291   } elsif (`grep '^gmake: .*No rule to make target' $Prefix-$SubDir-ProgramTest.txt | wc -l` + 0) {
292     $TestError = 1;
293     $ProgramsTable =
294       "<font color=white><h2>Makefile error running tests!</h2></font>";
295   } else {
296     $TestError = 0;
297     $ProgramsTable = ReadFile "report.nightly.html";
298
299     #
300     # Create a list of the tests which were run...
301     #
302     system "egrep 'TEST-(PASS|FAIL)' < $Prefix-$SubDir-ProgramTest.txt "
303          . "| sort > $Prefix-$SubDir-Tests.txt";
304   }
305
306   # Compress the test output
307   system "gzip -f $Prefix-$SubDir-ProgramTest.txt";
308   chdir "../../.." or die "Cannot return to parent directory!";
309   return $ProgramsTable;
310 }
311
312 # If we build the tree successfully, the nightly programs tests...
313 if ($BuildError eq "") {
314   $SingleSourceProgramsTable = TestDirectory("SingleSource");
315   $MultiSourceProgramsTable = TestDirectory("MultiSource");
316   system "cat $Prefix-SingleSource-Tests.txt $Prefix-MultiSource-Tests.txt ".
317          " | sort > $Prefix-Tests.txt";
318 }
319
320 my ($TestsAdded, $TestsRemoved, $TestsFixed, $TestsBroken) = ("","","","");
321
322 if ($TestError) {
323   $TestsAdded   = "<b>error testing</b><br>";
324   $TestsRemoved = "<b>error testing</b><br>";
325   $TestsFixed   = "<b>error testing</b><br>";
326   $TestsBroken  = "<b>error testing</b><br>";
327 } else {
328   my ($RTestsAdded, $RTestsRemoved) = DiffFiles "-Tests.txt";
329
330   my @RawTestsAddedArray = split '\n', $RTestsAdded;
331   my @RawTestsRemovedArray = split '\n', $RTestsRemoved;
332
333   my %OldTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
334     @RawTestsRemovedArray;
335   my %NewTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
336     @RawTestsAddedArray;
337
338   foreach $Test (keys %NewTests) {
339     if (!exists $OldTests{$Test}) {  # TestAdded if in New but not old
340       $TestsAdded = "$TestsAdded$Test\n";
341     } else {
342       if ($OldTests{$Test} =~ /TEST-PASS/) {  # Was the old one a pass?
343         $TestsBroken = "$TestsBroken$Test\n";  # New one must be a failure
344       } else {
345         $TestsFixed = "$TestsFixed$Test\n";    # No, new one is a pass.
346       }
347     }
348   }
349   foreach $Test (keys %OldTests) {  # TestRemoved if in Old but not New
350     $TestsRemoved = "$TestsRemoved$Test\n" if (!exists $NewTests{$Test});
351   }
352
353   $TestsAdded   = AddPreTag $TestsAdded;
354   $TestsRemoved = AddPreTag $TestsRemoved;
355   $TestsFixed   = AddPreTag $TestsFixed;
356   $TestsBroken  = AddPreTag $TestsBroken;
357 }
358
359 #
360 # Get a list of the previous days that we can link to...
361 #
362 my @PrevDays = map {s/.html//; $_} GetDir ".html";
363
364 splice @PrevDays, 20;  # Trim down list to something reasonable...
365
366 my $PrevDaysList =     # Format list for sidebar
367   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
368
369 #
370 # Start outputing files into the web directory
371 #
372 chdir $WebDir or die "Could not change into web directory!";
373
374 # Add information to the files which accumulate information for graphs...
375 AddRecord($LOC, "running_loc.txt");
376 AddRecord($BuildTime, "running_build_time.txt");
377
378 #
379 # Rebuild the graphs now...
380 #
381 system "/usr/dcs/software/supported/bin/gnuplot " .
382        "$BuildDir/llvm/utils/NightlyTest.gnuplot";
383
384 #
385 # Remove the cvs tree...
386 #
387 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
388
389
390
391 #
392 # Print out information...
393 #
394 if (0) {
395   print "DateString: $DateString\n";
396   print "CVS Checkout: $CVSCheckoutTime seconds\n";
397   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
398
399   print "Build Time: $BuildTime seconds\n";
400   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
401
402   print "WARNINGS:\n  $WarningsList\n";
403
404
405   print "Users committed: $UserCommitList\n";
406   print "Added Files: \n  $AddedFilesList\n";
407   print "Modified Files: \n  $ModifiedFilesList\n";
408   print "Removed Files: \n  $RemovedFilesList\n";
409
410   print "Previous Days =\n  $PrevDaysList\n";
411 }
412
413
414 #
415 # Output the files...
416 #
417
418 # Main HTML file...
419 my $Output;
420 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
421 WriteFile "$DATE.html", $Output;
422
423 # Change the index.html symlink...
424 system "ln -sf $DATE.html index.html";
425
426 sub AddRecord {
427   my ($Val, $Filename) = @_;
428   my @Records;
429   if (open FILE, $Filename) {
430     @Records = grep !/$DATE/, split "\n", <FILE>;
431     close FILE;
432   }
433   push @Records, "$DATE: $Val";
434   WriteFile $Filename, (join "\n", @Records) . "\n";
435   return @Records;
436 }