rev 49524 : imported patch 8200426-g1h-refactoring

   1 /*
   2  * Copyright (c) 2012, 2018, 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 
  25 #include "precompiled.hpp"
  26 
  27 #include <new>
  28 
  29 #include "classfile/classLoaderData.hpp"
  30 #include "classfile/modules.hpp"
  31 #include "classfile/stringTable.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "compiler/methodMatcher.hpp"
  34 #include "compiler/directivesParser.hpp"
  35 #include "gc/shared/gcConfig.hpp"
  36 #include "gc/shared/genCollectedHeap.hpp"
  37 #include "jvmtifiles/jvmtiEnv.hpp"
  38 #include "memory/metadataFactory.hpp"
  39 #include "memory/metaspaceShared.hpp"
  40 #include "memory/iterator.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "memory/oopFactory.hpp"
  44 #include "oops/array.hpp"
  45 #include "oops/constantPool.inline.hpp"
  46 #include "oops/method.inline.hpp"
  47 #include "oops/objArrayKlass.hpp"
  48 #include "oops/objArrayOop.inline.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "oops/typeArrayOop.inline.hpp"
  51 #include "prims/wbtestmethods/parserTests.hpp"
  52 #include "prims/whitebox.inline.hpp"
  53 #include "runtime/arguments.hpp"
  54 #include "runtime/compilationPolicy.hpp"
  55 #include "runtime/deoptimization.hpp"
  56 #include "runtime/frame.inline.hpp"
  57 #include "runtime/handshake.hpp"
  58 #include "runtime/interfaceSupport.inline.hpp"
  59 #include "runtime/javaCalls.hpp"
  60 #include "runtime/jniHandles.inline.hpp"
  61 #include "runtime/os.hpp"
  62 #include "runtime/sweeper.hpp"
  63 #include "runtime/thread.hpp"
  64 #include "runtime/threadSMR.hpp"
  65 #include "runtime/vm_version.hpp"
  66 #include "services/memoryService.hpp"
  67 #include "utilities/align.hpp"
  68 #include "utilities/debug.hpp"
  69 #include "utilities/elfFile.hpp"
  70 #include "utilities/exceptions.hpp"
  71 #include "utilities/macros.hpp"
  72 #if INCLUDE_CDS
  73 #include "prims/cdsoffsets.hpp"
  74 #endif // INCLUDE_CDS
  75 #if INCLUDE_ALL_GCS
  76 #include "gc/g1/concurrentMarkThread.hpp"
  77 #include "gc/g1/g1CollectedHeap.inline.hpp"
  78 #include "gc/g1/g1ConcurrentMark.hpp"
  79 #include "gc/g1/heapRegionRemSet.hpp"
  80 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
  81 #include "gc/parallel/adjoiningGenerations.hpp"
  82 #endif // INCLUDE_ALL_GCS
  83 #if INCLUDE_NMT
  84 #include "services/mallocSiteTable.hpp"
  85 #include "services/memTracker.hpp"
  86 #include "utilities/nativeCallStack.hpp"
  87 #endif // INCLUDE_NMT
  88 
  89 #ifdef LINUX
  90 #include "osContainer_linux.hpp"
  91 #endif
  92 
  93 #define SIZE_T_MAX_VALUE ((size_t) -1)
  94 
  95 #define CHECK_JNI_EXCEPTION_(env, value)                               \
  96   do {                                                                 \
  97     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
  98     if (HAS_PENDING_EXCEPTION) {                                       \
  99       return(value);                                                   \
 100     }                                                                  \
 101   } while (0)
 102 
 103 #define CHECK_JNI_EXCEPTION(env)                                       \
 104   do {                                                                 \
 105     JavaThread* THREAD = JavaThread::thread_from_jni_environment(env); \
 106     if (HAS_PENDING_EXCEPTION) {                                       \
 107       return;                                                          \
 108     }                                                                  \
 109   } while (0)
 110 
 111 bool WhiteBox::_used = false;
 112 volatile bool WhiteBox::compilation_locked = false;
 113 
 114 class VM_WhiteBoxOperation : public VM_Operation {
 115  public:
 116   VM_WhiteBoxOperation()                         { }
 117   VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
 118   bool allow_nested_vm_operations() const        { return true; }
 119 };
 120 
 121 
 122 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
 123   return (jlong)(void*)JNIHandles::resolve(obj);
 124 WB_END
 125 
 126 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
 127   return heapOopSize;
 128 WB_END
 129 
 130 WB_ENTRY(jint, WB_GetVMPageSize(JNIEnv* env, jobject o))
 131   return os::vm_page_size();
 132 WB_END
 133 
 134 WB_ENTRY(jlong, WB_GetVMAllocationGranularity(JNIEnv* env, jobject o))
 135   return os::vm_allocation_granularity();
 136 WB_END
 137 
 138 WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o))
 139   return os::large_page_size();
 140 WB_END
 141 
 142 class WBIsKlassAliveClosure : public KlassClosure {
 143     Symbol* _name;
 144     bool _found;
 145 public:
 146     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
 147 
 148     void do_klass(Klass* k) {
 149       if (_found) return;
 150       Symbol* ksym = k->name();
 151       if (ksym->fast_compare(_name) == 0) {
 152         _found = true;
 153       }
 154     }
 155 
 156     bool found() const {
 157         return _found;
 158     }
 159 };
 160 
 161 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
 162   oop h_name = JNIHandles::resolve(name);
 163   if (h_name == NULL) return false;
 164   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
 165   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 166 
 167   WBIsKlassAliveClosure closure(sym);
 168   ClassLoaderDataGraph::classes_do(&closure);
 169 
 170   return closure.found();
 171 WB_END
 172 
 173 WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 174 #if INCLUDE_JVMTI
 175   ResourceMark rm;
 176   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 177   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 178   jvmtiError err = jvmti_env->AddToBootstrapClassLoaderSearch(seg);
 179   assert(err == JVMTI_ERROR_NONE, "must not fail");
 180 #endif
 181 }
 182 WB_END
 183 
 184 WB_ENTRY(void, WB_AddToSystemClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 185 #if INCLUDE_JVMTI
 186   ResourceMark rm;
 187   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 188   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 189   jvmtiError err = jvmti_env->AddToSystemClassLoaderSearch(seg);
 190   assert(err == JVMTI_ERROR_NONE, "must not fail");
 191 #endif
 192 }
 193 WB_END
 194 
 195 
 196 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
 197   return (jlong)Arguments::max_heap_for_compressed_oops();
 198 }
 199 WB_END
 200 
 201 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
 202   CollectorPolicy * p = Universe::heap()->collector_policy();
 203   tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap "
 204     SIZE_FORMAT " Maximum heap " SIZE_FORMAT " Space alignment " SIZE_FORMAT " Heap alignment " SIZE_FORMAT,
 205     p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(),
 206     p->space_alignment(), p->heap_alignment());
 207 }
 208 WB_END
 209 
 210 #ifndef PRODUCT
 211 // Forward declaration
 212 void TestReservedSpace_test();
 213 void TestReserveMemorySpecial_test();
 214 void TestVirtualSpace_test();
 215 void TestMetaspaceUtils_test();
 216 #endif
 217 
 218 WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o))
 219 #ifndef PRODUCT
 220   TestReservedSpace_test();
 221   TestReserveMemorySpecial_test();
 222   TestVirtualSpace_test();
 223   TestMetaspaceUtils_test();
 224 #endif
 225 WB_END
 226 
 227 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
 228   size_t granularity = os::vm_allocation_granularity();
 229   ReservedHeapSpace rhs(100 * granularity, granularity, false);
 230   VirtualSpace vs;
 231   vs.initialize(rhs, 50 * granularity);
 232 
 233   // Check if constraints are complied
 234   if (!( UseCompressedOops && rhs.base() != NULL &&
 235          Universe::narrow_oop_base() != NULL &&
 236          Universe::narrow_oop_use_implicit_null_checks() )) {
 237     tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
 238                   "\tUseCompressedOops is %d\n"
 239                   "\trhs.base() is " PTR_FORMAT "\n"
 240                   "\tUniverse::narrow_oop_base() is " PTR_FORMAT "\n"
 241                   "\tUniverse::narrow_oop_use_implicit_null_checks() is %d",
 242                   UseCompressedOops,
 243                   p2i(rhs.base()),
 244                   p2i(Universe::narrow_oop_base()),
 245                   Universe::narrow_oop_use_implicit_null_checks());
 246     return;
 247   }
 248   tty->print_cr("Reading from no access area... ");
 249   tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
 250                 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
 251 WB_END
 252 
 253 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
 254                                            size_t magnitude, size_t iterations) {
 255   size_t granularity = os::vm_allocation_granularity();
 256   ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false);
 257   VirtualSpace vs;
 258   if (!vs.initialize(rhs, 0)) {
 259     tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
 260     return 3;
 261   }
 262 
 263   int seed = os::random();
 264   tty->print_cr("Random seed is %d", seed);
 265   os::init_random(seed);
 266 
 267   for (size_t i = 0; i < iterations; i++) {
 268 
 269     // Whether we will shrink or grow
 270     bool shrink = os::random() % 2L == 0;
 271 
 272     // Get random delta to resize virtual space
 273     size_t delta = (size_t)os::random() % magnitude;
 274 
 275     // If we are about to shrink virtual space below zero, then expand instead
 276     if (shrink && vs.committed_size() < delta) {
 277       shrink = false;
 278     }
 279 
 280     // Resizing by delta
 281     if (shrink) {
 282       vs.shrink_by(delta);
 283     } else {
 284       // If expanding fails expand_by will silently return false
 285       vs.expand_by(delta, true);
 286     }
 287   }
 288   return 0;
 289 }
 290 
 291 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
 292         jlong reserved_space_size, jlong magnitude, jlong iterations))
 293   tty->print_cr("reservedSpaceSize=" JLONG_FORMAT ", magnitude=" JLONG_FORMAT ", "
 294                 "iterations=" JLONG_FORMAT "\n", reserved_space_size, magnitude,
 295                 iterations);
 296   if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
 297     tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
 298     return 1;
 299   }
 300 
 301   // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
 302   // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
 303   if (sizeof(size_t) < sizeof(jlong)) {
 304     jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE;
 305     if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
 306         || iterations > size_t_max_value) {
 307       tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
 308       return 2;
 309     }
 310   }
 311 
 312   return wb_stress_virtual_space_resize((size_t) reserved_space_size,
 313                                         (size_t) magnitude, (size_t) iterations);
 314 WB_END
 315 
 316 WB_ENTRY(jboolean, WB_IsGCSupported(JNIEnv* env, jobject o, jint name))
 317   return GCConfig::is_gc_supported((CollectedHeap::Name)name);
 318 WB_END
 319 
 320 WB_ENTRY(jboolean, WB_IsGCSelected(JNIEnv* env, jobject o, jint name))
 321   return GCConfig::is_gc_selected((CollectedHeap::Name)name);
 322 WB_END
 323 
 324 WB_ENTRY(jboolean, WB_IsGCSelectedErgonomically(JNIEnv* env, jobject o))
 325   return GCConfig::is_gc_selected_ergonomically();
 326 WB_END
 327 
 328 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
 329   oop p = JNIHandles::resolve(obj);
 330 #if INCLUDE_ALL_GCS
 331   if (UseG1GC) {
 332     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 333     const HeapRegion* hr = g1->heap_region_containing(p);
 334     if (hr == NULL) {
 335       return false;
 336     }
 337     return !(hr->is_young());
 338   } else if (UseParallelGC) {
 339     ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
 340     return !psh->is_in_young(p);
 341   }
 342 #endif // INCLUDE_ALL_GCS
 343   GenCollectedHeap* gch = GenCollectedHeap::heap();
 344   return !gch->is_in_young(p);
 345 WB_END
 346 
 347 WB_ENTRY(jlong, WB_GetObjectSize(JNIEnv* env, jobject o, jobject obj))
 348   oop p = JNIHandles::resolve(obj);
 349   return p->size() * HeapWordSize;
 350 WB_END
 351 
 352 WB_ENTRY(jlong, WB_GetHeapSpaceAlignment(JNIEnv* env, jobject o))
 353   size_t alignment = Universe::heap()->collector_policy()->space_alignment();
 354   return (jlong)alignment;
 355 WB_END
 356 
 357 WB_ENTRY(jlong, WB_GetHeapAlignment(JNIEnv* env, jobject o))
 358   size_t alignment = Universe::heap()->collector_policy()->heap_alignment();
 359   return (jlong)alignment;
 360 WB_END
 361 
 362 WB_ENTRY(jboolean, WB_SupportsConcurrentGCPhaseControl(JNIEnv* env, jobject o))
 363   return Universe::heap()->supports_concurrent_phase_control();
 364 WB_END
 365 
 366 WB_ENTRY(jobjectArray, WB_GetConcurrentGCPhases(JNIEnv* env, jobject o))
 367   const char* const* phases = Universe::heap()->concurrent_phases();
 368   jint nphases = 0;
 369   for ( ; phases[nphases] != NULL; ++nphases) ;
 370 
 371   ResourceMark rm(thread);
 372   ThreadToNativeFromVM ttn(thread);
 373   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
 374   CHECK_JNI_EXCEPTION_(env, NULL);
 375 
 376   jobjectArray result = env->NewObjectArray(nphases, clazz, NULL);
 377   CHECK_JNI_EXCEPTION_(env, NULL);
 378 
 379   // If push fails, return with pending exception.
 380   if (env->PushLocalFrame(nphases) < 0) return NULL;
 381   for (jint i = 0; i < nphases; ++i) {
 382     jstring phase = env->NewStringUTF(phases[i]);
 383     CHECK_JNI_EXCEPTION_(env, NULL);
 384     env->SetObjectArrayElement(result, i, phase);
 385     CHECK_JNI_EXCEPTION_(env, NULL);
 386   }
 387   env->PopLocalFrame(NULL);
 388 
 389   return result;
 390 WB_END
 391 
 392 WB_ENTRY(jboolean, WB_RequestConcurrentGCPhase(JNIEnv* env, jobject o, jstring name))
 393   Handle h_name(THREAD, JNIHandles::resolve(name));
 394   ResourceMark rm;
 395   const char* c_name = java_lang_String::as_utf8_string(h_name());
 396   return Universe::heap()->request_concurrent_phase(c_name);
 397 WB_END
 398 
 399 #if INCLUDE_ALL_GCS
 400 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
 401   if (UseG1GC) {
 402     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 403     oop result = JNIHandles::resolve(obj);
 404     const HeapRegion* hr = g1->heap_region_containing(result);
 405     return hr->is_humongous();
 406   }
 407   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1IsHumongous: G1 GC is not enabled");
 408 WB_END
 409 
 410 WB_ENTRY(jboolean, WB_G1BelongsToHumongousRegion(JNIEnv* env, jobject o, jlong addr))
 411   if (UseG1GC) {
 412     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 413     const HeapRegion* hr = g1->heap_region_containing((void*) addr);
 414     return hr->is_humongous();
 415   }
 416   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToHumongousRegion: G1 GC is not enabled");
 417 WB_END
 418 
 419 WB_ENTRY(jboolean, WB_G1BelongsToFreeRegion(JNIEnv* env, jobject o, jlong addr))
 420   if (UseG1GC) {
 421     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 422     const HeapRegion* hr = g1->heap_region_containing((void*) addr);
 423     return hr->is_free();
 424   }
 425   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1BelongsToFreeRegion: G1 GC is not enabled");
 426 WB_END
 427 
 428 WB_ENTRY(jlong, WB_G1NumMaxRegions(JNIEnv* env, jobject o))
 429   if (UseG1GC) {
 430     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 431     size_t nr = g1->max_regions();
 432     return (jlong)nr;
 433   }
 434   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumMaxRegions: G1 GC is not enabled");
 435 WB_END
 436 
 437 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 438   if (UseG1GC) {
 439     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 440     size_t nr = g1->num_free_regions();
 441     return (jlong)nr;
 442   }
 443   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1NumFreeRegions: G1 GC is not enabled");
 444 WB_END
 445 
 446 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 447   if (UseG1GC) {
 448     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 449     return g1h->concurrent_mark()->cm_thread()->during_cycle();
 450   }
 451   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1InConcurrentMark: G1 GC is not enabled");
 452 WB_END
 453 
 454 WB_ENTRY(jboolean, WB_G1StartMarkCycle(JNIEnv* env, jobject o))
 455   if (UseG1GC) {
 456     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 457     if (!g1h->concurrent_mark()->cm_thread()->during_cycle()) {
 458       g1h->collect(GCCause::_wb_conc_mark);
 459       return true;
 460     }
 461     return false;
 462   }
 463   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1StartMarkCycle: G1 GC is not enabled");
 464 WB_END
 465 
 466 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 467   if (UseG1GC) {
 468     return (jint)HeapRegion::GrainBytes;
 469   }
 470   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1RegionSize: G1 GC is not enabled");
 471 WB_END
 472 
 473 WB_ENTRY(jlong, WB_PSVirtualSpaceAlignment(JNIEnv* env, jobject o))
 474 #if INCLUDE_ALL_GCS
 475   if (UseParallelGC) {
 476     return ParallelScavengeHeap::heap()->gens()->virtual_spaces()->alignment();
 477   }
 478 #endif // INCLUDE_ALL_GCS
 479   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSVirtualSpaceAlignment: Parallel GC is not enabled");
 480 WB_END
 481 
 482 WB_ENTRY(jlong, WB_PSHeapGenerationAlignment(JNIEnv* env, jobject o))
 483 #if INCLUDE_ALL_GCS
 484   if (UseParallelGC) {
 485     return ParallelScavengeHeap::heap()->generation_alignment();
 486   }
 487 #endif // INCLUDE_ALL_GCS
 488   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_PSHeapGenerationAlignment: Parallel GC is not enabled");
 489 WB_END
 490 
 491 WB_ENTRY(jobject, WB_G1AuxiliaryMemoryUsage(JNIEnv* env))
 492 #if INCLUDE_ALL_GCS
 493   if (UseG1GC) {
 494     ResourceMark rm(THREAD);
 495     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 496     MemoryUsage usage = g1h->get_auxiliary_data_memory_usage();
 497     Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
 498     return JNIHandles::make_local(env, h());
 499   }
 500 #endif // INCLUDE_ALL_GCS
 501   THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1AuxiliaryMemoryUsage: G1 GC is not enabled");
 502 WB_END
 503 
 504 class OldRegionsLivenessClosure: public HeapRegionClosure {
 505 
 506  private:
 507   const int _liveness;
 508   size_t _total_count;
 509   size_t _total_memory;
 510   size_t _total_memory_to_free;
 511 
 512  public:
 513   OldRegionsLivenessClosure(int liveness) :
 514     _liveness(liveness),
 515     _total_count(0),
 516     _total_memory(0),
 517     _total_memory_to_free(0) { }
 518 
 519     size_t total_count() { return _total_count; }
 520     size_t total_memory() { return _total_memory; }
 521     size_t total_memory_to_free() { return _total_memory_to_free; }
 522 
 523   bool do_heap_region(HeapRegion* r) {
 524     if (r->is_old()) {
 525       size_t prev_live = r->marked_bytes();
 526       size_t live = r->live_bytes();
 527       size_t size = r->used();
 528       size_t reg_size = HeapRegion::GrainBytes;
 529       if (size > 0 && ((int)(live * 100 / size) < _liveness)) {
 530         _total_memory += size;
 531         ++_total_count;
 532         if (size == reg_size) {
 533         // we don't include non-full regions since they are unlikely included in mixed gc
 534         // for testing purposes it's enough to have lowest estimation of total memory that is expected to be freed
 535           _total_memory_to_free += size - prev_live;
 536         }
 537       }
 538     }
 539     return false;
 540   }
 541 };
 542 
 543 
 544 WB_ENTRY(jlongArray, WB_G1GetMixedGCInfo(JNIEnv* env, jobject o, jint liveness))
 545   if (!UseG1GC) {
 546     THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1GetMixedGCInfo: G1 GC is not enabled");
 547   }
 548   if (liveness < 0) {
 549     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "liveness value should be non-negative");
 550   }
 551 
 552   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 553   OldRegionsLivenessClosure rli(liveness);
 554   g1h->heap_region_iterate(&rli);
 555 
 556   typeArrayOop result = oopFactory::new_longArray(3, CHECK_NULL);
 557   result->long_at_put(0, rli.total_count());
 558   result->long_at_put(1, rli.total_memory());
 559   result->long_at_put(2, rli.total_memory_to_free());
 560   return (jlongArray) JNIHandles::make_local(env, result);
 561 WB_END
 562 
 563 #endif // INCLUDE_ALL_GCS
 564 
 565 #if INCLUDE_NMT
 566 // Alloc memory using the test memory type so that we can use that to see if
 567 // NMT picks it up correctly
 568 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
 569   jlong addr = 0;
 570   addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
 571   return addr;
 572 WB_END
 573 
 574 // Alloc memory with pseudo call stack. The test can create psudo malloc
 575 // allocation site to stress the malloc tracking.
 576 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStack(JNIEnv* env, jobject o, jlong size, jint pseudo_stack))
 577   address pc = (address)(size_t)pseudo_stack;
 578   NativeCallStack stack(&pc, 1);
 579   return (jlong)(uintptr_t)os::malloc(size, mtTest, stack);
 580 WB_END
 581 
 582 // Free the memory allocated by NMTAllocTest
 583 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
 584   os::free((void*)(uintptr_t)mem);
 585 WB_END
 586 
 587 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
 588   jlong addr = 0;
 589 
 590   addr = (jlong)(uintptr_t)os::reserve_memory(size);
 591   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 592 
 593   return addr;
 594 WB_END
 595 
 596 WB_ENTRY(jlong, WB_NMTAttemptReserveMemoryAt(JNIEnv* env, jobject o, jlong addr, jlong size))
 597   addr = (jlong)(uintptr_t)os::attempt_reserve_memory_at((size_t)size, (char*)(uintptr_t)addr);
 598   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 599 
 600   return addr;
 601 WB_END
 602 
 603 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 604   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
 605   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
 606 WB_END
 607 
 608 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 609   os::uncommit_memory((char *)(uintptr_t)addr, size);
 610 WB_END
 611 
 612 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 613   os::release_memory((char *)(uintptr_t)addr, size);
 614 WB_END
 615 
 616 WB_ENTRY(jboolean, WB_NMTChangeTrackingLevel(JNIEnv* env))
 617   // Test that we can downgrade NMT levels but not upgrade them.
 618   if (MemTracker::tracking_level() == NMT_off) {
 619     MemTracker::transition_to(NMT_off);
 620     return MemTracker::tracking_level() == NMT_off;
 621   } else {
 622     assert(MemTracker::tracking_level() == NMT_detail, "Should start out as detail tracking");
 623     MemTracker::transition_to(NMT_summary);
 624     assert(MemTracker::tracking_level() == NMT_summary, "Should be summary now");
 625 
 626     // Can't go to detail once NMT is set to summary.
 627     MemTracker::transition_to(NMT_detail);
 628     assert(MemTracker::tracking_level() == NMT_summary, "Should still be summary now");
 629 
 630     // Shutdown sets tracking level to minimal.
 631     MemTracker::shutdown();
 632     assert(MemTracker::tracking_level() == NMT_minimal, "Should be minimal now");
 633 
 634     // Once the tracking level is minimal, we cannot increase to summary.
 635     // The code ignores this request instead of asserting because if the malloc site
 636     // table overflows in another thread, it tries to change the code to summary.
 637     MemTracker::transition_to(NMT_summary);
 638     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 639 
 640     // Really can never go up to detail, verify that the code would never do this.
 641     MemTracker::transition_to(NMT_detail);
 642     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 643     return MemTracker::tracking_level() == NMT_minimal;
 644   }
 645 WB_END
 646 
 647 WB_ENTRY(jint, WB_NMTGetHashSize(JNIEnv* env, jobject o))
 648   int hash_size = MallocSiteTable::hash_buckets();
 649   assert(hash_size > 0, "NMT hash_size should be > 0");
 650   return (jint)hash_size;
 651 WB_END
 652 #endif // INCLUDE_NMT
 653 
 654 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
 655   assert(method != NULL, "method should not be null");
 656   ThreadToNativeFromVM ttn(thread);
 657   return env->FromReflectedMethod(method);
 658 }
 659 
 660 // Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
 661 class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
 662  private:
 663   int _result;
 664   const bool _make_not_entrant;
 665  public:
 666   VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
 667         _result(0), _make_not_entrant(make_not_entrant) { }
 668   int  result() const { return _result; }
 669 
 670   void doit() {
 671     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 672       if (t->has_last_Java_frame()) {
 673         for (StackFrameStream fst(t, UseBiasedLocking); !fst.is_done(); fst.next()) {
 674           frame* f = fst.current();
 675           if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
 676             RegisterMap* reg_map = fst.register_map();
 677             Deoptimization::deoptimize(t, *f, reg_map);
 678             if (_make_not_entrant) {
 679                 CompiledMethod* cm = CodeCache::find_compiled(f->pc());
 680                 assert(cm != NULL, "sanity check");
 681                 cm->make_not_entrant();
 682             }
 683             ++_result;
 684           }
 685         }
 686       }
 687     }
 688   }
 689 };
 690 
 691 WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
 692   VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
 693   VMThread::execute(&op);
 694   return op.result();
 695 WB_END
 696 
 697 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
 698   MutexLockerEx mu(Compile_lock);
 699   CodeCache::mark_all_nmethods_for_deoptimization();
 700   VM_Deoptimize op;
 701   VMThread::execute(&op);
 702 WB_END
 703 
 704 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 705   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 706   int result = 0;
 707   CHECK_JNI_EXCEPTION_(env, result);
 708   MutexLockerEx mu(Compile_lock);
 709   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 710   if (is_osr) {
 711     result += mh->mark_osr_nmethods();
 712   } else if (mh->code() != NULL) {
 713     mh->code()->mark_for_deoptimization();
 714     ++result;
 715   }
 716   result += CodeCache::mark_for_deoptimization(mh());
 717   if (result > 0) {
 718     VM_Deoptimize op;
 719     VMThread::execute(&op);
 720   }
 721   return result;
 722 WB_END
 723 
 724 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 725   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 726   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 727   MutexLockerEx mu(Compile_lock);
 728   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 729   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 730   if (code == NULL) {
 731     return JNI_FALSE;
 732   }
 733   return (code->is_alive() && !code->is_marked_for_deoptimization());
 734 WB_END
 735 
 736 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 737   if (method == NULL || comp_level > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier)) {
 738     return false;
 739   }
 740   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 741   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 742   MutexLockerEx mu(Compile_lock);
 743   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 744   if (is_osr) {
 745     return CompilationPolicy::can_be_osr_compiled(mh, comp_level);
 746   } else {
 747     return CompilationPolicy::can_be_compiled(mh, comp_level);
 748   }
 749 WB_END
 750 
 751 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
 752   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 753   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 754   MutexLockerEx mu(Compile_lock);
 755   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 756   return mh->queued_for_compilation();
 757 WB_END
 758 
 759 WB_ENTRY(jboolean, WB_IsIntrinsicAvailable(JNIEnv* env, jobject o, jobject method, jobject compilation_context, jint compLevel))
 760   if (compLevel < CompLevel_none || compLevel > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier)) {
 761     return false; // Intrinsic is not available on a non-existent compilation level.
 762   }
 763   jmethodID method_id, compilation_context_id;
 764   method_id = reflected_method_to_jmid(thread, env, method);
 765   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 766   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(method_id));
 767 
 768   DirectiveSet* directive;
 769   AbstractCompiler* comp = CompileBroker::compiler((int)compLevel);
 770   assert(comp != NULL, "compiler not available");
 771   if (compilation_context != NULL) {
 772     compilation_context_id = reflected_method_to_jmid(thread, env, compilation_context);
 773     CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 774     methodHandle cch(THREAD, Method::checked_resolve_jmethod_id(compilation_context_id));
 775     directive = DirectivesStack::getMatchingDirective(cch, comp);
 776   } else {
 777     // Calling with NULL matches default directive
 778     directive = DirectivesStack::getDefaultDirective(comp);
 779   }
 780   bool result = comp->is_intrinsic_available(mh, directive);
 781   DirectivesStack::release(directive);
 782   return result;
 783 WB_END
 784 
 785 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 786   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 787   CHECK_JNI_EXCEPTION_(env, CompLevel_none);
 788   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 789   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 790   return (code != NULL ? code->comp_level() : CompLevel_none);
 791 WB_END
 792 
 793 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 794   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 795   CHECK_JNI_EXCEPTION(env);
 796   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 797   if (is_osr) {
 798     mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox");
 799   } else {
 800     mh->set_not_compilable(comp_level, true /* report */, "WhiteBox");
 801   }
 802 WB_END
 803 
 804 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 805   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 806   CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
 807   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 808   CompiledMethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 809   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 810 WB_END
 811 
 812 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 813   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 814   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 815   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 816   bool result = mh->dont_inline();
 817   mh->set_dont_inline(value == JNI_TRUE);
 818   return result;
 819 WB_END
 820 
 821 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
 822   if (comp_level == CompLevel_any) {
 823     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
 824         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
 825   } else {
 826     return CompileBroker::queue_size(comp_level);
 827   }
 828 WB_END
 829 
 830 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 831   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 832   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 833   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 834   bool result = mh->force_inline();
 835   mh->set_force_inline(value == JNI_TRUE);
 836   return result;
 837 WB_END
 838 
 839 bool WhiteBox::compile_method(Method* method, int comp_level, int bci, Thread* THREAD) {
 840   // Screen for unavailable/bad comp level or null method
 841   AbstractCompiler* comp = CompileBroker::compiler(comp_level);
 842   if (method == NULL || comp_level > MIN2((CompLevel) TieredStopAtLevel, CompLevel_highest_tier) || comp == NULL) {
 843     return false;
 844   }
 845 
 846   // Check if compilation is blocking
 847   methodHandle mh(THREAD, method);
 848   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, comp);
 849   bool is_blocking = !directive->BackgroundCompilationOption;
 850   DirectivesStack::release(directive);
 851 
 852   // Compile method and check result
 853   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), CompileTask::Reason_Whitebox, THREAD);
 854   MutexLockerEx mu(Compile_lock);
 855   return ((!is_blocking && mh->queued_for_compilation()) || nm != NULL);
 856 }
 857 
 858 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
 859   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 860   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 861   return WhiteBox::compile_method(Method::checked_resolve_jmethod_id(jmid), comp_level, bci, THREAD);
 862 WB_END
 863 
 864 WB_ENTRY(jboolean, WB_EnqueueInitializerForCompilation(JNIEnv* env, jobject o, jclass klass, jint comp_level))
 865   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
 866   return WhiteBox::compile_method(ik->class_initializer(), comp_level, InvocationEntryBci, THREAD);
 867 WB_END
 868 
 869 WB_ENTRY(jboolean, WB_ShouldPrintAssembly(JNIEnv* env, jobject o, jobject method, jint comp_level))
 870   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 871   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 872 
 873   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 874   DirectiveSet* directive = DirectivesStack::getMatchingDirective(mh, CompileBroker::compiler(comp_level));
 875   bool result = directive->PrintAssemblyOption;
 876   DirectivesStack::release(directive);
 877 
 878   return result;
 879 WB_END
 880 
 881 WB_ENTRY(jint, WB_MatchesInline(JNIEnv* env, jobject o, jobject method, jstring pattern))
 882   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 883   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 884 
 885   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 886 
 887   ResourceMark rm;
 888   const char* error_msg = NULL;
 889   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
 890   InlineMatcher* m = InlineMatcher::parse_inline_pattern(method_str, error_msg);
 891 
 892   if (m == NULL) {
 893     assert(error_msg != NULL, "Always have an error message");
 894     tty->print_cr("Got error: %s", error_msg);
 895     return -1; // Pattern failed
 896   }
 897 
 898   // Pattern works - now check if it matches
 899   int result;
 900   if (m->match(mh, InlineMatcher::force_inline)) {
 901     result = 2; // Force inline match
 902   } else if (m->match(mh, InlineMatcher::dont_inline)) {
 903     result = 1; // Dont inline match
 904   } else {
 905     result = 0; // No match
 906   }
 907   delete m;
 908   return result;
 909 WB_END
 910 
 911 WB_ENTRY(jint, WB_MatchesMethod(JNIEnv* env, jobject o, jobject method, jstring pattern))
 912   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 913   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 914 
 915   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 916 
 917   ResourceMark rm;
 918   char* method_str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(pattern));
 919 
 920   const char* error_msg = NULL;
 921 
 922   BasicMatcher* m = BasicMatcher::parse_method_pattern(method_str, error_msg);
 923   if (m == NULL) {
 924     assert(error_msg != NULL, "Must have error_msg");
 925     tty->print_cr("Got error: %s", error_msg);
 926     return -1;
 927   }
 928 
 929   // Pattern works - now check if it matches
 930   int result = m->matches(mh);
 931   delete m;
 932   assert(result == 0 || result == 1, "Result out of range");
 933   return result;
 934 WB_END
 935 
 936 static AlwaysFalseClosure always_false;
 937 
 938 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
 939   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 940   CHECK_JNI_EXCEPTION(env);
 941   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 942   MutexLockerEx mu(Compile_lock);
 943   MethodData* mdo = mh->method_data();
 944   MethodCounters* mcs = mh->method_counters();
 945 
 946   if (mdo != NULL) {
 947     mdo->init();
 948     ResourceMark rm;
 949     int arg_count = mdo->method()->size_of_parameters();
 950     for (int i = 0; i < arg_count; i++) {
 951       mdo->set_arg_modified(i, 0);
 952     }
 953     MutexLockerEx mu(mdo->extra_data_lock());
 954     mdo->clean_method_data(&always_false);
 955   }
 956 
 957   mh->clear_not_c1_compilable();
 958   mh->clear_not_c2_compilable();
 959   mh->clear_not_c2_osr_compilable();
 960   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
 961   if (mcs != NULL) {
 962     mcs->backedge_counter()->init();
 963     mcs->invocation_counter()->init();
 964     mcs->set_interpreter_invocation_count(0);
 965     mcs->set_interpreter_throwout_count(0);
 966 
 967 #ifdef TIERED
 968     mcs->set_rate(0.0F);
 969     mh->set_prev_event_count(0);
 970     mh->set_prev_time(0);
 971 #endif
 972   }
 973 WB_END
 974 
 975 template <typename T>
 976 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, Flag::Error (*TAt)(const char*, T*, bool, bool)) {
 977   if (name == NULL) {
 978     return false;
 979   }
 980   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 981   const char* flag_name = env->GetStringUTFChars(name, NULL);
 982   CHECK_JNI_EXCEPTION_(env, false);
 983   Flag::Error result = (*TAt)(flag_name, value, true, true);
 984   env->ReleaseStringUTFChars(name, flag_name);
 985   return (result == Flag::SUCCESS);
 986 }
 987 
 988 template <typename T>
 989 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, Flag::Error (*TAtPut)(const char*, T*, Flag::Flags)) {
 990   if (name == NULL) {
 991     return false;
 992   }
 993   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 994   const char* flag_name = env->GetStringUTFChars(name, NULL);
 995   CHECK_JNI_EXCEPTION_(env, false);
 996   Flag::Error result = (*TAtPut)(flag_name, value, Flag::INTERNAL);
 997   env->ReleaseStringUTFChars(name, flag_name);
 998   return (result == Flag::SUCCESS);
 999 }
1000 
1001 template <typename T>
1002 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
1003   ResourceMark rm(thread);
1004   jclass clazz = env->FindClass(name->as_C_string());
1005   CHECK_JNI_EXCEPTION_(env, NULL);
1006   jmethodID methodID = env->GetStaticMethodID(clazz,
1007         vmSymbols::valueOf_name()->as_C_string(),
1008         sig->as_C_string());
1009   CHECK_JNI_EXCEPTION_(env, NULL);
1010   jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
1011   CHECK_JNI_EXCEPTION_(env, NULL);
1012   return result;
1013 }
1014 
1015 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
1016   return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
1017 }
1018 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
1019   return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
1020 }
1021 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
1022   return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
1023 }
1024 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
1025   return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
1026 }*/
1027 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
1028   return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
1029 }
1030 
1031 static Flag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
1032   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1033   const char* flag_name = env->GetStringUTFChars(name, NULL);
1034   CHECK_JNI_EXCEPTION_(env, NULL);
1035   Flag* result = Flag::find_flag(flag_name, strlen(flag_name), true, true);
1036   env->ReleaseStringUTFChars(name, flag_name);
1037   return result;
1038 }
1039 
1040 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
1041   Flag* flag = getVMFlag(thread, env, name);
1042   return (flag != NULL) && flag->is_constant_in_binary();
1043 WB_END
1044 
1045 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
1046   Flag* flag = getVMFlag(thread, env, name);
1047   return (flag != NULL) && !(flag->is_unlocked() || flag->is_unlocker());
1048 WB_END
1049 
1050 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
1051   bool result;
1052   if (GetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAt)) {
1053     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1054     return booleanBox(thread, env, result);
1055   }
1056   return NULL;
1057 WB_END
1058 
1059 WB_ENTRY(jobject, WB_GetIntVMFlag(JNIEnv* env, jobject o, jstring name))
1060   int result;
1061   if (GetVMFlag <int> (thread, env, name, &result, &CommandLineFlags::intAt)) {
1062     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1063     return longBox(thread, env, result);
1064   }
1065   return NULL;
1066 WB_END
1067 
1068 WB_ENTRY(jobject, WB_GetUintVMFlag(JNIEnv* env, jobject o, jstring name))
1069   uint result;
1070   if (GetVMFlag <uint> (thread, env, name, &result, &CommandLineFlags::uintAt)) {
1071     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1072     return longBox(thread, env, result);
1073   }
1074   return NULL;
1075 WB_END
1076 
1077 WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
1078   intx result;
1079   if (GetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAt)) {
1080     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1081     return longBox(thread, env, result);
1082   }
1083   return NULL;
1084 WB_END
1085 
1086 WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
1087   uintx result;
1088   if (GetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAt)) {
1089     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1090     return longBox(thread, env, result);
1091   }
1092   return NULL;
1093 WB_END
1094 
1095 WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
1096   uint64_t result;
1097   if (GetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAt)) {
1098     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1099     return longBox(thread, env, result);
1100   }
1101   return NULL;
1102 WB_END
1103 
1104 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
1105   uintx result;
1106   if (GetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAt)) {
1107     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1108     return longBox(thread, env, result);
1109   }
1110   return NULL;
1111 WB_END
1112 
1113 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
1114   double result;
1115   if (GetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAt)) {
1116     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1117     return doubleBox(thread, env, result);
1118   }
1119   return NULL;
1120 WB_END
1121 
1122 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
1123   ccstr ccstrResult;
1124   if (GetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAt)) {
1125     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1126     jstring result = env->NewStringUTF(ccstrResult);
1127     CHECK_JNI_EXCEPTION_(env, NULL);
1128     return result;
1129   }
1130   return NULL;
1131 WB_END
1132 
1133 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
1134   bool result = value == JNI_TRUE ? true : false;
1135   SetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAtPut);
1136 WB_END
1137 
1138 WB_ENTRY(void, WB_SetIntVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1139   int result = value;
1140   SetVMFlag <int> (thread, env, name, &result, &CommandLineFlags::intAtPut);
1141 WB_END
1142 
1143 WB_ENTRY(void, WB_SetUintVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1144   uint result = value;
1145   SetVMFlag <uint> (thread, env, name, &result, &CommandLineFlags::uintAtPut);
1146 WB_END
1147 
1148 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1149   intx result = value;
1150   SetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAtPut);
1151 WB_END
1152 
1153 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1154   uintx result = value;
1155   SetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAtPut);
1156 WB_END
1157 
1158 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1159   uint64_t result = value;
1160   SetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAtPut);
1161 WB_END
1162 
1163 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
1164   size_t result = value;
1165   SetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAtPut);
1166 WB_END
1167 
1168 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
1169   double result = value;
1170   SetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAtPut);
1171 WB_END
1172 
1173 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
1174   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
1175   const char* ccstrValue;
1176   if (value == NULL) {
1177     ccstrValue = NULL;
1178   }
1179   else {
1180     ccstrValue = env->GetStringUTFChars(value, NULL);
1181     CHECK_JNI_EXCEPTION(env);
1182   }
1183   ccstr ccstrResult = ccstrValue;
1184   bool needFree;
1185   {
1186     ThreadInVMfromNative ttvfn(thread); // back to VM
1187     needFree = SetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAtPut);
1188   }
1189   if (value != NULL) {
1190     env->ReleaseStringUTFChars(value, ccstrValue);
1191   }
1192   if (needFree) {
1193     FREE_C_HEAP_ARRAY(char, ccstrResult);
1194   }
1195 WB_END
1196 
1197 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
1198   WhiteBox::compilation_locked = true;
1199 WB_END
1200 
1201 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
1202   MonitorLockerEx mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
1203   WhiteBox::compilation_locked = false;
1204   mo.notify_all();
1205 WB_END
1206 
1207 WB_ENTRY(void, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
1208   // Force a code cache sweep and block until it finished
1209   NMethodSweeper::force_sweep();
1210 WB_END
1211 
1212 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
1213   ResourceMark rm(THREAD);
1214   int len;
1215   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
1216   return (StringTable::lookup(name, len) != NULL);
1217 WB_END
1218 
1219 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
1220   Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1221   Universe::heap()->collect(GCCause::_wb_full_gc);
1222 #if INCLUDE_ALL_GCS
1223   if (UseG1GC) {
1224     // Needs to be cleared explicitly for G1
1225     Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1226   }
1227 #endif // INCLUDE_ALL_GCS
1228 WB_END
1229 
1230 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
1231   Universe::heap()->collect(GCCause::_wb_young_gc);
1232 WB_END
1233 
1234 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
1235   // static+volatile in order to force the read to happen
1236   // (not be eliminated by the compiler)
1237   static char c;
1238   static volatile char* p;
1239 
1240   p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
1241   if (p == NULL) {
1242     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
1243   }
1244 
1245   c = *p;
1246 WB_END
1247 
1248 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
1249   const char* features = VM_Version::features_string();
1250   ThreadToNativeFromVM ttn(thread);
1251   jstring features_string = env->NewStringUTF(features);
1252 
1253   CHECK_JNI_EXCEPTION_(env, NULL);
1254 
1255   return features_string;
1256 WB_END
1257 
1258 int WhiteBox::get_blob_type(const CodeBlob* code) {
1259   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1260   if (code->is_aot()) {
1261     return -1;
1262   }
1263   return CodeCache::get_code_heap(code)->code_blob_type();
1264 }
1265 
1266 CodeHeap* WhiteBox::get_code_heap(int blob_type) {
1267   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1268   return CodeCache::get_code_heap(blob_type);
1269 }
1270 
1271 struct CodeBlobStub {
1272   CodeBlobStub(const CodeBlob* blob) :
1273       name(os::strdup(blob->name())),
1274       size(blob->size()),
1275       blob_type(WhiteBox::get_blob_type(blob)),
1276       address((jlong) blob) { }
1277   ~CodeBlobStub() { os::free((void*) name); }
1278   const char* const name;
1279   const jint        size;
1280   const jint        blob_type;
1281   const jlong       address;
1282 };
1283 
1284 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
1285   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1286   CHECK_JNI_EXCEPTION_(env, NULL);
1287   jobjectArray result = env->NewObjectArray(4, clazz, NULL);
1288 
1289   jstring name = env->NewStringUTF(cb->name);
1290   CHECK_JNI_EXCEPTION_(env, NULL);
1291   env->SetObjectArrayElement(result, 0, name);
1292 
1293   jobject obj = integerBox(thread, env, cb->size);
1294   CHECK_JNI_EXCEPTION_(env, NULL);
1295   env->SetObjectArrayElement(result, 1, obj);
1296 
1297   obj = integerBox(thread, env, cb->blob_type);
1298   CHECK_JNI_EXCEPTION_(env, NULL);
1299   env->SetObjectArrayElement(result, 2, obj);
1300 
1301   obj = longBox(thread, env, cb->address);
1302   CHECK_JNI_EXCEPTION_(env, NULL);
1303   env->SetObjectArrayElement(result, 3, obj);
1304 
1305   return result;
1306 }
1307 
1308 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
1309   ResourceMark rm(THREAD);
1310   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1311   CHECK_JNI_EXCEPTION_(env, NULL);
1312   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
1313   CompiledMethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
1314   jobjectArray result = NULL;
1315   if (code == NULL) {
1316     return result;
1317   }
1318   int comp_level = code->comp_level();
1319   int insts_size = comp_level == CompLevel_aot ? code->code_end() - code->code_begin() : code->insts_size();
1320 
1321   ThreadToNativeFromVM ttn(thread);
1322   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1323   CHECK_JNI_EXCEPTION_(env, NULL);
1324   result = env->NewObjectArray(5, clazz, NULL);
1325   if (result == NULL) {
1326     return result;
1327   }
1328 
1329   CodeBlobStub stub(code);
1330   jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
1331   CHECK_JNI_EXCEPTION_(env, NULL);
1332   env->SetObjectArrayElement(result, 0, codeBlob);
1333 
1334   jobject level = integerBox(thread, env, comp_level);
1335   CHECK_JNI_EXCEPTION_(env, NULL);
1336   env->SetObjectArrayElement(result, 1, level);
1337 
1338   jbyteArray insts = env->NewByteArray(insts_size);
1339   CHECK_JNI_EXCEPTION_(env, NULL);
1340   env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
1341   env->SetObjectArrayElement(result, 2, insts);
1342 
1343   jobject id = integerBox(thread, env, code->compile_id());
1344   CHECK_JNI_EXCEPTION_(env, NULL);
1345   env->SetObjectArrayElement(result, 3, id);
1346 
1347   jobject entry_point = longBox(thread, env, (jlong) code->entry_point());
1348   CHECK_JNI_EXCEPTION_(env, NULL);
1349   env->SetObjectArrayElement(result, 4, entry_point);
1350 
1351   return result;
1352 WB_END
1353 
1354 CodeBlob* WhiteBox::allocate_code_blob(int size, int blob_type) {
1355   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
1356   BufferBlob* blob;
1357   int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
1358   if (full_size < size) {
1359     full_size += align_up(size - full_size, oopSize);
1360   }
1361   {
1362     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1363     blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
1364     ::new (blob) BufferBlob("WB::DummyBlob", full_size);
1365   }
1366   // Track memory usage statistic after releasing CodeCache_lock
1367   MemoryService::track_code_cache_memory_usage();
1368   return blob;
1369 }
1370 
1371 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
1372   if (size < 0) {
1373     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1374       err_msg("WB_AllocateCodeBlob: size is negative: " INT32_FORMAT, size));
1375   }
1376   return (jlong) WhiteBox::allocate_code_blob(size, blob_type);
1377 WB_END
1378 
1379 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
1380   if (addr == 0) {
1381     return;
1382   }
1383   BufferBlob::free((BufferBlob*) addr);
1384 WB_END
1385 
1386 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1387   ResourceMark rm;
1388   GrowableArray<CodeBlobStub*> blobs;
1389   {
1390     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1391     CodeHeap* heap = WhiteBox::get_code_heap(blob_type);
1392     if (heap == NULL) {
1393       return NULL;
1394     }
1395     for (CodeBlob* cb = (CodeBlob*) heap->first();
1396          cb != NULL; cb = (CodeBlob*) heap->next(cb)) {
1397       CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1398       new (stub) CodeBlobStub(cb);
1399       blobs.append(stub);
1400     }
1401   }
1402   if (blobs.length() == 0) {
1403     return NULL;
1404   }
1405   ThreadToNativeFromVM ttn(thread);
1406   jobjectArray result = NULL;
1407   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1408   CHECK_JNI_EXCEPTION_(env, NULL);
1409   result = env->NewObjectArray(blobs.length(), clazz, NULL);
1410   CHECK_JNI_EXCEPTION_(env, NULL);
1411   if (result == NULL) {
1412     return result;
1413   }
1414   int i = 0;
1415   for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1416        it != blobs.end(); ++it) {
1417     jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1418     CHECK_JNI_EXCEPTION_(env, NULL);
1419     env->SetObjectArrayElement(result, i, obj);
1420     CHECK_JNI_EXCEPTION_(env, NULL);
1421     ++i;
1422   }
1423   return result;
1424 WB_END
1425 
1426 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1427   return CompileBroker::get_compilation_activity_mode();
1428 WB_END
1429 
1430 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1431   if (addr == 0) {
1432     THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(),
1433       "WB_GetCodeBlob: addr is null");
1434   }
1435   ThreadToNativeFromVM ttn(thread);
1436   CodeBlobStub stub((CodeBlob*) addr);
1437   return codeBlob2objectArray(thread, env, &stub);
1438 WB_END
1439 
1440 WB_ENTRY(jlong, WB_GetMethodData(JNIEnv* env, jobject wv, jobject method))
1441   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1442   CHECK_JNI_EXCEPTION_(env, 0);
1443   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1444   return (jlong) mh->method_data();
1445 WB_END
1446 
1447 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1448   return (jlong) Thread::current()->stack_size();
1449 WB_END
1450 
1451 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1452   JavaThread* t = JavaThread::current();
1453   return (jlong) t->stack_available(os::current_stack_pointer()) - (jlong)JavaThread::stack_shadow_zone_size();
1454 WB_END
1455 
1456 
1457 int WhiteBox::array_bytes_to_length(size_t bytes) {
1458   return Array<u1>::bytes_to_length(bytes);
1459 }
1460 
1461 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1462   if (size < 0) {
1463     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1464         err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1465   }
1466 
1467   oop class_loader_oop = JNIHandles::resolve(class_loader);
1468   ClassLoaderData* cld = class_loader_oop != NULL
1469       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1470       : ClassLoaderData::the_null_class_loader_data();
1471 
1472   void* metadata = MetadataFactory::new_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1473 
1474   return (jlong)(uintptr_t)metadata;
1475 WB_END
1476 
1477 WB_ENTRY(void, WB_FreeMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong addr, jlong size))
1478   oop class_loader_oop = JNIHandles::resolve(class_loader);
1479   ClassLoaderData* cld = class_loader_oop != NULL
1480       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1481       : ClassLoaderData::the_null_class_loader_data();
1482 
1483   MetadataFactory::free_array(cld, (Array<u1>*)(uintptr_t)addr);
1484 WB_END
1485 
1486 WB_ENTRY(void, WB_DefineModule(JNIEnv* env, jobject o, jobject module, jboolean is_open,
1487                                 jstring version, jstring location, jobjectArray packages))
1488   ResourceMark rm(THREAD);
1489 
1490   objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages));
1491   objArrayHandle packages_h(THREAD, packages_oop);
1492   int num_packages = (packages_h == NULL ? 0 : packages_h->length());
1493 
1494   char** pkgs = NULL;
1495   if (num_packages > 0) {
1496     pkgs = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char*, num_packages);
1497     for (int x = 0; x < num_packages; x++) {
1498       oop pkg_str = packages_h->obj_at(x);
1499       if (pkg_str == NULL || !pkg_str->is_a(SystemDictionary::String_klass())) {
1500         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1501                   err_msg("Bad package name"));
1502       }
1503       pkgs[x] = java_lang_String::as_utf8_string(pkg_str);
1504     }
1505   }
1506   Modules::define_module(module, is_open, version, location, (const char* const*)pkgs, num_packages, CHECK);
1507 WB_END
1508 
1509 WB_ENTRY(void, WB_AddModuleExports(JNIEnv* env, jobject o, jobject from_module, jstring package, jobject to_module))
1510   ResourceMark rm(THREAD);
1511   char* package_name = NULL;
1512   if (package != NULL) {
1513       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1514   }
1515   Modules::add_module_exports_qualified(from_module, package_name, to_module, CHECK);
1516 WB_END
1517 
1518 WB_ENTRY(void, WB_AddModuleExportsToAllUnnamed(JNIEnv* env, jobject o, jclass module, jstring package))
1519   ResourceMark rm(THREAD);
1520   char* package_name = NULL;
1521   if (package != NULL) {
1522       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1523   }
1524   Modules::add_module_exports_to_all_unnamed(module, package_name, CHECK);
1525 WB_END
1526 
1527 WB_ENTRY(void, WB_AddModuleExportsToAll(JNIEnv* env, jobject o, jclass module, jstring package))
1528   ResourceMark rm(THREAD);
1529   char* package_name = NULL;
1530   if (package != NULL) {
1531       package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package));
1532   }
1533   Modules::add_module_exports(module, package_name, NULL, CHECK);
1534 WB_END
1535 
1536 WB_ENTRY(void, WB_AddReadsModule(JNIEnv* env, jobject o, jobject from_module, jobject source_module))
1537   Modules::add_reads_module(from_module, source_module, CHECK);
1538 WB_END
1539 
1540 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1541   if (inc < 0) {
1542     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1543         err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1544   }
1545 
1546   jlong max_size_t = (jlong) ((size_t) -1);
1547   if (inc > max_size_t) {
1548     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1549         err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1550   }
1551 
1552   size_t new_cap_until_GC = 0;
1553   size_t aligned_inc = align_down((size_t) inc, Metaspace::commit_alignment());
1554   bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1555   if (!success) {
1556     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1557                 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1558                 "due to contention with another thread");
1559   }
1560   return (jlong) new_cap_until_GC;
1561 WB_END
1562 
1563 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1564   return (jlong) MetaspaceGC::capacity_until_GC();
1565 WB_END
1566 
1567 WB_ENTRY(jboolean, WB_MetaspaceShouldConcurrentCollect(JNIEnv* env, jobject wb))
1568   return MetaspaceGC::should_concurrent_collect();
1569 WB_END
1570 
1571 WB_ENTRY(jlong, WB_MetaspaceReserveAlignment(JNIEnv* env, jobject wb))
1572   return (jlong)Metaspace::reserve_alignment();
1573 WB_END
1574 
1575 WB_ENTRY(void, WB_AssertMatchingSafepointCalls(JNIEnv* env, jobject o, jboolean mutexSafepointValue, jboolean attemptedNoSafepointValue))
1576   Monitor::SafepointCheckRequired sfpt_check_required = mutexSafepointValue ?
1577                                            Monitor::_safepoint_check_always :
1578                                            Monitor::_safepoint_check_never;
1579   MutexLockerEx ml(new Mutex(Mutex::leaf, "SFPT_Test_lock", true, sfpt_check_required),
1580                    attemptedNoSafepointValue == JNI_TRUE);
1581 WB_END
1582 
1583 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1584   oop obj_oop = JNIHandles::resolve(obj);
1585   return (jboolean) obj_oop->mark()->has_monitor();
1586 WB_END
1587 
1588 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1589   VM_ForceSafepoint force_safepoint_op;
1590   VMThread::execute(&force_safepoint_op);
1591 WB_END
1592 
1593 WB_ENTRY(jlong, WB_GetConstantPool(JNIEnv* env, jobject wb, jclass klass))
1594   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1595   return (jlong) ik->constants();
1596 WB_END
1597 
1598 WB_ENTRY(jint, WB_GetConstantPoolCacheIndexTag(JNIEnv* env, jobject wb))
1599   return ConstantPool::CPCACHE_INDEX_TAG;
1600 WB_END
1601 
1602 WB_ENTRY(jint, WB_GetConstantPoolCacheLength(JNIEnv* env, jobject wb, jclass klass))
1603   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1604   ConstantPool* cp = ik->constants();
1605   if (cp->cache() == NULL) {
1606       return -1;
1607   }
1608   return cp->cache()->length();
1609 WB_END
1610 
1611 WB_ENTRY(jint, WB_ConstantPoolRemapInstructionOperandFromCache(JNIEnv* env, jobject wb, jclass klass, jint index))
1612   InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve(klass)));
1613   ConstantPool* cp = ik->constants();
1614   if (cp->cache() == NULL) {
1615     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(), "Constant pool does not have a cache");
1616   }
1617   jint cpci = index;
1618   jint cpciTag = ConstantPool::CPCACHE_INDEX_TAG;
1619   if (cpciTag > cpci || cpci >= cp->cache()->length() + cpciTag) {
1620     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool cache index is out of range");
1621   }
1622   jint cpi = cp->remap_instruction_operand_from_cache(cpci);
1623   return cpi;
1624 WB_END
1625 
1626 WB_ENTRY(jint, WB_ConstantPoolEncodeIndyIndex(JNIEnv* env, jobject wb, jint index))
1627   return ConstantPool::encode_invokedynamic_index(index);
1628 WB_END
1629 
1630 WB_ENTRY(void, WB_ClearInlineCaches(JNIEnv* env, jobject wb, jboolean preserve_static_stubs))
1631   VM_ClearICs clear_ics(preserve_static_stubs == JNI_TRUE);
1632   VMThread::execute(&clear_ics);
1633 WB_END
1634 
1635 template <typename T>
1636 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1637   assert(value != NULL, "sanity");
1638   if (method == NULL || name == NULL) {
1639     return false;
1640   }
1641   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1642   CHECK_JNI_EXCEPTION_(env, false);
1643   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1644   // can't be in VM when we call JNI
1645   ThreadToNativeFromVM ttnfv(thread);
1646   const char* flag_name = env->GetStringUTFChars(name, NULL);
1647   CHECK_JNI_EXCEPTION_(env, false);
1648   bool result =  CompilerOracle::has_option_value(mh, flag_name, *value);
1649   env->ReleaseStringUTFChars(name, flag_name);
1650   return result;
1651 }
1652 
1653 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1654   bool result;
1655   if (GetMethodOption<bool> (thread, env, method, name, &result)) {
1656     // can't be in VM when we call JNI
1657     ThreadToNativeFromVM ttnfv(thread);
1658     return booleanBox(thread, env, result);
1659   }
1660   return NULL;
1661 WB_END
1662 
1663 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1664   intx result;
1665   if (GetMethodOption <intx> (thread, env, method, name, &result)) {
1666     // can't be in VM when we call JNI
1667     ThreadToNativeFromVM ttnfv(thread);
1668     return longBox(thread, env, result);
1669   }
1670   return NULL;
1671 WB_END
1672 
1673 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1674   uintx result;
1675   if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
1676     // can't be in VM when we call JNI
1677     ThreadToNativeFromVM ttnfv(thread);
1678     return longBox(thread, env, result);
1679   }
1680   return NULL;
1681 WB_END
1682 
1683 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1684   double result;
1685   if (GetMethodOption <double> (thread, env, method, name, &result)) {
1686     // can't be in VM when we call JNI
1687     ThreadToNativeFromVM ttnfv(thread);
1688     return doubleBox(thread, env, result);
1689   }
1690   return NULL;
1691 WB_END
1692 
1693 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1694   ccstr ccstrResult;
1695   if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
1696     // can't be in VM when we call JNI
1697     ThreadToNativeFromVM ttnfv(thread);
1698     jstring result = env->NewStringUTF(ccstrResult);
1699     CHECK_JNI_EXCEPTION_(env, NULL);
1700     return result;
1701   }
1702   return NULL;
1703 WB_END
1704 
1705 WB_ENTRY(jboolean, WB_IsShared(JNIEnv* env, jobject wb, jobject obj))
1706   oop obj_oop = JNIHandles::resolve(obj);
1707   return oopDesc::is_archive_object(obj_oop);
1708 WB_END
1709 
1710 WB_ENTRY(jboolean, WB_IsSharedClass(JNIEnv* env, jobject wb, jclass clazz))
1711   return (jboolean)MetaspaceShared::is_in_shared_metaspace(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
1712 WB_END
1713 
1714 WB_ENTRY(jboolean, WB_AreSharedStringsIgnored(JNIEnv* env))
1715   return !StringTable::shared_string_mapped();
1716 WB_END
1717 
1718 WB_ENTRY(jobject, WB_GetResolvedReferences(JNIEnv* env, jobject wb, jclass clazz))
1719   Klass *k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1720   if (k->is_instance_klass()) {
1721     InstanceKlass *ik = InstanceKlass::cast(k);
1722     ConstantPool *cp = ik->constants();
1723     objArrayOop refs =  cp->resolved_references();
1724     return (jobject)JNIHandles::make_local(env, refs);
1725   } else {
1726     return NULL;
1727   }
1728 WB_END
1729 
1730 WB_ENTRY(jboolean, WB_AreOpenArchiveHeapObjectsMapped(JNIEnv* env))
1731   return MetaspaceShared::open_archive_heap_region_mapped();
1732 WB_END
1733 
1734 WB_ENTRY(jboolean, WB_IsCDSIncludedInVmBuild(JNIEnv* env))
1735 #if INCLUDE_CDS
1736 # ifdef _LP64
1737     if (!UseCompressedOops || !UseCompressedClassPointers) {
1738       // On 64-bit VMs, CDS is supported only with compressed oops/pointers
1739       return false;
1740     }
1741 # endif // _LP64
1742   return true;
1743 #else
1744   return false;
1745 #endif // INCLUDE_CDS
1746 WB_END
1747 
1748 WB_ENTRY(jboolean, WB_IsJavaHeapArchiveSupported(JNIEnv* env))
1749   return MetaspaceShared::is_heap_object_archiving_allowed();
1750 WB_END
1751 
1752 
1753 #if INCLUDE_CDS
1754 
1755 WB_ENTRY(jint, WB_GetOffsetForName(JNIEnv* env, jobject o, jstring name))
1756   ResourceMark rm;
1757   char* c_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
1758   int result = CDSOffsets::find_offset(c_name);
1759   return (jint)result;
1760 WB_END
1761 
1762 #endif // INCLUDE_CDS
1763 
1764 WB_ENTRY(jint, WB_HandshakeWalkStack(JNIEnv* env, jobject wb, jobject thread_handle, jboolean all_threads))
1765   class TraceSelfClosure : public ThreadClosure {
1766     jint _num_threads_completed;
1767 
1768     void do_thread(Thread* th) {
1769       assert(th->is_Java_thread(), "sanity");
1770       JavaThread* jt = (JavaThread*)th;
1771       ResourceMark rm;
1772 
1773       jt->print_on(tty);
1774       jt->print_stack_on(tty);
1775       tty->cr();
1776       Atomic::inc(&_num_threads_completed);
1777     }
1778 
1779   public:
1780     TraceSelfClosure() : _num_threads_completed(0) {}
1781 
1782     jint num_threads_completed() const { return _num_threads_completed; }
1783   };
1784   TraceSelfClosure tsc;
1785 
1786   if (all_threads) {
1787     Handshake::execute(&tsc);
1788   } else {
1789     oop thread_oop = JNIHandles::resolve(thread_handle);
1790     if (thread_oop != NULL) {
1791       JavaThread* target = java_lang_Thread::thread(thread_oop);
1792       Handshake::execute(&tsc, target);
1793     }
1794   }
1795   return tsc.num_threads_completed();
1796 WB_END
1797 
1798 //Some convenience methods to deal with objects from java
1799 int WhiteBox::offset_for_field(const char* field_name, oop object,
1800     Symbol* signature_symbol) {
1801   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
1802   Thread* THREAD = Thread::current();
1803 
1804   //Get the class of our object
1805   Klass* arg_klass = object->klass();
1806   //Turn it into an instance-klass
1807   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
1808 
1809   //Create symbols to look for in the class
1810   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
1811       THREAD);
1812 
1813   //To be filled in with an offset of the field we're looking for
1814   fieldDescriptor fd;
1815 
1816   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
1817   if (res == NULL) {
1818     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
1819         name_symbol->as_C_string());
1820     vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
1821   }
1822 
1823   //fetch the field at the offset we've found
1824   int dest_offset = fd.offset();
1825 
1826   return dest_offset;
1827 }
1828 
1829 
1830 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
1831   int offset = offset_for_field(field_name, object,
1832       vmSymbols::string_signature());
1833   oop string = object->obj_field(offset);
1834   if (string == NULL) {
1835     return NULL;
1836   }
1837   const char* ret = java_lang_String::as_utf8_string(string);
1838   return ret;
1839 }
1840 
1841 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
1842   int offset =
1843       offset_for_field(field_name, object, vmSymbols::bool_signature());
1844   bool ret = (object->bool_field(offset) == JNI_TRUE);
1845   return ret;
1846 }
1847 
1848 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
1849   ResourceMark rm;
1850   ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1851 
1852   //  one by one registration natives for exception catching
1853   jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
1854   CHECK_JNI_EXCEPTION(env);
1855   for (int i = 0, n = method_count; i < n; ++i) {
1856     // Skip dummy entries
1857     if (method_array[i].fnPtr == NULL) continue;
1858     if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
1859       jthrowable throwable_obj = env->ExceptionOccurred();
1860       if (throwable_obj != NULL) {
1861         env->ExceptionClear();
1862         if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
1863           // NoSuchMethodError is thrown when a method can't be found or a method is not native.
1864           // Ignoring the exception since it is not preventing use of other WhiteBox methods.
1865           tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s",
1866               method_array[i].name, method_array[i].signature);
1867         }
1868       } else {
1869         // Registration failed unexpectedly.
1870         tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered",
1871             method_array[i].name, method_array[i].signature);
1872         env->UnregisterNatives(wbclass);
1873         break;
1874       }
1875     }
1876   }
1877 }
1878 
1879 WB_ENTRY(jint, WB_AddCompilerDirective(JNIEnv* env, jobject o, jstring compDirect))
1880   // can't be in VM when we call JNI
1881   ThreadToNativeFromVM ttnfv(thread);
1882   const char* dir = env->GetStringUTFChars(compDirect, NULL);
1883   CHECK_JNI_EXCEPTION_(env, 0);
1884   int ret;
1885   {
1886     ThreadInVMfromNative ttvfn(thread); // back to VM
1887     ret = DirectivesParser::parse_string(dir, tty);
1888   }
1889   env->ReleaseStringUTFChars(compDirect, dir);
1890   // -1 for error parsing directive. Return 0 as number of directives added.
1891   if (ret == -1) {
1892     ret = 0;
1893   }
1894   return (jint) ret;
1895 WB_END
1896 
1897 WB_ENTRY(void, WB_RemoveCompilerDirective(JNIEnv* env, jobject o, jint count))
1898   DirectivesStack::pop(count);
1899 WB_END
1900 
1901 // Checks that the library libfile has the noexecstack bit set.
1902 WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile))
1903   jboolean ret = false;
1904 #ifdef LINUX
1905   // Can't be in VM when we call JNI.
1906   ThreadToNativeFromVM ttnfv(thread);
1907   const char* lf = env->GetStringUTFChars(libfile, NULL);
1908   CHECK_JNI_EXCEPTION_(env, 0);
1909   ret = (jboolean) ElfFile::specifies_noexecstack(lf);
1910   env->ReleaseStringUTFChars(libfile, lf);
1911 #endif
1912   return ret;
1913 WB_END
1914 
1915 WB_ENTRY(jboolean, WB_IsContainerized(JNIEnv* env, jobject o))
1916   LINUX_ONLY(return OSContainer::is_containerized();)
1917   return false;
1918 WB_END
1919 
1920 WB_ENTRY(void, WB_PrintOsInfo(JNIEnv* env, jobject o))
1921   os::print_os_info(tty);
1922 WB_END
1923 
1924 // Elf decoder
1925 WB_ENTRY(void, WB_DisableElfSectionCache(JNIEnv* env))
1926 #if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
1927   ElfFile::_do_not_cache_elf_section = true;
1928 #endif
1929 WB_END
1930 
1931 
1932 #define CC (char*)
1933 
1934 static JNINativeMethod methods[] = {
1935   {CC"getObjectAddress0",                CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
1936   {CC"getObjectSize0",                   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize     },
1937   {CC"isObjectInOldGen0",                CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen  },
1938   {CC"getHeapOopSize",                   CC"()I",                   (void*)&WB_GetHeapOopSize    },
1939   {CC"getVMPageSize",                    CC"()I",                   (void*)&WB_GetVMPageSize     },
1940   {CC"getVMAllocationGranularity",       CC"()J",                   (void*)&WB_GetVMAllocationGranularity },
1941   {CC"getVMLargePageSize",               CC"()J",                   (void*)&WB_GetVMLargePageSize},
1942   {CC"getHeapSpaceAlignment",            CC"()J",                   (void*)&WB_GetHeapSpaceAlignment},
1943   {CC"getHeapAlignment",                 CC"()J",                   (void*)&WB_GetHeapAlignment},
1944   {CC"isClassAlive0",                    CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
1945   {CC"parseCommandLine0",
1946       CC"(Ljava/lang/String;C[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
1947       (void*) &WB_ParseCommandLine
1948   },
1949   {CC"addToBootstrapClassLoaderSearch0", CC"(Ljava/lang/String;)V",
1950                                                       (void*)&WB_AddToBootstrapClassLoaderSearch},
1951   {CC"addToSystemClassLoaderSearch0",    CC"(Ljava/lang/String;)V",
1952                                                       (void*)&WB_AddToSystemClassLoaderSearch},
1953   {CC"getCompressedOopsMaxHeapSize", CC"()J",
1954       (void*)&WB_GetCompressedOopsMaxHeapSize},
1955   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
1956   {CC"runMemoryUnitTests", CC"()V",                   (void*)&WB_RunMemoryUnitTests},
1957   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
1958   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
1959 #if INCLUDE_CDS
1960   {CC"getOffsetForName0", CC"(Ljava/lang/String;)I",  (void*)&WB_GetOffsetForName},
1961 #endif
1962 #if INCLUDE_ALL_GCS
1963   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
1964   {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
1965   {CC"g1BelongsToHumongousRegion0", CC"(J)Z",         (void*)&WB_G1BelongsToHumongousRegion},
1966   {CC"g1BelongsToFreeRegion0", CC"(J)Z",              (void*)&WB_G1BelongsToFreeRegion},
1967   {CC"g1NumMaxRegions",    CC"()J",                   (void*)&WB_G1NumMaxRegions  },
1968   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
1969   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
1970   {CC"g1StartConcMarkCycle",       CC"()Z",           (void*)&WB_G1StartMarkCycle  },
1971   {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;",
1972                                                       (void*)&WB_G1AuxiliaryMemoryUsage  },
1973   {CC"psVirtualSpaceAlignment",CC"()J",               (void*)&WB_PSVirtualSpaceAlignment},
1974   {CC"psHeapGenerationAlignment",CC"()J",             (void*)&WB_PSHeapGenerationAlignment},
1975   {CC"g1GetMixedGCInfo",   CC"(I)[J",                 (void*)&WB_G1GetMixedGCInfo },
1976 #endif // INCLUDE_ALL_GCS
1977 #if INCLUDE_NMT
1978   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
1979   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
1980   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
1981   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
1982   {CC"NMTAttemptReserveMemoryAt",    CC"(JJ)J",       (void*)&WB_NMTAttemptReserveMemoryAt },
1983   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
1984   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
1985   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
1986   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
1987   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
1988 #endif // INCLUDE_NMT
1989   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
1990   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
1991   {CC"deoptimizeMethod0",   CC"(Ljava/lang/reflect/Executable;Z)I",
1992                                                       (void*)&WB_DeoptimizeMethod  },
1993   {CC"isMethodCompiled0",   CC"(Ljava/lang/reflect/Executable;Z)Z",
1994                                                       (void*)&WB_IsMethodCompiled  },
1995   {CC"isMethodCompilable0", CC"(Ljava/lang/reflect/Executable;IZ)Z",
1996                                                       (void*)&WB_IsMethodCompilable},
1997   {CC"isMethodQueuedForCompilation0",
1998       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
1999   {CC"isIntrinsicAvailable0",
2000       CC"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Executable;I)Z",
2001                                                       (void*)&WB_IsIntrinsicAvailable},
2002   {CC"makeMethodNotCompilable0",
2003       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
2004   {CC"testSetDontInlineMethod0",
2005       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
2006   {CC"getMethodCompilationLevel0",
2007       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
2008   {CC"getMethodEntryBci0",
2009       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
2010   {CC"getCompileQueueSize",
2011       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
2012   {CC"testSetForceInlineMethod0",
2013       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
2014   {CC"enqueueMethodForCompilation0",
2015       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
2016   {CC"enqueueInitializerForCompilation0",
2017       CC"(Ljava/lang/Class;I)Z",                      (void*)&WB_EnqueueInitializerForCompilation},
2018   {CC"clearMethodState0",
2019       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
2020   {CC"lockCompilation",    CC"()V",                   (void*)&WB_LockCompilation},
2021   {CC"unlockCompilation",  CC"()V",                   (void*)&WB_UnlockCompilation},
2022   {CC"matchesMethod",
2023       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2024                                                       (void*)&WB_MatchesMethod},
2025   {CC"matchesInline",
2026       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)I",
2027                                                       (void*)&WB_MatchesInline},
2028   {CC"shouldPrintAssembly",
2029         CC"(Ljava/lang/reflect/Executable;I)Z",
2030                                                         (void*)&WB_ShouldPrintAssembly},
2031 
2032   {CC"isConstantVMFlag",   CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
2033   {CC"isLockedVMFlag",     CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
2034   {CC"setBooleanVMFlag",   CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
2035   {CC"setIntVMFlag",       CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntVMFlag},
2036   {CC"setUintVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintVMFlag},
2037   {CC"setIntxVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
2038   {CC"setUintxVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
2039   {CC"setUint64VMFlag",    CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
2040   {CC"setSizeTVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
2041   {CC"setDoubleVMFlag",    CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
2042   {CC"setStringVMFlag",    CC"(Ljava/lang/String;Ljava/lang/String;)V",
2043                                                       (void*)&WB_SetStringVMFlag},
2044   {CC"getBooleanVMFlag",   CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
2045                                                       (void*)&WB_GetBooleanVMFlag},
2046   {CC"getIntVMFlag",       CC"(Ljava/lang/String;)Ljava/lang/Long;",
2047                                                       (void*)&WB_GetIntVMFlag},
2048   {CC"getUintVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2049                                                       (void*)&WB_GetUintVMFlag},
2050   {CC"getIntxVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
2051                                                       (void*)&WB_GetIntxVMFlag},
2052   {CC"getUintxVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2053                                                       (void*)&WB_GetUintxVMFlag},
2054   {CC"getUint64VMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Long;",
2055                                                       (void*)&WB_GetUint64VMFlag},
2056   {CC"getSizeTVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
2057                                                       (void*)&WB_GetSizeTVMFlag},
2058   {CC"getDoubleVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Double;",
2059                                                       (void*)&WB_GetDoubleVMFlag},
2060   {CC"getStringVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/String;",
2061                                                       (void*)&WB_GetStringVMFlag},
2062   {CC"isInStringTable",    CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable  },
2063   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
2064   {CC"youngGC",  CC"()V",                             (void*)&WB_YoungGC },
2065   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
2066   {CC"allocateMetaspace",
2067      CC"(Ljava/lang/ClassLoader;J)J",                 (void*)&WB_AllocateMetaspace },
2068   {CC"freeMetaspace",
2069      CC"(Ljava/lang/ClassLoader;JJ)V",                (void*)&WB_FreeMetaspace },
2070   {CC"incMetaspaceCapacityUntilGC", CC"(J)J",         (void*)&WB_IncMetaspaceCapacityUntilGC },
2071   {CC"metaspaceCapacityUntilGC", CC"()J",             (void*)&WB_MetaspaceCapacityUntilGC },
2072   {CC"metaspaceShouldConcurrentCollect", CC"()Z",     (void*)&WB_MetaspaceShouldConcurrentCollect },
2073   {CC"metaspaceReserveAlignment", CC"()J",            (void*)&WB_MetaspaceReserveAlignment },
2074   {CC"getCPUFeatures",     CC"()Ljava/lang/String;",  (void*)&WB_GetCPUFeatures     },
2075   {CC"getNMethod0",         CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
2076                                                       (void*)&WB_GetNMethod         },
2077   {CC"forceNMethodSweep",  CC"()V",                   (void*)&WB_ForceNMethodSweep  },
2078   {CC"allocateCodeBlob",   CC"(II)J",                 (void*)&WB_AllocateCodeBlob   },
2079   {CC"freeCodeBlob",       CC"(J)V",                  (void*)&WB_FreeCodeBlob       },
2080   {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
2081   {CC"getCompilationActivityMode",
2082                            CC"()I",                   (void*)&WB_GetCompilationActivityMode},
2083   {CC"getMethodData0",     CC"(Ljava/lang/reflect/Executable;)J",
2084                                                       (void*)&WB_GetMethodData      },
2085   {CC"getCodeBlob",        CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob        },
2086   {CC"getThreadStackSize", CC"()J",                   (void*)&WB_GetThreadStackSize },
2087   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
2088   {CC"DefineModule",       CC"(Ljava/lang/Object;ZLjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V",
2089                                                       (void*)&WB_DefineModule },
2090   {CC"AddModuleExports",   CC"(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V",
2091                                                       (void*)&WB_AddModuleExports },
2092   {CC"AddReadsModule",     CC"(Ljava/lang/Object;Ljava/lang/Object;)V",
2093                                                       (void*)&WB_AddReadsModule },
2094   {CC"AddModuleExportsToAllUnnamed", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2095                                                       (void*)&WB_AddModuleExportsToAllUnnamed },
2096   {CC"AddModuleExportsToAll", CC"(Ljava/lang/Object;Ljava/lang/String;)V",
2097                                                       (void*)&WB_AddModuleExportsToAll },
2098   {CC"assertMatchingSafepointCalls", CC"(ZZ)V",       (void*)&WB_AssertMatchingSafepointCalls },
2099   {CC"isMonitorInflated0", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated  },
2100   {CC"forceSafepoint",     CC"()V",                   (void*)&WB_ForceSafepoint     },
2101   {CC"getConstantPool0",   CC"(Ljava/lang/Class;)J",  (void*)&WB_GetConstantPool    },
2102   {CC"getConstantPoolCacheIndexTag0", CC"()I",  (void*)&WB_GetConstantPoolCacheIndexTag},
2103   {CC"getConstantPoolCacheLength0", CC"(Ljava/lang/Class;)I",  (void*)&WB_GetConstantPoolCacheLength},
2104   {CC"remapInstructionOperandFromCPCache0",
2105       CC"(Ljava/lang/Class;I)I",                      (void*)&WB_ConstantPoolRemapInstructionOperandFromCache},
2106   {CC"encodeConstantPoolIndyIndex0",
2107       CC"(I)I",                      (void*)&WB_ConstantPoolEncodeIndyIndex},
2108   {CC"getMethodBooleanOption",
2109       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
2110                                                       (void*)&WB_GetMethodBooleaneOption},
2111   {CC"getMethodIntxOption",
2112       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2113                                                       (void*)&WB_GetMethodIntxOption},
2114   {CC"getMethodUintxOption",
2115       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
2116                                                       (void*)&WB_GetMethodUintxOption},
2117   {CC"getMethodDoubleOption",
2118       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
2119                                                       (void*)&WB_GetMethodDoubleOption},
2120   {CC"getMethodStringOption",
2121       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
2122                                                       (void*)&WB_GetMethodStringOption},
2123   {CC"isShared",           CC"(Ljava/lang/Object;)Z", (void*)&WB_IsShared },
2124   {CC"isSharedClass",      CC"(Ljava/lang/Class;)Z",  (void*)&WB_IsSharedClass },
2125   {CC"areSharedStringsIgnored",           CC"()Z",    (void*)&WB_AreSharedStringsIgnored },
2126   {CC"getResolvedReferences", CC"(Ljava/lang/Class;)Ljava/lang/Object;", (void*)&WB_GetResolvedReferences},
2127   {CC"areOpenArchiveHeapObjectsMapped",   CC"()Z",    (void*)&WB_AreOpenArchiveHeapObjectsMapped},
2128   {CC"isCDSIncludedInVmBuild",            CC"()Z",    (void*)&WB_IsCDSIncludedInVmBuild },
2129   {CC"isJavaHeapArchiveSupported",      CC"()Z",      (void*)&WB_IsJavaHeapArchiveSupported },
2130 
2131   {CC"clearInlineCaches0",  CC"(Z)V",                 (void*)&WB_ClearInlineCaches },
2132   {CC"handshakeWalkStack", CC"(Ljava/lang/Thread;Z)I", (void*)&WB_HandshakeWalkStack },
2133   {CC"addCompilerDirective",    CC"(Ljava/lang/String;)I",
2134                                                       (void*)&WB_AddCompilerDirective },
2135   {CC"removeCompilerDirective",   CC"(I)V",           (void*)&WB_RemoveCompilerDirective },
2136   {CC"isGCSupported",             CC"(I)Z",           (void*)&WB_IsGCSupported},
2137   {CC"isGCSelected",              CC"(I)Z",           (void*)&WB_IsGCSelected},
2138   {CC"isGCSelectedErgonomically", CC"()Z",            (void*)&WB_IsGCSelectedErgonomically},
2139   {CC"supportsConcurrentGCPhaseControl", CC"()Z",     (void*)&WB_SupportsConcurrentGCPhaseControl},
2140   {CC"getConcurrentGCPhases",     CC"()[Ljava/lang/String;",
2141                                                       (void*)&WB_GetConcurrentGCPhases},
2142   {CC"requestConcurrentGCPhase0", CC"(Ljava/lang/String;)Z",
2143                                                       (void*)&WB_RequestConcurrentGCPhase},
2144   {CC"checkLibSpecifiesNoexecstack", CC"(Ljava/lang/String;)Z",
2145                                                       (void*)&WB_CheckLibSpecifiesNoexecstack},
2146   {CC"isContainerized",           CC"()Z",            (void*)&WB_IsContainerized },
2147   {CC"printOsInfo",               CC"()V",            (void*)&WB_PrintOsInfo },
2148   {CC"disableElfSectionCache",    CC"()V",            (void*)&WB_DisableElfSectionCache },
2149 };
2150 
2151 
2152 #undef CC
2153 
2154 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
2155   {
2156     if (WhiteBoxAPI) {
2157       // Make sure that wbclass is loaded by the null classloader
2158       InstanceKlass* ik = InstanceKlass::cast(JNIHandles::resolve(wbclass)->klass());
2159       Handle loader(THREAD, ik->class_loader());
2160       if (loader.is_null()) {
2161         WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
2162         WhiteBox::set_used();
2163       }
2164     }
2165   }
2166 JVM_END
--- EOF ---