60 #include "prims/jvmtiExport.hpp"
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/lockStack.inline.hpp"
78 #include "runtime/objectMonitor.inline.hpp"
79 #include "runtime/osThread.hpp"
80 #include "runtime/safepointVerifiers.hpp"
81 #include "runtime/sharedRuntime.hpp"
82 #include "runtime/signature.hpp"
83 #include "runtime/stackFrameStream.inline.hpp"
84 #include "runtime/stackValue.hpp"
85 #include "runtime/stackWatermarkSet.hpp"
86 #include "runtime/stubRoutines.hpp"
87 #include "runtime/synchronizer.hpp"
88 #include "runtime/threadSMR.hpp"
89 #include "runtime/threadWXSetters.inline.hpp"
90 #include "runtime/vframe.hpp"
91 #include "runtime/vframe_hp.hpp"
92 #include "runtime/vframeArray.hpp"
93 #include "runtime/vmOperations.hpp"
94 #include "utilities/checkedCast.hpp"
95 #include "utilities/events.hpp"
96 #include "utilities/growableArray.hpp"
97 #include "utilities/macros.hpp"
98 #include "utilities/preserveException.hpp"
99 #include "utilities/xmlstream.hpp"
100 #if INCLUDE_JFR
101 #include "jfr/jfr.inline.hpp"
102 #include "jfr/jfrEvents.hpp"
103 #include "jfr/metadata/jfrSerializer.hpp"
104 #endif
105
106 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
107 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
108 bool DeoptimizationScope::_committing_in_progress = false;
109
110 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
111 DEBUG_ONLY(_deopted = false;)
112
113 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) {
853 case Bytecodes::_athrow:
854 case Bytecodes::_areturn:
855 case Bytecodes::_dreturn:
856 case Bytecodes::_freturn:
857 case Bytecodes::_ireturn:
858 case Bytecodes::_lreturn:
859 case Bytecodes::_jsr:
860 case Bytecodes::_ret:
861 case Bytecodes::_return:
862 case Bytecodes::_lookupswitch:
863 case Bytecodes::_tableswitch:
864 return false;
865 default:
866 return true;
867 }
868 }
869 #endif
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.
1759 if (monitors != nullptr) {
1760 // Unlock in reverse order starting from most nested monitor.
1761 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1762 BasicObjectLock* src = monitors->at(j);
1763 if (src->obj() != nullptr) {
1764 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1765 }
1766 }
1767 array->element(i)->free_monitors();
1768 #ifdef ASSERT
1769 array->element(i)->set_removed_monitors();
1770 #endif
1771 }
1772 }
1773 }
1774 #endif
1775
1776 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1777 assert(fr.can_be_deoptimized(), "checking frame type");
1778
1779 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1780
1781 if (LogCompilation && xtty != nullptr) {
1782 nmethod* nm = fr.cb()->as_nmethod_or_null();
1783 assert(nm != nullptr, "only compiled methods can deopt");
1784
1785 ttyLocker ttyl;
1786 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1787 nm->log_identity(xtty);
1788 xtty->end_head();
1789 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1790 xtty->begin_elem("jvms bci='%d'", sd->bci());
1791 xtty->method(sd->method());
1792 xtty->end_elem();
1793 if (sd->is_top()) break;
1794 }
1795 xtty->tail("deoptimized");
1796 }
1797
1798 Continuation::notify_deopt(thread, fr.sp());
1799
1800 // Patch the compiled method so that when execution returns to it we will
1801 // deopt the execution state and return to the interpreter.
1802 fr.deoptimize(thread);
1803 }
1804
2004 if (!serializers_registered) {
2005 register_serializers();
2006 serializers_registered = true;
2007 }
2008 EventDeoptimization event;
2009 event.set_compileId(nm->compile_id());
2010 event.set_compiler(nm->compiler_type());
2011 event.set_method(method);
2012 event.set_lineNumber(method->line_number_from_bci(trap_bci));
2013 event.set_bci(trap_bci);
2014 event.set_instruction(instruction);
2015 event.set_reason(reason);
2016 event.set_action(action);
2017 event.commit();
2018 }
2019 }
2020
2021 #endif // INCLUDE_JFR
2022
2023 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2024 const char* reason_name, const char* reason_action) {
2025 LogTarget(Debug, deoptimization) lt;
2026 if (lt.is_enabled()) {
2027 LogStream ls(lt);
2028 bool is_osr = nm->is_osr_method();
2029 ls.print("cid=%4d %s level=%d",
2030 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
2031 ls.print(" %s", tm->name_and_sig_as_C_string());
2032 ls.print(" trap_bci=%d ", trap_bci);
2033 if (is_osr) {
2034 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2035 }
2036 ls.print("%s ", reason_name);
2037 ls.print("%s ", reason_action);
2038 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2039 pc, fr.pc() - nm->code_begin());
2040 }
2041 }
2042
2043 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2044 HandleMark hm(current);
2045
2046 // uncommon_trap() is called at the beginning of the uncommon trap
2047 // handler. Note this fact before we start generating temporary frames
2048 // that can confuse an asynchronous stack walker. This counter is
2049 // decremented at the end of unpack_frames().
2050
2051 current->inc_in_deopt_handler();
2052
2053 #if INCLUDE_JVMCI
2054 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2055 RegisterMap reg_map(current,
2056 RegisterMap::UpdateMap::include,
2057 RegisterMap::ProcessFrames::include,
2058 RegisterMap::WalkContinuation::skip);
2059 #else
2060 RegisterMap reg_map(current,
2061 RegisterMap::UpdateMap::skip,
2062 RegisterMap::ProcessFrames::include,
2063 RegisterMap::WalkContinuation::skip);
2099 #if INCLUDE_JVMCI
2100 jlong speculation = current->pending_failed_speculation();
2101 if (nm->is_compiled_by_jvmci()) {
2102 nm->update_speculation(current);
2103 } else {
2104 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2105 }
2106
2107 if (trap_bci == SynchronizationEntryBCI) {
2108 trap_bci = 0;
2109 current->set_pending_monitorenter(true);
2110 }
2111
2112 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2113 current->set_pending_transfer_to_interpreter(true);
2114 }
2115 #endif
2116
2117 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2118 // Record this event in the histogram.
2119 gather_statistics(reason, action, trap_bc);
2120
2121 // Ensure that we can record deopt. history:
2122 bool create_if_missing = ProfileTraps;
2123
2124 methodHandle profiled_method;
2125 #if INCLUDE_JVMCI
2126 if (nm->is_compiled_by_jvmci()) {
2127 profiled_method = methodHandle(current, nm->method());
2128 } else {
2129 profiled_method = trap_method;
2130 }
2131 #else
2132 profiled_method = trap_method;
2133 #endif
2134
2135 MethodData* trap_mdo =
2136 get_method_data(current, profiled_method, create_if_missing);
2137
2138 { // Log Deoptimization event for JFR, UL and event system
2139 Method* tm = trap_method();
2140 const char* reason_name = trap_reason_name(reason);
2141 const char* reason_action = trap_action_name(action);
2142 intptr_t pc = p2i(fr.pc());
2143
2144 JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)
2145 log_deopt(nm, tm, pc, fr, trap_bci, reason_name, reason_action);
2146 Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s",
2147 reason_name, reason_action, pc,
2148 tm->name_and_sig_as_C_string(), trap_bci, nm->compiler_name());
2149 }
2150
2151 // Print a bunch of diagnostics, if requested.
2152 if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2153 ResourceMark rm;
2154
2155 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2156 // We must do this already now, since we cannot acquire this lock while
2157 // holding the tty lock (lock ordering by rank).
2158 ConditionalMutexLocker ml((trap_mdo != nullptr) ? trap_mdo->extra_data_lock() : nullptr,
2159 (trap_mdo != nullptr),
2160 Mutex::_no_safepoint_check_flag);
2161
2162 ttyLocker ttyl;
2163
2164 char buf[100];
2165 if (xtty != nullptr) {
2166 xtty->begin_head("uncommon_trap thread='%zu' %s",
2167 os::current_thread_id(),
2168 format_trap_request(buf, sizeof(buf), trap_request));
2201 ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2202 int dos = (pdata == nullptr)? 0: pdata->trap_state();
2203 if (dos != 0) {
2204 xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2205 if (trap_state_is_recompiled(dos)) {
2206 int recnt2 = trap_mdo->overflow_recompile_count();
2207 if (recnt2 != 0)
2208 xtty->print(" recompiles2='%d'", recnt2);
2209 }
2210 }
2211 }
2212 if (xtty != nullptr) {
2213 xtty->stamp();
2214 xtty->end_head();
2215 }
2216 if (TraceDeoptimization) { // make noise on the tty
2217 stringStream st;
2218 st.print("UNCOMMON TRAP method=%s", trap_scope->method()->name_and_sig_as_C_string());
2219 st.print(" bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT JVMCI_ONLY(", debug_id=%d"),
2220 trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin() JVMCI_ONLY(COMMA debug_id));
2221 st.print(" compiler=%s compile_id=%d", nm->compiler_name(), nm->compile_id());
2222 #if INCLUDE_JVMCI
2223 if (nm->is_compiled_by_jvmci()) {
2224 const char* installed_code_name = nm->jvmci_name();
2225 if (installed_code_name != nullptr) {
2226 st.print(" (JVMCI: installed code name=%s) ", installed_code_name);
2227 }
2228 }
2229 #endif
2230 st.print(" (@" INTPTR_FORMAT ") thread=%zu reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2231 p2i(fr.pc()),
2232 os::current_thread_id(),
2233 trap_reason_name(reason),
2234 trap_action_name(action),
2235 unloaded_class_index
2236 #if INCLUDE_JVMCI
2237 , debug_id
2238 #endif
2239 );
2240 if (class_name != nullptr) {
2241 st.print(unresolved ? " unresolved class: " : " symbol: ");
2619 bool ignore_maybe_prior_recompile;
2620 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2621 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2622 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2623
2624 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2625 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2626
2627 query_update_method_data(trap_mdo, trap_bci,
2628 (DeoptReason)reason,
2629 update_total_counts,
2630 #if INCLUDE_JVMCI
2631 false,
2632 #endif
2633 nullptr,
2634 ignore_this_trap_count,
2635 ignore_maybe_prior_trap,
2636 ignore_maybe_prior_recompile);
2637 }
2638
2639 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2640 // Enable WXWrite: current function is called from methods compiled by C2 directly
2641 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2642
2643 // Still in Java no safepoints
2644 {
2645 // This enters VM and may safepoint
2646 uncommon_trap_inner(current, trap_request);
2647 }
2648 HandleMark hm(current);
2649 return fetch_unroll_info_helper(current, exec_mode);
2650 }
2651
2652 // Local derived constants.
2653 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2654 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2655 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2656
2657 //---------------------------trap_state_reason---------------------------------
2658 Deoptimization::DeoptReason
2659 Deoptimization::trap_state_reason(int trap_state) {
2660 // This assert provides the link between the width of DataLayout::trap_bits
2661 // and the encoding of "recorded" reasons. It ensures there are enough
2662 // bits to store all needed reasons in the per-BCI MDO profile.
2663 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2664 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2665 trap_state -= recompile_bit;
2666 if (trap_state == DS_REASON_MASK) {
2667 return Reason_many;
2668 } else {
2669 assert((int)Reason_none == 0, "state=0 => Reason_none");
2670 return (DeoptReason)trap_state;
2820 size_t len;
2821 if (unloaded_class_index < 0) {
2822 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2823 reason, action
2824 #if INCLUDE_JVMCI
2825 ,debug_id
2826 #endif
2827 );
2828 } else {
2829 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2830 reason, action, unloaded_class_index
2831 #if INCLUDE_JVMCI
2832 ,debug_id
2833 #endif
2834 );
2835 }
2836 return buf;
2837 }
2838
2839 juint Deoptimization::_deoptimization_hist
2840 [Deoptimization::Reason_LIMIT]
2841 [1 + Deoptimization::Action_LIMIT]
2842 [Deoptimization::BC_CASE_LIMIT]
2843 = {0};
2844
2845 enum {
2846 LSB_BITS = 8,
2847 LSB_MASK = right_n_bits(LSB_BITS)
2848 };
2849
2850 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2851 Bytecodes::Code bc) {
2852 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2853 assert(action >= 0 && action < Action_LIMIT, "oob");
2854 _deoptimization_hist[Reason_none][0][0] += 1; // total
2855 _deoptimization_hist[reason][0][0] += 1; // per-reason total
2856 juint* cases = _deoptimization_hist[reason][1+action];
2857 juint* bc_counter_addr = nullptr;
2858 juint bc_counter = 0;
2859 // Look for an unused counter, or an exact match to this BC.
2860 if (bc != Bytecodes::_illegal) {
2861 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2862 juint* counter_addr = &cases[bc_case];
2863 juint counter = *counter_addr;
2864 if ((counter == 0 && bc_counter_addr == nullptr)
2865 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2866 // this counter is either free or is already devoted to this BC
2867 bc_counter_addr = counter_addr;
2868 bc_counter = counter | bc;
2869 }
2870 }
2871 }
2872 if (bc_counter_addr == nullptr) {
2873 // Overflow, or no given bytecode.
2874 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2875 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2876 }
2877 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2878 }
2879
2880 jint Deoptimization::total_deoptimization_count() {
2881 return _deoptimization_hist[Reason_none][0][0];
2882 }
2883
2884 // Get the deopt count for a specific reason and a specific action. If either
2885 // one of 'reason' or 'action' is null, the method returns the sum of all
2886 // deoptimizations with the specific 'action' or 'reason' respectively.
2887 // If both arguments are null, the method returns the total deopt count.
2888 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2889 if (reason_str == nullptr && action_str == nullptr) {
2890 return total_deoptimization_count();
2891 }
2892 juint counter = 0;
2893 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2894 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2895 for (int action = 0; action < Action_LIMIT; action++) {
2896 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2897 juint* cases = _deoptimization_hist[reason][1+action];
2898 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2899 counter += cases[bc_case] >> LSB_BITS;
2900 }
2901 }
2902 }
2903 }
2904 }
2905 return counter;
2906 }
2907
2908 void Deoptimization::print_statistics() {
2909 juint total = total_deoptimization_count();
2910 juint account = total;
2911 if (total != 0) {
2912 ttyLocker ttyl;
2913 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2914 tty->print_cr("Deoptimization traps recorded:");
2915 #define PRINT_STAT_LINE(name, r) \
2916 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2917 PRINT_STAT_LINE("total", total);
2918 // For each non-zero entry in the histogram, print the reason,
2919 // the action, and (if specifically known) the type of bytecode.
2920 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2921 for (int action = 0; action < Action_LIMIT; action++) {
2922 juint* cases = _deoptimization_hist[reason][1+action];
2923 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2924 juint counter = cases[bc_case];
2925 if (counter != 0) {
2926 char name[1*K];
2927 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2928 os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2929 trap_reason_name(reason),
2930 trap_action_name(action),
2931 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2932 juint r = counter >> LSB_BITS;
2933 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2934 account -= r;
2935 }
2936 }
2937 }
2938 }
2939 if (account != 0) {
2940 PRINT_STAT_LINE("unaccounted", account);
2941 }
2942 #undef PRINT_STAT_LINE
2943 if (xtty != nullptr) xtty->tail("statistics");
2944 }
2945 }
2946
2947 #else // COMPILER2_OR_JVMCI
2948
2949
2950 // Stubs for C1 only system.
2951 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2952 return false;
2953 }
2954
2955 const char* Deoptimization::trap_reason_name(int reason) {
2956 return "unknown";
2957 }
2958
2959 jint Deoptimization::total_deoptimization_count() {
2960 return 0;
2961 }
2962
2963 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2964 return 0;
2965 }
2966
2967 void Deoptimization::print_statistics() {
2968 // no output
2969 }
2970
2971 void
2972 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2973 // no update
2974 }
2975
2976 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2977 return 0;
2978 }
2979
2980 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2981 Bytecodes::Code bc) {
2982 // no update
2983 }
2984
2985 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2986 int trap_state) {
2987 jio_snprintf(buf, buflen, "#%d", trap_state);
2988 return buf;
2989 }
2990
2991 #endif // COMPILER2_OR_JVMCI
|
60 #include "prims/jvmtiExport.hpp"
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/lockStack.inline.hpp"
78 #include "runtime/objectMonitor.inline.hpp"
79 #include "runtime/osThread.hpp"
80 #include "runtime/perfData.inline.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.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 "services/management.hpp"
96 #include "utilities/checkedCast.hpp"
97 #include "utilities/events.hpp"
98 #include "utilities/growableArray.hpp"
99 #include "utilities/macros.hpp"
100 #include "utilities/preserveException.hpp"
101 #include "utilities/xmlstream.hpp"
102 #if INCLUDE_JFR
103 #include "jfr/jfr.inline.hpp"
104 #include "jfr/jfrEvents.hpp"
105 #include "jfr/metadata/jfrSerializer.hpp"
106 #endif
107
108 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
109 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
110 bool DeoptimizationScope::_committing_in_progress = false;
111
112 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
113 DEBUG_ONLY(_deopted = false;)
114
115 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) {
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_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.
1761 if (monitors != nullptr) {
1762 // Unlock in reverse order starting from most nested monitor.
1763 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1764 BasicObjectLock* src = monitors->at(j);
1765 if (src->obj() != nullptr) {
1766 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1767 }
1768 }
1769 array->element(i)->free_monitors();
1770 #ifdef ASSERT
1771 array->element(i)->set_removed_monitors();
1772 #endif
1773 }
1774 }
1775 }
1776 #endif
1777
1778 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1779 assert(fr.can_be_deoptimized(), "checking frame type");
1780
1781 nmethod* nm = fr.cb()->as_nmethod_or_null();
1782 assert(nm != nullptr, "only compiled methods can deopt");
1783 DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1784 ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1785 Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1786 : cur_sd->method()->java_code_at(cur_sd->bci()));
1787 gather_statistics(nm, reason, action, bc);
1788
1789 if (LogCompilation && xtty != nullptr) {
1790 ttyLocker ttyl;
1791 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1792 nm->log_identity(xtty);
1793 xtty->end_head();
1794 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1795 xtty->begin_elem("jvms bci='%d'", sd->bci());
1796 xtty->method(sd->method());
1797 xtty->end_elem();
1798 if (sd->is_top()) break;
1799 }
1800 xtty->tail("deoptimized");
1801 }
1802
1803 Continuation::notify_deopt(thread, fr.sp());
1804
1805 // Patch the compiled method so that when execution returns to it we will
1806 // deopt the execution state and return to the interpreter.
1807 fr.deoptimize(thread);
1808 }
1809
2009 if (!serializers_registered) {
2010 register_serializers();
2011 serializers_registered = true;
2012 }
2013 EventDeoptimization event;
2014 event.set_compileId(nm->compile_id());
2015 event.set_compiler(nm->compiler_type());
2016 event.set_method(method);
2017 event.set_lineNumber(method->line_number_from_bci(trap_bci));
2018 event.set_bci(trap_bci);
2019 event.set_instruction(instruction);
2020 event.set_reason(reason);
2021 event.set_action(action);
2022 event.commit();
2023 }
2024 }
2025
2026 #endif // INCLUDE_JFR
2027
2028 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2029 const char* reason_name, const char* reason_action, const char* class_name) {
2030 LogTarget(Debug, deoptimization) lt;
2031 if (lt.is_enabled()) {
2032 LogStream ls(lt);
2033 bool is_osr = nm->is_osr_method();
2034 ls.print("cid=%4d %s%s%s level=%d",
2035 nm->compile_id(), (is_osr ? "osr" : " "),
2036 (nm->is_aot() ? "aot " : ""),
2037 (nm->preloaded() ? "preload " : ""),
2038 nm->comp_level());
2039 ls.print(" %s", tm->name_and_sig_as_C_string());
2040 ls.print(" trap_bci=%d ", trap_bci);
2041 if (is_osr) {
2042 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2043 }
2044 ls.print("%s ", reason_name);
2045 ls.print("%s ", reason_action);
2046 if (class_name != nullptr) {
2047 ls.print("%s ", class_name);
2048 }
2049 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2050 pc, fr.pc() - nm->code_begin());
2051 }
2052 }
2053
2054 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2055 HandleMark hm(current);
2056
2057 // uncommon_trap() is called at the beginning of the uncommon trap
2058 // handler. Note this fact before we start generating temporary frames
2059 // that can confuse an asynchronous stack walker. This counter is
2060 // decremented at the end of unpack_frames().
2061
2062 current->inc_in_deopt_handler();
2063
2064 #if INCLUDE_JVMCI
2065 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2066 RegisterMap reg_map(current,
2067 RegisterMap::UpdateMap::include,
2068 RegisterMap::ProcessFrames::include,
2069 RegisterMap::WalkContinuation::skip);
2070 #else
2071 RegisterMap reg_map(current,
2072 RegisterMap::UpdateMap::skip,
2073 RegisterMap::ProcessFrames::include,
2074 RegisterMap::WalkContinuation::skip);
2110 #if INCLUDE_JVMCI
2111 jlong speculation = current->pending_failed_speculation();
2112 if (nm->is_compiled_by_jvmci()) {
2113 nm->update_speculation(current);
2114 } else {
2115 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2116 }
2117
2118 if (trap_bci == SynchronizationEntryBCI) {
2119 trap_bci = 0;
2120 current->set_pending_monitorenter(true);
2121 }
2122
2123 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2124 current->set_pending_transfer_to_interpreter(true);
2125 }
2126 #endif
2127
2128 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2129 // Record this event in the histogram.
2130 gather_statistics(nm, reason, action, trap_bc);
2131
2132 // Ensure that we can record deopt. history:
2133 bool create_if_missing = ProfileTraps;
2134
2135 methodHandle profiled_method;
2136 #if INCLUDE_JVMCI
2137 if (nm->is_compiled_by_jvmci()) {
2138 profiled_method = methodHandle(current, nm->method());
2139 } else {
2140 profiled_method = trap_method;
2141 }
2142 #else
2143 profiled_method = trap_method;
2144 #endif
2145
2146 const char* nm_kind = nm->compile_kind();
2147 MethodData* trap_mdo =
2148 get_method_data(current, profiled_method, create_if_missing);
2149
2150 { // Log Deoptimization event for JFR, UL and event system
2151 Method* tm = trap_method();
2152 const char* reason_name = trap_reason_name(reason);
2153 const char* reason_action = trap_action_name(action);
2154 intptr_t pc = p2i(fr.pc());
2155
2156 JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)
2157
2158 ResourceMark rm;
2159
2160 const char* class_name = nullptr;
2161 stringStream st;
2162 if (unloaded_class_index >= 0) {
2163 constantPoolHandle constants (current, tm->constants());
2164 Symbol* class_symbol = nullptr;
2165 if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
2166 class_symbol = constants->klass_name_at(unloaded_class_index);
2167 } else if (constants->tag_at(unloaded_class_index).is_symbol()) {
2168 class_symbol = constants->symbol_at(unloaded_class_index);
2169 }
2170 if (class_symbol != nullptr) {
2171 class_symbol->print_symbol_on(&st);
2172 class_name = st.freeze();
2173 }
2174 }
2175
2176 log_deopt(nm, tm, pc, fr, trap_bci, reason_name, reason_action, class_name);
2177 Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s %s %s",
2178 reason_name, reason_action, pc,
2179 tm->name_and_sig_as_C_string(), trap_bci, (class_name != nullptr ? class_name : ""), nm->compiler_name(), nm_kind);
2180 }
2181
2182 // Print a bunch of diagnostics, if requested.
2183 if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2184 ResourceMark rm;
2185
2186 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2187 // We must do this already now, since we cannot acquire this lock while
2188 // holding the tty lock (lock ordering by rank).
2189 ConditionalMutexLocker ml((trap_mdo != nullptr) ? trap_mdo->extra_data_lock() : nullptr,
2190 (trap_mdo != nullptr),
2191 Mutex::_no_safepoint_check_flag);
2192
2193 ttyLocker ttyl;
2194
2195 char buf[100];
2196 if (xtty != nullptr) {
2197 xtty->begin_head("uncommon_trap thread='%zu' %s",
2198 os::current_thread_id(),
2199 format_trap_request(buf, sizeof(buf), trap_request));
2232 ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2233 int dos = (pdata == nullptr)? 0: pdata->trap_state();
2234 if (dos != 0) {
2235 xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2236 if (trap_state_is_recompiled(dos)) {
2237 int recnt2 = trap_mdo->overflow_recompile_count();
2238 if (recnt2 != 0)
2239 xtty->print(" recompiles2='%d'", recnt2);
2240 }
2241 }
2242 }
2243 if (xtty != nullptr) {
2244 xtty->stamp();
2245 xtty->end_head();
2246 }
2247 if (TraceDeoptimization) { // make noise on the tty
2248 stringStream st;
2249 st.print("UNCOMMON TRAP method=%s", trap_scope->method()->name_and_sig_as_C_string());
2250 st.print(" bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT JVMCI_ONLY(", debug_id=%d"),
2251 trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin() JVMCI_ONLY(COMMA debug_id));
2252 st.print(" compiler=%s compile_id=%d kind=%s", nm->compiler_name(), nm->compile_id(), nm_kind);
2253 #if INCLUDE_JVMCI
2254 if (nm->is_compiled_by_jvmci()) {
2255 const char* installed_code_name = nm->jvmci_name();
2256 if (installed_code_name != nullptr) {
2257 st.print(" (JVMCI: installed code name=%s) ", installed_code_name);
2258 }
2259 }
2260 #endif
2261 st.print(" (@" INTPTR_FORMAT ") thread=%zu reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2262 p2i(fr.pc()),
2263 os::current_thread_id(),
2264 trap_reason_name(reason),
2265 trap_action_name(action),
2266 unloaded_class_index
2267 #if INCLUDE_JVMCI
2268 , debug_id
2269 #endif
2270 );
2271 if (class_name != nullptr) {
2272 st.print(unresolved ? " unresolved class: " : " symbol: ");
2650 bool ignore_maybe_prior_recompile;
2651 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2652 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2653 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2654
2655 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2656 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2657
2658 query_update_method_data(trap_mdo, trap_bci,
2659 (DeoptReason)reason,
2660 update_total_counts,
2661 #if INCLUDE_JVMCI
2662 false,
2663 #endif
2664 nullptr,
2665 ignore_this_trap_count,
2666 ignore_maybe_prior_trap,
2667 ignore_maybe_prior_recompile);
2668 }
2669
2670 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2671 // Enable WXWrite: current function is called from methods compiled by C2 directly
2672 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2673
2674 // Still in Java no safepoints
2675 {
2676 // This enters VM and may safepoint
2677 uncommon_trap_inner(current, trap_request);
2678 }
2679 HandleMark hm(current);
2680 return fetch_unroll_info_helper(current, exec_mode);
2681 PROF_END
2682
2683 // Local derived constants.
2684 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2685 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2686 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2687
2688 //---------------------------trap_state_reason---------------------------------
2689 Deoptimization::DeoptReason
2690 Deoptimization::trap_state_reason(int trap_state) {
2691 // This assert provides the link between the width of DataLayout::trap_bits
2692 // and the encoding of "recorded" reasons. It ensures there are enough
2693 // bits to store all needed reasons in the per-BCI MDO profile.
2694 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2695 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2696 trap_state -= recompile_bit;
2697 if (trap_state == DS_REASON_MASK) {
2698 return Reason_many;
2699 } else {
2700 assert((int)Reason_none == 0, "state=0 => Reason_none");
2701 return (DeoptReason)trap_state;
2851 size_t len;
2852 if (unloaded_class_index < 0) {
2853 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2854 reason, action
2855 #if INCLUDE_JVMCI
2856 ,debug_id
2857 #endif
2858 );
2859 } else {
2860 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2861 reason, action, unloaded_class_index
2862 #if INCLUDE_JVMCI
2863 ,debug_id
2864 #endif
2865 );
2866 }
2867 return buf;
2868 }
2869
2870 juint Deoptimization::_deoptimization_hist
2871 [1 + 4 + 5] // total + online + archived
2872 [Deoptimization::Reason_LIMIT]
2873 [1 + Deoptimization::Action_LIMIT]
2874 [Deoptimization::BC_CASE_LIMIT]
2875 = {0};
2876
2877 enum {
2878 LSB_BITS = 8,
2879 LSB_MASK = right_n_bits(LSB_BITS)
2880 };
2881
2882 static void update(juint* cases, Bytecodes::Code bc) {
2883 juint* bc_counter_addr = nullptr;
2884 juint bc_counter = 0;
2885 // Look for an unused counter, or an exact match to this BC.
2886 if (bc != Bytecodes::_illegal) {
2887 for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2888 juint* counter_addr = &cases[bc_case];
2889 juint counter = *counter_addr;
2890 if ((counter == 0 && bc_counter_addr == nullptr)
2891 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2892 // this counter is either free or is already devoted to this BC
2893 bc_counter_addr = counter_addr;
2894 bc_counter = counter | bc;
2895 }
2896 }
2897 }
2898 if (bc_counter_addr == nullptr) {
2899 // Overflow, or no given bytecode.
2900 bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2901 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2902 }
2903 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2904 }
2905
2906
2907 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2908 Bytecodes::Code bc) {
2909 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2910 assert(action >= 0 && action < Action_LIMIT, "oob");
2911 _deoptimization_hist[0][Reason_none][0][0] += 1; // total
2912 _deoptimization_hist[0][reason][0][0] += 1; // per-reason total
2913
2914 update(_deoptimization_hist[0][reason][1+action], bc);
2915
2916 uint lvl = nm->comp_level() + (nm->is_aot() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2917 _deoptimization_hist[lvl][Reason_none][0][0] += 1; // total
2918 _deoptimization_hist[lvl][reason][0][0] += 1; // per-reason total
2919 update(_deoptimization_hist[lvl][reason][1+action], bc);
2920 }
2921
2922 jint Deoptimization::total_deoptimization_count() {
2923 return _deoptimization_hist[0][Reason_none][0][0];
2924 }
2925
2926 // Get the deopt count for a specific reason and a specific action. If either
2927 // one of 'reason' or 'action' is null, the method returns the sum of all
2928 // deoptimizations with the specific 'action' or 'reason' respectively.
2929 // If both arguments are null, the method returns the total deopt count.
2930 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2931 if (reason_str == nullptr && action_str == nullptr) {
2932 return total_deoptimization_count();
2933 }
2934 juint counter = 0;
2935 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2936 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2937 for (int action = 0; action < Action_LIMIT; action++) {
2938 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2939 juint* cases = _deoptimization_hist[0][reason][1+action];
2940 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2941 counter += cases[bc_case] >> LSB_BITS;
2942 }
2943 }
2944 }
2945 }
2946 }
2947 return counter;
2948 }
2949
2950 void Deoptimization::print_statistics() {
2951 ttyLocker ttyl;
2952 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2953 tty->print_cr("Deoptimization traps recorded:");
2954 print_statistics_on(tty);
2955 if (xtty != nullptr) xtty->tail("statistics");
2956 }
2957
2958 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2959 juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2960 juint account = total;
2961 #define PRINT_STAT_LINE(name, r) \
2962 st->print_cr(" %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2963 if (total > 0) {
2964 st->print(" %s: ", title);
2965 PRINT_STAT_LINE("total", total);
2966 // For each non-zero entry in the histogram, print the reason,
2967 // the action, and (if specifically known) the type of bytecode.
2968 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2969 for (int action = 0; action < Action_LIMIT; action++) {
2970 juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2971 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2972 juint counter = cases[bc_case];
2973 if (counter != 0) {
2974 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2975 const char* bc_name = "other";
2976 if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2977 // overwritten
2978 } else if (Bytecodes::is_defined(bc)) {
2979 bc_name = Bytecodes::name(bc);
2980 }
2981 juint r = counter >> LSB_BITS;
2982 st->print_cr(" %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2983 trap_reason_name(reason), trap_action_name(action), bc_name,
2984 r, (r * 100.0) / total);
2985 account -= r;
2986 }
2987 }
2988 }
2989 }
2990 if (account != 0) {
2991 PRINT_STAT_LINE("unaccounted", account);
2992 }
2993 #undef PRINT_STAT_LINE
2994 }
2995 }
2996
2997 void Deoptimization::print_statistics_on(outputStream* st) {
2998 // print_statistics_on("Total", 0, st);
2999 print_statistics_on("Tier1", 1, st);
3000 print_statistics_on("Tier2", 2, st);
3001 print_statistics_on("Tier3", 3, st);
3002 print_statistics_on("Tier4", 4, st);
3003
3004 print_statistics_on("AOT Code Tier1", 5, st);
3005 print_statistics_on("AOT Code Tier2", 6, st);
3006 print_statistics_on("AOT Code Tier4", 8, st);
3007 print_statistics_on("AOT Code Tier5 (preloaded)", 9, st);
3008 }
3009
3010 #define DO_COUNTERS(macro) \
3011 macro(Deoptimization, fetch_unroll_info) \
3012 macro(Deoptimization, unpack_frames) \
3013 macro(Deoptimization, uncommon_trap_inner) \
3014 macro(Deoptimization, uncommon_trap)
3015
3016 #define INIT_COUNTER(sub, name) \
3017 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
3018 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
3019
3020 void Deoptimization::init_counters() {
3021 if (ProfileRuntimeCalls && UsePerfData) {
3022 EXCEPTION_MARK;
3023
3024 DO_COUNTERS(INIT_COUNTER)
3025
3026 if (HAS_PENDING_EXCEPTION) {
3027 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
3028 }
3029 }
3030 }
3031 #undef INIT_COUNTER
3032
3033 #define PRINT_COUNTER(sub, name) { \
3034 jlong count = _perf_##sub##_##name##_count->get_value(); \
3035 if (count > 0) { \
3036 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
3037 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
3038 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
3039 count); \
3040 }}
3041
3042 void Deoptimization::print_counters_on(outputStream* st) {
3043 if (ProfileRuntimeCalls && UsePerfData) {
3044 DO_COUNTERS(PRINT_COUNTER)
3045 } else {
3046 st->print_cr(" Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3047 }
3048 }
3049
3050 #undef PRINT_COUNTER
3051 #undef DO_COUNTERS
3052
3053 #else // COMPILER2_OR_JVMCI
3054
3055
3056 // Stubs for C1 only system.
3057 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
3058 return false;
3059 }
3060
3061 const char* Deoptimization::trap_reason_name(int reason) {
3062 return "unknown";
3063 }
3064
3065 jint Deoptimization::total_deoptimization_count() {
3066 return 0;
3067 }
3068
3069 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
3070 return 0;
3071 }
3072
3073 void Deoptimization::print_statistics() {
3074 // no output
3075 }
3076
3077 void
3078 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
3079 // no update
3080 }
3081
3082 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
3083 return 0;
3084 }
3085
3086 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
3087 Bytecodes::Code bc) {
3088 // no update
3089 }
3090
3091 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
3092 int trap_state) {
3093 jio_snprintf(buf, buflen, "#%d", trap_state);
3094 return buf;
3095 }
3096
3097 void Deoptimization::init_counters() {
3098 // nothing to do
3099 }
3100
3101 void Deoptimization::print_counters_on(outputStream* st) {
3102 // no output
3103 }
3104
3105 void Deoptimization::print_statistics_on(outputStream* st) {
3106 // no output
3107 }
3108
3109 #endif // COMPILER2_OR_JVMCI
|