< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  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/lightweightSynchronizer.hpp"
  78 #include "runtime/lockStack.inline.hpp"
  79 #include "runtime/objectMonitor.inline.hpp"
  80 #include "runtime/osThread.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.inline.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 "utilities/checkedCast.hpp"
  96 #include "utilities/events.hpp"
  97 #include "utilities/growableArray.hpp"
  98 #include "utilities/macros.hpp"
  99 #include "utilities/preserveException.hpp"
 100 #include "utilities/xmlstream.hpp"
 101 #if INCLUDE_JFR
 102 #include "jfr/jfr.inline.hpp"
 103 #include "jfr/jfrEvents.hpp"
 104 #include "jfr/metadata/jfrSerializer.hpp"
 105 #endif
 106 
 107 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 108 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 109 bool     DeoptimizationScope::_committing_in_progress = false;
 110 
 111 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 112   DEBUG_ONLY(_deopted = false;)
 113 
 114   MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

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

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






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

1991     event.set_compileId(nm->compile_id());
1992     event.set_compiler(nm->compiler_type());
1993     event.set_method(method);
1994     event.set_lineNumber(method->line_number_from_bci(trap_bci));
1995     event.set_bci(trap_bci);
1996     event.set_instruction(instruction);
1997     event.set_reason(reason);
1998     event.set_action(action);
1999     event.commit();
2000   }
2001 }
2002 
2003 #endif // INCLUDE_JFR
2004 
2005 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2006                               const char* reason_name, const char* reason_action) {
2007   LogTarget(Debug, deoptimization) lt;
2008   if (lt.is_enabled()) {
2009     LogStream ls(lt);
2010     bool is_osr = nm->is_osr_method();
2011     ls.print("cid=%4d %s level=%d",
2012              nm->compile_id(), (is_osr ? "osr" : "   "), nm->comp_level());



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

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

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

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

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

2800   size_t len;
2801   if (unloaded_class_index < 0) {
2802     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2803                        reason, action
2804 #if INCLUDE_JVMCI
2805                        ,debug_id
2806 #endif
2807                        );
2808   } else {
2809     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2810                        reason, action, unloaded_class_index
2811 #if INCLUDE_JVMCI
2812                        ,debug_id
2813 #endif
2814                        );
2815   }
2816   return buf;
2817 }
2818 
2819 juint Deoptimization::_deoptimization_hist

2820         [Deoptimization::Reason_LIMIT]
2821     [1 + Deoptimization::Action_LIMIT]
2822         [Deoptimization::BC_CASE_LIMIT]
2823   = {0};
2824 
2825 enum {
2826   LSB_BITS = 8,
2827   LSB_MASK = right_n_bits(LSB_BITS)
2828 };
2829 
2830 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2831                                        Bytecodes::Code bc) {
2832   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2833   assert(action >= 0 && action < Action_LIMIT, "oob");
2834   _deoptimization_hist[Reason_none][0][0] += 1;  // total
2835   _deoptimization_hist[reason][0][0]      += 1;  // per-reason total
2836   juint* cases = _deoptimization_hist[reason][1+action];
2837   juint* bc_counter_addr = nullptr;
2838   juint  bc_counter      = 0;
2839   // Look for an unused counter, or an exact match to this BC.
2840   if (bc != Bytecodes::_illegal) {
2841     for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2842       juint* counter_addr = &cases[bc_case];
2843       juint  counter = *counter_addr;
2844       if ((counter == 0 && bc_counter_addr == nullptr)
2845           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2846         // this counter is either free or is already devoted to this BC
2847         bc_counter_addr = counter_addr;
2848         bc_counter = counter | bc;
2849       }
2850     }
2851   }
2852   if (bc_counter_addr == nullptr) {
2853     // Overflow, or no given bytecode.
2854     bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2855     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2856   }
2857   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2858 }
2859 
















2860 jint Deoptimization::total_deoptimization_count() {
2861   return _deoptimization_hist[Reason_none][0][0];
2862 }
2863 
2864 // Get the deopt count for a specific reason and a specific action. If either
2865 // one of 'reason' or 'action' is null, the method returns the sum of all
2866 // deoptimizations with the specific 'action' or 'reason' respectively.
2867 // If both arguments are null, the method returns the total deopt count.
2868 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2869   if (reason_str == nullptr && action_str == nullptr) {
2870     return total_deoptimization_count();
2871   }
2872   juint counter = 0;
2873   for (int reason = 0; reason < Reason_LIMIT; reason++) {
2874     if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2875       for (int action = 0; action < Action_LIMIT; action++) {
2876         if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2877           juint* cases = _deoptimization_hist[reason][1+action];
2878           for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2879             counter += cases[bc_case] >> LSB_BITS;
2880           }
2881         }
2882       }
2883     }
2884   }
2885   return counter;
2886 }
2887 
2888 void Deoptimization::print_statistics() {
2889   juint total = total_deoptimization_count();








2890   juint account = total;
2891   if (total != 0) {
2892     ttyLocker ttyl;
2893     if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
2894     tty->print_cr("Deoptimization traps recorded:");
2895     #define PRINT_STAT_LINE(name, r) \
2896       tty->print_cr("  %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2897     PRINT_STAT_LINE("total", total);
2898     // For each non-zero entry in the histogram, print the reason,
2899     // the action, and (if specifically known) the type of bytecode.
2900     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2901       for (int action = 0; action < Action_LIMIT; action++) {
2902         juint* cases = _deoptimization_hist[reason][1+action];
2903         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2904           juint counter = cases[bc_case];
2905           if (counter != 0) {
2906             char name[1*K];
2907             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2908             os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2909                     trap_reason_name(reason),
2910                     trap_action_name(action),
2911                     Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");


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


2914             account -= r;
2915           }
2916         }
2917       }
2918     }
2919     if (account != 0) {
2920       PRINT_STAT_LINE("unaccounted", account);
2921     }
2922     #undef PRINT_STAT_LINE
2923     if (xtty != nullptr)  xtty->tail("statistics");
2924   }
2925 }
2926 
























































2927 #else // COMPILER2_OR_JVMCI
2928 
2929 
2930 // Stubs for C1 only system.
2931 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2932   return false;
2933 }
2934 
2935 const char* Deoptimization::trap_reason_name(int reason) {
2936   return "unknown";
2937 }
2938 
2939 jint Deoptimization::total_deoptimization_count() {
2940   return 0;
2941 }
2942 
2943 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2944   return 0;
2945 }
2946 
2947 void Deoptimization::print_statistics() {
2948   // no output
2949 }
2950 
2951 void
2952 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2953   // no update
2954 }
2955 
2956 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2957   return 0;
2958 }
2959 
2960 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2961                                        Bytecodes::Code bc) {
2962   // no update
2963 }
2964 
2965 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2966                                               int trap_state) {
2967   jio_snprintf(buf, buflen, "#%d", trap_state);
2968   return buf;
2969 }
2970 












2971 #endif // COMPILER2_OR_JVMCI

  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/lightweightSynchronizer.hpp"
  78 #include "runtime/lockStack.inline.hpp"
  79 #include "runtime/objectMonitor.inline.hpp"
  80 #include "runtime/osThread.hpp"
  81 #include "runtime/perfData.inline.hpp"
  82 #include "runtime/safepointVerifiers.hpp"
  83 #include "runtime/sharedRuntime.hpp"
  84 #include "runtime/signature.hpp"
  85 #include "runtime/stackFrameStream.inline.hpp"
  86 #include "runtime/stackValue.hpp"
  87 #include "runtime/stackWatermarkSet.hpp"
  88 #include "runtime/stubRoutines.hpp"
  89 #include "runtime/synchronizer.inline.hpp"
  90 #include "runtime/threadSMR.hpp"
  91 #include "runtime/threadWXSetters.inline.hpp"
  92 #include "runtime/vframe.hpp"
  93 #include "runtime/vframe_hp.hpp"
  94 #include "runtime/vframeArray.hpp"
  95 #include "runtime/vmOperations.hpp"
  96 #include "services/management.hpp"
  97 #include "utilities/checkedCast.hpp"
  98 #include "utilities/events.hpp"
  99 #include "utilities/growableArray.hpp"
 100 #include "utilities/macros.hpp"
 101 #include "utilities/preserveException.hpp"
 102 #include "utilities/xmlstream.hpp"
 103 #if INCLUDE_JFR
 104 #include "jfr/jfr.inline.hpp"
 105 #include "jfr/jfrEvents.hpp"
 106 #include "jfr/metadata/jfrSerializer.hpp"
 107 #endif
 108 
 109 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 110 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 111 bool     DeoptimizationScope::_committing_in_progress = false;
 112 
 113 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 114   DEBUG_ONLY(_deopted = false;)
 115 
 116   MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

 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_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, 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.

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



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 

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

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

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

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

2809   size_t len;
2810   if (unloaded_class_index < 0) {
2811     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2812                        reason, action
2813 #if INCLUDE_JVMCI
2814                        ,debug_id
2815 #endif
2816                        );
2817   } else {
2818     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2819                        reason, action, unloaded_class_index
2820 #if INCLUDE_JVMCI
2821                        ,debug_id
2822 #endif
2823                        );
2824   }
2825   return buf;
2826 }
2827 
2828 juint Deoptimization::_deoptimization_hist
2829         [1 + 4 + 5] // total + online + archived
2830         [Deoptimization::Reason_LIMIT]
2831     [1 + Deoptimization::Action_LIMIT]
2832         [Deoptimization::BC_CASE_LIMIT]
2833   = {0};
2834 
2835 enum {
2836   LSB_BITS = 8,
2837   LSB_MASK = right_n_bits(LSB_BITS)
2838 };
2839 
2840 static void update(juint* cases, Bytecodes::Code bc) {






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


2923     PRINT_STAT_LINE("total", total);
2924     // For each non-zero entry in the histogram, print the reason,
2925     // the action, and (if specifically known) the type of bytecode.
2926     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2927       for (int action = 0; action < Action_LIMIT; action++) {
2928         juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2929         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2930           juint counter = cases[bc_case];
2931           if (counter != 0) {

2932             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2933             const char* bc_name = "other";
2934             if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2935               // overwritten
2936             } else if (Bytecodes::is_defined(bc)) {
2937               bc_name = Bytecodes::name(bc);
2938             }
2939             juint r = counter >> LSB_BITS;
2940             st->print_cr("    %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2941                          trap_reason_name(reason), trap_action_name(action), bc_name,
2942                          r, (r * 100.0) / total);
2943             account -= r;
2944           }
2945         }
2946       }
2947     }
2948     if (account != 0) {
2949       PRINT_STAT_LINE("unaccounted", account);
2950     }
2951 #undef PRINT_STAT_LINE

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