WhiteBox.java revision 2208:e85afddde2fd
1/* 2 * Copyright (c) 2012, 2016, 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 25package sun.hotspot; 26 27import java.lang.management.MemoryUsage; 28import java.lang.reflect.Executable; 29import java.util.Arrays; 30import java.util.List; 31import java.util.function.BiFunction; 32import java.util.function.Function; 33import java.security.BasicPermission; 34import java.util.Objects; 35 36import sun.hotspot.parser.DiagnosticCommand; 37 38public class WhiteBox { 39 @SuppressWarnings("serial") 40 public static class WhiteBoxPermission extends BasicPermission { 41 public WhiteBoxPermission(String s) { 42 super(s); 43 } 44 } 45 46 private WhiteBox() {} 47 private static final WhiteBox instance = new WhiteBox(); 48 private static native void registerNatives(); 49 50 /** 51 * Returns the singleton WhiteBox instance. 52 * 53 * The returned WhiteBox object should be carefully guarded 54 * by the caller, since it can be used to read and write data 55 * at arbitrary memory addresses. It must never be passed to 56 * untrusted code. 57 */ 58 public synchronized static WhiteBox getWhiteBox() { 59 SecurityManager sm = System.getSecurityManager(); 60 if (sm != null) { 61 sm.checkPermission(new WhiteBoxPermission("getInstance")); 62 } 63 return instance; 64 } 65 66 static { 67 registerNatives(); 68 } 69 70 // Get the maximum heap size supporting COOPs 71 public native long getCompressedOopsMaxHeapSize(); 72 // Arguments 73 public native void printHeapSizes(); 74 75 // Memory 76 private native long getObjectAddress0(Object o); 77 public long getObjectAddress(Object o) { 78 Objects.requireNonNull(o); 79 return getObjectAddress0(o); 80 } 81 82 public native int getHeapOopSize(); 83 public native int getVMPageSize(); 84 public native long getVMAllocationGranularity(); 85 public native long getVMLargePageSize(); 86 public native long getHeapSpaceAlignment(); 87 public native long getHeapAlignment(); 88 89 private native boolean isObjectInOldGen0(Object o); 90 public boolean isObjectInOldGen(Object o) { 91 Objects.requireNonNull(o); 92 return isObjectInOldGen0(o); 93 } 94 95 private native long getObjectSize0(Object o); 96 public long getObjectSize(Object o) { 97 Objects.requireNonNull(o); 98 return getObjectSize0(o); 99 } 100 101 // Runtime 102 // Make sure class name is in the correct format 103 public boolean isClassAlive(String name) { 104 return isClassAlive0(name.replace('.', '/')); 105 } 106 private native boolean isClassAlive0(String name); 107 108 private native boolean isMonitorInflated0(Object obj); 109 public boolean isMonitorInflated(Object obj) { 110 Objects.requireNonNull(obj); 111 return isMonitorInflated0(obj); 112 } 113 114 public native void forceSafepoint(); 115 116 private native long getConstantPool0(Class<?> aClass); 117 public long getConstantPool(Class<?> aClass) { 118 Objects.requireNonNull(aClass); 119 return getConstantPool0(aClass); 120 } 121 122 private native int getConstantPoolCacheIndexTag0(); 123 public int getConstantPoolCacheIndexTag() { 124 return getConstantPoolCacheIndexTag0(); 125 } 126 127 private native int getConstantPoolCacheLength0(Class<?> aClass); 128 public int getConstantPoolCacheLength(Class<?> aClass) { 129 Objects.requireNonNull(aClass); 130 return getConstantPoolCacheLength0(aClass); 131 } 132 133 private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index); 134 public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) { 135 Objects.requireNonNull(aClass); 136 return remapInstructionOperandFromCPCache0(aClass, index); 137 } 138 139 private native int encodeConstantPoolIndyIndex0(int index); 140 public int encodeConstantPoolIndyIndex(int index) { 141 return encodeConstantPoolIndyIndex0(index); 142 } 143 144 // JVMTI 145 private native void addToBootstrapClassLoaderSearch0(String segment); 146 public void addToBootstrapClassLoaderSearch(String segment){ 147 Objects.requireNonNull(segment); 148 addToBootstrapClassLoaderSearch0(segment); 149 } 150 151 private native void addToSystemClassLoaderSearch0(String segment); 152 public void addToSystemClassLoaderSearch(String segment) { 153 Objects.requireNonNull(segment); 154 addToSystemClassLoaderSearch0(segment); 155 } 156 157 // G1 158 public native boolean g1InConcurrentMark(); 159 private native boolean g1IsHumongous0(Object o); 160 public boolean g1IsHumongous(Object o) { 161 Objects.requireNonNull(o); 162 return g1IsHumongous0(o); 163 } 164 165 private native boolean g1BelongsToHumongousRegion0(long adr); 166 public boolean g1BelongsToHumongousRegion(long adr) { 167 if (adr == 0) { 168 throw new IllegalArgumentException("adr argument should not be null"); 169 } 170 return g1BelongsToHumongousRegion0(adr); 171 } 172 173 174 private native boolean g1BelongsToFreeRegion0(long adr); 175 public boolean g1BelongsToFreeRegion(long adr) { 176 if (adr == 0) { 177 throw new IllegalArgumentException("adr argument should not be null"); 178 } 179 return g1BelongsToFreeRegion0(adr); 180 } 181 182 public native long g1NumMaxRegions(); 183 public native long g1NumFreeRegions(); 184 public native int g1RegionSize(); 185 public native MemoryUsage g1AuxiliaryMemoryUsage(); 186 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 187 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 188 Objects.requireNonNull(args); 189 return parseCommandLine0(commandline, delim, args); 190 } 191 192 // Parallel GC 193 public native long psVirtualSpaceAlignment(); 194 public native long psHeapGenerationAlignment(); 195 196 /** 197 * Enumerates old regions with liveness less than specified and produces some statistics 198 * @param liveness percent of region's liveness (live_objects / total_region_size * 100). 199 * @return long[3] array where long[0] - total count of old regions 200 * long[1] - total memory of old regions 201 * long[2] - lowest estimation of total memory of old regions to be freed (non-full 202 * regions are not included) 203 */ 204 public native long[] g1GetMixedGCInfo(int liveness); 205 206 // NMT 207 public native long NMTMalloc(long size); 208 public native void NMTFree(long mem); 209 public native long NMTReserveMemory(long size); 210 public native void NMTCommitMemory(long addr, long size); 211 public native void NMTUncommitMemory(long addr, long size); 212 public native void NMTReleaseMemory(long addr, long size); 213 public native long NMTMallocWithPseudoStack(long size, int index); 214 public native boolean NMTChangeTrackingLevel(); 215 public native int NMTGetHashSize(); 216 217 // Compiler 218 public native int matchesMethod(Executable method, String pattern); 219 public native int matchesInline(Executable method, String pattern); 220 public native boolean shouldPrintAssembly(Executable method, int comp_level); 221 public native int deoptimizeFrames(boolean makeNotEntrant); 222 public native void deoptimizeAll(); 223 224 public boolean isMethodCompiled(Executable method) { 225 return isMethodCompiled(method, false /*not osr*/); 226 } 227 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 228 public boolean isMethodCompiled(Executable method, boolean isOsr){ 229 Objects.requireNonNull(method); 230 return isMethodCompiled0(method, isOsr); 231 } 232 public boolean isMethodCompilable(Executable method) { 233 return isMethodCompilable(method, -1 /*any*/); 234 } 235 public boolean isMethodCompilable(Executable method, int compLevel) { 236 return isMethodCompilable(method, compLevel, false /*not osr*/); 237 } 238 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 239 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 240 Objects.requireNonNull(method); 241 return isMethodCompilable0(method, compLevel, isOsr); 242 } 243 private native boolean isMethodQueuedForCompilation0(Executable method); 244 public boolean isMethodQueuedForCompilation(Executable method) { 245 Objects.requireNonNull(method); 246 return isMethodQueuedForCompilation0(method); 247 } 248 // Determine if the compiler corresponding to the compilation level 'compLevel' 249 // and to the compilation context 'compilation_context' provides an intrinsic 250 // for the method 'method'. An intrinsic is available for method 'method' if: 251 // - the intrinsic is enabled (by using the appropriate command-line flag) and 252 // - the platform on which the VM is running provides the instructions necessary 253 // for the compiler to generate the intrinsic code. 254 // 255 // The compilation context is related to using the DisableIntrinsic flag on a 256 // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp 257 // for more details. 258 public boolean isIntrinsicAvailable(Executable method, 259 Executable compilationContext, 260 int compLevel) { 261 Objects.requireNonNull(method); 262 return isIntrinsicAvailable0(method, compilationContext, compLevel); 263 } 264 // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored), 265 // use the below method that does not require the compilation context as argument. 266 public boolean isIntrinsicAvailable(Executable method, int compLevel) { 267 return isIntrinsicAvailable(method, null, compLevel); 268 } 269 private native boolean isIntrinsicAvailable0(Executable method, 270 Executable compilationContext, 271 int compLevel); 272 public int deoptimizeMethod(Executable method) { 273 return deoptimizeMethod(method, false /*not osr*/); 274 } 275 private native int deoptimizeMethod0(Executable method, boolean isOsr); 276 public int deoptimizeMethod(Executable method, boolean isOsr) { 277 Objects.requireNonNull(method); 278 return deoptimizeMethod0(method, isOsr); 279 } 280 public void makeMethodNotCompilable(Executable method) { 281 makeMethodNotCompilable(method, -1 /*any*/); 282 } 283 public void makeMethodNotCompilable(Executable method, int compLevel) { 284 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 285 } 286 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 287 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 288 Objects.requireNonNull(method); 289 makeMethodNotCompilable0(method, compLevel, isOsr); 290 } 291 public int getMethodCompilationLevel(Executable method) { 292 return getMethodCompilationLevel(method, false /*not ost*/); 293 } 294 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 295 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 296 Objects.requireNonNull(method); 297 return getMethodCompilationLevel0(method, isOsr); 298 } 299 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 300 public boolean testSetDontInlineMethod(Executable method, boolean value) { 301 Objects.requireNonNull(method); 302 return testSetDontInlineMethod0(method, value); 303 } 304 public int getCompileQueuesSize() { 305 return getCompileQueueSize(-1 /*any*/); 306 } 307 public native int getCompileQueueSize(int compLevel); 308 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 309 public boolean testSetForceInlineMethod(Executable method, boolean value) { 310 Objects.requireNonNull(method); 311 return testSetForceInlineMethod0(method, value); 312 } 313 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 314 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 315 } 316 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 317 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 318 Objects.requireNonNull(method); 319 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 320 } 321 private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel); 322 public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) { 323 Objects.requireNonNull(aClass); 324 return enqueueInitializerForCompilation0(aClass, compLevel); 325 } 326 private native void clearMethodState0(Executable method); 327 public void clearMethodState(Executable method) { 328 Objects.requireNonNull(method); 329 clearMethodState0(method); 330 } 331 public native void lockCompilation(); 332 public native void unlockCompilation(); 333 private native int getMethodEntryBci0(Executable method); 334 public int getMethodEntryBci(Executable method) { 335 Objects.requireNonNull(method); 336 return getMethodEntryBci0(method); 337 } 338 private native Object[] getNMethod0(Executable method, boolean isOsr); 339 public Object[] getNMethod(Executable method, boolean isOsr) { 340 Objects.requireNonNull(method); 341 return getNMethod0(method, isOsr); 342 } 343 public native long allocateCodeBlob(int size, int type); 344 public long allocateCodeBlob(long size, int type) { 345 int intSize = (int) size; 346 if ((long) intSize != size || size < 0) { 347 throw new IllegalArgumentException( 348 "size argument has illegal value " + size); 349 } 350 return allocateCodeBlob( intSize, type); 351 } 352 public native void freeCodeBlob(long addr); 353 public native void forceNMethodSweep(); 354 public native Object[] getCodeHeapEntries(int type); 355 public native int getCompilationActivityMode(); 356 private native long getMethodData0(Executable method); 357 public long getMethodData(Executable method) { 358 Objects.requireNonNull(method); 359 return getMethodData0(method); 360 } 361 public native Object[] getCodeBlob(long addr); 362 363 private native void clearInlineCaches0(boolean preserve_static_stubs); 364 public void clearInlineCaches() { 365 clearInlineCaches0(false); 366 } 367 public void clearInlineCaches(boolean preserve_static_stubs) { 368 clearInlineCaches0(preserve_static_stubs); 369 } 370 371 // Intered strings 372 public native boolean isInStringTable(String str); 373 374 // Memory 375 public native void readReservedMemory(); 376 public native long allocateMetaspace(ClassLoader classLoader, long size); 377 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 378 public native long incMetaspaceCapacityUntilGC(long increment); 379 public native long metaspaceCapacityUntilGC(); 380 public native boolean metaspaceShouldConcurrentCollect(); 381 382 // Don't use these methods directly 383 // Use sun.hotspot.gc.GC class instead. 384 public native int currentGC(); 385 public native int allSupportedGC(); 386 public native boolean gcSelectedByErgo(); 387 388 // Force Young GC 389 public native void youngGC(); 390 391 // Force Full GC 392 public native void fullGC(); 393 394 // Method tries to start concurrent mark cycle. 395 // It returns false if CM Thread is always in concurrent cycle. 396 public native boolean g1StartConcMarkCycle(); 397 398 // Tests on ReservedSpace/VirtualSpace classes 399 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 400 public native void runMemoryUnitTests(); 401 public native void readFromNoaccessArea(); 402 public native long getThreadStackSize(); 403 public native long getThreadRemainingStackSize(); 404 405 // CPU features 406 public native String getCPUFeatures(); 407 408 // Native extensions 409 public native long getHeapUsageForContext(int context); 410 public native long getHeapRegionCountForContext(int context); 411 private native int getContextForObject0(Object obj); 412 public int getContextForObject(Object obj) { 413 Objects.requireNonNull(obj); 414 return getContextForObject0(obj); 415 } 416 public native void printRegionInfo(int context); 417 418 // VM flags 419 public native boolean isConstantVMFlag(String name); 420 public native boolean isLockedVMFlag(String name); 421 public native void setBooleanVMFlag(String name, boolean value); 422 public native void setIntVMFlag(String name, long value); 423 public native void setUintVMFlag(String name, long value); 424 public native void setIntxVMFlag(String name, long value); 425 public native void setUintxVMFlag(String name, long value); 426 public native void setUint64VMFlag(String name, long value); 427 public native void setSizeTVMFlag(String name, long value); 428 public native void setStringVMFlag(String name, String value); 429 public native void setDoubleVMFlag(String name, double value); 430 public native Boolean getBooleanVMFlag(String name); 431 public native Long getIntVMFlag(String name); 432 public native Long getUintVMFlag(String name); 433 public native Long getIntxVMFlag(String name); 434 public native Long getUintxVMFlag(String name); 435 public native Long getUint64VMFlag(String name); 436 public native Long getSizeTVMFlag(String name); 437 public native String getStringVMFlag(String name); 438 public native Double getDoubleVMFlag(String name); 439 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 440 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 441 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 442 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 443 444 public Object getVMFlag(String name) { 445 return flagsGetters.stream() 446 .map(f -> f.apply(name)) 447 .filter(x -> x != null) 448 .findAny() 449 .orElse(null); 450 } 451 452 // Jigsaw 453 public native void DefineModule(Object module, String version, String location, 454 Object[] packages); 455 public native void AddModuleExports(Object from_module, String pkg, Object to_module); 456 public native void AddReadsModule(Object from_module, Object source_module); 457 public native boolean CanReadModule(Object asking_module, Object source_module); 458 public native boolean IsExportedToModule(Object from_module, String pkg, Object to_module); 459 public native void AddModulePackage(Object module, String pkg); 460 public native void AddModuleExportsToAllUnnamed(Object module, String pkg); 461 public native void AddModuleExportsToAll(Object module, String pkg); 462 public native Object GetModuleByPackageName(Object ldr, String pkg); 463 464 public native int getOffsetForName0(String name); 465 public int getOffsetForName(String name) throws Exception { 466 int offset = getOffsetForName0(name); 467 if (offset == -1) { 468 throw new RuntimeException(name + " not found"); 469 } 470 return offset; 471 } 472 public native Boolean getMethodBooleanOption(Executable method, String name); 473 public native Long getMethodIntxOption(Executable method, String name); 474 public native Long getMethodUintxOption(Executable method, String name); 475 public native Double getMethodDoubleOption(Executable method, String name); 476 public native String getMethodStringOption(Executable method, String name); 477 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 478 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 479 this::getMethodUintxOption, this::getMethodDoubleOption, 480 this::getMethodStringOption); 481 482 public Object getMethodOption(Executable method, String name) { 483 return methodOptionGetters.stream() 484 .map(f -> f.apply(method, name)) 485 .filter(x -> x != null) 486 .findAny() 487 .orElse(null); 488 } 489 490 // Safepoint Checking 491 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 492 493 // Sharing 494 public native boolean isShared(Object o); 495 public native boolean isSharedClass(Class<?> c); 496 public native boolean areSharedStringsIgnored(); 497 498 // Compiler Directive 499 public native int addCompilerDirective(String compDirect); 500 public native void removeCompilerDirective(int count); 501} 502