< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/basicLock.inline.hpp"
  66 #include "runtime/continuation.hpp"
  67 #include "runtime/continuationEntry.inline.hpp"
  68 #include "runtime/deoptimization.hpp"
  69 #include "runtime/escapeBarrier.hpp"
  70 #include "runtime/fieldDescriptor.hpp"
  71 #include "runtime/fieldDescriptor.inline.hpp"
  72 #include "runtime/frame.inline.hpp"
  73 #include "runtime/handles.inline.hpp"
  74 #include "runtime/interfaceSupport.inline.hpp"
  75 #include "runtime/javaThread.hpp"
  76 #include "runtime/jniHandles.inline.hpp"
  77 #include "runtime/keepStackGCProcessed.hpp"
  78 #include "runtime/lightweightSynchronizer.hpp"
  79 #include "runtime/lockStack.inline.hpp"
  80 #include "runtime/objectMonitor.inline.hpp"
  81 #include "runtime/osThread.hpp"

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

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

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

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

1743     if (monitors != nullptr) {
1744       // Unlock in reverse order starting from most nested monitor.
1745       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1746         BasicObjectLock* src = monitors->at(j);
1747         if (src->obj() != nullptr) {
1748           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1749         }
1750       }
1751       array->element(i)->free_monitors();
1752 #ifdef ASSERT
1753       array->element(i)->set_removed_monitors();
1754 #endif
1755     }
1756   }
1757 }
1758 #endif
1759 
1760 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1761   assert(fr.can_be_deoptimized(), "checking frame type");
1762 
1763   gather_statistics(reason, Action_none, Bytecodes::_illegal);






1764 
1765   if (LogCompilation && xtty != nullptr) {
1766     nmethod* nm = fr.cb()->as_nmethod_or_null();
1767     assert(nm != nullptr, "only compiled methods can deopt");
1768 
1769     ttyLocker ttyl;
1770     xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1771     nm->log_identity(xtty);
1772     xtty->end_head();
1773     for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1774       xtty->begin_elem("jvms bci='%d'", sd->bci());
1775       xtty->method(sd->method());
1776       xtty->end_elem();
1777       if (sd->is_top())  break;
1778     }
1779     xtty->tail("deoptimized");
1780   }
1781 
1782   Continuation::notify_deopt(thread, fr.sp());
1783 
1784   // Patch the compiled method so that when execution returns to it we will
1785   // deopt the execution state and return to the interpreter.
1786   fr.deoptimize(thread);
1787 }
1788 

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

2074 #if INCLUDE_JVMCI
2075     jlong           speculation = current->pending_failed_speculation();
2076     if (nm->is_compiled_by_jvmci()) {
2077       nm->update_speculation(current);
2078     } else {
2079       assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2080     }
2081 
2082     if (trap_bci == SynchronizationEntryBCI) {
2083       trap_bci = 0;
2084       current->set_pending_monitorenter(true);
2085     }
2086 
2087     if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2088       current->set_pending_transfer_to_interpreter(true);
2089     }
2090 #endif
2091 
2092     Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
2093     // Record this event in the histogram.
2094     gather_statistics(reason, action, trap_bc);
2095 
2096     // Ensure that we can record deopt. history:
2097     bool create_if_missing = ProfileTraps;
2098 
2099     methodHandle profiled_method;
2100 #if INCLUDE_JVMCI
2101     if (nm->is_compiled_by_jvmci()) {
2102       profiled_method = methodHandle(current, nm->method());
2103     } else {
2104       profiled_method = trap_method;
2105     }
2106 #else
2107     profiled_method = trap_method;
2108 #endif
2109 
2110     MethodData* trap_mdo =
2111       get_method_data(current, profiled_method, create_if_missing);
2112 
2113     { // Log Deoptimization event for JFR, UL and event system
2114       Method* tm = trap_method();

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

2785   size_t len;
2786   if (unloaded_class_index < 0) {
2787     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2788                        reason, action
2789 #if INCLUDE_JVMCI
2790                        ,debug_id
2791 #endif
2792                        );
2793   } else {
2794     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2795                        reason, action, unloaded_class_index
2796 #if INCLUDE_JVMCI
2797                        ,debug_id
2798 #endif
2799                        );
2800   }
2801   return buf;
2802 }
2803 
2804 juint Deoptimization::_deoptimization_hist

2805         [Deoptimization::Reason_LIMIT]
2806     [1 + Deoptimization::Action_LIMIT]
2807         [Deoptimization::BC_CASE_LIMIT]
2808   = {0};
2809 
2810 enum {
2811   LSB_BITS = 8,
2812   LSB_MASK = right_n_bits(LSB_BITS)
2813 };
2814 
2815 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2816                                        Bytecodes::Code bc) {
2817   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2818   assert(action >= 0 && action < Action_LIMIT, "oob");
2819   _deoptimization_hist[Reason_none][0][0] += 1;  // total
2820   _deoptimization_hist[reason][0][0]      += 1;  // per-reason total
2821   juint* cases = _deoptimization_hist[reason][1+action];
2822   juint* bc_counter_addr = nullptr;
2823   juint  bc_counter      = 0;
2824   // Look for an unused counter, or an exact match to this BC.
2825   if (bc != Bytecodes::_illegal) {
2826     for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2827       juint* counter_addr = &cases[bc_case];
2828       juint  counter = *counter_addr;
2829       if ((counter == 0 && bc_counter_addr == nullptr)
2830           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2831         // this counter is either free or is already devoted to this BC
2832         bc_counter_addr = counter_addr;
2833         bc_counter = counter | bc;
2834       }
2835     }
2836   }
2837   if (bc_counter_addr == nullptr) {
2838     // Overflow, or no given bytecode.
2839     bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2840     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2841   }
2842   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2843 }
2844 
















2845 jint Deoptimization::total_deoptimization_count() {
2846   return _deoptimization_hist[Reason_none][0][0];
2847 }
2848 
2849 // Get the deopt count for a specific reason and a specific action. If either
2850 // one of 'reason' or 'action' is null, the method returns the sum of all
2851 // deoptimizations with the specific 'action' or 'reason' respectively.
2852 // If both arguments are null, the method returns the total deopt count.
2853 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2854   if (reason_str == nullptr && action_str == nullptr) {
2855     return total_deoptimization_count();
2856   }
2857   juint counter = 0;
2858   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2859     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2860       for (int action = 0; action < Action_LIMIT; action++) {
2861         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2862           juint* cases = _deoptimization_hist[reason][1+action];
2863           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2864             counter += cases[bc_case] >> LSB_BITS;
2865           }
2866         }
2867       }
2868     }
2869   }
2870   return counter;
2871 }
2872 
2873 void Deoptimization::print_statistics() {
2874   juint total = total_deoptimization_count();








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


2901             account -= r;
2902           }
2903         }
2904       }
2905     }
2906     if (account != 0) {
2907       PRINT_STAT_LINE("unaccounted", account);
2908     }
2909     #undef PRINT_STAT_LINE
2910     if (xtty != nullptr)  xtty->tail("statistics");
2911   }
2912 }
2913 
























































2914 #else // COMPILER2_OR_JVMCI
2915 
2916 
2917 // Stubs for C1 only system.
2918 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2919   return false;
2920 }
2921 
2922 const char* Deoptimization::trap_reason_name(int reason) {
2923   return "unknown";
2924 }
2925 
2926 jint Deoptimization::total_deoptimization_count() {
2927   return 0;
2928 }
2929 
2930 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2931   return 0;
2932 }
2933 
2934 void Deoptimization::print_statistics() {
2935   // no output
2936 }
2937 
2938 void
2939 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2940   // no update
2941 }
2942 
2943 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2944   return 0;
2945 }
2946 
2947 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2948                                        Bytecodes::Code bc) {
2949   // no update
2950 }
2951 
2952 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2953                                               int trap_state) {
2954   jio_snprintf(buf, buflen, "#%d", trap_state);
2955   return buf;
2956 }
2957 












2958 #endif // COMPILER2_OR_JVMCI

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

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

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

1745     if (monitors != nullptr) {
1746       // Unlock in reverse order starting from most nested monitor.
1747       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1748         BasicObjectLock* src = monitors->at(j);
1749         if (src->obj() != nullptr) {
1750           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1751         }
1752       }
1753       array->element(i)->free_monitors();
1754 #ifdef ASSERT
1755       array->element(i)->set_removed_monitors();
1756 #endif
1757     }
1758   }
1759 }
1760 #endif
1761 
1762 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1763   assert(fr.can_be_deoptimized(), "checking frame type");
1764 
1765   nmethod* nm = fr.cb()->as_nmethod_or_null();
1766   assert(nm != nullptr, "only compiled methods can deopt");
1767   DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1768   ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1769   Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1770                                             : cur_sd->method()->java_code_at(cur_sd->bci()));
1771   gather_statistics(nm, reason, action, bc);
1772 
1773   if (LogCompilation && xtty != nullptr) {



1774     ttyLocker ttyl;
1775     xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1776     nm->log_identity(xtty);
1777     xtty->end_head();
1778     for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1779       xtty->begin_elem("jvms bci='%d'", sd->bci());
1780       xtty->method(sd->method());
1781       xtty->end_elem();
1782       if (sd->is_top())  break;
1783     }
1784     xtty->tail("deoptimized");
1785   }
1786 
1787   Continuation::notify_deopt(thread, fr.sp());
1788 
1789   // Patch the compiled method so that when execution returns to it we will
1790   // deopt the execution state and return to the interpreter.
1791   fr.deoptimize(thread);
1792 }
1793 

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

2079 #if INCLUDE_JVMCI
2080     jlong           speculation = current->pending_failed_speculation();
2081     if (nm->is_compiled_by_jvmci()) {
2082       nm->update_speculation(current);
2083     } else {
2084       assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2085     }
2086 
2087     if (trap_bci == SynchronizationEntryBCI) {
2088       trap_bci = 0;
2089       current->set_pending_monitorenter(true);
2090     }
2091 
2092     if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2093       current->set_pending_transfer_to_interpreter(true);
2094     }
2095 #endif
2096 
2097     Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
2098     // Record this event in the histogram.
2099     gather_statistics(nm, reason, action, trap_bc);
2100 
2101     // Ensure that we can record deopt. history:
2102     bool create_if_missing = ProfileTraps;
2103 
2104     methodHandle profiled_method;
2105 #if INCLUDE_JVMCI
2106     if (nm->is_compiled_by_jvmci()) {
2107       profiled_method = methodHandle(current, nm->method());
2108     } else {
2109       profiled_method = trap_method;
2110     }
2111 #else
2112     profiled_method = trap_method;
2113 #endif
2114 
2115     MethodData* trap_mdo =
2116       get_method_data(current, profiled_method, create_if_missing);
2117 
2118     { // Log Deoptimization event for JFR, UL and event system
2119       Method* tm = trap_method();

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

2790   size_t len;
2791   if (unloaded_class_index < 0) {
2792     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2793                        reason, action
2794 #if INCLUDE_JVMCI
2795                        ,debug_id
2796 #endif
2797                        );
2798   } else {
2799     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2800                        reason, action, unloaded_class_index
2801 #if INCLUDE_JVMCI
2802                        ,debug_id
2803 #endif
2804                        );
2805   }
2806   return buf;
2807 }
2808 
2809 juint Deoptimization::_deoptimization_hist
2810         [1 + 4 + 5] // total + online + archived
2811         [Deoptimization::Reason_LIMIT]
2812     [1 + Deoptimization::Action_LIMIT]
2813         [Deoptimization::BC_CASE_LIMIT]
2814   = {0};
2815 
2816 enum {
2817   LSB_BITS = 8,
2818   LSB_MASK = right_n_bits(LSB_BITS)
2819 };
2820 
2821 static void update(juint* cases, Bytecodes::Code bc) {






2822   juint* bc_counter_addr = nullptr;
2823   juint  bc_counter      = 0;
2824   // Look for an unused counter, or an exact match to this BC.
2825   if (bc != Bytecodes::_illegal) {
2826     for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2827       juint* counter_addr = &cases[bc_case];
2828       juint  counter = *counter_addr;
2829       if ((counter == 0 && bc_counter_addr == nullptr)
2830           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2831         // this counter is either free or is already devoted to this BC
2832         bc_counter_addr = counter_addr;
2833         bc_counter = counter | bc;
2834       }
2835     }
2836   }
2837   if (bc_counter_addr == nullptr) {
2838     // Overflow, or no given bytecode.
2839     bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2840     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2841   }
2842   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2843 }
2844 
2845 
2846 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2847                                        Bytecodes::Code bc) {
2848   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2849   assert(action >= 0 && action < Action_LIMIT, "oob");
2850   _deoptimization_hist[0][Reason_none][0][0] += 1;  // total
2851   _deoptimization_hist[0][reason][0][0]      += 1;  // per-reason total
2852 
2853   update(_deoptimization_hist[0][reason][1+action], bc);
2854 
2855   uint lvl = nm->comp_level() + (nm->is_scc() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2856   _deoptimization_hist[lvl][Reason_none][0][0] += 1;  // total
2857   _deoptimization_hist[lvl][reason][0][0]      += 1;  // per-reason total
2858   update(_deoptimization_hist[lvl][reason][1+action], bc);
2859 }
2860 
2861 jint Deoptimization::total_deoptimization_count() {
2862   return _deoptimization_hist[0][Reason_none][0][0];
2863 }
2864 
2865 // Get the deopt count for a specific reason and a specific action. If either
2866 // one of 'reason' or 'action' is null, the method returns the sum of all
2867 // deoptimizations with the specific 'action' or 'reason' respectively.
2868 // If both arguments are null, the method returns the total deopt count.
2869 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2870   if (reason_str == nullptr && action_str == nullptr) {
2871     return total_deoptimization_count();
2872   }
2873   juint counter = 0;
2874   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2875     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2876       for (int action = 0; action < Action_LIMIT; action++) {
2877         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2878           juint* cases = _deoptimization_hist[0][reason][1+action];
2879           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2880             counter += cases[bc_case] >> LSB_BITS;
2881           }
2882         }
2883       }
2884     }
2885   }
2886   return counter;
2887 }
2888 
2889 void Deoptimization::print_statistics() {
2890   ttyLocker ttyl;
2891   if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2892   tty->print_cr("Deoptimization traps recorded:");
2893   print_statistics_on(tty);
2894   if (xtty != nullptr)  xtty->tail("statistics");
2895 }
2896 
2897 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2898   juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2899   juint account = total;
2900 #define PRINT_STAT_LINE(name, r) \
2901       st->print_cr("    %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2902   if (total > 0) {
2903     st->print("  %s: ", title);


2904     PRINT_STAT_LINE("total", total);
2905     // For each non-zero entry in the histogram, print the reason,
2906     // the action, and (if specifically known) the type of bytecode.
2907     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2908       for (int action = 0; action < Action_LIMIT; action++) {
2909         juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2910         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2911           juint counter = cases[bc_case];
2912           if (counter != 0) {

2913             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2914             const char* bc_name = "other";
2915             if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2916               // overwritten
2917             } else if (Bytecodes::is_defined(bc)) {
2918               bc_name = Bytecodes::name(bc);
2919             }
2920             juint r = counter >> LSB_BITS;
2921             st->print_cr("    %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2922                          trap_reason_name(reason), trap_action_name(action), bc_name,
2923                          r, (r * 100.0) / total);
2924             account -= r;
2925           }
2926         }
2927       }
2928     }
2929     if (account != 0) {
2930       PRINT_STAT_LINE("unaccounted", account);
2931     }
2932 #undef PRINT_STAT_LINE

2933   }
2934 }
2935 
2936 void Deoptimization::print_statistics_on(outputStream* st) {
2937 //  print_statistics_on("Total", 0, st);
2938   print_statistics_on("Tier1", 1, st);
2939   print_statistics_on("Tier2", 2, st);
2940   print_statistics_on("Tier3", 3, st);
2941   print_statistics_on("Tier4", 4, st);
2942 
2943   print_statistics_on("SC Tier1", 5, st);
2944   print_statistics_on("SC Tier2", 6, st);
2945   print_statistics_on("SC Tier4", 8, st);
2946   print_statistics_on("SC Tier5 (preloaded)", 9, st);
2947 }
2948 
2949 #define DO_COUNTERS(macro) \
2950   macro(Deoptimization, fetch_unroll_info)   \
2951   macro(Deoptimization, unpack_frames) \
2952   macro(Deoptimization, uncommon_trap_inner) \
2953   macro(Deoptimization, uncommon_trap)
2954 
2955 #define INIT_COUNTER(sub, name) \
2956   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2957   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2958 
2959 void Deoptimization::init_counters() {
2960   if (ProfileRuntimeCalls && UsePerfData) {
2961     EXCEPTION_MARK;
2962 
2963     DO_COUNTERS(INIT_COUNTER)
2964 
2965     if (HAS_PENDING_EXCEPTION) {
2966       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2967     }
2968   }
2969 }
2970 #undef INIT_COUNTER
2971 
2972 #define PRINT_COUNTER(sub, name) { \
2973   jlong count = _perf_##sub##_##name##_count->get_value(); \
2974   if (count > 0) { \
2975     st->print_cr("  %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2976                  _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2977                  _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2978                  count); \
2979   }}
2980 
2981 void Deoptimization::print_counters_on(outputStream* st) {
2982   if (ProfileRuntimeCalls && UsePerfData) {
2983     DO_COUNTERS(PRINT_COUNTER)
2984   } else {
2985     st->print_cr("  Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
2986   }
2987 }
2988 
2989 #undef PRINT_COUNTER
2990 #undef DO_COUNTERS
2991 
2992 #else // COMPILER2_OR_JVMCI
2993 
2994 
2995 // Stubs for C1 only system.
2996 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2997   return false;
2998 }
2999 
3000 const char* Deoptimization::trap_reason_name(int reason) {
3001   return "unknown";
3002 }
3003 
3004 jint Deoptimization::total_deoptimization_count() {
3005   return 0;
3006 }
3007 
3008 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
3009   return 0;
3010 }
3011 
3012 void Deoptimization::print_statistics() {
3013   // no output
3014 }
3015 
3016 void
3017 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
3018   // no update
3019 }
3020 
3021 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
3022   return 0;
3023 }
3024 
3025 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
3026                                        Bytecodes::Code bc) {
3027   // no update
3028 }
3029 
3030 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
3031                                               int trap_state) {
3032   jio_snprintf(buf, buflen, "#%d", trap_state);
3033   return buf;
3034 }
3035 
3036 void Deoptimization::init_counters() {
3037   // nothing to do
3038 }
3039 
3040 void Deoptimization::print_counters_on(outputStream* st) {
3041   // no output
3042 }
3043 
3044 void Deoptimization::print_statistics_on(outputStream* st) {
3045   // no output
3046 }
3047 
3048 #endif // COMPILER2_OR_JVMCI
< prev index next >