Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: build/android/pylib/forwarder.py

Issue 18907002: Reland r210268: "Improve forwarder2 setup/tear down in telemetry." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 logging 5 import logging
6 import os 6 import os
7 import re 7 import re
8 import sys 8 import sys
9 import threading 9 import threading
10 import time 10 import time
(...skipping 22 matching lines...) Expand all
33 """Forwards TCP ports on the device back to the host. 33 """Forwards TCP ports on the device back to the host.
34 34
35 Works like adb forward, but in reverse. 35 Works like adb forward, but in reverse.
36 36
37 Args: 37 Args:
38 adb: Instance of AndroidCommands for talking to the device. 38 adb: Instance of AndroidCommands for talking to the device.
39 build_type: 'Release' or 'Debug'. 39 build_type: 'Release' or 'Debug'.
40 """ 40 """
41 assert build_type in ('Release', 'Debug') 41 assert build_type in ('Release', 'Debug')
42 self._adb = adb 42 self._adb = adb
43 self._device_to_host_port_map = dict()
43 self._host_to_device_port_map = dict() 44 self._host_to_device_port_map = dict()
44 self._device_initialized = False 45 self._device_initialized = False
45 self._host_adb_control_port = 0 46 self._host_adb_control_port = 0
46 self._lock = threading.Lock() 47 self._lock = threading.Lock()
47 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') 48 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder')
48 self._device_forwarder_path_on_host = os.path.join( 49 self._device_forwarder_path_on_host = os.path.join(
49 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') 50 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist')
50 51
51 def Run(self, port_pairs, tool): 52 def Run(self, port_pairs, tool):
52 """Runs the forwarder. 53 """Runs the forwarder.
(...skipping 29 matching lines...) Expand all
82 else: raise 83 else: raise
83 if exit_code != 0: 84 if exit_code != 0:
84 raise Exception('%s exited with %d:\n%s' % ( 85 raise Exception('%s exited with %d:\n%s' % (
85 self._host_forwarder_path, exit_code, '\n'.join(output))) 86 self._host_forwarder_path, exit_code, '\n'.join(output)))
86 tokens = output.split(':') 87 tokens = output.split(':')
87 if len(tokens) != 2: 88 if len(tokens) != 2:
88 raise Exception(('Unexpected host forwarder output "%s", ' + 89 raise Exception(('Unexpected host forwarder output "%s", ' +
89 'expected "device_port:host_port"') % output) 90 'expected "device_port:host_port"') % output)
90 device_port = int(tokens[0]) 91 device_port = int(tokens[0])
91 host_port = int(tokens[1]) 92 host_port = int(tokens[1])
93 self._device_to_host_port_map[device_port] = host_port
92 self._host_to_device_port_map[host_port] = device_port 94 self._host_to_device_port_map[host_port] = device_port
93 logging.info('Forwarding device port: %d to host port: %d.', 95 logging.info('Forwarding device port: %d to host port: %d.',
94 device_port, host_port) 96 device_port, host_port)
95 97
96 def _InitDeviceLocked(self, tool): 98 def _InitDeviceLocked(self, tool):
97 """Initializes the device forwarder process (only once).""" 99 """Initializes the device forwarder process (only once)."""
98 if self._device_initialized: 100 if self._device_initialized:
99 return 101 return
100 self._adb.PushIfNeeded( 102 self._adb.PushIfNeeded(
101 self._device_forwarder_path_on_host, 103 self._device_forwarder_path_on_host,
102 Forwarder._DEVICE_FORWARDER_FOLDER) 104 Forwarder._DEVICE_FORWARDER_FOLDER)
103 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( 105 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput(
104 '%s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), 106 '%s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(),
105 Forwarder._DEVICE_FORWARDER_PATH)) 107 Forwarder._DEVICE_FORWARDER_PATH))
106 if exit_code != 0: 108 if exit_code != 0:
107 raise Exception( 109 raise Exception(
108 'Failed to start device forwarder:\n%s' % '\n'.join(output)) 110 'Failed to start device forwarder:\n%s' % '\n'.join(output))
109 self._device_initialized = True 111 self._device_initialized = True
110 112
111 def UnmapDevicePort(self, device_port): 113 def UnmapDevicePort(self, device_port):
112 """Unmaps a previously forwarded device port. 114 """Unmaps a previously forwarded device port.
113 115
114 Args: 116 Args:
115 device_port: A previously forwarded port (through Run()). 117 device_port: A previously forwarded port (through Run()).
116 """ 118 """
117 with self._lock: 119 with self._lock:
118 redirection_command = [ 120 self._UnmapDevicePortInternalLocked(device_port)
119 '--serial-id=' + self._adb.Adb().GetSerialNumber(), '--unmap', 121
120 str(device_port)] 122 def _UnmapDevicePortInternalLocked(self, device_port):
121 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( 123 if not device_port in self._device_to_host_port_map:
122 [self._host_forwarder_path] + redirection_command) 124 return
123 if exit_code != 0: 125 redirection_command = [
124 logging.error('%s exited with %d:\n%s' % ( 126 '--serial-id=' + self._adb.Adb().GetSerialNumber(), '--unmap',
125 self._host_forwarder_path, exit_code, '\n'.join(output))) 127 str(device_port)]
128 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
129 [self._host_forwarder_path] + redirection_command)
130 if exit_code != 0:
131 logging.error('%s exited with %d:\n%s' % (
132 self._host_forwarder_path, exit_code, '\n'.join(output)))
133 host_port = self._device_to_host_port_map[device_port]
134 del self._device_to_host_port_map[device_port]
135 del self._host_to_device_port_map[host_port]
126 136
127 @staticmethod 137 @staticmethod
128 def KillHost(build_type): 138 def KillHost(build_type='Debug'):
129 """Kills the forwarder process running on the host. 139 """Kills the forwarder process running on the host.
130 140
131 Args: 141 Args:
132 build_type: 'Release' or 'Debug' 142 build_type: 'Release' or 'Debug' (default='Debug')
133 """ 143 """
134 logging.info('Killing host_forwarder.') 144 logging.info('Killing host_forwarder.')
135 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') 145 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder')
146 if not os.path.exists(host_forwarder_path):
147 host_forwarder_path = _MakeBinaryPath(
148 'Release' if build_type == 'Debug' else 'Debug', 'host_forwarder')
136 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' 149 assert os.path.exists(host_forwarder_path), 'Please build forwarder2'
137 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( 150 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
138 [host_forwarder_path, '--kill-server']) 151 [host_forwarder_path, '--kill-server'])
139 if exit_code != 0: 152 if exit_code != 0:
140 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( 153 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
141 ['pkill', 'host_forwarder']) 154 ['pkill', 'host_forwarder'])
142 if exit_code != 0: 155 if exit_code != 0:
143 raise Exception('%s exited with %d:\n%s' % ( 156 raise Exception('%s exited with %d:\n%s' % (
144 host_forwarder_path, exit_code, '\n'.join(output))) 157 host_forwarder_path, exit_code, '\n'.join(output)))
145 158
(...skipping 20 matching lines...) Expand all
166 if not processes_killed: 179 if not processes_killed:
167 pids = adb.ExtractPid('device_forwarder') 180 pids = adb.ExtractPid('device_forwarder')
168 if pids: 181 if pids:
169 raise Exception('Timed out while killing device_forwarder') 182 raise Exception('Timed out while killing device_forwarder')
170 183
171 def DevicePortForHostPort(self, host_port): 184 def DevicePortForHostPort(self, host_port):
172 """Returns the device port that corresponds to a given host port.""" 185 """Returns the device port that corresponds to a given host port."""
173 with self._lock: 186 with self._lock:
174 return self._host_to_device_port_map.get(host_port) 187 return self._host_to_device_port_map.get(host_port)
175 188
176 # Deprecated.
177 def Close(self): 189 def Close(self):
178 """Terminates the forwarder process.""" 190 """Releases the previously forwarded ports."""
179 # TODO(pliard): Remove references in client code. 191 with self._lock:
180 pass 192 for device_port in self._device_to_host_port_map.copy():
193 self._UnmapDevicePortInternalLocked(device_port)
OLDNEW
« no previous file with comments | « build/android/bb_run_sharded_steps.py ('k') | tools/telemetry/telemetry/core/chrome/adb_commands.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698