| 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 | |
| 10 import sys | |
| 11 import time | |
| 12 | 9 |
| 13 import android_commands | 10 from . import cmd_helper |
| 14 import cmd_helper | 11 from . import constants |
| 15 import constants | 12 from . import valgrind_tools |
| 16 | |
| 17 from pylib import valgrind_tools | |
| 18 | 13 |
| 19 | 14 |
| 20 def _GetProcessStartTime(pid): | 15 def _GetProcessStartTime(pid): |
| 21 return psutil.Process(pid).create_time | 16 return psutil.Process(pid).create_time |
| 22 | 17 |
| 23 | 18 |
| 24 class _FileLock(object): | 19 class _FileLock(object): |
| 25 """With statement-aware implementation of a file lock. | 20 """With statement-aware implementation of a file lock. |
| 26 | 21 |
| 27 File locks are needed for cross-process synchronization when the | 22 File locks are needed for cross-process synchronization when the |
| 28 multiprocessing Python module is used. | 23 multiprocessing Python module is used. |
| 29 """ | 24 """ |
| 30 def __init__(self, path): | 25 def __init__(self, path): |
| 26 self._fd = -1 |
| 31 self._path = path | 27 self._path = path |
| 32 | 28 |
| 33 def __enter__(self): | 29 def __enter__(self): |
| 34 self._fd = os.open(self._path, os.O_RDONLY | os.O_CREAT) | 30 self._fd = os.open(self._path, os.O_RDONLY | os.O_CREAT) |
| 35 if self._fd < 0: | 31 if self._fd < 0: |
| 36 raise Exception('Could not open file %s for reading' % self._path) | 32 raise Exception('Could not open file %s for reading' % self._path) |
| 37 fcntl.flock(self._fd, fcntl.LOCK_EX) | 33 fcntl.flock(self._fd, fcntl.LOCK_EX) |
| 38 | 34 |
| 39 def __exit__(self, type, value, traceback): | 35 def __exit__(self, _exception_type, _exception_value, traceback): |
| 40 fcntl.flock(self._fd, fcntl.LOCK_UN) | 36 fcntl.flock(self._fd, fcntl.LOCK_UN) |
| 41 os.close(self._fd) | 37 os.close(self._fd) |
| 42 | 38 |
| 43 | 39 |
| 44 class Forwarder(object): | 40 class Forwarder(object): |
| 45 """Thread-safe class to manage port forwards from the device to the host.""" | 41 """Thread-safe class to manage port forwards from the device to the host.""" |
| 46 | 42 |
| 47 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + | 43 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + |
| 48 '/forwarder/') | 44 '/forwarder/') |
| 49 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + | 45 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 # There are no more ports mapped, kill the device_forwarder. | 143 # There are no more ports mapped, kill the device_forwarder. |
| 148 tool = valgrind_tools.CreateTool(None, adb) | 144 tool = valgrind_tools.CreateTool(None, adb) |
| 149 Forwarder._KillDeviceLocked(adb, tool) | 145 Forwarder._KillDeviceLocked(adb, tool) |
| 150 Forwarder._instance._initialized_devices.remove(adb_serial) | 146 Forwarder._instance._initialized_devices.remove(adb_serial) |
| 151 | 147 |
| 152 | 148 |
| 153 @staticmethod | 149 @staticmethod |
| 154 def DevicePortForHostPort(host_port): | 150 def DevicePortForHostPort(host_port): |
| 155 """Returns the device port that corresponds to a given host port.""" | 151 """Returns the device port that corresponds to a given host port.""" |
| 156 with _FileLock(Forwarder._LOCK_PATH): | 152 with _FileLock(Forwarder._LOCK_PATH): |
| 157 (device_serial, device_port) = Forwarder._GetInstanceLocked( | 153 (_device_serial, device_port) = Forwarder._GetInstanceLocked( |
| 158 None)._host_to_device_port_map.get(host_port) | 154 None)._host_to_device_port_map.get(host_port) |
| 159 return device_port | 155 return device_port |
| 160 | 156 |
| 161 @staticmethod | 157 @staticmethod |
| 162 def RemoveHostLog(): | 158 def RemoveHostLog(): |
| 163 if os.path.exists(Forwarder._HOST_FORWARDER_LOG): | 159 if os.path.exists(Forwarder._HOST_FORWARDER_LOG): |
| 164 os.unlink(Forwarder._HOST_FORWARDER_LOG) | 160 os.unlink(Forwarder._HOST_FORWARDER_LOG) |
| 165 | 161 |
| 166 @staticmethod | 162 @staticmethod |
| 167 def GetHostLog(): | 163 def GetHostLog(): |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 Note that the global lock must be acquired before calling this method. | 308 Note that the global lock must be acquired before calling this method. |
| 313 | 309 |
| 314 Args: | 310 Args: |
| 315 adb: Instance of AndroidCommands for talking to the device. | 311 adb: Instance of AndroidCommands for talking to the device. |
| 316 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device | 312 tool: Wrapper tool (e.g. valgrind) that can be used to execute the device |
| 317 forwarder (see valgrind_tools.py). | 313 forwarder (see valgrind_tools.py). |
| 318 """ | 314 """ |
| 319 logging.info('Killing device_forwarder.') | 315 logging.info('Killing device_forwarder.') |
| 320 if not adb.FileExistsOnDevice(Forwarder._DEVICE_FORWARDER_PATH): | 316 if not adb.FileExistsOnDevice(Forwarder._DEVICE_FORWARDER_PATH): |
| 321 return | 317 return |
| 322 (exit_code, output) = adb.GetShellCommandStatusAndOutput( | 318 adb.GetShellCommandStatusAndOutput( |
| 323 '%s %s --kill-server' % (tool.GetUtilWrapper(), | 319 '%s %s --kill-server' % (tool.GetUtilWrapper(), |
| 324 Forwarder._DEVICE_FORWARDER_PATH)) | 320 Forwarder._DEVICE_FORWARDER_PATH)) |
| 325 # TODO(pliard): Remove the following call to KillAllBlocking() when we are | 321 # TODO(pliard): Remove the following call to KillAllBlocking() when we are |
| 326 # sure that the old version of device_forwarder (not supporting | 322 # sure that the old version of device_forwarder (not supporting |
| 327 # 'kill-server') is not running on the bots anymore. | 323 # 'kill-server') is not running on the bots anymore. |
| 328 timeout_sec = 5 | 324 timeout_sec = 5 |
| 329 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) | 325 processes_killed = adb.KillAllBlocking('device_forwarder', timeout_sec) |
| 330 if not processes_killed: | 326 if not processes_killed: |
| 331 pids = adb.ExtractPid('device_forwarder') | 327 pids = adb.ExtractPid('device_forwarder') |
| 332 if pids: | 328 if pids: |
| 333 raise Exception('Timed out while killing device_forwarder') | 329 raise Exception('Timed out while killing device_forwarder') |
| OLD | NEW |