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/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);
116 // If there is nothing to deopt _required_gen is the same as comitted.
264 return checked_cast<int>(result);
265 }
266
267 void Deoptimization::UnrollBlock::print() {
268 ResourceMark rm;
269 stringStream st;
270 st.print_cr("UnrollBlock");
271 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
272 st.print( " frame_sizes: ");
273 for (int index = 0; index < number_of_frames(); index++) {
274 st.print("%zd ", frame_sizes()[index]);
275 }
276 st.cr();
277 tty->print_raw(st.freeze());
278 }
279
280 // In order to make fetch_unroll_info work properly with escape
281 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
282 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
283 // which is called from the method fetch_unroll_info_helper below.
284 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
285 // fetch_unroll_info() is called at the beginning of the deoptimization
286 // handler. Note this fact before we start generating temporary frames
287 // that can confuse an asynchronous stack walker. This counter is
288 // decremented at the end of unpack_frames().
289 current->inc_in_deopt_handler();
290
291 if (exec_mode == Unpack_exception) {
292 // When we get here, a callee has thrown an exception into a deoptimized
293 // frame. That throw might have deferred stack watermark checking until
294 // after unwinding. So we deal with such deferred requests here.
295 StackWatermarkSet::after_unwind(current);
296 }
297
298 return fetch_unroll_info_helper(current, exec_mode);
299 JRT_END
300
301 #if COMPILER2_OR_JVMCI
302 // print information about reallocated objects
303 static void print_objects(JavaThread* deoptee_thread,
304 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
840
841 assert(f->is_interpreted_frame(), "must be interpreted");
842 }
843
844 #ifndef PRODUCT
845 static bool falls_through(Bytecodes::Code bc) {
846 switch (bc) {
847 // List may be incomplete. Here we really only care about bytecodes where compiled code
848 // can deoptimize.
849 case Bytecodes::_goto:
850 case Bytecodes::_goto_w:
851 case Bytecodes::_athrow:
852 return false;
853 default:
854 return true;
855 }
856 }
857 #endif
858
859 // Return BasicType of value being returned
860 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
861 assert(thread == JavaThread::current(), "pre-condition");
862
863 // We are already active in the special DeoptResourceMark any ResourceObj's we
864 // allocate will be freed at the end of the routine.
865
866 // JRT_LEAF methods don't normally allocate handles and there is a
867 // NoHandleMark to enforce that. It is actually safe to use Handles
868 // in a JRT_LEAF method, and sometimes desirable, but to do so we
869 // must use ResetNoHandleMark to bypass the NoHandleMark, and
870 // then use a HandleMark to ensure any Handles we do create are
871 // cleaned up in this scope.
872 ResetNoHandleMark rnhm;
873 HandleMark hm(thread);
874
875 frame stub_frame = thread->last_frame();
876
877 Continuation::notify_deopt(thread, stub_frame.sp());
878
879 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
880 // must point to the vframeArray for the unpack frame.
1753 if (monitors != nullptr) {
1754 // Unlock in reverse order starting from most nested monitor.
1755 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1756 BasicObjectLock* src = monitors->at(j);
1757 if (src->obj() != nullptr) {
1758 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1759 }
1760 }
1761 array->element(i)->free_monitors();
1762 #ifdef ASSERT
1763 array->element(i)->set_removed_monitors();
1764 #endif
1765 }
1766 }
1767 }
1768 #endif
1769
1770 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1771 assert(fr.can_be_deoptimized(), "checking frame type");
1772
1773 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1774
1775 if (LogCompilation && xtty != nullptr) {
1776 nmethod* nm = fr.cb()->as_nmethod_or_null();
1777 assert(nm != nullptr, "only compiled methods can deopt");
1778
1779 ttyLocker ttyl;
1780 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1781 nm->log_identity(xtty);
1782 xtty->end_head();
1783 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1784 xtty->begin_elem("jvms bci='%d'", sd->bci());
1785 xtty->method(sd->method());
1786 xtty->end_elem();
1787 if (sd->is_top()) break;
1788 }
1789 xtty->tail("deoptimized");
1790 }
1791
1792 Continuation::notify_deopt(thread, fr.sp());
1793
1794 // Patch the compiled method so that when execution returns to it we will
1795 // deopt the execution state and return to the interpreter.
1796 fr.deoptimize(thread);
1797 }
1798
1994 event.set_compileId(nm->compile_id());
1995 event.set_compiler(nm->compiler_type());
1996 event.set_method(method);
1997 event.set_lineNumber(method->line_number_from_bci(trap_bci));
1998 event.set_bci(trap_bci);
1999 event.set_instruction(instruction);
2000 event.set_reason(reason);
2001 event.set_action(action);
2002 event.commit();
2003 }
2004 }
2005
2006 #endif // INCLUDE_JFR
2007
2008 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2009 const char* reason_name, const char* reason_action) {
2010 LogTarget(Debug, deoptimization) lt;
2011 if (lt.is_enabled()) {
2012 LogStream ls(lt);
2013 bool is_osr = nm->is_osr_method();
2014 ls.print("cid=%4d %s level=%d",
2015 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
2016 ls.print(" %s", tm->name_and_sig_as_C_string());
2017 ls.print(" trap_bci=%d ", trap_bci);
2018 if (is_osr) {
2019 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2020 }
2021 ls.print("%s ", reason_name);
2022 ls.print("%s ", reason_action);
2023 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2024 pc, fr.pc() - nm->code_begin());
2025 }
2026 }
2027
2028 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2029 HandleMark hm(current);
2030
2031 // uncommon_trap() is called at the beginning of the uncommon trap
2032 // handler. Note this fact before we start generating temporary frames
2033 // that can confuse an asynchronous stack walker. This counter is
2034 // decremented at the end of unpack_frames().
2035
2036 current->inc_in_deopt_handler();
2037
2038 #if INCLUDE_JVMCI
2039 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2040 RegisterMap reg_map(current,
2041 RegisterMap::UpdateMap::include,
2042 RegisterMap::ProcessFrames::include,
2043 RegisterMap::WalkContinuation::skip);
2044 #else
2045 RegisterMap reg_map(current,
2046 RegisterMap::UpdateMap::skip,
2047 RegisterMap::ProcessFrames::include,
2048 RegisterMap::WalkContinuation::skip);
2084 #if INCLUDE_JVMCI
2085 jlong speculation = current->pending_failed_speculation();
2086 if (nm->is_compiled_by_jvmci()) {
2087 nm->update_speculation(current);
2088 } else {
2089 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2090 }
2091
2092 if (trap_bci == SynchronizationEntryBCI) {
2093 trap_bci = 0;
2094 current->set_pending_monitorenter(true);
2095 }
2096
2097 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2098 current->set_pending_transfer_to_interpreter(true);
2099 }
2100 #endif
2101
2102 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2103 // Record this event in the histogram.
2104 gather_statistics(reason, action, trap_bc);
2105
2106 // Ensure that we can record deopt. history:
2107 bool create_if_missing = ProfileTraps;
2108
2109 methodHandle profiled_method;
2110 #if INCLUDE_JVMCI
2111 if (nm->is_compiled_by_jvmci()) {
2112 profiled_method = methodHandle(current, nm->method());
2113 } else {
2114 profiled_method = trap_method;
2115 }
2116 #else
2117 profiled_method = trap_method;
2118 #endif
2119
2120 MethodData* trap_mdo =
2121 get_method_data(current, profiled_method, create_if_missing);
2122
2123 { // Log Deoptimization event for JFR, UL and event system
2124 Method* tm = trap_method();
2595 bool ignore_maybe_prior_recompile;
2596 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2597 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2598 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2599
2600 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2601 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2602
2603 query_update_method_data(trap_mdo, trap_bci,
2604 (DeoptReason)reason,
2605 update_total_counts,
2606 #if INCLUDE_JVMCI
2607 false,
2608 #endif
2609 nullptr,
2610 ignore_this_trap_count,
2611 ignore_maybe_prior_trap,
2612 ignore_maybe_prior_recompile);
2613 }
2614
2615 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2616 // Enable WXWrite: current function is called from methods compiled by C2 directly
2617 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2618
2619 // Still in Java no safepoints
2620 {
2621 // This enters VM and may safepoint
2622 uncommon_trap_inner(current, trap_request);
2623 }
2624 HandleMark hm(current);
2625 return fetch_unroll_info_helper(current, exec_mode);
2626 }
2627
2628 // Local derived constants.
2629 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2630 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2631 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2632
2633 //---------------------------trap_state_reason---------------------------------
2634 Deoptimization::DeoptReason
2635 Deoptimization::trap_state_reason(int trap_state) {
2636 // This assert provides the link between the width of DataLayout::trap_bits
2637 // and the encoding of "recorded" reasons. It ensures there are enough
2638 // bits to store all needed reasons in the per-BCI MDO profile.
2639 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2640 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2641 trap_state -= recompile_bit;
2642 if (trap_state == DS_REASON_MASK) {
2643 return Reason_many;
2644 } else {
2645 assert((int)Reason_none == 0, "state=0 => Reason_none");
2646 return (DeoptReason)trap_state;
2796 size_t len;
2797 if (unloaded_class_index < 0) {
2798 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2799 reason, action
2800 #if INCLUDE_JVMCI
2801 ,debug_id
2802 #endif
2803 );
2804 } else {
2805 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2806 reason, action, unloaded_class_index
2807 #if INCLUDE_JVMCI
2808 ,debug_id
2809 #endif
2810 );
2811 }
2812 return buf;
2813 }
2814
2815 juint Deoptimization::_deoptimization_hist
2816 [Deoptimization::Reason_LIMIT]
2817 [1 + Deoptimization::Action_LIMIT]
2818 [Deoptimization::BC_CASE_LIMIT]
2819 = {0};
2820
2821 enum {
2822 LSB_BITS = 8,
2823 LSB_MASK = right_n_bits(LSB_BITS)
2824 };
2825
2826 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2827 Bytecodes::Code bc) {
2828 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2829 assert(action >= 0 && action < Action_LIMIT, "oob");
2830 _deoptimization_hist[Reason_none][0][0] += 1; // total
2831 _deoptimization_hist[reason][0][0] += 1; // per-reason total
2832 juint* cases = _deoptimization_hist[reason][1+action];
2833 juint* bc_counter_addr = nullptr;
2834 juint bc_counter = 0;
2835 // Look for an unused counter, or an exact match to this BC.
2836 if (bc != Bytecodes::_illegal) {
2837 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2838 juint* counter_addr = &cases[bc_case];
2839 juint counter = *counter_addr;
2840 if ((counter == 0 && bc_counter_addr == nullptr)
2841 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2842 // this counter is either free or is already devoted to this BC
2843 bc_counter_addr = counter_addr;
2844 bc_counter = counter | bc;
2845 }
2846 }
2847 }
2848 if (bc_counter_addr == nullptr) {
2849 // Overflow, or no given bytecode.
2850 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2851 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2852 }
2853 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2854 }
2855
2856 jint Deoptimization::total_deoptimization_count() {
2857 return _deoptimization_hist[Reason_none][0][0];
2858 }
2859
2860 // Get the deopt count for a specific reason and a specific action. If either
2861 // one of 'reason' or 'action' is null, the method returns the sum of all
2862 // deoptimizations with the specific 'action' or 'reason' respectively.
2863 // If both arguments are null, the method returns the total deopt count.
2864 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2865 if (reason_str == nullptr && action_str == nullptr) {
2866 return total_deoptimization_count();
2867 }
2868 juint counter = 0;
2869 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2870 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2871 for (int action = 0; action < Action_LIMIT; action++) {
2872 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2873 juint* cases = _deoptimization_hist[reason][1+action];
2874 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2875 counter += cases[bc_case] >> LSB_BITS;
2876 }
2877 }
2878 }
2879 }
2880 }
2881 return counter;
2882 }
2883
2884 void Deoptimization::print_statistics() {
2885 juint total = total_deoptimization_count();
2886 juint account = total;
2887 if (total != 0) {
2888 ttyLocker ttyl;
2889 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2890 tty->print_cr("Deoptimization traps recorded:");
2891 #define PRINT_STAT_LINE(name, r) \
2892 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2893 PRINT_STAT_LINE("total", total);
2894 // For each non-zero entry in the histogram, print the reason,
2895 // the action, and (if specifically known) the type of bytecode.
2896 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2897 for (int action = 0; action < Action_LIMIT; action++) {
2898 juint* cases = _deoptimization_hist[reason][1+action];
2899 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2900 juint counter = cases[bc_case];
2901 if (counter != 0) {
2902 char name[1*K];
2903 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2904 if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2905 bc = Bytecodes::_illegal;
2906 os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2907 trap_reason_name(reason),
2908 trap_action_name(action),
2909 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2910 juint r = counter >> LSB_BITS;
2911 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2912 account -= r;
2913 }
2914 }
2915 }
2916 }
2917 if (account != 0) {
2918 PRINT_STAT_LINE("unaccounted", account);
2919 }
2920 #undef PRINT_STAT_LINE
2921 if (xtty != nullptr) xtty->tail("statistics");
2922 }
2923 }
2924
2925 #else // COMPILER2_OR_JVMCI
2926
2927
2928 // Stubs for C1 only system.
2929 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2930 return false;
2931 }
2932
2933 const char* Deoptimization::trap_reason_name(int reason) {
2934 return "unknown";
2935 }
2936
2937 jint Deoptimization::total_deoptimization_count() {
2938 return 0;
2939 }
2940
2941 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2942 return 0;
2943 }
2944
2945 void Deoptimization::print_statistics() {
2946 // no output
2947 }
2948
2949 void
2950 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2951 // no update
2952 }
2953
2954 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2955 return 0;
2956 }
2957
2958 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2959 Bytecodes::Code bc) {
2960 // no update
2961 }
2962
2963 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2964 int trap_state) {
2965 jio_snprintf(buf, buflen, "#%d", trap_state);
2966 return buf;
2967 }
2968
2969 #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/jfrEvents.hpp"
107 #include "jfr/metadata/jfrSerializer.hpp"
108 #endif
109
110 uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
111 uint64_t DeoptimizationScope::_active_deopt_gen = 1;
112 bool DeoptimizationScope::_committing_in_progress = false;
113
114 DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
115 DEBUG_ONLY(_deopted = false;)
116
117 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
118 // If there is nothing to deopt _required_gen is the same as comitted.
266 return checked_cast<int>(result);
267 }
268
269 void Deoptimization::UnrollBlock::print() {
270 ResourceMark rm;
271 stringStream st;
272 st.print_cr("UnrollBlock");
273 st.print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
274 st.print( " frame_sizes: ");
275 for (int index = 0; index < number_of_frames(); index++) {
276 st.print("%zd ", frame_sizes()[index]);
277 }
278 st.cr();
279 tty->print_raw(st.freeze());
280 }
281
282 // In order to make fetch_unroll_info work properly with escape
283 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
284 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
285 // which is called from the method fetch_unroll_info_helper below.
286 JRT_BLOCK_ENTRY_PROF(Deoptimization::UnrollBlock*, Deoptimization, fetch_unroll_info, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
287 // fetch_unroll_info() is called at the beginning of the deoptimization
288 // handler. Note this fact before we start generating temporary frames
289 // that can confuse an asynchronous stack walker. This counter is
290 // decremented at the end of unpack_frames().
291 current->inc_in_deopt_handler();
292
293 if (exec_mode == Unpack_exception) {
294 // When we get here, a callee has thrown an exception into a deoptimized
295 // frame. That throw might have deferred stack watermark checking until
296 // after unwinding. So we deal with such deferred requests here.
297 StackWatermarkSet::after_unwind(current);
298 }
299
300 return fetch_unroll_info_helper(current, exec_mode);
301 JRT_END
302
303 #if COMPILER2_OR_JVMCI
304 // print information about reallocated objects
305 static void print_objects(JavaThread* deoptee_thread,
306 GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
842
843 assert(f->is_interpreted_frame(), "must be interpreted");
844 }
845
846 #ifndef PRODUCT
847 static bool falls_through(Bytecodes::Code bc) {
848 switch (bc) {
849 // List may be incomplete. Here we really only care about bytecodes where compiled code
850 // can deoptimize.
851 case Bytecodes::_goto:
852 case Bytecodes::_goto_w:
853 case Bytecodes::_athrow:
854 return false;
855 default:
856 return true;
857 }
858 }
859 #endif
860
861 // Return BasicType of value being returned
862 JRT_LEAF_PROF_NO_THREAD(BasicType, Deoptimization, unpack_frames, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
863 assert(thread == JavaThread::current(), "pre-condition");
864
865 // We are already active in the special DeoptResourceMark any ResourceObj's we
866 // allocate will be freed at the end of the routine.
867
868 // JRT_LEAF methods don't normally allocate handles and there is a
869 // NoHandleMark to enforce that. It is actually safe to use Handles
870 // in a JRT_LEAF method, and sometimes desirable, but to do so we
871 // must use ResetNoHandleMark to bypass the NoHandleMark, and
872 // then use a HandleMark to ensure any Handles we do create are
873 // cleaned up in this scope.
874 ResetNoHandleMark rnhm;
875 HandleMark hm(thread);
876
877 frame stub_frame = thread->last_frame();
878
879 Continuation::notify_deopt(thread, stub_frame.sp());
880
881 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
882 // must point to the vframeArray for the unpack frame.
1755 if (monitors != nullptr) {
1756 // Unlock in reverse order starting from most nested monitor.
1757 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1758 BasicObjectLock* src = monitors->at(j);
1759 if (src->obj() != nullptr) {
1760 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1761 }
1762 }
1763 array->element(i)->free_monitors();
1764 #ifdef ASSERT
1765 array->element(i)->set_removed_monitors();
1766 #endif
1767 }
1768 }
1769 }
1770 #endif
1771
1772 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1773 assert(fr.can_be_deoptimized(), "checking frame type");
1774
1775 nmethod* nm = fr.cb()->as_nmethod_or_null();
1776 assert(nm != nullptr, "only compiled methods can deopt");
1777 DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1778 ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1779 Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1780 : cur_sd->method()->java_code_at(cur_sd->bci()));
1781 gather_statistics(nm, reason, action, bc);
1782
1783 if (LogCompilation && xtty != nullptr) {
1784 ttyLocker ttyl;
1785 xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1786 nm->log_identity(xtty);
1787 xtty->end_head();
1788 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1789 xtty->begin_elem("jvms bci='%d'", sd->bci());
1790 xtty->method(sd->method());
1791 xtty->end_elem();
1792 if (sd->is_top()) break;
1793 }
1794 xtty->tail("deoptimized");
1795 }
1796
1797 Continuation::notify_deopt(thread, fr.sp());
1798
1799 // Patch the compiled method so that when execution returns to it we will
1800 // deopt the execution state and return to the interpreter.
1801 fr.deoptimize(thread);
1802 }
1803
1999 event.set_compileId(nm->compile_id());
2000 event.set_compiler(nm->compiler_type());
2001 event.set_method(method);
2002 event.set_lineNumber(method->line_number_from_bci(trap_bci));
2003 event.set_bci(trap_bci);
2004 event.set_instruction(instruction);
2005 event.set_reason(reason);
2006 event.set_action(action);
2007 event.commit();
2008 }
2009 }
2010
2011 #endif // INCLUDE_JFR
2012
2013 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
2014 const char* reason_name, const char* reason_action) {
2015 LogTarget(Debug, deoptimization) lt;
2016 if (lt.is_enabled()) {
2017 LogStream ls(lt);
2018 bool is_osr = nm->is_osr_method();
2019 ls.print("cid=%4d %s%s level=%d",
2020 nm->compile_id(), (is_osr ? "osr" : " "), (nm->preloaded() ? "preload" : ""), nm->comp_level());
2021 ls.print(" %s", tm->name_and_sig_as_C_string());
2022 ls.print(" trap_bci=%d ", trap_bci);
2023 if (is_osr) {
2024 ls.print("osr_bci=%d ", nm->osr_entry_bci());
2025 }
2026 ls.print("%s ", reason_name);
2027 ls.print("%s ", reason_action);
2028 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2029 pc, fr.pc() - nm->code_begin());
2030 }
2031 }
2032
2033 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2034 HandleMark hm(current);
2035
2036 // uncommon_trap() is called at the beginning of the uncommon trap
2037 // handler. Note this fact before we start generating temporary frames
2038 // that can confuse an asynchronous stack walker. This counter is
2039 // decremented at the end of unpack_frames().
2040
2041 current->inc_in_deopt_handler();
2042
2043 #if INCLUDE_JVMCI
2044 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2045 RegisterMap reg_map(current,
2046 RegisterMap::UpdateMap::include,
2047 RegisterMap::ProcessFrames::include,
2048 RegisterMap::WalkContinuation::skip);
2049 #else
2050 RegisterMap reg_map(current,
2051 RegisterMap::UpdateMap::skip,
2052 RegisterMap::ProcessFrames::include,
2053 RegisterMap::WalkContinuation::skip);
2089 #if INCLUDE_JVMCI
2090 jlong speculation = current->pending_failed_speculation();
2091 if (nm->is_compiled_by_jvmci()) {
2092 nm->update_speculation(current);
2093 } else {
2094 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2095 }
2096
2097 if (trap_bci == SynchronizationEntryBCI) {
2098 trap_bci = 0;
2099 current->set_pending_monitorenter(true);
2100 }
2101
2102 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2103 current->set_pending_transfer_to_interpreter(true);
2104 }
2105 #endif
2106
2107 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2108 // Record this event in the histogram.
2109 gather_statistics(nm, reason, action, trap_bc);
2110
2111 // Ensure that we can record deopt. history:
2112 bool create_if_missing = ProfileTraps;
2113
2114 methodHandle profiled_method;
2115 #if INCLUDE_JVMCI
2116 if (nm->is_compiled_by_jvmci()) {
2117 profiled_method = methodHandle(current, nm->method());
2118 } else {
2119 profiled_method = trap_method;
2120 }
2121 #else
2122 profiled_method = trap_method;
2123 #endif
2124
2125 MethodData* trap_mdo =
2126 get_method_data(current, profiled_method, create_if_missing);
2127
2128 { // Log Deoptimization event for JFR, UL and event system
2129 Method* tm = trap_method();
2600 bool ignore_maybe_prior_recompile;
2601 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2602 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2603 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2604
2605 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2606 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2607
2608 query_update_method_data(trap_mdo, trap_bci,
2609 (DeoptReason)reason,
2610 update_total_counts,
2611 #if INCLUDE_JVMCI
2612 false,
2613 #endif
2614 nullptr,
2615 ignore_this_trap_count,
2616 ignore_maybe_prior_trap,
2617 ignore_maybe_prior_recompile);
2618 }
2619
2620 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2621 // Enable WXWrite: current function is called from methods compiled by C2 directly
2622 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2623
2624 // Still in Java no safepoints
2625 {
2626 // This enters VM and may safepoint
2627 uncommon_trap_inner(current, trap_request);
2628 }
2629 HandleMark hm(current);
2630 return fetch_unroll_info_helper(current, exec_mode);
2631 PROF_END
2632
2633 // Local derived constants.
2634 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2635 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2636 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2637
2638 //---------------------------trap_state_reason---------------------------------
2639 Deoptimization::DeoptReason
2640 Deoptimization::trap_state_reason(int trap_state) {
2641 // This assert provides the link between the width of DataLayout::trap_bits
2642 // and the encoding of "recorded" reasons. It ensures there are enough
2643 // bits to store all needed reasons in the per-BCI MDO profile.
2644 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2645 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2646 trap_state -= recompile_bit;
2647 if (trap_state == DS_REASON_MASK) {
2648 return Reason_many;
2649 } else {
2650 assert((int)Reason_none == 0, "state=0 => Reason_none");
2651 return (DeoptReason)trap_state;
2801 size_t len;
2802 if (unloaded_class_index < 0) {
2803 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2804 reason, action
2805 #if INCLUDE_JVMCI
2806 ,debug_id
2807 #endif
2808 );
2809 } else {
2810 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2811 reason, action, unloaded_class_index
2812 #if INCLUDE_JVMCI
2813 ,debug_id
2814 #endif
2815 );
2816 }
2817 return buf;
2818 }
2819
2820 juint Deoptimization::_deoptimization_hist
2821 [1 + 4 + 5] // total + online + archived
2822 [Deoptimization::Reason_LIMIT]
2823 [1 + Deoptimization::Action_LIMIT]
2824 [Deoptimization::BC_CASE_LIMIT]
2825 = {0};
2826
2827 enum {
2828 LSB_BITS = 8,
2829 LSB_MASK = right_n_bits(LSB_BITS)
2830 };
2831
2832 static void update(juint* cases, Bytecodes::Code bc) {
2833 juint* bc_counter_addr = nullptr;
2834 juint bc_counter = 0;
2835 // Look for an unused counter, or an exact match to this BC.
2836 if (bc != Bytecodes::_illegal) {
2837 for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2838 juint* counter_addr = &cases[bc_case];
2839 juint counter = *counter_addr;
2840 if ((counter == 0 && bc_counter_addr == nullptr)
2841 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2842 // this counter is either free or is already devoted to this BC
2843 bc_counter_addr = counter_addr;
2844 bc_counter = counter | bc;
2845 }
2846 }
2847 }
2848 if (bc_counter_addr == nullptr) {
2849 // Overflow, or no given bytecode.
2850 bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2851 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2852 }
2853 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2854 }
2855
2856
2857 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2858 Bytecodes::Code bc) {
2859 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2860 assert(action >= 0 && action < Action_LIMIT, "oob");
2861 _deoptimization_hist[0][Reason_none][0][0] += 1; // total
2862 _deoptimization_hist[0][reason][0][0] += 1; // per-reason total
2863
2864 update(_deoptimization_hist[0][reason][1+action], bc);
2865
2866 uint lvl = nm->comp_level() + (nm->is_scc() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2867 _deoptimization_hist[lvl][Reason_none][0][0] += 1; // total
2868 _deoptimization_hist[lvl][reason][0][0] += 1; // per-reason total
2869 update(_deoptimization_hist[lvl][reason][1+action], bc);
2870 }
2871
2872 jint Deoptimization::total_deoptimization_count() {
2873 return _deoptimization_hist[0][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[0][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 ttyLocker ttyl;
2902 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2903 tty->print_cr("Deoptimization traps recorded:");
2904 print_statistics_on(tty);
2905 if (xtty != nullptr) xtty->tail("statistics");
2906 }
2907
2908 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2909 juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2910 juint account = total;
2911 #define PRINT_STAT_LINE(name, r) \
2912 st->print_cr(" %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2913 if (total > 0) {
2914 st->print(" %s: ", title);
2915 PRINT_STAT_LINE("total", total);
2916 // For each non-zero entry in the histogram, print the reason,
2917 // the action, and (if specifically known) the type of bytecode.
2918 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2919 for (int action = 0; action < Action_LIMIT; action++) {
2920 juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2921 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2922 juint counter = cases[bc_case];
2923 if (counter != 0) {
2924 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2925 const char* bc_name = "other";
2926 if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2927 // overwritten
2928 } else if (Bytecodes::is_defined(bc)) {
2929 bc_name = Bytecodes::name(bc);
2930 }
2931 juint r = counter >> LSB_BITS;
2932 st->print_cr(" %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2933 trap_reason_name(reason), trap_action_name(action), bc_name,
2934 r, (r * 100.0) / total);
2935 account -= r;
2936 }
2937 }
2938 }
2939 }
2940 if (account != 0) {
2941 PRINT_STAT_LINE("unaccounted", account);
2942 }
2943 #undef PRINT_STAT_LINE
2944 }
2945 }
2946
2947 void Deoptimization::print_statistics_on(outputStream* st) {
2948 // print_statistics_on("Total", 0, st);
2949 print_statistics_on("Tier1", 1, st);
2950 print_statistics_on("Tier2", 2, st);
2951 print_statistics_on("Tier3", 3, st);
2952 print_statistics_on("Tier4", 4, st);
2953
2954 print_statistics_on("SC Tier1", 5, st);
2955 print_statistics_on("SC Tier2", 6, st);
2956 print_statistics_on("SC Tier4", 8, st);
2957 print_statistics_on("SC Tier5 (preloaded)", 9, st);
2958 }
2959
2960 #define DO_COUNTERS(macro) \
2961 macro(Deoptimization, fetch_unroll_info) \
2962 macro(Deoptimization, unpack_frames) \
2963 macro(Deoptimization, uncommon_trap_inner) \
2964 macro(Deoptimization, uncommon_trap)
2965
2966 #define INIT_COUNTER(sub, name) \
2967 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2968 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2969
2970 void Deoptimization::init_counters() {
2971 if (ProfileRuntimeCalls && UsePerfData) {
2972 EXCEPTION_MARK;
2973
2974 DO_COUNTERS(INIT_COUNTER)
2975
2976 if (HAS_PENDING_EXCEPTION) {
2977 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2978 }
2979 }
2980 }
2981 #undef INIT_COUNTER
2982
2983 #define PRINT_COUNTER(sub, name) { \
2984 jlong count = _perf_##sub##_##name##_count->get_value(); \
2985 if (count > 0) { \
2986 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2987 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2988 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2989 count); \
2990 }}
2991
2992 void Deoptimization::print_counters_on(outputStream* st) {
2993 if (ProfileRuntimeCalls && UsePerfData) {
2994 DO_COUNTERS(PRINT_COUNTER)
2995 } else {
2996 st->print_cr(" Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
2997 }
2998 }
2999
3000 #undef PRINT_COUNTER
3001 #undef DO_COUNTERS
3002
3003 #else // COMPILER2_OR_JVMCI
3004
3005
3006 // Stubs for C1 only system.
3007 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
3008 return false;
3009 }
3010
3011 const char* Deoptimization::trap_reason_name(int reason) {
3012 return "unknown";
3013 }
3014
3015 jint Deoptimization::total_deoptimization_count() {
3016 return 0;
3017 }
3018
3019 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
3020 return 0;
3021 }
3022
3023 void Deoptimization::print_statistics() {
3024 // no output
3025 }
3026
3027 void
3028 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
3029 // no update
3030 }
3031
3032 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
3033 return 0;
3034 }
3035
3036 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
3037 Bytecodes::Code bc) {
3038 // no update
3039 }
3040
3041 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
3042 int trap_state) {
3043 jio_snprintf(buf, buflen, "#%d", trap_state);
3044 return buf;
3045 }
3046
3047 void Deoptimization::init_counters() {
3048 // nothing to do
3049 }
3050
3051 void Deoptimization::print_counters_on(outputStream* st) {
3052 // no output
3053 }
3054
3055 void Deoptimization::print_statistics_on(outputStream* st) {
3056 // no output
3057 }
3058
3059 #endif // COMPILER2_OR_JVMCI
|