< prev index next >

src/java.base/share/classes/jdk/internal/misc/VM.java

Print this page
rev 52935 : 8215159: Improve initial setup of system Properties
Reviewed-by: mchung, rriggs


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import static java.lang.Thread.State.*;


  29 import java.util.Map;
  30 import java.util.Properties;
  31 
  32 public class VM {
  33 
  34     // the init level when the VM is fully initialized
  35     private static final int JAVA_LANG_SYSTEM_INITED     = 1;
  36     private static final int MODULE_SYSTEM_INITED        = 2;
  37     private static final int SYSTEM_LOADER_INITIALIZING  = 3;
  38     private static final int SYSTEM_BOOTED               = 4;
  39     private static final int SYSTEM_SHUTDOWN             = 5;
  40 
  41 
  42     // 0, 1, 2, ...
  43     private static volatile int initLevel;
  44     private static final Object lock = new Object();
  45 
  46     /**
  47      * Sets the init level.
  48      *


 158      */
 159     public static String getSavedProperty(String key) {
 160         if (savedProps == null)
 161             throw new IllegalStateException("Not yet initialized");
 162 
 163         return savedProps.get(key);
 164     }
 165 
 166     /**
 167      * Gets an unmodifiable view of the system properties saved at system
 168      * initialization time. This method should only be used
 169      * for the system properties that are not changed during runtime.
 170      *
 171      * Note that the saved system properties do not include
 172      * the ones set by java.lang.VersionProps.init().
 173      */
 174     public static Map<String, String> getSavedProperties() {
 175         if (savedProps == null)
 176             throw new IllegalStateException("Not yet initialized");
 177 
 178         return savedProps;
 179     }
 180 
 181     private static Map<String, String> savedProps;
 182 
 183     // Save a private copy of the system properties and remove
 184     // the system properties that are not intended for public access.
 185     //
 186     // This method can only be invoked during system initialization.
 187     public static void saveAndRemoveProperties(Properties props) {
 188         if (initLevel() != 0)
 189             throw new IllegalStateException("Wrong init level");
 190 
 191         @SuppressWarnings({"rawtypes", "unchecked"})
 192         Map<String, String> sp =
 193             Map.ofEntries(props.entrySet().toArray(new Map.Entry[0]));
 194         // only main thread is running at this time, so savedProps and
 195         // its content will be correctly published to threads started later
 196         savedProps = sp;


 197 
 198         // Set the maximum amount of direct memory.  This value is controlled
 199         // by the vm option -XX:MaxDirectMemorySize=<size>.
 200         // The maximum amount of allocatable direct buffer memory (in bytes)
 201         // from the system property sun.nio.MaxDirectMemorySize set by the VM.
 202         // If not set or set to -1, the max memory will be used
 203         // The system property will be removed.
 204         String s = (String)props.remove("sun.nio.MaxDirectMemorySize");
 205         if (s == null || s.isEmpty() || s.equals("-1")) {
 206             // -XX:MaxDirectMemorySize not given, take default
 207             directMemory = Runtime.getRuntime().maxMemory();
 208         } else {
 209             long l = Long.parseLong(s);
 210             if (l > -1)
 211                 directMemory = l;
 212         }
 213 
 214         // Check if direct buffers should be page aligned
 215         s = (String)props.remove("sun.nio.PageAlignDirectMemory");
 216         if ("true".equals(s))
 217             pageAlignDirectMemory = true;
 218 
 219         // Remove other private system properties
 220         // used by java.lang.Integer.IntegerCache
 221         props.remove("java.lang.Integer.IntegerCache.high");
 222 
 223         // used by sun.launcher.LauncherHelper
 224         props.remove("sun.java.launcher.diag");
 225 
 226         // used by jdk.internal.loader.ClassLoaders
 227         props.remove("jdk.boot.class.path.append");
 228     }
 229 
 230     // Initialize any miscellaneous operating system settings that need to be
 231     // set for the class libraries.
 232     //
 233     public static void initializeOSEnvironment() {
 234         if (initLevel() == 0) {
 235             OSEnvironment.initialize();
 236         }
 237     }
 238 
 239     /* Current count of objects pending for finalization */
 240     private static volatile int finalRefCount;
 241 
 242     /* Peak count of objects pending for finalization */
 243     private static volatile int peakFinalRefCount;
 244 
 245     /*
 246      * Gets the number of objects pending for finalization.
 247      *




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.misc;
  27 
  28 import static java.lang.Thread.State.*;
  29 
  30 import java.util.Collections;
  31 import java.util.Map;
  32 import java.util.Properties;
  33 
  34 public class VM {
  35 
  36     // the init level when the VM is fully initialized
  37     private static final int JAVA_LANG_SYSTEM_INITED     = 1;
  38     private static final int MODULE_SYSTEM_INITED        = 2;
  39     private static final int SYSTEM_LOADER_INITIALIZING  = 3;
  40     private static final int SYSTEM_BOOTED               = 4;
  41     private static final int SYSTEM_SHUTDOWN             = 5;
  42 
  43 
  44     // 0, 1, 2, ...
  45     private static volatile int initLevel;
  46     private static final Object lock = new Object();
  47 
  48     /**
  49      * Sets the init level.
  50      *


 160      */
 161     public static String getSavedProperty(String key) {
 162         if (savedProps == null)
 163             throw new IllegalStateException("Not yet initialized");
 164 
 165         return savedProps.get(key);
 166     }
 167 
 168     /**
 169      * Gets an unmodifiable view of the system properties saved at system
 170      * initialization time. This method should only be used
 171      * for the system properties that are not changed during runtime.
 172      *
 173      * Note that the saved system properties do not include
 174      * the ones set by java.lang.VersionProps.init().
 175      */
 176     public static Map<String, String> getSavedProperties() {
 177         if (savedProps == null)
 178             throw new IllegalStateException("Not yet initialized");
 179 
 180         return Collections.unmodifiableMap(savedProps);
 181     }
 182 
 183     private static Map<String, String> savedProps;
 184 
 185     // Save a private copy of the system properties and remove
 186     // the system properties that are not intended for public access.
 187     //
 188     // This method can only be invoked during system initialization.
 189     public static void saveProperties(Map<String, String> props) {
 190         if (initLevel() != 0)
 191             throw new IllegalStateException("Wrong init level");
 192 



 193         // only main thread is running at this time, so savedProps and
 194         // its content will be correctly published to threads started later
 195         if (savedProps == null) {
 196             savedProps = props;
 197         }
 198 
 199         // Set the maximum amount of direct memory.  This value is controlled
 200         // by the vm option -XX:MaxDirectMemorySize=<size>.
 201         // The maximum amount of allocatable direct buffer memory (in bytes)
 202         // from the system property sun.nio.MaxDirectMemorySize set by the VM.
 203         // If not set or set to -1, the max memory will be used
 204         // The system property will be removed.
 205         String s = props.get("sun.nio.MaxDirectMemorySize");
 206         if (s == null || s.isEmpty() || s.equals("-1")) {
 207             // -XX:MaxDirectMemorySize not given, take default
 208             directMemory = Runtime.getRuntime().maxMemory();
 209         } else {
 210             long l = Long.parseLong(s);
 211             if (l > -1)
 212                 directMemory = l;
 213         }
 214 
 215         // Check if direct buffers should be page aligned
 216         s = props.get("sun.nio.PageAlignDirectMemory");
 217         if ("true".equals(s))
 218             pageAlignDirectMemory = true;










 219     }
 220 
 221     // Initialize any miscellaneous operating system settings that need to be
 222     // set for the class libraries.
 223     //
 224     public static void initializeOSEnvironment() {
 225         if (initLevel() == 0) {
 226             OSEnvironment.initialize();
 227         }
 228     }
 229 
 230     /* Current count of objects pending for finalization */
 231     private static volatile int finalRefCount;
 232 
 233     /* Peak count of objects pending for finalization */
 234     private static volatile int peakFinalRefCount;
 235 
 236     /*
 237      * Gets the number of objects pending for finalization.
 238      *


< prev index next >