< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

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

  81 #include "runtime/safepointVerifiers.hpp"
  82 #include "runtime/sharedRuntime.hpp"
  83 #include "runtime/signature.hpp"
  84 #include "runtime/stackFrameStream.inline.hpp"
  85 #include "runtime/stackValue.hpp"
  86 #include "runtime/stackWatermarkSet.hpp"
  87 #include "runtime/stubRoutines.hpp"
  88 #include "runtime/synchronizer.inline.hpp"
  89 #include "runtime/threadSMR.hpp"
  90 #include "runtime/threadWXSetters.inline.hpp"
  91 #include "runtime/vframe.hpp"
  92 #include "runtime/vframe_hp.hpp"
  93 #include "runtime/vframeArray.hpp"
  94 #include "runtime/vmOperations.hpp"

  95 #include "utilities/checkedCast.hpp"
  96 #include "utilities/events.hpp"
  97 #include "utilities/growableArray.hpp"
  98 #include "utilities/macros.hpp"
  99 #include "utilities/preserveException.hpp"
 100 #include "utilities/xmlstream.hpp"
 101 #if INCLUDE_JFR
 102 #include "jfr/jfr.inline.hpp"
 103 #include "jfr/jfrEvents.hpp"
 104 #include "jfr/metadata/jfrSerializer.hpp"
 105 #endif
 106 
 107 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
 108 uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
 109 bool     DeoptimizationScope::_committing_in_progress = false;
 110 
 111 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
 112   DEBUG_ONLY(_deopted = false;)
 113 
 114   MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);

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

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

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






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

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



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

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

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

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

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

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

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
















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








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


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


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
























































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












2976 #endif // COMPILER2_OR_JVMCI

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

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

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

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



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

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

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

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

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

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






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


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

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

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