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