Skip to content

Commit

Permalink
8233462: serviceability/tmtools/jstat tests times out with -Xcomp
Browse files Browse the repository at this point in the history
Backport-of: d717078
  • Loading branch information
GoeLin committed Feb 16, 2023
1 parent 5c1748a commit 1d32290
Showing 1 changed file with 19 additions and 32 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,30 +22,22 @@
*/
package common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.time.Instant;

/**
* This class starts a process specified by the passed command line waits till
* the process completes and returns the process exit code and stdout and stderr
* output as ToolResults
*/
class ToolRunner {

private final List<String> cmdArgs = new LinkedList<>();
private final String[] cmdArgs;

ToolRunner(String cmdLine) {
StringTokenizer st = new StringTokenizer(cmdLine);
while (st.hasMoreTokens()) {
cmdArgs.add(st.nextToken());
}
cmdArgs = cmdLine.split(" +");
}

/**
Expand All @@ -56,23 +48,18 @@ class ToolRunner {
* @throws Exception if anything goes wrong
*/
ToolResults runToCompletion() throws Exception {

ProcessBuilder pb = new ProcessBuilder(cmdArgs);
OutputAnalyzer oa = ProcessTools.executeProcess(pb);

return new ToolResults(oa.getExitValue(),
stringToList(oa.getStdout()),
stringToList(oa.getStderr()));

}
Path out = Files.createTempFile(Paths.get("."), "out.", ".txt");
Path err = out.resolveSibling(out.getFileName().toString().replaceFirst("out", "err"));

private static List<String> stringToList(String s) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(s));
List<String> strings = new ArrayList<>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
strings.add(line);
}
reader.close();
return strings;
Process p = pb.redirectOutput(ProcessBuilder.Redirect.to(out.toFile()))
.redirectError(ProcessBuilder.Redirect.to(err.toFile()))
.start();
System.out.printf("[%s] started process %d %s with out/err redirected to '%s' and '%s'%n",
Instant.now().toString(), p.pid(), pb.command(), out.toString(), err.toString());
int exitCode = p.waitFor();
System.out.printf("[%s] process %d finished with exit code = %d%n",
Instant.now().toString(), p.pid(), exitCode);
return new ToolResults(exitCode, Files.readAllLines(out), Files.readAllLines(err));
}
}

1 comment on commit 1d32290

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.