Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / testing / performance / talos / ffprocess_linux.py
blobae0a0b75e4fbc1409d1dfe4684b51e9df6b5dc99
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
14 # The Original Code is standalone Firefox Windows performance test.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
20 # Contributor(s):
21 # Annie Sullivan <annie.sullivan@gmail.com> (original author)
22 # Ben Hearsum <bhearsum@wittydomain.com> (OS independence)
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
38 import subprocess
39 import signal
40 import os
41 from select import select
42 import time
46 def GenerateFirefoxCommandLine(firefox_path, profile_dir, url):
47 """Generates the command line for a process to run Firefox
49 Args:
50 firefox_path: String containing the path to the firefox exe to use
51 profile_dir: String containing the directory of the profile to run Firefox in
52 url: String containing url to start with.
53 """
55 profile_arg = ''
56 if profile_dir:
57 profile_arg = '-profile %s' % profile_dir
59 cmd = '%s %s %s' % (firefox_path,
60 profile_arg,
61 url)
62 return cmd
65 def GetPidsByName(process_name):
66 """Searches for processes containing a given string.
67 This function is UNIX specific.
69 Args:
70 process_name: The string to be searched for
72 Returns:
73 A list of PIDs containing the string. An empty list is returned if none are
74 found.
75 """
77 matchingPids = []
79 command = ['ps', 'ax']
80 handle = subprocess.Popen(command, stdout=subprocess.PIPE)
82 # wait for the process to terminate
83 handle.wait()
84 data = handle.stdout.read()
86 # find all matching processes and add them to the list
87 for line in data.splitlines():
88 if line.find(process_name) >= 0:
89 # splits by whitespace, the first one should be the pid
90 pid = int(line.split()[0])
91 matchingPids.append(pid)
93 return matchingPids
96 def ProcessesWithNameExist(*process_names):
97 """Returns true if there are any processes running with the
98 given name. Useful to check whether a Firefox process is still running
100 Args:
101 process_names: String or strings containing the process name, i.e. "firefox"
103 Returns:
104 True if any processes with that name are running, False otherwise.
107 for process_name in process_names:
108 pids = GetPidsByName(process_name)
109 if len(pids) > 0:
110 return True
111 return False
114 def TerminateProcess(pid):
115 """Helper function to terminate a process, given the pid
117 Args:
118 pid: integer process id of the process to terminate.
120 try:
121 if ProcessesWithNameExist(str(pid)):
122 os.kill(pid, signal.SIGTERM)
123 time.sleep(5)
124 if ProcessesWithNameExist(str(pid)):
125 os.kill(pid, signal.SIGKILL)
126 except OSError, (errno, strerror):
127 print 'WARNING: failed os.kill: %s : %s' % (errno, strerror)
129 def TerminateAllProcesses(*process_names):
130 """Helper function to terminate all processes with the given process name
132 Args:
133 process_names: String or strings containing the process name, i.e. "firefox"
136 # Get all the process ids of running instances of this process,
137 # and terminate them
138 for process_name in process_names:
139 pids = GetPidsByName(process_name)
140 for pid in pids:
141 TerminateProcess(pid)
144 def NonBlockingReadProcessOutput(handle):
145 """Does a non-blocking read from the output of the process
146 with the given handle.
148 Args:
149 handle: The process handle returned from os.popen()
151 Returns:
152 A tuple (bytes, output) containing the number of output
153 bytes read, and the actual output.
156 output = ""
157 num_avail = 0
159 # check for data
160 # select() does not seem to work well with pipes.
161 # after data is available once it *always* thinks there is data available
162 # readline() will continue to return an empty string however
163 # so we can use this behavior to work around the problem
164 while select([handle], [], [], 0)[0]:
165 line = handle.readline()
166 if line:
167 output += line
168 else:
169 break
170 # this statement is true for encodings that have 1byte/char
171 num_avail = len(output)
173 return (num_avail, output)