< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  63 #include "prims/methodHandles.hpp"
  64 #include "prims/vectorSupport.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/basicLock.inline.hpp"
  67 #include "runtime/continuation.hpp"
  68 #include "runtime/continuationEntry.inline.hpp"
  69 #include "runtime/deoptimization.hpp"
  70 #include "runtime/escapeBarrier.hpp"
  71 #include "runtime/fieldDescriptor.hpp"
  72 #include "runtime/fieldDescriptor.inline.hpp"
  73 #include "runtime/frame.inline.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/interfaceSupport.inline.hpp"
  76 #include "runtime/javaThread.hpp"
  77 #include "runtime/jniHandles.inline.hpp"
  78 #include "runtime/keepStackGCProcessed.hpp"
  79 #include "runtime/lightweightSynchronizer.hpp"
  80 #include "runtime/lockStack.inline.hpp"
  81 #include "runtime/objectMonitor.inline.hpp"
  82 #include "runtime/osThread.hpp"

  83 #include "runtime/safepointVerifiers.hpp"
  84 #include "runtime/sharedRuntime.hpp"
  85 #include "runtime/signature.hpp"
  86 #include "runtime/stackFrameStream.inline.hpp"
  87 #include "runtime/stackValue.hpp"
  88 #include "runtime/stackWatermarkSet.hpp"
  89 #include "runtime/stubRoutines.hpp"
  90 #include "runtime/synchronizer.inline.hpp"
  91 #include "runtime/threadSMR.hpp"
  92 #include "runtime/threadWXSetters.inline.hpp"
  93 #include "runtime/vframe.hpp"
  94 #include "runtime/vframeArray.hpp"
  95 #include "runtime/vframe_hp.hpp"
  96 #include "runtime/vmOperations.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(Deoptimization::UnrollBlock*, 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) {

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

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

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

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
















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








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


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
























































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












2985 #endif // COMPILER2_OR_JVMCI

  63 #include "prims/methodHandles.hpp"
  64 #include "prims/vectorSupport.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/basicLock.inline.hpp"
  67 #include "runtime/continuation.hpp"
  68 #include "runtime/continuationEntry.inline.hpp"
  69 #include "runtime/deoptimization.hpp"
  70 #include "runtime/escapeBarrier.hpp"
  71 #include "runtime/fieldDescriptor.hpp"
  72 #include "runtime/fieldDescriptor.inline.hpp"
  73 #include "runtime/frame.inline.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/interfaceSupport.inline.hpp"
  76 #include "runtime/javaThread.hpp"
  77 #include "runtime/jniHandles.inline.hpp"
  78 #include "runtime/keepStackGCProcessed.hpp"
  79 #include "runtime/lightweightSynchronizer.hpp"
  80 #include "runtime/lockStack.inline.hpp"
  81 #include "runtime/objectMonitor.inline.hpp"
  82 #include "runtime/osThread.hpp"
  83 #include "runtime/perfData.inline.hpp"
  84 #include "runtime/safepointVerifiers.hpp"
  85 #include "runtime/sharedRuntime.hpp"
  86 #include "runtime/signature.hpp"
  87 #include "runtime/stackFrameStream.inline.hpp"
  88 #include "runtime/stackValue.hpp"
  89 #include "runtime/stackWatermarkSet.hpp"
  90 #include "runtime/stubRoutines.hpp"
  91 #include "runtime/synchronizer.inline.hpp"
  92 #include "runtime/threadSMR.hpp"
  93 #include "runtime/threadWXSetters.inline.hpp"
  94 #include "runtime/vframe.hpp"
  95 #include "runtime/vframeArray.hpp"
  96 #include "runtime/vframe_hp.hpp"
  97 #include "runtime/vmOperations.hpp"
  98 #include "services/management.hpp"
  99 #include "utilities/checkedCast.hpp"
 100 #include "utilities/events.hpp"
 101 #include "utilities/growableArray.hpp"
 102 #include "utilities/macros.hpp"
 103 #include "utilities/preserveException.hpp"
 104 #include "utilities/xmlstream.hpp"
 105 #if INCLUDE_JFR
 106 #include "jfr/jfr.inline.hpp"
 107 #include "jfr/jfrEvents.hpp"
 108 #include "jfr/metadata/jfrSerializer.hpp"
 109 #endif
 110 
 111 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 112 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 113 bool     DeoptimizationScope::_committing_in_progress = false;
 114 
 115 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 116   DEBUG_ONLY(_deopted = false;)
 117 
 118   MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

 855     case Bytecodes::_goto_w:
 856     case Bytecodes::_athrow:
 857     case Bytecodes::_areturn:
 858     case Bytecodes::_dreturn:
 859     case Bytecodes::_freturn:
 860     case Bytecodes::_ireturn:
 861     case Bytecodes::_lreturn:
 862     case Bytecodes::_jsr:
 863     case Bytecodes::_ret:
 864     case Bytecodes::_return:
 865     case Bytecodes::_lookupswitch:
 866     case Bytecodes::_tableswitch:
 867       return false;
 868     default:
 869       return true;
 870   }
 871 }
 872 #endif
 873 
 874 // Return BasicType of value being returned
 875 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, 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.

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: ");

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

2818   size_t len;
2819   if (unloaded_class_index < 0) {
2820     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2821                        reason, action
2822 #if INCLUDE_JVMCI
2823                        ,debug_id
2824 #endif
2825                        );
2826   } else {
2827     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2828                        reason, action, unloaded_class_index
2829 #if INCLUDE_JVMCI
2830                        ,debug_id
2831 #endif
2832                        );
2833   }
2834   return buf;
2835 }
2836 
2837 juint Deoptimization::_deoptimization_hist
2838         [1 + 4 + 5] // total + online + archived
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 static void update(juint* cases, Bytecodes::Code bc) {






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


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

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

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