1 /*
   2  * Copyright (c) 2014, 2017, 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 /* @test LimitSharedSizes
  25  * @summary Test handling of limits on shared space size
  26  * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true)
  27  * @library /test/lib /runtime/CommandLine/OptionsValidation/common
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  *          jdk.attach/sun.tools.attach
  31  * @run main LimitSharedSizes
  32  */
  33 
  34 import jdk.test.lib.cds.CDSTestUtils;
  35 import jdk.test.lib.process.ProcessTools;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.Platform;
  38 import optionsvalidation.JVMOptionsUtils;
  39 
  40 public class LimitSharedSizes {
  41     static enum Result {
  42         OUT_OF_RANGE,
  43         TOO_SMALL,
  44         VALID,
  45         VALID_ARCHIVE
  46     }
  47 
  48     static enum Region {
  49         RO, RW, MD, MC
  50     }
  51 
  52     private static final boolean fitsRange(String name, String value) throws RuntimeException {
  53         boolean fits = true;
  54         try {
  55             fits = JVMOptionsUtils.fitsRange(name, value);
  56         } catch (Exception e) {
  57             throw new RuntimeException(e.getMessage());
  58         }
  59         return fits;
  60     }
  61 
  62     private static class SharedSizeTestData {
  63         public String optionName;
  64         public String optionValue;
  65         public Result optionResult;
  66 
  67         public SharedSizeTestData(Region region, String value) {
  68             optionName = "-XX:"+getName(region);
  69             optionValue = value;
  70             if (fitsRange(getName(region), value) == false) {
  71                 optionResult = Result.OUT_OF_RANGE;
  72             } else {
  73                 optionResult = Result.TOO_SMALL;
  74             }
  75         }
  76 
  77         public SharedSizeTestData(Region region, String value, Result result) {
  78             optionName = "-XX:"+getName(region);
  79             optionValue = value;
  80             optionResult = result;
  81         }
  82 
  83         private String getName(Region region) {
  84             String name;
  85             switch (region) {
  86                 case RO:
  87                     name = "SharedReadOnlySize";
  88                     break;
  89                 case RW:
  90                     name = "SharedReadWriteSize";
  91                     break;
  92                 case MD:
  93                     name = "SharedMiscDataSize";
  94                     break;
  95                 case MC:
  96                     name = "SharedMiscCodeSize";
  97                     break;
  98                 default:
  99                     name = "Unknown";
 100                     break;
 101             }
 102             return name;
 103         }
 104 
 105         public Result getResult() {
 106             return optionResult;
 107         }
 108     }
 109 
 110     private static final SharedSizeTestData[] testTable = {
 111         // Too small of a region size should not cause a vm crash.
 112         // It should result in an error message either like the following #1:
 113         // The shared miscellaneous code space is not large enough
 114         // to preload requested classes. Use -XX:SharedMiscCodeSize=
 115         // to increase the initial size of shared miscellaneous code space.
 116         // or #2:
 117         // The shared miscellaneous code space is outside the allowed range
 118         new SharedSizeTestData(Region.RO, "4M"),
 119         new SharedSizeTestData(Region.RW, "4M"),
 120         new SharedSizeTestData(Region.MD, "50k"),
 121         new SharedSizeTestData(Region.MC, "20k"),
 122 
 123         // these values are larger than default ones, and should
 124         // be acceptable and not cause failure
 125         new SharedSizeTestData(Region.RO, "20M", Result.VALID),
 126         new SharedSizeTestData(Region.RW, "20M", Result.VALID),
 127         new SharedSizeTestData(Region.MD, "20M", Result.VALID),
 128         new SharedSizeTestData(Region.MC, "20M", Result.VALID),
 129 
 130         // test with sizes which just meet the minimum required sizes
 131         // the following tests also attempt to use the shared archive
 132         new SharedSizeTestData(Region.RO, Platform.is64bit() ? "14M":"9M", Result.VALID_ARCHIVE),
 133         new SharedSizeTestData(Region.RW, Platform.is64bit() ? "12M":"7M", Result.VALID_ARCHIVE),
 134         new SharedSizeTestData(Region.MD, Platform.is64bit() ? "4M":"2M", Result.VALID_ARCHIVE),
 135         new SharedSizeTestData(Region.MC, "120k", Result.VALID_ARCHIVE),
 136     };
 137 
 138     public static void main(String[] args) throws Exception {
 139         int counter = 0;
 140         for (SharedSizeTestData td : testTable) {
 141             String fileName = "LimitSharedSizes" + counter + ".jsa";
 142             counter++;
 143 
 144             String option = td.optionName + "=" + td.optionValue;
 145             System.out.println("testing option number <" + counter + ">");
 146             System.out.println("testing option <" + option + ">");
 147 
 148             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 149                "-XX:+UnlockDiagnosticVMOptions",
 150                "-XX:SharedArchiveFile=./" + fileName,
 151                option,
 152                "-Xshare:dump");
 153 
 154             OutputAnalyzer output = CDSTestUtils.executeAndLog(pb, "dump" + counter);
 155 
 156             switch (td.getResult()) {
 157                 case VALID:
 158                 case VALID_ARCHIVE:
 159                 {
 160                   output.shouldNotContain("space is not large enough");
 161                   output.shouldHaveExitValue(0);
 162 
 163                   if (td.getResult() == Result.VALID_ARCHIVE) {
 164                       // try to use the archive
 165                       pb = ProcessTools.createJavaProcessBuilder(
 166                          "-XX:+UnlockDiagnosticVMOptions",
 167                          "-XX:SharedArchiveFile=./" + fileName,
 168                          "-XX:+PrintSharedArchiveAndExit",
 169                          "-version");
 170 
 171                       output = CDSTestUtils.executeAndLog(pb, "use" + counter);
 172                       if(CDSTestUtils.isUnableToMap(output)) {
 173                           System.out.println("Unable to use shared archive: " +
 174                                              "test not executed; assumed passed");
 175                           continue;
 176                       } else {
 177                           output.shouldHaveExitValue(0);
 178                       }
 179                   }
 180                 }
 181                 break;
 182                 case TOO_SMALL:
 183                 {
 184                     output.shouldContain("space is not large enough");
 185                     output.shouldHaveExitValue(2);
 186                 }
 187                 break;
 188                 case OUT_OF_RANGE:
 189                 {
 190                     output.shouldContain("outside the allowed range");
 191                     output.shouldHaveExitValue(1);
 192                 }
 193                 break;
 194             }
 195         }
 196     }
 197 }