1 /*
   2  * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 import jdk.test.lib.process.ProcessTools;
  26 import jdk.test.lib.process.OutputAnalyzer;
  27 import jdk.test.lib.JDKToolFinder;
  28 
  29 /*
  30  * @test
  31  * @summary Test the VM.metaspace command
  32  * @requires vm.gc != "Z" & vm.bits != "32"
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  * @run main/othervm -XX:MaxMetaspaceSize=201M -Xmx100M -XX:+UseCompressedOops -XX:+UseCompressedClassPointers PrintMetaspaceDcmd with-compressed-class-space
  37  * @run main/othervm -XX:MaxMetaspaceSize=201M -Xmx100M -XX:-UseCompressedOops -XX:-UseCompressedClassPointers PrintMetaspaceDcmd without-compressed-class-space
  38  */
  39 /*
  40  * @test
  41  * @summary Test the VM.metaspace command
  42  * @requires vm.gc != "Z" & vm.bits == "32"
  43  * @library /test/lib
  44  * @modules java.base/jdk.internal.misc
  45  *          java.management
  46  * @run main/othervm -XX:MaxMetaspaceSize=201M -Xmx100M PrintMetaspaceDcmd without-compressed-class-space
  47  */
  48 
  49 public class PrintMetaspaceDcmd {
  50 
  51     // Run jcmd VM.metaspace against a VM with CompressedClassPointers on.
  52     // The report should detail Non-Class and Class portions separately.
  53     private static void doTheTest(boolean usesCompressedClassSpace) throws Exception {
  54         ProcessBuilder pb = new ProcessBuilder();
  55         OutputAnalyzer output;
  56         // Grab my own PID
  57         String pid = Long.toString(ProcessTools.getProcessId());
  58 
  59         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "basic"});
  60         output = new OutputAnalyzer(pb.start());
  61         output.shouldHaveExitValue(0);
  62         if (usesCompressedClassSpace) {
  63             output.shouldContain("Non-Class:");
  64             output.shouldContain("Class:");
  65         }
  66         output.shouldContain("Virtual space:");
  67         output.shouldContain("Chunk freelists:");
  68         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
  69 
  70         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace"});
  71         output = new OutputAnalyzer(pb.start());
  72         output.shouldHaveExitValue(0);
  73         if (usesCompressedClassSpace) {
  74             output.shouldContain("Non-Class:");
  75             output.shouldContain("Class:");
  76         }
  77         output.shouldContain("Virtual space:");
  78         output.shouldContain("Chunk freelist");
  79         output.shouldContain("Waste");
  80         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
  81 
  82         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "show-loaders"});
  83         output = new OutputAnalyzer(pb.start());
  84         output.shouldHaveExitValue(0);
  85         output.shouldMatch("CLD.*<bootstrap>");
  86 
  87         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "by-chunktype"});
  88         output = new OutputAnalyzer(pb.start());
  89         output.shouldHaveExitValue(0);
  90         output.shouldContain("specialized:");
  91         output.shouldContain("small:");
  92         output.shouldContain("medium:");
  93         output.shouldContain("humongous:");
  94 
  95         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "vslist"});
  96         output = new OutputAnalyzer(pb.start());
  97         output.shouldHaveExitValue(0);
  98         output.shouldContain("Virtual space list");
  99         output.shouldMatch("node.*reserved.*committed.*used.*");
 100 
 101         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "vsmap"});
 102         output = new OutputAnalyzer(pb.start());
 103         output.shouldHaveExitValue(0);
 104         output.shouldContain("Virtual space map:");
 105         output.shouldContain("HHHHHHHHHHH");
 106 
 107         // Test with different scales
 108         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=G"});
 109         output = new OutputAnalyzer(pb.start());
 110         output.shouldHaveExitValue(0);
 111         output.shouldMatch("MaxMetaspaceSize:.*0.2.*GB");
 112 
 113         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=K"});
 114         output = new OutputAnalyzer(pb.start());
 115         output.shouldHaveExitValue(0);
 116         output.shouldMatch("MaxMetaspaceSize:.*205824.00 KB");
 117 
 118         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=1"});
 119         output = new OutputAnalyzer(pb.start());
 120         output.shouldHaveExitValue(0);
 121         output.shouldMatch("MaxMetaspaceSize:.*210763776 bytes");
 122     }
 123 
 124     public static void main(String args[]) throws Exception {
 125         boolean testForCompressedClassSpace = false;
 126         if (args[0].equals("with-compressed-class-space")) {
 127             testForCompressedClassSpace = true;
 128         } else if (args[0].equals("without-compressed-class-space")) {
 129             testForCompressedClassSpace = false;
 130         } else {
 131             throw new IllegalArgumentException("Invalid argument: " + args[0]);
 132         }
 133         doTheTest(testForCompressedClassSpace);
 134     }
 135 }