whitebox.cpp revision 6760:22b98ab2a69f
1/* 2 * Copyright (c) 2012, 2014, 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 "code/codeCache.hpp" 28 29#include "memory/universe.hpp" 30#include "oops/oop.inline.hpp" 31 32#include "classfile/stringTable.hpp" 33#include "classfile/classLoaderData.hpp" 34 35#include "prims/whitebox.hpp" 36#include "prims/wbtestmethods/parserTests.hpp" 37 38#include "runtime/thread.hpp" 39#include "runtime/arguments.hpp" 40#include "runtime/interfaceSupport.hpp" 41#include "runtime/os.hpp" 42#include "runtime/vm_version.hpp" 43 44#include "utilities/debug.hpp" 45#include "utilities/macros.hpp" 46#include "utilities/exceptions.hpp" 47 48#if INCLUDE_ALL_GCS 49#include "gc_implementation/g1/concurrentMark.hpp" 50#include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 51#include "gc_implementation/g1/heapRegionRemSet.hpp" 52#endif // INCLUDE_ALL_GCS 53 54#ifdef INCLUDE_NMT 55#include "services/memTracker.hpp" 56#endif // INCLUDE_NMT 57 58#include "compiler/compileBroker.hpp" 59#include "runtime/compilationPolicy.hpp" 60 61PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 62 63#define SIZE_T_MAX_VALUE ((size_t) -1) 64 65bool WhiteBox::_used = false; 66 67WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj)) 68 return (jlong)(void*)JNIHandles::resolve(obj); 69WB_END 70 71WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o)) 72 return heapOopSize; 73WB_END 74 75 76class WBIsKlassAliveClosure : public KlassClosure { 77 Symbol* _name; 78 bool _found; 79public: 80 WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {} 81 82 void do_klass(Klass* k) { 83 if (_found) return; 84 Symbol* ksym = k->name(); 85 if (ksym->fast_compare(_name) == 0) { 86 _found = true; 87 } 88 } 89 90 bool found() const { 91 return _found; 92 } 93}; 94 95WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name)) 96 Handle h_name = JNIHandles::resolve(name); 97 if (h_name.is_null()) return false; 98 Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false); 99 TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return 100 101 WBIsKlassAliveClosure closure(sym); 102 ClassLoaderDataGraph::classes_do(&closure); 103 104 return closure.found(); 105WB_END 106 107WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) { 108 return (jlong)Arguments::max_heap_for_compressed_oops(); 109} 110WB_END 111 112WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) { 113 CollectorPolicy * p = Universe::heap()->collector_policy(); 114 gclog_or_tty->print_cr("Minimum heap "SIZE_FORMAT" Initial heap " 115 SIZE_FORMAT" Maximum heap "SIZE_FORMAT" Space alignment "SIZE_FORMAT" Heap alignment "SIZE_FORMAT, 116 p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(), 117 p->space_alignment(), p->heap_alignment()); 118} 119WB_END 120 121#ifndef PRODUCT 122// Forward declaration 123void TestReservedSpace_test(); 124void TestReserveMemorySpecial_test(); 125void TestVirtualSpace_test(); 126void TestMetaspaceAux_test(); 127#endif 128 129WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o)) 130#ifndef PRODUCT 131 TestReservedSpace_test(); 132 TestReserveMemorySpecial_test(); 133 TestVirtualSpace_test(); 134 TestMetaspaceAux_test(); 135#endif 136WB_END 137 138WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o)) 139 size_t granularity = os::vm_allocation_granularity(); 140 ReservedHeapSpace rhs(100 * granularity, granularity, false, NULL); 141 VirtualSpace vs; 142 vs.initialize(rhs, 50 * granularity); 143 144 //Check if constraints are complied 145 if (!( UseCompressedOops && rhs.base() != NULL && 146 Universe::narrow_oop_base() != NULL && 147 Universe::narrow_oop_use_implicit_null_checks() )) { 148 tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n " 149 "\tUseCompressedOops is %d\n" 150 "\trhs.base() is "PTR_FORMAT"\n" 151 "\tUniverse::narrow_oop_base() is "PTR_FORMAT"\n" 152 "\tUniverse::narrow_oop_use_implicit_null_checks() is %d", 153 UseCompressedOops, 154 rhs.base(), 155 Universe::narrow_oop_base(), 156 Universe::narrow_oop_use_implicit_null_checks()); 157 return; 158 } 159 tty->print_cr("Reading from no access area... "); 160 tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c", 161 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 )); 162WB_END 163 164static jint wb_stress_virtual_space_resize(size_t reserved_space_size, 165 size_t magnitude, size_t iterations) { 166 size_t granularity = os::vm_allocation_granularity(); 167 ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false, NULL); 168 VirtualSpace vs; 169 if (!vs.initialize(rhs, 0)) { 170 tty->print_cr("Failed to initialize VirtualSpace. Can't proceed."); 171 return 3; 172 } 173 174 long seed = os::random(); 175 tty->print_cr("Random seed is %ld", seed); 176 os::init_random(seed); 177 178 for (size_t i = 0; i < iterations; i++) { 179 180 // Whether we will shrink or grow 181 bool shrink = os::random() % 2L == 0; 182 183 // Get random delta to resize virtual space 184 size_t delta = (size_t)os::random() % magnitude; 185 186 // If we are about to shrink virtual space below zero, then expand instead 187 if (shrink && vs.committed_size() < delta) { 188 shrink = false; 189 } 190 191 // Resizing by delta 192 if (shrink) { 193 vs.shrink_by(delta); 194 } else { 195 // If expanding fails expand_by will silently return false 196 vs.expand_by(delta, true); 197 } 198 } 199 return 0; 200} 201 202WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o, 203 jlong reserved_space_size, jlong magnitude, jlong iterations)) 204 tty->print_cr("reservedSpaceSize="JLONG_FORMAT", magnitude="JLONG_FORMAT", " 205 "iterations="JLONG_FORMAT"\n", reserved_space_size, magnitude, 206 iterations); 207 if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) { 208 tty->print_cr("One of variables printed above is negative. Can't proceed.\n"); 209 return 1; 210 } 211 212 // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is 213 // always 8 byte. That's why we should avoid overflow in case of 32bit platform. 214 if (sizeof(size_t) < sizeof(jlong)) { 215 jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE; 216 if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value 217 || iterations > size_t_max_value) { 218 tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n"); 219 return 2; 220 } 221 } 222 223 return wb_stress_virtual_space_resize((size_t) reserved_space_size, 224 (size_t) magnitude, (size_t) iterations); 225WB_END 226 227#if INCLUDE_ALL_GCS 228WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj)) 229 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 230 oop result = JNIHandles::resolve(obj); 231 const HeapRegion* hr = g1->heap_region_containing(result); 232 return hr->isHumongous(); 233WB_END 234 235WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o)) 236 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 237 size_t nr = g1->free_regions(); 238 return (jlong)nr; 239WB_END 240 241WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o)) 242 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 243 ConcurrentMark* cm = g1->concurrent_mark(); 244 return cm->concurrent_marking_in_progress(); 245WB_END 246 247WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o)) 248 return (jint)HeapRegion::GrainBytes; 249WB_END 250#endif // INCLUDE_ALL_GCS 251 252#if INCLUDE_NMT 253// Alloc memory using the test memory type so that we can use that to see if 254// NMT picks it up correctly 255WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size)) 256 jlong addr = 0; 257 258 if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) { 259 addr = (jlong)(uintptr_t)os::malloc(size, mtTest); 260 } 261 262 return addr; 263WB_END 264 265// Free the memory allocated by NMTAllocTest 266WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem)) 267 os::free((void*)(uintptr_t)mem, mtTest); 268WB_END 269 270WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size)) 271 jlong addr = 0; 272 273 if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) { 274 addr = (jlong)(uintptr_t)os::reserve_memory(size); 275 MemTracker::record_virtual_memory_type((address)addr, mtTest); 276 } 277 278 return addr; 279WB_END 280 281 282WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 283 os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem); 284 MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest); 285WB_END 286 287WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 288 os::uncommit_memory((char *)(uintptr_t)addr, size); 289WB_END 290 291WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size)) 292 os::release_memory((char *)(uintptr_t)addr, size); 293WB_END 294 295// Block until the current generation of NMT data to be merged, used to reliably test the NMT feature 296WB_ENTRY(jboolean, WB_NMTWaitForDataMerge(JNIEnv* env)) 297 298 if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) { 299 return false; 300 } 301 302 return MemTracker::wbtest_wait_for_data_merge(); 303WB_END 304 305WB_ENTRY(jboolean, WB_NMTIsDetailSupported(JNIEnv* env)) 306 return MemTracker::tracking_level() == MemTracker::NMT_detail; 307WB_END 308 309#endif // INCLUDE_NMT 310 311static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) { 312 assert(method != NULL, "method should not be null"); 313 ThreadToNativeFromVM ttn(thread); 314 return env->FromReflectedMethod(method); 315} 316 317WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o)) 318 MutexLockerEx mu(Compile_lock); 319 CodeCache::mark_all_nmethods_for_deoptimization(); 320 VM_Deoptimize op; 321 VMThread::execute(&op); 322WB_END 323 324WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 325 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 326 int result = 0; 327 CHECK_JNI_EXCEPTION_(env, result); 328 MutexLockerEx mu(Compile_lock); 329 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 330 nmethod* code; 331 if (is_osr) { 332 int bci = InvocationEntryBci; 333 while ((code = mh->lookup_osr_nmethod_for(bci, CompLevel_none, false)) != NULL) { 334 code->mark_for_deoptimization(); 335 ++result; 336 bci = code->osr_entry_bci() + 1; 337 } 338 } else { 339 code = mh->code(); 340 } 341 if (code != NULL) { 342 code->mark_for_deoptimization(); 343 ++result; 344 } 345 result += CodeCache::mark_for_deoptimization(mh()); 346 if (result > 0) { 347 VM_Deoptimize op; 348 VMThread::execute(&op); 349 } 350 return result; 351WB_END 352 353WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 354 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 355 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 356 MutexLockerEx mu(Compile_lock); 357 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 358 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 359 if (code == NULL) { 360 return JNI_FALSE; 361 } 362 return (code->is_alive() && !code->is_marked_for_deoptimization()); 363WB_END 364 365WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr)) 366 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 367 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 368 MutexLockerEx mu(Compile_lock); 369 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 370 if (is_osr) { 371 return CompilationPolicy::can_be_osr_compiled(mh, comp_level); 372 } else { 373 return CompilationPolicy::can_be_compiled(mh, comp_level); 374 } 375WB_END 376 377WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method)) 378 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 379 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 380 MutexLockerEx mu(Compile_lock); 381 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 382 return mh->queued_for_compilation(); 383WB_END 384 385WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 386 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 387 CHECK_JNI_EXCEPTION_(env, CompLevel_none); 388 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 389 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 390 return (code != NULL ? code->comp_level() : CompLevel_none); 391WB_END 392 393WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr)) 394 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 395 CHECK_JNI_EXCEPTION(env); 396 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 397 if (is_osr) { 398 mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox"); 399 } else { 400 mh->set_not_compilable(comp_level, true /* report */, "WhiteBox"); 401 } 402WB_END 403 404WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method)) 405 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 406 CHECK_JNI_EXCEPTION_(env, InvocationEntryBci); 407 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 408 nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false); 409 return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci); 410WB_END 411 412WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value)) 413 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 414 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 415 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 416 bool result = mh->dont_inline(); 417 mh->set_dont_inline(value == JNI_TRUE); 418 return result; 419WB_END 420 421WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level)) 422 if (comp_level == CompLevel_any) { 423 return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ + 424 CompileBroker::queue_size(CompLevel_full_profile) /* C1 */; 425 } else { 426 return CompileBroker::queue_size(comp_level); 427 } 428WB_END 429 430WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value)) 431 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 432 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 433 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 434 bool result = mh->force_inline(); 435 mh->set_force_inline(value == JNI_TRUE); 436 return result; 437WB_END 438 439WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci)) 440 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 441 CHECK_JNI_EXCEPTION_(env, JNI_FALSE); 442 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 443 nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), "WhiteBox", THREAD); 444 MutexLockerEx mu(Compile_lock); 445 return (mh->queued_for_compilation() || nm != NULL); 446WB_END 447 448class VM_WhiteBoxOperation : public VM_Operation { 449 public: 450 VM_WhiteBoxOperation() { } 451 VMOp_Type type() const { return VMOp_WhiteBoxOperation; } 452 bool allow_nested_vm_operations() const { return true; } 453}; 454 455class AlwaysFalseClosure : public BoolObjectClosure { 456 public: 457 bool do_object_b(oop p) { return false; } 458}; 459 460static AlwaysFalseClosure always_false; 461 462class VM_WhiteBoxCleanMethodData : public VM_WhiteBoxOperation { 463 public: 464 VM_WhiteBoxCleanMethodData(MethodData* mdo) : _mdo(mdo) { } 465 void doit() { 466 _mdo->clean_method_data(&always_false); 467 } 468 private: 469 MethodData* _mdo; 470}; 471 472WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method)) 473 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 474 CHECK_JNI_EXCEPTION(env); 475 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 476 MutexLockerEx mu(Compile_lock); 477 MethodData* mdo = mh->method_data(); 478 MethodCounters* mcs = mh->method_counters(); 479 480 if (mdo != NULL) { 481 mdo->init(); 482 ResourceMark rm; 483 int arg_count = mdo->method()->size_of_parameters(); 484 for (int i = 0; i < arg_count; i++) { 485 mdo->set_arg_modified(i, 0); 486 } 487 VM_WhiteBoxCleanMethodData op(mdo); 488 VMThread::execute(&op); 489 } 490 491 mh->clear_not_c1_compilable(); 492 mh->clear_not_c2_compilable(); 493 mh->clear_not_c2_osr_compilable(); 494 NOT_PRODUCT(mh->set_compiled_invocation_count(0)); 495 if (mcs != NULL) { 496 mcs->backedge_counter()->init(); 497 mcs->invocation_counter()->init(); 498 mcs->set_interpreter_invocation_count(0); 499 mcs->set_interpreter_throwout_count(0); 500 501#ifdef TIERED 502 mcs->set_rate(0.0F); 503 mh->set_prev_event_count(0); 504 mh->set_prev_time(0); 505#endif 506 } 507WB_END 508 509template <typename T> 510static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, bool (*TAt)(const char*, T*)) { 511 if (name == NULL) { 512 return false; 513 } 514 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 515 const char* flag_name = env->GetStringUTFChars(name, NULL); 516 bool result = (*TAt)(flag_name, value); 517 env->ReleaseStringUTFChars(name, flag_name); 518 return result; 519} 520 521template <typename T> 522static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, bool (*TAtPut)(const char*, T*, Flag::Flags)) { 523 if (name == NULL) { 524 return false; 525 } 526 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 527 const char* flag_name = env->GetStringUTFChars(name, NULL); 528 bool result = (*TAtPut)(flag_name, value, Flag::INTERNAL); 529 env->ReleaseStringUTFChars(name, flag_name); 530 return result; 531} 532 533template <typename T> 534static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) { 535 ResourceMark rm(thread); 536 jclass clazz = env->FindClass(name->as_C_string()); 537 CHECK_JNI_EXCEPTION_(env, NULL); 538 jmethodID methodID = env->GetStaticMethodID(clazz, 539 vmSymbols::valueOf_name()->as_C_string(), 540 sig->as_C_string()); 541 CHECK_JNI_EXCEPTION_(env, NULL); 542 jobject result = env->CallStaticObjectMethod(clazz, methodID, value); 543 CHECK_JNI_EXCEPTION_(env, NULL); 544 return result; 545} 546 547static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) { 548 return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value); 549} 550static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) { 551 return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value); 552} 553static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) { 554 return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value); 555} 556/* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) { 557 return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value); 558}*/ 559static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) { 560 return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value); 561} 562 563WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name)) 564 bool result; 565 if (GetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAt)) { 566 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 567 return booleanBox(thread, env, result); 568 } 569 return NULL; 570WB_END 571 572WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name)) 573 intx result; 574 if (GetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAt)) { 575 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 576 return longBox(thread, env, result); 577 } 578 return NULL; 579WB_END 580 581WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name)) 582 uintx result; 583 if (GetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAt)) { 584 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 585 return longBox(thread, env, result); 586 } 587 return NULL; 588WB_END 589 590WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name)) 591 uint64_t result; 592 if (GetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAt)) { 593 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 594 return longBox(thread, env, result); 595 } 596 return NULL; 597WB_END 598 599WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name)) 600 double result; 601 if (GetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAt)) { 602 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 603 return doubleBox(thread, env, result); 604 } 605 return NULL; 606WB_END 607 608WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name)) 609 ccstr ccstrResult; 610 if (GetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAt)) { 611 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 612 jstring result = env->NewStringUTF(ccstrResult); 613 CHECK_JNI_EXCEPTION_(env, NULL); 614 return result; 615 } 616 return NULL; 617WB_END 618 619WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value)) 620 bool result = value == JNI_TRUE ? true : false; 621 SetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAtPut); 622WB_END 623 624WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value)) 625 intx result = value; 626 SetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAtPut); 627WB_END 628 629WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value)) 630 uintx result = value; 631 SetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAtPut); 632WB_END 633 634WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value)) 635 uint64_t result = value; 636 SetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAtPut); 637WB_END 638 639WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value)) 640 double result = value; 641 SetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAtPut); 642WB_END 643 644WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value)) 645 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 646 const char* ccstrValue = (value == NULL) ? NULL : env->GetStringUTFChars(value, NULL); 647 ccstr ccstrResult = ccstrValue; 648 bool needFree; 649 { 650 ThreadInVMfromNative ttvfn(thread); // back to VM 651 needFree = SetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAtPut); 652 } 653 if (value != NULL) { 654 env->ReleaseStringUTFChars(value, ccstrValue); 655 } 656 if (needFree) { 657 FREE_C_HEAP_ARRAY(char, ccstrResult, mtInternal); 658 } 659WB_END 660 661 662WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString)) 663 ResourceMark rm(THREAD); 664 int len; 665 jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false); 666 return (StringTable::lookup(name, len) != NULL); 667WB_END 668 669WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o)) 670 Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true); 671 Universe::heap()->collect(GCCause::_last_ditch_collection); 672WB_END 673 674 675WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o)) 676 // static+volatile in order to force the read to happen 677 // (not be eliminated by the compiler) 678 static char c; 679 static volatile char* p; 680 681 p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0); 682 if (p == NULL) { 683 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory"); 684 } 685 686 c = *p; 687WB_END 688 689WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o)) 690 const char* cpu_features = VM_Version::cpu_features(); 691 ThreadToNativeFromVM ttn(thread); 692 jstring features_string = env->NewStringUTF(cpu_features); 693 694 CHECK_JNI_EXCEPTION_(env, NULL); 695 696 return features_string; 697WB_END 698 699 700WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr)) 701 ResourceMark rm(THREAD); 702 jmethodID jmid = reflected_method_to_jmid(thread, env, method); 703 CHECK_JNI_EXCEPTION_(env, NULL); 704 methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid)); 705 nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code(); 706 jobjectArray result = NULL; 707 if (code == NULL) { 708 return result; 709 } 710 int insts_size = code->insts_size(); 711 712 ThreadToNativeFromVM ttn(thread); 713 jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string()); 714 CHECK_JNI_EXCEPTION_(env, NULL); 715 result = env->NewObjectArray(2, clazz, NULL); 716 if (result == NULL) { 717 return result; 718 } 719 720 jobject obj = integerBox(thread, env, code->comp_level()); 721 CHECK_JNI_EXCEPTION_(env, NULL); 722 env->SetObjectArrayElement(result, 0, obj); 723 724 jbyteArray insts = env->NewByteArray(insts_size); 725 CHECK_JNI_EXCEPTION_(env, NULL); 726 env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin()); 727 env->SetObjectArrayElement(result, 1, insts); 728 729 return result; 730WB_END 731 732 733WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o)) 734 return (jlong) Thread::current()->stack_size(); 735WB_END 736 737WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o)) 738 JavaThread* t = JavaThread::current(); 739 return (jlong) t->stack_available(os::current_stack_pointer()) - (jlong) StackShadowPages * os::vm_page_size(); 740WB_END 741 742//Some convenience methods to deal with objects from java 743int WhiteBox::offset_for_field(const char* field_name, oop object, 744 Symbol* signature_symbol) { 745 assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid"); 746 Thread* THREAD = Thread::current(); 747 748 //Get the class of our object 749 Klass* arg_klass = object->klass(); 750 //Turn it into an instance-klass 751 InstanceKlass* ik = InstanceKlass::cast(arg_klass); 752 753 //Create symbols to look for in the class 754 TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name), 755 THREAD); 756 757 //To be filled in with an offset of the field we're looking for 758 fieldDescriptor fd; 759 760 Klass* res = ik->find_field(name_symbol, signature_symbol, &fd); 761 if (res == NULL) { 762 tty->print_cr("Invalid layout of %s at %s", ik->external_name(), 763 name_symbol->as_C_string()); 764 fatal("Invalid layout of preloaded class"); 765 } 766 767 //fetch the field at the offset we've found 768 int dest_offset = fd.offset(); 769 770 return dest_offset; 771} 772 773 774const char* WhiteBox::lookup_jstring(const char* field_name, oop object) { 775 int offset = offset_for_field(field_name, object, 776 vmSymbols::string_signature()); 777 oop string = object->obj_field(offset); 778 if (string == NULL) { 779 return NULL; 780 } 781 const char* ret = java_lang_String::as_utf8_string(string); 782 return ret; 783} 784 785bool WhiteBox::lookup_bool(const char* field_name, oop object) { 786 int offset = 787 offset_for_field(field_name, object, vmSymbols::bool_signature()); 788 bool ret = (object->bool_field(offset) == JNI_TRUE); 789 return ret; 790} 791 792 793#define CC (char*) 794 795static JNINativeMethod methods[] = { 796 {CC"getObjectAddress", CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress }, 797 {CC"getHeapOopSize", CC"()I", (void*)&WB_GetHeapOopSize }, 798 {CC"isClassAlive0", CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive }, 799 {CC"parseCommandLine", 800 CC"(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;", 801 (void*) &WB_ParseCommandLine 802 }, 803 {CC"getCompressedOopsMaxHeapSize", CC"()J", 804 (void*)&WB_GetCompressedOopsMaxHeapSize}, 805 {CC"printHeapSizes", CC"()V", (void*)&WB_PrintHeapSizes }, 806 {CC"runMemoryUnitTests", CC"()V", (void*)&WB_RunMemoryUnitTests}, 807 {CC"readFromNoaccessArea",CC"()V", (void*)&WB_ReadFromNoaccessArea}, 808 {CC"stressVirtualSpaceResize",CC"(JJJ)I", (void*)&WB_StressVirtualSpaceResize}, 809#if INCLUDE_ALL_GCS 810 {CC"g1InConcurrentMark", CC"()Z", (void*)&WB_G1InConcurrentMark}, 811 {CC"g1IsHumongous", CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous }, 812 {CC"g1NumFreeRegions", CC"()J", (void*)&WB_G1NumFreeRegions }, 813 {CC"g1RegionSize", CC"()I", (void*)&WB_G1RegionSize }, 814#endif // INCLUDE_ALL_GCS 815#if INCLUDE_NMT 816 {CC"NMTMalloc", CC"(J)J", (void*)&WB_NMTMalloc }, 817 {CC"NMTFree", CC"(J)V", (void*)&WB_NMTFree }, 818 {CC"NMTReserveMemory", CC"(J)J", (void*)&WB_NMTReserveMemory }, 819 {CC"NMTCommitMemory", CC"(JJ)V", (void*)&WB_NMTCommitMemory }, 820 {CC"NMTUncommitMemory", CC"(JJ)V", (void*)&WB_NMTUncommitMemory }, 821 {CC"NMTReleaseMemory", CC"(JJ)V", (void*)&WB_NMTReleaseMemory }, 822 {CC"NMTWaitForDataMerge", CC"()Z", (void*)&WB_NMTWaitForDataMerge}, 823 {CC"NMTIsDetailSupported",CC"()Z", (void*)&WB_NMTIsDetailSupported}, 824#endif // INCLUDE_NMT 825 {CC"deoptimizeAll", CC"()V", (void*)&WB_DeoptimizeAll }, 826 {CC"deoptimizeMethod", CC"(Ljava/lang/reflect/Executable;Z)I", 827 (void*)&WB_DeoptimizeMethod }, 828 {CC"isMethodCompiled", CC"(Ljava/lang/reflect/Executable;Z)Z", 829 (void*)&WB_IsMethodCompiled }, 830 {CC"isMethodCompilable", CC"(Ljava/lang/reflect/Executable;IZ)Z", 831 (void*)&WB_IsMethodCompilable}, 832 {CC"isMethodQueuedForCompilation", 833 CC"(Ljava/lang/reflect/Executable;)Z", (void*)&WB_IsMethodQueuedForCompilation}, 834 {CC"makeMethodNotCompilable", 835 CC"(Ljava/lang/reflect/Executable;IZ)V", (void*)&WB_MakeMethodNotCompilable}, 836 {CC"testSetDontInlineMethod", 837 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetDontInlineMethod}, 838 {CC"getMethodCompilationLevel", 839 CC"(Ljava/lang/reflect/Executable;Z)I", (void*)&WB_GetMethodCompilationLevel}, 840 {CC"getMethodEntryBci", 841 CC"(Ljava/lang/reflect/Executable;)I", (void*)&WB_GetMethodEntryBci}, 842 {CC"getCompileQueueSize", 843 CC"(I)I", (void*)&WB_GetCompileQueueSize}, 844 {CC"testSetForceInlineMethod", 845 CC"(Ljava/lang/reflect/Executable;Z)Z", (void*)&WB_TestSetForceInlineMethod}, 846 {CC"enqueueMethodForCompilation", 847 CC"(Ljava/lang/reflect/Executable;II)Z", (void*)&WB_EnqueueMethodForCompilation}, 848 {CC"clearMethodState", 849 CC"(Ljava/lang/reflect/Executable;)V", (void*)&WB_ClearMethodState}, 850 {CC"setBooleanVMFlag", CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag}, 851 {CC"setIntxVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag}, 852 {CC"setUintxVMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag}, 853 {CC"setUint64VMFlag", CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag}, 854 {CC"setDoubleVMFlag", CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag}, 855 {CC"setStringVMFlag", CC"(Ljava/lang/String;Ljava/lang/String;)V", 856 (void*)&WB_SetStringVMFlag}, 857 {CC"getBooleanVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Boolean;", 858 (void*)&WB_GetBooleanVMFlag}, 859 {CC"getIntxVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;", 860 (void*)&WB_GetIntxVMFlag}, 861 {CC"getUintxVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;", 862 (void*)&WB_GetUintxVMFlag}, 863 {CC"getUint64VMFlag", CC"(Ljava/lang/String;)Ljava/lang/Long;", 864 (void*)&WB_GetUint64VMFlag}, 865 {CC"getDoubleVMFlag", CC"(Ljava/lang/String;)Ljava/lang/Double;", 866 (void*)&WB_GetDoubleVMFlag}, 867 {CC"getStringVMFlag", CC"(Ljava/lang/String;)Ljava/lang/String;", 868 (void*)&WB_GetStringVMFlag}, 869 {CC"isInStringTable", CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable }, 870 {CC"fullGC", CC"()V", (void*)&WB_FullGC }, 871 {CC"readReservedMemory", CC"()V", (void*)&WB_ReadReservedMemory }, 872 {CC"getCPUFeatures", CC"()Ljava/lang/String;", (void*)&WB_GetCPUFeatures }, 873 {CC"getNMethod", CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;", 874 (void*)&WB_GetNMethod }, 875 {CC"getThreadStackSize", CC"()J", (void*)&WB_GetThreadStackSize }, 876 {CC"getThreadRemainingStackSize", CC"()J", (void*)&WB_GetThreadRemainingStackSize }, 877}; 878 879#undef CC 880 881JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass)) 882 { 883 if (WhiteBoxAPI) { 884 // Make sure that wbclass is loaded by the null classloader 885 instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass()); 886 Handle loader(ikh->class_loader()); 887 if (loader.is_null()) { 888 ResourceMark rm; 889 ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI 890 bool result = true; 891 // one by one registration natives for exception catching 892 jclass exceptionKlass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string()); 893 CHECK_JNI_EXCEPTION(env); 894 for (int i = 0, n = sizeof(methods) / sizeof(methods[0]); i < n; ++i) { 895 if (env->RegisterNatives(wbclass, methods + i, 1) != 0) { 896 result = false; 897 jthrowable throwable_obj = env->ExceptionOccurred(); 898 if (throwable_obj != NULL) { 899 env->ExceptionClear(); 900 if (env->IsInstanceOf(throwable_obj, exceptionKlass)) { 901 // j.l.NoSuchMethodError is thrown when a method can't be found or a method is not native 902 // ignoring the exception 903 tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s", methods[i].name, methods[i].signature); 904 } 905 } else { 906 // register is failed w/o exception or w/ unexpected exception 907 tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered", methods[i].name, methods[i].signature); 908 env->UnregisterNatives(wbclass); 909 break; 910 } 911 } 912 } 913 914 if (result) { 915 WhiteBox::set_used(); 916 } 917 } 918 } 919 } 920JVM_END 921