test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
       
     1 /*
       
     2  * Copyright (c) 2019, 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 /**
       
    25  * @test
       
    26  * @bug 8225715
       
    27  * @requires vm.hasSAandCanAttach
       
    28  * @library /test/lib
       
    29  * @compile JShellHeapDumpTest.java
       
    30  * @run main/timeout=240 JShellHeapDumpTest
       
    31  */
       
    32 
       
    33 import static jdk.test.lib.Asserts.assertTrue;
       
    34 
       
    35 import java.io.IOException;
       
    36 import java.io.File;
       
    37 import java.util.List;
       
    38 import java.util.Arrays;
       
    39 import java.util.Map;
       
    40 
       
    41 import jdk.test.lib.JDKToolLauncher;
       
    42 import jdk.test.lib.JDKToolFinder;
       
    43 import jdk.test.lib.process.OutputAnalyzer;
       
    44 import jdk.test.lib.process.ProcessTools;
       
    45 import jdk.test.lib.hprof.parser.HprofReader;
       
    46 
       
    47 import jdk.jshell.JShell;
       
    48 
       
    49 public class JShellHeapDumpTest {
       
    50 
       
    51     static Process jShellProcess;
       
    52     static boolean doSleep = true; // By default do a short sleep when app starts up
       
    53 
       
    54     public static void launch(String expectedMessage, List<String> toolArgs)
       
    55         throws IOException {
       
    56 
       
    57         try {
       
    58             launchJshell();
       
    59             long jShellPID = jShellProcess.pid();
       
    60 
       
    61             System.out.println("Starting " + toolArgs.get(0) + " against " + jShellPID);
       
    62             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
       
    63 
       
    64             for (String cmd : toolArgs) {
       
    65                 launcher.addToolArg(cmd);
       
    66             }
       
    67 
       
    68             launcher.addToolArg("--pid=" + Long.toString(jShellPID));
       
    69 
       
    70             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
       
    71             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
       
    72             System.out.println("jhsdb jmap stdout:");
       
    73             System.out.println(output.getStdout());
       
    74             System.out.println("jhsdb jmap stderr:");
       
    75             System.out.println(output.getStderr());
       
    76             System.out.println("###### End of all output:");
       
    77             output.shouldHaveExitValue(0);
       
    78         } catch (Exception ex) {
       
    79             throw new RuntimeException("Test ERROR " + ex, ex);
       
    80         } finally {
       
    81             if (jShellProcess.isAlive()) {
       
    82                 System.out.println("Destroying jshell");
       
    83                 jShellProcess.destroy();
       
    84                 System.out.println("Jshell destroyed");
       
    85             } else {
       
    86                 System.out.println("Jshell not alive");
       
    87             }
       
    88         }
       
    89     }
       
    90 
       
    91     public static void launch(String expectedMessage, String... toolArgs)
       
    92         throws IOException {
       
    93 
       
    94         launch(expectedMessage, Arrays.asList(toolArgs));
       
    95     }
       
    96 
       
    97     public static void printStackTraces(String file) throws IOException {
       
    98         try {
       
    99             String output = HprofReader.getStack(file, 0);
       
   100             if (!output.contains("JShellToolProvider")) {
       
   101                 throw new RuntimeException("'JShellToolProvider' missing from stdout/stderr");
       
   102             }
       
   103         } catch (Exception ex) {
       
   104             throw new RuntimeException("Test ERROR " + ex, ex);
       
   105         }
       
   106     }
       
   107 
       
   108     public static void testHeapDump() throws IOException {
       
   109         File hprofFile = new File("jhsdb.jmap.heap." +
       
   110                              System.currentTimeMillis() + ".hprof");
       
   111         if (hprofFile.exists()) {
       
   112             hprofFile.delete();
       
   113         }
       
   114 
       
   115         launch("heap written to", "jmap",
       
   116                "--binaryheap", "--dumpfile=" + hprofFile.getAbsolutePath());
       
   117 
       
   118         assertTrue(hprofFile.exists() && hprofFile.isFile(),
       
   119                    "Could not create dump file " + hprofFile.getAbsolutePath());
       
   120 
       
   121         printStackTraces(hprofFile.getAbsolutePath());
       
   122 
       
   123         System.out.println("hprof file size: " + hprofFile.length());
       
   124         hprofFile.delete();
       
   125     }
       
   126 
       
   127     public static void launchJshell() throws IOException {
       
   128         System.out.println("Starting Jshell");
       
   129         long startTime = System.currentTimeMillis();
       
   130         try {
       
   131             ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));
       
   132             jShellProcess = ProcessTools.startProcess("JShell", pb,
       
   133                                                       s -> {  // warm-up predicate
       
   134                                                           return s.contains("Welcome to JShell");
       
   135                                                       });
       
   136         } catch (Exception ex) {
       
   137             throw new RuntimeException("Test ERROR " + ex, ex);
       
   138         }
       
   139 
       
   140         long elapsedTime = System.currentTimeMillis() - startTime;
       
   141         System.out.println("Jshell Started in " + elapsedTime + "ms");
       
   142 
       
   143         // Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
       
   144         try {
       
   145             if (doSleep) {
       
   146                 Thread.sleep(2000);
       
   147             }
       
   148         } catch (Exception e) {
       
   149         }
       
   150     }
       
   151 
       
   152     public static void main(String[] args) throws Exception {
       
   153         if (args.length == 1) {
       
   154             if (args[0].equals("nosleep")) {
       
   155                 doSleep = false;
       
   156             } else {
       
   157                 throw new RuntimeException("Invalid arg: " + args[0]);
       
   158             }
       
   159         } else if (args.length != 0) {
       
   160             throw new RuntimeException("Too many args: " + args.length);
       
   161         }
       
   162         testHeapDump();
       
   163 
       
   164         // The test throws RuntimeException on error.
       
   165         // IOException is thrown if Jshell can't start because of some bad
       
   166         // environment condition
       
   167         System.out.println("Test PASSED");
       
   168     }
       
   169 }