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

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

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






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

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

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

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

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

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

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

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
















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








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


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


2919             account -= r;
2920           }
2921         }
2922       }
2923     }
2924     if (account != 0) {
2925       PRINT_STAT_LINE("unaccounted", account);
2926     }
2927     #undef PRINT_STAT_LINE
2928     if (xtty != nullptr)  xtty->tail("statistics");
2929   }
2930 }
2931 
























































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












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

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

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



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

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

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

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

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

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






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


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

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

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