Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/sun/hotspot/WhiteBox.java
38838 views
/*1* Copyright (c) 2012, 2018, 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;33import java.net.URL;3435import sun.hotspot.parser.DiagnosticCommand;3637public 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() {58SecurityManager sm = System.getSecurityManager();59if (sm != null) {60sm.checkPermission(new WhiteBoxPermission("getInstance"));61}62return instance;63}6465static {66registerNatives();67}6869// Get the maximum heap size supporting COOPs70public native long getCompressedOopsMaxHeapSize();71// Arguments72public native void printHeapSizes();7374// Memory75public native long getObjectAddress(Object o);76public native int getHeapOopSize();77public native int getVMPageSize();78public native long getVMAllocationGranularity();79public native long getVMLargePageSize();80public native long getHeapSpaceAlignment();81public native long getHeapAlignment();8283public native boolean isObjectInOldGen(Object o);84public native long getObjectSize(Object o);8586public native boolean classKnownToNotExist(ClassLoader loader, String name);87public native URL[] getLookupCacheURLs(ClassLoader loader);88public native int[] getLookupCacheMatches(ClassLoader loader, String name);8990// Runtime91// Make sure class name is in the correct format92public boolean isClassAlive(String name) {93return isClassAlive0(name.replace('.', '/'));94}95private native boolean isClassAlive0(String name);9697public native boolean isMonitorInflated(Object obj);9899public native void forceSafepoint();100101private native long getConstantPool0(Class<?> aClass);102public long getConstantPool(Class<?> aClass) {103Objects.requireNonNull(aClass);104return getConstantPool0(aClass);105}106107private native int getConstantPoolCacheIndexTag0();108public int getConstantPoolCacheIndexTag() {109return getConstantPoolCacheIndexTag0();110}111112private native int getConstantPoolCacheLength0(Class<?> aClass);113public int getConstantPoolCacheLength(Class<?> aClass) {114Objects.requireNonNull(aClass);115return getConstantPoolCacheLength0(aClass);116}117118private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index);119public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) {120Objects.requireNonNull(aClass);121return remapInstructionOperandFromCPCache0(aClass, index);122}123124private native int encodeConstantPoolIndyIndex0(int index);125public int encodeConstantPoolIndyIndex(int index) {126return encodeConstantPoolIndyIndex0(index);127}128129// JVMTI130public native void addToBootstrapClassLoaderSearch(String segment);131public native void addToSystemClassLoaderSearch(String segment);132133// G1134public native boolean g1InConcurrentMark();135public native boolean g1IsHumongous(Object o);136public native boolean g1BelongsToHumongousRegion(long adr);137public native boolean g1BelongsToFreeRegion(long adr);138public native long g1NumMaxRegions();139public native long g1NumFreeRegions();140public native int g1RegionSize();141public native MemoryUsage g1AuxiliaryMemoryUsage();142public native Object[] parseCommandLine(String commandline, DiagnosticCommand[] args);143144// Parallel GC145public native long psVirtualSpaceAlignment();146public native long psHeapGenerationAlignment();147148/**149* Enumerates old regions with liveness less than specified and produces some statistics150* @param liveness percent of region's liveness (live_objects / total_region_size * 100).151* @return long[3] array where long[0] - total count of old regions152* long[1] - total memory of old regions153* long[2] - lowest estimation of total memory of old regions to be freed (non-full154* regions are not included)155*/156public native long[] g1GetMixedGCInfo(int liveness);157158// NMT159public native long NMTMalloc(long size);160public native void NMTFree(long mem);161public native long NMTReserveMemory(long size);162public native long NMTAttemptReserveMemoryAt(long addr, long size);163public native void NMTCommitMemory(long addr, long size);164public native void NMTUncommitMemory(long addr, long size);165public native void NMTReleaseMemory(long addr, long size);166public native long NMTMallocWithPseudoStack(long size, int index);167public native long NMTMallocWithPseudoStackAndType(long size, int index, int type);168public native boolean NMTIsDetailSupported();169public native boolean NMTChangeTrackingLevel();170public native int NMTGetHashSize();171172// Compiler173public native int matchesMethod(Executable method, String pattern);174public native int matchesInline(Executable method, String pattern);175public native boolean shouldPrintAssembly(Executable method, int comp_level);176public native int deoptimizeFrames(boolean makeNotEntrant);177public native void deoptimizeAll();178179public boolean isMethodCompiled(Executable method) {180return isMethodCompiled(method, false /*not osr*/);181}182public native boolean isMethodCompiled(Executable method, boolean isOsr);183public boolean isMethodCompilable(Executable method) {184return isMethodCompilable(method, -2 /*any*/);185}186public boolean isMethodCompilable(Executable method, int compLevel) {187return isMethodCompilable(method, compLevel, false /*not osr*/);188}189public native boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr);190191public native boolean isMethodQueuedForCompilation(Executable method);192193// Determine if the compiler corresponding to the compilation level 'compLevel'194// and to the compilation context 'compilation_context' provides an intrinsic195// for the method 'method'. An intrinsic is available for method 'method' if:196// - the intrinsic is enabled (by using the appropriate command-line flag) and197// - the platform on which the VM is running provides the instructions necessary198// for the compiler to generate the intrinsic code.199//200// The compilation context is related to using the DisableIntrinsic flag on a201// per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp202// for more details.203public boolean isIntrinsicAvailable(Executable method,204Executable compilationContext,205int compLevel) {206Objects.requireNonNull(method);207return isIntrinsicAvailable0(method, compilationContext, compLevel);208}209// If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),210// use the below method that does not require the compilation context as argument.211public boolean isIntrinsicAvailable(Executable method, int compLevel) {212return isIntrinsicAvailable(method, null, compLevel);213}214private native boolean isIntrinsicAvailable0(Executable method,215Executable compilationContext,216int compLevel);217public int deoptimizeMethod(Executable method) {218return deoptimizeMethod(method, false /*not osr*/);219}220public native int deoptimizeMethod(Executable method, boolean isOsr);221public void makeMethodNotCompilable(Executable method) {222makeMethodNotCompilable(method, -2 /*any*/);223}224public void makeMethodNotCompilable(Executable method, int compLevel) {225makeMethodNotCompilable(method, compLevel, false /*not osr*/);226}227public native void makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr);228public int getMethodCompilationLevel(Executable method) {229return getMethodCompilationLevel(method, false /*not ost*/);230}231public native int getMethodCompilationLevel(Executable method, boolean isOsr);232public native boolean testSetDontInlineMethod(Executable method, boolean value);233public int getCompileQueuesSize() {234return getCompileQueueSize(-2 /*any*/);235}236public native int getCompileQueueSize(int compLevel);237public native boolean testSetForceInlineMethod(Executable method, boolean value);238239public boolean enqueueMethodForCompilation(Executable method, int compLevel) {240return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);241}242private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);243public boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {244Objects.requireNonNull(method);245return enqueueMethodForCompilation0(method, compLevel, entry_bci);246}247private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel);248public boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) {249Objects.requireNonNull(aClass);250return enqueueInitializerForCompilation0(aClass, compLevel);251}252public native void clearMethodState(Executable method);253public native void markMethodProfiled(Executable method);254public native void lockCompilation();255public native void unlockCompilation();256public native int getMethodEntryBci(Executable method);257public native Object[] getNMethod(Executable method, boolean isOsr);258public native long allocateCodeBlob(int size, int type);259public long allocateCodeBlob(long size, int type) {260int intSize = (int) size;261if ((long) intSize != size || size < 0) {262throw new IllegalArgumentException(263"size argument has illegal value " + size);264}265return allocateCodeBlob( intSize, type);266}267public native void freeCodeBlob(long addr);268public native Object[] getCodeHeapEntries(int type);269public native int getCompilationActivityMode();270private native long getMethodData0(Executable method);271public long getMethodData(Executable method) {272Objects.requireNonNull(method);273return getMethodData0(method);274}275public native Object[] getCodeBlob(long addr);276277private native void clearInlineCaches0(boolean preserve_static_stubs);278public void clearInlineCaches() {279clearInlineCaches0(false);280}281public void clearInlineCaches(boolean preserve_static_stubs) {282clearInlineCaches0(preserve_static_stubs);283}284285// Intered strings286public native boolean isInStringTable(String str);287288// Memory289public native void readReservedMemory();290public native long allocateMetaspace(ClassLoader classLoader, long size);291public native void freeMetaspace(ClassLoader classLoader, long addr, long size);292public native long incMetaspaceCapacityUntilGC(long increment);293public native long metaspaceCapacityUntilGC();294public native boolean metaspaceShouldConcurrentCollect();295public native long metaspaceReserveAlignment();296297// Don't use these methods directly298// Use sun.hotspot.gc.GC class instead.299public native boolean isGCSupported(int name);300public native boolean isGCSelected(int name);301public native boolean isGCSelectedErgonomically();302303// Force Young GC304public native void youngGC();305306// Force Full GC307public native void fullGC();308309// Returns true if the current GC supports control of its concurrent310// phase via requestConcurrentGCPhase(). If false, a request will311// always fail.312public native boolean supportsConcurrentGCPhaseControl();313314// Returns an array of concurrent phase names provided by this315// collector. These are the names recognized by316// requestConcurrentGCPhase().317public native String[] getConcurrentGCPhases();318319// Attempt to put the collector into the indicated concurrent phase,320// and attempt to remain in that state until a new request is made.321//322// Returns immediately if already in the requested phase.323// Otherwise, waits until the phase is reached.324//325// Throws IllegalStateException if unsupported by the current collector.326// Throws NullPointerException if phase is null.327// Throws IllegalArgumentException if phase is not valid for the current collector.328public void requestConcurrentGCPhase(String phase) {329if (!supportsConcurrentGCPhaseControl()) {330throw new IllegalStateException("Concurrent GC phase control not supported");331} else if (phase == null) {332throw new NullPointerException("null phase");333} else if (!requestConcurrentGCPhase0(phase)) {334throw new IllegalArgumentException("Unknown concurrent GC phase: " + phase);335}336}337338// Helper for requestConcurrentGCPhase(). Returns true if request339// succeeded, false if the phase is invalid.340private native boolean requestConcurrentGCPhase0(String phase);341342// Method tries to start concurrent mark cycle.343// It returns false if CM Thread is always in concurrent cycle.344public native boolean g1StartConcMarkCycle();345346// Tests on ReservedSpace/VirtualSpace classes347public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);348public native void runMemoryUnitTests();349public native void readFromNoaccessArea();350public native long getThreadStackSize();351public native long getThreadRemainingStackSize();352353// CPU features354public native String getCPUFeatures();355356// VM flags357public native boolean isConstantVMFlag(String name);358public native boolean isLockedVMFlag(String name);359public native void setBooleanVMFlag(String name, boolean value);360public native void setIntVMFlag(String name, long value);361public native void setUintVMFlag(String name, long value);362public native void setIntxVMFlag(String name, long value);363public native void setUintxVMFlag(String name, long value);364public native void setUint64VMFlag(String name, long value);365public native void setSizeTVMFlag(String name, long value);366public native void setStringVMFlag(String name, String value);367public native void setDoubleVMFlag(String name, double value);368public native Boolean getBooleanVMFlag(String name);369public native Long getIntVMFlag(String name);370public native Long getUintVMFlag(String name);371public native Long getIntxVMFlag(String name);372public native Long getUintxVMFlag(String name);373public native Long getUint64VMFlag(String name);374public native Long getSizeTVMFlag(String name);375public native String getStringVMFlag(String name);376public native Double getDoubleVMFlag(String name);377private final List<Function<String,Object>> flagsGetters = Arrays.asList(378this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,379this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,380this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);381382public Object getVMFlag(String name) {383return flagsGetters.stream()384.map(f -> f.apply(name))385.filter(x -> x != null)386.findAny()387.orElse(null);388}389390// Jigsaw391public native void DefineModule(Object module, boolean is_open, String version,392String location, Object[] packages);393public native void AddModuleExports(Object from_module, String pkg, Object to_module);394public native void AddReadsModule(Object from_module, Object source_module);395public native void AddModuleExportsToAllUnnamed(Object module, String pkg);396public native void AddModuleExportsToAll(Object module, String pkg);397398public native int getOffsetForName0(String name);399public int getOffsetForName(String name) throws Exception {400int offset = getOffsetForName0(name);401if (offset == -1) {402throw new RuntimeException(name + " not found");403}404return offset;405}406public native Boolean getMethodBooleanOption(Executable method, String name);407public native Long getMethodIntxOption(Executable method, String name);408public native Long getMethodUintxOption(Executable method, String name);409public native Double getMethodDoubleOption(Executable method, String name);410public native String getMethodStringOption(Executable method, String name);411private final List<BiFunction<Executable,String,Object>> methodOptionGetters412= Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,413this::getMethodUintxOption, this::getMethodDoubleOption,414this::getMethodStringOption);415416public Object getMethodOption(Executable method, String name) {417return methodOptionGetters.stream()418.map(f -> f.apply(method, name))419.filter(x -> x != null)420.findAny()421.orElse(null);422}423424// Safepoint Checking425public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);426427// Sharing & archiving428public native boolean isShared(Object o);429public native boolean isSharedClass(Class<?> c);430public native boolean areSharedStringsIgnored();431public native boolean isCDSIncludedInVmBuild();432public native boolean isJFRIncludedInVmBuild();433public native boolean isJavaHeapArchiveSupported();434public native Object getResolvedReferences(Class<?> c);435public native boolean areOpenArchiveHeapObjectsMapped();436437// Handshakes438public native int handshakeWalkStack(Thread t, boolean all_threads);439440// Returns true on linux if library has the noexecstack flag set.441public native boolean checkLibSpecifiesNoexecstack(String libfilename);442443// Container testing444public native boolean isContainerized();445public native void printOsInfo();446447// Decoder448public native void disableElfSectionCache();449}450451452