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