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 """Provides an interface to communicate with the device via the adb command. | 5 """Provides an interface to communicate with the device via the adb command. |
6 | 6 |
7 Assumes adb binary is currently on system path. | 7 Assumes adb binary is currently on system path. |
8 """ | 8 """ |
9 | 9 |
10 import collections | 10 import collections |
(...skipping 1041 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 A tuple containg: | 1052 A tuple containg: |
1053 [0]: Dict of {metric:usage_kb}, for the process which has specified pid. | 1053 [0]: Dict of {metric:usage_kb}, for the process which has specified pid. |
1054 The metric keys which may be included are: Size, Rss, Pss, Shared_Clean, | 1054 The metric keys which may be included are: Size, Rss, Pss, Shared_Clean, |
1055 Shared_Dirty, Private_Clean, Private_Dirty, Referenced, Swap, | 1055 Shared_Dirty, Private_Clean, Private_Dirty, Referenced, Swap, |
1056 KernelPageSize, MMUPageSize, Nvidia (tablet only). | 1056 KernelPageSize, MMUPageSize, Nvidia (tablet only). |
1057 [1]: Detailed /proc/[PID]/smaps information. | 1057 [1]: Detailed /proc/[PID]/smaps information. |
1058 """ | 1058 """ |
1059 usage_dict = collections.defaultdict(int) | 1059 usage_dict = collections.defaultdict(int) |
1060 smaps = collections.defaultdict(dict) | 1060 smaps = collections.defaultdict(dict) |
1061 current_smap = '' | 1061 current_smap = '' |
1062 for line in self.GetFileContents('/proc/%s/smaps' % pid, log_result=False): | 1062 for line in self.GetProtectedFileContents('/proc/%s/smaps' % pid, |
| 1063 log_result=False): |
1063 items = line.split() | 1064 items = line.split() |
1064 # See man 5 proc for more details. The format is: | 1065 # See man 5 proc for more details. The format is: |
1065 # address perms offset dev inode pathname | 1066 # address perms offset dev inode pathname |
1066 if len(items) > 5: | 1067 if len(items) > 5: |
1067 current_smap = ' '.join(items[5:]) | 1068 current_smap = ' '.join(items[5:]) |
1068 elif len(items) > 3: | 1069 elif len(items) > 3: |
1069 current_smap = ' '.join(items[3:]) | 1070 current_smap = ' '.join(items[3:]) |
1070 match = re.match(MEMORY_INFO_RE, line) | 1071 match = re.match(MEMORY_INFO_RE, line) |
1071 if match: | 1072 if match: |
1072 key = match.group('key') | 1073 key = match.group('key') |
1073 usage_kb = int(match.group('usage_kb')) | 1074 usage_kb = int(match.group('usage_kb')) |
1074 usage_dict[key] += usage_kb | 1075 usage_dict[key] += usage_kb |
1075 if key not in smaps[current_smap]: | 1076 if key not in smaps[current_smap]: |
1076 smaps[current_smap][key] = 0 | 1077 smaps[current_smap][key] = 0 |
1077 smaps[current_smap][key] += usage_kb | 1078 smaps[current_smap][key] += usage_kb |
1078 if not usage_dict or not any(usage_dict.values()): | 1079 if not usage_dict or not any(usage_dict.values()): |
1079 # Presumably the process died between ps and calling this method. | 1080 # Presumably the process died between ps and calling this method. |
1080 logging.warning('Could not find memory usage for pid ' + str(pid)) | 1081 logging.warning('Could not find memory usage for pid ' + str(pid)) |
1081 | 1082 |
1082 for line in self.GetFileContents('/d/nvmap/generic-0/clients', | 1083 for line in self.GetProtectedFileContents('/d/nvmap/generic-0/clients', |
1083 log_result=False): | 1084 log_result=False): |
1084 match = re.match(NVIDIA_MEMORY_INFO_RE, line) | 1085 match = re.match(NVIDIA_MEMORY_INFO_RE, line) |
1085 if match and match.group('pid') == pid: | 1086 if match and match.group('pid') == pid: |
1086 usage_bytes = int(match.group('usage_bytes')) | 1087 usage_bytes = int(match.group('usage_bytes')) |
1087 usage_dict['Nvidia'] = int(round(usage_bytes / 1000.0)) # kB | 1088 usage_dict['Nvidia'] = int(round(usage_bytes / 1000.0)) # kB |
1088 break | 1089 break |
1089 | 1090 |
1090 return (usage_dict, smaps) | 1091 return (usage_dict, smaps) |
1091 | 1092 |
1092 def GetMemoryUsageForPackage(self, package): | 1093 def GetMemoryUsageForPackage(self, package): |
1093 """Returns the memory usage for all processes whose name contains |pacakge|. | 1094 """Returns the memory usage for all processes whose name contains |pacakge|. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1207 """ | 1208 """ |
1208 def __init__(self, output): | 1209 def __init__(self, output): |
1209 self._output = output | 1210 self._output = output |
1210 | 1211 |
1211 def write(self, data): | 1212 def write(self, data): |
1212 data = data.replace('\r\r\n', '\n') | 1213 data = data.replace('\r\r\n', '\n') |
1213 self._output.write(data) | 1214 self._output.write(data) |
1214 | 1215 |
1215 def flush(self): | 1216 def flush(self): |
1216 self._output.flush() | 1217 self._output.flush() |
OLD | NEW |