WhiteBox.java revision 2135:5d2c504ff630
1139823Simp/*
2102195Sarchie * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3102195Sarchie * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4102195Sarchie *
5102195Sarchie * This code is free software; you can redistribute it and/or modify it
6102195Sarchie * under the terms of the GNU General Public License version 2 only, as
7102195Sarchie * published by the Free Software Foundation.
8102195Sarchie *
9102195Sarchie * This code is distributed in the hope that it will be useful, but WITHOUT
10102195Sarchie * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11102195Sarchie * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12102195Sarchie * version 2 for more details (a copy is included in the LICENSE file that
13102195Sarchie * accompanied this code).
14102195Sarchie *
15102195Sarchie * You should have received a copy of the GNU General Public License version
16102195Sarchie * 2 along with this work; if not, write to the Free Software Foundation,
17102195Sarchie * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18102195Sarchie *
19102195Sarchie * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20102195Sarchie * or visit www.oracle.com if you need additional information or have any
21102195Sarchie * questions.
22102195Sarchie *
23102195Sarchie */
24102195Sarchie
25102195Sarchiepackage sun.hotspot;
26102195Sarchie
27102195Sarchieimport java.lang.management.MemoryUsage;
28102195Sarchieimport java.lang.reflect.Executable;
29102195Sarchieimport java.util.Arrays;
30102195Sarchieimport java.util.List;
31102195Sarchieimport java.util.function.BiFunction;
32102195Sarchieimport java.util.function.Function;
33102195Sarchieimport java.security.BasicPermission;
34102195Sarchieimport java.util.Objects;
35102195Sarchie
36102195Sarchieimport sun.hotspot.parser.DiagnosticCommand;
37102195Sarchie
38102195Sarchiepublic class WhiteBox {
39102195Sarchie  @SuppressWarnings("serial")
40102195Sarchie  public static class WhiteBoxPermission extends BasicPermission {
41102195Sarchie    public WhiteBoxPermission(String s) {
42122481Sru      super(s);
43122481Sru    }
44102195Sarchie  }
45102195Sarchie
46102195Sarchie  private WhiteBox() {}
47133060Sbz  private static final WhiteBox instance = new WhiteBox();
48102195Sarchie  private static native void registerNatives();
49102195Sarchie
50102195Sarchie  /**
51102195Sarchie   * Returns the singleton WhiteBox instance.
52102195Sarchie   *
53102195Sarchie   * The returned WhiteBox object should be carefully guarded
54102195Sarchie   * by the caller, since it can be used to read and write data
55102195Sarchie   * at arbitrary memory addresses. It must never be passed to
56102195Sarchie   * untrusted code.
57298813Spfg   */
58133058Sbz  public synchronized static WhiteBox getWhiteBox() {
59133058Sbz    SecurityManager sm = System.getSecurityManager();
60133058Sbz    if (sm != null) {
61133058Sbz      sm.checkPermission(new WhiteBoxPermission("getInstance"));
62133058Sbz    }
63133058Sbz    return instance;
64133058Sbz  }
65133058Sbz
66133058Sbz  static {
67133058Sbz    registerNatives();
68133058Sbz  }
69133058Sbz
70133058Sbz  // Get the maximum heap size supporting COOPs
71133058Sbz  public native long getCompressedOopsMaxHeapSize();
72102195Sarchie  // Arguments
73102195Sarchie  public native void printHeapSizes();
74102195Sarchie
75102195Sarchie  // Memory
76102195Sarchie  private native long getObjectAddress0(Object o);
77102195Sarchie  public           long getObjectAddress(Object o) {
78102195Sarchie    Objects.requireNonNull(o);
79102195Sarchie    return getObjectAddress0(o);
80102195Sarchie  }
81102195Sarchie
82102195Sarchie  public native int  getHeapOopSize();
83102195Sarchie  public native int  getVMPageSize();
84102195Sarchie  public native long getVMAllocationGranularity();
85102195Sarchie  public native long getVMLargePageSize();
86102195Sarchie  public native long getHeapSpaceAlignment();
87102195Sarchie
88102195Sarchie  private native boolean isObjectInOldGen0(Object o);
89102195Sarchie  public         boolean isObjectInOldGen(Object o) {
90102195Sarchie    Objects.requireNonNull(o);
91102195Sarchie    return isObjectInOldGen0(o);
92102195Sarchie  }
93102195Sarchie
94102195Sarchie  private native long getObjectSize0(Object o);
95102195Sarchie  public         long getObjectSize(Object o) {
96102195Sarchie    Objects.requireNonNull(o);
97102195Sarchie    return getObjectSize0(o);
98102195Sarchie  }
99102195Sarchie
100102195Sarchie  // Runtime
101102195Sarchie  // Make sure class name is in the correct format
102102195Sarchie  public boolean isClassAlive(String name) {
103102195Sarchie    return isClassAlive0(name.replace('.', '/'));
104102195Sarchie  }
105102195Sarchie  private native boolean isClassAlive0(String name);
106102195Sarchie
107102195Sarchie  private native boolean isMonitorInflated0(Object obj);
108102195Sarchie  public         boolean isMonitorInflated(Object obj) {
109102195Sarchie    Objects.requireNonNull(obj);
110102195Sarchie    return isMonitorInflated0(obj);
111102195Sarchie  }
112102195Sarchie
113102195Sarchie  public native void forceSafepoint();
114102195Sarchie
115102195Sarchie  private native long getConstantPool0(Class<?> aClass);
116102195Sarchie  public         long getConstantPool(Class<?> aClass) {
117102195Sarchie    Objects.requireNonNull(aClass);
118102195Sarchie    return getConstantPool0(aClass);
119102195Sarchie  }
120102195Sarchie
121102195Sarchie  private native int getConstantPoolCacheIndexTag0();
122102195Sarchie  public         int getConstantPoolCacheIndexTag() {
123102195Sarchie    return getConstantPoolCacheIndexTag0();
124102195Sarchie  }
125102195Sarchie
126102195Sarchie  private native int getConstantPoolCacheLength0(Class<?> aClass);
127102195Sarchie  public         int getConstantPoolCacheLength(Class<?> aClass) {
128102195Sarchie    Objects.requireNonNull(aClass);
129102195Sarchie    return getConstantPoolCacheLength0(aClass);
130102195Sarchie  }
131102195Sarchie
132102195Sarchie  private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index);
133102195Sarchie  public         int remapInstructionOperandFromCPCache(Class<?> aClass, int index) {
134102195Sarchie    Objects.requireNonNull(aClass);
135102195Sarchie    return remapInstructionOperandFromCPCache0(aClass, index);
136102195Sarchie  }
137102195Sarchie
138102195Sarchie  private native int encodeConstantPoolIndyIndex0(int index);
139102195Sarchie  public         int encodeConstantPoolIndyIndex(int index) {
140102195Sarchie    return encodeConstantPoolIndyIndex0(index);
141102195Sarchie  }
142102195Sarchie
143102195Sarchie  // JVMTI
144102195Sarchie  private native void addToBootstrapClassLoaderSearch0(String segment);
145102195Sarchie  public         void addToBootstrapClassLoaderSearch(String segment){
146102195Sarchie    Objects.requireNonNull(segment);
147102195Sarchie    addToBootstrapClassLoaderSearch0(segment);
148102195Sarchie  }
149102195Sarchie
150102195Sarchie  private native void addToSystemClassLoaderSearch0(String segment);
151102195Sarchie  public         void addToSystemClassLoaderSearch(String segment) {
152102195Sarchie    Objects.requireNonNull(segment);
153102195Sarchie    addToSystemClassLoaderSearch0(segment);
154102195Sarchie  }
155102195Sarchie
156102195Sarchie  // G1
157102195Sarchie  public native boolean g1InConcurrentMark();
158102195Sarchie  private native boolean g1IsHumongous0(Object o);
159102195Sarchie  public         boolean g1IsHumongous(Object o) {
160102195Sarchie    Objects.requireNonNull(o);
161102195Sarchie    return g1IsHumongous0(o);
162102195Sarchie  }
163133060Sbz
164133060Sbz  private native boolean g1BelongsToHumongousRegion0(long adr);
165133060Sbz  public         boolean g1BelongsToHumongousRegion(long adr) {
166133060Sbz    if (adr == 0) {
167133060Sbz      throw new IllegalArgumentException("adr argument should not be null");
168133060Sbz    }
169133060Sbz    return g1BelongsToHumongousRegion0(adr);
170133060Sbz  }
171133060Sbz
172133060Sbz
173133060Sbz  private native boolean g1BelongsToFreeRegion0(long adr);
174133060Sbz  public         boolean g1BelongsToFreeRegion(long adr) {
175133060Sbz    if (adr == 0) {
176133060Sbz      throw new IllegalArgumentException("adr argument should not be null");
177133060Sbz    }
178133060Sbz    return g1BelongsToFreeRegion0(adr);
179133060Sbz  }
180102195Sarchie
181102195Sarchie  public native long    g1NumMaxRegions();
182102195Sarchie  public native long    g1NumFreeRegions();
183102195Sarchie  public native int     g1RegionSize();
184102195Sarchie  public native MemoryUsage g1AuxiliaryMemoryUsage();
185102195Sarchie  private  native Object[]    parseCommandLine0(String commandline, char delim, DiagnosticCommand[] args);
186102195Sarchie  public          Object[]    parseCommandLine(String commandline, char delim, DiagnosticCommand[] args) {
187102195Sarchie    Objects.requireNonNull(args);
188102195Sarchie    return parseCommandLine0(commandline, delim, args);
189133060Sbz  }
190133060Sbz
191133060Sbz  // Parallel GC
192102195Sarchie  public native long psVirtualSpaceAlignment();
193133058Sbz  public native long psHeapGenerationAlignment();
194102195Sarchie
195102195Sarchie  /**
196122481Sru   * Enumerates old regions with liveness less than specified and produces some statistics
197   * @param liveness percent of region's liveness (live_objects / total_region_size * 100).
198   * @return long[3] array where long[0] - total count of old regions
199   *                             long[1] - total memory of old regions
200   *                             long[2] - lowest estimation of total memory of old regions to be freed (non-full
201   *                             regions are not included)
202   */
203  public native long[] g1GetMixedGCInfo(int liveness);
204
205  // NMT
206  public native long NMTMalloc(long size);
207  public native void NMTFree(long mem);
208  public native long NMTReserveMemory(long size);
209  public native void NMTCommitMemory(long addr, long size);
210  public native void NMTUncommitMemory(long addr, long size);
211  public native void NMTReleaseMemory(long addr, long size);
212  public native long NMTMallocWithPseudoStack(long size, int index);
213  public native boolean NMTChangeTrackingLevel();
214  public native int NMTGetHashSize();
215
216  // Compiler
217  public native int     matchesMethod(Executable method, String pattern);
218  public native int     matchesInline(Executable method, String pattern);
219  public native boolean shouldPrintAssembly(Executable method, int comp_level);
220  public native int     deoptimizeFrames(boolean makeNotEntrant);
221  public native void    deoptimizeAll();
222
223  public        boolean isMethodCompiled(Executable method) {
224    return isMethodCompiled(method, false /*not osr*/);
225  }
226  private native boolean isMethodCompiled0(Executable method, boolean isOsr);
227  public         boolean isMethodCompiled(Executable method, boolean isOsr){
228    Objects.requireNonNull(method);
229    return isMethodCompiled0(method, isOsr);
230  }
231  public        boolean isMethodCompilable(Executable method) {
232    return isMethodCompilable(method, -1 /*any*/);
233  }
234  public        boolean isMethodCompilable(Executable method, int compLevel) {
235    return isMethodCompilable(method, compLevel, false /*not osr*/);
236  }
237  private native boolean isMethodCompilable0(Executable method, int compLevel, boolean isOsr);
238  public         boolean isMethodCompilable(Executable method, int compLevel, boolean isOsr) {
239    Objects.requireNonNull(method);
240    return isMethodCompilable0(method, compLevel, isOsr);
241  }
242  private native boolean isMethodQueuedForCompilation0(Executable method);
243  public         boolean isMethodQueuedForCompilation(Executable method) {
244    Objects.requireNonNull(method);
245    return isMethodQueuedForCompilation0(method);
246  }
247  // Determine if the compiler corresponding to the compilation level 'compLevel'
248  // and to the compilation context 'compilation_context' provides an intrinsic
249  // for the method 'method'. An intrinsic is available for method 'method' if:
250  //  - the intrinsic is enabled (by using the appropriate command-line flag) and
251  //  - the platform on which the VM is running provides the instructions necessary
252  //    for the compiler to generate the intrinsic code.
253  //
254  // The compilation context is related to using the DisableIntrinsic flag on a
255  // per-method level, see hotspot/src/share/vm/compiler/abstractCompiler.hpp
256  // for more details.
257  public boolean isIntrinsicAvailable(Executable method,
258                                      Executable compilationContext,
259                                      int compLevel) {
260      Objects.requireNonNull(method);
261      return isIntrinsicAvailable0(method, compilationContext, compLevel);
262  }
263  // If usage of the DisableIntrinsic flag is not expected (or the usage can be ignored),
264  // use the below method that does not require the compilation context as argument.
265  public boolean isIntrinsicAvailable(Executable method, int compLevel) {
266      return isIntrinsicAvailable(method, null, compLevel);
267  }
268  private native boolean isIntrinsicAvailable0(Executable method,
269                                               Executable compilationContext,
270                                               int compLevel);
271  public        int     deoptimizeMethod(Executable method) {
272    return deoptimizeMethod(method, false /*not osr*/);
273  }
274  private native int     deoptimizeMethod0(Executable method, boolean isOsr);
275  public         int     deoptimizeMethod(Executable method, boolean isOsr) {
276    Objects.requireNonNull(method);
277    return deoptimizeMethod0(method, isOsr);
278  }
279  public        void    makeMethodNotCompilable(Executable method) {
280    makeMethodNotCompilable(method, -1 /*any*/);
281  }
282  public        void    makeMethodNotCompilable(Executable method, int compLevel) {
283    makeMethodNotCompilable(method, compLevel, false /*not osr*/);
284  }
285  private native void    makeMethodNotCompilable0(Executable method, int compLevel, boolean isOsr);
286  public         void    makeMethodNotCompilable(Executable method, int compLevel, boolean isOsr) {
287    Objects.requireNonNull(method);
288    makeMethodNotCompilable0(method, compLevel, isOsr);
289  }
290  public        int     getMethodCompilationLevel(Executable method) {
291    return getMethodCompilationLevel(method, false /*not ost*/);
292  }
293  private native int     getMethodCompilationLevel0(Executable method, boolean isOsr);
294  public         int     getMethodCompilationLevel(Executable method, boolean isOsr) {
295    Objects.requireNonNull(method);
296    return getMethodCompilationLevel0(method, isOsr);
297  }
298  private native boolean testSetDontInlineMethod0(Executable method, boolean value);
299  public         boolean testSetDontInlineMethod(Executable method, boolean value) {
300    Objects.requireNonNull(method);
301    return testSetDontInlineMethod0(method, value);
302  }
303  public        int     getCompileQueuesSize() {
304    return getCompileQueueSize(-1 /*any*/);
305  }
306  public native int     getCompileQueueSize(int compLevel);
307  private native boolean testSetForceInlineMethod0(Executable method, boolean value);
308  public         boolean testSetForceInlineMethod(Executable method, boolean value) {
309    Objects.requireNonNull(method);
310    return testSetForceInlineMethod0(method, value);
311  }
312  public        boolean enqueueMethodForCompilation(Executable method, int compLevel) {
313    return enqueueMethodForCompilation(method, compLevel, -1 /*InvocationEntryBci*/);
314  }
315  private native boolean enqueueMethodForCompilation0(Executable method, int compLevel, int entry_bci);
316  public  boolean enqueueMethodForCompilation(Executable method, int compLevel, int entry_bci) {
317    Objects.requireNonNull(method);
318    return enqueueMethodForCompilation0(method, compLevel, entry_bci);
319  }
320  private native boolean enqueueInitializerForCompilation0(Class<?> aClass, int compLevel);
321  public  boolean enqueueInitializerForCompilation(Class<?> aClass, int compLevel) {
322    Objects.requireNonNull(aClass);
323    return enqueueInitializerForCompilation0(aClass, compLevel);
324  }
325  private native void    clearMethodState0(Executable method);
326  public         void    clearMethodState(Executable method) {
327    Objects.requireNonNull(method);
328    clearMethodState0(method);
329  }
330  public native void    lockCompilation();
331  public native void    unlockCompilation();
332  private native int     getMethodEntryBci0(Executable method);
333  public         int     getMethodEntryBci(Executable method) {
334    Objects.requireNonNull(method);
335    return getMethodEntryBci0(method);
336  }
337  private native Object[] getNMethod0(Executable method, boolean isOsr);
338  public         Object[] getNMethod(Executable method, boolean isOsr) {
339    Objects.requireNonNull(method);
340    return getNMethod0(method, isOsr);
341  }
342  public native long    allocateCodeBlob(int size, int type);
343  public        long    allocateCodeBlob(long size, int type) {
344      int intSize = (int) size;
345      if ((long) intSize != size || size < 0) {
346          throw new IllegalArgumentException(
347                "size argument has illegal value " + size);
348      }
349      return allocateCodeBlob( intSize, type);
350  }
351  public native void    freeCodeBlob(long addr);
352  public native void    forceNMethodSweep();
353  public native Object[] getCodeHeapEntries(int type);
354  public native int     getCompilationActivityMode();
355  private native long getMethodData0(Executable method);
356  public         long getMethodData(Executable method) {
357    Objects.requireNonNull(method);
358    return getMethodData0(method);
359  }
360  public native Object[] getCodeBlob(long addr);
361
362  private native void clearInlineCaches0(boolean preserve_static_stubs);
363  public void clearInlineCaches() {
364    clearInlineCaches0(false);
365  }
366  public void clearInlineCaches(boolean preserve_static_stubs) {
367    clearInlineCaches0(preserve_static_stubs);
368  }
369
370  // Intered strings
371  public native boolean isInStringTable(String str);
372
373  // Memory
374  public native void readReservedMemory();
375  public native long allocateMetaspace(ClassLoader classLoader, long size);
376  public native void freeMetaspace(ClassLoader classLoader, long addr, long size);
377  public native long incMetaspaceCapacityUntilGC(long increment);
378  public native long metaspaceCapacityUntilGC();
379  public native boolean metaspaceShouldConcurrentCollect();
380
381  // Don't use these methods directly
382  // Use sun.hotspot.gc.GC class instead.
383  public native int currentGC();
384  public native int allSupportedGC();
385  public native boolean gcSelectedByErgo();
386
387  // Force Young GC
388  public native void youngGC();
389
390  // Force Full GC
391  public native void fullGC();
392
393  // Method tries to start concurrent mark cycle.
394  // It returns false if CM Thread is always in concurrent cycle.
395  public native boolean g1StartConcMarkCycle();
396
397  // Tests on ReservedSpace/VirtualSpace classes
398  public native int stressVirtualSpaceResize(long reservedSpaceSize, long magnitude, long iterations);
399  public native void runMemoryUnitTests();
400  public native void readFromNoaccessArea();
401  public native long getThreadStackSize();
402  public native long getThreadRemainingStackSize();
403
404  // CPU features
405  public native String getCPUFeatures();
406
407  // Native extensions
408  public native long getHeapUsageForContext(int context);
409  public native long getHeapRegionCountForContext(int context);
410  private native int getContextForObject0(Object obj);
411  public         int getContextForObject(Object obj) {
412    Objects.requireNonNull(obj);
413    return getContextForObject0(obj);
414  }
415  public native void printRegionInfo(int context);
416
417  // VM flags
418  public native boolean isConstantVMFlag(String name);
419  public native boolean isLockedVMFlag(String name);
420  public native void    setBooleanVMFlag(String name, boolean value);
421  public native void    setIntVMFlag(String name, long value);
422  public native void    setUintVMFlag(String name, long value);
423  public native void    setIntxVMFlag(String name, long value);
424  public native void    setUintxVMFlag(String name, long value);
425  public native void    setUint64VMFlag(String name, long value);
426  public native void    setSizeTVMFlag(String name, long value);
427  public native void    setStringVMFlag(String name, String value);
428  public native void    setDoubleVMFlag(String name, double value);
429  public native Boolean getBooleanVMFlag(String name);
430  public native Long    getIntVMFlag(String name);
431  public native Long    getUintVMFlag(String name);
432  public native Long    getIntxVMFlag(String name);
433  public native Long    getUintxVMFlag(String name);
434  public native Long    getUint64VMFlag(String name);
435  public native Long    getSizeTVMFlag(String name);
436  public native String  getStringVMFlag(String name);
437  public native Double  getDoubleVMFlag(String name);
438  private final List<Function<String,Object>> flagsGetters = Arrays.asList(
439    this::getBooleanVMFlag, this::getIntVMFlag, this::getUintVMFlag,
440    this::getIntxVMFlag, this::getUintxVMFlag, this::getUint64VMFlag,
441    this::getSizeTVMFlag, this::getStringVMFlag, this::getDoubleVMFlag);
442
443  public Object getVMFlag(String name) {
444    return flagsGetters.stream()
445                       .map(f -> f.apply(name))
446                       .filter(x -> x != null)
447                       .findAny()
448                       .orElse(null);
449  }
450
451  // Jigsaw
452  public native void DefineModule(Object module, String version, String location,
453                                  Object[] packages);
454  public native void AddModuleExports(Object from_module, String pkg, Object to_module);
455  public native void AddReadsModule(Object from_module, Object source_module);
456  public native boolean CanReadModule(Object asking_module, Object source_module);
457  public native boolean IsExportedToModule(Object from_module, String pkg, Object to_module);
458  public native void AddModulePackage(Object module, String pkg);
459  public native void AddModuleExportsToAllUnnamed(Object module, String pkg);
460  public native void AddModuleExportsToAll(Object module, String pkg);
461  public native Object GetModuleByPackageName(Object ldr, String pkg);
462
463  public native int getOffsetForName0(String name);
464  public int getOffsetForName(String name) throws Exception {
465    int offset = getOffsetForName0(name);
466    if (offset == -1) {
467      throw new RuntimeException(name + " not found");
468    }
469    return offset;
470  }
471  public native Boolean getMethodBooleanOption(Executable method, String name);
472  public native Long    getMethodIntxOption(Executable method, String name);
473  public native Long    getMethodUintxOption(Executable method, String name);
474  public native Double  getMethodDoubleOption(Executable method, String name);
475  public native String  getMethodStringOption(Executable method, String name);
476  private final List<BiFunction<Executable,String,Object>> methodOptionGetters
477      = Arrays.asList(this::getMethodBooleanOption, this::getMethodIntxOption,
478          this::getMethodUintxOption, this::getMethodDoubleOption,
479          this::getMethodStringOption);
480
481  public Object getMethodOption(Executable method, String name) {
482    return methodOptionGetters.stream()
483                              .map(f -> f.apply(method, name))
484                              .filter(x -> x != null)
485                              .findAny()
486                              .orElse(null);
487  }
488
489  // Safepoint Checking
490  public native void assertMatchingSafepointCalls(boolean mutexSafepointValue, boolean attemptedNoSafepointValue);
491
492  // Sharing
493  public native boolean isShared(Object o);
494  public native boolean isSharedClass(Class<?> c);
495  public native boolean areSharedStringsIgnored();
496
497  // Compiler Directive
498  public native int addCompilerDirective(String compDirect);
499  public native void removeCompilerDirective(int count);
500}
501