Fix bogus assertion (537854, r=mrbkap).
[mozilla-central.git] / build / profile_pageloader.pl
blob45a0b9c50a70487f767b75f2c21d9912b9f32a6a
1 #!/usr/bin/perl
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
13 # License.
15 # The Original Code is the Mozilla Browser code.
17 # The Initial Developer of the Original Code is
18 # Netscape Communications Corporation.
19 # Portions created by the Initial Developer are Copyright (C) 2002
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
23 # Chris Mcafee <mcafee@netscape.com>
24 # Brian Ryner <bryner@brianryner.com>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
40 use Cwd;
41 use File::Find ();
43 use POSIX qw(sys_wait_h);
45 sub kill_process {
46 my ($target_pid) = @_;
47 my $start_time = time;
49 # Try to kill and wait 10 seconds, then try a kill -9
50 my $sig;
51 for $sig ('TERM', 'KILL') {
52 print "kill $sig $target_pid\n";
53 kill $sig => $target_pid;
54 my $interval_start = time;
55 while (time - $interval_start < 10) {
56 # the following will work with 'cygwin' perl on win32, but not
57 # with 'MSWin32' (ActiveState) perl
58 my $pid = waitpid($target_pid, POSIX::WNOHANG());
59 if (($pid == $target_pid and POSIX::WIFEXITED($?)) or $pid == -1) {
60 my $secs = time - $start_time;
61 $secs = $secs == 1 ? '1 second' : "$secs seconds";
62 print "Process killed. Took $secs to die.\n";
63 return;
65 sleep 1;
68 die "Unable to kill process: $target_pid";
71 # Stripped down version of fork_and_log().
72 sub system_fork_and_log {
73 # Fork a sub process and log the output.
74 my ($cmd) = @_;
76 my $pid = fork; # Fork off a child process.
78 unless ($pid) { # child
79 exec { $cmd->[0] } @$cmd;
80 die "Could not exec()";
82 return $pid;
86 sub wait_for_pid {
87 # Wait for a process to exit or kill it if it takes too long.
88 my ($pid, $timeout_secs) = @_;
89 my ($exit_value, $signal_num, $dumped_core, $timed_out) = (0,0,0,0);
90 my $sig_name;
91 my $loop_count;
93 die ("Invalid timeout value passed to wait_for_pid()\n")
94 if ($timeout_secs <= 0);
96 eval {
97 $loop_count = 0;
98 while (++$loop_count < $timeout_secs) {
99 my $wait_pid = waitpid($pid, POSIX::WNOHANG());
100 # the following will work with 'cygwin' perl on win32, but not
101 # with 'MSWin32' (ActiveState) perl
102 last if ($wait_pid == $pid and POSIX::WIFEXITED($?)) or $wait_pid == -1;
103 sleep 1;
106 $exit_value = $? >> 8;
107 $signal_num = $? >> 127;
108 $dumped_core = $? & 128;
109 if ($loop_count >= $timeout_secs) {
110 die "timeout";
112 return "done";
115 if ($@) {
116 if ($@ =~ /timeout/) {
117 kill_process($pid);
118 $timed_out = 1;
119 } else { # Died for some other reason.
120 die; # Propagate the error up.
123 # $sig_name = $signal_num ? signal_name($signal_num) : '';
125 # return { timed_out=>$timed_out,
126 # exit_value=>$exit_value,
127 # sig_name=>$sig_name,
128 # dumped_core=>$dumped_core };
131 # System version of run_cmd().
132 sub run_system_cmd {
133 my ($cmd, $timeout_secs) = @_;
135 # print_log "cmd = $cmd\n";
136 my $pid = system_fork_and_log($cmd);
137 my $result = wait_for_pid($pid, $timeout_secs);
139 return $result;
143 # Given profile directory, find pref file hidden in salt directory.
144 # profile $Settings::MozProfileName must exist before calling this sub.
146 sub find_pref_file {
147 my $profile_dir = shift;
149 # default to *nix
150 my $pref_file = "prefs.js";
152 unless (-e $profile_dir) {
153 return; # empty list
156 my $found = undef;
157 my $sub = sub {$pref_file = $File::Find::name, $found++ if $pref_file eq $_};
158 File::Find::find($sub, $profile_dir);
159 unless ($found) {
160 return; # empty list
163 return $pref_file;
166 my $topdir = cwd();
168 chdir $ENV{OBJDIR};
169 my $app_name = `grep "MOZ_APP_NAME " config/autoconf.mk | sed "s/.*= //"`;
170 chomp($app_name);
172 # On mac, the app directory is the product name with the first
173 # letter capitalized
175 my $toolkit = `grep "MOZ_WIDGET_TOOLKIT " config/autoconf.mk |sed "s/.*= //"`;
176 chomp($toolkit);
178 if ($toolkit =~ /(mac|cocoa)/) {
179 my $app_dir = uc(substr($app_name, 0, 1)).substr($app_name, 1);
180 chdir "dist/$app_dir.app/Contents/MacOS";
181 } else {
182 chdir "dist/bin";
185 my $bin_suffix = "";
186 if ($toolkit =~ /(windows|os2)/) {
187 $bin_suffix = ".exe";
190 my $old_home = $ENV{HOME};
191 $ENV{HOME} = cwd();
193 # Create a profile to test with.
194 run_system_cmd(["./".$app_name.$bin_suffix, "-createProfile", "testprofile"], 45);
196 my $pref_file = find_pref_file(".mozilla/".$app_name);
197 open PREFS, ">>$pref_file";
198 # Add allow_scripts_to_close_windows; this lets us cleanly exit.
199 print PREFS "user_pref(\"dom.allow_scripts_to_close_windows\", true);\n";
200 # Suppress the default browser dialog since it keeps the test from starting.
201 print PREFS "user_pref(\"browser.shell.checkDefaultBrowser\", false);\n";
202 close PREFS;
204 # Run the pageload test.
205 run_system_cmd(["./".$app_name.$bin_suffix, $ENV{PAGELOAD_URL}."/loader.pl?maxcyc=2&delay=500&nocache=0&timeout=30000&auto=1"], 240);
207 # Start up again; this will gather data for reading global history and
208 # reading the fastload file.
209 run_system_cmd(["./".$app_name.$bin_suffix, "file://$topdir/build/profile_pageloader.html"], 45);
211 chdir $topdir;