60 #include "prims/jvmtiDeferredUpdates.hpp"
61 #include "prims/jvmtiExport.hpp"
62 #include "prims/jvmtiThreadState.hpp"
63 #include "prims/methodHandles.hpp"
64 #include "prims/vectorSupport.hpp"
65 #include "runtime/atomic.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.hpp"
71 #include "runtime/fieldDescriptor.inline.hpp"
72 #include "runtime/frame.inline.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/interfaceSupport.inline.hpp"
75 #include "runtime/javaThread.hpp"
76 #include "runtime/jniHandles.inline.hpp"
77 #include "runtime/keepStackGCProcessed.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/vframeArray.hpp"
92 #include "runtime/vframe_hp.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/jfrEvents.hpp"
102 #include "jfr/metadata/jfrSerializer.hpp"
103 #endif
104
105 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
106 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
107 bool DeoptimizationScope::_committing_in_progress = false;
108
109 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
110 DEBUG_ONLY(_deopted = false;)
111
112 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
113 // If there is nothing to deopt _required_gen is the same as comitted.
257 return checked_cast<int>(result);
258 }
259
260 void Deoptimization::UnrollBlock::print() {
261 ResourceMark rm;
262 stringStream st;
263 st.print_cr("UnrollBlock");
264 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
265 st.print( " frame_sizes: ");
266 for (int index = 0; index < number_of_frames(); index++) {
267 st.print(INTX_FORMAT " ", frame_sizes()[index]);
268 }
269 st.cr();
270 tty->print_raw(st.freeze());
271 }
272
273 // In order to make fetch_unroll_info work properly with escape
274 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
275 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
276 // which is called from the method fetch_unroll_info_helper below.
277 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
278 // fetch_unroll_info() is called at the beginning of the deoptimization
279 // handler. Note this fact before we start generating temporary frames
280 // that can confuse an asynchronous stack walker. This counter is
281 // decremented at the end of unpack_frames().
282 current->inc_in_deopt_handler();
283
284 if (exec_mode == Unpack_exception) {
285 // When we get here, a callee has thrown an exception into a deoptimized
286 // frame. That throw might have deferred stack watermark checking until
287 // after unwinding. So we deal with such deferred requests here.
288 StackWatermarkSet::after_unwind(current);
289 }
290
291 return fetch_unroll_info_helper(current, exec_mode);
292 JRT_END
293
294 #if COMPILER2_OR_JVMCI
295 // print information about reallocated objects
296 static void print_objects(JavaThread* deoptee_thread,
297 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
829
830 assert(f->is_interpreted_frame(), "must be interpreted");
831 }
832
833 #ifndef PRODUCT
834 static bool falls_through(Bytecodes::Code bc) {
835 switch (bc) {
836 // List may be incomplete. Here we really only care about bytecodes where compiled code
837 // can deoptimize.
838 case Bytecodes::_goto:
839 case Bytecodes::_goto_w:
840 case Bytecodes::_athrow:
841 return false;
842 default:
843 return true;
844 }
845 }
846 #endif
847
848 // Return BasicType of value being returned
849 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
850 assert(thread == JavaThread::current(), "pre-condition");
851
852 // We are already active in the special DeoptResourceMark any ResourceObj's we
853 // allocate will be freed at the end of the routine.
854
855 // JRT_LEAF methods don't normally allocate handles and there is a
856 // NoHandleMark to enforce that. It is actually safe to use Handles
857 // in a JRT_LEAF method, and sometimes desirable, but to do so we
858 // must use ResetNoHandleMark to bypass the NoHandleMark, and
859 // then use a HandleMark to ensure any Handles we do create are
860 // cleaned up in this scope.
861 ResetNoHandleMark rnhm;
862 HandleMark hm(thread);
863
864 frame stub_frame = thread->last_frame();
865
866 Continuation::notify_deopt(thread, stub_frame.sp());
867
868 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
869 // must point to the vframeArray for the unpack frame.
1731 if (monitors != nullptr) {
1732 // Unlock in reverse order starting from most nested monitor.
1733 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1734 BasicObjectLock* src = monitors->at(j);
1735 if (src->obj() != nullptr) {
1736 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1737 }
1738 }
1739 array->element(i)->free_monitors();
1740 #ifdef ASSERT
1741 array->element(i)->set_removed_monitors();
1742 #endif
1743 }
1744 }
1745 }
1746 #endif
1747
1748 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1749 assert(fr.can_be_deoptimized(), "checking frame type");
1750
1751 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1752
1753 if (LogCompilation && xtty != nullptr) {
1754 nmethod* nm = fr.cb()->as_nmethod_or_null();
1755 assert(nm != nullptr, "only compiled methods can deopt");
1756
1757 ttyLocker ttyl;
1758 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1759 nm->log_identity(xtty);
1760 xtty->end_head();
1761 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1762 xtty->begin_elem("jvms bci='%d'", sd->bci());
1763 xtty->method(sd->method());
1764 xtty->end_elem();
1765 if (sd->is_top()) break;
1766 }
1767 xtty->tail("deoptimized");
1768 }
1769
1770 Continuation::notify_deopt(thread, fr.sp());
1771
1772 // Patch the compiled method so that when execution returns to it we will
1773 // deopt the execution state and return to the interpreter.
1774 fr.deoptimize(thread);
1775 }
1776
1986 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1987 const char* reason_name, const char* reason_action) {
1988 LogTarget(Debug, deoptimization) lt;
1989 if (lt.is_enabled()) {
1990 LogStream ls(lt);
1991 bool is_osr = nm->is_osr_method();
1992 ls.print("cid=%4d %s level=%d",
1993 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
1994 ls.print(" %s", tm->name_and_sig_as_C_string());
1995 ls.print(" trap_bci=%d ", trap_bci);
1996 if (is_osr) {
1997 ls.print("osr_bci=%d ", nm->osr_entry_bci());
1998 }
1999 ls.print("%s ", reason_name);
2000 ls.print("%s ", reason_action);
2001 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2002 pc, fr.pc() - nm->code_begin());
2003 }
2004 }
2005
2006 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2007 HandleMark hm(current);
2008
2009 // uncommon_trap() is called at the beginning of the uncommon trap
2010 // handler. Note this fact before we start generating temporary frames
2011 // that can confuse an asynchronous stack walker. This counter is
2012 // decremented at the end of unpack_frames().
2013
2014 current->inc_in_deopt_handler();
2015
2016 #if INCLUDE_JVMCI
2017 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2018 RegisterMap reg_map(current,
2019 RegisterMap::UpdateMap::include,
2020 RegisterMap::ProcessFrames::include,
2021 RegisterMap::WalkContinuation::skip);
2022 #else
2023 RegisterMap reg_map(current,
2024 RegisterMap::UpdateMap::skip,
2025 RegisterMap::ProcessFrames::include,
2026 RegisterMap::WalkContinuation::skip);
2062 #if INCLUDE_JVMCI
2063 jlong speculation = current->pending_failed_speculation();
2064 if (nm->is_compiled_by_jvmci()) {
2065 nm->update_speculation(current);
2066 } else {
2067 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2068 }
2069
2070 if (trap_bci == SynchronizationEntryBCI) {
2071 trap_bci = 0;
2072 current->set_pending_monitorenter(true);
2073 }
2074
2075 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2076 current->set_pending_transfer_to_interpreter(true);
2077 }
2078 #endif
2079
2080 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2081 // Record this event in the histogram.
2082 gather_statistics(reason, action, trap_bc);
2083
2084 // Ensure that we can record deopt. history:
2085 // Need MDO to record RTM code generation state.
2086 bool create_if_missing = ProfileTraps RTM_OPT_ONLY( || UseRTMLocking );
2087
2088 methodHandle profiled_method;
2089 #if INCLUDE_JVMCI
2090 if (nm->is_compiled_by_jvmci()) {
2091 profiled_method = methodHandle(current, nm->method());
2092 } else {
2093 profiled_method = trap_method;
2094 }
2095 #else
2096 profiled_method = trap_method;
2097 #endif
2098
2099 MethodData* trap_mdo =
2100 get_method_data(current, profiled_method, create_if_missing);
2101
2102 { // Log Deoptimization event for JFR, UL and event system
2585 bool ignore_maybe_prior_recompile;
2586 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2587 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2588 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2589
2590 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2591 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2592
2593 query_update_method_data(trap_mdo, trap_bci,
2594 (DeoptReason)reason,
2595 update_total_counts,
2596 #if INCLUDE_JVMCI
2597 false,
2598 #endif
2599 nullptr,
2600 ignore_this_trap_count,
2601 ignore_maybe_prior_trap,
2602 ignore_maybe_prior_recompile);
2603 }
2604
2605 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2606 // Enable WXWrite: current function is called from methods compiled by C2 directly
2607 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2608
2609 // Still in Java no safepoints
2610 {
2611 // This enters VM and may safepoint
2612 uncommon_trap_inner(current, trap_request);
2613 }
2614 HandleMark hm(current);
2615 return fetch_unroll_info_helper(current, exec_mode);
2616 }
2617
2618 // Local derived constants.
2619 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2620 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2621 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2622
2623 //---------------------------trap_state_reason---------------------------------
2624 Deoptimization::DeoptReason
2625 Deoptimization::trap_state_reason(int trap_state) {
2626 // This assert provides the link between the width of DataLayout::trap_bits
2627 // and the encoding of "recorded" reasons. It ensures there are enough
2628 // bits to store all needed reasons in the per-BCI MDO profile.
2629 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2630 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2631 trap_state -= recompile_bit;
2632 if (trap_state == DS_REASON_MASK) {
2633 return Reason_many;
2634 } else {
2635 assert((int)Reason_none == 0, "state=0 => Reason_none");
2636 return (DeoptReason)trap_state;
2786 size_t len;
2787 if (unloaded_class_index < 0) {
2788 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2789 reason, action
2790 #if INCLUDE_JVMCI
2791 ,debug_id
2792 #endif
2793 );
2794 } else {
2795 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2796 reason, action, unloaded_class_index
2797 #if INCLUDE_JVMCI
2798 ,debug_id
2799 #endif
2800 );
2801 }
2802 return buf;
2803 }
2804
2805 juint Deoptimization::_deoptimization_hist
2806 [Deoptimization::Reason_LIMIT]
2807 [1 + Deoptimization::Action_LIMIT]
2808 [Deoptimization::BC_CASE_LIMIT]
2809 = {0};
2810
2811 enum {
2812 LSB_BITS = 8,
2813 LSB_MASK = right_n_bits(LSB_BITS)
2814 };
2815
2816 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2817 Bytecodes::Code bc) {
2818 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2819 assert(action >= 0 && action < Action_LIMIT, "oob");
2820 _deoptimization_hist[Reason_none][0][0] += 1; // total
2821 _deoptimization_hist[reason][0][0] += 1; // per-reason total
2822 juint* cases = _deoptimization_hist[reason][1+action];
2823 juint* bc_counter_addr = nullptr;
2824 juint bc_counter = 0;
2825 // Look for an unused counter, or an exact match to this BC.
2826 if (bc != Bytecodes::_illegal) {
2827 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2828 juint* counter_addr = &cases[bc_case];
2829 juint counter = *counter_addr;
2830 if ((counter == 0 && bc_counter_addr == nullptr)
2831 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2832 // this counter is either free or is already devoted to this BC
2833 bc_counter_addr = counter_addr;
2834 bc_counter = counter | bc;
2835 }
2836 }
2837 }
2838 if (bc_counter_addr == nullptr) {
2839 // Overflow, or no given bytecode.
2840 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2841 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2842 }
2843 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2844 }
2845
2846 jint Deoptimization::total_deoptimization_count() {
2847 return _deoptimization_hist[Reason_none][0][0];
2848 }
2849
2850 // Get the deopt count for a specific reason and a specific action. If either
2851 // one of 'reason' or 'action' is null, the method returns the sum of all
2852 // deoptimizations with the specific 'action' or 'reason' respectively.
2853 // If both arguments are null, the method returns the total deopt count.
2854 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2855 if (reason_str == nullptr && action_str == nullptr) {
2856 return total_deoptimization_count();
2857 }
2858 juint counter = 0;
2859 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2860 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2861 for (int action = 0; action < Action_LIMIT; action++) {
2862 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2863 juint* cases = _deoptimization_hist[reason][1+action];
2864 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2865 counter += cases[bc_case] >> LSB_BITS;
2866 }
2867 }
2868 }
2869 }
2870 }
2871 return counter;
2872 }
2873
2874 void Deoptimization::print_statistics() {
2875 juint total = total_deoptimization_count();
2876 juint account = total;
2877 if (total != 0) {
2878 ttyLocker ttyl;
2879 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2880 tty->print_cr("Deoptimization traps recorded:");
2881 #define PRINT_STAT_LINE(name, r) \
2882 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2883 PRINT_STAT_LINE("total", total);
2884 // For each non-zero entry in the histogram, print the reason,
2885 // the action, and (if specifically known) the type of bytecode.
2886 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2887 for (int action = 0; action < Action_LIMIT; action++) {
2888 juint* cases = _deoptimization_hist[reason][1+action];
2889 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2890 juint counter = cases[bc_case];
2891 if (counter != 0) {
2892 char name[1*K];
2893 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2894 if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2895 bc = Bytecodes::_illegal;
2896 os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2897 trap_reason_name(reason),
2898 trap_action_name(action),
2899 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2900 juint r = counter >> LSB_BITS;
2901 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2902 account -= r;
2903 }
2904 }
2905 }
2906 }
2907 if (account != 0) {
2908 PRINT_STAT_LINE("unaccounted", account);
2909 }
2910 #undef PRINT_STAT_LINE
2911 if (xtty != nullptr) xtty->tail("statistics");
2912 }
2913 }
2914
2915 #else // COMPILER2_OR_JVMCI
2916
2917
2918 // Stubs for C1 only system.
2919 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2920 return false;
2921 }
2922
2923 const char* Deoptimization::trap_reason_name(int reason) {
2924 return "unknown";
2925 }
2926
2927 jint Deoptimization::total_deoptimization_count() {
2928 return 0;
2929 }
2930
2931 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2932 return 0;
2933 }
2934
2940 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2941 // no update
2942 }
2943
2944 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2945 return 0;
2946 }
2947
2948 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2949 Bytecodes::Code bc) {
2950 // no update
2951 }
2952
2953 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2954 int trap_state) {
2955 jio_snprintf(buf, buflen, "#%d", trap_state);
2956 return buf;
2957 }
2958
2959 #endif // COMPILER2_OR_JVMCI
|
60 #include "prims/jvmtiDeferredUpdates.hpp"
61 #include "prims/jvmtiExport.hpp"
62 #include "prims/jvmtiThreadState.hpp"
63 #include "prims/methodHandles.hpp"
64 #include "prims/vectorSupport.hpp"
65 #include "runtime/atomic.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.hpp"
71 #include "runtime/fieldDescriptor.inline.hpp"
72 #include "runtime/frame.inline.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/interfaceSupport.inline.hpp"
75 #include "runtime/javaThread.hpp"
76 #include "runtime/jniHandles.inline.hpp"
77 #include "runtime/keepStackGCProcessed.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/vframeArray.hpp"
93 #include "runtime/vframe_hp.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/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);
115 // If there is nothing to deopt _required_gen is the same as comitted.
259 return checked_cast<int>(result);
260 }
261
262 void Deoptimization::UnrollBlock::print() {
263 ResourceMark rm;
264 stringStream st;
265 st.print_cr("UnrollBlock");
266 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
267 st.print( " frame_sizes: ");
268 for (int index = 0; index < number_of_frames(); index++) {
269 st.print(INTX_FORMAT " ", frame_sizes()[index]);
270 }
271 st.cr();
272 tty->print_raw(st.freeze());
273 }
274
275 // In order to make fetch_unroll_info work properly with escape
276 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
277 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
278 // which is called from the method fetch_unroll_info_helper below.
279 JRT_BLOCK_ENTRY_PROF(Deoptimization::UnrollBlock*, Deoptimization, fetch_unroll_info, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
280 // fetch_unroll_info() is called at the beginning of the deoptimization
281 // handler. Note this fact before we start generating temporary frames
282 // that can confuse an asynchronous stack walker. This counter is
283 // decremented at the end of unpack_frames().
284 current->inc_in_deopt_handler();
285
286 if (exec_mode == Unpack_exception) {
287 // When we get here, a callee has thrown an exception into a deoptimized
288 // frame. That throw might have deferred stack watermark checking until
289 // after unwinding. So we deal with such deferred requests here.
290 StackWatermarkSet::after_unwind(current);
291 }
292
293 return fetch_unroll_info_helper(current, exec_mode);
294 JRT_END
295
296 #if COMPILER2_OR_JVMCI
297 // print information about reallocated objects
298 static void print_objects(JavaThread* deoptee_thread,
299 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
831
832 assert(f->is_interpreted_frame(), "must be interpreted");
833 }
834
835 #ifndef PRODUCT
836 static bool falls_through(Bytecodes::Code bc) {
837 switch (bc) {
838 // List may be incomplete. Here we really only care about bytecodes where compiled code
839 // can deoptimize.
840 case Bytecodes::_goto:
841 case Bytecodes::_goto_w:
842 case Bytecodes::_athrow:
843 return false;
844 default:
845 return true;
846 }
847 }
848 #endif
849
850 // Return BasicType of value being returned
851 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
852 assert(thread == JavaThread::current(), "pre-condition");
853
854 // We are already active in the special DeoptResourceMark any ResourceObj's we
855 // allocate will be freed at the end of the routine.
856
857 // JRT_LEAF methods don't normally allocate handles and there is a
858 // NoHandleMark to enforce that. It is actually safe to use Handles
859 // in a JRT_LEAF method, and sometimes desirable, but to do so we
860 // must use ResetNoHandleMark to bypass the NoHandleMark, and
861 // then use a HandleMark to ensure any Handles we do create are
862 // cleaned up in this scope.
863 ResetNoHandleMark rnhm;
864 HandleMark hm(thread);
865
866 frame stub_frame = thread->last_frame();
867
868 Continuation::notify_deopt(thread, stub_frame.sp());
869
870 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
871 // must point to the vframeArray for the unpack frame.
1733 if (monitors != nullptr) {
1734 // Unlock in reverse order starting from most nested monitor.
1735 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1736 BasicObjectLock* src = monitors->at(j);
1737 if (src->obj() != nullptr) {
1738 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1739 }
1740 }
1741 array->element(i)->free_monitors();
1742 #ifdef ASSERT
1743 array->element(i)->set_removed_monitors();
1744 #endif
1745 }
1746 }
1747 }
1748 #endif
1749
1750 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1751 assert(fr.can_be_deoptimized(), "checking frame type");
1752
1753 nmethod* nm = fr.cb()->as_nmethod_or_null();
1754 assert(nm != nullptr, "only compiled methods can deopt");
1755 DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1756 ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1757 Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1758 : cur_sd->method()->java_code_at(cur_sd->bci()));
1759 gather_statistics(nm, reason, action, bc);
1760
1761 if (LogCompilation && xtty != nullptr) {
1762 ttyLocker ttyl;
1763 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1764 nm->log_identity(xtty);
1765 xtty->end_head();
1766 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1767 xtty->begin_elem("jvms bci='%d'", sd->bci());
1768 xtty->method(sd->method());
1769 xtty->end_elem();
1770 if (sd->is_top()) break;
1771 }
1772 xtty->tail("deoptimized");
1773 }
1774
1775 Continuation::notify_deopt(thread, fr.sp());
1776
1777 // Patch the compiled method so that when execution returns to it we will
1778 // deopt the execution state and return to the interpreter.
1779 fr.deoptimize(thread);
1780 }
1781
1991 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1992 const char* reason_name, const char* reason_action) {
1993 LogTarget(Debug, deoptimization) lt;
1994 if (lt.is_enabled()) {
1995 LogStream ls(lt);
1996 bool is_osr = nm->is_osr_method();
1997 ls.print("cid=%4d %s level=%d",
1998 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
1999 ls.print(" %s", tm->name_and_sig_as_C_string());
2000 ls.print(" trap_bci=%d ", trap_bci);
2001 if (is_osr) {
2002 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2003 }
2004 ls.print("%s ", reason_name);
2005 ls.print("%s ", reason_action);
2006 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2007 pc, fr.pc() - nm->code_begin());
2008 }
2009 }
2010
2011 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2012 HandleMark hm(current);
2013
2014 // uncommon_trap() is called at the beginning of the uncommon trap
2015 // handler. Note this fact before we start generating temporary frames
2016 // that can confuse an asynchronous stack walker. This counter is
2017 // decremented at the end of unpack_frames().
2018
2019 current->inc_in_deopt_handler();
2020
2021 #if INCLUDE_JVMCI
2022 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2023 RegisterMap reg_map(current,
2024 RegisterMap::UpdateMap::include,
2025 RegisterMap::ProcessFrames::include,
2026 RegisterMap::WalkContinuation::skip);
2027 #else
2028 RegisterMap reg_map(current,
2029 RegisterMap::UpdateMap::skip,
2030 RegisterMap::ProcessFrames::include,
2031 RegisterMap::WalkContinuation::skip);
2067 #if INCLUDE_JVMCI
2068 jlong speculation = current->pending_failed_speculation();
2069 if (nm->is_compiled_by_jvmci()) {
2070 nm->update_speculation(current);
2071 } else {
2072 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2073 }
2074
2075 if (trap_bci == SynchronizationEntryBCI) {
2076 trap_bci = 0;
2077 current->set_pending_monitorenter(true);
2078 }
2079
2080 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2081 current->set_pending_transfer_to_interpreter(true);
2082 }
2083 #endif
2084
2085 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2086 // Record this event in the histogram.
2087 gather_statistics(nm, reason, action, trap_bc);
2088
2089 // Ensure that we can record deopt. history:
2090 // Need MDO to record RTM code generation state.
2091 bool create_if_missing = ProfileTraps RTM_OPT_ONLY( || UseRTMLocking );
2092
2093 methodHandle profiled_method;
2094 #if INCLUDE_JVMCI
2095 if (nm->is_compiled_by_jvmci()) {
2096 profiled_method = methodHandle(current, nm->method());
2097 } else {
2098 profiled_method = trap_method;
2099 }
2100 #else
2101 profiled_method = trap_method;
2102 #endif
2103
2104 MethodData* trap_mdo =
2105 get_method_data(current, profiled_method, create_if_missing);
2106
2107 { // Log Deoptimization event for JFR, UL and event system
2590 bool ignore_maybe_prior_recompile;
2591 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2592 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2593 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2594
2595 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2596 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2597
2598 query_update_method_data(trap_mdo, trap_bci,
2599 (DeoptReason)reason,
2600 update_total_counts,
2601 #if INCLUDE_JVMCI
2602 false,
2603 #endif
2604 nullptr,
2605 ignore_this_trap_count,
2606 ignore_maybe_prior_trap,
2607 ignore_maybe_prior_recompile);
2608 }
2609
2610 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2611 // Enable WXWrite: current function is called from methods compiled by C2 directly
2612 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2613
2614 // Still in Java no safepoints
2615 {
2616 // This enters VM and may safepoint
2617 uncommon_trap_inner(current, trap_request);
2618 }
2619 HandleMark hm(current);
2620 return fetch_unroll_info_helper(current, exec_mode);
2621 PROF_END
2622
2623 // Local derived constants.
2624 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2625 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2626 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2627
2628 //---------------------------trap_state_reason---------------------------------
2629 Deoptimization::DeoptReason
2630 Deoptimization::trap_state_reason(int trap_state) {
2631 // This assert provides the link between the width of DataLayout::trap_bits
2632 // and the encoding of "recorded" reasons. It ensures there are enough
2633 // bits to store all needed reasons in the per-BCI MDO profile.
2634 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2635 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2636 trap_state -= recompile_bit;
2637 if (trap_state == DS_REASON_MASK) {
2638 return Reason_many;
2639 } else {
2640 assert((int)Reason_none == 0, "state=0 => Reason_none");
2641 return (DeoptReason)trap_state;
2791 size_t len;
2792 if (unloaded_class_index < 0) {
2793 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2794 reason, action
2795 #if INCLUDE_JVMCI
2796 ,debug_id
2797 #endif
2798 );
2799 } else {
2800 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2801 reason, action, unloaded_class_index
2802 #if INCLUDE_JVMCI
2803 ,debug_id
2804 #endif
2805 );
2806 }
2807 return buf;
2808 }
2809
2810 juint Deoptimization::_deoptimization_hist
2811 [1 + 4 + 5] // total + online + archived
2812 [Deoptimization::Reason_LIMIT]
2813 [1 + Deoptimization::Action_LIMIT]
2814 [Deoptimization::BC_CASE_LIMIT]
2815 = {0};
2816
2817 enum {
2818 LSB_BITS = 8,
2819 LSB_MASK = right_n_bits(LSB_BITS)
2820 };
2821
2822 static void update(juint* cases, Bytecodes::Code bc) {
2823 juint* bc_counter_addr = nullptr;
2824 juint bc_counter = 0;
2825 // Look for an unused counter, or an exact match to this BC.
2826 if (bc != Bytecodes::_illegal) {
2827 for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2828 juint* counter_addr = &cases[bc_case];
2829 juint counter = *counter_addr;
2830 if ((counter == 0 && bc_counter_addr == nullptr)
2831 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2832 // this counter is either free or is already devoted to this BC
2833 bc_counter_addr = counter_addr;
2834 bc_counter = counter | bc;
2835 }
2836 }
2837 }
2838 if (bc_counter_addr == nullptr) {
2839 // Overflow, or no given bytecode.
2840 bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2841 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2842 }
2843 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2844 }
2845
2846
2847 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2848 Bytecodes::Code bc) {
2849 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2850 assert(action >= 0 && action < Action_LIMIT, "oob");
2851 _deoptimization_hist[0][Reason_none][0][0] += 1; // total
2852 _deoptimization_hist[0][reason][0][0] += 1; // per-reason total
2853
2854 update(_deoptimization_hist[0][reason][1+action], bc);
2855
2856 uint lvl = nm->comp_level() + (nm->is_scc() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2857 _deoptimization_hist[lvl][Reason_none][0][0] += 1; // total
2858 _deoptimization_hist[lvl][reason][0][0] += 1; // per-reason total
2859 update(_deoptimization_hist[lvl][reason][1+action], bc);
2860 }
2861
2862 jint Deoptimization::total_deoptimization_count() {
2863 return _deoptimization_hist[0][Reason_none][0][0];
2864 }
2865
2866 // Get the deopt count for a specific reason and a specific action. If either
2867 // one of 'reason' or 'action' is null, the method returns the sum of all
2868 // deoptimizations with the specific 'action' or 'reason' respectively.
2869 // If both arguments are null, the method returns the total deopt count.
2870 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2871 if (reason_str == nullptr && action_str == nullptr) {
2872 return total_deoptimization_count();
2873 }
2874 juint counter = 0;
2875 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2876 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2877 for (int action = 0; action < Action_LIMIT; action++) {
2878 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2879 juint* cases = _deoptimization_hist[0][reason][1+action];
2880 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2881 counter += cases[bc_case] >> LSB_BITS;
2882 }
2883 }
2884 }
2885 }
2886 }
2887 return counter;
2888 }
2889
2890 void Deoptimization::print_statistics() {
2891 ttyLocker ttyl;
2892 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2893 tty->print_cr("Deoptimization traps recorded:");
2894 print_statistics_on(tty);
2895 if (xtty != nullptr) xtty->tail("statistics");
2896 }
2897
2898 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2899 juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2900 juint account = total;
2901 #define PRINT_STAT_LINE(name, r) \
2902 st->print_cr(" %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2903 if (total > 0) {
2904 st->print(" %s: ", title);
2905 PRINT_STAT_LINE("total", total);
2906 // For each non-zero entry in the histogram, print the reason,
2907 // the action, and (if specifically known) the type of bytecode.
2908 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2909 for (int action = 0; action < Action_LIMIT; action++) {
2910 juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2911 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2912 juint counter = cases[bc_case];
2913 if (counter != 0) {
2914 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2915 const char* bc_name = "other";
2916 if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2917 // overwritten
2918 } else if (Bytecodes::is_defined(bc)) {
2919 bc_name = Bytecodes::name(bc);
2920 }
2921 juint r = counter >> LSB_BITS;
2922 st->print_cr(" %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2923 trap_reason_name(reason), trap_action_name(action), bc_name,
2924 r, (r * 100.0) / total);
2925 account -= r;
2926 }
2927 }
2928 }
2929 }
2930 if (account != 0) {
2931 PRINT_STAT_LINE("unaccounted", account);
2932 }
2933 #undef PRINT_STAT_LINE
2934 }
2935 }
2936
2937 void Deoptimization::print_statistics_on(outputStream* st) {
2938 // print_statistics_on("Total", 0, st);
2939 print_statistics_on("Tier1", 1, st);
2940 print_statistics_on("Tier2", 2, st);
2941 print_statistics_on("Tier3", 3, st);
2942 print_statistics_on("Tier4", 4, st);
2943
2944 print_statistics_on("SC Tier1", 5, st);
2945 print_statistics_on("SC Tier2", 6, st);
2946 print_statistics_on("SC Tier4", 8, st);
2947 print_statistics_on("SC Tier5 (preloaded)", 9, st);
2948 }
2949
2950 #else // COMPILER2_OR_JVMCI
2951
2952
2953 // Stubs for C1 only system.
2954 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2955 return false;
2956 }
2957
2958 const char* Deoptimization::trap_reason_name(int reason) {
2959 return "unknown";
2960 }
2961
2962 jint Deoptimization::total_deoptimization_count() {
2963 return 0;
2964 }
2965
2966 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2967 return 0;
2968 }
2969
2975 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2976 // no update
2977 }
2978
2979 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2980 return 0;
2981 }
2982
2983 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2984 Bytecodes::Code bc) {
2985 // no update
2986 }
2987
2988 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2989 int trap_state) {
2990 jio_snprintf(buf, buflen, "#%d", trap_state);
2991 return buf;
2992 }
2993
2994 #endif // COMPILER2_OR_JVMCI
2995
2996 #define DO_COUNTERS(macro) \
2997 macro(Deoptimization, fetch_unroll_info) \
2998 macro(Deoptimization, unpack_frames) \
2999 macro(Deoptimization, uncommon_trap_inner) \
3000 macro(Deoptimization, uncommon_trap)
3001
3002 #define INIT_COUNTER(sub, name) \
3003 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
3004 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
3005
3006 void Deoptimization::init_counters() {
3007 if (ProfileRuntimeCalls && UsePerfData) {
3008 EXCEPTION_MARK;
3009
3010 DO_COUNTERS(INIT_COUNTER)
3011
3012 if (HAS_PENDING_EXCEPTION) {
3013 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
3014 }
3015 }
3016 }
3017 #undef INIT_COUNTER
3018
3019 #define PRINT_COUNTER(sub, name) { \
3020 jlong count = _perf_##sub##_##name##_count->get_value(); \
3021 if (count > 0) { \
3022 st->print_cr(" %-50s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
3023 _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
3024 _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
3025 count); \
3026 }}
3027
3028 void Deoptimization::print_counters_on(outputStream* st) {
3029 if (ProfileRuntimeCalls && UsePerfData) {
3030 DO_COUNTERS(PRINT_COUNTER)
3031 } else {
3032 st->print_cr(" Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3033 }
3034 }
3035
3036 #undef PRINT_COUNTER
3037 #undef DO_COUNTERS
|