buildstate.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env vpython3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  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. The idea is that if the build is serialized (not many build steps running) then
  9. you can run this to see what it is serialized on.
  10. This uses python3 on Windows and vpython elsewhere (for psutil).
  11. """
  12. import sys
  13. def main():
  14. parents = []
  15. processes = []
  16. print('Gathering process data...')
  17. # Ninja's name on Linux is ninja-linux64, presumably different elsewhere, so
  18. # we look for a matching prefix.
  19. ninja_prefix = 'ninja.exe' if sys.platform in ['win32', 'cygwin'] else 'ninja'
  20. if sys.platform in ['win32', 'cygwin']:
  21. # psutil handles short-lived ninja descendants poorly on Windows (it misses
  22. # most of them) so use wmic instead.
  23. import subprocess
  24. cmd = 'wmic process get Caption,ParentProcessId,ProcessId,CommandLine'
  25. lines = subprocess.check_output(cmd, universal_newlines=True).splitlines()
  26. # Find the offsets for the various data columns by looking at the labels in
  27. # the first line of output.
  28. CAPTION_OFF = 0
  29. COMMAND_LINE_OFF = lines[0].find('CommandLine')
  30. PARENT_PID_OFF = lines[0].find('ParentProcessId')
  31. PID_OFF = lines[0].find(' ProcessId') + 1
  32. for line in lines[1:]:
  33. # Ignore blank lines
  34. if not line.strip():
  35. continue
  36. command = line[:COMMAND_LINE_OFF].strip()
  37. command_line = line[COMMAND_LINE_OFF:PARENT_PID_OFF].strip()
  38. parent_pid = int(line[PARENT_PID_OFF:PID_OFF].strip())
  39. pid = int(line[PID_OFF:].strip())
  40. processes.append((command, command_line, parent_pid, pid))
  41. else:
  42. # Portable process-collection code, but works badly on Windows.
  43. import psutil
  44. for proc in psutil.process_iter(['pid', 'ppid', 'name', 'cmdline']):
  45. try:
  46. cmdline = proc.cmdline()
  47. # Convert from list to a single string.
  48. cmdline = ' '.join(cmdline)
  49. except psutil.AccessDenied:
  50. cmdline = "Access denied"
  51. processes.append(
  52. (proc.name()[:], cmdline, int(proc.ppid()), int(proc.pid)))
  53. # Scan the list of processes to find ninja.
  54. for process in processes:
  55. command, command_line, parent_pid, pid = process
  56. if command.startswith(ninja_prefix):
  57. parents.append(pid)
  58. if not parents:
  59. print('No interesting parent processes found.')
  60. return 1
  61. print('Tracking the children of these PIDs:')
  62. print(', '.join(map(lambda x: str(x), parents)))
  63. print()
  64. # Print all the processes that have parent-processes of interest.
  65. count = 0
  66. for process in processes:
  67. command, command_line, parent_pid, pid = process
  68. if parent_pid in parents:
  69. if not command_line:
  70. command_line = command
  71. print('%5d: %s' % (pid, command_line[:160]))
  72. count += 1
  73. print('Found %d children' % count)
  74. return 0
  75. if __name__ == '__main__':
  76. main()