WhiteBox.java revision 1813:ec0e9c395cc0
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.security.BasicPermission;
34import java.util.Objects;
35import jdk.internal.HotSpotIntrinsicCandidate;
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  public native long getHeapSpaceAlignment();
88
89  private native boolean isObjectInOldGen0(Object o);
90  public         boolean isObjectInOldGen(Object o) {
91    Objects.requireNonNull(o);
92    return isObjectInOldGen0(o);
93  }
94
95  private native long getObjectSize0(Object o);
96  public         long getObjectSize(Object o) {
97    Objects.requireNonNull(o);
98    return getObjectSize0(o);
99  }
100
101  // Runtime
102  // Make sure class name is in the correct format
103  public boolean isClassAlive(String name) {
104    return isClassAlive0(name.replace('.', '/'));
105  }
106  private native boolean isClassAlive0(String name);
107
108  private native boolean isMonitorInflated0(Object obj);
109  public         boolean isMonitorInflated(Object obj) {
110    Objects.requireNonNull(obj);
111    return isMonitorInflated0(obj);
112  }
113
114  public native void forceSafepoint();
115
116  private native long getConstantPool0(Class<?> aClass);
117  public         long getConstantPool(Class<?> aClass) {
118    Objects.requireNonNull(aClass);
119    return getConstantPool0(aClass);
120  }
121
122  // JVMTI
123  private native void addToBootstrapClassLoaderSearch0(String segment);
124  public         void addToBootstrapClassLoaderSearch(String segment){
125    Objects.requireNonNull(segment);
126    addToBootstrapClassLoaderSearch0(segment);
127  }
128
129  private native void addToSystemClassLoaderSearch0(String segment);
130  public         void addToSystemClassLoaderSearch(String segment) {
131    Objects.requireNonNull(segment);
132    addToSystemClassLoaderSearch0(segment);
133  }
134
135  // G1
136  public native boolean g1InConcurrentMark();
137  private native boolean g1IsHumongous0(Object o);
138  public         boolean g1IsHumongous(Object o) {
139    Objects.requireNonNull(o);
140    return g1IsHumongous0(o);
141  }
142
143  public native long    g1NumMaxRegions();
144  public native long    g1NumFreeRegions();
145  public native int     g1RegionSize();
146  public native MemoryUsage g1AuxiliaryMemoryUsage();
147  private  native Object[]    parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args);
148  public          Object[]    parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) {
149    Objects.requireNonNull(args);
150    return parseCommandLine0(commandline, delim, args);
151  }
152
153  // Parallel GC
154  public native long psVirtualSpaceAlignment();
155  public native long psHeapGenerationAlignment();
156
157  // NMT
158  public native long NMTMalloc(long size);
159  public native void NMTFree(long mem);
160  public native long NMTReserveMemory(long size);
161  public native void NMTCommitMemory(long addr, long size);
162  public native void NMTUncommitMemory(long addr, long size);
163  public native void NMTReleaseMemory(long addr, long size);
164  public native long NMTMallocWithPseudoStack(long size, int index);
165  public native boolean NMTChangeTrackingLevel();
166  public native int NMTGetHashSize();
167
168  // Compiler
169  public native int     matchesMethod(Executable method, String pattern);
170  public native int     matchesInline(Executable method, String pattern);
171  public native boolean shouldPrintAssembly(Executable method);
172  public native int     deoptimizeFrames(boolean makeNotEntrant);
173  public native void    deoptimizeAll();
174
175  @HotSpotIntrinsicCandidate
176  public        void    deoptimize() {}
177  public        boolean isMethodCompiled(Executable method) {
178    return isMethodCompiled(method, false /*not osr*/);
179  }
180  private native boolean isMethodCompiled0(Executable method, boolean isOsr);
181  public         boolean isMethodCompiled(Executable method, boolean isOsr){
182    Objects.requireNonNull(method);
183    return isMethodCompiled0(method, isOsr);
184  }
185  public        boolean isMethodCompilable(Executable method) {
186    return isMethodCompilable(method, -1 /*any*/);
187  }
188  public        boolean isMethodCompilable(Executable method, int compLevel) {
189    return isMethodCompilable(method, compLevel, false /*not osr*/);
190  }
191  private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
192  public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
193    Objects.requireNonNull(method);
194    return isMethodCompilable0(method, compLevel, isOsr);
195  }
196  private native boolean isMethodQueuedForCompilation0(Executable method);
197  public         boolean isMethodQueuedForCompilation(Executable method) {
198    Objects.requireNonNull(method);
199    return isMethodQueuedForCompilation0(method);
200  }
201  // Determine if the compiler corresponding to the compilation level 'compLevel'
202  // and to the compilation context 'compilation_context' provides an intrinsic
203  // for the method 'method'. An intrinsic is available for method 'method' if:
204  //  - the intrinsic is enabled (by using the appropriate command-line flag) and
205  //  - the platform on which the VM is running provides the instructions necessary
206  //    for the compiler to generate the intrinsic code.
207  //
208  // The compilation context is related to using the DisableIntrinsic flag on a
209  // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp
210  // for more details.
211  public boolean isIntrinsicAvailable(Executable method,
212                                      Executable compilationContext,
213                                      int compLevel) {
214      Objects.requireNonNull(method);
215      return isIntrinsicAvailable0(method, compilationContext, compLevel);
216  }
217  // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),
218  // use the below method that does not require the compilation context as argument.
219  public boolean isIntrinsicAvailable(Executable method, int compLevel) {
220      return isIntrinsicAvailable(method, null, compLevel);
221  }
222  private native boolean isIntrinsicAvailable0(Executable method,
223                                               Executable compilationContext,
224                                               int compLevel);
225  public        int     deoptimizeMethod(Executable method) {
226    return deoptimizeMethod(method, false /*not osr*/);
227  }
228  private native int     deoptimizeMethod0(Executable method, boolean isOsr);
229  public         int     deoptimizeMethod(Executable method, boolean isOsr) {
230    Objects.requireNonNull(method);
231    return deoptimizeMethod0(method, isOsr);
232  }
233  public        void    makeMethodNotCompilable(Executable method) {
234    makeMethodNotCompilable(method, -1 /*any*/);
235  }
236  public        void    makeMethodNotCompilable(Executable method, int compLevel) {
237    makeMethodNotCompilable(method, compLevel, false /*not osr*/);
238  }
239  private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
240  public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
241    Objects.requireNonNull(method);
242    makeMethodNotCompilable0(method, compLevel, isOsr);
243  }
244  public        int     getMethodCompilationLevel(Executable method) {
245    return getMethodCompilationLevel(method, false /*not ost*/);
246  }
247  private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
248  public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
249    Objects.requireNonNull(method);
250    return getMethodCompilationLevel0(method, isOsr);
251  }
252  private native boolean testSetDontInlineMethod0(Executable method, boolean value);
253  public         boolean testSetDontInlineMethod(Executable method, boolean value) {
254    Objects.requireNonNull(method);
255    return testSetDontInlineMethod0(method, value);
256  }
257  public        int     getCompileQueuesSize() {
258    return getCompileQueueSize(-1 /*any*/);
259  }
260  public native int     getCompileQueueSize(int compLevel);
261  private native boolean testSetForceInlineMethod0(Executable method, boolean value);
262  public         boolean testSetForceInlineMethod(Executable method, boolean value) {
263    Objects.requireNonNull(method);
264    return testSetForceInlineMethod0(method, value);
265  }
266  public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
267    return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
268  }
269  private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
270  public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
271    Objects.requireNonNull(method);
272    return enqueueMethodForCompilation0(method, compLevel, entry_bci);
273  }
274  private native void    clearMethodState0(Executable method);
275  public         void    clearMethodState(Executable method) {
276    Objects.requireNonNull(method);
277    clearMethodState0(method);
278  }
279  public native void    lockCompilation();
280  public native void    unlockCompilation();
281  private native int     getMethodEntryBci0(Executable method);
282  public         int     getMethodEntryBci(Executable method) {
283    Objects.requireNonNull(method);
284    return getMethodEntryBci0(method);
285  }
286  private native Object[] getNMethod0(Executable method, boolean isOsr);
287  public         Object[] getNMethod(Executable method, boolean isOsr) {
288    Objects.requireNonNull(method);
289    return getNMethod0(method, isOsr);
290  }
291  public native long    allocateCodeBlob(int size, int type);
292  public        long    allocateCodeBlob(long size, int type) {
293      int intSize = (int) size;
294      if ((long) intSize != size || size < 0) {
295          throw new IllegalArgumentException(
296                "size argument has illegal value " + size);
297      }
298      return allocateCodeBlob( intSize, type);
299  }
300  public native void    freeCodeBlob(long addr);
301  public native void    forceNMethodSweep();
302  public native Object[] getCodeHeapEntries(int type);
303  public native int     getCompilationActivityMode();
304  private native long getMethodData0(Executable method);
305  public         long getMethodData(Executable method) {
306    Objects.requireNonNull(method);
307    return getMethodData0(method);
308  }
309  public native Object[] getCodeBlob(long addr);
310
311  public native void clearInlineCaches();
312
313  // Intered strings
314  public native boolean isInStringTable(String str);
315
316  // Memory
317  public native void readReservedMemory();
318  public native long allocateMetaspace(ClassLoader classLoader, long size);
319  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
320  public native long incMetaspaceCapacityUntilGC(long increment);
321  public native long metaspaceCapacityUntilGC();
322
323  // Force Young GC
324  public native void youngGC();
325
326  // Force Full GC
327  public native void fullGC();
328
329  // Method tries to start concurrent mark cycle.
330  // It returns false if CM Thread is always in concurrent cycle.
331  public native boolean g1StartConcMarkCycle();
332
333  // Tests on ReservedSpace/VirtualSpace classes
334  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
335  public native void runMemoryUnitTests();
336  public native void readFromNoaccessArea();
337  public native long getThreadStackSize();
338  public native long getThreadRemainingStackSize();
339
340  // CPU features
341  public native String getCPUFeatures();
342
343  // Native extensions
344  public native long getHeapUsageForContext(int context);
345  public native long getHeapRegionCountForContext(int context);
346  private native int getContextForObject0(Object obj);
347  public         int getContextForObject(Object obj) {
348    Objects.requireNonNull(obj);
349    return getContextForObject0(obj);
350  }
351  public native void printRegionInfo(int context);
352
353  // VM flags
354  public native boolean isConstantVMFlag(String name);
355  public native boolean isLockedVMFlag(String name);
356  public native void    setBooleanVMFlag(String name, boolean value);
357  public native void    setIntVMFlag(String name, long value);
358  public native void    setUintVMFlag(String name, long value);
359  public native void    setIntxVMFlag(String name, long value);
360  public native void    setUintxVMFlag(String name, long value);
361  public native void    setUint64VMFlag(String name, long value);
362  public native void    setSizeTVMFlag(String name, long value);
363  public native void    setStringVMFlag(String name, String value);
364  public native void    setDoubleVMFlag(String name, double value);
365  public native Boolean getBooleanVMFlag(String name);
366  public native Long    getIntVMFlag(String name);
367  public native Long    getUintVMFlag(String name);
368  public native Long    getIntxVMFlag(String name);
369  public native Long    getUintxVMFlag(String name);
370  public native Long    getUint64VMFlag(String name);
371  public native Long    getSizeTVMFlag(String name);
372  public native String  getStringVMFlag(String name);
373  public native Double  getDoubleVMFlag(String name);
374  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
375    this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
376    this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
377    this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
378
379  public Object getVMFlag(String name) {
380    return flagsGetters.stream()
381                       .map(f -> f.apply(name))
382                       .filter(x -> x != null)
383                       .findAny()
384                       .orElse(null);
385  }
386  public native int getOffsetForName0(String name);
387  public int getOffsetForName(String name) throws Exception {
388    int offset = getOffsetForName0(name);
389    if (offset == -1) {
390      throw new RuntimeException(name + " not found");
391    }
392    return offset;
393  }
394  public native Boolean getMethodBooleanOption(Executable method, String name);
395  public native Long    getMethodIntxOption(Executable method, String name);
396  public native Long    getMethodUintxOption(Executable method, String name);
397  public native Double  getMethodDoubleOption(Executable method, String name);
398  public native String  getMethodStringOption(Executable method, String name);
399  private final List<BiFunction<Executable,String,Object>> methodOptionGetters
400      = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
401          this::getMethodUintxOption, this::getMethodDoubleOption,
402          this::getMethodStringOption);
403
404  public Object getMethodOption(Executable method, String name) {
405    return methodOptionGetters.stream()
406                              .map(f -> f.apply(method, name))
407                              .filter(x -> x != null)
408                              .findAny()
409                              .orElse(null);
410  }
411
412  // Safepoint Checking
413  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
414
415  // Sharing
416  public native boolean isShared(Object o);
417  public native boolean areSharedStringsIgnored();
418}
419