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