1 /*
  2  * Copyright (c) 2014, 2021, 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
 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
 32  *          java.management
 33  * @run driver CompressedClassSpaceSize
 34  */
 35 
 36 import jdk.test.lib.process.ProcessTools;
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 
 39 public class CompressedClassSpaceSize {
 40 
 41     // Sizes beyond this will be rejected by hotspot arg parsing
 42     // (Lilliput: see Metaspace::max_class_space_size() for details)
 43     static final long max_class_space_size = 2013265920;
 44 
 45     // Below this size class space will be silently enlarged to a multiple of this size
 46     static final long min_class_space_size = 4194304;
 47 
 48     public static void main(String[] args) throws Exception {
 49         ProcessBuilder pb;
 50         OutputAnalyzer output;
 51 
 52         // Invalid size of -1 should be handled correctly
 53         pb = ProcessTools.createJavaProcessBuilder("-XX:CompressedClassSpaceSize=-1",
 54                                                    "-version");
 55         output = new OutputAnalyzer(pb.start());
 56         output.shouldContain("Improperly specified VM option 'CompressedClassSpaceSize=-1'")
 57               .shouldHaveExitValue(1);
 58 
 59         ///////////
 60 
 61         // Going below the minimum size for class space (one root chunk size = atm 4M) should be transparently
 62         // handled by the hotspot, which should round up class space size and not report an error.
 63         pb = ProcessTools.createJavaProcessBuilder("-XX:CompressedClassSpaceSize=1m",
 64                                                    "-Xlog:gc+metaspace=trace",
 65                                                    "-version");
 66         output = new OutputAnalyzer(pb.start());
 67         output.shouldMatch("Compressed class space.*" + min_class_space_size)
 68               .shouldHaveExitValue(0);
 69 
 70         ///////////
 71 
 72         // Try 0. Same result expected.
 73         pb = ProcessTools.createJavaProcessBuilder("-XX:CompressedClassSpaceSize=0",
 74                 "-Xlog:gc+metaspace=trace",
 75                 "-version");
 76         output = new OutputAnalyzer(pb.start());
 77         output.shouldMatch("Compressed class space.*" + min_class_space_size)
 78                 .shouldHaveExitValue(0);
 79 
 80         ///////////
 81 
 82         // Try max allowed size, which should be accepted
 83         pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
 84                                                    "-XX:CompressedClassSpaceSize=" + max_class_space_size,
 85                                                    "-Xlog:gc+metaspace=trace",
 86                                                    "-version");
 87         output = new OutputAnalyzer(pb.start());
 88         output.shouldMatch("Compressed class space.*" + max_class_space_size)
 89               .shouldHaveExitValue(0);
 90 
 91         ///////////
 92 
 93         // Set max allowed size + 1, which should graciously fail
 94         pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
 95                 "-XX:CompressedClassSpaceSize=" + (max_class_space_size + 1),
 96                 "-Xlog:gc+metaspace=trace",
 97                 "-version");
 98         output = new OutputAnalyzer(pb.start());
 99         output.shouldContain("CompressedClassSpaceSize " + (max_class_space_size + 1) + " too large (max: " + max_class_space_size)
100               .shouldHaveExitValue(1);
101 
102     }
103 }