WhiteBox.java revision 1720:5e06ef3a390a
1/* 2 * Copyright (c) 2012, 2015, 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 // JVMTI 122 private native void addToBootstrapClassLoaderSearch0(String segment); 123 public void addToBootstrapClassLoaderSearch(String segment){ 124 Objects.requireNonNull(segment); 125 addToBootstrapClassLoaderSearch0(segment); 126 } 127 128 private native void addToSystemClassLoaderSearch0(String segment); 129 public void addToSystemClassLoaderSearch(String segment) { 130 Objects.requireNonNull(segment); 131 addToSystemClassLoaderSearch0(segment); 132 } 133 134 // G1 135 public native boolean g1InConcurrentMark(); 136 private native boolean g1IsHumongous0(Object o); 137 public boolean g1IsHumongous(Object o) { 138 Objects.requireNonNull(o); 139 return g1IsHumongous0(o); 140 } 141 142 public native long g1NumMaxRegions(); 143 public native long g1NumFreeRegions(); 144 public native int g1RegionSize(); 145 public native MemoryUsage g1AuxiliaryMemoryUsage(); 146 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 147 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 148 Objects.requireNonNull(args); 149 return parseCommandLine0(commandline, delim, args); 150 } 151 152 // Parallel GC 153 public native long psVirtualSpaceAlignment(); 154 public native long psHeapGenerationAlignment(); 155 156 // NMT 157 public native long NMTMalloc(long size); 158 public native void NMTFree(long mem); 159 public native long NMTReserveMemory(long size); 160 public native void NMTCommitMemory(long addr, long size); 161 public native void NMTUncommitMemory(long addr, long size); 162 public native void NMTReleaseMemory(long addr, long size); 163 public native long NMTMallocWithPseudoStack(long size, int index); 164 public native boolean NMTChangeTrackingLevel(); 165 public native int NMTGetHashSize(); 166 167 // Compiler 168 public native int matchesMethod(Executable method, String pattern); 169 public native int matchesInline(Executable method, String pattern); 170 public native boolean shouldPrintAssembly(Executable method); 171 public native int deoptimizeFrames(boolean makeNotEntrant); 172 public native void deoptimizeAll(); 173 public boolean isMethodCompiled(Executable method) { 174 return isMethodCompiled(method, false /*not osr*/); 175 } 176 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 177 public boolean isMethodCompiled(Executable method, boolean isOsr){ 178 Objects.requireNonNull(method); 179 return isMethodCompiled0(method, isOsr); 180 } 181 public boolean isMethodCompilable(Executable method) { 182 return isMethodCompilable(method, -1 /*any*/); 183 } 184 public boolean isMethodCompilable(Executable method, int compLevel) { 185 return isMethodCompilable(method, compLevel, false /*not osr*/); 186 } 187 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 188 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 189 Objects.requireNonNull(method); 190 return isMethodCompilable0(method, compLevel, isOsr); 191 } 192 private native boolean isMethodQueuedForCompilation0(Executable method); 193 public boolean isMethodQueuedForCompilation(Executable method) { 194 Objects.requireNonNull(method); 195 return isMethodQueuedForCompilation0(method); 196 } 197 // Determine if the compiler corresponding to the compilation level 'compLevel' 198 // and to the compilation context 'compilation_context' provides an intrinsic 199 // for the method 'method'. An intrinsic is available for method 'method' if: 200 // - the intrinsic is enabled (by using the appropriate command-line flag) and 201 // - the platform on which the VM is running provides the instructions necessary 202 // for the compiler to generate the intrinsic code. 203 // 204 // The compilation context is related to using the DisableIntrinsic flag on a 205 // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp 206 // for more details. 207 public boolean isIntrinsicAvailable(Executable method, 208 Executable compilationContext, 209 int compLevel) { 210 Objects.requireNonNull(method); 211 return isIntrinsicAvailable0(method, compilationContext, compLevel); 212 } 213 // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored), 214 // use the below method that does not require the compilation context as argument. 215 public boolean isIntrinsicAvailable(Executable method, int compLevel) { 216 return isIntrinsicAvailable(method, null, compLevel); 217 } 218 private native boolean isIntrinsicAvailable0(Executable method, 219 Executable compilationContext, 220 int compLevel); 221 public int deoptimizeMethod(Executable method) { 222 return deoptimizeMethod(method, false /*not osr*/); 223 } 224 private native int deoptimizeMethod0(Executable method, boolean isOsr); 225 public int deoptimizeMethod(Executable method, boolean isOsr) { 226 Objects.requireNonNull(method); 227 return deoptimizeMethod0(method, isOsr); 228 } 229 public void makeMethodNotCompilable(Executable method) { 230 makeMethodNotCompilable(method, -1 /*any*/); 231 } 232 public void makeMethodNotCompilable(Executable method, int compLevel) { 233 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 234 } 235 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 236 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 237 Objects.requireNonNull(method); 238 makeMethodNotCompilable0(method, compLevel, isOsr); 239 } 240 public int getMethodCompilationLevel(Executable method) { 241 return getMethodCompilationLevel(method, false /*not ost*/); 242 } 243 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 244 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 245 Objects.requireNonNull(method); 246 return getMethodCompilationLevel0(method, isOsr); 247 } 248 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 249 public boolean testSetDontInlineMethod(Executable method, boolean value) { 250 Objects.requireNonNull(method); 251 return testSetDontInlineMethod0(method, value); 252 } 253 public int getCompileQueuesSize() { 254 return getCompileQueueSize(-1 /*any*/); 255 } 256 public native int getCompileQueueSize(int compLevel); 257 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 258 public boolean testSetForceInlineMethod(Executable method, boolean value) { 259 Objects.requireNonNull(method); 260 return testSetForceInlineMethod0(method, value); 261 } 262 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 263 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 264 } 265 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 266 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 267 Objects.requireNonNull(method); 268 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 269 } 270 private native void clearMethodState0(Executable method); 271 public void clearMethodState(Executable method) { 272 Objects.requireNonNull(method); 273 clearMethodState0(method); 274 } 275 public native void lockCompilation(); 276 public native void unlockCompilation(); 277 private native int getMethodEntryBci0(Executable method); 278 public int getMethodEntryBci(Executable method) { 279 Objects.requireNonNull(method); 280 return getMethodEntryBci0(method); 281 } 282 private native Object[] getNMethod0(Executable method, boolean isOsr); 283 public Object[] getNMethod(Executable method, boolean isOsr) { 284 Objects.requireNonNull(method); 285 return getNMethod0(method, isOsr); 286 } 287 public native long allocateCodeBlob(int size, int type); 288 public long allocateCodeBlob(long size, int type) { 289 int intSize = (int) size; 290 if ((long) intSize != size || size < 0) { 291 throw new IllegalArgumentException( 292 "size argument has illegal value " + size); 293 } 294 return allocateCodeBlob( intSize, type); 295 } 296 public native void freeCodeBlob(long addr); 297 public native void forceNMethodSweep(); 298 public native Object[] getCodeHeapEntries(int type); 299 public native int getCompilationActivityMode(); 300 private native long getMethodData0(Executable method); 301 public long getMethodData(Executable method) { 302 Objects.requireNonNull(method); 303 return getMethodData0(method); 304 } 305 public native Object[] getCodeBlob(long addr); 306 307 // Intered strings 308 public native boolean isInStringTable(String str); 309 310 // Memory 311 public native void readReservedMemory(); 312 public native long allocateMetaspace(ClassLoader classLoader, long size); 313 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 314 public native long incMetaspaceCapacityUntilGC(long increment); 315 public native long metaspaceCapacityUntilGC(); 316 317 // Force Young GC 318 public native void youngGC(); 319 320 // Force Full GC 321 public native void fullGC(); 322 323 // Method tries to start concurrent mark cycle. 324 // It returns false if CM Thread is always in concurrent cycle. 325 public native boolean g1StartConcMarkCycle(); 326 327 // Tests on ReservedSpace/VirtualSpace classes 328 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 329 public native void runMemoryUnitTests(); 330 public native void readFromNoaccessArea(); 331 public native long getThreadStackSize(); 332 public native long getThreadRemainingStackSize(); 333 334 // CPU features 335 public native String getCPUFeatures(); 336 337 // Native extensions 338 public native long getHeapUsageForContext(int context); 339 public native long getHeapRegionCountForContext(int context); 340 private native int getContextForObject0(Object obj); 341 public int getContextForObject(Object obj) { 342 Objects.requireNonNull(obj); 343 return getContextForObject0(obj); 344 } 345 public native void printRegionInfo(int context); 346 347 // VM flags 348 public native boolean isConstantVMFlag(String name); 349 public native boolean isLockedVMFlag(String name); 350 public native void setBooleanVMFlag(String name, boolean value); 351 public native void setIntVMFlag(String name, long value); 352 public native void setUintVMFlag(String name, long value); 353 public native void setIntxVMFlag(String name, long value); 354 public native void setUintxVMFlag(String name, long value); 355 public native void setUint64VMFlag(String name, long value); 356 public native void setSizeTVMFlag(String name, long value); 357 public native void setStringVMFlag(String name, String value); 358 public native void setDoubleVMFlag(String name, double value); 359 public native Boolean getBooleanVMFlag(String name); 360 public native Long getIntVMFlag(String name); 361 public native Long getUintVMFlag(String name); 362 public native Long getIntxVMFlag(String name); 363 public native Long getUintxVMFlag(String name); 364 public native Long getUint64VMFlag(String name); 365 public native Long getSizeTVMFlag(String name); 366 public native String getStringVMFlag(String name); 367 public native Double getDoubleVMFlag(String name); 368 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 369 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 370 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 371 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 372 373 public Object getVMFlag(String name) { 374 return flagsGetters.stream() 375 .map(f -> f.apply(name)) 376 .filter(x -> x != null) 377 .findAny() 378 .orElse(null); 379 } 380 public native int getOffsetForName0(String name); 381 public int getOffsetForName(String name) throws Exception { 382 int offset = getOffsetForName0(name); 383 if (offset == -1) { 384 throw new RuntimeException(name + " not found"); 385 } 386 return offset; 387 } 388 public native Boolean getMethodBooleanOption(Executable method, String name); 389 public native Long getMethodIntxOption(Executable method, String name); 390 public native Long getMethodUintxOption(Executable method, String name); 391 public native Double getMethodDoubleOption(Executable method, String name); 392 public native String getMethodStringOption(Executable method, String name); 393 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 394 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 395 this::getMethodUintxOption, this::getMethodDoubleOption, 396 this::getMethodStringOption); 397 398 public Object getMethodOption(Executable method, String name) { 399 return methodOptionGetters.stream() 400 .map(f -> f.apply(method, name)) 401 .filter(x -> x != null) 402 .findAny() 403 .orElse(null); 404 } 405 406 // Safepoint Checking 407 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 408 409 // Sharing 410 public native boolean isShared(Object o); 411 public native boolean areSharedStringsIgnored(); 412} 413