WhiteBox.java revision 1459:a264f2988733
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  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        void    forceNMethodSweep() {
261    try {
262        forceNMethodSweep0().join();
263    } catch (InterruptedException e) {
264        Thread.currentThread().interrupt();
265    }
266  }
267  public native Thread  forceNMethodSweep0();
268  public native Object[] getCodeHeapEntries(int type);
269  public native int     getCompilationActivityMode();
270  public native Object[] getCodeBlob(long addr);
271
272  // Intered strings
273  public native boolean isInStringTable(String str);
274
275  // Memory
276  public native void readReservedMemory();
277  public native long allocateMetaspace(ClassLoader classLoader, long size);
278  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
279  public native long incMetaspaceCapacityUntilGC(long increment);
280  public native long metaspaceCapacityUntilGC();
281
282  // Force Young GC
283  public native void youngGC();
284
285  // Force Full GC
286  public native void fullGC();
287
288  // Method tries to start concurrent mark cycle.
289  // It returns false if CM Thread is always in concurrent cycle.
290  public native boolean g1StartConcMarkCycle();
291
292  // Tests on ReservedSpace/VirtualSpace classes
293  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
294  public native void runMemoryUnitTests();
295  public native void readFromNoaccessArea();
296  public native long getThreadStackSize();
297  public native long getThreadRemainingStackSize();
298
299  // CPU features
300  public native String getCPUFeatures();
301
302  // Native extensions
303  public native long getHeapUsageForContext(int context);
304  public native long getHeapRegionCountForContext(int context);
305  private native int getContextForObject0(Object obj);
306  public         int getContextForObject(Object obj) {
307    Objects.requireNonNull(obj);
308    return getContextForObject0(obj);
309  }
310  public native void printRegionInfo(int context);
311
312  // VM flags
313  public native boolean isConstantVMFlag(String name);
314  public native boolean isLockedVMFlag(String name);
315  public native void    setBooleanVMFlag(String name, boolean value);
316  public native void    setIntxVMFlag(String name, long value);
317  public native void    setUintxVMFlag(String name, long value);
318  public native void    setUint64VMFlag(String name, long value);
319  public native void    setSizeTVMFlag(String name, long value);
320  public native void    setStringVMFlag(String name, String value);
321  public native void    setDoubleVMFlag(String name, double value);
322  public native Boolean getBooleanVMFlag(String name);
323  public native Long    getIntxVMFlag(String name);
324  public native Long    getUintxVMFlag(String name);
325  public native Long    getUint64VMFlag(String name);
326  public native Long    getSizeTVMFlag(String name);
327  public native String  getStringVMFlag(String name);
328  public native Double  getDoubleVMFlag(String name);
329  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
330    this::getBooleanVMFlag, this::getIntxVMFlag, this::getUintxVMFlag,
331    this::getUint64VMFlag, this::getSizeTVMFlag, this::getStringVMFlag,
332    this::getDoubleVMFlag);
333
334  public Object getVMFlag(String name) {
335    return flagsGetters.stream()
336                       .map(f -> f.apply(name))
337                       .filter(x -> x != null)
338                       .findAny()
339                       .orElse(null);
340  }
341  public native int getOffsetForName0(String name);
342  public int getOffsetForName(String name) throws Exception {
343    int offset = getOffsetForName0(name);
344    if (offset == -1) {
345      throw new RuntimeException(name + " not found");
346    }
347    return offset;
348  }
349  public native Boolean getMethodBooleanOption(Executable method, String name);
350  public native Long    getMethodIntxOption(Executable method, String name);
351  public native Long    getMethodUintxOption(Executable method, String name);
352  public native Double  getMethodDoubleOption(Executable method, String name);
353  public native String  getMethodStringOption(Executable method, String name);
354  private final List<BiFunction<Executable,String,Object>> methodOptionGetters
355      = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
356          this::getMethodUintxOption, this::getMethodDoubleOption,
357          this::getMethodStringOption);
358
359  public Object getMethodOption(Executable method, String name) {
360    return methodOptionGetters.stream()
361                              .map(f -> f.apply(method, name))
362                              .filter(x -> x != null)
363                              .findAny()
364                              .orElse(null);
365  }
366
367  // Safepoint Checking
368  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
369}
370