| 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 # pylint: disable=W0212 | 5 # pylint: disable=W0212 |
| 6 | 6 |
| 7 import fcntl | 7 import fcntl |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import psutil | 10 import psutil |
| 11 | 11 |
| 12 from pylib import cmd_helper | 12 from pylib import cmd_helper |
| 13 from pylib import constants | 13 from pylib import constants |
| 14 from pylib import valgrind_tools | 14 from pylib import valgrind_tools |
| 15 from pylib.device import device_errors | |
| 16 | 15 |
| 17 # TODO(jbudorick) Remove once telemetry gets switched over. | 16 # TODO(jbudorick) Remove once telemetry gets switched over. |
| 18 import pylib.android_commands | 17 import pylib.android_commands |
| 19 import pylib.device.device_utils | 18 import pylib.device.device_utils |
| 20 | 19 |
| 21 | 20 |
| 22 def _GetProcessStartTime(pid): | 21 def _GetProcessStartTime(pid): |
| 23 return psutil.Process(pid).create_time | 22 return psutil.Process(pid).create_time |
| 24 | 23 |
| 25 | 24 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 if isinstance(device, pylib.android_commands.AndroidCommands): | 83 if isinstance(device, pylib.android_commands.AndroidCommands): |
| 85 device = pylib.device.device_utils.DeviceUtils(device) | 84 device = pylib.device.device_utils.DeviceUtils(device) |
| 86 if not tool: | 85 if not tool: |
| 87 tool = valgrind_tools.CreateTool(None, device) | 86 tool = valgrind_tools.CreateTool(None, device) |
| 88 with _FileLock(Forwarder._LOCK_PATH): | 87 with _FileLock(Forwarder._LOCK_PATH): |
| 89 instance = Forwarder._GetInstanceLocked(tool) | 88 instance = Forwarder._GetInstanceLocked(tool) |
| 90 instance._InitDeviceLocked(device, tool) | 89 instance._InitDeviceLocked(device, tool) |
| 91 | 90 |
| 92 device_serial = str(device) | 91 device_serial = str(device) |
| 93 redirection_commands = [ | 92 redirection_commands = [ |
| 94 ['--serial-id=' + device_serial, '--map', str(device), | 93 ['--serial-id=' + device_serial, '--map', str(device_port), |
| 95 str(host)] for device, host in port_pairs] | 94 str(host_port)] for device_port, host_port in port_pairs] |
| 96 logging.info('Forwarding using commands: %s', redirection_commands) | 95 logging.info('Forwarding using commands: %s', redirection_commands) |
| 97 | 96 |
| 98 for redirection_command in redirection_commands: | 97 for redirection_command in redirection_commands: |
| 99 try: | 98 try: |
| 100 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 99 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
| 101 [instance._host_forwarder_path] + redirection_command) | 100 [instance._host_forwarder_path] + redirection_command) |
| 102 except OSError as e: | 101 except OSError as e: |
| 103 if e.errno == 2: | 102 if e.errno == 2: |
| 104 raise Exception('Unable to start host forwarder. Make sure you have' | 103 raise Exception('Unable to start host forwarder. Make sure you have' |
| 105 ' built host_forwarder.') | 104 ' built host_forwarder.') |
| 106 else: raise | 105 else: raise |
| 107 if exit_code != 0: | 106 if exit_code != 0: |
| 107 Forwarder._KillDeviceLocked(device, tool) |
| 108 raise Exception('%s exited with %d:\n%s' % ( | 108 raise Exception('%s exited with %d:\n%s' % ( |
| 109 instance._host_forwarder_path, exit_code, '\n'.join(output))) | 109 instance._host_forwarder_path, exit_code, '\n'.join(output))) |
| 110 tokens = output.split(':') | 110 tokens = output.split(':') |
| 111 if len(tokens) != 2: | 111 if len(tokens) != 2: |
| 112 raise Exception(('Unexpected host forwarder output "%s", ' + | 112 raise Exception('Unexpected host forwarder output "%s", ' |
| 113 'expected "device_port:host_port"') % output) | 113 'expected "device_port:host_port"' % output) |
| 114 device_port = int(tokens[0]) | 114 device_port = int(tokens[0]) |
| 115 host_port = int(tokens[1]) | 115 host_port = int(tokens[1]) |
| 116 serial_with_port = (device_serial, device_port) | 116 serial_with_port = (device_serial, device_port) |
| 117 instance._device_to_host_port_map[serial_with_port] = host_port | 117 instance._device_to_host_port_map[serial_with_port] = host_port |
| 118 instance._host_to_device_port_map[host_port] = serial_with_port | 118 instance._host_to_device_port_map[host_port] = serial_with_port |
| 119 logging.info('Forwarding device port: %d to host port: %d.', | 119 logging.info('Forwarding device port: %d to host port: %d.', |
| 120 device_port, host_port) | 120 device_port, host_port) |
| 121 | 121 |
| 122 @staticmethod | 122 @staticmethod |
| 123 def UnmapDevicePort(device_port, device): | 123 def UnmapDevicePort(device_port, device): |
| (...skipping 27 matching lines...) Expand all Loading... |
| 151 if adb_serial not in Forwarder._instance._initialized_devices: | 151 if adb_serial not in Forwarder._instance._initialized_devices: |
| 152 return | 152 return |
| 153 port_map = Forwarder._GetInstanceLocked( | 153 port_map = Forwarder._GetInstanceLocked( |
| 154 None)._device_to_host_port_map | 154 None)._device_to_host_port_map |
| 155 for (device_serial, device_port) in port_map.keys(): | 155 for (device_serial, device_port) in port_map.keys(): |
| 156 if adb_serial == device_serial: | 156 if adb_serial == device_serial: |
| 157 Forwarder._UnmapDevicePortLocked(device_port, device) | 157 Forwarder._UnmapDevicePortLocked(device_port, device) |
| 158 # There are no more ports mapped, kill the device_forwarder. | 158 # There are no more ports mapped, kill the device_forwarder. |
| 159 tool = valgrind_tools.CreateTool(None, device) | 159 tool = valgrind_tools.CreateTool(None, device) |
| 160 Forwarder._KillDeviceLocked(device, tool) | 160 Forwarder._KillDeviceLocked(device, tool) |
| 161 Forwarder._instance._initialized_devices.remove(adb_serial) | |
| 162 | |
| 163 | 161 |
| 164 @staticmethod | 162 @staticmethod |
| 165 def DevicePortForHostPort(host_port): | 163 def DevicePortForHostPort(host_port): |
| 166 """Returns the device port that corresponds to a given host port.""" | 164 """Returns the device port that corresponds to a given host port.""" |
| 167 with _FileLock(Forwarder._LOCK_PATH): | 165 with _FileLock(Forwarder._LOCK_PATH): |
| 168 (_device_serial, device_port) = Forwarder._GetInstanceLocked( | 166 (_device_serial, device_port) = Forwarder._GetInstanceLocked( |
| 169 None)._host_to_device_port_map.get(host_port) | 167 None)._host_to_device_port_map.get(host_port) |
| 170 return device_port | 168 return device_port |
| 171 | 169 |
| 172 @staticmethod | 170 @staticmethod |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 """Kills the forwarder process running on the device. | 319 """Kills the forwarder process running on the device. |
| 322 | 320 |
| 323 Note that the global lock must be acquired before calling this method. | 321 Note that the global lock must be acquired before calling this method. |
| 324 | 322 |
| 325 Args: | 323 Args: |
| 326 device: Instance of DeviceUtils for talking to the device. | 324 device: Instance of DeviceUtils for talking to the device. |
| 327 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device | 325 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device |
| 328 forwarder (see valgrind_tools.py). | 326 forwarder (see valgrind_tools.py). |
| 329 """ | 327 """ |
| 330 logging.info('Killing device_forwarder.') | 328 logging.info('Killing device_forwarder.') |
| 329 Forwarder._instance._initialized_devices.discard(str(device)) |
| 331 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): | 330 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): |
| 332 return | 331 return |
| 333 | 332 |
| 334 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), | 333 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), |
| 335 Forwarder._DEVICE_FORWARDER_PATH) | 334 Forwarder._DEVICE_FORWARDER_PATH) |
| 336 device.old_interface.GetAndroidToolStatusAndOutput( | 335 device.old_interface.GetAndroidToolStatusAndOutput( |
| 337 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER) | 336 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER) |
| 338 | |
| 339 # TODO(pliard): Remove the following call to KillAllBlocking() when we are | |
| 340 # sure that the old version of device_forwarder (not supporting | |
| 341 # 'kill-server') is not running on the bots anymore. | |
| 342 timeout_sec = 5 | |
| 343 try: | |
| 344 device.KillAll('device_forwarder', blocking=True, timeout=timeout_sec) | |
| 345 except device_errors.CommandFailedError: | |
| 346 if device.GetPids('device_forwarder'): | |
| 347 raise | |
| OLD | NEW |