Chromium Code Reviews| 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 | |
| 11 import logging | 13 import logging |
| 12 import multiprocessing | 14 import multiprocessing |
| 13 import os | 15 import os |
| 14 import re | 16 import re |
| 15 import sys | 17 import sys |
| 16 import tempfile | 18 import tempfile |
| 17 import time | 19 import time |
| 18 import zipfile | 20 import zipfile |
| 19 | 21 |
| 20 import pylib.android_commands | 22 import pylib.android_commands |
| (...skipping 1295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1316 @decorators.WithTimeoutAndRetriesFromInstance() | 1318 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1317 def GetMemoryUsageForPid(self, pid, timeout=None, retries=None): | 1319 def GetMemoryUsageForPid(self, pid, timeout=None, retries=None): |
| 1318 """Gets the memory usage for the given PID. | 1320 """Gets the memory usage for the given PID. |
| 1319 | 1321 |
| 1320 Args: | 1322 Args: |
| 1321 pid: PID of the process. | 1323 pid: PID of the process. |
| 1322 timeout: timeout in seconds | 1324 timeout: timeout in seconds |
| 1323 retries: number of retries | 1325 retries: number of retries |
| 1324 | 1326 |
| 1325 Returns: | 1327 Returns: |
| 1326 A 2-tuple containing: | 1328 A dict containing memory usage statistics for the PID. May include: |
| 1327 - A dict containing the overall memory usage statistics for the PID. | 1329 Size, Rss, Pss, Shared_Clean, Shared_Dirty, Private_Clean, |
| 1328 - A dict containing memory usage statistics broken down by mapping. | 1330 Private_Dirty, VmHWM |
| 1329 | 1331 |
| 1330 Raises: | 1332 Raises: |
| 1331 CommandTimeoutError on timeout. | 1333 CommandTimeoutError on timeout. |
| 1332 """ | 1334 """ |
| 1333 return self.old_interface.GetMemoryUsageForPid(pid) | 1335 result = collections.defaultdict(int) |
| 1336 | |
| 1337 try: | |
| 1338 result.update(self._GetMemoryUsageForPidFromSmaps(pid)) | |
| 1339 except device_errors.CommandFailedError: | |
| 1340 logging.exception('Error getting memory usage from smaps') | |
| 1341 | |
| 1342 try: | |
| 1343 result.update(self._GetMemoryUsageForPidFromStatus(pid)) | |
| 1344 except device_errors.CommandFailedError: | |
| 1345 logging.exception('Error getting memory usage from status') | |
| 1346 | |
| 1347 return result | |
| 1348 | |
| 1349 def _GetMemoryUsageForPidFromSmaps(self, pid): | |
| 1350 SMAPS_COLUMNS = ( | |
| 1351 'Size', 'Rss', 'Pss', 'Shared_Clean', 'Shared_Dirty', 'Private_Clean', | |
| 1352 'Private_Dirty') | |
| 1353 | |
| 1354 showmap_out = self.RunShellCommand( | |
| 1355 ['showmap', str(pid)], as_root=True, check_return=True) | |
| 1356 if not showmap_out: | |
| 1357 raise device_errors.CommandFailedError('No output from showmap') | |
| 1358 | |
| 1359 split_totals = showmap_out[-1].split() | |
| 1360 if (not split_totals | |
| 1361 or len(split_totals) != 9 | |
| 1362 or split_totals[-1] != 'TOTAL'): | |
| 1363 raise device_errors.CommandFailedError( | |
| 1364 'Invalid output from showmap: %s' % '\n'.join(showmap_out)) | |
| 1365 | |
| 1366 return dict(itertools.izip( | |
| 1367 SMAPS_COLUMNS, | |
| 1368 (int(split_totals[i]) for i in xrange(0, len(SMAPS_COLUMNS))))) | |
|
perezju
2015/02/16 11:17:57
I would just write:
(int(num) for num in split
jbudorick
2015/02/17 16:59:15
Done.
| |
| 1369 | |
| 1370 def _GetMemoryUsageForPidFromStatus(self, pid): | |
| 1371 for line in self.ReadFile( | |
| 1372 '/proc/%s/status' % str(pid), as_root=True).splitlines(): | |
| 1373 if line.startswith('VmHWM:'): | |
| 1374 return {'VmHWM': int(line.split()[1])} | |
| 1375 else: | |
| 1376 raise device_errors.CommandFailedError( | |
| 1377 'Could not find memory peak value for pid %s', str(pid)) | |
| 1334 | 1378 |
| 1335 @decorators.WithTimeoutAndRetriesFromInstance() | 1379 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1336 def GetLogcatMonitor(self, timeout=None, retries=None, *args, **kwargs): | 1380 def GetLogcatMonitor(self, timeout=None, retries=None, *args, **kwargs): |
| 1337 """Returns a new LogcatMonitor associated with this device. | 1381 """Returns a new LogcatMonitor associated with this device. |
| 1338 | 1382 |
| 1339 Parameters passed to this function are passed directly to | 1383 Parameters passed to this function are passed directly to |
| 1340 |logcat_monitor.LogcatMonitor| and are documented there. | 1384 |logcat_monitor.LogcatMonitor| and are documented there. |
| 1341 | 1385 |
| 1342 Args: | 1386 Args: |
| 1343 timeout: timeout in seconds | 1387 timeout: timeout in seconds |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 1366 Returns: | 1410 Returns: |
| 1367 A Parallelizer operating over |devices|. | 1411 A Parallelizer operating over |devices|. |
| 1368 """ | 1412 """ |
| 1369 if not devices: | 1413 if not devices: |
| 1370 devices = adb_wrapper.AdbWrapper.GetDevices() | 1414 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 1371 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1415 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
| 1372 if async: | 1416 if async: |
| 1373 return parallelizer.Parallelizer(devices) | 1417 return parallelizer.Parallelizer(devices) |
| 1374 else: | 1418 else: |
| 1375 return parallelizer.SyncParallelizer(devices) | 1419 return parallelizer.SyncParallelizer(devices) |
| OLD | NEW |