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 id=test-64bit-noccs
 72  * @summary Test the VM.metaspace command
 73  * @requires vm.bits == "64"
 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 -XX:-UseCompressedOops -XX:-UseCompressedClassPointers PrintMetaspaceDcmd
 78  */
 79 
 80  /*
 81  * @test id=test-nospecified
 82  * @summary Test the VM.metaspace command
 83  * @requires vm.bits == "64"
 84  * @library /test/lib
 85  * @modules java.base/jdk.internal.misc
 86  *          java.management
 87  * @run main/othervm -Dno-specified-flag -Xmx100M -XX:-UseCompressedOops -XX:-UseCompressedClassPointers PrintMetaspaceDcmd
 88  */
 89 
 90 /*
 91  * @test test-32bit
 92  * @summary Test the VM.metaspace command
 93  * @requires vm.bits == "32"
 94  * @library /test/lib
 95  * @modules java.base/jdk.internal.misc
 96  *          java.management
 97  * @run main/othervm -Dwithout-compressed-class-space -XX:MaxMetaspaceSize=201M -Xmx100M PrintMetaspaceDcmd
 98  */
 99 
100 public class PrintMetaspaceDcmd {
101 
102     private static void doTheNoSpecifiedPropTest() throws Exception {
103         ProcessBuilder pb = new ProcessBuilder();
104         OutputAnalyzer output;
105         // Grab my own PID
106         String pid = Long.toString(ProcessTools.getProcessId());
107 
108         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "basic"});
109         output = new OutputAnalyzer(pb.start());
110         output.shouldHaveExitValue(0);
111         output.shouldMatch("MaxMetaspaceSize: unlimited");
112     }
113 
114     private static void doTheCCSPropTest(boolean usesCompressedClassSpace) throws Exception {
115         ProcessBuilder pb = new ProcessBuilder();
116         OutputAnalyzer output;
117         // Grab my own PID
118         String pid = Long.toString(ProcessTools.getProcessId());
119 
120         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "basic"});
121         output = new OutputAnalyzer(pb.start());
122         output.shouldHaveExitValue(0);
123         if (usesCompressedClassSpace) {
124             output.shouldContain("Non-Class:");
125             output.shouldContain("Class:");
126         }
127         output.shouldContain("Virtual space:");
128         output.shouldContain("Chunk freelists:");
129         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
130 
131         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace"});
132         output = new OutputAnalyzer(pb.start());
133         output.shouldHaveExitValue(0);
134         if (usesCompressedClassSpace) {
135             output.shouldContain("Non-Class:");
136             output.shouldContain("Class:");
137         }
138         output.shouldContain("Virtual space:");
139         output.shouldContain("Chunk freelist");
140         output.shouldContain("Waste");
141         output.shouldMatch("MaxMetaspaceSize:.*201.00.*MB");
142 
143         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "show-loaders"});
144         output = new OutputAnalyzer(pb.start());
145         output.shouldHaveExitValue(0);
146         output.shouldMatch("CLD.*<bootstrap>");
147 
148         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "by-chunktype"});
149         output = new OutputAnalyzer(pb.start());
150         output.shouldHaveExitValue(0);
151         output.shouldContain("1k:");
152         output.shouldContain("2k:");
153         output.shouldContain("4k:");
154         output.shouldContain("8k:");
155         output.shouldContain("16k:");
156         output.shouldContain("32k:");
157         output.shouldContain("64k:");
158         output.shouldContain("128k:");
159         output.shouldContain("256k:");
160         output.shouldContain("512k:");
161         output.shouldContain("1m:");
162         output.shouldContain("2m:");
163         output.shouldContain("4m:");
164 
165         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "vslist"});
166         output = new OutputAnalyzer(pb.start());
167         output.shouldHaveExitValue(0);
168         output.shouldContain("Virtual space list");
169         output.shouldMatch("node.*reserved.*committed.*used.*");
170 
171         // Test with different scales
172         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=G"});
173         output = new OutputAnalyzer(pb.start());
174         output.shouldHaveExitValue(0);
175         output.shouldMatch("MaxMetaspaceSize:.*0.2.*GB");
176 
177         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=K"});
178         output = new OutputAnalyzer(pb.start());
179         output.shouldHaveExitValue(0);
180         output.shouldMatch("MaxMetaspaceSize:.*205824.00 KB");
181 
182         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=1"});
183         output = new OutputAnalyzer(pb.start());
184         output.shouldHaveExitValue(0);
185         output.shouldMatch("MaxMetaspaceSize:.*210763776 bytes");
186     }
187 
188     public static void main(String args[]) throws Exception {
189         if (System.getProperty("no-specified-flag") != null) {
190             doTheNoSpecifiedPropTest();
191         } else if (System.getProperty("with-compressed-class-space") != null) {
192             doTheCCSPropTest(true);
193         } else if (System.getProperty("without-compressed-class-space") != null) {
194             doTheCCSPropTest(false);
195         } else {
196             throw new IllegalArgumentException("Unrecognized running mode");
197         }
198     }
199 }