Properly fix instruction syntax in comments, using `imm' for instructions that
[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 # FIXME: This should just be utils/...
102 my $Template   = "$HOME/llvm/utils/NightlyTestTemplate.html";
103
104 if (@ARGV == 3) {
105   $CVSRootDir = $ARGV[0];
106   $BuildDir   = $ARGV[1];
107   $WebDir     = $ARGV[2];
108 }
109
110 my $Prefix = "$WebDir/$DATE";
111
112 if (0) {
113   print "CVS Root = $CVSRootDir\n";
114   print "BuildDir = $BuildDir\n";
115   print "WebDir   = $WebDir\n";
116   print "Prefix   = $Prefix\n";
117 }
118
119
120 #
121 # Create the CVS repository directory
122 #
123 if (!$NOCHECKOUT) {
124   mkdir $BuildDir or die "Could not create CVS checkout directory!";
125 }
126 chdir $BuildDir or die "Could not change to CVS checkout directory!";
127
128
129 #
130 # Check out the llvm tree, saving CVS messages to the cvs log...
131 #
132 system "(time -p cvs -d $CVSRootDir co llvm) > $Prefix-CVS-Log.txt 2>&1"
133   if (!$NOCHECKOUT);
134
135 chdir "llvm" or die "Could not change into llvm directory!";
136
137 system "cvs up -P -d > /dev/null 2>&1" if (!$NOCHECKOUT);
138
139 # Read in the HTML template file...
140 undef $/;
141 my $TemplateContents = ReadFile $Template;
142
143
144 #
145 # Get some static statistics about the current state of CVS
146 #
147 my $CVSCheckoutTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-CVS-Log.txt`;
148 my $NumFilesInCVS = `grep ^U $Prefix-CVS-Log.txt | wc -l` + 0;
149 my $NumDirsInCVS  = `grep '^cvs checkout' $Prefix-CVS-Log.txt | wc -l` + 0;
150 $LOC = GetRegex "([0-9]+) +total", `wc -l \`utils/getsrcs.sh\` | grep total`;
151
152 #
153 # Build the entire tree, saving build messages to the build log
154 #
155 if (!$NOCHECKOUT) {
156   system "(time -p ./configure) > $Prefix-Build-Log.txt 2>&1";
157   # Change the Makefile.config to not strip executables...
158   system "echo 'KEEP_SYMBOLS := 1' >> Makefile.config";
159
160   # Build the entire tree, capturing the output into $Prefix-Build-Log.txt
161   system "(time -p gmake $MAKEOPTS) >> $Prefix-Build-Log.txt 2>&1";
162 }
163
164
165 #
166 # Get some statistics about the build...
167 #
168 my @Linked = split '\n', `grep Linking $Prefix-Build-Log.txt`;
169 my $NumExecutables = scalar(grep(/executable/, @Linked));
170 my $NumLibraries   = scalar(grep(!/executable/, @Linked));
171 my $NumObjects     = `grep '^Compiling' $Prefix-Build-Log.txt | wc -l` + 0;
172 my $BuildTimeU = GetRegex "([0-9.]+)", `grep '^user' $Prefix-Build-Log.txt`;
173 my $BuildTimeS = GetRegex "([0-9.]+)", `grep '^sys' $Prefix-Build-Log.txt`;
174 my $BuildWallTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-Build-Log.txt`;
175 my $BuildTime  = $BuildTimeU+$BuildTimeS;  # BuildTime = User+System
176 my $BuildError = "";
177 if (`grep '^gmake: .*Error' $Prefix-Build-Log.txt | wc -l` + 0) {
178   $BuildError = "<h3>Build error: compilation <a href=\"$DATE-Build-Log.txt\">"
179               . "aborted</a></h3>";
180 }
181
182
183 #
184 # Get warnings from the build
185 #
186 my @Warn = split "\n", `grep -E 'warning:|Entering dir' $Prefix-Build-Log.txt`;
187 my @Warnings;
188 my $CurDir = "";
189
190 foreach $Warning (@Warn) {
191   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
192     $CurDir = $1;                 # Keep track of directory warning is in...
193     if ($CurDir =~ m|$BuildDir/llvm/(.*)|) { # Remove buildir prefix if included
194       $CurDir = $1;
195     }
196   } else {
197     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
198   }
199 }
200 my $WarningsFile =  join "\n", @Warnings;
201 my $WarningsList = AddPreTag $WarningsFile;
202 $WarningsFile =~ s/:[0-9]+:/::/g;
203
204 # Emit the warnings file, so we can diff...
205 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
206 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
207 $WarningsAdded = AddPreTag $WarningsAdded;
208 $WarningsRemoved = AddPreTag $WarningsRemoved;
209
210
211 #
212 # Get some statistics about CVS commits over the current day...
213 #
214 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
215 #print join "\n", @CVSHistory; print "\n";
216
217 # Extract some information from the CVS history... use a hash so no duplicate
218 # stuff is stored.
219 my (%AddedFiles, %ModifiedFiles, %RemovedFiles,
220     %UsersCommitted, %UsersUpdated);
221
222 my $DateRE = "[-:0-9 ]+\\+[0-9]+";
223
224 # Loop over every record from the CVS history, filling in the hashes.
225 foreach $File (@CVSHistory) {
226   my ($Type, $Date, $UID, $Rev, $Filename);
227   if ($File =~ /([AMRUGC])\s($DateRE)\s([^\s].......) (.[0-9.]*)\s+([^\s]+)\s+([^\s]+)/) {
228     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
229   } elsif ($File =~ /([W])\s($DateRE)\s([^\s]+) +([^\s]+)\s+([^\s]+)/) {
230     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
231   } elsif ($File =~ /([O]) ($DateRE) ([^ ]+) +([^\s]+)/) {
232     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "$4/");
233   } else {
234     print "UNMATCHABLE: $File\n";
235   }
236   # print "$File\nTy = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
237
238   if ($Filename =~ /^llvm/) {
239     if ($Type eq 'M') {        # Modified
240       $ModifiedFiles{$Filename} = 1;
241       $UsersCommitted{$UID} = 1;
242     } elsif ($Type eq 'A') {   # Added
243       $AddedFiles{$Filename} = 1;
244       $UsersCommitted{$UID} = 1;
245     } elsif ($Type eq 'R') {   # Removed
246       $RemovedFiles{$Filename} = 1;
247       $UsersCommitted{$UID} = 1;
248     } else {
249       $UsersUpdated{$UID} = 1;
250     }
251   }
252 }
253
254 my $UserCommitList = join "\n", keys %UsersCommitted;
255 my $UserUpdateList = join "\n", keys %UsersUpdated;
256 my $AddedFilesList = AddPreTag join "\n", keys %AddedFiles;
257 my $ModifiedFilesList = AddPreTag join "\n", keys %ModifiedFiles;
258 my $RemovedFilesList = AddPreTag join "\n", keys %RemovedFiles;
259
260 my $TestError = 1;
261 my $ProgramsTable;
262
263 # If we build the tree successfully, the nightly programs tests...
264 if ($BuildError eq "") {
265   chdir "test/Programs" or die "Could not change into programs testdir!";
266
267   # Run the programs tests... creating a report.nightly.html file
268   if (!$NOTEST) {
269     system "gmake $MAKEOPTS report.nightly.html TEST=nightly "
270          . "RUNTIMELIMIT=300 > $Prefix-ProgramTest.txt 2>&1";
271   } else {
272     system "gunzip $Prefix-ProgramTest.txt.gz";
273   }
274
275   if (`grep '^gmake: .*Error' $Prefix-ProgramTest.txt | wc -l` + 0) {
276     $TestError = 1;
277     $ProgramsTable = "<font color=white><h2>Error running tests!</h2></font>";
278   } elsif (`grep '^gmake: .*No rule to make target' $Prefix-ProgramTest.txt | wc -l` + 0) {
279     $TestError = 1;
280     $ProgramsTable =
281       "<font color=white><h2>Makefile error running tests!</h2></font>";
282   } else {
283     $TestError = 0;
284     $ProgramsTable = ReadFile "report.nightly.html";
285
286     #
287     # Create a list of the tests which were run...
288     #
289     system "grep -E 'TEST-(PASS|FAIL)' < $Prefix-ProgramTest.txt "
290          . "| sort > $Prefix-Tests.txt";
291   }
292
293   # Compress the test output
294   system "gzip -f $Prefix-ProgramTest.txt";
295 }
296
297 my ($TestsAdded, $TestsRemoved, $TestsFixed, $TestsBroken) = ("","","","");
298
299 if ($TestError) {
300   $TestsAdded   = "<b>error testing</b><br>";
301   $TestsRemoved = "<b>error testing</b><br>";
302   $TestsFixed   = "<b>error testing</b><br>";
303   $TestsBroken  = "<b>error testing</b><br>";
304 } else {
305   my ($RTestsAdded, $RTestsRemoved) = DiffFiles "-Tests.txt";
306
307   my @RawTestsAddedArray = split '\n', $RTestsAdded;
308   my @RawTestsRemovedArray = split '\n', $RTestsRemoved;
309
310   my %OldTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
311     @RawTestsRemovedArray;
312   my %NewTests = map {GetRegex('TEST-....: (.+)', $_)=>$_}
313     @RawTestsAddedArray;
314
315   foreach $Test (keys %NewTests) {
316     if (!exists $OldTests{$Test}) {  # TestAdded if in New but not old
317       $TestsAdded = "$TestsAdded$Test\n";
318     } else {
319       if ($OldTests{$Test} =~ /TEST-PASS/) {  # Was the old one a pass?
320         $TestsBroken = "$TestsBroken$Test\n";  # New one must be a failure
321       } else {
322         $TestsFixed = "$TestsFixed$Test\n";    # No, new one is a pass.
323       }
324     }
325   }
326   foreach $Test (keys %OldTests) {  # TestRemoved if in Old but not New
327     $TestsRemoved = "$TestsRemoved$Test\n" if (!exists $NewTests{$Test});
328   }
329
330   $TestsAdded   = AddPreTag $TestsAdded;
331   $TestsRemoved = AddPreTag $TestsRemoved;
332   $TestsFixed   = AddPreTag $TestsFixed;
333   $TestsBroken  = AddPreTag $TestsBroken;
334 }
335
336 #
337 # Get a list of the previous days that we can link to...
338 #
339 my @PrevDays = map {s/.html//; $_} GetDir ".html";
340
341 splice @PrevDays, 20;  # Trim down list to something reasonable...
342
343 my $PrevDaysList =     # Format list for sidebar
344   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
345
346
347 #
348 # Remove the cvs tree...
349 #
350 chdir $WebDir or die "Could not change into web directory!";
351 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
352
353
354 #
355 # Print out information...
356 #
357 if (0) {
358   print "DateString: $DateString\n";
359   print "CVS Checkout: $CVSCheckoutTime seconds\n";
360   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
361
362   print "Build Time: $BuildTime seconds\n";
363   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
364
365   print "WARNINGS:\n  $WarningsList\n";
366
367
368   print "Users committed: $UserCommitList\n";
369   print "Added Files: \n  $AddedFilesList\n";
370   print "Modified Files: \n  $ModifiedFilesList\n";
371   print "Removed Files: \n  $RemovedFilesList\n";
372
373   print "Previous Days =\n  $PrevDaysList\n";
374 }
375
376
377 #
378 # Output the files...
379 #
380
381 # Main HTML file...
382 my $Output;
383 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
384 WriteFile "$DATE.html", $Output;
385
386 # Change the index.html symlink...
387 system "ln -sf $DATE.html index.html";
388
389 sub AddRecord {
390   my ($Val, $Filename) = @_;
391   my @Records;
392   if (open FILE, $Filename) {
393     @Records = grep !/$DATE/, split "\n", <FILE>;
394     close FILE;
395   }
396   push @Records, "$DATE: $Val";
397   WriteFile $Filename, (join "\n", @Records) . "\n";
398   return @Records;
399 }
400
401 # Add information to the files which accumulate information for graphs...
402 AddRecord($LOC, "running_loc.txt");
403 AddRecord($BuildTime, "running_build_time.txt");