Index: tools/telemetry/telemetry/core/platform/win_platform_backend.py |
diff --git a/tools/telemetry/telemetry/core/platform/win_platform_backend.py b/tools/telemetry/telemetry/core/platform/win_platform_backend.py |
index 71038ecb60498c8224de6a8ff76d5c47cf8df851..144ed45b69ddb35f2ae5218ecd010f3340de774f 100644 |
--- a/tools/telemetry/telemetry/core/platform/win_platform_backend.py |
+++ b/tools/telemetry/telemetry/core/platform/win_platform_backend.py |
@@ -100,22 +100,46 @@ class WinPlatformBackend(desktop_platform_backend.DesktopPlatformBackend): |
'ReadTransferCount': io_stats['ReadTransferCount'], |
'WriteTransferCount': io_stats['WriteTransferCount']} |
+ def KillProcess(self, pid, kill_process_tree=False): |
+ # os.kill for Windows is Python 2.7. |
+ cmd = ['taskkill', '/F', '/PID', str(pid)] |
+ if kill_process_tree: |
+ cmd.append('/T') |
+ subprocess.Popen(cmd, stdout=subprocess.PIPE, |
+ stderr=subprocess.STDOUT).wait() |
+ |
+ def GetSystemProcessInfo(self): |
+ # [3:] To skip 2 blank lines and header. |
+ lines = subprocess.Popen( |
+ ['wmic', 'process', 'get', |
+ 'CommandLine,CreationDate,Name,ParentProcessId,ProcessId', |
+ '/format:csv'], |
+ stdout=subprocess.PIPE).communicate()[0].splitlines()[3:] |
+ process_info = [] |
+ for line in lines: |
+ if not line: |
+ continue |
+ parts = line.split(',') |
+ pi = {} |
+ pi['ProcessId'] = int(parts[-1]) |
+ pi['ParentProcessId'] = int(parts[-2]) |
+ pi['Name'] = parts[-3] |
+ creation_date = None |
+ if parts[-4]: |
+ creation_date = float(re.split('[+-]', parts[-4])[0]) |
+ pi['CreationDate'] = creation_date |
+ pi['CommandLine'] = ','.join(parts[1:-4]) |
+ process_info.append(pi) |
+ return process_info |
+ |
def GetChildPids(self, pid): |
"""Retunds a list of child pids of |pid|.""" |
- creation_ppid_pid_list = subprocess.Popen( |
- ['wmic', 'process', 'get', 'CreationDate,ParentProcessId,ProcessId', |
- '/format:csv'], |
- stdout=subprocess.PIPE).communicate()[0] |
ppid_map = collections.defaultdict(list) |
creation_map = {} |
- # [3:] To skip 2 blank lines and header. |
- for creation_ppid_pid in creation_ppid_pid_list.splitlines()[3:]: |
- if not creation_ppid_pid: |
- continue |
- _, creation, curr_ppid, curr_pid = creation_ppid_pid.split(',') |
- ppid_map[int(curr_ppid)].append(int(curr_pid)) |
- if creation: |
- creation_map[int(curr_pid)] = float(re.split('[+-]', creation)[0]) |
+ for pi in self.GetSystemProcessInfo(): |
+ ppid_map[pi['ParentProcessId']].append(pi['ProcessId']) |
+ if pi['CreationDate']: |
+ creation_map[pi['ProcessId']] = pi['CreationDate'] |
def _InnerGetChildPids(pid): |
if not pid or pid not in ppid_map: |
@@ -130,19 +154,9 @@ class WinPlatformBackend(desktop_platform_backend.DesktopPlatformBackend): |
return _InnerGetChildPids(pid) |
def GetCommandLine(self, pid): |
- command_pid_list = subprocess.Popen( |
- ['wmic', 'process', 'get', 'CommandLine,ProcessId', |
- '/format:csv'], |
- stdout=subprocess.PIPE).communicate()[0] |
- # [3:] To skip 2 blank lines and header. |
- for command_pid in command_pid_list.splitlines()[3:]: |
- if not command_pid: |
- continue |
- parts = command_pid.split(',') |
- curr_pid = parts[-1] |
- if pid == int(curr_pid): |
- command = ','.join(parts[1:-1]) |
- return command |
+ for pi in self.GetSystemProcessInfo(): |
+ if pid == pi['ProcessId']: |
+ return pi['CommandLine'] |
raise Exception('Could not get command line for %d' % pid) |
def GetOSName(self): |