static int getConsoleProcessCount()

in src/com/pty4j/windows/conpty/ConsoleProcessListFetcher.java [30:80]


  static int getConsoleProcessCount(long pid) throws IOException {
    ProcessBuilder builder = new ProcessBuilder(concat(
        List.of(getPathToJavaExecutable(),
            //  tune JVM to behave more like a client VM for faster startup
            "-XX:TieredStopAtLevel=1", "-XX:CICompilerCount=1", "-XX:+UseSerialGC",
            "-XX:-UsePerfData", // disable the performance monitoring feature
            // Sometimes, ConsoleProcessListChildProcessMain and required dependencies could be packed in huge jars for better classloading performance.
            // For example, when lib\app.jar=438m and lib\3rd-party-rt.jar=73m, the JVM requires at least -Xmx17m to start.
            // Let's give it a bit more.
            "-Xms32m", "-Xmx64m"),
        formatInheritedSystemProperties(JNA_SYSTEM_PROPERTIES),
        List.of("-cp",
            buildClasspath(ConsoleProcessListChildProcessMain.class, Library.class, WinDef.DWORD.class),
            ConsoleProcessListChildProcessMain.class.getName(),
            String.valueOf(pid))
    ));
    // ignore common Java cli options as it may slow down the VM startup
    JAVA_OPTIONS_ENV_VARS.forEach(builder.environment().keySet()::remove);
    builder.redirectErrorStream(true);
    Process process = builder.start();
    StreamGobbler stdout = new StreamGobbler(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
    try {
      process.waitFor(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignored) {
    }
    if (process.isAlive()) {
      LOG.info("Terminating still running child process");
      process.destroy();
    }
    stdout.awaitReadingEnds();
    int exitCode;
    try {
      exitCode = process.exitValue();
    } catch (IllegalThreadStateException e) {
      throw new IOException("Still running child process");
    }
    if (exitCode != 0) {
      throw new IOException("Failed to get console process list: exit code " + exitCode + ", output: " + stdout.getText());
    }
    String processCountStr = getProcessCountStr(stdout.getText());
    try {
      int result = Integer.parseInt(processCountStr);
      if (result <= 1) {
        throw new IOException("Unexpected amount of console processes: " + result);
      }
      return result - 1; // minus "java ConsoleProcessListChildProcessMain" process
    } catch (NumberFormatException e) {
      throw new IOException("Failed to get console process list: cannot parse int from '" + processCountStr +
          "', all output: " + stdout.getText().trim());
    }
  }