WhiteBox.java revision 1458:e36425f51dff
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.util.stream.Stream; 34import java.security.BasicPermission; 35import java.util.Objects; 36 37import sun.hotspot.parser.DiagnosticCommand; 38 39public class WhiteBox { 40 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 87 private native boolean isObjectInOldGen0(Object o); 88 public boolean isObjectInOldGen(Object o) { 89 Objects.requireNonNull(o); 90 return isObjectInOldGen0(o); 91 } 92 93 private native long getObjectSize0(Object o); 94 public long getObjectSize(Object o) { 95 Objects.requireNonNull(o); 96 return getObjectSize0(o); 97 } 98 99 // Runtime 100 // Make sure class name is in the correct format 101 public boolean isClassAlive(String name) { 102 return isClassAlive0(name.replace('.', '/')); 103 } 104 private native boolean isClassAlive0(String name); 105 106 private native boolean isMonitorInflated0(Object obj); 107 public boolean isMonitorInflated(Object obj) { 108 Objects.requireNonNull(obj); 109 return isMonitorInflated0(obj); 110 } 111 112 public native void forceSafepoint(); 113 114 // JVMTI 115 private native void addToBootstrapClassLoaderSearch0(String segment); 116 public void addToBootstrapClassLoaderSearch(String segment){ 117 Objects.requireNonNull(segment); 118 addToBootstrapClassLoaderSearch0(segment); 119 } 120 121 private native void addToSystemClassLoaderSearch0(String segment); 122 public void addToSystemClassLoaderSearch(String segment) { 123 Objects.requireNonNull(segment); 124 addToSystemClassLoaderSearch0(segment); 125 } 126 127 // G1 128 public native boolean g1InConcurrentMark(); 129 private native boolean g1IsHumongous0(Object o); 130 public boolean g1IsHumongous(Object o) { 131 Objects.requireNonNull(o); 132 return g1IsHumongous0(o); 133 } 134 135 public native long g1NumMaxRegions(); 136 public native long g1NumFreeRegions(); 137 public native int g1RegionSize(); 138 public native MemoryUsage g1AuxiliaryMemoryUsage(); 139 private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args); 140 public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) { 141 Objects.requireNonNull(args); 142 return parseCommandLine0(commandline, delim, args); 143 } 144 145 // NMT 146 public native long NMTMalloc(long size); 147 public native void NMTFree(long mem); 148 public native long NMTReserveMemory(long size); 149 public native void NMTCommitMemory(long addr, long size); 150 public native void NMTUncommitMemory(long addr, long size); 151 public native void NMTReleaseMemory(long addr, long size); 152 public native long NMTMallocWithPseudoStack(long size, int index); 153 public native boolean NMTChangeTrackingLevel(); 154 public native int NMTGetHashSize(); 155 156 // Compiler 157 public native int deoptimizeFrames(boolean makeNotEntrant); 158 public native void deoptimizeAll(); 159 public boolean isMethodCompiled(Executable method) { 160 return isMethodCompiled(method, false /*not osr*/); 161 } 162 private native boolean isMethodCompiled0(Executable method, boolean isOsr); 163 public boolean isMethodCompiled(Executable method, boolean isOsr){ 164 Objects.requireNonNull(method); 165 return isMethodCompiled0(method, isOsr); 166 } 167 public boolean isMethodCompilable(Executable method) { 168 return isMethodCompilable(method, -1 /*any*/); 169 } 170 public boolean isMethodCompilable(Executable method, int compLevel) { 171 return isMethodCompilable(method, compLevel, false /*not osr*/); 172 } 173 private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr); 174 public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) { 175 Objects.requireNonNull(method); 176 return isMethodCompilable0(method, compLevel, isOsr); 177 } 178 private native boolean isMethodQueuedForCompilation0(Executable method); 179 public boolean isMethodQueuedForCompilation(Executable method) { 180 Objects.requireNonNull(method); 181 return isMethodQueuedForCompilation0(method); 182 } 183 public int deoptimizeMethod(Executable method) { 184 return deoptimizeMethod(method, false /*not osr*/); 185 } 186 private native int deoptimizeMethod0(Executable method, boolean isOsr); 187 public int deoptimizeMethod(Executable method, boolean isOsr) { 188 Objects.requireNonNull(method); 189 return deoptimizeMethod0(method, isOsr); 190 } 191 public void makeMethodNotCompilable(Executable method) { 192 makeMethodNotCompilable(method, -1 /*any*/); 193 } 194 public void makeMethodNotCompilable(Executable method, int compLevel) { 195 makeMethodNotCompilable(method, compLevel, false /*not osr*/); 196 } 197 private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr); 198 public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) { 199 Objects.requireNonNull(method); 200 makeMethodNotCompilable0(method, compLevel, isOsr); 201 } 202 public int getMethodCompilationLevel(Executable method) { 203 return getMethodCompilationLevel(method, false /*not ost*/); 204 } 205 private native int getMethodCompilationLevel0(Executable method, boolean isOsr); 206 public int getMethodCompilationLevel(Executable method, boolean isOsr) { 207 Objects.requireNonNull(method); 208 return getMethodCompilationLevel0(method, isOsr); 209 } 210 private native boolean testSetDontInlineMethod0(Executable method, boolean value); 211 public boolean testSetDontInlineMethod(Executable method, boolean value) { 212 Objects.requireNonNull(method); 213 return testSetDontInlineMethod0(method, value); 214 } 215 public int getCompileQueuesSize() { 216 return getCompileQueueSize(-1 /*any*/); 217 } 218 public native int getCompileQueueSize(int compLevel); 219 private native boolean testSetForceInlineMethod0(Executable method, boolean value); 220 public boolean testSetForceInlineMethod(Executable method, boolean value) { 221 Objects.requireNonNull(method); 222 return testSetForceInlineMethod0(method, value); 223 } 224 public boolean enqueueMethodForCompilation(Executable method, int compLevel) { 225 return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/); 226 } 227 private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci); 228 public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) { 229 Objects.requireNonNull(method); 230 return enqueueMethodForCompilation0(method, compLevel, entry_bci); 231 } 232 private native void clearMethodState0(Executable method); 233 public void clearMethodState(Executable method) { 234 Objects.requireNonNull(method); 235 clearMethodState0(method); 236 } 237 public native void lockCompilation(); 238 public native void unlockCompilation(); 239 private native int getMethodEntryBci0(Executable method); 240 public int getMethodEntryBci(Executable method) { 241 Objects.requireNonNull(method); 242 return getMethodEntryBci0(method); 243 } 244 private native Object[] getNMethod0(Executable method, boolean isOsr); 245 public Object[] getNMethod(Executable method, boolean isOsr) { 246 Objects.requireNonNull(method); 247 return getNMethod0(method, isOsr); 248 } 249 public native long allocateCodeBlob(int size, int type); 250 public long allocateCodeBlob(long size, int type) { 251 int intSize = (int) size; 252 if ((long) intSize != size || size < 0) { 253 throw new IllegalArgumentException( 254 "size argument has illegal value " + size); 255 } 256 return allocateCodeBlob( intSize, type); 257 } 258 public native void freeCodeBlob(long addr); 259 public void forceNMethodSweep() { 260 try { 261 forceNMethodSweep0().join(); 262 } catch (InterruptedException e) { 263 Thread.currentThread().interrupt(); 264 } 265 } 266 public native Thread forceNMethodSweep0(); 267 public native Object[] getCodeHeapEntries(int type); 268 public native int getCompilationActivityMode(); 269 public native Object[] getCodeBlob(long addr); 270 271 // Intered strings 272 public native boolean isInStringTable(String str); 273 274 // Memory 275 public native void readReservedMemory(); 276 public native long allocateMetaspace(ClassLoader classLoader, long size); 277 public native void freeMetaspace(ClassLoader classLoader, long addr, long size); 278 public native long incMetaspaceCapacityUntilGC(long increment); 279 public native long metaspaceCapacityUntilGC(); 280 281 // Force Young GC 282 public native void youngGC(); 283 284 // Force Full GC 285 public native void fullGC(); 286 287 // Method tries to start concurrent mark cycle. 288 // It returns false if CM Thread is always in concurrent cycle. 289 public native boolean g1StartConcMarkCycle(); 290 291 // Tests on ReservedSpace/VirtualSpace classes 292 public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations); 293 public native void runMemoryUnitTests(); 294 public native void readFromNoaccessArea(); 295 public native long getThreadStackSize(); 296 public native long getThreadRemainingStackSize(); 297 298 // CPU features 299 public native String getCPUFeatures(); 300 301 // Native extensions 302 public native long getHeapUsageForContext(int context); 303 public native long getHeapRegionCountForContext(int context); 304 private native int getContextForObject0(Object obj); 305 public int getContextForObject(Object obj) { 306 Objects.requireNonNull(obj); 307 return getContextForObject0(obj); 308 } 309 public native void printRegionInfo(int context); 310 311 // VM flags 312 public native boolean isConstantVMFlag(String name); 313 public native boolean isLockedVMFlag(String name); 314 public native void setBooleanVMFlag(String name, boolean value); 315 public native void setIntxVMFlag(String name, long value); 316 public native void setUintxVMFlag(String name, long value); 317 public native void setUint64VMFlag(String name, long value); 318 public native void setSizeTVMFlag(String name, long value); 319 public native void setStringVMFlag(String name, String value); 320 public native void setDoubleVMFlag(String name, double value); 321 public native Boolean getBooleanVMFlag(String name); 322 public native Long getIntxVMFlag(String name); 323 public native Long getUintxVMFlag(String name); 324 public native Long getUint64VMFlag(String name); 325 public native Long getSizeTVMFlag(String name); 326 public native String getStringVMFlag(String name); 327 public native Double getDoubleVMFlag(String name); 328 private final List<Function<String,Object>> flagsGetters = Arrays.asList( 329 this::getBooleanVMFlag, this::getIntxVMFlag, this::getUintxVMFlag, 330 this::getUint64VMFlag, this::getSizeTVMFlag, this::getStringVMFlag, 331 this::getDoubleVMFlag); 332 333 public Object getVMFlag(String name) { 334 return flagsGetters.stream() 335 .map(f -> f.apply(name)) 336 .filter(x -> x != null) 337 .findAny() 338 .orElse(null); 339 } 340 public native int getOffsetForName0(String name); 341 public int getOffsetForName(String name) throws Exception { 342 int offset = getOffsetForName0(name); 343 if (offset == -1) { 344 throw new RuntimeException(name + " not found"); 345 } 346 return offset; 347 } 348 public native Boolean getMethodBooleanOption(Executable method, String name); 349 public native Long getMethodIntxOption(Executable method, String name); 350 public native Long getMethodUintxOption(Executable method, String name); 351 public native Double getMethodDoubleOption(Executable method, String name); 352 public native String getMethodStringOption(Executable method, String name); 353 private final List<BiFunction<Executable,String,Object>> methodOptionGetters 354 = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption, 355 this::getMethodUintxOption, this::getMethodDoubleOption, 356 this::getMethodStringOption); 357 358 public Object getMethodOption(Executable method, String name) { 359 return methodOptionGetters.stream() 360 .map(f -> f.apply(method, name)) 361 .filter(x -> x != null) 362 .findAny() 363 .orElse(null); 364 } 365 366 // Safepoint Checking 367 public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue); 368} 369