WhiteBox.java revision 2134:114b709abea1
1279377Simp/* 2279377Simp * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3279377Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4279377Simp * 5279377Simp * This code is free software; you can redistribute it and/or modify it 6279377Simp * under the terms of the GNU General Public License version 2 only, as 7279377Simp * published by the Free Software Foundation. 8279377Simp * 9279377Simp * This code is distributed in the hope that it will be useful, but WITHOUT 10279377Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11279377Simp * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12279377Simp * version 2 for more details (a copy is included in the LICENSE file that 13279377Simp * accompanied this code). 14279377Simp * 15279377Simp * You should have received a copy of the GNU General Public License version 16279377Simp * 2 along with this work; if not, write to the Free Software Foundation, 17279377Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18279377Simp * 19279377Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20279377Simp * or visit www.oracle.com if you need additional information or have any 21279377Simp * questions. 22279377Simp * 23279377Simp */ 24279377Simp 25279377Simppackage sun.hotspot; 26279377Simp 27279377Simpimport java.lang.management.MemoryUsage; 28279377Simpimport java.lang.reflect.Executable; 29279377Simpimport java.util.Arrays; 30279377Simpimport java.util.List; 31279377Simpimport java.util.function.BiFunction; 32279377Simpimport java.util.function.Function; 33279377Simpimport java.security.BasicPermission; 34279377Simpimport java.util.Objects; 35279377Simp 36279377Simpimport sun.hotspot.parser.DiagnosticCommand; 37279377Simp 38279377Simppublic class WhiteBox { 39279377Simp @SuppressWarnings("serial") 40279377Simp public static class WhiteBoxPermission extends BasicPermission { 41279377Simp public WhiteBoxPermission(String s) { 42279377Simp super(s); 43279377Simp } 44279377Simp } 45279377Simp 46279377Simp private WhiteBox() {} 47279377Simp private static final WhiteBox instance = new WhiteBox(); 48279377Simp private static native void registerNatives(); 49279377Simp 50279377Simp /** 51279377Simp * Returns the singleton WhiteBox instance. 52279377Simp * 53279377Simp * The returned WhiteBox object should be carefully guarded 54279377Simp * by the caller, since it can be used to read and write data 55279377Simp * at arbitrary memory addresses. It must never be passed to 56279377Simp * untrusted code. 57279377Simp */ 58279377Simp public synchronized static WhiteBox getWhiteBox() { 59279377Simp SecurityManager sm = System.getSecurityManager(); 60279377Simp if (sm != null) { 61279377Simp sm.checkPermission(new WhiteBoxPermission("getInstance")); 62279377Simp } 63279377Simp return instance; 64279377Simp } 65279377Simp 66279377Simp static { 67279377Simp registerNatives(); 68279377Simp } 69279377Simp 70279377Simp // Get the maximum heap size supporting COOPs 71279377Simp public native long getCompressedOopsMaxHeapSize(); 72279377Simp // Arguments 73279377Simp public native void printHeapSizes(); 74279377Simp 75279377Simp // Memory 76279377Simp private native long getObjectAddress0(Object o); 77279377Simp public long getObjectAddress(Object o) { 78279377Simp Objects.requireNonNull(o); 79279377Simp return getObjectAddress0(o); 80279377Simp } 81279377Simp 82279377Simp public native int getHeapOopSize(); 83279377Simp public native int getVMPageSize(); 84279377Simp public native long getVMAllocationGranularity(); 85279377Simp public native long getVMLargePageSize(); 86279377Simp public native long getHeapSpaceAlignment(); 87279377Simp 88279377Simp private native boolean isObjectInOldGen0(Object o); 89279377Simp public boolean isObjectInOldGen(Object o) { 90279377Simp Objects.requireNonNull(o); 91279377Simp return isObjectInOldGen0(o); 92279377Simp } 93279377Simp 94279377Simp private native long getObjectSize0(Object o); 95279377Simp public long getObjectSize(Object o) { 96279377Simp Objects.requireNonNull(o); 97279377Simp return getObjectSize0(o); 98279377Simp } 99279377Simp 100279377Simp // Runtime 101279377Simp // Make sure class name is in the correct format 102279377Simp public boolean isClassAlive(String name) { 103279377Simp return isClassAlive0(name.replace('.', '/')); 104279377Simp } 105279377Simp private native boolean isClassAlive0(String name); 106279377Simp 107279377Simp private native boolean isMonitorInflated0(Object obj); 108279377Simp public boolean isMonitorInflated(Object obj) { 109279377Simp Objects.requireNonNull(obj); 110279377Simp return isMonitorInflated0(obj); 111279377Simp } 112279377Simp 113279377Simp public native void forceSafepoint(); 114279377Simp 115279377Simp private native long getConstantPool0(Class<?> aClass); 116279377Simp public long getConstantPool(Class<?> aClass) { 117279377Simp Objects.requireNonNull(aClass); 118295436Sandrew return getConstantPool0(aClass); 119279377Simp } 120279377Simp 121279377Simp private native int getConstantPoolCacheIndexTag0(); 122279377Simp public int getConstantPoolCacheIndexTag() { 123279377Simp return getConstantPoolCacheIndexTag0(); 124279377Simp } 125279377Simp 126279377Simp private native int getConstantPoolCacheLength0(Class<?> aClass); 127279377Simp public int getConstantPoolCacheLength(Class<?> aClass) { 128279377Simp Objects.requireNonNull(aClass); 129279377Simp return getConstantPoolCacheLength0(aClass); 130279377Simp } 131279377Simp 132279377Simp private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index); 133279377Simp public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) { 134279377Simp Objects.requireNonNull(aClass); 135279377Simp return remapInstructionOperandFromCPCache0(aClass, index); 136279377Simp } 137279377Simp 138279377Simp private native int encodeConstantPoolIndyIndex0(int index); 139279377Simp public int encodeConstantPoolIndyIndex(int index) { 140279377Simp return encodeConstantPoolIndyIndex0(index); 141279377Simp } 142279377Simp 143279377Simp // JVMTI 144279377Simp private native void addToBootstrapClassLoaderSearch0(String segment); 145279377Simp public void addToBootstrapClassLoaderSearch(String segment){ 146279377Simp Objects.requireNonNull(segment); 147279377Simp addToBootstrapClassLoaderSearch0(segment); 148279377Simp } 149279377Simp 150279377Simp private native void addToSystemClassLoaderSearch0(String segment); 151279377Simp public void addToSystemClassLoaderSearch(String segment) { 152279377Simp Objects.requireNonNull(segment); 153279377Simp addToSystemClassLoaderSearch0(segment); 154279377Simp } 155279377Simp 156279377Simp // G1 157279377Simp public native boolean g1InConcurrentMark(); 158279377Simp private native boolean g1IsHumongous0(Object o); 159279377Simp public boolean g1IsHumongous(Object o) { 160279377Simp Objects.requireNonNull(o); 161279377Simp return g1IsHumongous0(o); 162279377Simp } 163279377Simp 164279377Simp private native boolean g1BelongsToHumongousRegion0(long adr); 165279377Simp public boolean g1BelongsToHumongousRegion(long adr) { 166279377Simp if (adr == 0) { 167279377Simp throw new IllegalArgumentException("adr argument should not be null"); 168279377Simp } 169279377Simp return g1BelongsToHumongousRegion0(adr); 170279377Simp } 171279377Simp 172279377Simp 173295436Sandrew private native boolean g1BelongsToFreeRegion0(long adr); 174279377Simp public boolean g1BelongsToFreeRegion(long adr) { 175279377Simp if (adr == 0) { 176279377Simp throw new IllegalArgumentException("adr argument should not be null"); 177279377Simp } 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 /** 196 * Enumerates old regions with liveness less than specified and produces some statistics 197 * @param liveness percent of region's liveness (live_objects / total_region_size * 100). 198 * @return long[3] array where long[0] - total count of old regions 199 * long[1] - total memory of old regions 200 * long[2] - lowest estimation of total memory of old regions to be freed (non-full 201 * regions are not included) 202 */ 203 public native long[] g1GetMixedGCInfo(int liveness); 204 205 // NMT 206 public native long NMTMalloc(long size); 207 public native void NMTFree(long mem); 208 public native long NMTReserveMemory(long size); 209 public native void NMTCommitMemory(long addr, long size); 210 public native void NMTUncommitMemory(long addr, long size); 211 public native void NMTReleaseMemory(long addr, long size); 212 public native long NMTMallocWithPseudoStack(long size, int index); 213 public native boolean NMTChangeTrackingLevel(); 214 public native int NMTGetHashSize(); 215 216 // Compiler 217 public native int matchesMethod(Executable method, String pattern); 218 public native int matchesInline(Executable method, String pattern); 219 public native boolean shouldPrintAssembly(Executable method, int comp_level); 220 public native int deoptimizeFrames(boolean makeNotEntrant); 221 public native void deoptimizeAll(); 222 223 public boolean isMethodCompiled(Executable method) { 224 return isMethodCompiled(method, false /*not osr*/); 225 } 226 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 227 public boolean isMethodCompiled(Executable method, boolean isOsr){ 228 Objects.requireNonNull(method); 229 return isMethodCompiled0(method, isOsr); 230 } 231 public boolean isMethodCompilable(Executable method) { 232 return isMethodCompilable(method, -1 /*any*/); 233 } 234 public boolean isMethodCompilable(Executable method, int compLevel) { 235 return isMethodCompilable(method, compLevel, false /*not osr*/); 236 } 237 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 238 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 239 Objects.requireNonNull(method); 240 return isMethodCompilable0(method, compLevel, isOsr); 241 } 242 private native boolean isMethodQueuedForCompilation0(Executable method); 243 public boolean isMethodQueuedForCompilation(Executable method) { 244 Objects.requireNonNull(method); 245 return isMethodQueuedForCompilation0(method); 246 } 247 // Determine if the compiler corresponding to the compilation level 'compLevel' 248 // and to the compilation context 'compilation_context' provides an intrinsic 249 // for the method 'method'. An intrinsic is available for method 'method' if: 250 // - the intrinsic is enabled (by using the appropriate command-line flag) and 251 // - the platform on which the VM is running provides the instructions necessary 252 // for the compiler to generate the intrinsic code. 253 // 254 // The compilation context is related to using the DisableIntrinsic flag on a 255 // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp 256 // for more details. 257 public boolean isIntrinsicAvailable(Executable method, 258 Executable compilationContext, 259 int compLevel) { 260 Objects.requireNonNull(method); 261 return isIntrinsicAvailable0(method, compilationContext, compLevel); 262 } 263 // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored), 264 // use the below method that does not require the compilation context as argument. 265 public boolean isIntrinsicAvailable(Executable method, int compLevel) { 266 return isIntrinsicAvailable(method, null, compLevel); 267 } 268 private native boolean isIntrinsicAvailable0(Executable method, 269 Executable compilationContext, 270 int compLevel); 271 public int deoptimizeMethod(Executable method) { 272 return deoptimizeMethod(method, false /*not osr*/); 273 } 274 private native int deoptimizeMethod0(Executable method, boolean isOsr); 275 public int deoptimizeMethod(Executable method, boolean isOsr) { 276 Objects.requireNonNull(method); 277 return deoptimizeMethod0(method, isOsr); 278 } 279 public void makeMethodNotCompilable(Executable method) { 280 makeMethodNotCompilable(method, -1 /*any*/); 281 } 282 public void makeMethodNotCompilable(Executable method, int compLevel) { 283 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 284 } 285 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 286 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 287 Objects.requireNonNull(method); 288 makeMethodNotCompilable0(method, compLevel, isOsr); 289 } 290 public int getMethodCompilationLevel(Executable method) { 291 return getMethodCompilationLevel(method, false /*not ost*/); 292 } 293 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 294 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 295 Objects.requireNonNull(method); 296 return getMethodCompilationLevel0(method, isOsr); 297 } 298 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 299 public boolean testSetDontInlineMethod(Executable method, boolean value) { 300 Objects.requireNonNull(method); 301 return testSetDontInlineMethod0(method, value); 302 } 303 public int getCompileQueuesSize() { 304 return getCompileQueueSize(-1 /*any*/); 305 } 306 public native int getCompileQueueSize(int compLevel); 307 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 308 public boolean testSetForceInlineMethod(Executable method, boolean value) { 309 Objects.requireNonNull(method); 310 return testSetForceInlineMethod0(method, value); 311 } 312 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 313 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 314 } 315 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 316 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 317 Objects.requireNonNull(method); 318 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 319 } 320 private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel); 321 public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) { 322 Objects.requireNonNull(aClass); 323 return enqueueInitializerForCompilation0(aClass, compLevel); 324 } 325 private native void clearMethodState0(Executable method); 326 public void clearMethodState(Executable method) { 327 Objects.requireNonNull(method); 328 clearMethodState0(method); 329 } 330 public native void lockCompilation(); 331 public native void unlockCompilation(); 332 private native int getMethodEntryBci0(Executable method); 333 public int getMethodEntryBci(Executable method) { 334 Objects.requireNonNull(method); 335 return getMethodEntryBci0(method); 336 } 337 private native Object[] getNMethod0(Executable method, boolean isOsr); 338 public Object[] getNMethod(Executable method, boolean isOsr) { 339 Objects.requireNonNull(method); 340 return getNMethod0(method, isOsr); 341 } 342 public native long allocateCodeBlob(int size, int type); 343 public long allocateCodeBlob(long size, int type) { 344 int intSize = (int) size; 345 if ((long) intSize != size || size < 0) { 346 throw new IllegalArgumentException( 347 "size argument has illegal value " + size); 348 } 349 return allocateCodeBlob( intSize, type); 350 } 351 public native void freeCodeBlob(long addr); 352 public native void forceNMethodSweep(); 353 public native Object[] getCodeHeapEntries(int type); 354 public native int getCompilationActivityMode(); 355 private native long getMethodData0(Executable method); 356 public long getMethodData(Executable method) { 357 Objects.requireNonNull(method); 358 return getMethodData0(method); 359 } 360 public native Object[] getCodeBlob(long addr); 361 362 private native void clearInlineCaches0(boolean preserve_static_stubs); 363 public void clearInlineCaches() { 364 clearInlineCaches0(false); 365 } 366 public void clearInlineCaches(boolean preserve_static_stubs) { 367 clearInlineCaches0(preserve_static_stubs); 368 } 369 370 // Intered strings 371 public native boolean isInStringTable(String str); 372 373 // Memory 374 public native void readReservedMemory(); 375 public native long allocateMetaspace(ClassLoader classLoader, long size); 376 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 377 public native long incMetaspaceCapacityUntilGC(long increment); 378 public native long metaspaceCapacityUntilGC(); 379 public native boolean metaspaceShouldConcurrentCollect(); 380 381 // Force Young GC 382 public native void youngGC(); 383 384 // Force Full GC 385 public native void fullGC(); 386 387 // Method tries to start concurrent mark cycle. 388 // It returns false if CM Thread is always in concurrent cycle. 389 public native boolean g1StartConcMarkCycle(); 390 391 // Tests on ReservedSpace/VirtualSpace classes 392 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 393 public native void runMemoryUnitTests(); 394 public native void readFromNoaccessArea(); 395 public native long getThreadStackSize(); 396 public native long getThreadRemainingStackSize(); 397 398 // CPU features 399 public native String getCPUFeatures(); 400 401 // Native extensions 402 public native long getHeapUsageForContext(int context); 403 public native long getHeapRegionCountForContext(int context); 404 private native int getContextForObject0(Object obj); 405 public int getContextForObject(Object obj) { 406 Objects.requireNonNull(obj); 407 return getContextForObject0(obj); 408 } 409 public native void printRegionInfo(int context); 410 411 // VM flags 412 public native boolean isConstantVMFlag(String name); 413 public native boolean isLockedVMFlag(String name); 414 public native void setBooleanVMFlag(String name, boolean value); 415 public native void setIntVMFlag(String name, long value); 416 public native void setUintVMFlag(String name, long value); 417 public native void setIntxVMFlag(String name, long value); 418 public native void setUintxVMFlag(String name, long value); 419 public native void setUint64VMFlag(String name, long value); 420 public native void setSizeTVMFlag(String name, long value); 421 public native void setStringVMFlag(String name, String value); 422 public native void setDoubleVMFlag(String name, double value); 423 public native Boolean getBooleanVMFlag(String name); 424 public native Long getIntVMFlag(String name); 425 public native Long getUintVMFlag(String name); 426 public native Long getIntxVMFlag(String name); 427 public native Long getUintxVMFlag(String name); 428 public native Long getUint64VMFlag(String name); 429 public native Long getSizeTVMFlag(String name); 430 public native String getStringVMFlag(String name); 431 public native Double getDoubleVMFlag(String name); 432 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 433 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 434 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 435 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 436 437 public Object getVMFlag(String name) { 438 return flagsGetters.stream() 439 .map(f -> f.apply(name)) 440 .filter(x -> x != null) 441 .findAny() 442 .orElse(null); 443 } 444 445 // Jigsaw 446 public native void DefineModule(Object module, String version, String location, 447 Object[] packages); 448 public native void AddModuleExports(Object from_module, String pkg, Object to_module); 449 public native void AddReadsModule(Object from_module, Object source_module); 450 public native boolean CanReadModule(Object asking_module, Object source_module); 451 public native boolean IsExportedToModule(Object from_module, String pkg, Object to_module); 452 public native void AddModulePackage(Object module, String pkg); 453 public native void AddModuleExportsToAllUnnamed(Object module, String pkg); 454 public native void AddModuleExportsToAll(Object module, String pkg); 455 public native Object GetModuleByPackageName(Object ldr, String pkg); 456 457 public native int getOffsetForName0(String name); 458 public int getOffsetForName(String name) throws Exception { 459 int offset = getOffsetForName0(name); 460 if (offset == -1) { 461 throw new RuntimeException(name + " not found"); 462 } 463 return offset; 464 } 465 public native Boolean getMethodBooleanOption(Executable method, String name); 466 public native Long getMethodIntxOption(Executable method, String name); 467 public native Long getMethodUintxOption(Executable method, String name); 468 public native Double getMethodDoubleOption(Executable method, String name); 469 public native String getMethodStringOption(Executable method, String name); 470 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 471 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 472 this::getMethodUintxOption, this::getMethodDoubleOption, 473 this::getMethodStringOption); 474 475 public Object getMethodOption(Executable method, String name) { 476 return methodOptionGetters.stream() 477 .map(f -> f.apply(method, name)) 478 .filter(x -> x != null) 479 .findAny() 480 .orElse(null); 481 } 482 483 // Safepoint Checking 484 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 485 486 // Sharing 487 public native boolean isShared(Object o); 488 public native boolean isSharedClass(Class<?> c); 489 public native boolean areSharedStringsIgnored(); 490 491 // Compiler Directive 492 public native int addCompilerDirective(String compDirect); 493 public native void removeCompilerDirective(int count); 494} 495