1 /*
  2  * Copyright (c) 2018, 2021, 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 id=test-64bit-ccs
 31  * @summary Test the VM.metaspace command
 32  * @requires vm.bits == "64"
 33  * @library /test/lib
 34  * @modules java.base/jdk.internal.misc
 35  *          java.management
 36  * @run main/othervm -Dwith-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M -XX:+UseCompressedOops -XX:+UseCompressedClassPointers PrintMetaspaceDcmd
 37  */
 38 
 39 /*
 40  * @test id=test-64bit-ccs-noreclaim
 41  * @summary Test the VM.metaspace command
 42  * @requires vm.bits == "64"
 43  * @library /test/lib
 44  * @modules java.base/jdk.internal.misc
 45  *          java.management
 46  * @run main/othervm -Dwith-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:MetaspaceReclaimPolicy=none PrintMetaspaceDcmd
 47  */
 48 
 49 /*
 50  * @test id=test-64bit-ccs-aggressivereclaim
 51  * @summary Test the VM.metaspace command
 52  * @requires vm.bits == "64"
 53  * @library /test/lib
 54  * @modules java.base/jdk.internal.misc
 55  *          java.management
 56  * @run main/othervm -Dwith-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:MetaspaceReclaimPolicy=aggressive PrintMetaspaceDcmd
 57  */
 58 
 59 /*
 60  * @test id=test-64bit-ccs-guarded
 61  * @summary Test the VM.metaspace command
 62  * @requires vm.bits == "64"
 63  * @requires vm.debug == true
 64  * @library /test/lib
 65  * @modules java.base/jdk.internal.misc
 66  *          java.management
 67  * @run main/othervm -Dwith-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UnlockDiagnosticVMOptions -XX:+MetaspaceGuardAllocations PrintMetaspaceDcmd
 68  */
 69 
 70 /*
 71  * @test test-32bit
 72  * @summary Test the VM.metaspace command
 73  * @requires vm.bits == "32"
 74  * @library /test/lib
 75  * @modules java.base/jdk.internal.misc
 76  *          java.management
 77  * @run main/othervm -Dwithout-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M PrintMetaspaceDcmd
 78  */
 79 
 80 public class PrintMetaspaceDcmd {
 81 
 82     private static void doTheNoSpecifiedPropTest() throws Exception {
 83         ProcessBuilder pb = new ProcessBuilder();
 84         OutputAnalyzer output;
 85         // Grab my own PID
 86         String pid = Long.toString(ProcessTools.getProcessId());
 87 
 88         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "basic"});
 89         output = new OutputAnalyzer(pb.start());
 90         output.shouldHaveExitValue(0);
 91         output.shouldMatch("MaxMetaspaceSize: unlimited");
 92     }
 93 
 94     private static void doTheCCSPropTest(boolean usesCompressedClassSpace) throws Exception {
 95         ProcessBuilder pb = new ProcessBuilder();
 96         OutputAnalyzer output;
 97         // Grab my own PID
 98         String pid = Long.toString(ProcessTools.getProcessId());
 99 
100         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "basic"});
101         output = new OutputAnalyzer(pb.start());
102         output.shouldHaveExitValue(0);
103         if (usesCompressedClassSpace) {
104             output.shouldContain("Non-Class:");
105             output.shouldContain("Class:");
106         }
107         output.shouldContain("Virtual space:");
108         output.shouldContain("Chunk freelists:");
109         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
110 
111         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace"});
112         output = new OutputAnalyzer(pb.start());
113         output.shouldHaveExitValue(0);
114         if (usesCompressedClassSpace) {
115             output.shouldContain("Non-Class:");
116             output.shouldContain("Class:");
117         }
118         output.shouldContain("Virtual space:");
119         output.shouldContain("Chunk freelist");
120         output.shouldContain("Waste");
121         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
122 
123         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "show-loaders"});
124         output = new OutputAnalyzer(pb.start());
125         output.shouldHaveExitValue(0);
126         output.shouldMatch("CLD.*<bootstrap>");
127 
128         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "by-chunktype"});
129         output = new OutputAnalyzer(pb.start());
130         output.shouldHaveExitValue(0);
131         output.shouldContain("1k:");
132         output.shouldContain("2k:");
133         output.shouldContain("4k:");
134         output.shouldContain("8k:");
135         output.shouldContain("16k:");
136         output.shouldContain("32k:");
137         output.shouldContain("64k:");
138         output.shouldContain("128k:");
139         output.shouldContain("256k:");
140         output.shouldContain("512k:");
141         output.shouldContain("1m:");
142         output.shouldContain("2m:");
143         output.shouldContain("4m:");
144 
145         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "vslist"});
146         output = new OutputAnalyzer(pb.start());
147         output.shouldHaveExitValue(0);
148         output.shouldContain("Virtual space list");
149         output.shouldMatch("node.*reserved.*committed.*used.*");
150 
151         // Test with different scales
152         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=G"});
153         output = new OutputAnalyzer(pb.start());
154         output.shouldHaveExitValue(0);
155         output.shouldMatch("MaxMetaspaceSize:.*0.2.*GB");
156 
157         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=K"});
158         output = new OutputAnalyzer(pb.start());
159         output.shouldHaveExitValue(0);
160         output.shouldMatch("MaxMetaspaceSize:.*205824.00 KB");
161 
162         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=1"});
163         output = new OutputAnalyzer(pb.start());
164         output.shouldHaveExitValue(0);
165         output.shouldMatch("MaxMetaspaceSize:.*210763776 bytes");
166     }
167 
168     public static void main(String args[]) throws Exception {
169         if (System.getProperty("no-specified-flag") != null) {
170             doTheNoSpecifiedPropTest();
171         } else if (System.getProperty("with-compressed-class-space") != null) {
172             doTheCCSPropTest(true);
173         } else if (System.getProperty("without-compressed-class-space") != null) {
174             doTheCCSPropTest(false);
175         } else {
176             throw new IllegalArgumentException("Unrecognized running mode");
177         }
178     }
179 }