f3170b974341c30450c3bcfd91d470dd7504629e
[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 WriteFile {  # (filename, contents)
25   open (FILE, ">$_[0]") or die "Could not open file '$_[0]' for writing!";
26   print FILE $_[1];
27   close FILE;
28 }
29
30 sub GetRegex {   # (Regex with ()'s, value)
31   $_[1] =~ /$_[0]/;
32   return $1;
33 }
34
35 sub AddPreTag {  # Add pre tags around nonempty list, or convert to "none"
36   $_ = shift;
37   if (length) { return "<ul><pre>$_</pre></ul>"; } else { "<b>none</b><br>"; }
38 }
39
40 sub GetDir {
41   my $Suffix = shift;
42   opendir DH, $WebDir;
43   my @Result = reverse sort grep !/$DATE/, grep /[-0-9]+$Suffix/, readdir DH;
44   closedir DH;
45   return @Result;
46 }
47
48 # DiffFiles - Diff the current version of the file against the last version of
49 # the file, reporting things added and removed.  This is used to report, for
50 # example, added and removed warnings.  This returns a pair (added, removed)
51 #
52 sub DiffFiles {
53   my $Suffix = shift;
54   my @Others = GetDir $Suffix;
55   if (@Others == 0) {  # No other files?  We added all entries...
56     return (`cat $WebDir/$DATE$Suffix`, "");
57   }
58   # Diff the files now...
59   my @Diffs = split "\n", `diff $WebDir/$DATE$Suffix $WebDir/$Others[0]`;
60   my $Added   = join "\n", grep /^</, @Diffs;
61   my $Removed = join "\n", grep /^>/, @Diffs;
62   $Added =~ s/^< //gm;
63   $Removed =~ s/^> //gm;
64   return ($Added, $Removed);
65 }
66
67
68 # Command line argument settings...
69 my $NOCHECKOUT = 0;
70 my $NOREMOVE   = 0;
71 my $MAKEOPTS   = "";
72
73 # Parse arguments...
74 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
75   shift;
76   last if /^--$/;  # Stop processing arguments on --
77
78   # List command line options here...
79   if (/^-nocheckout$/) { $NOCHECKOUT = 1; next; }
80   if (/^-noremove$/)   { $NOREMOVE   = 1; next; }
81   if (/^-parallel$/)   { $MAKEOPTS   = "-j2 -l3.0"; next; }
82
83   print "Unknown option: $_ : ignoring!\n";
84 }
85
86 die "Must specify 0 or 3 options!" if (@ARGV != 0 and @ARGV != 3);
87
88 # FIXME: This should just be utils/...
89 my $Template   = "$HOME/llvm/utils/NightlyTestTemplate.html";
90
91 if (@ARGV == 3) {
92   $CVSRootDir = $ARGV[0];
93   $BuildDir   = $ARGV[1];
94   $WebDir     = $ARGV[2];
95 }
96
97 my $Prefix = "$WebDir/$DATE";
98
99 if (0) {
100   print "CVS Root = $CVSRootDir\n";
101   print "BuildDir = $BuildDir\n";
102   print "WebDir   = $WebDir\n";
103   print "Prefix   = $Prefix\n";
104 }
105
106 # Create the CVS repository directory
107 if (!$NOCHECKOUT) {
108   mkdir $BuildDir or die "Could not create CVS checkout directory!";
109 }
110 chdir $BuildDir or die "Could not change to CVS checkout directory!";
111
112 # Check out the llvm tree, saving CVS messages to the cvs log...
113 system "(time -p cvs -d $CVSRootDir co llvm) > $Prefix-CVS-Log.txt 2>&1"
114   if (!$NOCHECKOUT);
115
116 chdir "llvm" or die "Could not change into llvm directory!";
117
118 # Read in the HTML template file...
119 undef $/;
120 open (TEMPLATEFILE, $Template) or die "Could not open file 'llvm/$Template'!";
121 my $TemplateContents = <TEMPLATEFILE>;
122 close(TEMPLATEFILE);
123
124 # Get some static statistics about the current state of CVS
125 my $CVSCheckoutTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-CVS-Log.txt`;
126 my $NumFilesInCVS = `grep ^U $Prefix-CVS-Log.txt | wc -l` + 0;
127 my $NumDirsInCVS  = `grep '^cvs checkout' $Prefix-CVS-Log.txt | wc -l` + 0;
128 $LOC = GetRegex "([0-9]+) +total", `wc -l \`utils/getsrcs.sh\` | grep total`;
129
130 # Build the entire tree, saving build messages to the build log
131 if (!$NOCHECKOUT) {
132   # Change the Makefile.config to build into the local directory...
133   rename "Makefile.config", "Makefile.config.orig";
134   system "sed '/^LLVM_OBJ_DIR/d' < Makefile.config.orig > Makefile.config";
135   system "echo >> Makefile.config";
136   system "echo 'LLVM_OBJ_DIR := .' >> Makefile.config";
137
138   # Change the Makefile.config to not strip executables...
139   system "echo 'KEEP_SYMBOLS := 1' >> Makefile.config";
140
141   # Build the entire tree, capturing the output into $Prefix-Build-Log.txt
142   system "(time -p gmake $MAKEOPTS) > $Prefix-Build-Log.txt 2>&1";
143 }
144
145 # Get some statistics about the build...
146 my @Linked = split '\n', `grep Linking $Prefix-Build-Log.txt`;
147 my $NumExecutables = scalar(grep(/executable/, @Linked));
148 my $NumLibraries   = scalar(grep(!/executable/, @Linked));
149 my $NumObjects     = `grep '^Compiling' $Prefix-Build-Log.txt | wc -l` + 0;
150 my $BuildTime = GetRegex "([0-9.]+)", `grep '^real' $Prefix-Build-Log.txt`;
151
152
153 # Get warnings from the build
154 my @Warn = split "\n", `grep -E 'warning:|Entering dir' $Prefix-Build-Log.txt`;
155 my @Warnings;
156 my $CurDir = "";
157
158 foreach $Warning (@Warn) {
159   if ($Warning =~ m/Entering directory \`([^\`]+)\'/) {
160     $CurDir = $1;                 # Keep track of directory warning is in...
161     if ($CurDir =~ m|$BuildDir/llvm/(.*)|) { # Remove buildir prefix if included
162       $CurDir = $1;
163     }
164   } else {
165     push @Warnings, "$CurDir/$Warning";     # Add directory to warning...
166   }
167 }
168 my $WarningsFile =  join "\n", @Warnings;
169 my $WarningsList = AddPreTag $WarningsFile;
170 $WarningsFile =~ s/:[0-9]+:/::/g;
171
172 # Emit the warnings file, so we can diff...
173 WriteFile "$WebDir/$DATE-Warnings.txt", $WarningsFile . "\n";
174 my ($WarningsAdded, $WarningsRemoved) = DiffFiles "-Warnings.txt";
175 $WarningsAdded = AddPreTag $WarningsAdded;
176 $WarningsRemoved = AddPreTag $WarningsRemoved;
177
178 # Get some statistics about CVS commits over the current day...
179 @CVSHistory = split "\n", `cvs history -D '1 day ago' -a -xAMROCGUW`;
180 #print join "\n", @CVSHistory; print "\n";
181
182 # Extract some information from the CVS history... use a hash so no duplicate
183 # stuff is stored.
184 my (%AddedFiles, %ModifiedFiles, %RemovedFiles,
185     %UsersCommitted, %UsersUpdated);
186
187 # Loop over every record from the CVS history, filling in the hashes.
188 foreach $File (@CVSHistory) {
189   my ($Type, $Date, $UID, $Rev, $Filename);
190   if ($File =~ /([AMR]) ([-:0-9 ]+\+[0-9]+) ([^ ]+) ([0-9.]+) +([^ ]+) +([^ ]+)/) {
191     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, $4, "$6/$5");
192   } elsif ($File =~ /([OCGUW]) ([-:0-9 ]+\+[0-9]+) ([^ ]+)/) {
193     ($Type, $Date, $UID, $Rev, $Filename) = ($1, $2, $3, "", "");
194   }
195   #print "Ty = $Type Date = '$Date' UID=$UID Rev=$Rev File = '$Filename'\n";
196
197   if ($Type eq 'M') {        # Modified
198     $ModifiedFiles{$Filename} = 1;
199     $UsersCommitted{$UID} = 1;
200   } elsif ($Type eq 'A') {   # Added
201     $AddedFiles{$Filename} = 1;
202     $UsersCommitted{$UID} = 1;
203   } elsif ($Type eq 'R') {   # Removed
204     $RemovedFiles{$Filename} = 1;
205     $UsersCommitted{$UID} = 1;
206   } else {
207     $UsersUpdated{$UID} = 1;
208   }
209 }
210
211 my $UserCommitList = join "\n", keys %UsersCommitted;
212 my $UserUpdateList = join "\n", keys %UsersUpdated;
213 my $AddedFilesList = AddPreTag join "\n", keys %AddedFiles;
214 my $ModifiedFilesList = AddPreTag join "\n", keys %ModifiedFiles;
215 my $RemovedFilesList = AddPreTag join "\n", keys %RemovedFiles;
216
217 # Get a list of the previous days that we can link to...
218 my @PrevDays = map {s/.html//; $_} GetDir ".html";
219
220 splice @PrevDays, 20;  # Trim down list to something reasonable...
221
222 my $PrevDaysList =     # Format list for sidebar
223   join "\n  ", map { "<a href=\"$_.html\">$_</a><br>" } @PrevDays;
224
225 #
226 # Remove the cvs tree...
227 #
228 system "rm -rf $BuildDir" if (!$NOCHECKOUT and !$NOREMOVE);
229
230 # Print out information...
231 if (0) {
232   print "DateString: $DateString\n";
233   print "CVS Checkout: $CVSCheckoutTime seconds\n";
234   print "Files/Dirs/LOC in CVS: $NumFilesInCVS/$NumDirsInCVS/$LOC\n";
235
236   print "Build Time: $BuildTime seconds\n";
237   print "Libraries/Executables/Objects built: $NumLibraries/$NumExecutables/$NumObjects\n";
238
239   print "WARNINGS:\n  $WarningsList\n";
240
241
242   print "Users committed: $UserCommitList\n";
243   print "Added Files: \n  $AddedFilesList\n";
244   print "Modified Files: \n  $ModifiedFilesList\n";
245   print "Removed Files: \n  $RemovedFilesList\n";
246
247   print "Previous Days =\n  $PrevDaysList\n";
248 }
249
250 #
251 # Output the files...
252 #
253
254 # Main HTML file...
255 chdir $WebDir or die "Could not change into web directory!";
256 my $Output;
257 eval "\$Output = <<ENDOFFILE;$TemplateContents\nENDOFFILE\n";
258 WriteFile "$DATE.html", $Output;
259
260 # Change the index.html symlink...
261 system "ln -sf $DATE.html index.html";
262
263 sub AddRecord {
264   my ($Val, $Filename) = @_;
265   my @Records;
266   if (open FILE, $Filename) {
267     @Records = grep !/$DATE/, split "\n", <FILE>;
268     close FILE;
269   }
270   push @Records, "$DATE: $Val";
271   WriteFile $Filename, (join "\n", @Records) . "\n";
272   return @Records;
273 }
274
275 # Add information to the files which accumulate information for graphs...
276 AddRecord($LOC, "running_loc.txt");
277 AddRecord($BuildTime, "running_build_time.txt");