Remove dedicated HashSet<Element*> for DocumentTimeline::runningAnimationsForElementA...
[WebKit-https.git] / Tools / BuildSlaveSupport / kill-old-processes
1 #!/usr/bin/env python
2 # Copyright (C) 2010 Apple Inc.  All rights reserved.
3 # Copyright (C) 2011 Google Inc.  All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1.  Redistributions of source code must retain the above copyright
10 #     notice, this list of conditions and the following disclaimer.
11 # 2.  Redistributions in binary form must reproduce the above copyright
12 #     notice, this list of conditions and the following disclaimer in the
13 #     documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 from __future__ import print_function
27 import os, sys
28 import getpass
29 import re
30 import subprocess
31
32 def listAllWebKitPrograms(builddir_bin):
33     foundWebKitPrograms = []
34     for root, dirs, files in os.walk(builddir_bin):
35         for file in files:
36             if os.access(os.path.join(root, file), os.X_OK):
37                 foundWebKitPrograms.append(file)
38     return foundWebKitPrograms
39
40 def removeOrphanShmSegments():
41     # When Apache2 is not gracefully shut down, it may leave orphaned shared memory segments
42     # that could cause following server instances to abort on startup.
43     try:
44         username = getpass.getuser()
45         segments = orphanedShmSegmentsByUser(username)
46         for s in segments:
47             subprocess.check_call(["ipcrm", "-m", s])
48     except Exception as e:
49         print("Error removing orphaned shared memory segments: {}".format(e), file=sys.stderr)
50
51 def orphanedShmSegmentsByUser(username):
52     ret = []
53     output = subprocess.check_output(["ipcs", "-m"])
54     lines = output.split('\n')
55     if len(lines) < 3:
56         return []
57
58     headers = [header.strip() for header in lines[2].split()]
59     wanted_headers = ['nattch', 'owner', 'shmid']
60
61     if any(h not in headers for h in wanted_headers):
62         raise RuntimeError("Failed to find required nattch, owner and shmid cols in ipcs output")
63     nattch_idx = headers.index('nattch')
64     owner_idx = headers.index('owner')
65     shmid_idx = headers.index('shmid')
66
67     for line in lines[3:]:
68         line = line.strip()
69         tokens = [x.strip() for x in line.split()]
70         try:
71             if tokens[nattch_idx] == '0' and tokens[owner_idx] == username:
72                 ret.append(tokens[shmid_idx])
73         except IndexError:
74             continue
75     return ret
76
77 def main(user=None):
78     tasksToKillWin = [
79         "cl.exe",
80         "devenv.com",
81         "devenv.exe",
82         "DumpRenderTree.exe",
83         "DumpRenderTree_debug.exe",
84         "httpd.exe",
85         "imagediff.exe",
86         "imagediff_debug.exe",
87         "jsc.exe",
88         "jsc_debug.exe",
89         "LightTPD.exe",
90         "link.exe",
91         "midl.exe",
92         "perl.exe",
93         "Safari.exe",
94         "svn.exe",
95         "testapi.exe",
96         "testapi_debug.exe",
97         "VcBuildHelper.exe",
98         "wdiff.exe",
99         "WebKitNetworkProcess.exe",
100         "WebKitNetworkProcess_debug.exe",
101         "WebKitWebProcess.exe",
102         "WebKitWebProcess_debug.exe",
103         "WebKitTestRunner.exe",
104         "WebKitTestRunner_debug.exe",
105     ]
106
107     tasksToKillMac = [
108         "apache2",
109         "cc1plus",
110         "cc1objplus",
111         "clang",
112         r"clang\\+\\+",
113         "gcc-4.2",
114         "httpd",
115         "i686-apple-darwin10-gcc-4.2.1",
116         "ImageDiff",
117         "jsc",
118         "LayoutTestHelper",
119         "make",
120         "perl5.12",
121         "perl5.16",
122         "perl5.18",        
123         "perl",
124         "Problem Reporter",
125         "ruby",
126         "Safari Web Content",
127         "Safari",
128         "svn",
129         "DumpRenderTree",
130         "TestWebKitAPI Web Content",
131         "TestWebKitAPI",
132         "PluginProcess",
133         "com.apple.WebKit.Plugin.64",
134         "com.apple.WebKit.Plugin.32",
135         "com.apple.WebKit.PluginProcess",
136         "com.apple.WebKit.PluginProcess.Development"
137         "WebKitTestRunner Web Content",
138         "WebKitTestRunner",
139         "WebProcess",
140         "xcodebuild",
141         # FIXME: Consider moving iOS-specific tasks to their own list.
142         "Simulator.app",
143     ]
144
145     taskToKillUnix = [
146         # System process that are executed as part of the build process
147         "cc1plus",
148         "ld.gold",
149         "ld",
150         "make",
151         "cmake",
152         "svn",
153         # System process that are executed as part of the test steps
154         "Xvfb",
155         "gdb",
156         "ruby",
157         "apache2",
158         "httpd",
159         # WebKit related process names are automatically generated for GTK+/WPE/JSCOnly
160     ]
161
162     # This allows to specify on the environment of the bot a list of process that should be killed.
163     tasksToKill = os.getenv("WEBKITBOT_TASKSTOKILL", "").split()
164     if sys.platform == 'darwin':
165         os.system("ps aux")
166         for task in tasksToKill + tasksToKillMac:
167             os.system("killall -9 -v -m " + task)
168         # Shutdown any simulators
169         if os.geteuid() == 0 and user:
170             os.system("sudo -u {} xcrun simctl shutdown booted".format(user))
171         else:
172             os.system("xcrun simctl shutdown booted")
173         # Kill all instances of python executing run-webkit-tests
174         os.system("ps aux | grep -E '.+/Python .+(run_webkit_tests|run-webkit-tests|mod_pywebsocket)' | grep -v grep | awk '{print $2}' | xargs kill")
175     elif sys.platform == 'cygwin' or sys.platform == 'win32':
176         for task in tasksToKill + tasksToKillWin:
177             os.system("taskkill /t /f /im " + task)
178     elif sys.platform.startswith('linux'):
179         os.system("ps aux")
180         builddir_bin = "WebKitBuild/Release/bin" if os.path.isdir("WebKitBuild/Release/bin") else "WebKitBuild/Debug/bin"
181         for task in tasksToKill + taskToKillUnix + listAllWebKitPrograms(builddir_bin):
182             os.system("killall -9 -v " + task)
183         os.system("ps aux | grep -P '.+/python .+(run_webkit_tests|run-webkit-tests|mod_pywebsocket)' | grep -v grep | awk '{print $2}' | xargs -r kill")
184         removeOrphanShmSegments()
185     else:
186         sys.exit()
187         # FIXME: Should we return an exit code based on how the kills went?
188
189
190 if __name__ == '__main__':
191     user = sys.argv[1] if len(sys.argv) > 1 else None
192     sys.exit(main(user))