1 /*
   2  * Copyright (c) 2017, 2018, 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 sun.hotspot.code.Compiler;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  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.OutputAnalyzer;
  35 
  36 /*
  37  * @test
  38  * @summary Test the 'universe' command of jhsdb clhsdb.
  39  * @bug 8190307
  40  * @library /test/lib
  41  * @build jdk.test.lib.apps.*
  42  * @build sun.hotspot.WhiteBox
  43  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  44  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestUniverse
  45  */
  46 
  47 public class TestUniverse {
  48 
  49     private static void testClhsdbForUniverse(long lingeredAppPid,
  50                                               String gc) throws Exception {
  51 
  52         Process p;
  53         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  54         launcher.addToolArg("clhsdb");
  55         launcher.addToolArg("--pid");
  56         launcher.addToolArg(Long.toString(lingeredAppPid));
  57 
  58         ProcessBuilder pb = new ProcessBuilder();
  59         pb.command(launcher.getCommand());
  60         System.out.println(
  61             pb.command().stream().collect(Collectors.joining(" ")));
  62 
  63         try {
  64             p = pb.start();
  65         } catch (Exception attachE) {
  66             throw new Error("Couldn't start jhsdb or attach to LingeredApp : " + attachE);
  67         }
  68 
  69         // Issue the 'universe' command at the clhsdb prompt.
  70         OutputStream input = p.getOutputStream();
  71         try {
  72             input.write("universe\n".getBytes());
  73             input.write("quit\n".getBytes());
  74             input.flush();
  75         } catch (IOException ioe) {
  76             throw new Error("Problem issuing the 'universe' command ", ioe);
  77         }
  78 
  79         OutputAnalyzer output = new OutputAnalyzer(p);
  80 
  81         try {
  82             p.waitFor();
  83         } catch (InterruptedException ie) {
  84             p.destroyForcibly();
  85             throw new Error("Problem awaiting the child process: " + ie, ie);
  86         }
  87 
  88         output.shouldHaveExitValue(0);
  89         System.out.println(output.getOutput());
  90 
  91         output.shouldContain("Heap Parameters");
  92         if (gc.contains("G1GC")) {
  93             output.shouldContain("garbage-first heap");
  94             output.shouldContain("region size");
  95             output.shouldContain("G1 Young Generation:");
  96             output.shouldContain("regions  =");
  97         }
  98         if (gc.contains("UseConcMarkSweepGC")) {
  99             output.shouldContain("Gen 1: concurrent mark-sweep generation");
 100         }
 101         if (gc.contains("UseSerialGC")) {
 102             output.shouldContain("Gen 1:   old");
 103         }
 104         if (gc.contains("UseParallelGC")) {
 105             output.shouldContain("ParallelScavengeHeap");
 106             output.shouldContain("PSYoungGen");
 107             output.shouldContain("eden");
 108         }
 109         if (gc.contains("UseEpsilonGC")) {
 110             output.shouldContain("Epsilon heap");
 111             output.shouldContain("reserved");
 112             output.shouldContain("committed");
 113             output.shouldContain("used");
 114         }
 115     }
 116 
 117     public static void test(String gc) throws Exception {
 118         LingeredApp app = null;
 119         try {
 120             List<String> vmArgs = new ArrayList<String>();
 121             vmArgs.add("-XX:+UnlockExperimentalVMOptions"); // unlock experimental GCs
 122             vmArgs.add(gc);
 123             app = LingeredApp.startApp(vmArgs);
 124             System.out.println ("Started LingeredApp with the GC option " + gc +
 125                                 " and pid " + app.getPid());
 126             testClhsdbForUniverse(app.getPid(), gc);
 127         } finally {
 128             LingeredApp.stopApp(app);
 129         }
 130     }
 131 
 132 
 133     public static void main (String... args) throws Exception {
 134 
 135         if (!Platform.shouldSAAttach()) {
 136             System.out.println(
 137                "SA attach not expected to work - test skipped.");
 138             return;
 139         }
 140 
 141         try {
 142             test("-XX:+UseG1GC");
 143             test("-XX:+UseParallelGC");
 144             test("-XX:+UseSerialGC");
 145             if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
 146               test("-XX:+UseConcMarkSweepGC");
 147             }
 148             test("-XX:+UseEpsilonGC");
 149         } catch (Exception e) {
 150             throw new Error("Test failed with " + e);
 151         }
 152     }
 153 }