WhiteBox.java revision 1635:da355bc6d1f9
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  @SuppressWarnings("serial")
41  public static class WhiteBoxPermission extends BasicPermission {
42    public WhiteBoxPermission(String s) {
43      super(s);
44    }
45  }
46
47  private WhiteBox() {}
48  private static final WhiteBox instance = new WhiteBox();
49  private static native void registerNatives();
50
51  /**
52   * Returns the singleton WhiteBox instance.
53   *
54   * The returned WhiteBox object should be carefully guarded
55   * by the caller, since it can be used to read and write data
56   * at arbitrary memory addresses. It must never be passed to
57   * untrusted code.
58   */
59  public synchronized static WhiteBox getWhiteBox() {
60    SecurityManager sm = System.getSecurityManager();
61    if (sm != null) {
62      sm.checkPermission(new WhiteBoxPermission("getInstance"));
63    }
64    return instance;
65  }
66
67  static {
68    registerNatives();
69  }
70
71  // Get the maximum heap size supporting COOPs
72  public native long getCompressedOopsMaxHeapSize();
73  // Arguments
74  public native void printHeapSizes();
75
76  // Memory
77  private native long getObjectAddress0(Object o);
78  public           long getObjectAddress(Object o) {
79    Objects.requireNonNull(o);
80    return getObjectAddress0(o);
81  }
82
83  public native int  getHeapOopSize();
84  public native int  getVMPageSize();
85  public native long getVMAllocationGranularity();
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  // Determine if the compiler corresponding to the compilation level 'compLevel'
185  // and to the compilation context 'compilation_context' provides an intrinsic
186  // for the method 'method'. An intrinsic is available for method 'method' if:
187  //  - the intrinsic is enabled (by using the appropriate command-line flag) and
188  //  - the platform on which the VM is running provides the instructions necessary
189  //    for the compiler to generate the intrinsic code.
190  //
191  // The compilation context is related to using the DisableIntrinsic flag on a
192  // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp
193  // for more details.
194  public boolean isIntrinsicAvailable(Executable method,
195                                      Executable compilationContext,
196                                      int compLevel) {
197      Objects.requireNonNull(method);
198      return isIntrinsicAvailable0(method, compilationContext, compLevel);
199  }
200  // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),
201  // use the below method that does not require the compilation context as argument.
202  public boolean isIntrinsicAvailable(Executable method, int compLevel) {
203      return isIntrinsicAvailable(method, null, compLevel);
204  }
205  private native boolean isIntrinsicAvailable0(Executable method,
206                                               Executable compilationContext,
207                                               int compLevel);
208  public        int     deoptimizeMethod(Executable method) {
209    return deoptimizeMethod(method, false /*not osr*/);
210  }
211  private native int     deoptimizeMethod0(Executable method, boolean isOsr);
212  public         int     deoptimizeMethod(Executable method, boolean isOsr) {
213    Objects.requireNonNull(method);
214    return deoptimizeMethod0(method, isOsr);
215  }
216  public        void    makeMethodNotCompilable(Executable method) {
217    makeMethodNotCompilable(method, -1 /*any*/);
218  }
219  public        void    makeMethodNotCompilable(Executable method, int compLevel) {
220    makeMethodNotCompilable(method, compLevel, false /*not osr*/);
221  }
222  private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
223  public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
224    Objects.requireNonNull(method);
225    makeMethodNotCompilable0(method, compLevel, isOsr);
226  }
227  public        int     getMethodCompilationLevel(Executable method) {
228    return getMethodCompilationLevel(method, false /*not ost*/);
229  }
230  private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
231  public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
232    Objects.requireNonNull(method);
233    return getMethodCompilationLevel0(method, isOsr);
234  }
235  private native boolean testSetDontInlineMethod0(Executable method, boolean value);
236  public         boolean testSetDontInlineMethod(Executable method, boolean value) {
237    Objects.requireNonNull(method);
238    return testSetDontInlineMethod0(method, value);
239  }
240  public        int     getCompileQueuesSize() {
241    return getCompileQueueSize(-1 /*any*/);
242  }
243  public native int     getCompileQueueSize(int compLevel);
244  private native boolean testSetForceInlineMethod0(Executable method, boolean value);
245  public         boolean testSetForceInlineMethod(Executable method, boolean value) {
246    Objects.requireNonNull(method);
247    return testSetForceInlineMethod0(method, value);
248  }
249  public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
250    return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
251  }
252  private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
253  public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
254    Objects.requireNonNull(method);
255    return enqueueMethodForCompilation0(method, compLevel, entry_bci);
256  }
257  private native void    clearMethodState0(Executable method);
258  public         void    clearMethodState(Executable method) {
259    Objects.requireNonNull(method);
260    clearMethodState0(method);
261  }
262  public native void    lockCompilation();
263  public native void    unlockCompilation();
264  private native int     getMethodEntryBci0(Executable method);
265  public         int     getMethodEntryBci(Executable method) {
266    Objects.requireNonNull(method);
267    return getMethodEntryBci0(method);
268  }
269  private native Object[] getNMethod0(Executable method, boolean isOsr);
270  public         Object[] getNMethod(Executable method, boolean isOsr) {
271    Objects.requireNonNull(method);
272    return getNMethod0(method, isOsr);
273  }
274  public native long    allocateCodeBlob(int size, int type);
275  public        long    allocateCodeBlob(long size, int type) {
276      int intSize = (int) size;
277      if ((long) intSize != size || size < 0) {
278          throw new IllegalArgumentException(
279                "size argument has illegal value " + size);
280      }
281      return allocateCodeBlob( intSize, type);
282  }
283  public native void    freeCodeBlob(long addr);
284  public native void    forceNMethodSweep();
285  public native Object[] getCodeHeapEntries(int type);
286  public native int     getCompilationActivityMode();
287  public native Object[] getCodeBlob(long addr);
288
289  // Intered strings
290  public native boolean isInStringTable(String str);
291
292  // Memory
293  public native void readReservedMemory();
294  public native long allocateMetaspace(ClassLoader classLoader, long size);
295  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
296  public native long incMetaspaceCapacityUntilGC(long increment);
297  public native long metaspaceCapacityUntilGC();
298
299  // Force Young GC
300  public native void youngGC();
301
302  // Force Full GC
303  public native void fullGC();
304
305  // Method tries to start concurrent mark cycle.
306  // It returns false if CM Thread is always in concurrent cycle.
307  public native boolean g1StartConcMarkCycle();
308
309  // Tests on ReservedSpace/VirtualSpace classes
310  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
311  public native void runMemoryUnitTests();
312  public native void readFromNoaccessArea();
313  public native long getThreadStackSize();
314  public native long getThreadRemainingStackSize();
315
316  // CPU features
317  public native String getCPUFeatures();
318
319  // Native extensions
320  public native long getHeapUsageForContext(int context);
321  public native long getHeapRegionCountForContext(int context);
322  private native int getContextForObject0(Object obj);
323  public         int getContextForObject(Object obj) {
324    Objects.requireNonNull(obj);
325    return getContextForObject0(obj);
326  }
327  public native void printRegionInfo(int context);
328
329  // VM flags
330  public native boolean isConstantVMFlag(String name);
331  public native boolean isLockedVMFlag(String name);
332  public native void    setBooleanVMFlag(String name, boolean value);
333  public native void    setIntVMFlag(String name, long value);
334  public native void    setUintVMFlag(String name, long value);
335  public native void    setIntxVMFlag(String name, long value);
336  public native void    setUintxVMFlag(String name, long value);
337  public native void    setUint64VMFlag(String name, long value);
338  public native void    setSizeTVMFlag(String name, long value);
339  public native void    setStringVMFlag(String name, String value);
340  public native void    setDoubleVMFlag(String name, double value);
341  public native Boolean getBooleanVMFlag(String name);
342  public native Long    getIntVMFlag(String name);
343  public native Long    getUintVMFlag(String name);
344  public native Long    getIntxVMFlag(String name);
345  public native Long    getUintxVMFlag(String name);
346  public native Long    getUint64VMFlag(String name);
347  public native Long    getSizeTVMFlag(String name);
348  public native String  getStringVMFlag(String name);
349  public native Double  getDoubleVMFlag(String name);
350  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
351    this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
352    this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
353    this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
354
355  public Object getVMFlag(String name) {
356    return flagsGetters.stream()
357                       .map(f -> f.apply(name))
358                       .filter(x -> x != null)
359                       .findAny()
360                       .orElse(null);
361  }
362  public native int getOffsetForName0(String name);
363  public int getOffsetForName(String name) throws Exception {
364    int offset = getOffsetForName0(name);
365    if (offset == -1) {
366      throw new RuntimeException(name + " not found");
367    }
368    return offset;
369  }
370  public native Boolean getMethodBooleanOption(Executable method, String name);
371  public native Long    getMethodIntxOption(Executable method, String name);
372  public native Long    getMethodUintxOption(Executable method, String name);
373  public native Double  getMethodDoubleOption(Executable method, String name);
374  public native String  getMethodStringOption(Executable method, String name);
375  private final List<BiFunction<Executable,String,Object>> methodOptionGetters
376      = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
377          this::getMethodUintxOption, this::getMethodDoubleOption,
378          this::getMethodStringOption);
379
380  public Object getMethodOption(Executable method, String name) {
381    return methodOptionGetters.stream()
382                              .map(f -> f.apply(method, name))
383                              .filter(x -> x != null)
384                              .findAny()
385                              .orElse(null);
386  }
387
388  // Safepoint Checking
389  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
390
391  // Sharing
392  public native boolean isShared(Object o);
393  public native boolean areSharedStringsIgnored();
394}
395