OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
6 | 6 |
7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
8 """ | 8 """ |
9 # pylint: disable=unused-argument | 9 # pylint: disable=unused-argument |
10 | 10 |
11 import collections | |
12 import itertools | |
13 import logging | 11 import logging |
14 import multiprocessing | 12 import multiprocessing |
15 import os | 13 import os |
16 import re | 14 import re |
17 import sys | 15 import sys |
18 import tempfile | 16 import tempfile |
19 import time | 17 import time |
20 import zipfile | 18 import zipfile |
21 | 19 |
22 import pylib.android_commands | 20 import pylib.android_commands |
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1301 @decorators.WithTimeoutAndRetriesFromInstance() | 1299 @decorators.WithTimeoutAndRetriesFromInstance() |
1302 def GetMemoryUsageForPid(self, pid, timeout=None, retries=None): | 1300 def GetMemoryUsageForPid(self, pid, timeout=None, retries=None): |
1303 """Gets the memory usage for the given PID. | 1301 """Gets the memory usage for the given PID. |
1304 | 1302 |
1305 Args: | 1303 Args: |
1306 pid: PID of the process. | 1304 pid: PID of the process. |
1307 timeout: timeout in seconds | 1305 timeout: timeout in seconds |
1308 retries: number of retries | 1306 retries: number of retries |
1309 | 1307 |
1310 Returns: | 1308 Returns: |
1311 A dict containing memory usage statistics for the PID. May include: | 1309 A 2-tuple containing: |
1312 Size, Rss, Pss, Shared_Clean, Shared_Dirty, Private_Clean, | 1310 - A dict containing the overall memory usage statistics for the PID. |
1313 Private_Dirty, VmHWM | 1311 - A dict containing memory usage statistics broken down by mapping. |
1314 | 1312 |
1315 Raises: | 1313 Raises: |
1316 CommandTimeoutError on timeout. | 1314 CommandTimeoutError on timeout. |
1317 """ | 1315 """ |
1318 result = collections.defaultdict(int) | 1316 return self.old_interface.GetMemoryUsageForPid(pid) |
1319 | |
1320 try: | |
1321 result.update(self._GetMemoryUsageForPidFromSmaps(pid)) | |
1322 except device_errors.CommandFailedError: | |
1323 logging.exception('Error getting memory usage from smaps') | |
1324 | |
1325 try: | |
1326 result.update(self._GetMemoryUsageForPidFromStatus(pid)) | |
1327 except device_errors.CommandFailedError: | |
1328 logging.exception('Error getting memory usage from status') | |
1329 | |
1330 return result | |
1331 | |
1332 def _GetMemoryUsageForPidFromSmaps(self, pid): | |
1333 SMAPS_COLUMNS = ( | |
1334 'Size', 'Rss', 'Pss', 'Shared_Clean', 'Shared_Dirty', 'Private_Clean', | |
1335 'Private_Dirty') | |
1336 | |
1337 showmap_out = self.RunShellCommand( | |
1338 ['showmap', str(pid)], as_root=True, check_return=True) | |
1339 if not showmap_out: | |
1340 raise device_errors.CommandFailedError('No output from showmap') | |
1341 | |
1342 split_totals = showmap_out[-1].split() | |
1343 if (not split_totals | |
1344 or len(split_totals) != 9 | |
1345 or split_totals[-1] != 'TOTAL'): | |
1346 raise device_errors.CommandFailedError( | |
1347 'Invalid output from showmap: %s' % '\n'.join(showmap_out)) | |
1348 | |
1349 return dict(itertools.izip(SMAPS_COLUMNS, (int(n) for n in split_totals))) | |
1350 | |
1351 def _GetMemoryUsageForPidFromStatus(self, pid): | |
1352 for line in self.ReadFile( | |
1353 '/proc/%s/status' % str(pid), as_root=True).splitlines(): | |
1354 if line.startswith('VmHWM:'): | |
1355 return {'VmHWM': int(line.split()[1])} | |
1356 else: | |
1357 raise device_errors.CommandFailedError( | |
1358 'Could not find memory peak value for pid %s', str(pid)) | |
1359 | 1317 |
1360 @decorators.WithTimeoutAndRetriesFromInstance() | 1318 @decorators.WithTimeoutAndRetriesFromInstance() |
1361 def GetLogcatMonitor(self, timeout=None, retries=None, *args, **kwargs): | 1319 def GetLogcatMonitor(self, timeout=None, retries=None, *args, **kwargs): |
1362 """Returns a new LogcatMonitor associated with this device. | 1320 """Returns a new LogcatMonitor associated with this device. |
1363 | 1321 |
1364 Parameters passed to this function are passed directly to | 1322 Parameters passed to this function are passed directly to |
1365 |logcat_monitor.LogcatMonitor| and are documented there. | 1323 |logcat_monitor.LogcatMonitor| and are documented there. |
1366 | 1324 |
1367 Args: | 1325 Args: |
1368 timeout: timeout in seconds | 1326 timeout: timeout in seconds |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1421 from which DeviceUtils instances can be constructed. If None, | 1379 from which DeviceUtils instances can be constructed. If None, |
1422 all attached devices will be used. | 1380 all attached devices will be used. |
1423 async: If true, returns a Parallelizer that runs operations | 1381 async: If true, returns a Parallelizer that runs operations |
1424 asynchronously. | 1382 asynchronously. |
1425 | 1383 |
1426 Returns: | 1384 Returns: |
1427 A Parallelizer operating over |devices|. | 1385 A Parallelizer operating over |devices|. |
1428 """ | 1386 """ |
1429 if not devices: | 1387 if not devices: |
1430 devices = adb_wrapper.AdbWrapper.GetDevices() | 1388 devices = adb_wrapper.AdbWrapper.GetDevices() |
1431 if not devices: | |
1432 raise device_errors.NoDevicesError() | |
1433 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1389 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1434 if async: | 1390 if async: |
1435 return parallelizer.Parallelizer(devices) | 1391 return parallelizer.Parallelizer(devices) |
1436 else: | 1392 else: |
1437 return parallelizer.SyncParallelizer(devices) | 1393 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |