diff --git a/android/pylib/android_commands.py b/android/pylib/android_commands.py index a09461d29..4c2790759 100644 --- a/android/pylib/android_commands.py +++ b/android/pylib/android_commands.py @@ -22,6 +22,9 @@ import time import cmd_helper import constants import screenshot + +from utils import host_path_finder + try: from pylib import pexpect except: @@ -1418,6 +1421,20 @@ class AndroidCommands(object): logging.warning('Could not find disk IO stats.') return None + def PurgeUnpinnedAshmem(self): + """Purges the unpinned ashmem memory for the whole system. + + This can be used to make memory measurements more stable in particular. + """ + host_path = host_path_finder.GetMostRecentHostPath('purge_ashmem') + if not host_path: + raise Exception('Could not find the purge_ashmem binary.') + device_path = os.path.join(constants.TEST_EXECUTABLE_DIR, 'purge_ashmem') + self.PushIfNeeded(host_path, device_path) + if self.RunShellCommand(device_path, log_result=True): + return + raise Exception('Error while purging ashmem.') + def GetMemoryUsageForPid(self, pid): """Returns the memory usage for given pid. diff --git a/android/pylib/utils/host_path_finder.py b/android/pylib/utils/host_path_finder.py new file mode 100644 index 000000000..aea51a9cd --- /dev/null +++ b/android/pylib/utils/host_path_finder.py @@ -0,0 +1,23 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import os + +from pylib import constants + + +def GetMostRecentHostPath(file_name): + """Returns the most recent existing full path for the given file name. + + Returns: An empty string if no path could be found. + """ + out_dir = os.path.join( + constants.DIR_SOURCE_ROOT, os.environ.get('CHROMIUM_OUT_DIR', 'out')) + candidate_paths = map( + lambda build_type: os.path.join(out_dir, build_type, file_name), + ['Debug', 'Release']) + candidate_paths = filter(os.path.exists, candidate_paths) + candidate_paths = sorted(candidate_paths, key=os.path.getmtime, reverse=True) + candidate_paths.append('') + return candidate_paths[0]