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.Properties; 30 31 public class VM { 32 33 // the init level when the VM is fully initialized 34 private static final int JAVA_LANG_SYSTEM_INITED = 1; 35 private static final int MODULE_SYSTEM_INITED = 2; 36 private static final int SYSTEM_LOADER_INITIALIZING = 3; 37 private static final int SYSTEM_BOOTED = 4; 38 39 // 0, 1, 2, ... 40 private static volatile int initLevel; 41 private static final Object lock = new Object(); 42 43 /** 44 * Sets the init level. 45 * 46 * @see java.lang.System#initPhase1 47 * @see java.lang.System#initPhase2 48 * @see java.lang.System#initPhase3 49 */ 115 private static boolean pageAlignDirectMemory; 116 117 // Returns {@code true} if the direct buffers should be page aligned. This 118 // variable is initialized by saveAndRemoveProperties. 119 public static boolean isDirectMemoryPageAligned() { 120 return pageAlignDirectMemory; 121 } 122 123 /** 124 * Returns true if the given class loader is in the system domain 125 * in which all permissions are granted. 126 */ 127 public static boolean isSystemDomainLoader(ClassLoader loader) { 128 return loader == null; 129 } 130 131 /** 132 * Returns the system property of the specified key saved at 133 * system initialization time. This method should only be used 134 * for the system properties that are not changed during runtime. 135 * It accesses a private copy of the system properties so 136 * that user's locking of the system properties object will not 137 * cause the library to deadlock. 138 * 139 * Note that the saved system properties do not include 140 * the ones set by sun.misc.Version.init(). 141 * 142 */ 143 public static String getSavedProperty(String key) { 144 if (savedProps.isEmpty()) 145 throw new IllegalStateException("Should be non-empty if initialized"); 146 147 return savedProps.getProperty(key); 148 } 149 150 // TODO: the Property Management needs to be refactored and 151 // the appropriate prop keys need to be accessible to the 152 // calling classes to avoid duplication of keys. 153 private static final Properties savedProps = new Properties(); 154 155 // Save a private copy of the system properties and remove 156 // the system properties that are not intended for public access. 157 // 158 // This method can only be invoked during system initialization. 159 public static void saveAndRemoveProperties(Properties props) { 160 if (initLevel() != 0) 161 throw new IllegalStateException("Wrong init level"); 162 163 savedProps.putAll(props); 164 165 // Set the maximum amount of direct memory. This value is controlled 166 // by the vm option -XX:MaxDirectMemorySize=<size>. 167 // The maximum amount of allocatable direct buffer memory (in bytes) 168 // from the system property sun.nio.MaxDirectMemorySize set by the VM. 169 // The system property will be removed. 170 String s = (String)props.remove("sun.nio.MaxDirectMemorySize"); 171 if (s != null) { 172 if (s.equals("-1")) { 173 // -XX:MaxDirectMemorySize not given, take default 174 directMemory = Runtime.getRuntime().maxMemory(); 175 } else { 176 long l = Long.parseLong(s); 177 if (l > -1) 178 directMemory = l; 179 } 180 } 181 182 // Check if direct buffers should be page aligned 183 s = (String)props.remove("sun.nio.PageAlignDirectMemory"); | 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.HashMap; 31 import java.util.Properties; 32 import java.util.Collections; 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 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 * 49 * @see java.lang.System#initPhase1 50 * @see java.lang.System#initPhase2 51 * @see java.lang.System#initPhase3 52 */ 118 private static boolean pageAlignDirectMemory; 119 120 // Returns {@code true} if the direct buffers should be page aligned. This 121 // variable is initialized by saveAndRemoveProperties. 122 public static boolean isDirectMemoryPageAligned() { 123 return pageAlignDirectMemory; 124 } 125 126 /** 127 * Returns true if the given class loader is in the system domain 128 * in which all permissions are granted. 129 */ 130 public static boolean isSystemDomainLoader(ClassLoader loader) { 131 return loader == null; 132 } 133 134 /** 135 * Returns the system property of the specified key saved at 136 * system initialization time. This method should only be used 137 * for the system properties that are not changed during runtime. 138 * 139 * Note that the saved system properties do not include 140 * the ones set by java.lang.VersionProps.init(). 141 */ 142 public static String getSavedProperty(String key) { 143 if (savedProps == null) 144 throw new IllegalStateException("Not yet initialized"); 145 146 return savedProps.get(key); 147 } 148 149 /** 150 * Gets an unmodifiable view of the system properties saved at system 151 * initialization time. This method should only be used 152 * for the system properties that are not changed during runtime. 153 * 154 * Note that the saved system properties do not include 155 * the ones set by java.lang.VersionProps.init(). 156 */ 157 public static Map<String, String> getSavedProperties() { 158 if (savedProps == null) 159 throw new IllegalStateException("Not yet initialized"); 160 161 return savedProps; 162 } 163 164 private static Map<String, String> savedProps; 165 166 // Save a private copy of the system properties and remove 167 // the system properties that are not intended for public access. 168 // 169 // This method can only be invoked during system initialization. 170 public static void saveAndRemoveProperties(Properties props) { 171 if (initLevel() != 0) 172 throw new IllegalStateException("Wrong init level"); 173 174 @SuppressWarnings({"rawtypes", "unchecked"}) 175 Map<String, String> sp = 176 Map.ofEntries(props.entrySet().toArray(new Map.Entry[0])); 177 // only main thread is running at this time, so savedProps and 178 // its content will be correctly published to threads started later 179 savedProps = sp; 180 181 // Set the maximum amount of direct memory. This value is controlled 182 // by the vm option -XX:MaxDirectMemorySize=<size>. 183 // The maximum amount of allocatable direct buffer memory (in bytes) 184 // from the system property sun.nio.MaxDirectMemorySize set by the VM. 185 // The system property will be removed. 186 String s = (String)props.remove("sun.nio.MaxDirectMemorySize"); 187 if (s != null) { 188 if (s.equals("-1")) { 189 // -XX:MaxDirectMemorySize not given, take default 190 directMemory = Runtime.getRuntime().maxMemory(); 191 } else { 192 long l = Long.parseLong(s); 193 if (l > -1) 194 directMemory = l; 195 } 196 } 197 198 // Check if direct buffers should be page aligned 199 s = (String)props.remove("sun.nio.PageAlignDirectMemory"); |