test/hotspot/jtreg/serviceability/tmtools/share/common/ToolRunner.java
author iignatyev
Mon, 18 Nov 2019 16:03:51 -0800
changeset 59130 601b3fe3786d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233462: serviceability/tmtools/jstat tests times out with -Xcomp Reviewed-by: sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     1
/*
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     4
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     8
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    13
 * accompanied this code).
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    14
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    18
 *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    21
 * questions.
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    22
 */
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    23
package common;
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    24
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    25
import java.nio.file.Files;
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    26
import java.nio.file.Path;
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    27
import java.nio.file.Paths;
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    28
import java.util.Arrays;
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    29
import java.time.Instant;
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    30
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    31
/**
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    32
 * This class starts a process specified by the passed command line waits till
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    33
 * the process completes and returns the process exit code and stdout and stderr
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    34
 * output as ToolResults
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    35
 */
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    36
class ToolRunner {
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    37
    private final String[] cmdArgs;
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    38
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    39
    ToolRunner(String cmdLine) {
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    40
        cmdArgs = cmdLine.split(" +");
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    41
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    42
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    43
    /**
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    44
     * Starts the process, waits for the process completion and returns the
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    45
     * results
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    46
     *
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    47
     * @return process results
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    48
     * @throws Exception if anything goes wrong
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    49
     */
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    50
    ToolResults runToCompletion() throws Exception {
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    51
        ProcessBuilder pb = new ProcessBuilder(cmdArgs);
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    52
        Path out = Files.createTempFile(Paths.get("."), "out.", ".txt");
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    53
        Path err = out.resolveSibling(out.getFileName().toString().replaceFirst("out", "err"));
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    54
59130
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    55
        Process p = pb.redirectOutput(ProcessBuilder.Redirect.to(out.toFile()))
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    56
                      .redirectError(ProcessBuilder.Redirect.to(err.toFile()))
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    57
                      .start();
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    58
        System.out.printf("[%s] started process %d %s with out/err redirected to '%s' and '%s'%n",
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    59
                Instant.now().toString(), p.pid(), pb.command(), out.toString(), err.toString());
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    60
        int exitCode = p.waitFor();
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    61
        System.out.printf("[%s] process %d finished with exit code = %d%n",
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    62
                Instant.now().toString(), p.pid(), exitCode);
601b3fe3786d 8233462: serviceability/tmtools/jstat tests times out with -Xcomp
iignatyev
parents: 47216
diff changeset
    63
        return new ToolResults(exitCode, Files.readAllLines(out), Files.readAllLines(err));
35493
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    64
    }
863fb33f9940 8130063: Refactoring tmtools jstat and jstack tests to jtreg
akulyakh
parents:
diff changeset
    65
}