< 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) {

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

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






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

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



2030     ls.print(" %s", tm->name_and_sig_as_C_string());
2031     ls.print(" trap_bci=%d ", trap_bci);
2032     if (is_osr) {
2033       ls.print("osr_bci=%d ", nm->osr_entry_bci());
2034     }
2035     ls.print("%s ", reason_name);
2036     ls.print("%s ", reason_action);



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

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

2134     MethodData* trap_mdo =
2135       get_method_data(current, profiled_method, create_if_missing);
2136 
2137     { // Log Deoptimization event for JFR, UL and event system
2138       Method* tm = trap_method();
2139       const char* reason_name = trap_reason_name(reason);
2140       const char* reason_action = trap_action_name(action);
2141       intptr_t pc = p2i(fr.pc());
2142 
2143       JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)
2144       log_deopt(nm, tm, pc, fr, trap_bci, reason_name, reason_action);
2145       Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s",



















2146                                 reason_name, reason_action, pc,
2147                                 tm->name_and_sig_as_C_string(), trap_bci, nm->compiler_name());
2148     }
2149 
2150     // Print a bunch of diagnostics, if requested.
2151     if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2152       ResourceMark rm;
2153 
2154       // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2155       // We must do this already now, since we cannot acquire this lock while
2156       // holding the tty lock (lock ordering by rank).
2157       MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2158 
2159       ttyLocker ttyl;
2160 
2161       char buf[100];
2162       if (xtty != nullptr) {
2163         xtty->begin_head("uncommon_trap thread='%zu' %s",
2164                          os::current_thread_id(),
2165                          format_trap_request(buf, sizeof(buf), trap_request));
2166 #if INCLUDE_JVMCI
2167         if (speculation != 0) {

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

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

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

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
















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








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


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


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


































2941   }
2942 }





















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












2988 #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) {

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

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



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

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

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

2229         ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2230         int dos = (pdata == nullptr)? 0: pdata->trap_state();
2231         if (dos != 0) {
2232           xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2233           if (trap_state_is_recompiled(dos)) {
2234             int recnt2 = trap_mdo->overflow_recompile_count();
2235             if (recnt2 != 0)
2236               xtty->print(" recompiles2='%d'", recnt2);
2237           }
2238         }
2239       }
2240       if (xtty != nullptr) {
2241         xtty->stamp();
2242         xtty->end_head();
2243       }
2244       if (TraceDeoptimization) {  // make noise on the tty
2245         stringStream st;
2246         st.print("UNCOMMON TRAP method=%s", trap_scope->method()->name_and_sig_as_C_string());
2247         st.print("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT JVMCI_ONLY(", debug_id=%d"),
2248                  trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin() JVMCI_ONLY(COMMA debug_id));
2249         st.print(" compiler=%s compile_id=%d kind=%s", nm->compiler_name(), nm->compile_id(), nm_kind);
2250 #if INCLUDE_JVMCI
2251         if (nm->is_compiled_by_jvmci()) {
2252           const char* installed_code_name = nm->jvmci_name();
2253           if (installed_code_name != nullptr) {
2254             st.print(" (JVMCI: installed code name=%s) ", installed_code_name);
2255           }
2256         }
2257 #endif
2258         st.print(" (@" INTPTR_FORMAT ") thread=%zu reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2259                    p2i(fr.pc()),
2260                    os::current_thread_id(),
2261                    trap_reason_name(reason),
2262                    trap_action_name(action),
2263                    unloaded_class_index
2264 #if INCLUDE_JVMCI
2265                    , debug_id
2266 #endif
2267                    );
2268         if (class_name != nullptr) {
2269           st.print(unresolved ? " unresolved class: " : " symbol: ");

2647   bool ignore_maybe_prior_recompile;
2648   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2649   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2650   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2651 
2652   // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2653   MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2654 
2655   query_update_method_data(trap_mdo, trap_bci,
2656                            (DeoptReason)reason,
2657                            update_total_counts,
2658 #if INCLUDE_JVMCI
2659                            false,
2660 #endif
2661                            nullptr,
2662                            ignore_this_trap_count,
2663                            ignore_maybe_prior_trap,
2664                            ignore_maybe_prior_recompile);
2665 }
2666 
2667 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2668   // Enable WXWrite: current function is called from methods compiled by C2 directly
2669   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2670 
2671   // Still in Java no safepoints
2672   {
2673     // This enters VM and may safepoint
2674     uncommon_trap_inner(current, trap_request);
2675   }
2676   HandleMark hm(current);
2677   return fetch_unroll_info_helper(current, exec_mode);
2678 PROF_END
2679 
2680 // Local derived constants.
2681 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2682 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2683 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2684 
2685 //---------------------------trap_state_reason---------------------------------
2686 Deoptimization::DeoptReason
2687 Deoptimization::trap_state_reason(int trap_state) {
2688   // This assert provides the link between the width of DataLayout::trap_bits
2689   // and the encoding of "recorded" reasons.  It ensures there are enough
2690   // bits to store all needed reasons in the per-BCI MDO profile.
2691   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2692   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2693   trap_state -= recompile_bit;
2694   if (trap_state == DS_REASON_MASK) {
2695     return Reason_many;
2696   } else {
2697     assert((int)Reason_none == 0, "state=0 => Reason_none");
2698     return (DeoptReason)trap_state;

2848   size_t len;
2849   if (unloaded_class_index < 0) {
2850     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2851                        reason, action
2852 #if INCLUDE_JVMCI
2853                        ,debug_id
2854 #endif
2855                        );
2856   } else {
2857     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2858                        reason, action, unloaded_class_index
2859 #if INCLUDE_JVMCI
2860                        ,debug_id
2861 #endif
2862                        );
2863   }
2864   return buf;
2865 }
2866 
2867 juint Deoptimization::_deoptimization_hist
2868         [1 + 4 + 5] // total + online + archived
2869         [Deoptimization::Reason_LIMIT]
2870     [1 + Deoptimization::Action_LIMIT]
2871         [Deoptimization::BC_CASE_LIMIT]
2872   = {0};
2873 
2874 enum {
2875   LSB_BITS = 8,
2876   LSB_MASK = right_n_bits(LSB_BITS)
2877 };
2878 
2879 static void update(juint* cases, Bytecodes::Code bc) {






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


2962     PRINT_STAT_LINE("total", total);
2963     // For each non-zero entry in the histogram, print the reason,
2964     // the action, and (if specifically known) the type of bytecode.
2965     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2966       for (int action = 0; action < Action_LIMIT; action++) {
2967         juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2968         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2969           juint counter = cases[bc_case];
2970           if (counter != 0) {

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