< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomicAccess.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.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/lockStack.inline.hpp"
  78 #include "runtime/objectMonitor.inline.hpp"
  79 #include "runtime/osThread.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/vframe_hp.hpp"
  92 #include "runtime/vframeArray.hpp"
  93 #include "runtime/vmOperations.hpp"

  94 #include "utilities/checkedCast.hpp"
  95 #include "utilities/events.hpp"
  96 #include "utilities/growableArray.hpp"
  97 #include "utilities/macros.hpp"
  98 #include "utilities/preserveException.hpp"
  99 #include "utilities/xmlstream.hpp"
 100 #if INCLUDE_JFR
 101 #include "jfr/jfr.inline.hpp"
 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(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

 849     case Bytecodes::_athrow:
 850     case Bytecodes::_areturn:
 851     case Bytecodes::_dreturn:
 852     case Bytecodes::_freturn:
 853     case Bytecodes::_ireturn:
 854     case Bytecodes::_lreturn:
 855     case Bytecodes::_jsr:
 856     case Bytecodes::_ret:
 857     case Bytecodes::_return:
 858     case Bytecodes::_lookupswitch:
 859     case Bytecodes::_tableswitch:
 860       return false;
 861     default:
 862       return true;
 863   }
 864 }
 865 #endif
 866 #endif
 867 
 868 // Return BasicType of value being returned
 869 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
 870   assert(thread == JavaThread::current(), "pre-condition");
 871 
 872   // We are already active in the special DeoptResourceMark any ResourceObj's we
 873   // allocate will be freed at the end of the routine.
 874 
 875   // JRT_LEAF methods don't normally allocate handles and there is a
 876   // NoHandleMark to enforce that. It is actually safe to use Handles
 877   // in a JRT_LEAF method, and sometimes desirable, but to do so we
 878   // must use ResetNoHandleMark to bypass the NoHandleMark, and
 879   // then use a HandleMark to ensure any Handles we do create are
 880   // cleaned up in this scope.
 881   ResetNoHandleMark rnhm;
 882   HandleMark hm(thread);
 883 
 884   frame stub_frame = thread->last_frame();
 885 
 886   Continuation::notify_deopt(thread, stub_frame.sp());
 887 
 888   // Since the frame to unpack is the top frame of this thread, the vframe_array_head
 889   // must point to the vframeArray for the unpack frame.

1755     if (monitors != nullptr) {
1756       // Unlock in reverse order starting from most nested monitor.
1757       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1758         BasicObjectLock* src = monitors->at(j);
1759         if (src->obj() != nullptr) {
1760           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1761         }
1762       }
1763       array->element(i)->free_monitors();
1764 #ifdef ASSERT
1765       array->element(i)->set_removed_monitors();
1766 #endif
1767     }
1768   }
1769 }
1770 #endif
1771 
1772 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1773   assert(fr.can_be_deoptimized(), "checking frame type");
1774 
1775   gather_statistics(reason, Action_none, Bytecodes::_illegal);






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

2005     event.set_compileId(nm->compile_id());
2006     event.set_compiler(nm->compiler_type());
2007     event.set_method(method);
2008     event.set_lineNumber(method->line_number_from_bci(trap_bci));
2009     event.set_bci(trap_bci);
2010     event.set_instruction(instruction);
2011     event.set_reason(reason);
2012     event.set_action(action);
2013     event.commit();
2014   }
2015 }
2016 
2017 #endif // INCLUDE_JFR
2018 
2019 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2020                               const char* reason_name, const char* reason_action) {
2021   LogTarget(Debug, deoptimization) lt;
2022   if (lt.is_enabled()) {
2023     LogStream ls(lt);
2024     bool is_osr = nm->is_osr_method();
2025     ls.print("cid=%4d %s level=%d",
2026              nm->compile_id(), (is_osr ? "osr" : "   "), nm->comp_level());



2027     ls.print(" %s", tm->name_and_sig_as_C_string());
2028     ls.print(" trap_bci=%d ", trap_bci);
2029     if (is_osr) {
2030       ls.print("osr_bci=%d ", nm->osr_entry_bci());
2031     }
2032     ls.print("%s ", reason_name);
2033     ls.print("%s ", reason_action);
2034     ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2035              pc, fr.pc() - nm->code_begin());
2036   }
2037 }
2038 
2039 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2040   HandleMark hm(current);
2041 
2042   // uncommon_trap() is called at the beginning of the uncommon trap
2043   // handler. Note this fact before we start generating temporary frames
2044   // that can confuse an asynchronous stack walker. This counter is
2045   // decremented at the end of unpack_frames().
2046 
2047   current->inc_in_deopt_handler();
2048 
2049 #if INCLUDE_JVMCI
2050   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2051   RegisterMap reg_map(current,
2052                       RegisterMap::UpdateMap::include,
2053                       RegisterMap::ProcessFrames::include,
2054                       RegisterMap::WalkContinuation::skip);
2055 #else
2056   RegisterMap reg_map(current,
2057                       RegisterMap::UpdateMap::skip,
2058                       RegisterMap::ProcessFrames::include,
2059                       RegisterMap::WalkContinuation::skip);

2095 #if INCLUDE_JVMCI
2096     jlong           speculation = current->pending_failed_speculation();
2097     if (nm->is_compiled_by_jvmci()) {
2098       nm->update_speculation(current);
2099     } else {
2100       assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2101     }
2102 
2103     if (trap_bci == SynchronizationEntryBCI) {
2104       trap_bci = 0;
2105       current->set_pending_monitorenter(true);
2106     }
2107 
2108     if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2109       current->set_pending_transfer_to_interpreter(true);
2110     }
2111 #endif
2112 
2113     Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
2114     // Record this event in the histogram.
2115     gather_statistics(reason, action, trap_bc);
2116 
2117     // Ensure that we can record deopt. history:
2118     bool create_if_missing = ProfileTraps;
2119 
2120     methodHandle profiled_method;
2121 #if INCLUDE_JVMCI
2122     if (nm->is_compiled_by_jvmci()) {
2123       profiled_method = methodHandle(current, nm->method());
2124     } else {
2125       profiled_method = trap_method;
2126     }
2127 #else
2128     profiled_method = trap_method;
2129 #endif
2130 

2131     MethodData* trap_mdo =
2132       get_method_data(current, profiled_method, create_if_missing);
2133 
2134     { // Log Deoptimization event for JFR, UL and event system
2135       Method* tm = trap_method();
2136       const char* reason_name = trap_reason_name(reason);
2137       const char* reason_action = trap_action_name(action);
2138       intptr_t pc = p2i(fr.pc());
2139 
2140       JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)
2141       log_deopt(nm, tm, pc, fr, trap_bci, reason_name, reason_action);
2142       Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s",
2143                                 reason_name, reason_action, pc,
2144                                 tm->name_and_sig_as_C_string(), trap_bci, nm->compiler_name());
2145     }
2146 
2147     // Print a bunch of diagnostics, if requested.
2148     if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2149       ResourceMark rm;
2150 
2151       // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2152       // We must do this already now, since we cannot acquire this lock while
2153       // holding the tty lock (lock ordering by rank).
2154       MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2155 
2156       ttyLocker ttyl;
2157 
2158       char buf[100];
2159       if (xtty != nullptr) {
2160         xtty->begin_head("uncommon_trap thread='%zu' %s",
2161                          os::current_thread_id(),
2162                          format_trap_request(buf, sizeof(buf), trap_request));
2163 #if INCLUDE_JVMCI
2164         if (speculation != 0) {

2195         ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2196         int dos = (pdata == nullptr)? 0: pdata->trap_state();
2197         if (dos != 0) {
2198           xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2199           if (trap_state_is_recompiled(dos)) {
2200             int recnt2 = trap_mdo->overflow_recompile_count();
2201             if (recnt2 != 0)
2202               xtty->print(" recompiles2='%d'", recnt2);
2203           }
2204         }
2205       }
2206       if (xtty != nullptr) {
2207         xtty->stamp();
2208         xtty->end_head();
2209       }
2210       if (TraceDeoptimization) {  // make noise on the tty
2211         stringStream st;
2212         st.print("UNCOMMON TRAP method=%s", trap_scope->method()->name_and_sig_as_C_string());
2213         st.print("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT JVMCI_ONLY(", debug_id=%d"),
2214                  trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin() JVMCI_ONLY(COMMA debug_id));
2215         st.print(" compiler=%s compile_id=%d", nm->compiler_name(), nm->compile_id());
2216 #if INCLUDE_JVMCI
2217         if (nm->is_compiled_by_jvmci()) {
2218           const char* installed_code_name = nm->jvmci_name();
2219           if (installed_code_name != nullptr) {
2220             st.print(" (JVMCI: installed code name=%s) ", installed_code_name);
2221           }
2222         }
2223 #endif
2224         st.print(" (@" INTPTR_FORMAT ") thread=%zu reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2225                    p2i(fr.pc()),
2226                    os::current_thread_id(),
2227                    trap_reason_name(reason),
2228                    trap_action_name(action),
2229                    unloaded_class_index
2230 #if INCLUDE_JVMCI
2231                    , debug_id
2232 #endif
2233                    );
2234         if (class_name != nullptr) {
2235           st.print(unresolved ? " unresolved class: " : " symbol: ");

2613   bool ignore_maybe_prior_recompile;
2614   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2615   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2616   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2617 
2618   // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2619   MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2620 
2621   query_update_method_data(trap_mdo, trap_bci,
2622                            (DeoptReason)reason,
2623                            update_total_counts,
2624 #if INCLUDE_JVMCI
2625                            false,
2626 #endif
2627                            nullptr,
2628                            ignore_this_trap_count,
2629                            ignore_maybe_prior_trap,
2630                            ignore_maybe_prior_recompile);
2631 }
2632 
2633 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2634   // Enable WXWrite: current function is called from methods compiled by C2 directly
2635   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2636 
2637   // Still in Java no safepoints
2638   {
2639     // This enters VM and may safepoint
2640     uncommon_trap_inner(current, trap_request);
2641   }
2642   HandleMark hm(current);
2643   return fetch_unroll_info_helper(current, exec_mode);
2644 }
2645 
2646 // Local derived constants.
2647 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2648 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2649 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2650 
2651 //---------------------------trap_state_reason---------------------------------
2652 Deoptimization::DeoptReason
2653 Deoptimization::trap_state_reason(int trap_state) {
2654   // This assert provides the link between the width of DataLayout::trap_bits
2655   // and the encoding of "recorded" reasons.  It ensures there are enough
2656   // bits to store all needed reasons in the per-BCI MDO profile.
2657   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2658   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2659   trap_state -= recompile_bit;
2660   if (trap_state == DS_REASON_MASK) {
2661     return Reason_many;
2662   } else {
2663     assert((int)Reason_none == 0, "state=0 => Reason_none");
2664     return (DeoptReason)trap_state;

2814   size_t len;
2815   if (unloaded_class_index < 0) {
2816     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2817                        reason, action
2818 #if INCLUDE_JVMCI
2819                        ,debug_id
2820 #endif
2821                        );
2822   } else {
2823     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2824                        reason, action, unloaded_class_index
2825 #if INCLUDE_JVMCI
2826                        ,debug_id
2827 #endif
2828                        );
2829   }
2830   return buf;
2831 }
2832 
2833 juint Deoptimization::_deoptimization_hist

2834         [Deoptimization::Reason_LIMIT]
2835     [1 + Deoptimization::Action_LIMIT]
2836         [Deoptimization::BC_CASE_LIMIT]
2837   = {0};
2838 
2839 enum {
2840   LSB_BITS = 8,
2841   LSB_MASK = right_n_bits(LSB_BITS)
2842 };
2843 
2844 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2845                                        Bytecodes::Code bc) {
2846   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2847   assert(action >= 0 && action < Action_LIMIT, "oob");
2848   _deoptimization_hist[Reason_none][0][0] += 1;  // total
2849   _deoptimization_hist[reason][0][0]      += 1;  // per-reason total
2850   juint* cases = _deoptimization_hist[reason][1+action];
2851   juint* bc_counter_addr = nullptr;
2852   juint  bc_counter      = 0;
2853   // Look for an unused counter, or an exact match to this BC.
2854   if (bc != Bytecodes::_illegal) {
2855     for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2856       juint* counter_addr = &cases[bc_case];
2857       juint  counter = *counter_addr;
2858       if ((counter == 0 && bc_counter_addr == nullptr)
2859           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2860         // this counter is either free or is already devoted to this BC
2861         bc_counter_addr = counter_addr;
2862         bc_counter = counter | bc;
2863       }
2864     }
2865   }
2866   if (bc_counter_addr == nullptr) {
2867     // Overflow, or no given bytecode.
2868     bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2869     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2870   }
2871   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2872 }
2873 
















2874 jint Deoptimization::total_deoptimization_count() {
2875   return _deoptimization_hist[Reason_none][0][0];
2876 }
2877 
2878 // Get the deopt count for a specific reason and a specific action. If either
2879 // one of 'reason' or 'action' is null, the method returns the sum of all
2880 // deoptimizations with the specific 'action' or 'reason' respectively.
2881 // If both arguments are null, the method returns the total deopt count.
2882 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2883   if (reason_str == nullptr && action_str == nullptr) {
2884     return total_deoptimization_count();
2885   }
2886   juint counter = 0;
2887   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2888     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2889       for (int action = 0; action < Action_LIMIT; action++) {
2890         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2891           juint* cases = _deoptimization_hist[reason][1+action];
2892           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2893             counter += cases[bc_case] >> LSB_BITS;
2894           }
2895         }
2896       }
2897     }
2898   }
2899   return counter;
2900 }
2901 
2902 void Deoptimization::print_statistics() {
2903   juint total = total_deoptimization_count();








2904   juint account = total;
2905   if (total != 0) {
2906     ttyLocker ttyl;
2907     if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2908     tty->print_cr("Deoptimization traps recorded:");
2909     #define PRINT_STAT_LINE(name, r) \
2910       tty->print_cr("  %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2911     PRINT_STAT_LINE("total", total);
2912     // For each non-zero entry in the histogram, print the reason,
2913     // the action, and (if specifically known) the type of bytecode.
2914     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2915       for (int action = 0; action < Action_LIMIT; action++) {
2916         juint* cases = _deoptimization_hist[reason][1+action];
2917         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2918           juint counter = cases[bc_case];
2919           if (counter != 0) {
2920             char name[1*K];
2921             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2922             os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2923                     trap_reason_name(reason),
2924                     trap_action_name(action),
2925                     Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");


2926             juint r = counter >> LSB_BITS;
2927             tty->print_cr("  %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);


2928             account -= r;
2929           }
2930         }
2931       }
2932     }
2933     if (account != 0) {
2934       PRINT_STAT_LINE("unaccounted", account);
2935     }
2936     #undef PRINT_STAT_LINE
2937     if (xtty != nullptr)  xtty->tail("statistics");
2938   }
2939 }
2940 
























































2941 #else // COMPILER2_OR_JVMCI
2942 
2943 
2944 // Stubs for C1 only system.
2945 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2946   return false;
2947 }
2948 
2949 const char* Deoptimization::trap_reason_name(int reason) {
2950   return "unknown";
2951 }
2952 
2953 jint Deoptimization::total_deoptimization_count() {
2954   return 0;
2955 }
2956 
2957 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2958   return 0;
2959 }
2960 
2961 void Deoptimization::print_statistics() {
2962   // no output
2963 }
2964 
2965 void
2966 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2967   // no update
2968 }
2969 
2970 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2971   return 0;
2972 }
2973 
2974 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2975                                        Bytecodes::Code bc) {
2976   // no update
2977 }
2978 
2979 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2980                                               int trap_state) {
2981   jio_snprintf(buf, buflen, "#%d", trap_state);
2982   return buf;
2983 }
2984 












2985 #endif // COMPILER2_OR_JVMCI

  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomicAccess.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.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/lockStack.inline.hpp"
  78 #include "runtime/objectMonitor.inline.hpp"
  79 #include "runtime/osThread.hpp"
  80 #include "runtime/perfData.inline.hpp"
  81 #include "runtime/safepointVerifiers.hpp"
  82 #include "runtime/sharedRuntime.hpp"
  83 #include "runtime/signature.hpp"
  84 #include "runtime/stackFrameStream.inline.hpp"
  85 #include "runtime/stackValue.hpp"
  86 #include "runtime/stackWatermarkSet.hpp"
  87 #include "runtime/stubRoutines.hpp"
  88 #include "runtime/synchronizer.hpp"
  89 #include "runtime/threadSMR.hpp"
  90 #include "runtime/threadWXSetters.inline.hpp"
  91 #include "runtime/vframe.hpp"
  92 #include "runtime/vframe_hp.hpp"
  93 #include "runtime/vframeArray.hpp"
  94 #include "runtime/vmOperations.hpp"
  95 #include "services/management.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/jfr.inline.hpp"
 104 #include "jfr/jfrEvents.hpp"
 105 #include "jfr/metadata/jfrSerializer.hpp"
 106 #endif
 107 
 108 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 109 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 110 bool     DeoptimizationScope::_committing_in_progress = false;
 111 
 112 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 113   DEBUG_ONLY(_deopted = false;)
 114 
 115   MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

 851     case Bytecodes::_athrow:
 852     case Bytecodes::_areturn:
 853     case Bytecodes::_dreturn:
 854     case Bytecodes::_freturn:
 855     case Bytecodes::_ireturn:
 856     case Bytecodes::_lreturn:
 857     case Bytecodes::_jsr:
 858     case Bytecodes::_ret:
 859     case Bytecodes::_return:
 860     case Bytecodes::_lookupswitch:
 861     case Bytecodes::_tableswitch:
 862       return false;
 863     default:
 864       return true;
 865   }
 866 }
 867 #endif
 868 #endif
 869 
 870 // Return BasicType of value being returned
 871 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
 872   assert(thread == JavaThread::current(), "pre-condition");
 873 
 874   // We are already active in the special DeoptResourceMark any ResourceObj's we
 875   // allocate will be freed at the end of the routine.
 876 
 877   // JRT_LEAF methods don't normally allocate handles and there is a
 878   // NoHandleMark to enforce that. It is actually safe to use Handles
 879   // in a JRT_LEAF method, and sometimes desirable, but to do so we
 880   // must use ResetNoHandleMark to bypass the NoHandleMark, and
 881   // then use a HandleMark to ensure any Handles we do create are
 882   // cleaned up in this scope.
 883   ResetNoHandleMark rnhm;
 884   HandleMark hm(thread);
 885 
 886   frame stub_frame = thread->last_frame();
 887 
 888   Continuation::notify_deopt(thread, stub_frame.sp());
 889 
 890   // Since the frame to unpack is the top frame of this thread, the vframe_array_head
 891   // must point to the vframeArray for the unpack frame.

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



1786     ttyLocker ttyl;
1787     xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1788     nm->log_identity(xtty);
1789     xtty->end_head();
1790     for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1791       xtty->begin_elem("jvms bci='%d'", sd->bci());
1792       xtty->method(sd->method());
1793       xtty->end_elem();
1794       if (sd->is_top())  break;
1795     }
1796     xtty->tail("deoptimized");
1797   }
1798 
1799   Continuation::notify_deopt(thread, fr.sp());
1800 
1801   // Patch the compiled method so that when execution returns to it we will
1802   // deopt the execution state and return to the interpreter.
1803   fr.deoptimize(thread);
1804 }
1805 

2010     event.set_compileId(nm->compile_id());
2011     event.set_compiler(nm->compiler_type());
2012     event.set_method(method);
2013     event.set_lineNumber(method->line_number_from_bci(trap_bci));
2014     event.set_bci(trap_bci);
2015     event.set_instruction(instruction);
2016     event.set_reason(reason);
2017     event.set_action(action);
2018     event.commit();
2019   }
2020 }
2021 
2022 #endif // INCLUDE_JFR
2023 
2024 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2025                               const char* reason_name, const char* reason_action) {
2026   LogTarget(Debug, deoptimization) lt;
2027   if (lt.is_enabled()) {
2028     LogStream ls(lt);
2029     bool is_osr = nm->is_osr_method();
2030     ls.print("cid=%4d %s%s%s level=%d",
2031              nm->compile_id(), (is_osr ? "osr" : "   "),
2032              (nm->is_aot() ? "aot " : ""),
2033              (nm->preloaded() ? "preload " : ""),
2034              nm->comp_level());
2035     ls.print(" %s", tm->name_and_sig_as_C_string());
2036     ls.print(" trap_bci=%d ", trap_bci);
2037     if (is_osr) {
2038       ls.print("osr_bci=%d ", nm->osr_entry_bci());
2039     }
2040     ls.print("%s ", reason_name);
2041     ls.print("%s ", reason_action);
2042     ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2043              pc, fr.pc() - nm->code_begin());
2044   }
2045 }
2046 
2047 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2048   HandleMark hm(current);
2049 
2050   // uncommon_trap() is called at the beginning of the uncommon trap
2051   // handler. Note this fact before we start generating temporary frames
2052   // that can confuse an asynchronous stack walker. This counter is
2053   // decremented at the end of unpack_frames().
2054 
2055   current->inc_in_deopt_handler();
2056 
2057 #if INCLUDE_JVMCI
2058   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2059   RegisterMap reg_map(current,
2060                       RegisterMap::UpdateMap::include,
2061                       RegisterMap::ProcessFrames::include,
2062                       RegisterMap::WalkContinuation::skip);
2063 #else
2064   RegisterMap reg_map(current,
2065                       RegisterMap::UpdateMap::skip,
2066                       RegisterMap::ProcessFrames::include,
2067                       RegisterMap::WalkContinuation::skip);

2103 #if INCLUDE_JVMCI
2104     jlong           speculation = current->pending_failed_speculation();
2105     if (nm->is_compiled_by_jvmci()) {
2106       nm->update_speculation(current);
2107     } else {
2108       assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2109     }
2110 
2111     if (trap_bci == SynchronizationEntryBCI) {
2112       trap_bci = 0;
2113       current->set_pending_monitorenter(true);
2114     }
2115 
2116     if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2117       current->set_pending_transfer_to_interpreter(true);
2118     }
2119 #endif
2120 
2121     Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
2122     // Record this event in the histogram.
2123     gather_statistics(nm, reason, action, trap_bc);
2124 
2125     // Ensure that we can record deopt. history:
2126     bool create_if_missing = ProfileTraps;
2127 
2128     methodHandle profiled_method;
2129 #if INCLUDE_JVMCI
2130     if (nm->is_compiled_by_jvmci()) {
2131       profiled_method = methodHandle(current, nm->method());
2132     } else {
2133       profiled_method = trap_method;
2134     }
2135 #else
2136     profiled_method = trap_method;
2137 #endif
2138 
2139     const char* nm_kind = nm->compile_kind();
2140     MethodData* trap_mdo =
2141       get_method_data(current, profiled_method, create_if_missing);
2142 
2143     { // Log Deoptimization event for JFR, UL and event system
2144       Method* tm = trap_method();
2145       const char* reason_name = trap_reason_name(reason);
2146       const char* reason_action = trap_action_name(action);
2147       intptr_t pc = p2i(fr.pc());
2148 
2149       JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)
2150       log_deopt(nm, tm, pc, fr, trap_bci, reason_name, reason_action);
2151       Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s %s",
2152                                 reason_name, reason_action, pc,
2153                                 tm->name_and_sig_as_C_string(), trap_bci, nm->compiler_name(), nm_kind);
2154     }
2155 
2156     // Print a bunch of diagnostics, if requested.
2157     if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2158       ResourceMark rm;
2159 
2160       // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2161       // We must do this already now, since we cannot acquire this lock while
2162       // holding the tty lock (lock ordering by rank).
2163       MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2164 
2165       ttyLocker ttyl;
2166 
2167       char buf[100];
2168       if (xtty != nullptr) {
2169         xtty->begin_head("uncommon_trap thread='%zu' %s",
2170                          os::current_thread_id(),
2171                          format_trap_request(buf, sizeof(buf), trap_request));
2172 #if INCLUDE_JVMCI
2173         if (speculation != 0) {

2204         ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2205         int dos = (pdata == nullptr)? 0: pdata->trap_state();
2206         if (dos != 0) {
2207           xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2208           if (trap_state_is_recompiled(dos)) {
2209             int recnt2 = trap_mdo->overflow_recompile_count();
2210             if (recnt2 != 0)
2211               xtty->print(" recompiles2='%d'", recnt2);
2212           }
2213         }
2214       }
2215       if (xtty != nullptr) {
2216         xtty->stamp();
2217         xtty->end_head();
2218       }
2219       if (TraceDeoptimization) {  // make noise on the tty
2220         stringStream st;
2221         st.print("UNCOMMON TRAP method=%s", trap_scope->method()->name_and_sig_as_C_string());
2222         st.print("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT JVMCI_ONLY(", debug_id=%d"),
2223                  trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin() JVMCI_ONLY(COMMA debug_id));
2224         st.print(" compiler=%s compile_id=%d kind=%s", nm->compiler_name(), nm->compile_id(), nm_kind);
2225 #if INCLUDE_JVMCI
2226         if (nm->is_compiled_by_jvmci()) {
2227           const char* installed_code_name = nm->jvmci_name();
2228           if (installed_code_name != nullptr) {
2229             st.print(" (JVMCI: installed code name=%s) ", installed_code_name);
2230           }
2231         }
2232 #endif
2233         st.print(" (@" INTPTR_FORMAT ") thread=%zu reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2234                    p2i(fr.pc()),
2235                    os::current_thread_id(),
2236                    trap_reason_name(reason),
2237                    trap_action_name(action),
2238                    unloaded_class_index
2239 #if INCLUDE_JVMCI
2240                    , debug_id
2241 #endif
2242                    );
2243         if (class_name != nullptr) {
2244           st.print(unresolved ? " unresolved class: " : " symbol: ");

2622   bool ignore_maybe_prior_recompile;
2623   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2624   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2625   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2626 
2627   // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2628   MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2629 
2630   query_update_method_data(trap_mdo, trap_bci,
2631                            (DeoptReason)reason,
2632                            update_total_counts,
2633 #if INCLUDE_JVMCI
2634                            false,
2635 #endif
2636                            nullptr,
2637                            ignore_this_trap_count,
2638                            ignore_maybe_prior_trap,
2639                            ignore_maybe_prior_recompile);
2640 }
2641 
2642 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2643   // Enable WXWrite: current function is called from methods compiled by C2 directly
2644   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2645 
2646   // Still in Java no safepoints
2647   {
2648     // This enters VM and may safepoint
2649     uncommon_trap_inner(current, trap_request);
2650   }
2651   HandleMark hm(current);
2652   return fetch_unroll_info_helper(current, exec_mode);
2653 PROF_END
2654 
2655 // Local derived constants.
2656 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2657 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2658 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2659 
2660 //---------------------------trap_state_reason---------------------------------
2661 Deoptimization::DeoptReason
2662 Deoptimization::trap_state_reason(int trap_state) {
2663   // This assert provides the link between the width of DataLayout::trap_bits
2664   // and the encoding of "recorded" reasons.  It ensures there are enough
2665   // bits to store all needed reasons in the per-BCI MDO profile.
2666   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2667   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2668   trap_state -= recompile_bit;
2669   if (trap_state == DS_REASON_MASK) {
2670     return Reason_many;
2671   } else {
2672     assert((int)Reason_none == 0, "state=0 => Reason_none");
2673     return (DeoptReason)trap_state;

2823   size_t len;
2824   if (unloaded_class_index < 0) {
2825     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2826                        reason, action
2827 #if INCLUDE_JVMCI
2828                        ,debug_id
2829 #endif
2830                        );
2831   } else {
2832     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2833                        reason, action, unloaded_class_index
2834 #if INCLUDE_JVMCI
2835                        ,debug_id
2836 #endif
2837                        );
2838   }
2839   return buf;
2840 }
2841 
2842 juint Deoptimization::_deoptimization_hist
2843         [1 + 4 + 5] // total + online + archived
2844         [Deoptimization::Reason_LIMIT]
2845     [1 + Deoptimization::Action_LIMIT]
2846         [Deoptimization::BC_CASE_LIMIT]
2847   = {0};
2848 
2849 enum {
2850   LSB_BITS = 8,
2851   LSB_MASK = right_n_bits(LSB_BITS)
2852 };
2853 
2854 static void update(juint* cases, Bytecodes::Code bc) {






2855   juint* bc_counter_addr = nullptr;
2856   juint  bc_counter      = 0;
2857   // Look for an unused counter, or an exact match to this BC.
2858   if (bc != Bytecodes::_illegal) {
2859     for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2860       juint* counter_addr = &cases[bc_case];
2861       juint  counter = *counter_addr;
2862       if ((counter == 0 && bc_counter_addr == nullptr)
2863           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2864         // this counter is either free or is already devoted to this BC
2865         bc_counter_addr = counter_addr;
2866         bc_counter = counter | bc;
2867       }
2868     }
2869   }
2870   if (bc_counter_addr == nullptr) {
2871     // Overflow, or no given bytecode.
2872     bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2873     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2874   }
2875   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2876 }
2877 
2878 
2879 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2880                                        Bytecodes::Code bc) {
2881   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2882   assert(action >= 0 && action < Action_LIMIT, "oob");
2883   _deoptimization_hist[0][Reason_none][0][0] += 1;  // total
2884   _deoptimization_hist[0][reason][0][0]      += 1;  // per-reason total
2885 
2886   update(_deoptimization_hist[0][reason][1+action], bc);
2887 
2888   uint lvl = nm->comp_level() + (nm->is_aot() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2889   _deoptimization_hist[lvl][Reason_none][0][0] += 1;  // total
2890   _deoptimization_hist[lvl][reason][0][0]      += 1;  // per-reason total
2891   update(_deoptimization_hist[lvl][reason][1+action], bc);
2892 }
2893 
2894 jint Deoptimization::total_deoptimization_count() {
2895   return _deoptimization_hist[0][Reason_none][0][0];
2896 }
2897 
2898 // Get the deopt count for a specific reason and a specific action. If either
2899 // one of 'reason' or 'action' is null, the method returns the sum of all
2900 // deoptimizations with the specific 'action' or 'reason' respectively.
2901 // If both arguments are null, the method returns the total deopt count.
2902 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2903   if (reason_str == nullptr && action_str == nullptr) {
2904     return total_deoptimization_count();
2905   }
2906   juint counter = 0;
2907   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2908     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2909       for (int action = 0; action < Action_LIMIT; action++) {
2910         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2911           juint* cases = _deoptimization_hist[0][reason][1+action];
2912           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2913             counter += cases[bc_case] >> LSB_BITS;
2914           }
2915         }
2916       }
2917     }
2918   }
2919   return counter;
2920 }
2921 
2922 void Deoptimization::print_statistics() {
2923   ttyLocker ttyl;
2924   if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2925   tty->print_cr("Deoptimization traps recorded:");
2926   print_statistics_on(tty);
2927   if (xtty != nullptr)  xtty->tail("statistics");
2928 }
2929 
2930 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2931   juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2932   juint account = total;
2933 #define PRINT_STAT_LINE(name, r) \
2934       st->print_cr("    %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2935   if (total > 0) {
2936     st->print("  %s: ", title);


2937     PRINT_STAT_LINE("total", total);
2938     // For each non-zero entry in the histogram, print the reason,
2939     // the action, and (if specifically known) the type of bytecode.
2940     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2941       for (int action = 0; action < Action_LIMIT; action++) {
2942         juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2943         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2944           juint counter = cases[bc_case];
2945           if (counter != 0) {

2946             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2947             const char* bc_name = "other";
2948             if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2949               // overwritten
2950             } else if (Bytecodes::is_defined(bc)) {
2951               bc_name = Bytecodes::name(bc);
2952             }
2953             juint r = counter >> LSB_BITS;
2954             st->print_cr("    %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2955                          trap_reason_name(reason), trap_action_name(action), bc_name,
2956                          r, (r * 100.0) / total);
2957             account -= r;
2958           }
2959         }
2960       }
2961     }
2962     if (account != 0) {
2963       PRINT_STAT_LINE("unaccounted", account);
2964     }
2965 #undef PRINT_STAT_LINE

2966   }
2967 }
2968 
2969 void Deoptimization::print_statistics_on(outputStream* st) {
2970 //  print_statistics_on("Total", 0, st);
2971   print_statistics_on("Tier1", 1, st);
2972   print_statistics_on("Tier2", 2, st);
2973   print_statistics_on("Tier3", 3, st);
2974   print_statistics_on("Tier4", 4, st);
2975 
2976   print_statistics_on("AOT Code Tier1", 5, st);
2977   print_statistics_on("AOT Code Tier2", 6, st);
2978   print_statistics_on("AOT Code Tier4", 8, st);
2979   print_statistics_on("AOT Code Tier5 (preloaded)", 9, st);
2980 }
2981 
2982 #define DO_COUNTERS(macro) \
2983   macro(Deoptimization, fetch_unroll_info)   \
2984   macro(Deoptimization, unpack_frames) \
2985   macro(Deoptimization, uncommon_trap_inner) \
2986   macro(Deoptimization, uncommon_trap)
2987 
2988 #define INIT_COUNTER(sub, name) \
2989   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2990   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2991 
2992 void Deoptimization::init_counters() {
2993   if (ProfileRuntimeCalls && UsePerfData) {
2994     EXCEPTION_MARK;
2995 
2996     DO_COUNTERS(INIT_COUNTER)
2997 
2998     if (HAS_PENDING_EXCEPTION) {
2999       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
3000     }
3001   }
3002 }
3003 #undef INIT_COUNTER
3004 
3005 #define PRINT_COUNTER(sub, name) { \
3006   jlong count = _perf_##sub##_##name##_count->get_value(); \
3007   if (count > 0) { \
3008     st->print_cr("  %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
3009                  _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
3010                  _perf_##sub##_##name##_timer->thread_counter_value_us(), \
3011                  count); \
3012   }}
3013 
3014 void Deoptimization::print_counters_on(outputStream* st) {
3015   if (ProfileRuntimeCalls && UsePerfData) {
3016     DO_COUNTERS(PRINT_COUNTER)
3017   } else {
3018     st->print_cr("  Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3019   }
3020 }
3021 
3022 #undef PRINT_COUNTER
3023 #undef DO_COUNTERS
3024 
3025 #else // COMPILER2_OR_JVMCI
3026 
3027 
3028 // Stubs for C1 only system.
3029 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
3030   return false;
3031 }
3032 
3033 const char* Deoptimization::trap_reason_name(int reason) {
3034   return "unknown";
3035 }
3036 
3037 jint Deoptimization::total_deoptimization_count() {
3038   return 0;
3039 }
3040 
3041 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
3042   return 0;
3043 }
3044 
3045 void Deoptimization::print_statistics() {
3046   // no output
3047 }
3048 
3049 void
3050 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
3051   // no update
3052 }
3053 
3054 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
3055   return 0;
3056 }
3057 
3058 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
3059                                        Bytecodes::Code bc) {
3060   // no update
3061 }
3062 
3063 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
3064                                               int trap_state) {
3065   jio_snprintf(buf, buflen, "#%d", trap_state);
3066   return buf;
3067 }
3068 
3069 void Deoptimization::init_counters() {
3070   // nothing to do
3071 }
3072 
3073 void Deoptimization::print_counters_on(outputStream* st) {
3074   // no output
3075 }
3076 
3077 void Deoptimization::print_statistics_on(outputStream* st) {
3078   // no output
3079 }
3080 
3081 #endif // COMPILER2_OR_JVMCI
< prev index next >