63 #include "prims/methodHandles.hpp"
64 #include "prims/vectorSupport.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/basicLock.inline.hpp"
67 #include "runtime/continuation.hpp"
68 #include "runtime/continuationEntry.inline.hpp"
69 #include "runtime/deoptimization.hpp"
70 #include "runtime/escapeBarrier.hpp"
71 #include "runtime/fieldDescriptor.hpp"
72 #include "runtime/fieldDescriptor.inline.hpp"
73 #include "runtime/frame.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/interfaceSupport.inline.hpp"
76 #include "runtime/javaThread.hpp"
77 #include "runtime/jniHandles.inline.hpp"
78 #include "runtime/keepStackGCProcessed.hpp"
79 #include "runtime/lightweightSynchronizer.hpp"
80 #include "runtime/lockStack.inline.hpp"
81 #include "runtime/objectMonitor.inline.hpp"
82 #include "runtime/osThread.hpp"
83 #include "runtime/safepointVerifiers.hpp"
84 #include "runtime/sharedRuntime.hpp"
85 #include "runtime/signature.hpp"
86 #include "runtime/stackFrameStream.inline.hpp"
87 #include "runtime/stackValue.hpp"
88 #include "runtime/stackWatermarkSet.hpp"
89 #include "runtime/stubRoutines.hpp"
90 #include "runtime/synchronizer.inline.hpp"
91 #include "runtime/threadSMR.hpp"
92 #include "runtime/threadWXSetters.inline.hpp"
93 #include "runtime/vframe.hpp"
94 #include "runtime/vframeArray.hpp"
95 #include "runtime/vframe_hp.hpp"
96 #include "runtime/vmOperations.hpp"
97 #include "utilities/checkedCast.hpp"
98 #include "utilities/events.hpp"
99 #include "utilities/growableArray.hpp"
100 #include "utilities/macros.hpp"
101 #include "utilities/preserveException.hpp"
102 #include "utilities/xmlstream.hpp"
103 #if INCLUDE_JFR
104 #include "jfr/jfr.inline.hpp"
105 #include "jfr/jfrEvents.hpp"
106 #include "jfr/metadata/jfrSerializer.hpp"
107 #endif
108
109 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
110 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
111 bool DeoptimizationScope::_committing_in_progress = false;
112
113 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
114 DEBUG_ONLY(_deopted = false;)
115
116 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
265 return checked_cast<int>(result);
266 }
267
268 void Deoptimization::UnrollBlock::print() {
269 ResourceMark rm;
270 stringStream st;
271 st.print_cr("UnrollBlock");
272 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
273 st.print( " frame_sizes: ");
274 for (int index = 0; index < number_of_frames(); index++) {
275 st.print("%zd ", frame_sizes()[index]);
276 }
277 st.cr();
278 tty->print_raw(st.freeze());
279 }
280
281 // In order to make fetch_unroll_info work properly with escape
282 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
283 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
284 // which is called from the method fetch_unroll_info_helper below.
285 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
286 // fetch_unroll_info() is called at the beginning of the deoptimization
287 // handler. Note this fact before we start generating temporary frames
288 // that can confuse an asynchronous stack walker. This counter is
289 // decremented at the end of unpack_frames().
290 current->inc_in_deopt_handler();
291
292 if (exec_mode == Unpack_exception) {
293 // When we get here, a callee has thrown an exception into a deoptimized
294 // frame. That throw might have deferred stack watermark checking until
295 // after unwinding. So we deal with such deferred requests here.
296 StackWatermarkSet::after_unwind(current);
297 }
298
299 return fetch_unroll_info_helper(current, exec_mode);
300 JRT_END
301
302 #if COMPILER2_OR_JVMCI
303 // print information about reallocated objects
304 static void print_objects(JavaThread* deoptee_thread,
305 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
853 case Bytecodes::_goto_w:
854 case Bytecodes::_athrow:
855 case Bytecodes::_areturn:
856 case Bytecodes::_dreturn:
857 case Bytecodes::_freturn:
858 case Bytecodes::_ireturn:
859 case Bytecodes::_lreturn:
860 case Bytecodes::_jsr:
861 case Bytecodes::_ret:
862 case Bytecodes::_return:
863 case Bytecodes::_lookupswitch:
864 case Bytecodes::_tableswitch:
865 return false;
866 default:
867 return true;
868 }
869 }
870 #endif
871
872 // Return BasicType of value being returned
873 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
874 assert(thread == JavaThread::current(), "pre-condition");
875
876 // We are already active in the special DeoptResourceMark any ResourceObj's we
877 // allocate will be freed at the end of the routine.
878
879 // JRT_LEAF methods don't normally allocate handles and there is a
880 // NoHandleMark to enforce that. It is actually safe to use Handles
881 // in a JRT_LEAF method, and sometimes desirable, but to do so we
882 // must use ResetNoHandleMark to bypass the NoHandleMark, and
883 // then use a HandleMark to ensure any Handles we do create are
884 // cleaned up in this scope.
885 ResetNoHandleMark rnhm;
886 HandleMark hm(thread);
887
888 frame stub_frame = thread->last_frame();
889
890 Continuation::notify_deopt(thread, stub_frame.sp());
891
892 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
893 // must point to the vframeArray for the unpack frame.
1769 if (monitors != nullptr) {
1770 // Unlock in reverse order starting from most nested monitor.
1771 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1772 BasicObjectLock* src = monitors->at(j);
1773 if (src->obj() != nullptr) {
1774 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1775 }
1776 }
1777 array->element(i)->free_monitors();
1778 #ifdef ASSERT
1779 array->element(i)->set_removed_monitors();
1780 #endif
1781 }
1782 }
1783 }
1784 #endif
1785
1786 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1787 assert(fr.can_be_deoptimized(), "checking frame type");
1788
1789 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1790
1791 if (LogCompilation && xtty != nullptr) {
1792 nmethod* nm = fr.cb()->as_nmethod_or_null();
1793 assert(nm != nullptr, "only compiled methods can deopt");
1794
1795 ttyLocker ttyl;
1796 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1797 nm->log_identity(xtty);
1798 xtty->end_head();
1799 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1800 xtty->begin_elem("jvms bci='%d'", sd->bci());
1801 xtty->method(sd->method());
1802 xtty->end_elem();
1803 if (sd->is_top()) break;
1804 }
1805 xtty->tail("deoptimized");
1806 }
1807
1808 Continuation::notify_deopt(thread, fr.sp());
1809
1810 // Patch the compiled method so that when execution returns to it we will
1811 // deopt the execution state and return to the interpreter.
1812 fr.deoptimize(thread);
1813 }
1814
2010 event.set_compileId(nm->compile_id());
2011 event.set_compiler(nm->compiler_type());
2012 event.set_method(method);
2013 event.set_lineNumber(method->line_number_from_bci(trap_bci));
2014 event.set_bci(trap_bci);
2015 event.set_instruction(instruction);
2016 event.set_reason(reason);
2017 event.set_action(action);
2018 event.commit();
2019 }
2020 }
2021
2022 #endif // INCLUDE_JFR
2023
2024 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2025 const char* reason_name, const char* reason_action) {
2026 LogTarget(Debug, deoptimization) lt;
2027 if (lt.is_enabled()) {
2028 LogStream ls(lt);
2029 bool is_osr = nm->is_osr_method();
2030 ls.print("cid=%4d %s level=%d",
2031 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
2032 ls.print(" %s", tm->name_and_sig_as_C_string());
2033 ls.print(" trap_bci=%d ", trap_bci);
2034 if (is_osr) {
2035 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2036 }
2037 ls.print("%s ", reason_name);
2038 ls.print("%s ", reason_action);
2039 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2040 pc, fr.pc() - nm->code_begin());
2041 }
2042 }
2043
2044 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2045 HandleMark hm(current);
2046
2047 // uncommon_trap() is called at the beginning of the uncommon trap
2048 // handler. Note this fact before we start generating temporary frames
2049 // that can confuse an asynchronous stack walker. This counter is
2050 // decremented at the end of unpack_frames().
2051
2052 current->inc_in_deopt_handler();
2053
2054 #if INCLUDE_JVMCI
2055 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2056 RegisterMap reg_map(current,
2057 RegisterMap::UpdateMap::include,
2058 RegisterMap::ProcessFrames::include,
2059 RegisterMap::WalkContinuation::skip);
2060 #else
2061 RegisterMap reg_map(current,
2062 RegisterMap::UpdateMap::skip,
2063 RegisterMap::ProcessFrames::include,
2064 RegisterMap::WalkContinuation::skip);
2100 #if INCLUDE_JVMCI
2101 jlong speculation = current->pending_failed_speculation();
2102 if (nm->is_compiled_by_jvmci()) {
2103 nm->update_speculation(current);
2104 } else {
2105 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2106 }
2107
2108 if (trap_bci == SynchronizationEntryBCI) {
2109 trap_bci = 0;
2110 current->set_pending_monitorenter(true);
2111 }
2112
2113 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2114 current->set_pending_transfer_to_interpreter(true);
2115 }
2116 #endif
2117
2118 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2119 // Record this event in the histogram.
2120 gather_statistics(reason, action, trap_bc);
2121
2122 // Ensure that we can record deopt. history:
2123 bool create_if_missing = ProfileTraps;
2124
2125 methodHandle profiled_method;
2126 #if INCLUDE_JVMCI
2127 if (nm->is_compiled_by_jvmci()) {
2128 profiled_method = methodHandle(current, nm->method());
2129 } else {
2130 profiled_method = trap_method;
2131 }
2132 #else
2133 profiled_method = trap_method;
2134 #endif
2135
2136 MethodData* trap_mdo =
2137 get_method_data(current, profiled_method, create_if_missing);
2138
2139 { // Log Deoptimization event for JFR, UL and event system
2140 Method* tm = trap_method();
2611 bool ignore_maybe_prior_recompile;
2612 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2613 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2614 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2615
2616 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2617 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2618
2619 query_update_method_data(trap_mdo, trap_bci,
2620 (DeoptReason)reason,
2621 update_total_counts,
2622 #if INCLUDE_JVMCI
2623 false,
2624 #endif
2625 nullptr,
2626 ignore_this_trap_count,
2627 ignore_maybe_prior_trap,
2628 ignore_maybe_prior_recompile);
2629 }
2630
2631 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2632 // Enable WXWrite: current function is called from methods compiled by C2 directly
2633 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2634
2635 // Still in Java no safepoints
2636 {
2637 // This enters VM and may safepoint
2638 uncommon_trap_inner(current, trap_request);
2639 }
2640 HandleMark hm(current);
2641 return fetch_unroll_info_helper(current, exec_mode);
2642 }
2643
2644 // Local derived constants.
2645 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2646 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2647 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2648
2649 //---------------------------trap_state_reason---------------------------------
2650 Deoptimization::DeoptReason
2651 Deoptimization::trap_state_reason(int trap_state) {
2652 // This assert provides the link between the width of DataLayout::trap_bits
2653 // and the encoding of "recorded" reasons. It ensures there are enough
2654 // bits to store all needed reasons in the per-BCI MDO profile.
2655 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2656 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2657 trap_state -= recompile_bit;
2658 if (trap_state == DS_REASON_MASK) {
2659 return Reason_many;
2660 } else {
2661 assert((int)Reason_none == 0, "state=0 => Reason_none");
2662 return (DeoptReason)trap_state;
2812 size_t len;
2813 if (unloaded_class_index < 0) {
2814 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2815 reason, action
2816 #if INCLUDE_JVMCI
2817 ,debug_id
2818 #endif
2819 );
2820 } else {
2821 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2822 reason, action, unloaded_class_index
2823 #if INCLUDE_JVMCI
2824 ,debug_id
2825 #endif
2826 );
2827 }
2828 return buf;
2829 }
2830
2831 juint Deoptimization::_deoptimization_hist
2832 [Deoptimization::Reason_LIMIT]
2833 [1 + Deoptimization::Action_LIMIT]
2834 [Deoptimization::BC_CASE_LIMIT]
2835 = {0};
2836
2837 enum {
2838 LSB_BITS = 8,
2839 LSB_MASK = right_n_bits(LSB_BITS)
2840 };
2841
2842 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2843 Bytecodes::Code bc) {
2844 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2845 assert(action >= 0 && action < Action_LIMIT, "oob");
2846 _deoptimization_hist[Reason_none][0][0] += 1; // total
2847 _deoptimization_hist[reason][0][0] += 1; // per-reason total
2848 juint* cases = _deoptimization_hist[reason][1+action];
2849 juint* bc_counter_addr = nullptr;
2850 juint bc_counter = 0;
2851 // Look for an unused counter, or an exact match to this BC.
2852 if (bc != Bytecodes::_illegal) {
2853 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2854 juint* counter_addr = &cases[bc_case];
2855 juint counter = *counter_addr;
2856 if ((counter == 0 && bc_counter_addr == nullptr)
2857 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2858 // this counter is either free or is already devoted to this BC
2859 bc_counter_addr = counter_addr;
2860 bc_counter = counter | bc;
2861 }
2862 }
2863 }
2864 if (bc_counter_addr == nullptr) {
2865 // Overflow, or no given bytecode.
2866 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2867 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2868 }
2869 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2870 }
2871
2872 jint Deoptimization::total_deoptimization_count() {
2873 return _deoptimization_hist[Reason_none][0][0];
2874 }
2875
2876 // Get the deopt count for a specific reason and a specific action. If either
2877 // one of 'reason' or 'action' is null, the method returns the sum of all
2878 // deoptimizations with the specific 'action' or 'reason' respectively.
2879 // If both arguments are null, the method returns the total deopt count.
2880 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2881 if (reason_str == nullptr && action_str == nullptr) {
2882 return total_deoptimization_count();
2883 }
2884 juint counter = 0;
2885 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2886 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2887 for (int action = 0; action < Action_LIMIT; action++) {
2888 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2889 juint* cases = _deoptimization_hist[reason][1+action];
2890 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2891 counter += cases[bc_case] >> LSB_BITS;
2892 }
2893 }
2894 }
2895 }
2896 }
2897 return counter;
2898 }
2899
2900 void Deoptimization::print_statistics() {
2901 juint total = total_deoptimization_count();
2902 juint account = total;
2903 if (total != 0) {
2904 ttyLocker ttyl;
2905 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2906 tty->print_cr("Deoptimization traps recorded:");
2907 #define PRINT_STAT_LINE(name, r) \
2908 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2909 PRINT_STAT_LINE("total", total);
2910 // For each non-zero entry in the histogram, print the reason,
2911 // the action, and (if specifically known) the type of bytecode.
2912 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2913 for (int action = 0; action < Action_LIMIT; action++) {
2914 juint* cases = _deoptimization_hist[reason][1+action];
2915 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2916 juint counter = cases[bc_case];
2917 if (counter != 0) {
2918 char name[1*K];
2919 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2920 if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2921 bc = Bytecodes::_illegal;
2922 os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2923 trap_reason_name(reason),
2924 trap_action_name(action),
2925 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2926 juint r = counter >> LSB_BITS;
2927 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2928 account -= r;
2929 }
2930 }
2931 }
2932 }
2933 if (account != 0) {
2934 PRINT_STAT_LINE("unaccounted", account);
2935 }
2936 #undef PRINT_STAT_LINE
2937 if (xtty != nullptr) xtty->tail("statistics");
2938 }
2939 }
2940
2941 #else // COMPILER2_OR_JVMCI
2942
2943
2944 // Stubs for C1 only system.
2945 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2946 return false;
2947 }
2948
2949 const char* Deoptimization::trap_reason_name(int reason) {
2950 return "unknown";
2951 }
2952
2953 jint Deoptimization::total_deoptimization_count() {
2954 return 0;
2955 }
2956
2957 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2958 return 0;
2959 }
2960
2961 void Deoptimization::print_statistics() {
2962 // no output
2963 }
2964
2965 void
2966 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2967 // no update
2968 }
2969
2970 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2971 return 0;
2972 }
2973
2974 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2975 Bytecodes::Code bc) {
2976 // no update
2977 }
2978
2979 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2980 int trap_state) {
2981 jio_snprintf(buf, buflen, "#%d", trap_state);
2982 return buf;
2983 }
2984
2985 #endif // COMPILER2_OR_JVMCI
|
63 #include "prims/methodHandles.hpp"
64 #include "prims/vectorSupport.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/basicLock.inline.hpp"
67 #include "runtime/continuation.hpp"
68 #include "runtime/continuationEntry.inline.hpp"
69 #include "runtime/deoptimization.hpp"
70 #include "runtime/escapeBarrier.hpp"
71 #include "runtime/fieldDescriptor.hpp"
72 #include "runtime/fieldDescriptor.inline.hpp"
73 #include "runtime/frame.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/interfaceSupport.inline.hpp"
76 #include "runtime/javaThread.hpp"
77 #include "runtime/jniHandles.inline.hpp"
78 #include "runtime/keepStackGCProcessed.hpp"
79 #include "runtime/lightweightSynchronizer.hpp"
80 #include "runtime/lockStack.inline.hpp"
81 #include "runtime/objectMonitor.inline.hpp"
82 #include "runtime/osThread.hpp"
83 #include "runtime/perfData.inline.hpp"
84 #include "runtime/safepointVerifiers.hpp"
85 #include "runtime/sharedRuntime.hpp"
86 #include "runtime/signature.hpp"
87 #include "runtime/stackFrameStream.inline.hpp"
88 #include "runtime/stackValue.hpp"
89 #include "runtime/stackWatermarkSet.hpp"
90 #include "runtime/stubRoutines.hpp"
91 #include "runtime/synchronizer.inline.hpp"
92 #include "runtime/threadSMR.hpp"
93 #include "runtime/threadWXSetters.inline.hpp"
94 #include "runtime/vframe.hpp"
95 #include "runtime/vframeArray.hpp"
96 #include "runtime/vframe_hp.hpp"
97 #include "runtime/vmOperations.hpp"
98 #include "services/management.hpp"
99 #include "utilities/checkedCast.hpp"
100 #include "utilities/events.hpp"
101 #include "utilities/growableArray.hpp"
102 #include "utilities/macros.hpp"
103 #include "utilities/preserveException.hpp"
104 #include "utilities/xmlstream.hpp"
105 #if INCLUDE_JFR
106 #include "jfr/jfr.inline.hpp"
107 #include "jfr/jfrEvents.hpp"
108 #include "jfr/metadata/jfrSerializer.hpp"
109 #endif
110
111 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
112 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
113 bool DeoptimizationScope::_committing_in_progress = false;
114
115 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
116 DEBUG_ONLY(_deopted = false;)
117
118 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
267 return checked_cast<int>(result);
268 }
269
270 void Deoptimization::UnrollBlock::print() {
271 ResourceMark rm;
272 stringStream st;
273 st.print_cr("UnrollBlock");
274 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
275 st.print( " frame_sizes: ");
276 for (int index = 0; index < number_of_frames(); index++) {
277 st.print("%zd ", frame_sizes()[index]);
278 }
279 st.cr();
280 tty->print_raw(st.freeze());
281 }
282
283 // In order to make fetch_unroll_info work properly with escape
284 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
285 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
286 // which is called from the method fetch_unroll_info_helper below.
287 JRT_BLOCK_ENTRY_PROF(Deoptimization::UnrollBlock*, Deoptimization, fetch_unroll_info, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
288 // fetch_unroll_info() is called at the beginning of the deoptimization
289 // handler. Note this fact before we start generating temporary frames
290 // that can confuse an asynchronous stack walker. This counter is
291 // decremented at the end of unpack_frames().
292 current->inc_in_deopt_handler();
293
294 if (exec_mode == Unpack_exception) {
295 // When we get here, a callee has thrown an exception into a deoptimized
296 // frame. That throw might have deferred stack watermark checking until
297 // after unwinding. So we deal with such deferred requests here.
298 StackWatermarkSet::after_unwind(current);
299 }
300
301 return fetch_unroll_info_helper(current, exec_mode);
302 JRT_END
303
304 #if COMPILER2_OR_JVMCI
305 // print information about reallocated objects
306 static void print_objects(JavaThread* deoptee_thread,
307 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
855 case Bytecodes::_goto_w:
856 case Bytecodes::_athrow:
857 case Bytecodes::_areturn:
858 case Bytecodes::_dreturn:
859 case Bytecodes::_freturn:
860 case Bytecodes::_ireturn:
861 case Bytecodes::_lreturn:
862 case Bytecodes::_jsr:
863 case Bytecodes::_ret:
864 case Bytecodes::_return:
865 case Bytecodes::_lookupswitch:
866 case Bytecodes::_tableswitch:
867 return false;
868 default:
869 return true;
870 }
871 }
872 #endif
873
874 // Return BasicType of value being returned
875 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
876 assert(thread == JavaThread::current(), "pre-condition");
877
878 // We are already active in the special DeoptResourceMark any ResourceObj's we
879 // allocate will be freed at the end of the routine.
880
881 // JRT_LEAF methods don't normally allocate handles and there is a
882 // NoHandleMark to enforce that. It is actually safe to use Handles
883 // in a JRT_LEAF method, and sometimes desirable, but to do so we
884 // must use ResetNoHandleMark to bypass the NoHandleMark, and
885 // then use a HandleMark to ensure any Handles we do create are
886 // cleaned up in this scope.
887 ResetNoHandleMark rnhm;
888 HandleMark hm(thread);
889
890 frame stub_frame = thread->last_frame();
891
892 Continuation::notify_deopt(thread, stub_frame.sp());
893
894 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
895 // must point to the vframeArray for the unpack frame.
1771 if (monitors != nullptr) {
1772 // Unlock in reverse order starting from most nested monitor.
1773 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1774 BasicObjectLock* src = monitors->at(j);
1775 if (src->obj() != nullptr) {
1776 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1777 }
1778 }
1779 array->element(i)->free_monitors();
1780 #ifdef ASSERT
1781 array->element(i)->set_removed_monitors();
1782 #endif
1783 }
1784 }
1785 }
1786 #endif
1787
1788 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1789 assert(fr.can_be_deoptimized(), "checking frame type");
1790
1791 nmethod* nm = fr.cb()->as_nmethod_or_null();
1792 assert(nm != nullptr, "only compiled methods can deopt");
1793 DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1794 ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1795 Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1796 : cur_sd->method()->java_code_at(cur_sd->bci()));
1797 gather_statistics(nm, reason, action, bc);
1798
1799 if (LogCompilation && xtty != nullptr) {
1800 ttyLocker ttyl;
1801 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1802 nm->log_identity(xtty);
1803 xtty->end_head();
1804 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1805 xtty->begin_elem("jvms bci='%d'", sd->bci());
1806 xtty->method(sd->method());
1807 xtty->end_elem();
1808 if (sd->is_top()) break;
1809 }
1810 xtty->tail("deoptimized");
1811 }
1812
1813 Continuation::notify_deopt(thread, fr.sp());
1814
1815 // Patch the compiled method so that when execution returns to it we will
1816 // deopt the execution state and return to the interpreter.
1817 fr.deoptimize(thread);
1818 }
1819
2015 event.set_compileId(nm->compile_id());
2016 event.set_compiler(nm->compiler_type());
2017 event.set_method(method);
2018 event.set_lineNumber(method->line_number_from_bci(trap_bci));
2019 event.set_bci(trap_bci);
2020 event.set_instruction(instruction);
2021 event.set_reason(reason);
2022 event.set_action(action);
2023 event.commit();
2024 }
2025 }
2026
2027 #endif // INCLUDE_JFR
2028
2029 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2030 const char* reason_name, const char* reason_action) {
2031 LogTarget(Debug, deoptimization) lt;
2032 if (lt.is_enabled()) {
2033 LogStream ls(lt);
2034 bool is_osr = nm->is_osr_method();
2035 ls.print("cid=%4d %s%s level=%d",
2036 nm->compile_id(), (is_osr ? "osr" : " "), (nm->preloaded() ? "preload" : ""), nm->comp_level());
2037 ls.print(" %s", tm->name_and_sig_as_C_string());
2038 ls.print(" trap_bci=%d ", trap_bci);
2039 if (is_osr) {
2040 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2041 }
2042 ls.print("%s ", reason_name);
2043 ls.print("%s ", reason_action);
2044 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2045 pc, fr.pc() - nm->code_begin());
2046 }
2047 }
2048
2049 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2050 HandleMark hm(current);
2051
2052 // uncommon_trap() is called at the beginning of the uncommon trap
2053 // handler. Note this fact before we start generating temporary frames
2054 // that can confuse an asynchronous stack walker. This counter is
2055 // decremented at the end of unpack_frames().
2056
2057 current->inc_in_deopt_handler();
2058
2059 #if INCLUDE_JVMCI
2060 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2061 RegisterMap reg_map(current,
2062 RegisterMap::UpdateMap::include,
2063 RegisterMap::ProcessFrames::include,
2064 RegisterMap::WalkContinuation::skip);
2065 #else
2066 RegisterMap reg_map(current,
2067 RegisterMap::UpdateMap::skip,
2068 RegisterMap::ProcessFrames::include,
2069 RegisterMap::WalkContinuation::skip);
2105 #if INCLUDE_JVMCI
2106 jlong speculation = current->pending_failed_speculation();
2107 if (nm->is_compiled_by_jvmci()) {
2108 nm->update_speculation(current);
2109 } else {
2110 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2111 }
2112
2113 if (trap_bci == SynchronizationEntryBCI) {
2114 trap_bci = 0;
2115 current->set_pending_monitorenter(true);
2116 }
2117
2118 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2119 current->set_pending_transfer_to_interpreter(true);
2120 }
2121 #endif
2122
2123 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2124 // Record this event in the histogram.
2125 gather_statistics(nm, reason, action, trap_bc);
2126
2127 // Ensure that we can record deopt. history:
2128 bool create_if_missing = ProfileTraps;
2129
2130 methodHandle profiled_method;
2131 #if INCLUDE_JVMCI
2132 if (nm->is_compiled_by_jvmci()) {
2133 profiled_method = methodHandle(current, nm->method());
2134 } else {
2135 profiled_method = trap_method;
2136 }
2137 #else
2138 profiled_method = trap_method;
2139 #endif
2140
2141 MethodData* trap_mdo =
2142 get_method_data(current, profiled_method, create_if_missing);
2143
2144 { // Log Deoptimization event for JFR, UL and event system
2145 Method* tm = trap_method();
2616 bool ignore_maybe_prior_recompile;
2617 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2618 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2619 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2620
2621 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2622 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2623
2624 query_update_method_data(trap_mdo, trap_bci,
2625 (DeoptReason)reason,
2626 update_total_counts,
2627 #if INCLUDE_JVMCI
2628 false,
2629 #endif
2630 nullptr,
2631 ignore_this_trap_count,
2632 ignore_maybe_prior_trap,
2633 ignore_maybe_prior_recompile);
2634 }
2635
2636 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2637 // Enable WXWrite: current function is called from methods compiled by C2 directly
2638 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2639
2640 // Still in Java no safepoints
2641 {
2642 // This enters VM and may safepoint
2643 uncommon_trap_inner(current, trap_request);
2644 }
2645 HandleMark hm(current);
2646 return fetch_unroll_info_helper(current, exec_mode);
2647 PROF_END
2648
2649 // Local derived constants.
2650 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2651 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2652 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2653
2654 //---------------------------trap_state_reason---------------------------------
2655 Deoptimization::DeoptReason
2656 Deoptimization::trap_state_reason(int trap_state) {
2657 // This assert provides the link between the width of DataLayout::trap_bits
2658 // and the encoding of "recorded" reasons. It ensures there are enough
2659 // bits to store all needed reasons in the per-BCI MDO profile.
2660 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2661 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2662 trap_state -= recompile_bit;
2663 if (trap_state == DS_REASON_MASK) {
2664 return Reason_many;
2665 } else {
2666 assert((int)Reason_none == 0, "state=0 => Reason_none");
2667 return (DeoptReason)trap_state;
2817 size_t len;
2818 if (unloaded_class_index < 0) {
2819 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2820 reason, action
2821 #if INCLUDE_JVMCI
2822 ,debug_id
2823 #endif
2824 );
2825 } else {
2826 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2827 reason, action, unloaded_class_index
2828 #if INCLUDE_JVMCI
2829 ,debug_id
2830 #endif
2831 );
2832 }
2833 return buf;
2834 }
2835
2836 juint Deoptimization::_deoptimization_hist
2837 [1 + 4 + 5] // total + online + archived
2838 [Deoptimization::Reason_LIMIT]
2839 [1 + Deoptimization::Action_LIMIT]
2840 [Deoptimization::BC_CASE_LIMIT]
2841 = {0};
2842
2843 enum {
2844 LSB_BITS = 8,
2845 LSB_MASK = right_n_bits(LSB_BITS)
2846 };
2847
2848 static void update(juint* cases, Bytecodes::Code bc) {
2849 juint* bc_counter_addr = nullptr;
2850 juint bc_counter = 0;
2851 // Look for an unused counter, or an exact match to this BC.
2852 if (bc != Bytecodes::_illegal) {
2853 for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2854 juint* counter_addr = &cases[bc_case];
2855 juint counter = *counter_addr;
2856 if ((counter == 0 && bc_counter_addr == nullptr)
2857 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2858 // this counter is either free or is already devoted to this BC
2859 bc_counter_addr = counter_addr;
2860 bc_counter = counter | bc;
2861 }
2862 }
2863 }
2864 if (bc_counter_addr == nullptr) {
2865 // Overflow, or no given bytecode.
2866 bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2867 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2868 }
2869 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2870 }
2871
2872
2873 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2874 Bytecodes::Code bc) {
2875 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2876 assert(action >= 0 && action < Action_LIMIT, "oob");
2877 _deoptimization_hist[0][Reason_none][0][0] += 1; // total
2878 _deoptimization_hist[0][reason][0][0] += 1; // per-reason total
2879
2880 update(_deoptimization_hist[0][reason][1+action], bc);
2881
2882 uint lvl = nm->comp_level() + (nm->is_aot() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2883 _deoptimization_hist[lvl][Reason_none][0][0] += 1; // total
2884 _deoptimization_hist[lvl][reason][0][0] += 1; // per-reason total
2885 update(_deoptimization_hist[lvl][reason][1+action], bc);
2886 }
2887
2888 jint Deoptimization::total_deoptimization_count() {
2889 return _deoptimization_hist[0][Reason_none][0][0];
2890 }
2891
2892 // Get the deopt count for a specific reason and a specific action. If either
2893 // one of 'reason' or 'action' is null, the method returns the sum of all
2894 // deoptimizations with the specific 'action' or 'reason' respectively.
2895 // If both arguments are null, the method returns the total deopt count.
2896 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2897 if (reason_str == nullptr && action_str == nullptr) {
2898 return total_deoptimization_count();
2899 }
2900 juint counter = 0;
2901 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2902 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2903 for (int action = 0; action < Action_LIMIT; action++) {
2904 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2905 juint* cases = _deoptimization_hist[0][reason][1+action];
2906 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2907 counter += cases[bc_case] >> LSB_BITS;
2908 }
2909 }
2910 }
2911 }
2912 }
2913 return counter;
2914 }
2915
2916 void Deoptimization::print_statistics() {
2917 ttyLocker ttyl;
2918 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2919 tty->print_cr("Deoptimization traps recorded:");
2920 print_statistics_on(tty);
2921 if (xtty != nullptr) xtty->tail("statistics");
2922 }
2923
2924 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2925 juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2926 juint account = total;
2927 #define PRINT_STAT_LINE(name, r) \
2928 st->print_cr(" %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2929 if (total > 0) {
2930 st->print(" %s: ", title);
2931 PRINT_STAT_LINE("total", total);
2932 // For each non-zero entry in the histogram, print the reason,
2933 // the action, and (if specifically known) the type of bytecode.
2934 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2935 for (int action = 0; action < Action_LIMIT; action++) {
2936 juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2937 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2938 juint counter = cases[bc_case];
2939 if (counter != 0) {
2940 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2941 const char* bc_name = "other";
2942 if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2943 // overwritten
2944 } else if (Bytecodes::is_defined(bc)) {
2945 bc_name = Bytecodes::name(bc);
2946 }
2947 juint r = counter >> LSB_BITS;
2948 st->print_cr(" %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2949 trap_reason_name(reason), trap_action_name(action), bc_name,
2950 r, (r * 100.0) / total);
2951 account -= r;
2952 }
2953 }
2954 }
2955 }
2956 if (account != 0) {
2957 PRINT_STAT_LINE("unaccounted", account);
2958 }
2959 #undef PRINT_STAT_LINE
2960 }
2961 }
2962
2963 void Deoptimization::print_statistics_on(outputStream* st) {
2964 // print_statistics_on("Total", 0, st);
2965 print_statistics_on("Tier1", 1, st);
2966 print_statistics_on("Tier2", 2, st);
2967 print_statistics_on("Tier3", 3, st);
2968 print_statistics_on("Tier4", 4, st);
2969
2970 print_statistics_on("AOT Code Tier1", 5, st);
2971 print_statistics_on("AOT Code Tier2", 6, st);
2972 print_statistics_on("AOT Code Tier4", 8, st);
2973 print_statistics_on("AOT Code Tier5 (preloaded)", 9, st);
2974 }
2975
2976 #define DO_COUNTERS(macro) \
2977 macro(Deoptimization, fetch_unroll_info) \
2978 macro(Deoptimization, unpack_frames) \
2979 macro(Deoptimization, uncommon_trap_inner) \
2980 macro(Deoptimization, uncommon_trap)
2981
2982 #define INIT_COUNTER(sub, name) \
2983 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2984 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2985
2986 void Deoptimization::init_counters() {
2987 if (ProfileRuntimeCalls && UsePerfData) {
2988 EXCEPTION_MARK;
2989
2990 DO_COUNTERS(INIT_COUNTER)
2991
2992 if (HAS_PENDING_EXCEPTION) {
2993 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2994 }
2995 }
2996 }
2997 #undef INIT_COUNTER
2998
2999 #define PRINT_COUNTER(sub, name) { \
3000 jlong count = _perf_##sub##_##name##_count->get_value(); \
3001 if (count > 0) { \
3002 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
3003 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
3004 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
3005 count); \
3006 }}
3007
3008 void Deoptimization::print_counters_on(outputStream* st) {
3009 if (ProfileRuntimeCalls && UsePerfData) {
3010 DO_COUNTERS(PRINT_COUNTER)
3011 } else {
3012 st->print_cr(" Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3013 }
3014 }
3015
3016 #undef PRINT_COUNTER
3017 #undef DO_COUNTERS
3018
3019 #else // COMPILER2_OR_JVMCI
3020
3021
3022 // Stubs for C1 only system.
3023 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
3024 return false;
3025 }
3026
3027 const char* Deoptimization::trap_reason_name(int reason) {
3028 return "unknown";
3029 }
3030
3031 jint Deoptimization::total_deoptimization_count() {
3032 return 0;
3033 }
3034
3035 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
3036 return 0;
3037 }
3038
3039 void Deoptimization::print_statistics() {
3040 // no output
3041 }
3042
3043 void
3044 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
3045 // no update
3046 }
3047
3048 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
3049 return 0;
3050 }
3051
3052 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
3053 Bytecodes::Code bc) {
3054 // no update
3055 }
3056
3057 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
3058 int trap_state) {
3059 jio_snprintf(buf, buflen, "#%d", trap_state);
3060 return buf;
3061 }
3062
3063 void Deoptimization::init_counters() {
3064 // nothing to do
3065 }
3066
3067 void Deoptimization::print_counters_on(outputStream* st) {
3068 // no output
3069 }
3070
3071 void Deoptimization::print_statistics_on(outputStream* st) {
3072 // no output
3073 }
3074
3075 #endif // COMPILER2_OR_JVMCI
|