Path: blob/master/test/lib/sun/hotspot/WhiteBox.java
66643 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package sun.hotspot;2425import java.lang.management.MemoryUsage;26import java.lang.reflect.Executable;27import java.util.Arrays;28import java.util.List;29import java.util.function.BiFunction;30import java.util.function.Function;31import java.security.BasicPermission;32import java.util.Objects;3334import jdk.test.whitebox.parser.DiagnosticCommand;3536@Deprecated37public class WhiteBox {38@SuppressWarnings("serial")39public static class WhiteBoxPermission extends BasicPermission {40public WhiteBoxPermission(String s) {41super(s);42}43}4445private WhiteBox() {}46private static final WhiteBox instance = new WhiteBox();47private static native void registerNatives();4849/**50* Returns the singleton WhiteBox instance.51*52* The returned WhiteBox object should be carefully guarded53* by the caller, since it can be used to read and write data54* at arbitrary memory addresses. It must never be passed to55* untrusted code.56*/57public synchronized static WhiteBox getWhiteBox() {58@SuppressWarnings("removal")59SecurityManager sm = System.getSecurityManager();60if (sm != null) {61throw new SecurityException("can't use old whitebox with SecurityManager, please switch to jdk.test.whitebox.WhiteBox");62}63return instance;64}6566static {67registerNatives();68}6970// Get the maximum heap size supporting COOPs71public native long getCompressedOopsMaxHeapSize();72// Arguments73public native void printHeapSizes();7475// Memory76private native long getObjectAddress0(Object o);77public long getObjectAddress(Object o) {78Objects.requireNonNull(o);79return getObjectAddress0(o);80}8182public native int getHeapOopSize();83public native int getVMPageSize();84public native long getVMAllocationGranularity();85public native long getVMLargePageSize();86public native long getHeapSpaceAlignment();87public native long getHeapAlignment();8889private native boolean isObjectInOldGen0(Object o);90public boolean isObjectInOldGen(Object o) {91Objects.requireNonNull(o);92return isObjectInOldGen0(o);93}9495private native long getObjectSize0(Object o);96public long getObjectSize(Object o) {97Objects.requireNonNull(o);98return getObjectSize0(o);99}100101// Runtime102// Make sure class name is in the correct format103public int countAliveClasses(String name) {104return countAliveClasses0(name.replace('.', '/'));105}106private native int countAliveClasses0(String name);107108public boolean isClassAlive(String name) {109return countAliveClasses(name) != 0;110}111112public native int getSymbolRefcount(String name);113114public native boolean deflateIdleMonitors();115116private native boolean isMonitorInflated0(Object obj);117public boolean isMonitorInflated(Object obj) {118Objects.requireNonNull(obj);119return isMonitorInflated0(obj);120}121122public native void forceSafepoint();123124private native long getConstantPool0(Class<?> aClass);125public long getConstantPool(Class<?> aClass) {126Objects.requireNonNull(aClass);127return getConstantPool0(aClass);128}129130private native int getConstantPoolCacheIndexTag0();131public int getConstantPoolCacheIndexTag() {132return getConstantPoolCacheIndexTag0();133}134135private native int getConstantPoolCacheLength0(Class<?> aClass);136public int getConstantPoolCacheLength(Class<?> aClass) {137Objects.requireNonNull(aClass);138return getConstantPoolCacheLength0(aClass);139}140141private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index);142public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) {143Objects.requireNonNull(aClass);144return remapInstructionOperandFromCPCache0(aClass, index);145}146147private native int encodeConstantPoolIndyIndex0(int index);148public int encodeConstantPoolIndyIndex(int index) {149return encodeConstantPoolIndyIndex0(index);150}151152// JVMTI153private native void addToBootstrapClassLoaderSearch0(String segment);154public void addToBootstrapClassLoaderSearch(String segment){155Objects.requireNonNull(segment);156addToBootstrapClassLoaderSearch0(segment);157}158159private native void addToSystemClassLoaderSearch0(String segment);160public void addToSystemClassLoaderSearch(String segment) {161Objects.requireNonNull(segment);162addToSystemClassLoaderSearch0(segment);163}164165// G1166public native boolean g1InConcurrentMark();167public native boolean g1HasRegionsToUncommit();168private native boolean g1IsHumongous0(Object o);169public boolean g1IsHumongous(Object o) {170Objects.requireNonNull(o);171return g1IsHumongous0(o);172}173174private native boolean g1BelongsToHumongousRegion0(long adr);175public boolean g1BelongsToHumongousRegion(long adr) {176if (adr == 0) {177throw new IllegalArgumentException("adr argument should not be null");178}179return g1BelongsToHumongousRegion0(adr);180}181182183private native boolean g1BelongsToFreeRegion0(long adr);184public boolean g1BelongsToFreeRegion(long adr) {185if (adr == 0) {186throw new IllegalArgumentException("adr argument should not be null");187}188return g1BelongsToFreeRegion0(adr);189}190191public native long g1NumMaxRegions();192public native long g1NumFreeRegions();193public native int g1RegionSize();194public native MemoryUsage g1AuxiliaryMemoryUsage();195private native Object[] parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args);196public Object[] parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) {197Objects.requireNonNull(args);198return parseCommandLine0(commandline, delim, args);199}200201public native int g1ActiveMemoryNodeCount();202public native int[] g1MemoryNodeIds();203204// Parallel GC205public native long psVirtualSpaceAlignment();206public native long psHeapGenerationAlignment();207208/**209* Enumerates old regions with liveness less than specified and produces some statistics210* @param liveness percent of region's liveness (live_objects / total_region_size * 100).211* @return long[3] array where long[0] - total count of old regions212* long[1] - total memory of old regions213* long[2] - lowest estimation of total memory of old regions to be freed (non-full214* regions are not included)215*/216public native long[] g1GetMixedGCInfo(int liveness);217218// NMT219public native long NMTMalloc(long size);220public native void NMTFree(long mem);221public native long NMTReserveMemory(long size);222public native long NMTAttemptReserveMemoryAt(long addr, long size);223public native void NMTCommitMemory(long addr, long size);224public native void NMTUncommitMemory(long addr, long size);225public native void NMTReleaseMemory(long addr, long size);226public native long NMTMallocWithPseudoStack(long size, int index);227public native long NMTMallocWithPseudoStackAndType(long size, int index, int type);228public native boolean NMTChangeTrackingLevel();229public native int NMTGetHashSize();230public native long NMTNewArena(long initSize);231public native void NMTFreeArena(long arena);232public native void NMTArenaMalloc(long arena, long size);233234// Compiler235public native boolean isC2OrJVMCIIncluded();236public native boolean isJVMCISupportedByGC();237238public native int matchesMethod(Executable method, String pattern);239public native int matchesInline(Executable method, String pattern);240public native boolean shouldPrintAssembly(Executable method, int comp_level);241public native int deoptimizeFrames(boolean makeNotEntrant);242public native boolean isFrameDeoptimized(int depth);243public native void deoptimizeAll();244245public boolean isMethodCompiled(Executable method) {246return isMethodCompiled(method, false /*not osr*/);247}248private native boolean isMethodCompiled0(Executable method, boolean isOsr);249public boolean isMethodCompiled(Executable method, boolean isOsr){250Objects.requireNonNull(method);251return isMethodCompiled0(method, isOsr);252}253public boolean isMethodCompilable(Executable method) {254return isMethodCompilable(method, -1 /*any*/);255}256public boolean isMethodCompilable(Executable method, int compLevel) {257return isMethodCompilable(method, compLevel, false /*not osr*/);258}259private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);260public boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {261Objects.requireNonNull(method);262return isMethodCompilable0(method, compLevel, isOsr);263}264private native boolean isMethodQueuedForCompilation0(Executable method);265public boolean isMethodQueuedForCompilation(Executable method) {266Objects.requireNonNull(method);267return isMethodQueuedForCompilation0(method);268}269// Determine if the compiler corresponding to the compilation level 'compLevel'270// and to the compilation context 'compilation_context' provides an intrinsic271// for the method 'method'. An intrinsic is available for method 'method' if:272// - the intrinsic is enabled (by using the appropriate command-line flag) and273// - the platform on which the VM is running provides the instructions necessary274// for the compiler to generate the intrinsic code.275//276// The compilation context is related to using the DisableIntrinsic flag on a277// per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp278// for more details.279public boolean isIntrinsicAvailable(Executable method,280Executable compilationContext,281int compLevel) {282Objects.requireNonNull(method);283return isIntrinsicAvailable0(method, compilationContext, compLevel);284}285// If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),286// use the below method that does not require the compilation context as argument.287public boolean isIntrinsicAvailable(Executable method, int compLevel) {288return isIntrinsicAvailable(method, null, compLevel);289}290private native boolean isIntrinsicAvailable0(Executable method,291Executable compilationContext,292int compLevel);293public int deoptimizeMethod(Executable method) {294return deoptimizeMethod(method, false /*not osr*/);295}296private native int deoptimizeMethod0(Executable method, boolean isOsr);297public int deoptimizeMethod(Executable method, boolean isOsr) {298Objects.requireNonNull(method);299return deoptimizeMethod0(method, isOsr);300}301public void makeMethodNotCompilable(Executable method) {302makeMethodNotCompilable(method, -1 /*any*/);303}304public void makeMethodNotCompilable(Executable method, int compLevel) {305makeMethodNotCompilable(method, compLevel, false /*not osr*/);306}307private native void makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);308public void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {309Objects.requireNonNull(method);310makeMethodNotCompilable0(method, compLevel, isOsr);311}312public int getMethodCompilationLevel(Executable method) {313return getMethodCompilationLevel(method, false /*not osr*/);314}315private native int getMethodCompilationLevel0(Executable method, boolean isOsr);316public int getMethodCompilationLevel(Executable method, boolean isOsr) {317Objects.requireNonNull(method);318return getMethodCompilationLevel0(method, isOsr);319}320private native boolean testSetDontInlineMethod0(Executable method, boolean value);321public boolean testSetDontInlineMethod(Executable method, boolean value) {322Objects.requireNonNull(method);323return testSetDontInlineMethod0(method, value);324}325public int getCompileQueuesSize() {326return getCompileQueueSize(-1 /*any*/);327}328public native int getCompileQueueSize(int compLevel);329private native boolean testSetForceInlineMethod0(Executable method, boolean value);330public boolean testSetForceInlineMethod(Executable method, boolean value) {331Objects.requireNonNull(method);332return testSetForceInlineMethod0(method, value);333}334public boolean enqueueMethodForCompilation(Executable method, int compLevel) {335return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);336}337private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);338public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {339Objects.requireNonNull(method);340return enqueueMethodForCompilation0(method, compLevel, entry_bci);341}342private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel);343public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) {344Objects.requireNonNull(aClass);345return enqueueInitializerForCompilation0(aClass, compLevel);346}347private native void clearMethodState0(Executable method);348public native void markMethodProfiled(Executable method);349public void clearMethodState(Executable method) {350Objects.requireNonNull(method);351clearMethodState0(method);352}353public native void lockCompilation();354public native void unlockCompilation();355private native int getMethodEntryBci0(Executable method);356public int getMethodEntryBci(Executable method) {357Objects.requireNonNull(method);358return getMethodEntryBci0(method);359}360private native Object[] getNMethod0(Executable method, boolean isOsr);361public Object[] getNMethod(Executable method, boolean isOsr) {362Objects.requireNonNull(method);363return getNMethod0(method, isOsr);364}365public native long allocateCodeBlob(int size, int type);366public long allocateCodeBlob(long size, int type) {367int intSize = (int) size;368if ((long) intSize != size || size < 0) {369throw new IllegalArgumentException(370"size argument has illegal value " + size);371}372return allocateCodeBlob( intSize, type);373}374public native void freeCodeBlob(long addr);375public native void forceNMethodSweep();376public native Object[] getCodeHeapEntries(int type);377public native int getCompilationActivityMode();378private native long getMethodData0(Executable method);379public long getMethodData(Executable method) {380Objects.requireNonNull(method);381return getMethodData0(method);382}383public native Object[] getCodeBlob(long addr);384385private native void clearInlineCaches0(boolean preserve_static_stubs);386public void clearInlineCaches() {387clearInlineCaches0(false);388}389public void clearInlineCaches(boolean preserve_static_stubs) {390clearInlineCaches0(preserve_static_stubs);391}392393// Intered strings394public native boolean isInStringTable(String str);395396// Memory397public native void readReservedMemory();398public native long allocateMetaspace(ClassLoader classLoader, long size);399public native long incMetaspaceCapacityUntilGC(long increment);400public native long metaspaceCapacityUntilGC();401public native long metaspaceSharedRegionAlignment();402403// Metaspace Arena Tests404public native long createMetaspaceTestContext(long commit_limit, long reserve_limit);405public native void destroyMetaspaceTestContext(long context);406public native void purgeMetaspaceTestContext(long context);407public native void printMetaspaceTestContext(long context);408public native long getTotalCommittedWordsInMetaspaceTestContext(long context);409public native long getTotalUsedWordsInMetaspaceTestContext(long context);410public native long createArenaInTestContext(long context, boolean is_micro);411public native void destroyMetaspaceTestArena(long arena);412public native long allocateFromMetaspaceTestArena(long arena, long word_size);413public native void deallocateToMetaspaceTestArena(long arena, long p, long word_size);414415public native long maxMetaspaceAllocationSize();416417// Don't use these methods directly418// Use sun.hotspot.gc.GC class instead.419public native boolean isGCSupported(int name);420public native boolean isGCSupportedByJVMCICompiler(int name);421public native boolean isGCSelected(int name);422public native boolean isGCSelectedErgonomically();423424// Force Young GC425public native void youngGC();426427// Force Full GC428public native void fullGC();429430// Returns true if the current GC supports concurrent collection control.431public native boolean supportsConcurrentGCBreakpoints();432433private void checkConcurrentGCBreakpointsSupported() {434if (!supportsConcurrentGCBreakpoints()) {435throw new UnsupportedOperationException("Concurrent GC breakpoints not supported");436}437}438439private native void concurrentGCAcquireControl0();440private native void concurrentGCReleaseControl0();441private native void concurrentGCRunToIdle0();442private native boolean concurrentGCRunTo0(String breakpoint);443444private static boolean concurrentGCIsControlled = false;445private void checkConcurrentGCIsControlled() {446if (!concurrentGCIsControlled) {447throw new IllegalStateException("Not controlling concurrent GC");448}449}450451// All collectors supporting concurrent GC breakpoints are expected452// to provide at least the following breakpoints.453public final String AFTER_MARKING_STARTED = "AFTER MARKING STARTED";454public final String BEFORE_MARKING_COMPLETED = "BEFORE MARKING COMPLETED";455456// Collectors supporting concurrent GC breakpoints that do reference457// processing concurrently should provide the following breakpoint.458public final String AFTER_CONCURRENT_REFERENCE_PROCESSING_STARTED =459"AFTER CONCURRENT REFERENCE PROCESSING STARTED";460461public void concurrentGCAcquireControl() {462checkConcurrentGCBreakpointsSupported();463if (concurrentGCIsControlled) {464throw new IllegalStateException("Already controlling concurrent GC");465}466concurrentGCAcquireControl0();467concurrentGCIsControlled = true;468}469470public void concurrentGCReleaseControl() {471checkConcurrentGCBreakpointsSupported();472concurrentGCReleaseControl0();473concurrentGCIsControlled = false;474}475476// Keep concurrent GC idle. Release from breakpoint.477public void concurrentGCRunToIdle() {478checkConcurrentGCBreakpointsSupported();479checkConcurrentGCIsControlled();480concurrentGCRunToIdle0();481}482483// Allow concurrent GC to run to breakpoint.484// Throws IllegalStateException if reached end of cycle first.485public void concurrentGCRunTo(String breakpoint) {486concurrentGCRunTo(breakpoint, true);487}488489// Allow concurrent GC to run to breakpoint.490// Returns true if reached breakpoint. If reached end of cycle first,491// then throws IllegalStateException if errorIfFail is true, returning492// false otherwise.493public boolean concurrentGCRunTo(String breakpoint, boolean errorIfFail) {494checkConcurrentGCBreakpointsSupported();495checkConcurrentGCIsControlled();496if (breakpoint == null) {497throw new NullPointerException("null breakpoint");498} else if (concurrentGCRunTo0(breakpoint)) {499return true;500} else if (errorIfFail) {501throw new IllegalStateException("Missed requested breakpoint \"" + breakpoint + "\"");502} else {503return false;504}505}506507// Method tries to start concurrent mark cycle.508// It returns false if CM Thread is always in concurrent cycle.509public native boolean g1StartConcMarkCycle();510511// Tests on ReservedSpace/VirtualSpace classes512public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);513public native void readFromNoaccessArea();514public native long getThreadStackSize();515public native long getThreadRemainingStackSize();516517// CPU features518public native String getCPUFeatures();519520// VM flags521public native boolean isConstantVMFlag(String name);522public native boolean isLockedVMFlag(String name);523public native void setBooleanVMFlag(String name, boolean value);524public native void setIntVMFlag(String name, long value);525public native void setUintVMFlag(String name, long value);526public native void setIntxVMFlag(String name, long value);527public native void setUintxVMFlag(String name, long value);528public native void setUint64VMFlag(String name, long value);529public native void setSizeTVMFlag(String name, long value);530public native void setStringVMFlag(String name, String value);531public native void setDoubleVMFlag(String name, double value);532public native Boolean getBooleanVMFlag(String name);533public native Long getIntVMFlag(String name);534public native Long getUintVMFlag(String name);535public native Long getIntxVMFlag(String name);536public native Long getUintxVMFlag(String name);537public native Long getUint64VMFlag(String name);538public native Long getSizeTVMFlag(String name);539public native String getStringVMFlag(String name);540public native Double getDoubleVMFlag(String name);541private final List<Function<String,Object>> flagsGetters = Arrays.asList(542this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,543this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,544this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);545546public Object getVMFlag(String name) {547return flagsGetters.stream()548.map(f -> f.apply(name))549.filter(x -> x != null)550.findAny()551.orElse(null);552}553554// Jigsaw555public native void DefineModule(Object module, boolean is_open, String version,556String location, Object[] packages);557public native void AddModuleExports(Object from_module, String pkg, Object to_module);558public native void AddReadsModule(Object from_module, Object source_module);559public native void AddModuleExportsToAllUnnamed(Object module, String pkg);560public native void AddModuleExportsToAll(Object module, String pkg);561562public native int getOffsetForName0(String name);563public int getOffsetForName(String name) throws Exception {564int offset = getOffsetForName0(name);565if (offset == -1) {566throw new RuntimeException(name + " not found");567}568return offset;569}570public native Boolean getMethodBooleanOption(Executable method, String name);571public native Long getMethodIntxOption(Executable method, String name);572public native Long getMethodUintxOption(Executable method, String name);573public native Double getMethodDoubleOption(Executable method, String name);574public native String getMethodStringOption(Executable method, String name);575private final List<BiFunction<Executable,String,Object>> methodOptionGetters576= Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,577this::getMethodUintxOption, this::getMethodDoubleOption,578this::getMethodStringOption);579580public Object getMethodOption(Executable method, String name) {581return methodOptionGetters.stream()582.map(f -> f.apply(method, name))583.filter(x -> x != null)584.findAny()585.orElse(null);586}587588// Sharing & archiving589public native String getDefaultArchivePath();590public native boolean cdsMemoryMappingFailed();591public native boolean isSharingEnabled();592public native boolean isShared(Object o);593public native boolean isSharedClass(Class<?> c);594public native boolean areSharedStringsIgnored();595public native boolean isCDSIncluded();596public native boolean isJFRIncluded();597public native boolean isDTraceIncluded();598public native boolean isJavaHeapArchiveSupported();599public native Object getResolvedReferences(Class<?> c);600public native void linkClass(Class<?> c);601public native boolean areOpenArchiveHeapObjectsMapped();602603// Compiler Directive604public native int addCompilerDirective(String compDirect);605public native void removeCompilerDirective(int count);606607// Handshakes608public native int handshakeWalkStack(Thread t, boolean all_threads);609public native void asyncHandshakeWalkStack(Thread t);610611public native void lockAndBlock(boolean suspender);612613// Returns true on linux if library has the noexecstack flag set.614public native boolean checkLibSpecifiesNoexecstack(String libfilename);615616// Container testing617public native boolean isContainerized();618public native int validateCgroup(String procCgroups,619String procSelfCgroup,620String procSelfMountinfo);621public native void printOsInfo();622623// Decoder624public native void disableElfSectionCache();625626// Resolved Method Table627public native long resolvedMethodItemsCount();628629// Protection Domain Table630public native int protectionDomainRemovedCount();631632public native int getKlassMetadataSize(Class<?> c);633634// ThreadSMR GC safety check for threadObj635public native void checkThreadObjOfTerminatingThread(Thread target);636637// libc name638public native String getLibcName();639640// Walk stack frames of current thread641public native void verifyFrames(boolean log, boolean updateRegisterMap);642643public native boolean isJVMTIIncluded();644645public native void waitUnsafe(int time_ms);646647public native void lockCritical();648649public native void unlockCritical();650}651652653