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         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "chunkfreelist"});
172         // Output should look somewhat like this...
173         // vvvvvvvvvvvvvvvv
174         // Chunk freelist details:
175         // Non-Class:
176         // cm non-class-space: 5 chunks, total word size: 402944.
177         //         -- List[lv00]: empty
178         //         -- List[lv01]:  - <Chunk @0x00007f925c124090, state f, base 0x00007f9208600000, level lv01 (262144 words), used 0 words, committed 0 words.> - total : 1 chunks.
179         //         -- List[lv02]:  - <Chunk @0x00007f925c1240d8, state f, base 0x00007f9208500000, level lv02 (131072 words), used 0 words, committed 0 words.> - total : 1 chunks.
180         //         -- List[lv03]: empty
181         // .....
182         //
183         // total chunks: 5, total word size: 402944.
184         // ^^^^^^^^^^^^^^^^^
185         // .... but the actual number of chunks in the freelist is difficult to predict and may be low or zero since
186         //  no class unloading happened yet.
187         output = new OutputAnalyzer(pb.start());
188         output.shouldHaveExitValue(0);
189         output.shouldContain("Chunk freelist details:");
190         // ... but we should see at least one one chunk somewhere, the list should never be empty.
191         output.shouldMatch(".*-- List\\[lv00\\].*");
192         output.shouldMatch(".*total chunks.*total word size.*");
193 
194         // Test with different scales
195         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=G"});
196         output = new OutputAnalyzer(pb.start());
197         output.shouldHaveExitValue(0);
198         output.shouldMatch("MaxMetaspaceSize:.*0.2.*GB");
199 
200         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=K"});
201         output = new OutputAnalyzer(pb.start());
202         output.shouldHaveExitValue(0);
203         output.shouldMatch("MaxMetaspaceSize:.*205824.00 KB");
204 
205         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "scale=1"});
206         output = new OutputAnalyzer(pb.start());
207         output.shouldHaveExitValue(0);
208         output.shouldMatch("MaxMetaspaceSize:.*210763776 bytes");
209     }
210 
211     public static void main(String args[]) throws Exception {
212         if (System.getProperty("no-specified-flag") != null) {
213             doTheNoSpecifiedPropTest();
214         } else if (System.getProperty("with-compressed-class-space") != null) {
215             doTheCCSPropTest(true);
216         } else if (System.getProperty("without-compressed-class-space") != null) {
217             doTheCCSPropTest(false);
218         } else {
219             throw new IllegalArgumentException("Unrecognized running mode");
220         }
221     }
222 }
--- EOF ---