hotspot/hotspot/test/serviceability/sa/TestPrintMdo.java
changeset 43967 b41a5abbeebc
parent 43966 49d84b4ab981
parent 43965 d9ba85f5cb9d
child 43968 bf02bf2ff560
equal deleted inserted replaced
43966:49d84b4ab981 43967:b41a5abbeebc
     1 /*
       
     2  * Copyright (c) 2017, 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 import java.util.ArrayList;
       
    25 import java.util.Scanner;
       
    26 import java.util.List;
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.util.stream.Collectors;
       
    30 import java.io.OutputStream;
       
    31 import jdk.test.lib.apps.LingeredApp;
       
    32 import jdk.test.lib.JDKToolLauncher;
       
    33 import jdk.test.lib.Platform;
       
    34 import jdk.test.lib.process.ProcessTools;
       
    35 import jdk.test.lib.process.OutputAnalyzer;
       
    36 import jdk.test.lib.Utils;
       
    37 import jdk.test.lib.Asserts;
       
    38 
       
    39 /*
       
    40  * @test
       
    41  * @library /test/lib
       
    42  * @requires vm.flavor == "server" & !vm.emulatedClient
       
    43  * @build jdk.test.lib.apps.*
       
    44  * @run main/othervm TestPrintMdo
       
    45  */
       
    46 
       
    47 public class TestPrintMdo {
       
    48 
       
    49     private static final String PRINTMDO_OUT_FILE = "printmdo_out.txt";
       
    50 
       
    51     private static void verifyPrintMdoOutput() throws Exception {
       
    52 
       
    53         Exception unexpected = null;
       
    54         File printMdoFile = new File(PRINTMDO_OUT_FILE);
       
    55         Asserts.assertTrue(printMdoFile.exists() && printMdoFile.isFile(),
       
    56                            "File with printmdo output not created: " +
       
    57                            printMdoFile.getAbsolutePath());
       
    58         try {
       
    59             Scanner scanner = new Scanner(printMdoFile);
       
    60 
       
    61             String unexpectedMsg =
       
    62                  "One or more of 'VirtualCallData', 'CounterData', " +
       
    63                  "'ReceiverTypeData', 'bci', 'MethodData' "  +
       
    64                  "or 'java/lang/Object' not found";
       
    65             boolean knownClassFound = false;
       
    66             boolean knownProfileDataTypeFound = false;
       
    67             boolean knownTokensFound = false;
       
    68 
       
    69             while (scanner.hasNextLine()) {
       
    70                 String line = scanner.nextLine();
       
    71                 line = line.trim();
       
    72                 System.out.println(line);
       
    73 
       
    74                 if (line.contains("missing reason for ")) {
       
    75                     unexpected = new RuntimeException("Unexpected msg: missing reason for ");
       
    76                     break;
       
    77                 }
       
    78                 if (line.contains("VirtualCallData")  ||
       
    79                     line.contains("CounterData")      ||
       
    80                     line.contains("ReceiverTypeData")) {
       
    81                     knownProfileDataTypeFound = true;
       
    82                 }
       
    83                 if (line.contains("bci") ||
       
    84                     line.contains("MethodData")) {
       
    85                     knownTokensFound = true;
       
    86                 }
       
    87                 if (line.contains("java/lang/Object")) {
       
    88                     knownClassFound = true;
       
    89                 }
       
    90             }
       
    91             if ((knownClassFound           == false)  ||
       
    92                 (knownTokensFound          == false)  ||
       
    93                 (knownProfileDataTypeFound == false)) {
       
    94                 unexpected = new RuntimeException(unexpectedMsg);
       
    95             }
       
    96             if (unexpected != null) {
       
    97                 throw unexpected;
       
    98             }
       
    99         } catch (Exception ex) {
       
   100            throw new RuntimeException("Test ERROR " + ex, ex);
       
   101         } finally {
       
   102            printMdoFile.delete();
       
   103         }
       
   104     }
       
   105 
       
   106     private static void startClhsdbForPrintMdo(long lingeredAppPid) throws Exception {
       
   107 
       
   108         Process p;
       
   109         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
       
   110         launcher.addToolArg("clhsdb");
       
   111         launcher.addToolArg("--pid");
       
   112         launcher.addToolArg(Long.toString(lingeredAppPid));
       
   113 
       
   114         ProcessBuilder pb = new ProcessBuilder();
       
   115         pb.command(launcher.getCommand());
       
   116         System.out.println(
       
   117             pb.command().stream().collect(Collectors.joining(" ")));
       
   118 
       
   119         try {
       
   120             p = pb.start();
       
   121         } catch (Exception attachE) {
       
   122             throw new Error("Couldn't start jhsdb or attach to LingeredApp : " + attachE);
       
   123         }
       
   124 
       
   125         // Issue the 'printmdo' input at the clhsdb prompt.
       
   126         OutputStream input = p.getOutputStream();
       
   127         String str = "printmdo -a > " + PRINTMDO_OUT_FILE + "\nquit\n";
       
   128         try {
       
   129             input.write(str.getBytes());
       
   130             input.flush();
       
   131         } catch (IOException ioe) {
       
   132             throw new Error("Problem issuing the printmdo command: " + str, ioe);
       
   133         }
       
   134 
       
   135         try {
       
   136             p.waitFor();
       
   137         } catch (InterruptedException ie) {
       
   138             throw new Error("Problem awaiting the child process: " + ie, ie);
       
   139         }
       
   140 
       
   141         int exitValue = p.exitValue();
       
   142         if (exitValue != 0) {
       
   143             String output;
       
   144             try {
       
   145                 output = new OutputAnalyzer(p).getOutput();
       
   146             } catch (IOException ioe) {
       
   147                 throw new Error("Can't get failed clhsdb process output: " + ioe, ioe);
       
   148             }
       
   149             throw new AssertionError("clhsdb wasn't run successfully: " + output);
       
   150         }
       
   151     }
       
   152 
       
   153     public static void main (String... args) throws Exception {
       
   154 
       
   155         LingeredApp app = null;
       
   156 
       
   157         if (!Platform.shouldSAAttach()) {
       
   158             System.out.println(
       
   159                "SA attach not expected to work - test skipped.");
       
   160             return;
       
   161         }
       
   162 
       
   163         try {
       
   164             List<String> vmArgs = new ArrayList<String>();
       
   165             vmArgs.add("-XX:+ProfileInterpreter");
       
   166             vmArgs.addAll(Utils.getVmOptions());
       
   167 
       
   168             app = LingeredApp.startApp(vmArgs);
       
   169             System.out.println ("Started LingeredApp with pid " + app.getPid());
       
   170             startClhsdbForPrintMdo(app.getPid());
       
   171             verifyPrintMdoOutput();
       
   172         } finally {
       
   173             LingeredApp.stopApp(app);
       
   174         }
       
   175     }
       
   176 }