Takuto Ikuta | e840a3d | 2022-08-03 01:07:56 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 2 | # Copyright 2021 The Chromium Authors |
Bruce Dawson | df6fc16 | 2021-02-05 01:55:50 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """ |
| 6 | This script checks to see what build commands are currently running by printing |
| 7 | the command lines of any processes that are the children of ninja processes. |
| 8 | |
| 9 | The idea is that if the build is serialized (not many build steps running) then |
| 10 | you can run this to see what it is serialized on. |
| 11 | |
Takuto Ikuta | e840a3d | 2022-08-03 01:07:56 | [diff] [blame] | 12 | This uses python3 on Windows and vpython elsewhere (for psutil). |
Bruce Dawson | df6fc16 | 2021-02-05 01:55:50 | [diff] [blame] | 13 | """ |
| 14 | |
Bruce Dawson | df6fc16 | 2021-02-05 01:55:50 | [diff] [blame] | 15 | import sys |
| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | parents = [] |
| 20 | processes = [] |
| 21 | |
| 22 | print('Gathering process data...') |
| 23 | # Ninja's name on Linux is ninja-linux64, presumably different elsewhere, so |
| 24 | # we look for a matching prefix. |
| 25 | ninja_prefix = 'ninja.exe' if sys.platform in ['win32', 'cygwin'] else 'ninja' |
| 26 | |
| 27 | if sys.platform in ['win32', 'cygwin']: |
| 28 | # psutil handles short-lived ninja descendants poorly on Windows (it misses |
| 29 | # most of them) so use wmic instead. |
| 30 | import subprocess |
| 31 | cmd = 'wmic process get Caption,ParentProcessId,ProcessId,CommandLine' |
| 32 | lines = subprocess.check_output(cmd, universal_newlines=True).splitlines() |
| 33 | |
| 34 | # Find the offsets for the various data columns by looking at the labels in |
| 35 | # the first line of output. |
| 36 | CAPTION_OFF = 0 |
| 37 | COMMAND_LINE_OFF = lines[0].find('CommandLine') |
| 38 | PARENT_PID_OFF = lines[0].find('ParentProcessId') |
| 39 | PID_OFF = lines[0].find(' ProcessId') + 1 |
| 40 | |
| 41 | for line in lines[1:]: |
| 42 | # Ignore blank lines |
| 43 | if not line.strip(): |
| 44 | continue |
| 45 | command = line[:COMMAND_LINE_OFF].strip() |
| 46 | command_line = line[COMMAND_LINE_OFF:PARENT_PID_OFF].strip() |
| 47 | parent_pid = int(line[PARENT_PID_OFF:PID_OFF].strip()) |
| 48 | pid = int(line[PID_OFF:].strip()) |
| 49 | processes.append((command, command_line, parent_pid, pid)) |
| 50 | |
| 51 | else: |
| 52 | # Portable process-collection code, but works badly on Windows. |
| 53 | import psutil |
| 54 | for proc in psutil.process_iter(['pid', 'ppid', 'name', 'cmdline']): |
| 55 | try: |
| 56 | cmdline = proc.cmdline() |
| 57 | # Convert from list to a single string. |
| 58 | cmdline = ' '.join(cmdline) |
| 59 | except psutil.AccessDenied: |
| 60 | cmdline = "Access denied" |
| 61 | processes.append( |
| 62 | (proc.name()[:], cmdline, int(proc.ppid()), int(proc.pid))) |
| 63 | |
| 64 | # Scan the list of processes to find ninja. |
| 65 | for process in processes: |
| 66 | command, command_line, parent_pid, pid = process |
| 67 | if command.startswith(ninja_prefix): |
| 68 | parents.append(pid) |
| 69 | |
| 70 | if not parents: |
| 71 | print('No interesting parent processes found.') |
| 72 | return 1 |
| 73 | |
| 74 | print('Tracking the children of these PIDs:') |
| 75 | print(', '.join(map(lambda x: str(x), parents))) |
| 76 | |
| 77 | print() |
| 78 | |
| 79 | # Print all the processes that have parent-processes of interest. |
| 80 | count = 0 |
| 81 | for process in processes: |
| 82 | command, command_line, parent_pid, pid = process |
| 83 | if parent_pid in parents: |
| 84 | if not command_line: |
| 85 | command_line = command |
| 86 | print('%5d: %s' % (pid, command_line[:160])) |
| 87 | count += 1 |
| 88 | print('Found %d children' % count) |
| 89 | return 0 |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | main() |