OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import fcntl | 5 import fcntl |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import psutil | 8 import psutil |
9 import re | 9 import re |
10 import sys | 10 import sys |
11 import time | 11 import time |
12 | 12 |
13 import android_commands | 13 import android_commands |
14 import cmd_helper | 14 import cmd_helper |
15 import constants | 15 import constants |
16 | 16 |
17 from pylib import valgrind_tools | 17 from pylib import valgrind_tools |
18 | 18 |
19 | 19 |
20 def _MakeBinaryPath(build_type, binary_name): | |
21 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) | |
22 | |
23 | |
24 def _GetProcessStartTime(pid): | 20 def _GetProcessStartTime(pid): |
25 return psutil.Process(pid).create_time | 21 return psutil.Process(pid).create_time |
26 | 22 |
27 | 23 |
28 class _FileLock(object): | 24 class _FileLock(object): |
29 """With statement-aware implementation of a file lock. | 25 """With statement-aware implementation of a file lock. |
30 | 26 |
31 File locks are needed for cross-process synchronization when the | 27 File locks are needed for cross-process synchronization when the |
32 multiprocessing Python module is used. | 28 multiprocessing Python module is used. |
33 """ | 29 """ |
(...skipping 23 matching lines...) Expand all Loading... |
57 _MULTIPROCESSING_ENV_VAR = 'CHROME_FORWARDER_USE_MULTIPROCESSING' | 53 _MULTIPROCESSING_ENV_VAR = 'CHROME_FORWARDER_USE_MULTIPROCESSING' |
58 | 54 |
59 _instance = None | 55 _instance = None |
60 | 56 |
61 @staticmethod | 57 @staticmethod |
62 def UseMultiprocessing(): | 58 def UseMultiprocessing(): |
63 """Tells the forwarder that multiprocessing is used.""" | 59 """Tells the forwarder that multiprocessing is used.""" |
64 os.environ[Forwarder._MULTIPROCESSING_ENV_VAR] = '1' | 60 os.environ[Forwarder._MULTIPROCESSING_ENV_VAR] = '1' |
65 | 61 |
66 @staticmethod | 62 @staticmethod |
67 def Map(port_pairs, adb, build_type='Debug', tool=None): | 63 def Map(port_pairs, adb, tool=None): |
68 """Runs the forwarder. | 64 """Runs the forwarder. |
69 | 65 |
70 Args: | 66 Args: |
71 port_pairs: A list of tuples (device_port, host_port) to forward. Note | 67 port_pairs: A list of tuples (device_port, host_port) to forward. Note |
72 that you can specify 0 as a device_port, in which case a | 68 that you can specify 0 as a device_port, in which case a |
73 port will by dynamically assigned on the device. You can | 69 port will by dynamically assigned on the device. You can |
74 get the number of the assigned port using the | 70 get the number of the assigned port using the |
75 DevicePortForHostPort method. | 71 DevicePortForHostPort method. |
76 adb: An AndroidCommands instance. | 72 adb: An AndroidCommands instance. |
77 tool: Tool class to use to get wrapper, if necessary, for executing the | 73 tool: Tool class to use to get wrapper, if necessary, for executing the |
78 forwarder (see valgrind_tools.py). | 74 forwarder (see valgrind_tools.py). |
79 | 75 |
80 Raises: | 76 Raises: |
81 Exception on failure to forward the port. | 77 Exception on failure to forward the port. |
82 """ | 78 """ |
83 if not tool: | 79 if not tool: |
84 tool = valgrind_tools.CreateTool(None, adb) | 80 tool = valgrind_tools.CreateTool(None, adb) |
85 with _FileLock(Forwarder._LOCK_PATH): | 81 with _FileLock(Forwarder._LOCK_PATH): |
86 instance = Forwarder._GetInstanceLocked(build_type, tool) | 82 instance = Forwarder._GetInstanceLocked(tool) |
87 instance._InitDeviceLocked(adb, tool) | 83 instance._InitDeviceLocked(adb, tool) |
88 | 84 |
89 device_serial = adb.Adb().GetSerialNumber() | 85 device_serial = adb.Adb().GetSerialNumber() |
90 redirection_commands = [ | 86 redirection_commands = [ |
91 ['--serial-id=' + device_serial, '--map', str(device), | 87 ['--serial-id=' + device_serial, '--map', str(device), |
92 str(host)] for device, host in port_pairs] | 88 str(host)] for device, host in port_pairs] |
93 logging.info('Forwarding using commands: %s', redirection_commands) | 89 logging.info('Forwarding using commands: %s', redirection_commands) |
94 | 90 |
95 for redirection_command in redirection_commands: | 91 for redirection_command in redirection_commands: |
96 try: | 92 try: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 @staticmethod | 126 @staticmethod |
131 def UnmapAllDevicePorts(adb): | 127 def UnmapAllDevicePorts(adb): |
132 """Unmaps all the previously forwarded ports for the provided device. | 128 """Unmaps all the previously forwarded ports for the provided device. |
133 | 129 |
134 Args: | 130 Args: |
135 adb: An AndroidCommands instance. | 131 adb: An AndroidCommands instance. |
136 port_pairs: A list of tuples (device_port, host_port) to unmap. | 132 port_pairs: A list of tuples (device_port, host_port) to unmap. |
137 """ | 133 """ |
138 with _FileLock(Forwarder._LOCK_PATH): | 134 with _FileLock(Forwarder._LOCK_PATH): |
139 port_map = Forwarder._GetInstanceLocked( | 135 port_map = Forwarder._GetInstanceLocked( |
140 None, None)._device_to_host_port_map | 136 None)._device_to_host_port_map |
141 adb_serial = adb.Adb().GetSerialNumber() | 137 adb_serial = adb.Adb().GetSerialNumber() |
142 for (device_serial, device_port) in port_map.keys(): | 138 for (device_serial, device_port) in port_map.keys(): |
143 if adb_serial == device_serial: | 139 if adb_serial == device_serial: |
144 Forwarder._UnmapDevicePortLocked(device_port, adb) | 140 Forwarder._UnmapDevicePortLocked(device_port, adb) |
145 | 141 |
146 @staticmethod | 142 @staticmethod |
147 def DevicePortForHostPort(host_port): | 143 def DevicePortForHostPort(host_port): |
148 """Returns the device port that corresponds to a given host port.""" | 144 """Returns the device port that corresponds to a given host port.""" |
149 with _FileLock(Forwarder._LOCK_PATH): | 145 with _FileLock(Forwarder._LOCK_PATH): |
150 (device_serial, device_port) = Forwarder._GetInstanceLocked( | 146 (device_serial, device_port) = Forwarder._GetInstanceLocked( |
151 None, None)._host_to_device_port_map.get(host_port) | 147 None)._host_to_device_port_map.get(host_port) |
152 return device_port | 148 return device_port |
153 | 149 |
154 @staticmethod | 150 @staticmethod |
155 def _GetInstanceLocked(build_type, tool): | 151 def _GetInstanceLocked(tool): |
156 """Returns the singleton instance. | 152 """Returns the singleton instance. |
157 | 153 |
158 Note that the global lock must be acquired before calling this method. | 154 Note that the global lock must be acquired before calling this method. |
159 | 155 |
160 Args: | 156 Args: |
161 build_type: 'Release' or 'Debug' | |
162 tool: Tool class to use to get wrapper, if necessary, for executing the | 157 tool: Tool class to use to get wrapper, if necessary, for executing the |
163 forwarder (see valgrind_tools.py). | 158 forwarder (see valgrind_tools.py). |
164 """ | 159 """ |
165 if not Forwarder._instance: | 160 if not Forwarder._instance: |
166 Forwarder._instance = Forwarder(build_type, tool) | 161 Forwarder._instance = Forwarder(tool) |
167 return Forwarder._instance | 162 return Forwarder._instance |
168 | 163 |
169 def __init__(self, build_type, tool): | 164 def __init__(self, tool): |
170 """Constructs a new instance of Forwarder. | 165 """Constructs a new instance of Forwarder. |
171 | 166 |
172 Note that Forwarder is a singleton therefore this constructor should be | 167 Note that Forwarder is a singleton therefore this constructor should be |
173 called only once. | 168 called only once. |
174 | 169 |
175 Args: | 170 Args: |
176 build_type: 'Release' or 'Debug' | |
177 tool: Tool class to use to get wrapper, if necessary, for executing the | 171 tool: Tool class to use to get wrapper, if necessary, for executing the |
178 forwarder (see valgrind_tools.py). | 172 forwarder (see valgrind_tools.py). |
179 """ | 173 """ |
180 assert not Forwarder._instance | 174 assert not Forwarder._instance |
181 self._build_type = build_type | |
182 self._tool = tool | 175 self._tool = tool |
183 self._initialized_devices = set() | 176 self._initialized_devices = set() |
184 self._device_to_host_port_map = dict() | 177 self._device_to_host_port_map = dict() |
185 self._host_to_device_port_map = dict() | 178 self._host_to_device_port_map = dict() |
186 self._host_forwarder_path = _MakeBinaryPath( | 179 self._host_forwarder_path = os.path.join( |
187 self._build_type, 'host_forwarder') | 180 constants.GetOutDirectory(), 'host_forwarder') |
188 if not os.path.exists(self._host_forwarder_path): | 181 assert os.path.exists(self._host_forwarder_path), 'Please build forwarder2' |
189 self._build_type = 'Release' if self._build_type == 'Debug' else 'Debug' | |
190 self._host_forwarder_path = _MakeBinaryPath( | |
191 self._build_type, 'host_forwarder') | |
192 assert os.path.exists( | |
193 self._host_forwarder_path), 'Please build forwarder2' | |
194 self._device_forwarder_path_on_host = os.path.join( | 182 self._device_forwarder_path_on_host = os.path.join( |
195 cmd_helper.OutDirectory.get(), self._build_type, 'forwarder_dist') | 183 constants.GetOutDirectory(), 'forwarder_dist') |
196 self._InitHostLocked() | 184 self._InitHostLocked() |
197 | 185 |
198 @staticmethod | 186 @staticmethod |
199 def _UnmapDevicePortLocked(device_port, adb): | 187 def _UnmapDevicePortLocked(device_port, adb): |
200 """Internal method used by UnmapDevicePort(). | 188 """Internal method used by UnmapDevicePort(). |
201 | 189 |
202 Note that the global lock must be acquired before calling this method. | 190 Note that the global lock must be acquired before calling this method. |
203 """ | 191 """ |
204 instance = Forwarder._GetInstanceLocked(None, None) | 192 instance = Forwarder._GetInstanceLocked(None) |
205 serial = adb.Adb().GetSerialNumber() | 193 serial = adb.Adb().GetSerialNumber() |
206 serial_with_port = (serial, device_port) | 194 serial_with_port = (serial, device_port) |
207 if not serial_with_port in instance._device_to_host_port_map: | 195 if not serial_with_port in instance._device_to_host_port_map: |
208 logging.error('Trying to unmap non-forwarded port %d' % device_port) | 196 logging.error('Trying to unmap non-forwarded port %d' % device_port) |
209 return | 197 return |
210 redirection_command = ['--serial-id=' + serial, '--unmap', str(device_port)] | 198 redirection_command = ['--serial-id=' + serial, '--unmap', str(device_port)] |
211 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 199 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
212 [instance._host_forwarder_path] + redirection_command) | 200 [instance._host_forwarder_path] + redirection_command) |
213 if exit_code != 0: | 201 if exit_code != 0: |
214 logging.error('%s exited with %d:\n%s' % ( | 202 logging.error('%s exited with %d:\n%s' % ( |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 Forwarder._DEVICE_FORWARDER_PATH)) | 301 Forwarder._DEVICE_FORWARDER_PATH)) |
314 # TODO(pliard): Remove the following call to KillAllBlocking() when we are | 302 # TODO(pliard): Remove the following call to KillAllBlocking() when we are |
315 # sure that the old version of device_forwarder (not supporting | 303 # sure that the old version of device_forwarder (not supporting |
316 # 'kill-server') is not running on the bots anymore. | 304 # 'kill-server') is not running on the bots anymore. |
317 timeout_sec = 5 | 305 timeout_sec = 5 |
318 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) | 306 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) |
319 if not processes_killed: | 307 if not processes_killed: |
320 pids = adb.ExtractPid('device_forwarder') | 308 pids = adb.ExtractPid('device_forwarder') |
321 if pids: | 309 if pids: |
322 raise Exception('Timed out while killing device_forwarder') | 310 raise Exception('Timed out while killing device_forwarder') |
OLD | NEW |