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