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 838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 pid_list = self.ExtractPid(package) | 849 pid_list = self.ExtractPid(package) |
850 smaps = collections.defaultdict(dict) | 850 smaps = collections.defaultdict(dict) |
851 | 851 |
852 for pid in pid_list: | 852 for pid in pid_list: |
853 usage_dict_per_pid, smaps_per_pid = self.GetMemoryUsageForPid(pid) | 853 usage_dict_per_pid, smaps_per_pid = self.GetMemoryUsageForPid(pid) |
854 smaps[pid] = smaps_per_pid | 854 smaps[pid] = smaps_per_pid |
855 for (key, value) in usage_dict_per_pid.items(): | 855 for (key, value) in usage_dict_per_pid.items(): |
856 usage_dict[key] += value | 856 usage_dict[key] += value |
857 | 857 |
858 return usage_dict, smaps | 858 return usage_dict, smaps |
| 859 |
| 860 def ProcessesUsingDevicePort(self, device_port): |
| 861 """Lists the processes using the specified device port on loopback |
| 862 interface. |
| 863 |
| 864 Args: |
| 865 device_port: Port on device we want to check. |
| 866 |
| 867 Returns: |
| 868 A list of (pid, process_name) tuples using the specified port. |
| 869 """ |
| 870 tcp_results = self.RunShellCommand('cat /proc/net/tcp', log_result=False) |
| 871 tcp_address = "0100007F:%04X" % device_port |
| 872 pids = [] |
| 873 for single_connect in tcp_results: |
| 874 connect_results = single_connect.split() |
| 875 # Column 1 is the TCP port, and Column 9 is the inode of the socket |
| 876 if connect_results[1] == tcp_address: |
| 877 socket_inode = connect_results[9] |
| 878 socket_name = 'socket:[%s]' % socket_inode |
| 879 lsof_results = self.RunShellCommand('lsof', log_result=False) |
| 880 for single_process in lsof_results: |
| 881 process_results = single_process.split() |
| 882 # Ignore the line if it has less than nine columns in it, which may |
| 883 # be the case when a process stops while lsof is executing. |
| 884 if len(process_results) <= 8: |
| 885 continue |
| 886 # Column 0 is the executable name |
| 887 # Column 1 is the pid |
| 888 # Column 8 is the Inode in use |
| 889 if process_results[8] == socket_name: |
| 890 pids.append( (int(process_results[1]), process_results[0]) ) |
| 891 break |
| 892 logging.info('PidsUsingDevicePort: %s', pids) |
| 893 return pids |
OLD | NEW |