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