WhiteBox.java revision 1284:a09f9fd80f87
1136644Sache/*
2136644Sache * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3136644Sache * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4136644Sache *
5136644Sache * This code is free software; you can redistribute it and/or modify it
6136644Sache * under the terms of the GNU General Public License version 2 only, as
7136644Sache * published by the Free Software Foundation.
8136644Sache *
9136644Sache * This code is distributed in the hope that it will be useful, but WITHOUT
10136644Sache * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11136644Sache * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12136644Sache * version 2 for more details (a copy is included in the LICENSE file that
13136644Sache * accompanied this code).
14136644Sache *
15136644Sache * You should have received a copy of the GNU General Public License version
16136644Sache * 2 along with this work; if not, write to the Free Software Foundation,
17136644Sache * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18136644Sache *
19136644Sache * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20136644Sache * or visit www.oracle.com if you need additional information or have any
21136644Sache * questions.
22136644Sache *
23136644Sache */
24136644Sache
25136644Sachepackage sun.hotspot;
26136644Sache
27136644Sacheimport java.lang.reflect.Executable;
28136644Sacheimport java.util.Arrays;
29136644Sacheimport java.util.List;
30136644Sacheimport java.util.function.Function;
31136644Sacheimport java.util.stream.Stream;
32136644Sacheimport java.security.BasicPermission;
33136644Sache
34136644Sacheimport sun.hotspot.parser.DiagnosticCommand;
35136644Sache
36136644Sachepublic class WhiteBox {
37136644Sache
38136644Sache  @SuppressWarnings("serial")
39136644Sache  public static class WhiteBoxPermission extends BasicPermission {
40136644Sache    public WhiteBoxPermission(String s) {
41136644Sache      super(s);
42136644Sache    }
43136644Sache  }
44136644Sache
45136644Sache  private WhiteBox() {}
46136644Sache  private static final WhiteBox instance = new WhiteBox();
47136644Sache  private static native void registerNatives();
48136644Sache
49136644Sache  /**
50136644Sache   * Returns the singleton WhiteBox instance.
51136644Sache   *
52136644Sache   * The returned WhiteBox object should be carefully guarded
53136644Sache   * by the caller, since it can be used to read and write data
54136644Sache   * at arbitrary memory addresses. It must never be passed to
55136644Sache   * untrusted code.
56136644Sache   */
57136644Sache  public synchronized static WhiteBox getWhiteBox() {
58136644Sache    SecurityManager sm = System.getSecurityManager();
59136644Sache    if (sm != null) {
60136644Sache      sm.checkPermission(new WhiteBoxPermission("getInstance"));
61136644Sache    }
62136644Sache    return instance;
63136644Sache  }
64136644Sache
65136644Sache  static {
66136644Sache    registerNatives();
67136644Sache  }
68136644Sache
69136644Sache  // Get the maximum heap size supporting COOPs
70136644Sache  public native long getCompressedOopsMaxHeapSize();
71136644Sache  // Arguments
72136644Sache  public native void printHeapSizes();
73136644Sache
74136644Sache  // Memory
75136644Sache  public native long getObjectAddress(Object o);
76136644Sache  public native int  getHeapOopSize();
77136644Sache  public native int  getVMPageSize();
78136644Sache  public native boolean isObjectInOldGen(Object o);
79136644Sache  public native long getObjectSize(Object o);
80136644Sache
81136644Sache  // Runtime
82136644Sache  // Make sure class name is in the correct format
83136644Sache  public boolean isClassAlive(String name) {
84136644Sache    return isClassAlive0(name.replace('.', '/'));
85136644Sache  }
86136644Sache  private native boolean isClassAlive0(String name);
87136644Sache  public native boolean isMonitorInflated(Object obj);
88136644Sache  public native void forceSafepoint();
89136644Sache
90136644Sache  // JVMTI
91136644Sache  public native void addToBootstrapClassLoaderSearch(String segment);
92136644Sache  public native void addToSystemClassLoaderSearch(String segment);
93136644Sache
94136644Sache  // G1
95136644Sache  public native boolean g1InConcurrentMark();
96136644Sache  public native boolean g1IsHumongous(Object o);
97136644Sache  public native long    g1NumFreeRegions();
98136644Sache  public native int     g1RegionSize();
99136644Sache  public native Object[]    parseCommandLine(String commandline, char delim, DiagnosticCommand[] args);
100136644Sache
101136644Sache  // NMT
102136644Sache  public native long NMTMalloc(long size);
103136644Sache  public native void NMTFree(long mem);
104136644Sache  public native long NMTReserveMemory(long size);
105136644Sache  public native void NMTCommitMemory(long addr, long size);
106136644Sache  public native void NMTUncommitMemory(long addr, long size);
107136644Sache  public native void NMTReleaseMemory(long addr, long size);
108136644Sache  public native long NMTMallocWithPseudoStack(long size, int index);
109136644Sache  public native boolean NMTIsDetailSupported();
110136644Sache  public native boolean NMTChangeTrackingLevel();
111136644Sache  public native int NMTGetHashSize();
112136644Sache
113136644Sache  // Compiler
114136644Sache  public native int     deoptimizeFrames(boolean makeNotEntrant);
115136644Sache  public native void    deoptimizeAll();
116136644Sache  public        boolean isMethodCompiled(Executable method) {
117136644Sache    return isMethodCompiled(method, false /*not osr*/);
118136644Sache  }
119136644Sache  public native boolean isMethodCompiled(Executable method, boolean isOsr);
120136644Sache  public        boolean isMethodCompilable(Executable method) {
121136644Sache    return isMethodCompilable(method, -1 /*any*/);
122136644Sache  }
123136644Sache  public        boolean isMethodCompilable(Executable method, int compLevel) {
124136644Sache    return isMethodCompilable(method, compLevel, false /*not osr*/);
125136644Sache  }
126136644Sache  public native boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr);
127136644Sache  public native boolean isMethodQueuedForCompilation(Executable method);
128136644Sache  public        int     deoptimizeMethod(Executable method) {
129136644Sache    return deoptimizeMethod(method, false /*not osr*/);
130136644Sache  }
131136644Sache  public native int     deoptimizeMethod(Executable method, boolean isOsr);
132136644Sache  public        void    makeMethodNotCompilable(Executable method) {
133136644Sache    makeMethodNotCompilable(method, -1 /*any*/);
134136644Sache  }
135136644Sache  public        void    makeMethodNotCompilable(Executable method, int compLevel) {
136136644Sache    makeMethodNotCompilable(method, compLevel, false /*not osr*/);
137136644Sache  }
138136644Sache  public native void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr);
139136644Sache  public        int     getMethodCompilationLevel(Executable method) {
140136644Sache    return getMethodCompilationLevel(method, false /*not ost*/);
141136644Sache  }
142136644Sache  public native int     getMethodCompilationLevel(Executable method, boolean isOsr);
143136644Sache  public native boolean testSetDontInlineMethod(Executable method, boolean value);
144136644Sache  public        int     getCompileQueuesSize() {
145136644Sache    return getCompileQueueSize(-1 /*any*/);
146136644Sache  }
147136644Sache  public native int     getCompileQueueSize(int compLevel);
148136644Sache  public native boolean testSetForceInlineMethod(Executable method, boolean value);
149136644Sache  public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
150136644Sache    return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
151136644Sache  }
152136644Sache  public native boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci);
153136644Sache  public native void    clearMethodState(Executable method);
154136644Sache  public native void    lockCompilation();
155136644Sache  public native void    unlockCompilation();
156136644Sache  public native int     getMethodEntryBci(Executable method);
157136644Sache  public native Object[] getNMethod(Executable method, boolean isOsr);
158136644Sache  public native long    allocateCodeBlob(int size, int type);
159136644Sache  public        long    allocateCodeBlob(long size, int type) {
160136644Sache      int intSize = (int) size;
161136644Sache      if ((long) intSize != size || size < 0) {
162136644Sache          throw new IllegalArgumentException(
163136644Sache                "size argument has illegal value " + size);
164136644Sache      }
165136644Sache      return allocateCodeBlob( intSize, type);
166136644Sache  }
167136644Sache  public native void    freeCodeBlob(long addr);
168136644Sache  public        void    forceNMethodSweep() {
169136644Sache    try {
170136644Sache        forceNMethodSweep0().join();
171136644Sache    } catch (InterruptedException e) {
172136644Sache        Thread.currentThread().interrupt();
173136644Sache    }
174136644Sache  }
175136644Sache  public native Thread  forceNMethodSweep0();
176136644Sache  public native Object[] getCodeHeapEntries(int type);
177136644Sache  public native int     getCompilationActivityMode();
178136644Sache  public native Object[] getCodeBlob(long addr);
179136644Sache
180136644Sache  // Intered strings
181136644Sache  public native boolean isInStringTable(String str);
182136644Sache
183136644Sache  // Memory
184136644Sache  public native void readReservedMemory();
185136644Sache  public native long allocateMetaspace(ClassLoader classLoader, long size);
186136644Sache  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
187136644Sache  public native long incMetaspaceCapacityUntilGC(long increment);
188136644Sache  public native long metaspaceCapacityUntilGC();
189136644Sache
190136644Sache  // Force Young GC
191136644Sache  public native void youngGC();
192136644Sache
193136644Sache  // Force Full GC
194136644Sache  public native void fullGC();
195136644Sache
196136644Sache  // Method tries to start concurrent mark cycle.
197136644Sache  // It returns false if CM Thread is always in concurrent cycle.
198136644Sache  public native boolean g1StartConcMarkCycle();
199136644Sache
200136644Sache  // Tests on ReservedSpace/VirtualSpace classes
201136644Sache  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
202136644Sache  public native void runMemoryUnitTests();
203136644Sache  public native void readFromNoaccessArea();
204136644Sache  public native long getThreadStackSize();
205136644Sache  public native long getThreadRemainingStackSize();
206136644Sache
207136644Sache  // CPU features
208136644Sache  public native String getCPUFeatures();
209136644Sache
210136644Sache  // Native extensions
211136644Sache  public native long getHeapUsageForContext(int context);
212136644Sache  public native long getHeapRegionCountForContext(int context);
213136644Sache  public native int getContextForObject(Object obj);
214136644Sache  public native void printRegionInfo(int context);
215136644Sache
216136644Sache  // VM flags
217136644Sache  public native boolean isConstantVMFlag(String name);
218136644Sache  public native boolean isLockedVMFlag(String name);
219136644Sache  public native void    setBooleanVMFlag(String name, boolean value);
220136644Sache  public native void    setIntxVMFlag(String name, long value);
221136644Sache  public native void    setUintxVMFlag(String name, long value);
222136644Sache  public native void    setUint64VMFlag(String name, long value);
223136644Sache  public native void    setSizeTVMFlag(String name, long value);
224136644Sache  public native void    setStringVMFlag(String name, String value);
225136644Sache  public native void    setDoubleVMFlag(String name, double value);
226136644Sache  public native Boolean getBooleanVMFlag(String name);
227136644Sache  public native Long    getIntxVMFlag(String name);
228136644Sache  public native Long    getUintxVMFlag(String name);
229136644Sache  public native Long    getUint64VMFlag(String name);
230136644Sache  public native Long    getSizeTVMFlag(String name);
231136644Sache  public native String  getStringVMFlag(String name);
232136644Sache  public native Double  getDoubleVMFlag(String name);
233136644Sache  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
234136644Sache    this::getBooleanVMFlag, this::getIntxVMFlag, this::getUintxVMFlag,
235136644Sache    this::getUint64VMFlag, this::getSizeTVMFlag, this::getStringVMFlag,
236136644Sache    this::getDoubleVMFlag);
237136644Sache
238136644Sache  public Object getVMFlag(String name) {
239136644Sache    return flagsGetters.stream()
240136644Sache                       .map(f -> f.apply(name))
241136644Sache                       .filter(x -> x != null)
242136644Sache                       .findAny()
243136644Sache                       .orElse(null);
244136644Sache  }
245136644Sache  public native int getOffsetForName0(String name);
246136644Sache  public int getOffsetForName(String name) throws Exception {
247136644Sache    int offset = getOffsetForName0(name);
248136644Sache    if (offset == -1) {
249136644Sache      throw new RuntimeException(name + " not found");
250136644Sache    }
251136644Sache    return offset;
252136644Sache  }
253136644Sache
254136644Sache  // Safepoint Checking
255136644Sache  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
256136644Sache}
257136644Sache