1 /*
2 * Copyright (c) 2014, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test id=invalid
26 * @bug 8022865
27 * @summary Tests for the -XX:CompressedClassSpaceSize command line option
28 * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
29 * @requires vm.flagless
30 * @library /test/lib
31 * @modules java.base/jdk.internal.misc java.management
32 * @run driver CompressedClassSpaceSize invalid
33 */
34
35 /*
36 * @test id=valid_small
37 * @bug 8022865
38 * @summary Tests for the -XX:CompressedClassSpaceSize command line option
39 * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
40 * @requires vm.flagless
41 * @library /test/lib
42 * @modules java.base/jdk.internal.misc java.management
43 * @run driver CompressedClassSpaceSize valid_small
44 */
45
46 /*
47 * @test id=valid_large_nocds
48 * @bug 8022865
49 * @summary Tests for the -XX:CompressedClassSpaceSize command line option
50 * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
51 * @requires vm.flagless
52 * @library /test/lib
53 * @modules java.base/jdk.internal.misc java.management
54 * @run driver CompressedClassSpaceSize valid_large_nocds
55 */
56
57 /*
58 * @test id=valid_large_cds
59 * @bug 8022865
60 * @summary Tests for the -XX:CompressedClassSpaceSize command line option
61 * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true & vm.cds
62 * @requires vm.flagless
63 * @library /test/lib
64 * @modules java.base/jdk.internal.misc java.management
65 * @run driver CompressedClassSpaceSize valid_large_cds
66 */
67
68 import jdk.test.lib.process.ProcessTools;
69 import jdk.test.lib.process.OutputAnalyzer;
70
71 public class CompressedClassSpaceSize {
72
73 final static long MB = 1024 * 1024;
74
75 final static long minAllowedClassSpaceSize = MB;
76 final static long minRealClassSpaceSize = 16 * MB;
77 final static long maxClassSpaceSize = 4096 * MB;
78
79 // For the valid_large_cds sub test: we need to have a notion of what archive size to
80 // maximally expect, with a generous fudge factor to avoid having to tweak this test
81 // ofent. Note: today's default archives are around 16-20 MB.
82 final static long maxExpectedArchiveSize = 512 * MB;
83
84 public static void main(String[] args) throws Exception {
85 ProcessBuilder pb;
86 OutputAnalyzer output;
87
88 switch (args[0]) {
89 case "invalid": {
90 // < Minimum size
91 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:CompressedClassSpaceSize=0",
92 "-version");
93 output = new OutputAnalyzer(pb.start());
94 output.shouldContain("outside the allowed range")
95 .shouldHaveExitValue(1);
96
97 // Invalid size of -1 should be handled correctly
98 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:CompressedClassSpaceSize=-1",
99 "-version");
100 output = new OutputAnalyzer(pb.start());
101 output.shouldContain("Improperly specified VM option 'CompressedClassSpaceSize=-1'")
102 .shouldHaveExitValue(1);
103
104 // > Maximum size
105 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:CompressedClassSpaceSize=" + maxClassSpaceSize + 1,
106 "-version");
107 output = new OutputAnalyzer(pb.start());
108 output.shouldContain("outside the allowed range")
109 .shouldHaveExitValue(1);
110 }
111 break;
112 case "valid_small": {
113 // Make sure the minimum size is set correctly and printed
114 // (Note: ccs size are rounded up to the next larger root chunk boundary (16m).
115 // Note that this is **reserved** size and does not affect rss.
116 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
117 "-XX:CompressedClassSpaceSize=" + minAllowedClassSpaceSize,
118 "-Xlog:gc+metaspace",
119 "-version");
120 output = new OutputAnalyzer(pb.start());
121 output.shouldMatch("Compressed class space.*" + minRealClassSpaceSize)
122 .shouldHaveExitValue(0);
123 }
124 break;
125 case "valid_large_nocds": {
126 // Without CDS, we should get 4G
127 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:CompressedClassSpaceSize=" + maxClassSpaceSize,
128 "-Xshare:off", "-Xlog:metaspace*", "-version");
129 output = new OutputAnalyzer(pb.start());
130 output.shouldMatch("Compressed class space.*" + maxClassSpaceSize)
131 .shouldHaveExitValue(0);
132 }
133 break;
134 case "valid_large_cds": {
135 // Create archive
136 pb = ProcessTools.createLimitedTestJavaProcessBuilder(
137 "-XX:SharedArchiveFile=./abc.jsa", "-Xshare:dump", "-version");
138 output = new OutputAnalyzer(pb.start());
139 output.shouldHaveExitValue(0);
140
141 // With CDS, class space should fill whatever the CDS archive leaves us (modulo alignment)
142 pb = ProcessTools.createLimitedTestJavaProcessBuilder("-XX:CompressedClassSpaceSize=" + maxClassSpaceSize,
143 "-XX:SharedArchiveFile=./abc.jsa", "-Xshare:on", "-Xlog:metaspace*", "-version");
144 output = new OutputAnalyzer(pb.start());
145 output.shouldHaveExitValue(0);
146 long reducedSize = Long.parseLong(
147 output.firstMatch("reducing class space size from " + maxClassSpaceSize + " to (\\d+)", 1));
148 if (reducedSize < (maxClassSpaceSize - maxExpectedArchiveSize)) {
149 output.reportDiagnosticSummary();
150 throw new RuntimeException("Class space size too small?");
151 }
152 output.shouldMatch("Compressed class space.*" + reducedSize);
153 }
154 break;
155 default:
156 throw new RuntimeException("invalid sub test " + args[0]);
157 }
158 }
159 }