hotspot/test/serviceability/tmtools/share/common/TmTool.java
changeset 35493 863fb33f9940
child 41705 332239c052cc
equal deleted inserted replaced
35491:663c609dfeee 35493:863fb33f9940
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22 
       
    23  */
       
    24 package common;
       
    25 
       
    26 import java.nio.file.Path;
       
    27 import java.nio.file.Paths;
       
    28 import jdk.test.lib.Platform;
       
    29 
       
    30 /**
       
    31  * A tool, such as jstat, jmap, etc Specific tools are defined as subclasses
       
    32  * parameterized by their corresponding ToolResults subclasses
       
    33  */
       
    34 public abstract class TmTool<T extends ToolResults> {
       
    35 
       
    36     private final Class<T> resultsClz;
       
    37     private final String cmdLine;
       
    38 
       
    39     public TmTool(Class<T> resultsClz, String toolName, String otherArgs) {
       
    40         this.resultsClz = resultsClz;
       
    41         this.cmdLine = adjustForTestJava(toolName) + " " + otherArgs;
       
    42     }
       
    43 
       
    44     /**
       
    45      * Runs the tool to completion and returns the results
       
    46      *
       
    47      * @return the tool results
       
    48      * @throws Exception if anything goes wrong
       
    49      */
       
    50     public T measure() throws Exception {
       
    51         ToolRunner runner = new ToolRunner(cmdLine);
       
    52         ToolResults rawResults = runner.runToCompletion();
       
    53         System.out.println("Process output: " + rawResults);
       
    54         return resultsClz.getDeclaredConstructor(ToolResults.class).newInstance(rawResults);
       
    55     }
       
    56 
       
    57     private String adjustForTestJava(String toolName) {
       
    58         // We need to make sure we are running the tol from the JDK under testing
       
    59         String jdkPath = System.getProperty("test.jdk");
       
    60         if (jdkPath == null || !Paths.get(jdkPath).toFile().exists()) {
       
    61             throw new RuntimeException("property test.jdk not not set");
       
    62         }
       
    63         Path toolPath = Paths.get("bin", toolName + (Platform.isWindows() ? ".exe" : ""));
       
    64         return Paths.get(jdkPath, toolPath.toString()).toString();
       
    65     }
       
    66 
       
    67 }