< 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/atomic.hpp"
  65 #include "runtime/basicLock.inline.hpp"
  66 #include "runtime/continuation.hpp"
  67 #include "runtime/continuationEntry.inline.hpp"
  68 #include "runtime/deoptimization.hpp"
  69 #include "runtime/escapeBarrier.hpp"
  70 #include "runtime/fieldDescriptor.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.

1769     if (monitors != nullptr) {
1770       // Unlock in reverse order starting from most nested monitor.
1771       for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1772         BasicObjectLock* src = monitors->at(j);
1773         if (src->obj() != nullptr) {
1774           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1775         }
1776       }
1777       array->element(i)->free_monitors();
1778 #ifdef ASSERT
1779       array->element(i)->set_removed_monitors();
1780 #endif
1781     }
1782   }
1783 }
1784 #endif
1785 
1786 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1787   assert(fr.can_be_deoptimized(), "checking frame type");
1788 
1789   gather_statistics(reason, Action_none, Bytecodes::_illegal);






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

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

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

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

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

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

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

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
















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








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


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


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
























































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












2990 #endif // COMPILER2_OR_JVMCI

  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/basicLock.inline.hpp"
  66 #include "runtime/continuation.hpp"
  67 #include "runtime/continuationEntry.inline.hpp"
  68 #include "runtime/deoptimization.hpp"
  69 #include "runtime/escapeBarrier.hpp"
  70 #include "runtime/fieldDescriptor.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.

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



1800     ttyLocker ttyl;
1801     xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1802     nm->log_identity(xtty);
1803     xtty->end_head();
1804     for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1805       xtty->begin_elem("jvms bci='%d'", sd->bci());
1806       xtty->method(sd->method());
1807       xtty->end_elem();
1808       if (sd->is_top())  break;
1809     }
1810     xtty->tail("deoptimized");
1811   }
1812 
1813   Continuation::notify_deopt(thread, fr.sp());
1814 
1815   // Patch the compiled method so that when execution returns to it we will
1816   // deopt the execution state and return to the interpreter.
1817   fr.deoptimize(thread);
1818 }
1819 

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

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

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

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

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






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


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

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

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