WhiteBox.java revision 2725:cf738474cdb9
11573Srgrimes/* 21573Srgrimes * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. 31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41573Srgrimes * 51573Srgrimes * This code is free software; you can redistribute it and/or modify it 61573Srgrimes * under the terms of the GNU General Public License version 2 only, as 71573Srgrimes * published by the Free Software Foundation. 8227753Stheraven * 9227753Stheraven * This code is distributed in the hope that it will be useful, but WITHOUT 10227753Stheraven * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11227753Stheraven * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12227753Stheraven * version 2 for more details (a copy is included in the LICENSE file that 131573Srgrimes * accompanied this code). 141573Srgrimes * 151573Srgrimes * You should have received a copy of the GNU General Public License version 161573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181573Srgrimes * 191573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201573Srgrimes * or visit www.oracle.com if you need additional information or have any 21249808Semaste * questions. 221573Srgrimes */ 231573Srgrimes 241573Srgrimespackage sun.hotspot; 251573Srgrimes 261573Srgrimesimport java.lang.management.MemoryUsage; 271573Srgrimesimport java.lang.reflect.Executable; 281573Srgrimesimport java.util.Arrays; 291573Srgrimesimport java.util.List; 301573Srgrimesimport java.util.function.BiFunction; 311573Srgrimesimport java.util.function.Function; 321573Srgrimesimport java.security.BasicPermission; 331573Srgrimesimport java.util.Objects; 341573Srgrimes 351573Srgrimesimport sun.hotspot.parser.DiagnosticCommand; 361573Srgrimes 371573Srgrimespublic class WhiteBox { 381573Srgrimes @SuppressWarnings("serial") 391573Srgrimes public static class WhiteBoxPermission extends BasicPermission { 401573Srgrimes public WhiteBoxPermission(String s) { 4192986Sobrien super(s); 4292986Sobrien } 431573Srgrimes } 4471579Sdeischen 451573Srgrimes private WhiteBox() {} 461573Srgrimes private static final WhiteBox instance = new WhiteBox(); 4771579Sdeischen private static native void registerNatives(); 4835129Sjb 49108622Stjr /** 50227753Stheraven * Returns the singleton WhiteBox instance. 511573Srgrimes * 5213545Sjulian * The returned WhiteBox object should be carefully guarded 53104989Smike * by the caller, since it can be used to read and write data 541573Srgrimes * at arbitrary memory addresses. It must never be passed to 551573Srgrimes * untrusted code. 561573Srgrimes */ 571573Srgrimes public synchronized static WhiteBox getWhiteBox() { 581573Srgrimes SecurityManager sm = System.getSecurityManager(); 5935129Sjb if (sm != null) { 60227753Stheraven sm.checkPermission(new WhiteBoxPermission("getInstance")); 6135129Sjb } 621573Srgrimes return instance; 631573Srgrimes } 641573Srgrimes 65227753Stheraven static { 66227753Stheraven registerNatives(); 67227753Stheraven } 68227753Stheraven 69227753Stheraven // Get the maximum heap size supporting COOPs 70227753Stheraven public native long getCompressedOopsMaxHeapSize(); 71227753Stheraven // Arguments 72227753Stheraven public native void printHeapSizes(); 73227753Stheraven 74227753Stheraven // Memory 75227753Stheraven private native long getObjectAddress0(Object o); 76227753Stheraven public long getObjectAddress(Object o) { 77227753Stheraven Objects.requireNonNull(o); 78227753Stheraven return getObjectAddress0(o); 79 } 80 81 public native int getHeapOopSize(); 82 public native int getVMPageSize(); 83 public native long getVMAllocationGranularity(); 84 public native long getVMLargePageSize(); 85 public native long getHeapSpaceAlignment(); 86 public native long getHeapAlignment(); 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 /** 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, -2 /*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, -2 /*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(-2 /*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 // Don't use these methods directly 382 // Use sun.hotspot.gc.GC class instead. 383 public native int currentGC(); 384 public native int allSupportedGC(); 385 public native boolean gcSelectedByErgo(); 386 387 // Force Young GC 388 public native void youngGC(); 389 390 // Force Full GC 391 public native void fullGC(); 392 393 // Returns true if the current GC supports control of its concurrent 394 // phase via requestConcurrentGCPhase(). If false, a request will 395 // always fail. 396 public native boolean supportsConcurrentGCPhaseControl(); 397 398 // Returns an array of concurrent phase names provided by this 399 // collector. These are the names recognized by 400 // requestConcurrentGCPhase(). 401 public native String[] getConcurrentGCPhases(); 402 403 // Attempt to put the collector into the indicated concurrent phase, 404 // and attempt to remain in that state until a new request is made. 405 // 406 // Returns immediately if already in the requested phase. 407 // Otherwise, waits until the phase is reached. 408 // 409 // Throws IllegalStateException if unsupported by the current collector. 410 // Throws NullPointerException if phase is null. 411 // Throws IllegalArgumentException if phase is not valid for the current collector. 412 public void requestConcurrentGCPhase(String phase) { 413 if (!supportsConcurrentGCPhaseControl()) { 414 throw new IllegalStateException("Concurrent GC phase control not supported"); 415 } else if (phase == null) { 416 throw new NullPointerException("null phase"); 417 } else if (!requestConcurrentGCPhase0(phase)) { 418 throw new IllegalArgumentException("Unknown concurrent GC phase: " + phase); 419 } 420 } 421 422 // Helper for requestConcurrentGCPhase(). Returns true if request 423 // succeeded, false if the phase is invalid. 424 private native boolean requestConcurrentGCPhase0(String phase); 425 426 // Method tries to start concurrent mark cycle. 427 // It returns false if CM Thread is always in concurrent cycle. 428 public native boolean g1StartConcMarkCycle(); 429 430 // Tests on ReservedSpace/VirtualSpace classes 431 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 432 public native void runMemoryUnitTests(); 433 public native void readFromNoaccessArea(); 434 public native long getThreadStackSize(); 435 public native long getThreadRemainingStackSize(); 436 437 // CPU features 438 public native String getCPUFeatures(); 439 440 // Native extensions 441 public native long getHeapUsageForContext(int context); 442 public native long getHeapRegionCountForContext(int context); 443 private native int getContextForObject0(Object obj); 444 public int getContextForObject(Object obj) { 445 Objects.requireNonNull(obj); 446 return getContextForObject0(obj); 447 } 448 public native void printRegionInfo(int context); 449 450 // VM flags 451 public native boolean isConstantVMFlag(String name); 452 public native boolean isLockedVMFlag(String name); 453 public native void setBooleanVMFlag(String name, boolean value); 454 public native void setIntVMFlag(String name, long value); 455 public native void setUintVMFlag(String name, long value); 456 public native void setIntxVMFlag(String name, long value); 457 public native void setUintxVMFlag(String name, long value); 458 public native void setUint64VMFlag(String name, long value); 459 public native void setSizeTVMFlag(String name, long value); 460 public native void setStringVMFlag(String name, String value); 461 public native void setDoubleVMFlag(String name, double value); 462 public native Boolean getBooleanVMFlag(String name); 463 public native Long getIntVMFlag(String name); 464 public native Long getUintVMFlag(String name); 465 public native Long getIntxVMFlag(String name); 466 public native Long getUintxVMFlag(String name); 467 public native Long getUint64VMFlag(String name); 468 public native Long getSizeTVMFlag(String name); 469 public native String getStringVMFlag(String name); 470 public native Double getDoubleVMFlag(String name); 471 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 472 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 473 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 474 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 475 476 public Object getVMFlag(String name) { 477 return flagsGetters.stream() 478 .map(f -> f.apply(name)) 479 .filter(x -> x != null) 480 .findAny() 481 .orElse(null); 482 } 483 484 // Jigsaw 485 public native void DefineModule(Object module, String version, String location, 486 Object[] packages); 487 public native void AddModuleExports(Object from_module, String pkg, Object to_module); 488 public native void AddReadsModule(Object from_module, Object source_module); 489 public native void AddModulePackage(Object module, String pkg); 490 public native void AddModuleExportsToAllUnnamed(Object module, String pkg); 491 public native void AddModuleExportsToAll(Object module, String pkg); 492 493 public native int getOffsetForName0(String name); 494 public int getOffsetForName(String name) throws Exception { 495 int offset = getOffsetForName0(name); 496 if (offset == -1) { 497 throw new RuntimeException(name + " not found"); 498 } 499 return offset; 500 } 501 public native Boolean getMethodBooleanOption(Executable method, String name); 502 public native Long getMethodIntxOption(Executable method, String name); 503 public native Long getMethodUintxOption(Executable method, String name); 504 public native Double getMethodDoubleOption(Executable method, String name); 505 public native String getMethodStringOption(Executable method, String name); 506 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 507 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 508 this::getMethodUintxOption, this::getMethodDoubleOption, 509 this::getMethodStringOption); 510 511 public Object getMethodOption(Executable method, String name) { 512 return methodOptionGetters.stream() 513 .map(f -> f.apply(method, name)) 514 .filter(x -> x != null) 515 .findAny() 516 .orElse(null); 517 } 518 519 // Safepoint Checking 520 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 521 522 // Sharing 523 public native boolean isShared(Object o); 524 public native boolean isSharedClass(Class<?> c); 525 public native boolean areSharedStringsIgnored(); 526 527 // Compiler Directive 528 public native int addCompilerDirective(String compDirect); 529 public native void removeCompilerDirective(int count); 530} 531