1 /* 2 * Copyright (c) 1996, 2013, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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 sun.misc; 27 28 import static java.lang.Thread.State.*; 29 import java.util.Properties; 30 import java.util.HashMap; 31 import java.util.Map; 32 import java.util.Set; 33 34 public class VM { 35 36 /* The following methods used to be native methods that instruct 37 * the VM to selectively suspend certain threads in low-memory 38 * situations. They are inherently dangerous and not implementable 39 * on native threads. We removed them in JDK 1.2. The skeletons 40 * remain so that existing applications that use these methods 41 * will still work. 42 */ 43 private static boolean suspended = false; 44 45 /** @deprecated */ 46 @Deprecated 47 public static boolean threadsSuspended() { 48 return suspended; 49 } 50 51 @SuppressWarnings("deprecation") 52 public static boolean allowThreadSuspension(ThreadGroup g, boolean b) { 53 return g.allowThreadSuspension(b); 54 } 55 56 /** @deprecated */ 57 @Deprecated 58 public static boolean suspendThreads() { 59 suspended = true; 60 return true; 61 } 62 63 // Causes any suspended threadgroups to be resumed. 64 /** @deprecated */ 65 @Deprecated 66 public static void unsuspendThreads() { 67 suspended = false; 68 } 69 70 // Causes threadgroups no longer marked suspendable to be resumed. 71 /** @deprecated */ 72 @Deprecated 73 public static void unsuspendSomeThreads() { 74 } 75 76 /* Deprecated fields and methods -- Memory advice not supported in 1.2 */ 77 78 /** @deprecated */ 79 @Deprecated 80 public static final int STATE_GREEN = 1; 81 82 /** @deprecated */ 83 @Deprecated 84 public static final int STATE_YELLOW = 2; 85 86 /** @deprecated */ 87 @Deprecated 88 public static final int STATE_RED = 3; 89 90 /** @deprecated */ 91 @Deprecated 92 public static final int getState() { 93 return STATE_GREEN; 94 } 95 96 /** @deprecated */ 97 @Deprecated 98 public static void registerVMNotification(VMNotification n) { } 99 100 /** @deprecated */ 101 @Deprecated 102 public static void asChange(int as_old, int as_new) { } 103 104 /** @deprecated */ 105 @Deprecated 106 public static void asChange_otherthread(int as_old, int as_new) { } 107 108 /* 109 * Not supported in 1.2 because these will have to be exported as 110 * JVM functions, and we are not sure we want do that. Leaving 111 * here so it can be easily resurrected -- just remove the // 112 * comments. 113 */ 114 115 /** 116 * Resume Java profiling. All profiling data is added to any 117 * earlier profiling, unless <code>resetJavaProfiler</code> is 118 * called in between. If profiling was not started from the 119 * command line, <code>resumeJavaProfiler</code> will start it. 120 * <p> 121 * 122 * NOTE: Profiling must be enabled from the command line for a 123 * java.prof report to be automatically generated on exit; if not, 124 * writeJavaProfilerReport must be invoked to write a report. 125 * 126 * @see resetJavaProfiler 127 * @see writeJavaProfilerReport 128 */ 129 130 // public native static void resumeJavaProfiler(); 131 132 /** 133 * Suspend Java profiling. 134 */ 135 // public native static void suspendJavaProfiler(); 136 137 /** 138 * Initialize Java profiling. Any accumulated profiling 139 * information is discarded. 140 */ 141 // public native static void resetJavaProfiler(); 142 143 /** 144 * Write the current profiling contents to the file "java.prof". 145 * If the file already exists, it will be overwritten. 146 */ 147 // public native static void writeJavaProfilerReport(); 148 149 150 private static volatile boolean booted = false; 151 private static final Object lock = new Object(); 152 153 // Invoked by by System.initializeSystemClass just before returning. 154 // Subsystems that are invoked during initialization can check this 155 // property in order to avoid doing things that should wait until the 156 // application class loader has been set up. 157 // 158 public static void booted() { 159 synchronized (lock) { 160 booted = true; 161 lock.notifyAll(); 162 } 163 } 164 165 public static boolean isBooted() { 166 return booted; 167 } 168 169 // Waits until VM completes initialization 170 // 171 // This method is invoked by the Finalizer thread 172 public static void awaitBooted() throws InterruptedException { 173 synchronized (lock) { 174 while (!booted) { 175 lock.wait(); 176 } 177 } 178 } 179 180 // A user-settable upper limit on the maximum amount of allocatable direct 181 // buffer memory. This value may be changed during VM initialization if 182 // "java" is launched with "-XX:MaxDirectMemorySize=<size>". 183 // 184 // The initial value of this field is arbitrary; during JRE initialization 185 // it will be reset to the value specified on the command line, if any, 186 // otherwise to Runtime.getRuntime().maxMemory(). 187 // 188 private static long directMemory = 64 * 1024 * 1024; 189 190 // Returns the maximum amount of allocatable direct buffer memory. 191 // The directMemory variable is initialized during system initialization 192 // in the saveVMSystemProperties method. 193 // 194 public static long maxDirectMemory() { 195 return directMemory; 196 } 197 198 // User-controllable flag that determines if direct buffers should be page 199 // aligned. The "-XX:+PageAlignDirectMemory" option can be used to force 200 // buffers, allocated by ByteBuffer.allocateDirect, to be page aligned. 201 private static boolean pageAlignDirectMemory; 202 203 // Returns {@code true} if the direct buffers should be page aligned. This 204 // variable is initialized by saveVMSystemProperties. 205 public static boolean isDirectMemoryPageAligned() { 206 return pageAlignDirectMemory; 207 } 208 209 /** 210 * Returns true if the given class loader is in the system domain 211 * in which all permissions are granted. 212 */ 213 public static boolean isSystemDomainLoader(ClassLoader loader) { 214 return loader == null; 215 } 216 217 /** 218 * Returns the system property of the specified key saved at 219 * system initialization time. This method should only be used 220 * for the system properties that are not changed during runtime. 221 * It accesses a private copy of the system properties so 222 * that user's locking of the system properties object will not 223 * cause the library to deadlock. 224 */ 225 public static String getSavedProperty(String key) { 226 if (savedProps.isEmpty()) 227 throw new IllegalStateException("Should be non-empty if initialized"); 228 229 return savedProps.getProperty(key); 230 } 231 232 // TODO: the Property Management needs to be refactored and 233 // the appropriate prop keys need to be accessible to the 234 // calling classes to avoid duplication of keys. 235 private static final Properties savedProps = new Properties(); 236 237 /** 238 * Save a private copy of the system properties. 239 * 240 * @implNote This method can only be invoked during system initialization. 241 */ 242 public static void saveVMSystemProperties(Properties props) { 243 if (booted) 244 throw new IllegalStateException("System initialization has completed"); 245 246 if (!savedProps.isEmpty()) 247 throw new IllegalStateException("Properties already initialized"); 248 249 savedProps.putAll(props); 250 251 // Set the maximum amount of direct memory. This value is controlled 252 // by the vm option -XX:MaxDirectMemorySize=<size>. 253 // The maximum amount of allocatable direct buffer memory (in bytes) 254 // from the system property sun.nio.MaxDirectMemorySize set by the VM. 255 // The system property will be removed. 256 String s = savedProps.getProperty("sun.nio.MaxDirectMemorySize"); 257 if (s != null) { 258 if (s.equals("-1")) { 259 // -XX:MaxDirectMemorySize not given, take default 260 directMemory = Runtime.getRuntime().maxMemory(); 261 } else { 262 long l = Long.parseLong(s); 263 if (l > -1) 264 directMemory = l; 265 } 266 } 267 268 // Check if direct buffers should be page aligned 269 s = savedProps.getProperty("sun.nio.PageAlignDirectMemory"); 270 pageAlignDirectMemory = Boolean.parseBoolean(s); 271 272 } 273 274 /** 275 * Returns a copy of the saved system properties with private implementation 276 * properties removed. 277 * 278 * @return a newly created Properties instance containing only sanitized 279 */ 280 public static Properties getSanitizedSavedProperties() { 281 Properties sanitized = new Properties(); 282 283 sanitized.putAll(savedProps); 284 285 // Remove implementation private system properties 286 287 sanitized.remove("sun.nio.MaxDirectMemorySize"); 288 289 sanitized.remove("sun.nio.PageAlignDirectMemory"); 290 291 // used by java.lang.Integer.IntegerCache 292 sanitized.remove("java.lang.Integer.IntegerCache.high"); 293 294 // used by java.util.zip.ZipFile 295 sanitized.remove("sun.zip.disableMemoryMapping"); 296 297 // used by sun.launcher.LauncherHelper 298 sanitized.remove("sun.java.launcher.diag"); 299 300 return sanitized; 301 } 302 303 // Initialize any miscellenous operating system settings that need to be 304 // set for the class libraries. 305 // 306 public static void initializeOSEnvironment() { 307 if (!booted) { 308 OSEnvironment.initialize(); 309 } 310 } 311 312 /* Current count of objects pending for finalization */ 313 private static volatile int finalRefCount = 0; 314 315 /* Peak count of objects pending for finalization */ 316 private static volatile int peakFinalRefCount = 0; 317 318 /* 319 * Gets the number of objects pending for finalization. 320 * 321 * @return the number of objects pending for finalization. 322 */ 323 public static int getFinalRefCount() { 324 return finalRefCount; 325 } 326 327 /* 328 * Gets the peak number of objects pending for finalization. 329 * 330 * @return the peak number of objects pending for finalization. 331 */ 332 public static int getPeakFinalRefCount() { 333 return peakFinalRefCount; 334 } 335 336 /* 337 * Add <tt>n</tt> to the objects pending for finalization count. 338 * 339 * @param n an integer value to be added to the objects pending 340 * for finalization count 341 */ 342 public static void addFinalRefCount(int n) { 343 // The caller must hold lock to synchronize the update. 344 345 finalRefCount += n; 346 if (finalRefCount > peakFinalRefCount) { 347 peakFinalRefCount = finalRefCount; 348 } 349 } 350 351 /** 352 * Returns Thread.State for the given threadStatus 353 */ 354 public static Thread.State toThreadState(int threadStatus) { 355 if ((threadStatus & JVMTI_THREAD_STATE_RUNNABLE) != 0) { 356 return RUNNABLE; 357 } else if ((threadStatus & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) { 358 return BLOCKED; 359 } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_INDEFINITELY) != 0) { 360 return WAITING; 361 } else if ((threadStatus & JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT) != 0) { 362 return TIMED_WAITING; 363 } else if ((threadStatus & JVMTI_THREAD_STATE_TERMINATED) != 0) { 364 return TERMINATED; 365 } else if ((threadStatus & JVMTI_THREAD_STATE_ALIVE) == 0) { 366 return NEW; 367 } else { 368 return RUNNABLE; 369 } 370 } 371 372 /* The threadStatus field is set by the VM at state transition 373 * in the hotspot implementation. Its value is set according to 374 * the JVM TI specification GetThreadState function. 375 */ 376 private final static int JVMTI_THREAD_STATE_ALIVE = 0x0001; 377 private final static int JVMTI_THREAD_STATE_TERMINATED = 0x0002; 378 private final static int JVMTI_THREAD_STATE_RUNNABLE = 0x0004; 379 private final static int JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER = 0x0400; 380 private final static int JVMTI_THREAD_STATE_WAITING_INDEFINITELY = 0x0010; 381 private final static int JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT = 0x0020; 382 383 /* 384 * Returns the first non-null class loader up the execution stack, 385 * or null if only code from the null class loader is on the stack. 386 */ 387 public static native ClassLoader latestUserDefinedLoader(); 388 389 /** 390 * Returns {@code true} if we are in a set UID program. 391 */ 392 public static native boolean isSetUID(); 393 394 static { 395 initialize(); 396 } 397 private native static void initialize(); 398 }