WhiteBox.java revision 1578:e1e2a0c7cbee
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.nio.ByteBuffer;
30import java.util.Arrays;
31import java.util.List;
32import java.util.function.BiFunction;
33import java.util.function.Function;
34import java.util.stream.Stream;
35import java.security.BasicPermission;
36import java.util.Objects;
37
38import sun.hotspot.parser.DiagnosticCommand;
39
40public class WhiteBox {
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 getVMAllocationGranularity();
87  public native long getVMLargePageSize();
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  // JVMTI
117  private native void addToBootstrapClassLoaderSearch0(String segment);
118  public         void addToBootstrapClassLoaderSearch(String segment){
119    Objects.requireNonNull(segment);
120    addToBootstrapClassLoaderSearch0(segment);
121  }
122
123  private native void addToSystemClassLoaderSearch0(String segment);
124  public         void addToSystemClassLoaderSearch(String segment) {
125    Objects.requireNonNull(segment);
126    addToSystemClassLoaderSearch0(segment);
127  }
128
129  // G1
130  public native boolean g1InConcurrentMark();
131  private native boolean g1IsHumongous0(Object o);
132  public         boolean g1IsHumongous(Object o) {
133    Objects.requireNonNull(o);
134    return g1IsHumongous0(o);
135  }
136
137  public native long    g1NumMaxRegions();
138  public native long    g1NumFreeRegions();
139  public native int     g1RegionSize();
140  public native MemoryUsage g1AuxiliaryMemoryUsage();
141  private  native Object[]    parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args);
142  public          Object[]    parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) {
143    Objects.requireNonNull(args);
144    return parseCommandLine0(commandline, delim, args);
145  }
146
147  // NMT
148  public native long NMTMalloc(long size);
149  public native void NMTFree(long mem);
150  public native long NMTReserveMemory(long size);
151  public native void NMTCommitMemory(long addr, long size);
152  public native void NMTUncommitMemory(long addr, long size);
153  public native void NMTReleaseMemory(long addr, long size);
154  public native long NMTMallocWithPseudoStack(long size, int index);
155  public native boolean NMTChangeTrackingLevel();
156  public native int NMTGetHashSize();
157
158  // Compiler
159  public native int     deoptimizeFrames(boolean makeNotEntrant);
160  public native void    deoptimizeAll();
161  public        boolean isMethodCompiled(Executable method) {
162    return isMethodCompiled(method, false /*not osr*/);
163  }
164  private native boolean isMethodCompiled0(Executable method, boolean isOsr);
165  public         boolean isMethodCompiled(Executable method, boolean isOsr){
166    Objects.requireNonNull(method);
167    return isMethodCompiled0(method, isOsr);
168  }
169  public        boolean isMethodCompilable(Executable method) {
170    return isMethodCompilable(method, -1 /*any*/);
171  }
172  public        boolean isMethodCompilable(Executable method, int compLevel) {
173    return isMethodCompilable(method, compLevel, false /*not osr*/);
174  }
175  private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
176  public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
177    Objects.requireNonNull(method);
178    return isMethodCompilable0(method, compLevel, isOsr);
179  }
180  private native boolean isMethodQueuedForCompilation0(Executable method);
181  public         boolean isMethodQueuedForCompilation(Executable method) {
182    Objects.requireNonNull(method);
183    return isMethodQueuedForCompilation0(method);
184  }
185  public        int     deoptimizeMethod(Executable method) {
186    return deoptimizeMethod(method, false /*not osr*/);
187  }
188  private native int     deoptimizeMethod0(Executable method, boolean isOsr);
189  public         int     deoptimizeMethod(Executable method, boolean isOsr) {
190    Objects.requireNonNull(method);
191    return deoptimizeMethod0(method, isOsr);
192  }
193  public        void    makeMethodNotCompilable(Executable method) {
194    makeMethodNotCompilable(method, -1 /*any*/);
195  }
196  public        void    makeMethodNotCompilable(Executable method, int compLevel) {
197    makeMethodNotCompilable(method, compLevel, false /*not osr*/);
198  }
199  private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
200  public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
201    Objects.requireNonNull(method);
202    makeMethodNotCompilable0(method, compLevel, isOsr);
203  }
204  public        int     getMethodCompilationLevel(Executable method) {
205    return getMethodCompilationLevel(method, false /*not ost*/);
206  }
207  private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
208  public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
209    Objects.requireNonNull(method);
210    return getMethodCompilationLevel0(method, isOsr);
211  }
212  private native boolean testSetDontInlineMethod0(Executable method, boolean value);
213  public         boolean testSetDontInlineMethod(Executable method, boolean value) {
214    Objects.requireNonNull(method);
215    return testSetDontInlineMethod0(method, value);
216  }
217  public        int     getCompileQueuesSize() {
218    return getCompileQueueSize(-1 /*any*/);
219  }
220  public native int     getCompileQueueSize(int compLevel);
221  private native boolean testSetForceInlineMethod0(Executable method, boolean value);
222  public         boolean testSetForceInlineMethod(Executable method, boolean value) {
223    Objects.requireNonNull(method);
224    return testSetForceInlineMethod0(method, value);
225  }
226  public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
227    return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
228  }
229  private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
230  public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
231    Objects.requireNonNull(method);
232    return enqueueMethodForCompilation0(method, compLevel, entry_bci);
233  }
234  private native void    clearMethodState0(Executable method);
235  public         void    clearMethodState(Executable method) {
236    Objects.requireNonNull(method);
237    clearMethodState0(method);
238  }
239  public native void    lockCompilation();
240  public native void    unlockCompilation();
241  private native int     getMethodEntryBci0(Executable method);
242  public         int     getMethodEntryBci(Executable method) {
243    Objects.requireNonNull(method);
244    return getMethodEntryBci0(method);
245  }
246  private native Object[] getNMethod0(Executable method, boolean isOsr);
247  public         Object[] getNMethod(Executable method, boolean isOsr) {
248    Objects.requireNonNull(method);
249    return getNMethod0(method, isOsr);
250  }
251  public native long    allocateCodeBlob(int size, int type);
252  public        long    allocateCodeBlob(long size, int type) {
253      int intSize = (int) size;
254      if ((long) intSize != size || size < 0) {
255          throw new IllegalArgumentException(
256                "size argument has illegal value " + size);
257      }
258      return allocateCodeBlob( intSize, type);
259  }
260  public native void    freeCodeBlob(long addr);
261  public native void    forceNMethodSweep();
262  public native Object[] getCodeHeapEntries(int type);
263  public native int     getCompilationActivityMode();
264  public native Object[] getCodeBlob(long addr);
265
266  // Intered strings
267  public native boolean isInStringTable(String str);
268
269  // Memory
270  public native void readReservedMemory();
271  public native long allocateMetaspace(ClassLoader classLoader, long size);
272  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
273  public native long incMetaspaceCapacityUntilGC(long increment);
274  public native long metaspaceCapacityUntilGC();
275
276  // Force Young GC
277  public native void youngGC();
278
279  // Force Full GC
280  public native void fullGC();
281
282  // Method tries to start concurrent mark cycle.
283  // It returns false if CM Thread is always in concurrent cycle.
284  public native boolean g1StartConcMarkCycle();
285
286  // Tests on ReservedSpace/VirtualSpace classes
287  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
288  public native void runMemoryUnitTests();
289  public native void readFromNoaccessArea();
290  public native long getThreadStackSize();
291  public native long getThreadRemainingStackSize();
292
293  // CPU features
294  public native String getCPUFeatures();
295
296  // Native extensions
297  public native long getHeapUsageForContext(int context);
298  public native long getHeapRegionCountForContext(int context);
299  private native int getContextForObject0(Object obj);
300  public         int getContextForObject(Object obj) {
301    Objects.requireNonNull(obj);
302    return getContextForObject0(obj);
303  }
304  public native void printRegionInfo(int context);
305
306  // VM flags
307  public native boolean isConstantVMFlag(String name);
308  public native boolean isLockedVMFlag(String name);
309  public native void    setBooleanVMFlag(String name, boolean value);
310  public native void    setIntVMFlag(String name, long value);
311  public native void    setUintVMFlag(String name, long value);
312  public native void    setIntxVMFlag(String name, long value);
313  public native void    setUintxVMFlag(String name, long value);
314  public native void    setUint64VMFlag(String name, long value);
315  public native void    setSizeTVMFlag(String name, long value);
316  public native void    setStringVMFlag(String name, String value);
317  public native void    setDoubleVMFlag(String name, double value);
318  public native Boolean getBooleanVMFlag(String name);
319  public native Long    getIntVMFlag(String name);
320  public native Long    getUintVMFlag(String name);
321  public native Long    getIntxVMFlag(String name);
322  public native Long    getUintxVMFlag(String name);
323  public native Long    getUint64VMFlag(String name);
324  public native Long    getSizeTVMFlag(String name);
325  public native String  getStringVMFlag(String name);
326  public native Double  getDoubleVMFlag(String name);
327  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
328    this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
329    this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
330    this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
331
332  public Object getVMFlag(String name) {
333    return flagsGetters.stream()
334                       .map(f -> f.apply(name))
335                       .filter(x -> x != null)
336                       .findAny()
337                       .orElse(null);
338  }
339  public native int getOffsetForName0(String name);
340  public int getOffsetForName(String name) throws Exception {
341    int offset = getOffsetForName0(name);
342    if (offset == -1) {
343      throw new RuntimeException(name + " not found");
344    }
345    return offset;
346  }
347  public native Boolean getMethodBooleanOption(Executable method, String name);
348  public native Long    getMethodIntxOption(Executable method, String name);
349  public native Long    getMethodUintxOption(Executable method, String name);
350  public native Double  getMethodDoubleOption(Executable method, String name);
351  public native String  getMethodStringOption(Executable method, String name);
352  private final List<BiFunction<Executable,String,Object>> methodOptionGetters
353      = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
354          this::getMethodUintxOption, this::getMethodDoubleOption,
355          this::getMethodStringOption);
356
357  public Object getMethodOption(Executable method, String name) {
358    return methodOptionGetters.stream()
359                              .map(f -> f.apply(method, name))
360                              .filter(x -> x != null)
361                              .findAny()
362                              .orElse(null);
363  }
364
365  public native boolean readImageFile(String imagePath);
366  public native long imageOpenImage(String imagePath, boolean bigEndian);
367  public native void imageCloseImage(long id);
368  public native long imageGetIndexAddress(long id);
369  public native long imageGetDataAddress(long id);
370  public native boolean imageReadCompressed(long id, long offset,
371    ByteBuffer compressedBuffer, long compressedSize,
372    ByteBuffer uncompressedBuffer, long uncompressedSize);
373  public native boolean imageRead(long id, long offset,
374    ByteBuffer uncompressedBuffer, long uncompressedSize);
375  public native byte[] imageGetStringBytes(long id, int offset);
376  public native long imageGetStringsSize(long id);
377  public native long[] imageGetAttributes(long id, int offset);
378  public native long[] imageFindAttributes(long id, byte[] path);
379  public native int[] imageAttributeOffsets(long id);
380  public native int imageGetIntAtAddress(long address, int offset, boolean big_endian);
381
382  // Safepoint Checking
383  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
384
385  // Sharing
386  public native boolean isShared(Object o);
387  public native boolean areSharedStringsIgnored();
388}
389