< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  59 #include "prims/jvmtiDeferredUpdates.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/continuation.hpp"
  66 #include "runtime/continuationEntry.inline.hpp"
  67 #include "runtime/deoptimization.hpp"
  68 #include "runtime/escapeBarrier.hpp"
  69 #include "runtime/fieldDescriptor.hpp"
  70 #include "runtime/fieldDescriptor.inline.hpp"
  71 #include "runtime/frame.inline.hpp"
  72 #include "runtime/handles.inline.hpp"
  73 #include "runtime/interfaceSupport.inline.hpp"
  74 #include "runtime/javaThread.hpp"
  75 #include "runtime/jniHandles.inline.hpp"
  76 #include "runtime/keepStackGCProcessed.hpp"
  77 #include "runtime/objectMonitor.inline.hpp"
  78 #include "runtime/osThread.hpp"

  79 #include "runtime/safepointVerifiers.hpp"
  80 #include "runtime/sharedRuntime.hpp"
  81 #include "runtime/signature.hpp"
  82 #include "runtime/stackFrameStream.inline.hpp"
  83 #include "runtime/stackValue.hpp"
  84 #include "runtime/stackWatermarkSet.hpp"
  85 #include "runtime/stubRoutines.hpp"
  86 #include "runtime/synchronizer.hpp"
  87 #include "runtime/threadSMR.hpp"
  88 #include "runtime/threadWXSetters.inline.hpp"
  89 #include "runtime/vframe.hpp"
  90 #include "runtime/vframeArray.hpp"
  91 #include "runtime/vframe_hp.hpp"
  92 #include "runtime/vmOperations.hpp"

  93 #include "utilities/checkedCast.hpp"
  94 #include "utilities/events.hpp"
  95 #include "utilities/growableArray.hpp"
  96 #include "utilities/macros.hpp"
  97 #include "utilities/preserveException.hpp"
  98 #include "utilities/xmlstream.hpp"
  99 #if INCLUDE_JFR
 100 #include "jfr/jfrEvents.hpp"
 101 #include "jfr/metadata/jfrSerializer.hpp"
 102 #endif
 103 
 104 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 105 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 106 bool     DeoptimizationScope::_committing_in_progress = false;
 107 
 108 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 109   DEBUG_ONLY(_deopted = false;)
 110 
 111   MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
 112   // If there is nothing to deopt _required_gen is the same as comitted.

 256   return checked_cast<int>(result);
 257 }
 258 
 259 void Deoptimization::UnrollBlock::print() {
 260   ResourceMark rm;
 261   stringStream st;
 262   st.print_cr("UnrollBlock");
 263   st.print_cr("  size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
 264   st.print(   "  frame_sizes: ");
 265   for (int index = 0; index < number_of_frames(); index++) {
 266     st.print(INTX_FORMAT " ", frame_sizes()[index]);
 267   }
 268   st.cr();
 269   tty->print_raw(st.freeze());
 270 }
 271 
 272 // In order to make fetch_unroll_info work properly with escape
 273 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
 274 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
 275 // which is called from the method fetch_unroll_info_helper below.
 276 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
 277   // fetch_unroll_info() is called at the beginning of the deoptimization
 278   // handler. Note this fact before we start generating temporary frames
 279   // that can confuse an asynchronous stack walker. This counter is
 280   // decremented at the end of unpack_frames().
 281   current->inc_in_deopt_handler();
 282 
 283   if (exec_mode == Unpack_exception) {
 284     // When we get here, a callee has thrown an exception into a deoptimized
 285     // frame. That throw might have deferred stack watermark checking until
 286     // after unwinding. So we deal with such deferred requests here.
 287     StackWatermarkSet::after_unwind(current);
 288   }
 289 
 290   return fetch_unroll_info_helper(current, exec_mode);
 291 JRT_END
 292 
 293 #if COMPILER2_OR_JVMCI
 294 // print information about reallocated objects
 295 static void print_objects(JavaThread* deoptee_thread,
 296                           GrowableArray<ScopeValue*>* objects, bool realloc_failures) {

 828 
 829   assert(f->is_interpreted_frame(), "must be interpreted");
 830 }
 831 
 832 #ifndef PRODUCT
 833 static bool falls_through(Bytecodes::Code bc) {
 834   switch (bc) {
 835     // List may be incomplete.  Here we really only care about bytecodes where compiled code
 836     // can deoptimize.
 837     case Bytecodes::_goto:
 838     case Bytecodes::_goto_w:
 839     case Bytecodes::_athrow:
 840       return false;
 841     default:
 842       return true;
 843   }
 844 }
 845 #endif
 846 
 847 // Return BasicType of value being returned
 848 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
 849   assert(thread == JavaThread::current(), "pre-condition");
 850 
 851   // We are already active in the special DeoptResourceMark any ResourceObj's we
 852   // allocate will be freed at the end of the routine.
 853 
 854   // JRT_LEAF methods don't normally allocate handles and there is a
 855   // NoHandleMark to enforce that. It is actually safe to use Handles
 856   // in a JRT_LEAF method, and sometimes desirable, but to do so we
 857   // must use ResetNoHandleMark to bypass the NoHandleMark, and
 858   // then use a HandleMark to ensure any Handles we do create are
 859   // cleaned up in this scope.
 860   ResetNoHandleMark rnhm;
 861   HandleMark hm(thread);
 862 
 863   frame stub_frame = thread->last_frame();
 864 
 865   Continuation::notify_deopt(thread, stub_frame.sp());
 866 
 867   // Since the frame to unpack is the top frame of this thread, the vframe_array_head
 868   // must point to the vframeArray for the unpack frame.

1727     if (monitors != nullptr) {
1728       // Unlock in reverse order starting from most nested monitor.
1729       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1730         BasicObjectLock* src = monitors->at(j);
1731         if (src->obj() != nullptr) {
1732           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1733         }
1734       }
1735       array->element(i)->free_monitors(thread);
1736 #ifdef ASSERT
1737       array->element(i)->set_removed_monitors();
1738 #endif
1739     }
1740   }
1741 }
1742 #endif
1743 
1744 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1745   assert(fr.can_be_deoptimized(), "checking frame type");
1746 
1747   gather_statistics(reason, Action_none, Bytecodes::_illegal);






1748 
1749   if (LogCompilation && xtty != nullptr) {
1750     CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1751     assert(cm != nullptr, "only compiled methods can deopt");
1752 
1753     ttyLocker ttyl;
1754     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1755     cm->log_identity(xtty);
1756     xtty->end_head();
1757     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1758       xtty->begin_elem("jvms bci='%d'", sd->bci());
1759       xtty->method(sd->method());
1760       xtty->end_elem();
1761       if (sd->is_top())  break;
1762     }
1763     xtty->tail("deoptimized");
1764   }
1765 
1766   Continuation::notify_deopt(thread, fr.sp());
1767 
1768   // Patch the compiled method so that when execution returns to it we will
1769   // deopt the execution state and return to the interpreter.
1770   fr.deoptimize(thread);
1771 }
1772 

1982 static void log_deopt(CompiledMethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1983                               const char* reason_name, const char* reason_action) {
1984   LogTarget(Debug, deoptimization) lt;
1985   if (lt.is_enabled()) {
1986     LogStream ls(lt);
1987     bool is_osr = nm->is_osr_method();
1988     ls.print("cid=%4d %s level=%d",
1989              nm->compile_id(), (is_osr ? "osr" : "   "), nm->comp_level());
1990     ls.print(" %s", tm->name_and_sig_as_C_string());
1991     ls.print(" trap_bci=%d ", trap_bci);
1992     if (is_osr) {
1993       ls.print("osr_bci=%d ", nm->osr_entry_bci());
1994     }
1995     ls.print("%s ", reason_name);
1996     ls.print("%s ", reason_action);
1997     ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
1998              pc, fr.pc() - nm->code_begin());
1999   }
2000 }
2001 
2002 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2003   HandleMark hm(current);
2004 
2005   // uncommon_trap() is called at the beginning of the uncommon trap
2006   // handler. Note this fact before we start generating temporary frames
2007   // that can confuse an asynchronous stack walker. This counter is
2008   // decremented at the end of unpack_frames().
2009 
2010   current->inc_in_deopt_handler();
2011 
2012 #if INCLUDE_JVMCI
2013   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2014   RegisterMap reg_map(current,
2015                       RegisterMap::UpdateMap::include,
2016                       RegisterMap::ProcessFrames::include,
2017                       RegisterMap::WalkContinuation::skip);
2018 #else
2019   RegisterMap reg_map(current,
2020                       RegisterMap::UpdateMap::skip,
2021                       RegisterMap::ProcessFrames::include,
2022                       RegisterMap::WalkContinuation::skip);

2581   bool ignore_maybe_prior_recompile;
2582   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2583   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2584   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2585 
2586   // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2587   MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2588 
2589   query_update_method_data(trap_mdo, trap_bci,
2590                            (DeoptReason)reason,
2591                            update_total_counts,
2592 #if INCLUDE_JVMCI
2593                            false,
2594 #endif
2595                            nullptr,
2596                            ignore_this_trap_count,
2597                            ignore_maybe_prior_trap,
2598                            ignore_maybe_prior_recompile);
2599 }
2600 
2601 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2602   // Enable WXWrite: current function is called from methods compiled by C2 directly
2603   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2604 
2605   // Still in Java no safepoints
2606   {
2607     // This enters VM and may safepoint
2608     uncommon_trap_inner(current, trap_request);
2609   }
2610   HandleMark hm(current);
2611   return fetch_unroll_info_helper(current, exec_mode);
2612 }
2613 
2614 // Local derived constants.
2615 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2616 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2617 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2618 
2619 //---------------------------trap_state_reason---------------------------------
2620 Deoptimization::DeoptReason
2621 Deoptimization::trap_state_reason(int trap_state) {
2622   // This assert provides the link between the width of DataLayout::trap_bits
2623   // and the encoding of "recorded" reasons.  It ensures there are enough
2624   // bits to store all needed reasons in the per-BCI MDO profile.
2625   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2626   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2627   trap_state -= recompile_bit;
2628   if (trap_state == DS_REASON_MASK) {
2629     return Reason_many;
2630   } else {
2631     assert((int)Reason_none == 0, "state=0 => Reason_none");
2632     return (DeoptReason)trap_state;

2851   if (reason_str == nullptr && action_str == nullptr) {
2852     return total_deoptimization_count();
2853   }
2854   juint counter = 0;
2855   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2856     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2857       for (int action = 0; action < Action_LIMIT; action++) {
2858         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2859           juint* cases = _deoptimization_hist[reason][1+action];
2860           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2861             counter += cases[bc_case] >> LSB_BITS;
2862           }
2863         }
2864       }
2865     }
2866   }
2867   return counter;
2868 }
2869 
2870 void Deoptimization::print_statistics() {








2871   juint total = total_deoptimization_count();
2872   juint account = total;
2873   if (total != 0) {
2874     ttyLocker ttyl;
2875     if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2876     tty->print_cr("Deoptimization traps recorded:");
2877     #define PRINT_STAT_LINE(name, r) \
2878       tty->print_cr("  %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2879     PRINT_STAT_LINE("total", total);
2880     // For each non-zero entry in the histogram, print the reason,
2881     // the action, and (if specifically known) the type of bytecode.
2882     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2883       for (int action = 0; action < Action_LIMIT; action++) {
2884         juint* cases = _deoptimization_hist[reason][1+action];
2885         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2886           juint counter = cases[bc_case];
2887           if (counter != 0) {
2888             char name[1*K];
2889             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2890             if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2891               bc = Bytecodes::_illegal;
2892             os::snprintf_checked(name, sizeof(name), "%s/%s/%s",




2893                     trap_reason_name(reason),
2894                     trap_action_name(action),
2895                     Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2896             juint r = counter >> LSB_BITS;
2897             tty->print_cr("  %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2898             account -= r;
2899           }
2900         }
2901       }
2902     }
2903     if (account != 0) {
2904       PRINT_STAT_LINE("unaccounted", account);
2905     }
2906     #undef PRINT_STAT_LINE
2907     if (xtty != nullptr)  xtty->tail("statistics");
2908   }
2909 }
2910 
2911 #else // COMPILER2_OR_JVMCI
2912 
2913 
2914 // Stubs for C1 only system.
2915 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2916   return false;
2917 }
2918 
2919 const char* Deoptimization::trap_reason_name(int reason) {
2920   return "unknown";
2921 }
2922 
2923 jint Deoptimization::total_deoptimization_count() {
2924   return 0;
2925 }
2926 
2927 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {

2936 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2937   // no update
2938 }
2939 
2940 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2941   return 0;
2942 }
2943 
2944 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2945                                        Bytecodes::Code bc) {
2946   // no update
2947 }
2948 
2949 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2950                                               int trap_state) {
2951   jio_snprintf(buf, buflen, "#%d", trap_state);
2952   return buf;
2953 }
2954 
2955 #endif // COMPILER2_OR_JVMCI












































  59 #include "prims/jvmtiDeferredUpdates.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/continuation.hpp"
  66 #include "runtime/continuationEntry.inline.hpp"
  67 #include "runtime/deoptimization.hpp"
  68 #include "runtime/escapeBarrier.hpp"
  69 #include "runtime/fieldDescriptor.hpp"
  70 #include "runtime/fieldDescriptor.inline.hpp"
  71 #include "runtime/frame.inline.hpp"
  72 #include "runtime/handles.inline.hpp"
  73 #include "runtime/interfaceSupport.inline.hpp"
  74 #include "runtime/javaThread.hpp"
  75 #include "runtime/jniHandles.inline.hpp"
  76 #include "runtime/keepStackGCProcessed.hpp"
  77 #include "runtime/objectMonitor.inline.hpp"
  78 #include "runtime/osThread.hpp"
  79 #include "runtime/perfData.inline.hpp"
  80 #include "runtime/safepointVerifiers.hpp"
  81 #include "runtime/sharedRuntime.hpp"
  82 #include "runtime/signature.hpp"
  83 #include "runtime/stackFrameStream.inline.hpp"
  84 #include "runtime/stackValue.hpp"
  85 #include "runtime/stackWatermarkSet.hpp"
  86 #include "runtime/stubRoutines.hpp"
  87 #include "runtime/synchronizer.hpp"
  88 #include "runtime/threadSMR.hpp"
  89 #include "runtime/threadWXSetters.inline.hpp"
  90 #include "runtime/vframe.hpp"
  91 #include "runtime/vframeArray.hpp"
  92 #include "runtime/vframe_hp.hpp"
  93 #include "runtime/vmOperations.hpp"
  94 #include "services/management.hpp"
  95 #include "utilities/checkedCast.hpp"
  96 #include "utilities/events.hpp"
  97 #include "utilities/growableArray.hpp"
  98 #include "utilities/macros.hpp"
  99 #include "utilities/preserveException.hpp"
 100 #include "utilities/xmlstream.hpp"
 101 #if INCLUDE_JFR
 102 #include "jfr/jfrEvents.hpp"
 103 #include "jfr/metadata/jfrSerializer.hpp"
 104 #endif
 105 
 106 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 107 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 108 bool     DeoptimizationScope::_committing_in_progress = false;
 109 
 110 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 111   DEBUG_ONLY(_deopted = false;)
 112 
 113   MutexLocker ml(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
 114   // If there is nothing to deopt _required_gen is the same as comitted.

 258   return checked_cast<int>(result);
 259 }
 260 
 261 void Deoptimization::UnrollBlock::print() {
 262   ResourceMark rm;
 263   stringStream st;
 264   st.print_cr("UnrollBlock");
 265   st.print_cr("  size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
 266   st.print(   "  frame_sizes: ");
 267   for (int index = 0; index < number_of_frames(); index++) {
 268     st.print(INTX_FORMAT " ", frame_sizes()[index]);
 269   }
 270   st.cr();
 271   tty->print_raw(st.freeze());
 272 }
 273 
 274 // In order to make fetch_unroll_info work properly with escape
 275 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
 276 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
 277 // which is called from the method fetch_unroll_info_helper below.
 278 JRT_BLOCK_ENTRY_PROF(Deoptimization::UnrollBlock*, Deoptimization, fetch_unroll_info, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
 279   // fetch_unroll_info() is called at the beginning of the deoptimization
 280   // handler. Note this fact before we start generating temporary frames
 281   // that can confuse an asynchronous stack walker. This counter is
 282   // decremented at the end of unpack_frames().
 283   current->inc_in_deopt_handler();
 284 
 285   if (exec_mode == Unpack_exception) {
 286     // When we get here, a callee has thrown an exception into a deoptimized
 287     // frame. That throw might have deferred stack watermark checking until
 288     // after unwinding. So we deal with such deferred requests here.
 289     StackWatermarkSet::after_unwind(current);
 290   }
 291 
 292   return fetch_unroll_info_helper(current, exec_mode);
 293 JRT_END
 294 
 295 #if COMPILER2_OR_JVMCI
 296 // print information about reallocated objects
 297 static void print_objects(JavaThread* deoptee_thread,
 298                           GrowableArray<ScopeValue*>* objects, bool realloc_failures) {

 830 
 831   assert(f->is_interpreted_frame(), "must be interpreted");
 832 }
 833 
 834 #ifndef PRODUCT
 835 static bool falls_through(Bytecodes::Code bc) {
 836   switch (bc) {
 837     // List may be incomplete.  Here we really only care about bytecodes where compiled code
 838     // can deoptimize.
 839     case Bytecodes::_goto:
 840     case Bytecodes::_goto_w:
 841     case Bytecodes::_athrow:
 842       return false;
 843     default:
 844       return true;
 845   }
 846 }
 847 #endif
 848 
 849 // Return BasicType of value being returned
 850 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
 851   assert(thread == JavaThread::current(), "pre-condition");
 852 
 853   // We are already active in the special DeoptResourceMark any ResourceObj's we
 854   // allocate will be freed at the end of the routine.
 855 
 856   // JRT_LEAF methods don't normally allocate handles and there is a
 857   // NoHandleMark to enforce that. It is actually safe to use Handles
 858   // in a JRT_LEAF method, and sometimes desirable, but to do so we
 859   // must use ResetNoHandleMark to bypass the NoHandleMark, and
 860   // then use a HandleMark to ensure any Handles we do create are
 861   // cleaned up in this scope.
 862   ResetNoHandleMark rnhm;
 863   HandleMark hm(thread);
 864 
 865   frame stub_frame = thread->last_frame();
 866 
 867   Continuation::notify_deopt(thread, stub_frame.sp());
 868 
 869   // Since the frame to unpack is the top frame of this thread, the vframe_array_head
 870   // must point to the vframeArray for the unpack frame.

1729     if (monitors != nullptr) {
1730       // Unlock in reverse order starting from most nested monitor.
1731       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1732         BasicObjectLock* src = monitors->at(j);
1733         if (src->obj() != nullptr) {
1734           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1735         }
1736       }
1737       array->element(i)->free_monitors(thread);
1738 #ifdef ASSERT
1739       array->element(i)->set_removed_monitors();
1740 #endif
1741     }
1742   }
1743 }
1744 #endif
1745 
1746 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1747   assert(fr.can_be_deoptimized(), "checking frame type");
1748 
1749   CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1750   assert(cm != nullptr, "only compiled methods can deopt");
1751   DeoptAction action = (cm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1752   ScopeDesc* cur_sd = cm->scope_desc_at(fr.pc());
1753   Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1754                                             : cur_sd->method()->java_code_at(cur_sd->bci()));
1755   gather_statistics(reason, action, bc);
1756 
1757   if (LogCompilation && xtty != nullptr) {



1758     ttyLocker ttyl;
1759     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1760     cm->log_identity(xtty);
1761     xtty->end_head();
1762     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1763       xtty->begin_elem("jvms bci='%d'", sd->bci());
1764       xtty->method(sd->method());
1765       xtty->end_elem();
1766       if (sd->is_top())  break;
1767     }
1768     xtty->tail("deoptimized");
1769   }
1770 
1771   Continuation::notify_deopt(thread, fr.sp());
1772 
1773   // Patch the compiled method so that when execution returns to it we will
1774   // deopt the execution state and return to the interpreter.
1775   fr.deoptimize(thread);
1776 }
1777 

1987 static void log_deopt(CompiledMethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1988                               const char* reason_name, const char* reason_action) {
1989   LogTarget(Debug, deoptimization) lt;
1990   if (lt.is_enabled()) {
1991     LogStream ls(lt);
1992     bool is_osr = nm->is_osr_method();
1993     ls.print("cid=%4d %s level=%d",
1994              nm->compile_id(), (is_osr ? "osr" : "   "), nm->comp_level());
1995     ls.print(" %s", tm->name_and_sig_as_C_string());
1996     ls.print(" trap_bci=%d ", trap_bci);
1997     if (is_osr) {
1998       ls.print("osr_bci=%d ", nm->osr_entry_bci());
1999     }
2000     ls.print("%s ", reason_name);
2001     ls.print("%s ", reason_action);
2002     ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2003              pc, fr.pc() - nm->code_begin());
2004   }
2005 }
2006 
2007 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2008   HandleMark hm(current);
2009 
2010   // uncommon_trap() is called at the beginning of the uncommon trap
2011   // handler. Note this fact before we start generating temporary frames
2012   // that can confuse an asynchronous stack walker. This counter is
2013   // decremented at the end of unpack_frames().
2014 
2015   current->inc_in_deopt_handler();
2016 
2017 #if INCLUDE_JVMCI
2018   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2019   RegisterMap reg_map(current,
2020                       RegisterMap::UpdateMap::include,
2021                       RegisterMap::ProcessFrames::include,
2022                       RegisterMap::WalkContinuation::skip);
2023 #else
2024   RegisterMap reg_map(current,
2025                       RegisterMap::UpdateMap::skip,
2026                       RegisterMap::ProcessFrames::include,
2027                       RegisterMap::WalkContinuation::skip);

2586   bool ignore_maybe_prior_recompile;
2587   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2588   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2589   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2590 
2591   // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2592   MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2593 
2594   query_update_method_data(trap_mdo, trap_bci,
2595                            (DeoptReason)reason,
2596                            update_total_counts,
2597 #if INCLUDE_JVMCI
2598                            false,
2599 #endif
2600                            nullptr,
2601                            ignore_this_trap_count,
2602                            ignore_maybe_prior_trap,
2603                            ignore_maybe_prior_recompile);
2604 }
2605 
2606 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2607   // Enable WXWrite: current function is called from methods compiled by C2 directly
2608   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2609 
2610   // Still in Java no safepoints
2611   {
2612     // This enters VM and may safepoint
2613     uncommon_trap_inner(current, trap_request);
2614   }
2615   HandleMark hm(current);
2616   return fetch_unroll_info_helper(current, exec_mode);
2617 PROF_END
2618 
2619 // Local derived constants.
2620 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2621 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2622 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2623 
2624 //---------------------------trap_state_reason---------------------------------
2625 Deoptimization::DeoptReason
2626 Deoptimization::trap_state_reason(int trap_state) {
2627   // This assert provides the link between the width of DataLayout::trap_bits
2628   // and the encoding of "recorded" reasons.  It ensures there are enough
2629   // bits to store all needed reasons in the per-BCI MDO profile.
2630   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2631   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2632   trap_state -= recompile_bit;
2633   if (trap_state == DS_REASON_MASK) {
2634     return Reason_many;
2635   } else {
2636     assert((int)Reason_none == 0, "state=0 => Reason_none");
2637     return (DeoptReason)trap_state;

2856   if (reason_str == nullptr && action_str == nullptr) {
2857     return total_deoptimization_count();
2858   }
2859   juint counter = 0;
2860   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2861     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2862       for (int action = 0; action < Action_LIMIT; action++) {
2863         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2864           juint* cases = _deoptimization_hist[reason][1+action];
2865           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2866             counter += cases[bc_case] >> LSB_BITS;
2867           }
2868         }
2869       }
2870     }
2871   }
2872   return counter;
2873 }
2874 
2875 void Deoptimization::print_statistics() {
2876   ttyLocker ttyl;
2877   if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2878   tty->print_cr("Deoptimization traps recorded:");
2879   print_statistics_on(tty);
2880   if (xtty != nullptr)  xtty->tail("statistics");
2881 }
2882 
2883 void Deoptimization::print_statistics_on(outputStream* st) {
2884   juint total = total_deoptimization_count();
2885   juint account = total;
2886 #define PRINT_STAT_LINE(name, r) \
2887       st->print_cr("%d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2888   PRINT_STAT_LINE("total", total);
2889   if (total > 0) {



2890     // For each non-zero entry in the histogram, print the reason,
2891     // the action, and (if specifically known) the type of bytecode.
2892     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2893       for (int action = 0; action < Action_LIMIT; action++) {
2894         juint* cases = _deoptimization_hist[reason][1+action];
2895         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2896           juint counter = cases[bc_case];
2897           if (counter != 0) {
2898             char name[1*K];
2899             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2900             const char* bc_name = "other";
2901             if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2902               // overwritten
2903             } else if (Bytecodes::is_defined(bc)) {
2904               bc_name = Bytecodes::name(bc);
2905             }
2906             os::snprintf_checked(name, sizeof(name), "%-34s %16s %16s",
2907                     trap_reason_name(reason),
2908                     trap_action_name(action),
2909                     bc_name);
2910             juint r = counter >> LSB_BITS;
2911             st->print_cr("  %s: " UINT32_FORMAT_W(5) " (%4.1f%%)", name, r, (r * 100.0) / total);
2912             account -= r;
2913           }
2914         }
2915       }
2916     }
2917     if (account != 0) {
2918       PRINT_STAT_LINE("unaccounted", account);
2919     }
2920     #undef PRINT_STAT_LINE

2921   }
2922 }
2923 
2924 #else // COMPILER2_OR_JVMCI
2925 
2926 
2927 // Stubs for C1 only system.
2928 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2929   return false;
2930 }
2931 
2932 const char* Deoptimization::trap_reason_name(int reason) {
2933   return "unknown";
2934 }
2935 
2936 jint Deoptimization::total_deoptimization_count() {
2937   return 0;
2938 }
2939 
2940 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {

2949 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2950   // no update
2951 }
2952 
2953 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2954   return 0;
2955 }
2956 
2957 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2958                                        Bytecodes::Code bc) {
2959   // no update
2960 }
2961 
2962 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2963                                               int trap_state) {
2964   jio_snprintf(buf, buflen, "#%d", trap_state);
2965   return buf;
2966 }
2967 
2968 #endif // COMPILER2_OR_JVMCI
2969 
2970 #define DO_COUNTERS(macro) \
2971   macro(Deoptimization, fetch_unroll_info)   \
2972   macro(Deoptimization, unpack_frames) \
2973   macro(Deoptimization, uncommon_trap_inner) \
2974   macro(Deoptimization, uncommon_trap)
2975 
2976 #define INIT_COUNTER(sub, name) \
2977   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2978   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2979 
2980 void Deoptimization::init_counters() {
2981   if (ProfileRuntimeCalls && UsePerfData) {
2982     EXCEPTION_MARK;
2983 
2984     DO_COUNTERS(INIT_COUNTER)
2985 
2986     if (HAS_PENDING_EXCEPTION) {
2987       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2988     }
2989   }
2990 }
2991 #undef INIT_COUNTER
2992 
2993 #define PRINT_COUNTER(sub, name) { \
2994   jlong count = _perf_##sub##_##name##_count->get_value(); \
2995   if (count > 0) { \
2996     st->print_cr("  %-50s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
2997                  _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
2998                  _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
2999                  count); \
3000   }}
3001 
3002 void Deoptimization::print_counters_on(outputStream* st) {
3003   if (ProfileRuntimeCalls && UsePerfData) {
3004     DO_COUNTERS(PRINT_COUNTER)
3005   } else {
3006     st->print_cr("  Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3007   }
3008 }
3009 
3010 #undef PRINT_COUNTER
3011 #undef DO_COUNTERS
< prev index next >