< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




1669 }
1670 
1671 #endif // INCLUDE_JVMCI
1672 
1673 // A JavaThread is a normal Java thread
1674 
1675 void JavaThread::initialize() {
1676   // Initialize fields
1677 
1678   set_saved_exception_pc(NULL);
1679   set_threadObj(NULL);
1680   _anchor.clear();
1681   set_entry_point(NULL);
1682   set_jni_functions(jni_functions());
1683   set_callee_target(NULL);
1684   set_vm_result(NULL);
1685   set_vm_result_2(NULL);
1686   set_vframe_array_head(NULL);
1687   set_vframe_array_last(NULL);
1688   set_deferred_locals(NULL);

1689   set_deopt_mark(NULL);
1690   set_deopt_compiled_method(NULL);
1691   clear_must_deopt_id();
1692   set_monitor_chunks(NULL);
1693   _on_thread_list = false;
1694   set_thread_state(_thread_new);
1695   _terminated = _not_terminated;
1696   _array_for_gc = NULL;
1697   _suspend_equivalent = false;
1698   _in_deopt_handler = 0;
1699   _doing_unsafe_access = false;
1700   _stack_guard_state = stack_guard_unused;

1701 #if INCLUDE_JVMCI
1702   _pending_monitorenter = false;
1703   _pending_deoptimization = -1;
1704   _pending_failed_speculation = 0;
1705   _pending_transfer_to_interpreter = false;
1706   _in_retryable_allocation = false;
1707   _jvmci._alternate_call_target = NULL;
1708   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1709   _jvmci_counters = NULL;
1710   if (JVMCICounterSize > 0) {
1711     resize_counters(0, (int) JVMCICounterSize);
1712   }
1713 #endif // INCLUDE_JVMCI
1714   _reserved_stack_activation = NULL;  // stack base not known yet
1715   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1716   _exception_pc  = 0;
1717   _exception_handler_pc = 0;
1718   _is_method_handle_return = 0;
1719   _jvmti_thread_state= NULL;
1720   _should_post_on_exceptions_flag = JNI_FALSE;
1721   _interp_only_mode    = 0;
1722   _special_runtime_exit_condition = _no_async_condition;
1723   _pending_async_exception = NULL;
1724   _thread_stat = NULL;
1725   _thread_stat = new ThreadStatistics();
1726   _blocked_on_compilation = false;
1727   _jni_active_critical = 0;
1728   _pending_jni_exception_check_fn = NULL;
1729   _do_not_unlock_if_synchronized = false;
1730   _cached_monitor_info = NULL;
1731   _parker = Parker::Allocate(this);
1732 





1733 #ifndef PRODUCT
1734   _jmp_ring_index = 0;
1735   for (int ji = 0; ji < jump_ring_buffer_size; ji++) {
1736     record_jump(NULL, NULL, NULL, 0);
1737   }
1738 #endif // PRODUCT
1739 
1740   // Setup safepoint state info for this thread
1741   ThreadSafepointState::create(this);
1742 
1743   debug_only(_java_call_counter = 0);
1744 
1745   // JVMTI PopFrame support
1746   _popframe_condition = popframe_inactive;
1747   _popframe_preserved_args = NULL;
1748   _popframe_preserved_args_size = 0;
1749   _frames_to_pop_failed_realloc = 0;
1750 
1751   if (SafepointMechanism::uses_thread_local_poll()) {
1752     SafepointMechanism::initialize_header(this);


2345 
2346   assert(condition == _no_async_condition || has_pending_exception() ||
2347          (!check_unsafe_error && condition == _async_unsafe_access_error),
2348          "must have handled the async condition, if no exception");
2349 }
2350 
2351 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2352 
2353   // Check for pending external suspend.
2354   if (is_external_suspend_with_lock()) {
2355     frame_anchor()->make_walkable(this);
2356     java_suspend_self_with_safepoint_check();
2357   }
2358 
2359   // We might be here for reasons in addition to the self-suspend request
2360   // so check for other async requests.
2361   if (check_asyncs) {
2362     check_and_handle_async_exceptions();
2363   }
2364 








2365   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2366 }
2367 
2368 void JavaThread::send_thread_stop(oop java_throwable)  {
2369   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2370   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2371   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2372 
2373   // Do not throw asynchronous exceptions against the compiler thread
2374   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2375   if (!can_call_java()) return;
2376 
2377   {
2378     // Actually throw the Throwable against the target Thread - however
2379     // only if there is no thread death exception installed already.
2380     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2381       // If the topmost frame is a runtime stub, then we are calling into
2382       // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2383       // must deoptimize the caller before continuing, as the compiled  exception handler table
2384       // may not be valid


3034   case _thread_new_trans:         return "_thread_new_trans";
3035   case _thread_in_native:         return "_thread_in_native";
3036   case _thread_in_native_trans:   return "_thread_in_native_trans";
3037   case _thread_in_vm:             return "_thread_in_vm";
3038   case _thread_in_vm_trans:       return "_thread_in_vm_trans";
3039   case _thread_in_Java:           return "_thread_in_Java";
3040   case _thread_in_Java_trans:     return "_thread_in_Java_trans";
3041   case _thread_blocked:           return "_thread_blocked";
3042   case _thread_blocked_trans:     return "_thread_blocked_trans";
3043   default:                        return "unknown thread state";
3044   }
3045 }
3046 
3047 #ifndef PRODUCT
3048 void JavaThread::print_thread_state_on(outputStream *st) const {
3049   st->print_cr("   JavaThread state: %s", _get_thread_state_name(_thread_state));
3050 };
3051 void JavaThread::print_thread_state() const {
3052   print_thread_state_on(tty);
3053 }



3054 #endif // PRODUCT
3055 
3056 // Called by Threads::print() for VM_PrintThreads operation
3057 void JavaThread::print_on(outputStream *st, bool print_extended_info) const {
3058   st->print_raw("\"");
3059   st->print_raw(get_thread_name());
3060   st->print_raw("\" ");
3061   oop thread_oop = threadObj();
3062   if (thread_oop != NULL) {
3063     st->print("#" INT64_FORMAT " ", (int64_t)java_lang_Thread::thread_id(thread_oop));
3064     if (java_lang_Thread::is_daemon(thread_oop))  st->print("daemon ");
3065     st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
3066   }
3067   Thread::print_on(st, print_extended_info);
3068   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
3069   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
3070   if (thread_oop != NULL) {
3071     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
3072   }
3073 #ifndef PRODUCT


3358   f->oops_do(&print, NULL, (RegisterMap*)map);
3359 }
3360 
3361 // Print our all the locations that contain oops and whether they are
3362 // valid or not.  This useful when trying to find the oldest frame
3363 // where an oop has gone bad since the frame walk is from youngest to
3364 // oldest.
3365 void JavaThread::trace_oops() {
3366   tty->print_cr("[Trace oops]");
3367   frames_do(oops_print);
3368 }
3369 
3370 
3371 #ifdef ASSERT
3372 // Print or validate the layout of stack frames
3373 void JavaThread::print_frame_layout(int depth, bool validate_only) {
3374   ResourceMark rm;
3375   PRESERVE_EXCEPTION_MARK;
3376   FrameValues values;
3377   int frame_no = 0;
3378   for (StackFrameStream fst(this, false); !fst.is_done(); fst.next()) {
3379     fst.current()->describe(values, ++frame_no);
3380     if (depth == frame_no) break;
3381   }

3382   if (validate_only) {
3383     values.validate();
3384   } else {
3385     tty->print_cr("[Describe stack layout]");
3386     values.print(this);
3387   }
3388 }
3389 #endif
3390 
3391 void JavaThread::trace_stack_from(vframe* start_vf) {
3392   ResourceMark rm;
3393   int vframe_no = 1;
3394   for (vframe* f = start_vf; f; f = f->sender()) {
3395     if (f->is_java_frame()) {
3396       javaVFrame::cast(f)->print_activation(vframe_no++);
3397     } else {
3398       f->print();
3399     }
3400     if (vframe_no > StackPrintLimit) {
3401       tty->print_cr("...<more frames>...");
3402       return;
3403     }
3404   }
3405 }
3406 
3407 
3408 void JavaThread::trace_stack() {
3409   if (!has_last_Java_frame()) return;
3410   ResourceMark rm;
3411   HandleMark   hm;
3412   RegisterMap reg_map(this);
3413   trace_stack_from(last_java_vframe(&reg_map));
3414 }
3415 
3416 
3417 #endif // PRODUCT
3418 
3419 
3420 javaVFrame* JavaThread::last_java_vframe(RegisterMap *reg_map) {
3421   assert(reg_map != NULL, "a map must be given");
3422   frame f = last_frame();
3423   for (vframe* vf = vframe::new_vframe(&f, reg_map, this); vf; vf = vf->sender()) {
3424     if (vf->is_java_frame()) return javaVFrame::cast(vf);
3425   }
3426   return NULL;
3427 }
3428 





3429 
3430 Klass* JavaThread::security_get_caller_class(int depth) {
3431   vframeStream vfst(this);
3432   vfst.security_get_caller_frame(depth);
3433   if (!vfst.at_end()) {
3434     return vfst.method()->method_holder();
3435   }
3436   return NULL;
3437 }
3438 
3439 static void compiler_thread_entry(JavaThread* thread, TRAPS) {
3440   assert(thread->is_Compiler_thread(), "must be compiler thread");
3441   CompileBroker::compiler_thread_loop();
3442 }
3443 
3444 static void sweeper_thread_entry(JavaThread* thread, TRAPS) {
3445   NMethodSweeper::sweeper_loop();
3446 }
3447 
3448 // Create a CompilerThread




1669 }
1670 
1671 #endif // INCLUDE_JVMCI
1672 
1673 // A JavaThread is a normal Java thread
1674 
1675 void JavaThread::initialize() {
1676   // Initialize fields
1677 
1678   set_saved_exception_pc(NULL);
1679   set_threadObj(NULL);
1680   _anchor.clear();
1681   set_entry_point(NULL);
1682   set_jni_functions(jni_functions());
1683   set_callee_target(NULL);
1684   set_vm_result(NULL);
1685   set_vm_result_2(NULL);
1686   set_vframe_array_head(NULL);
1687   set_vframe_array_last(NULL);
1688   set_deferred_locals(NULL);
1689   set_keepalive_cleanup(new (ResourceObj::C_HEAP, mtInternal) GrowableArray<WeakHandle<vm_nmethod_keepalive_data> >(16, true));
1690   set_deopt_mark(NULL);
1691   set_deopt_compiled_method(NULL);
1692   clear_must_deopt_id();
1693   set_monitor_chunks(NULL);
1694   _on_thread_list = false;
1695   set_thread_state(_thread_new);
1696   _terminated = _not_terminated;
1697   _array_for_gc = NULL;
1698   _suspend_equivalent = false;
1699   _in_deopt_handler = 0;
1700   _doing_unsafe_access = false;
1701   _stack_guard_state = stack_guard_unused;
1702   DEBUG_ONLY(_continuation = NULL;)
1703 #if INCLUDE_JVMCI
1704   _pending_monitorenter = false;
1705   _pending_deoptimization = -1;
1706   _pending_failed_speculation = 0;
1707   _pending_transfer_to_interpreter = false;
1708   _in_retryable_allocation = false;
1709   _jvmci._alternate_call_target = NULL;
1710   assert(_jvmci._implicit_exception_pc == NULL, "must be");
1711   _jvmci_counters = NULL;
1712   if (JVMCICounterSize > 0) {
1713     resize_counters(0, (int) JVMCICounterSize);
1714   }
1715 #endif // INCLUDE_JVMCI
1716   _reserved_stack_activation = NULL;  // stack base not known yet
1717   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1718   _exception_pc  = 0;
1719   _exception_handler_pc = 0;
1720   _is_method_handle_return = 0;
1721   _jvmti_thread_state= NULL;
1722   _should_post_on_exceptions_flag = JNI_FALSE;
1723   _interp_only_mode    = 0;
1724   _special_runtime_exit_condition = _no_async_condition;
1725   _pending_async_exception = NULL;
1726   _thread_stat = NULL;
1727   _thread_stat = new ThreadStatistics();
1728   _blocked_on_compilation = false;
1729   _jni_active_critical = 0;
1730   _pending_jni_exception_check_fn = NULL;
1731   _do_not_unlock_if_synchronized = false;
1732   _cached_monitor_info = NULL;
1733   _parker = Parker::Allocate(this);
1734 
1735   _cont_yield = false;
1736   _cont_preempt = false;
1737   _cont_fastpath = 0;
1738   memset(&_cont_frame, 0, sizeof(FrameInfo));
1739 
1740 #ifndef PRODUCT
1741   _jmp_ring_index = 0;
1742   for (int ji = 0; ji < jump_ring_buffer_size; ji++) {
1743     record_jump(NULL, NULL, NULL, 0);
1744   }
1745 #endif // PRODUCT
1746 
1747   // Setup safepoint state info for this thread
1748   ThreadSafepointState::create(this);
1749 
1750   debug_only(_java_call_counter = 0);
1751 
1752   // JVMTI PopFrame support
1753   _popframe_condition = popframe_inactive;
1754   _popframe_preserved_args = NULL;
1755   _popframe_preserved_args_size = 0;
1756   _frames_to_pop_failed_realloc = 0;
1757 
1758   if (SafepointMechanism::uses_thread_local_poll()) {
1759     SafepointMechanism::initialize_header(this);


2352 
2353   assert(condition == _no_async_condition || has_pending_exception() ||
2354          (!check_unsafe_error && condition == _async_unsafe_access_error),
2355          "must have handled the async condition, if no exception");
2356 }
2357 
2358 void JavaThread::handle_special_runtime_exit_condition(bool check_asyncs) {
2359 
2360   // Check for pending external suspend.
2361   if (is_external_suspend_with_lock()) {
2362     frame_anchor()->make_walkable(this);
2363     java_suspend_self_with_safepoint_check();
2364   }
2365 
2366   // We might be here for reasons in addition to the self-suspend request
2367   // so check for other async requests.
2368   if (check_asyncs) {
2369     check_and_handle_async_exceptions();
2370   }
2371 
2372   if (is_cont_force_yield()) {
2373     log_develop_trace(jvmcont)("handle_special_runtime_exit_condition is_cont_force_yield: %d check_asyncs: %d", is_cont_force_yield(), check_asyncs);
2374     if (check_asyncs) { // TODO: we should probably be even more selective than that
2375       // we need this only for interpreted frames -- for compiled frames we install a return barrier on the safepoint stub in Continuation::try_force_yield
2376       StubRoutines::cont_jump_from_sp_C()();
2377     }
2378   }
2379 
2380   JFR_ONLY(SUSPEND_THREAD_CONDITIONAL(this);)
2381 }
2382 
2383 void JavaThread::send_thread_stop(oop java_throwable)  {
2384   assert(Thread::current()->is_VM_thread(), "should be in the vm thread");
2385   assert(Threads_lock->is_locked(), "Threads_lock should be locked by safepoint code");
2386   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
2387 
2388   // Do not throw asynchronous exceptions against the compiler thread
2389   // (the compiler thread should not be a Java thread -- fix in 1.4.2)
2390   if (!can_call_java()) return;
2391 
2392   {
2393     // Actually throw the Throwable against the target Thread - however
2394     // only if there is no thread death exception installed already.
2395     if (_pending_async_exception == NULL || !_pending_async_exception->is_a(SystemDictionary::ThreadDeath_klass())) {
2396       // If the topmost frame is a runtime stub, then we are calling into
2397       // OptoRuntime from compiled code. Some runtime stubs (new, monitor_exit..)
2398       // must deoptimize the caller before continuing, as the compiled  exception handler table
2399       // may not be valid


3049   case _thread_new_trans:         return "_thread_new_trans";
3050   case _thread_in_native:         return "_thread_in_native";
3051   case _thread_in_native_trans:   return "_thread_in_native_trans";
3052   case _thread_in_vm:             return "_thread_in_vm";
3053   case _thread_in_vm_trans:       return "_thread_in_vm_trans";
3054   case _thread_in_Java:           return "_thread_in_Java";
3055   case _thread_in_Java_trans:     return "_thread_in_Java_trans";
3056   case _thread_blocked:           return "_thread_blocked";
3057   case _thread_blocked_trans:     return "_thread_blocked_trans";
3058   default:                        return "unknown thread state";
3059   }
3060 }
3061 
3062 #ifndef PRODUCT
3063 void JavaThread::print_thread_state_on(outputStream *st) const {
3064   st->print_cr("   JavaThread state: %s", _get_thread_state_name(_thread_state));
3065 };
3066 void JavaThread::print_thread_state() const {
3067   print_thread_state_on(tty);
3068 }
3069 const char* JavaThread::thread_state_name() const {
3070   return _get_thread_state_name(_thread_state);
3071 }
3072 #endif // PRODUCT
3073 
3074 // Called by Threads::print() for VM_PrintThreads operation
3075 void JavaThread::print_on(outputStream *st, bool print_extended_info) const {
3076   st->print_raw("\"");
3077   st->print_raw(get_thread_name());
3078   st->print_raw("\" ");
3079   oop thread_oop = threadObj();
3080   if (thread_oop != NULL) {
3081     st->print("#" INT64_FORMAT " ", (int64_t)java_lang_Thread::thread_id(thread_oop));
3082     if (java_lang_Thread::is_daemon(thread_oop))  st->print("daemon ");
3083     st->print("prio=%d ", java_lang_Thread::priority(thread_oop));
3084   }
3085   Thread::print_on(st, print_extended_info);
3086   // print guess for valid stack memory region (assume 4K pages); helps lock debugging
3087   st->print_cr("[" INTPTR_FORMAT "]", (intptr_t)last_Java_sp() & ~right_n_bits(12));
3088   if (thread_oop != NULL) {
3089     st->print_cr("   java.lang.Thread.State: %s", java_lang_Thread::thread_status_name(thread_oop));
3090   }
3091 #ifndef PRODUCT


3376   f->oops_do(&print, NULL, (RegisterMap*)map);
3377 }
3378 
3379 // Print our all the locations that contain oops and whether they are
3380 // valid or not.  This useful when trying to find the oldest frame
3381 // where an oop has gone bad since the frame walk is from youngest to
3382 // oldest.
3383 void JavaThread::trace_oops() {
3384   tty->print_cr("[Trace oops]");
3385   frames_do(oops_print);
3386 }
3387 
3388 
3389 #ifdef ASSERT
3390 // Print or validate the layout of stack frames
3391 void JavaThread::print_frame_layout(int depth, bool validate_only) {
3392   ResourceMark rm;
3393   PRESERVE_EXCEPTION_MARK;
3394   FrameValues values;
3395   int frame_no = 0;
3396   for (StackFrameStream fst(this, false, true); !fst.is_done(); fst.next()) {
3397     fst.current()->describe(values, ++frame_no, fst.register_map());
3398     if (depth == frame_no) break;
3399   }
3400   Continuation::describe(values);
3401   if (validate_only) {
3402     values.validate();
3403   } else {
3404     tty->print_cr("[Describe stack layout]");
3405     values.print(this);
3406   }
3407 }
3408 #endif
3409 
3410 void JavaThread::trace_stack_from(vframe* start_vf) {
3411   ResourceMark rm;
3412   int vframe_no = 1;
3413   for (vframe* f = start_vf; f; f = f->sender()) {
3414     if (f->is_java_frame()) {
3415       javaVFrame::cast(f)->print_activation(vframe_no++);
3416     } else {
3417       f->print();
3418     }
3419     if (vframe_no > StackPrintLimit) {
3420       tty->print_cr("...<more frames>...");
3421       return;
3422     }
3423   }
3424 }
3425 
3426 
3427 void JavaThread::trace_stack() {
3428   if (!has_last_Java_frame()) return;
3429   ResourceMark rm;
3430   HandleMark   hm;
3431   RegisterMap reg_map(this, true, true);
3432   trace_stack_from(last_java_vframe(&reg_map));
3433 }
3434 
3435 
3436 #endif // PRODUCT
3437 
3438 
3439 javaVFrame* JavaThread::last_java_vframe(RegisterMap *reg_map) {
3440   assert(reg_map != NULL, "a map must be given");
3441   frame f = last_frame();
3442   for (vframe* vf = vframe::new_vframe(&f, reg_map, this); vf; vf = vf->sender()) {
3443     if (vf->is_java_frame()) return javaVFrame::cast(vf);
3444   }
3445   return NULL;
3446 }
3447 
3448 oop JavaThread::last_continuation() {
3449   if (threadObj() == (oop)NULL) return (oop)NULL; // happens during initialization
3450   
3451   return java_lang_Thread::continuation(threadObj());
3452 }
3453 
3454 Klass* JavaThread::security_get_caller_class(int depth) {
3455   vframeStream vfst(this);
3456   vfst.security_get_caller_frame(depth);
3457   if (!vfst.at_end()) {
3458     return vfst.method()->method_holder();
3459   }
3460   return NULL;
3461 }
3462 
3463 static void compiler_thread_entry(JavaThread* thread, TRAPS) {
3464   assert(thread->is_Compiler_thread(), "must be compiler thread");
3465   CompileBroker::compiler_thread_loop();
3466 }
3467 
3468 static void sweeper_thread_entry(JavaThread* thread, TRAPS) {
3469   NMethodSweeper::sweeper_loop();
3470 }
3471 
3472 // Create a CompilerThread


< prev index next >