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