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 25 package sun.hotspot; 26 27 import java.lang.management.MemoryUsage; 28 import java.lang.reflect.Executable; 29 import java.nio.ByteBuffer; 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.function.BiFunction; 33 import java.util.function.Function; 34 import java.security.BasicPermission; 35 import java.util.Objects; 36 37 import sun.hotspot.parser.DiagnosticCommand; 38 39 public class WhiteBox { 40 @SuppressWarnings("serial") 41 public static class WhiteBoxPermission extends BasicPermission { 42 public WhiteBoxPermission(String s) { 43 super(s); 44 } 45 } 46 47 private WhiteBox() {} 48 private static final WhiteBox instance = new WhiteBox(); 49 private static native void registerNatives(); 50 51 /** 52 * Returns the singleton WhiteBox instance. 53 * 54 * The returned WhiteBox object should be carefully guarded 55 * by the caller, since it can be used to read and write data 56 * at arbitrary memory addresses. It must never be passed to 57 * untrusted code. 58 */ 59 public synchronized static WhiteBox getWhiteBox() { 60 SecurityManager sm = System.getSecurityManager(); 61 if (sm != null) { 62 sm.checkPermission(new WhiteBoxPermission("getInstance")); 63 } 64 return instance; 65 } 66 67 static { 68 registerNatives(); 69 } 70 71 // Get the maximum heap size supporting COOPs 72 public native long getCompressedOopsMaxHeapSize(); 73 // Arguments 74 public native void printHeapSizes(); 75 76 // Memory 77 private native long getObjectAddress0(Object o); 78 public long getObjectAddress(Object o) { 79 Objects.requireNonNull(o); 80 return getObjectAddress0(o); 81 } 82 83 public native int getHeapOopSize(); 84 public native int getVMPageSize(); 85 public native long getVMAllocationGranularity(); 86 public native long getVMLargePageSize(); 87 public native long getHeapSpaceAlignment(); 88 89 private native boolean isObjectInOldGen0(Object o); 90 public boolean isObjectInOldGen(Object o) { 91 Objects.requireNonNull(o); 92 return isObjectInOldGen0(o); 93 } 94 95 private native long getObjectSize0(Object o); 96 public long getObjectSize(Object o) { 97 Objects.requireNonNull(o); 98 return getObjectSize0(o); 99 } 100 101 // Runtime 102 // Make sure class name is in the correct format 103 public boolean isClassAlive(String name) { 104 return isClassAlive0(name.replace('.', '/')); 105 } 106 private native boolean isClassAlive0(String name); 107 108 private native boolean isMonitorInflated0(Object obj); 109 public boolean isMonitorInflated(Object obj) { 110 Objects.requireNonNull(obj); 111 return isMonitorInflated0(obj); 112 } 113 114 public native void forceSafepoint(); 115 116 // JVMTI 117 private native void addToBootstrapClassLoaderSearch0(String segment); 118 public void addToBootstrapClassLoaderSearch(String segment){ 119 Objects.requireNonNull(segment); 120 addToBootstrapClassLoaderSearch0(segment); 121 } 122 123 private native void addToSystemClassLoaderSearch0(String segment); 124 public void addToSystemClassLoaderSearch(String segment) { 125 Objects.requireNonNull(segment); 126 addToSystemClassLoaderSearch0(segment); 127 } 128 129 // G1 130 public native boolean g1InConcurrentMark(); 131 private native boolean g1IsHumongous0(Object o); 132 public boolean g1IsHumongous(Object o) { 133 Objects.requireNonNull(o); 134 return g1IsHumongous0(o); 135 } 136 137 public native long g1NumMaxRegions(); 138 public native long g1NumFreeRegions(); 139 public native int g1RegionSize(); 140 public native MemoryUsage g1AuxiliaryMemoryUsage(); 141 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 142 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 143 Objects.requireNonNull(args); 144 return parseCommandLine0(commandline, delim, args); 145 } 146 147 // Parallel GC 148 public native long psVirtualSpaceAlignment(); 149 public native long psHeapGenerationAlignment(); 150 151 // NMT 152 public native long NMTMalloc(long size); 153 public native void NMTFree(long mem); 154 public native long NMTReserveMemory(long size); 155 public native void NMTCommitMemory(long addr, long size); 156 public native void NMTUncommitMemory(long addr, long size); 157 public native void NMTReleaseMemory(long addr, long size); 158 public native long NMTMallocWithPseudoStack(long size, int index); 159 public native boolean NMTChangeTrackingLevel(); 160 public native int NMTGetHashSize(); 161 162 // Compiler 163 public native int deoptimizeFrames(boolean makeNotEntrant); 164 public native void deoptimizeAll(); 165 public boolean isMethodCompiled(Executable method) { 166 return isMethodCompiled(method, false /*not osr*/); 167 } 168 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 169 public boolean isMethodCompiled(Executable method, boolean isOsr){ 170 Objects.requireNonNull(method); 171 return isMethodCompiled0(method, isOsr); 172 } 173 public boolean isMethodCompilable(Executable method) { 174 return isMethodCompilable(method, -1 /*any*/); 175 } 176 public boolean isMethodCompilable(Executable method, int compLevel) { 177 return isMethodCompilable(method, compLevel, false /*not osr*/); 178 } 179 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 180 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 181 Objects.requireNonNull(method); 182 return isMethodCompilable0(method, compLevel, isOsr); 183 } 184 private native boolean isMethodQueuedForCompilation0(Executable method); 185 public boolean isMethodQueuedForCompilation(Executable method) { 186 Objects.requireNonNull(method); 187 return isMethodQueuedForCompilation0(method); 188 } 189 public int deoptimizeMethod(Executable method) { 190 return deoptimizeMethod(method, false /*not osr*/); 191 } 192 private native int deoptimizeMethod0(Executable method, boolean isOsr); 193 public int deoptimizeMethod(Executable method, boolean isOsr) { 194 Objects.requireNonNull(method); 195 return deoptimizeMethod0(method, isOsr); 196 } 197 public void makeMethodNotCompilable(Executable method) { 198 makeMethodNotCompilable(method, -1 /*any*/); 199 } 200 public void makeMethodNotCompilable(Executable method, int compLevel) { 201 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 202 } 203 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 204 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 205 Objects.requireNonNull(method); 206 makeMethodNotCompilable0(method, compLevel, isOsr); 207 } 208 public int getMethodCompilationLevel(Executable method) { 209 return getMethodCompilationLevel(method, false /*not ost*/); 210 } 211 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 212 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 213 Objects.requireNonNull(method); 214 return getMethodCompilationLevel0(method, isOsr); 215 } 216 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 217 public boolean testSetDontInlineMethod(Executable method, boolean value) { 218 Objects.requireNonNull(method); 219 return testSetDontInlineMethod0(method, value); 220 } 221 public int getCompileQueuesSize() { 222 return getCompileQueueSize(-1 /*any*/); 223 } 224 public native int getCompileQueueSize(int compLevel); 225 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 226 public boolean testSetForceInlineMethod(Executable method, boolean value) { 227 Objects.requireNonNull(method); 228 return testSetForceInlineMethod0(method, value); 229 } 230 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 231 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 232 } 233 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 234 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 235 Objects.requireNonNull(method); 236 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 237 } 238 private native void clearMethodState0(Executable method); 239 public void clearMethodState(Executable method) { 240 Objects.requireNonNull(method); 241 clearMethodState0(method); 242 } 243 public native void lockCompilation(); 244 public native void unlockCompilation(); 245 private native int getMethodEntryBci0(Executable method); 246 public int getMethodEntryBci(Executable method) { 247 Objects.requireNonNull(method); 248 return getMethodEntryBci0(method); 249 } 250 private native Object[] getNMethod0(Executable method, boolean isOsr); 251 public Object[] getNMethod(Executable method, boolean isOsr) { 252 Objects.requireNonNull(method); 253 return getNMethod0(method, isOsr); 254 } 255 public native long allocateCodeBlob(int size, int type); 256 public long allocateCodeBlob(long size, int type) { 257 int intSize = (int) size; 258 if ((long) intSize != size || size < 0) { 259 throw new IllegalArgumentException( 260 "size argument has illegal value " + size); 261 } 262 return allocateCodeBlob( intSize, type); 263 } 264 public native void freeCodeBlob(long addr); 265 public native void forceNMethodSweep(); 266 public native Object[] getCodeHeapEntries(int type); 267 public native int getCompilationActivityMode(); 268 public native Object[] getCodeBlob(long addr); 269 270 // Intered strings 271 public native boolean isInStringTable(String str); 272 273 // Memory 274 public native void readReservedMemory(); 275 public native long allocateMetaspace(ClassLoader classLoader, long size); 276 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 277 public native long incMetaspaceCapacityUntilGC(long increment); 278 public native long metaspaceCapacityUntilGC(); 279 280 // Force Young GC 281 public native void youngGC(); 282 283 // Force Full GC 284 public native void fullGC(); 285 286 // Method tries to start concurrent mark cycle. 287 // It returns false if CM Thread is always in concurrent cycle. 288 public native boolean g1StartConcMarkCycle(); 289 290 // Tests on ReservedSpace/VirtualSpace classes 291 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 292 public native void runMemoryUnitTests(); 293 public native void readFromNoaccessArea(); 294 public native long getThreadStackSize(); 295 public native long getThreadRemainingStackSize(); 296 297 // CPU features 298 public native String getCPUFeatures(); 299 300 // Native extensions 301 public native long getHeapUsageForContext(int context); 302 public native long getHeapRegionCountForContext(int context); 303 private native int getContextForObject0(Object obj); 304 public int getContextForObject(Object obj) { 305 Objects.requireNonNull(obj); 306 return getContextForObject0(obj); 307 } 308 public native void printRegionInfo(int context); 309 310 // VM flags 311 public native boolean isConstantVMFlag(String name); 312 public native boolean isLockedVMFlag(String name); 313 public native void setBooleanVMFlag(String name, boolean value); 314 public native void setIntVMFlag(String name, long value); 315 public native void setUintVMFlag(String name, long value); 316 public native void setIntxVMFlag(String name, long value); 317 public native void setUintxVMFlag(String name, long value); 318 public native void setUint64VMFlag(String name, long value); 319 public native void setSizeTVMFlag(String name, long value); 320 public native void setStringVMFlag(String name, String value); 321 public native void setDoubleVMFlag(String name, double value); 322 public native Boolean getBooleanVMFlag(String name); 323 public native Long getIntVMFlag(String name); 324 public native Long getUintVMFlag(String name); 325 public native Long getIntxVMFlag(String name); 326 public native Long getUintxVMFlag(String name); 327 public native Long getUint64VMFlag(String name); 328 public native Long getSizeTVMFlag(String name); 329 public native String getStringVMFlag(String name); 330 public native Double getDoubleVMFlag(String name); 331 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 332 this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag, 333 this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag, 334 this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag); 335 336 public Object getVMFlag(String name) { 337 return flagsGetters.stream() 338 .map(f -> f.apply(name)) 339 .filter(x -> x != null) 340 .findAny() 341 .orElse(null); 342 } 343 public native int getOffsetForName0(String name); 344 public int getOffsetForName(String name) throws Exception { 345 int offset = getOffsetForName0(name); 346 if (offset == -1) { 347 throw new RuntimeException(name + " not found"); 348 } 349 return offset; 350 } 351 public native Boolean getMethodBooleanOption(Executable method, String name); 352 public native Long getMethodIntxOption(Executable method, String name); 353 public native Long getMethodUintxOption(Executable method, String name); 354 public native Double getMethodDoubleOption(Executable method, String name); 355 public native String getMethodStringOption(Executable method, String name); 356 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 357 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 358 this::getMethodUintxOption, this::getMethodDoubleOption, 359 this::getMethodStringOption); 360 361 public Object getMethodOption(Executable method, String name) { 362 return methodOptionGetters.stream() 363 .map(f -> f.apply(method, name)) 364 .filter(x -> x != null) 365 .findAny() 366 .orElse(null); 367 } 368 369 public native boolean readImageFile(String imagePath); 370 public native long imageOpenImage(String imagePath, boolean bigEndian); 371 public native void imageCloseImage(long id); 372 public native long imageGetIndexAddress(long id); 373 public native long imageGetDataAddress(long id); 374 public native boolean imageReadCompressed(long id, long offset, 375 ByteBuffer compressedBuffer, long compressedSize, 376 ByteBuffer uncompressedBuffer, long uncompressedSize); 377 public native boolean imageRead(long id, long offset, 378 ByteBuffer uncompressedBuffer, long uncompressedSize); 379 public native byte[] imageGetStringBytes(long id, int offset); 380 public native long imageGetStringsSize(long id); 381 public native long[] imageGetAttributes(long id, int offset); 382 public native long[] imageFindAttributes(long id, byte[] path); 383 public native int[] imageAttributeOffsets(long id); 384 public native int imageGetIntAtAddress(long address, int offset, boolean big_endian); 385 386 // Safepoint Checking 387 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 388 389 // Sharing 390 public native boolean isShared(Object o); 391 public native boolean areSharedStringsIgnored(); 392 }