hotspot/src/share/vm/runtime/thread.cpp
changeset 33160 c59f1676d27e
parent 32366 4b6a0ffabffe
child 33198 b37ad9fbf681
equal deleted inserted replaced
33159:89b942323bd1 33160:c59f1676d27e
    97 #if INCLUDE_ALL_GCS
    97 #if INCLUDE_ALL_GCS
    98 #include "gc/cms/concurrentMarkSweepThread.hpp"
    98 #include "gc/cms/concurrentMarkSweepThread.hpp"
    99 #include "gc/g1/concurrentMarkThread.inline.hpp"
    99 #include "gc/g1/concurrentMarkThread.inline.hpp"
   100 #include "gc/parallel/pcTasks.hpp"
   100 #include "gc/parallel/pcTasks.hpp"
   101 #endif // INCLUDE_ALL_GCS
   101 #endif // INCLUDE_ALL_GCS
       
   102 #if INCLUDE_JVMCI
       
   103 #include "jvmci/jvmciCompiler.hpp"
       
   104 #include "jvmci/jvmciRuntime.hpp"
       
   105 #endif
   102 #ifdef COMPILER1
   106 #ifdef COMPILER1
   103 #include "c1/c1_Compiler.hpp"
   107 #include "c1/c1_Compiler.hpp"
   104 #endif
   108 #endif
   105 #ifdef COMPILER2
   109 #ifdef COMPILER2
   106 #include "opto/c2compiler.hpp"
   110 #include "opto/c2compiler.hpp"
  1384   st->cr();
  1388   st->cr();
  1385 }
  1389 }
  1386 
  1390 
  1387 // ======= JavaThread ========
  1391 // ======= JavaThread ========
  1388 
  1392 
       
  1393 #if INCLUDE_JVMCI
       
  1394 
       
  1395 jlong* JavaThread::_jvmci_old_thread_counters;
       
  1396 
       
  1397 bool jvmci_counters_include(JavaThread* thread) {
       
  1398   oop threadObj = thread->threadObj();
       
  1399   return !JVMCICountersExcludeCompiler || !thread->is_Compiler_thread();
       
  1400 }
       
  1401 
       
  1402 void JavaThread::collect_counters(typeArrayOop array) {
       
  1403   if (JVMCICounterSize > 0) {
       
  1404     MutexLocker tl(Threads_lock);
       
  1405     for (int i = 0; i < array->length(); i++) {
       
  1406       array->long_at_put(i, _jvmci_old_thread_counters[i]);
       
  1407     }
       
  1408     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
       
  1409       if (jvmci_counters_include(tp)) {
       
  1410         for (int i = 0; i < array->length(); i++) {
       
  1411           array->long_at_put(i, array->long_at(i) + tp->_jvmci_counters[i]);
       
  1412         }
       
  1413       }
       
  1414     }
       
  1415   }
       
  1416 }
       
  1417 
       
  1418 #endif // INCLUDE_JVMCI
       
  1419 
  1389 // A JavaThread is a normal Java thread
  1420 // A JavaThread is a normal Java thread
  1390 
  1421 
  1391 void JavaThread::initialize() {
  1422 void JavaThread::initialize() {
  1392   // Initialize fields
  1423   // Initialize fields
  1393 
  1424 
  1416   _array_for_gc = NULL;
  1447   _array_for_gc = NULL;
  1417   _suspend_equivalent = false;
  1448   _suspend_equivalent = false;
  1418   _in_deopt_handler = 0;
  1449   _in_deopt_handler = 0;
  1419   _doing_unsafe_access = false;
  1450   _doing_unsafe_access = false;
  1420   _stack_guard_state = stack_guard_unused;
  1451   _stack_guard_state = stack_guard_unused;
       
  1452 #if INCLUDE_JVMCI
       
  1453   _pending_monitorenter = false;
       
  1454   _pending_deoptimization = -1;
       
  1455   _pending_failed_speculation = NULL;
       
  1456   _pending_transfer_to_interpreter = false;
       
  1457   _jvmci._alternate_call_target = NULL;
       
  1458   assert(_jvmci._implicit_exception_pc == NULL, "must be");
       
  1459   if (JVMCICounterSize > 0) {
       
  1460     _jvmci_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
       
  1461     memset(_jvmci_counters, 0, sizeof(jlong) * JVMCICounterSize);
       
  1462   } else {
       
  1463     _jvmci_counters = NULL;
       
  1464   }
       
  1465 #endif // INCLUDE_JVMCI
  1421   (void)const_cast<oop&>(_exception_oop = oop(NULL));
  1466   (void)const_cast<oop&>(_exception_oop = oop(NULL));
  1422   _exception_pc  = 0;
  1467   _exception_pc  = 0;
  1423   _exception_handler_pc = 0;
  1468   _exception_handler_pc = 0;
  1424   _is_method_handle_return = 0;
  1469   _is_method_handle_return = 0;
  1425   _jvmti_thread_state= NULL;
  1470   _jvmti_thread_state= NULL;
  1590 
  1635 
  1591   // All Java related clean up happens in exit
  1636   // All Java related clean up happens in exit
  1592   ThreadSafepointState::destroy(this);
  1637   ThreadSafepointState::destroy(this);
  1593   if (_thread_profiler != NULL) delete _thread_profiler;
  1638   if (_thread_profiler != NULL) delete _thread_profiler;
  1594   if (_thread_stat != NULL) delete _thread_stat;
  1639   if (_thread_stat != NULL) delete _thread_stat;
       
  1640 
       
  1641 #if INCLUDE_JVMCI
       
  1642   if (JVMCICounterSize > 0) {
       
  1643     if (jvmci_counters_include(this)) {
       
  1644       for (int i = 0; i < JVMCICounterSize; i++) {
       
  1645         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
       
  1646       }
       
  1647     }
       
  1648     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
       
  1649   }
       
  1650 #endif // INCLUDE_JVMCI
  1595 }
  1651 }
  1596 
  1652 
  1597 
  1653 
  1598 // The first routine called by a new Java thread
  1654 // The first routine called by a new Java thread
  1599 void JavaThread::run() {
  1655 void JavaThread::run() {
  2133   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
  2189   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
  2134   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
  2190   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
  2135 
  2191 
  2136   // Do not throw asynchronous exceptions against the compiler thread
  2192   // Do not throw asynchronous exceptions against the compiler thread
  2137   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
  2193   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
  2138   if (is_Compiler_thread()) return;
  2194   if (!can_call_java()) return;
  2139 
  2195 
  2140   {
  2196   {
  2141     // Actually throw the Throwable against the target Thread - however
  2197     // Actually throw the Throwable against the target Thread - however
  2142     // only if there is no thread death exception installed already.
  2198     // only if there is no thread death exception installed already.
  2143     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
  2199     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
  2612   if (!has_last_Java_frame()) return;
  2668   if (!has_last_Java_frame()) return;
  2613   // BiasedLocking needs an updated RegisterMap for the revoke monitors pass
  2669   // BiasedLocking needs an updated RegisterMap for the revoke monitors pass
  2614   StackFrameStream fst(this, UseBiasedLocking);
  2670   StackFrameStream fst(this, UseBiasedLocking);
  2615   for (; !fst.is_done(); fst.next()) {
  2671   for (; !fst.is_done(); fst.next()) {
  2616     if (fst.current()->should_be_deoptimized()) {
  2672     if (fst.current()->should_be_deoptimized()) {
  2617       if (LogCompilation && xtty != NULL) {
       
  2618         nmethod* nm = fst.current()->cb()->as_nmethod_or_null();
       
  2619         xtty->elem("deoptimized thread='" UINTX_FORMAT "' compile_id='%d'",
       
  2620                    this->name(), nm != NULL ? nm->compile_id() : -1);
       
  2621       }
       
  2622 
       
  2623       Deoptimization::deoptimize(this, *fst.current(), fst.register_map());
  2673       Deoptimization::deoptimize(this, *fst.current(), fst.register_map());
  2624     }
  2674     }
  2625   }
  2675   }
  2626 }
  2676 }
  2627 
  2677 
  2655   // The ThreadProfiler oops_do is done from FlatProfiler::oops_do
  2705   // The ThreadProfiler oops_do is done from FlatProfiler::oops_do
  2656   // since there may be more than one thread using each ThreadProfiler.
  2706   // since there may be more than one thread using each ThreadProfiler.
  2657 
  2707 
  2658   // Traverse the GCHandles
  2708   // Traverse the GCHandles
  2659   Thread::oops_do(f, cld_f, cf);
  2709   Thread::oops_do(f, cld_f, cf);
       
  2710 
       
  2711   JVMCI_ONLY(f->do_oop((oop*)&_pending_failed_speculation);)
  2660 
  2712 
  2661   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
  2713   assert((!has_last_Java_frame() && java_call_counter() == 0) ||
  2662          (has_last_Java_frame() && java_call_counter() > 0), "wrong java_sp info!");
  2714          (has_last_Java_frame() && java_call_counter() > 0), "wrong java_sp info!");
  2663 
  2715 
  2664   if (has_last_Java_frame()) {
  2716   if (has_last_Java_frame()) {
  3173 #ifndef PRODUCT
  3225 #ifndef PRODUCT
  3174   _ideal_graph_printer = NULL;
  3226   _ideal_graph_printer = NULL;
  3175 #endif
  3227 #endif
  3176 }
  3228 }
  3177 
  3229 
       
  3230 bool CompilerThread::can_call_java() const {
       
  3231   return _compiler != NULL && _compiler->is_jvmci();
       
  3232 }
       
  3233 
  3178 // Create sweeper thread
  3234 // Create sweeper thread
  3179 CodeCacheSweeperThread::CodeCacheSweeperThread()
  3235 CodeCacheSweeperThread::CodeCacheSweeperThread()
  3180 : JavaThread(&sweeper_thread_entry) {
  3236 : JavaThread(&sweeper_thread_entry) {
  3181   _scanned_nmethod = NULL;
  3237   _scanned_nmethod = NULL;
  3182 }
  3238 }
  3378   _number_of_non_daemon_threads = 0;
  3434   _number_of_non_daemon_threads = 0;
  3379 
  3435 
  3380   // Initialize global data structures and create system classes in heap
  3436   // Initialize global data structures and create system classes in heap
  3381   vm_init_globals();
  3437   vm_init_globals();
  3382 
  3438 
       
  3439 #if INCLUDE_JVMCI
       
  3440   if (JVMCICounterSize > 0) {
       
  3441     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtInternal);
       
  3442     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
       
  3443   } else {
       
  3444     JavaThread::_jvmci_old_thread_counters = NULL;
       
  3445   }
       
  3446 #endif // INCLUDE_JVMCI
       
  3447 
  3383   // Attach the main thread to this os thread
  3448   // Attach the main thread to this os thread
  3384   JavaThread* main_thread = new JavaThread();
  3449   JavaThread* main_thread = new JavaThread();
  3385   main_thread->set_thread_state(_thread_in_vm);
  3450   main_thread->set_thread_state(_thread_in_vm);
  3386   // must do this before set_active_handles and initialize_thread_local_storage
  3451   // must do this before set_active_handles and initialize_thread_local_storage
  3387   // Note: on solaris initialize_thread_local_storage() will (indirectly)
  3452   // Note: on solaris initialize_thread_local_storage() will (indirectly)
  3504   // Compute system loader. Note that this has to occur after set_init_completed, since
  3569   // Compute system loader. Note that this has to occur after set_init_completed, since
  3505   // valid exceptions may be thrown in the process.
  3570   // valid exceptions may be thrown in the process.
  3506   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
  3571   // Note that we do not use CHECK_0 here since we are inside an EXCEPTION_MARK and
  3507   // set_init_completed has just been called, causing exceptions not to be shortcut
  3572   // set_init_completed has just been called, causing exceptions not to be shortcut
  3508   // anymore. We call vm_exit_during_initialization directly instead.
  3573   // anymore. We call vm_exit_during_initialization directly instead.
  3509   SystemDictionary::compute_java_system_loader(CHECK_JNI_ERR);
  3574   SystemDictionary::compute_java_system_loader(CHECK_(JNI_ERR));
  3510 
  3575 
  3511 #if INCLUDE_ALL_GCS
  3576 #if INCLUDE_ALL_GCS
  3512   // Support for ConcurrentMarkSweep. This should be cleaned up
  3577   // Support for ConcurrentMarkSweep. This should be cleaned up
  3513   // and better encapsulated. The ugly nested if test would go away
  3578   // and better encapsulated. The ugly nested if test would go away
  3514   // once things are properly refactored. XXX YSR
  3579   // once things are properly refactored. XXX YSR
  3552 
  3617 
  3553   if (CleanChunkPoolAsync) {
  3618   if (CleanChunkPoolAsync) {
  3554     Chunk::start_chunk_pool_cleaner_task();
  3619     Chunk::start_chunk_pool_cleaner_task();
  3555   }
  3620   }
  3556 
  3621 
       
  3622 #if INCLUDE_JVMCI
       
  3623   if (EnableJVMCI) {
       
  3624     const char* jvmciCompiler = Arguments::PropertyList_get_value(Arguments::system_properties(), "jvmci.compiler");
       
  3625     if (jvmciCompiler != NULL) {
       
  3626       JVMCIRuntime::save_compiler(jvmciCompiler);
       
  3627     }
       
  3628   }
       
  3629 #endif // INCLUDE_JVMCI
       
  3630 
  3557   // initialize compiler(s)
  3631   // initialize compiler(s)
  3558 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
  3632 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK) || INCLUDE_JVMCI
  3559   CompileBroker::compilation_init();
  3633   CompileBroker::compilation_init();
  3560 #endif
  3634 #endif
  3561 
  3635 
  3562   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
  3636   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
  3563   // It is done after compilers are initialized, because otherwise compilations of
  3637   // It is done after compilers are initialized, because otherwise compilations of
  3961 
  4035 
  3962   notify_vm_shutdown();
  4036   notify_vm_shutdown();
  3963 
  4037 
  3964   delete thread;
  4038   delete thread;
  3965 
  4039 
       
  4040 #if INCLUDE_JVMCI
       
  4041   if (JVMCICounterSize > 0) {
       
  4042     FREE_C_HEAP_ARRAY(jlong, JavaThread::_jvmci_old_thread_counters);
       
  4043   }
       
  4044 #endif
       
  4045 
  3966   // exit_globals() will delete tty
  4046   // exit_globals() will delete tty
  3967   exit_globals();
  4047   exit_globals();
  3968 
  4048 
  3969   return true;
  4049   return true;
  3970 }
  4050 }
  4177 
  4257 
  4178   int i = 0;
  4258   int i = 0;
  4179   {
  4259   {
  4180     MutexLockerEx ml(doLock ? Threads_lock : NULL);
  4260     MutexLockerEx ml(doLock ? Threads_lock : NULL);
  4181     ALL_JAVA_THREADS(p) {
  4261     ALL_JAVA_THREADS(p) {
  4182       if (p->is_Compiler_thread()) continue;
  4262       if (!p->can_call_java()) continue;
  4183 
  4263 
  4184       address pending = (address)p->current_pending_monitor();
  4264       address pending = (address)p->current_pending_monitor();
  4185       if (pending == monitor) {             // found a match
  4265       if (pending == monitor) {             // found a match
  4186         if (i < count) result->append(p);   // save the first count matches
  4266         if (i < count) result->append(p);   // save the first count matches
  4187         i++;
  4267         i++;
  4234 
  4314 
  4235 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
  4315 // Threads::print_on() is called at safepoint by VM_PrintThreads operation.
  4236 void Threads::print_on(outputStream* st, bool print_stacks,
  4316 void Threads::print_on(outputStream* st, bool print_stacks,
  4237                        bool internal_format, bool print_concurrent_locks) {
  4317                        bool internal_format, bool print_concurrent_locks) {
  4238   char buf[32];
  4318   char buf[32];
  4239   st->print_cr("%s", os::local_time_string(buf, sizeof(buf)));
  4319   st->print_raw_cr(os::local_time_string(buf, sizeof(buf)));
  4240 
  4320 
  4241   st->print_cr("Full thread dump %s (%s %s):",
  4321   st->print_cr("Full thread dump %s (%s %s):",
  4242                Abstract_VM_Version::vm_name(),
  4322                Abstract_VM_Version::vm_name(),
  4243                Abstract_VM_Version::vm_release(),
  4323                Abstract_VM_Version::vm_release(),
  4244                Abstract_VM_Version::vm_info_string());
  4324                Abstract_VM_Version::vm_info_string());