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