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.
1725 if (monitors != nullptr) {
1726 // Unlock in reverse order starting from most nested monitor.
1727 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1728 BasicObjectLock* src = monitors->at(j);
1729 if (src->obj() != nullptr) {
1730 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1731 }
1732 }
1733 array->element(i)->free_monitors();
1734 #ifdef ASSERT
1735 array->element(i)->set_removed_monitors();
1736 #endif
1737 }
1738 }
1739 }
1740 #endif
1741
1742 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1743 assert(fr.can_be_deoptimized(), "checking frame type");
1744
1745 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1746
1747 if (LogCompilation && xtty != nullptr) {
1748 nmethod* nm = fr.cb()->as_nmethod_or_null();
1749 assert(nm != nullptr, "only compiled methods can deopt");
1750
1751 ttyLocker ttyl;
1752 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1753 nm->log_identity(xtty);
1754 xtty->end_head();
1755 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1756 xtty->begin_elem("jvms bci='%d'", sd->bci());
1757 xtty->method(sd->method());
1758 xtty->end_elem();
1759 if (sd->is_top()) break;
1760 }
1761 xtty->tail("deoptimized");
1762 }
1763
1764 Continuation::notify_deopt(thread, fr.sp());
1765
1766 // Patch the compiled method so that when execution returns to it we will
1767 // deopt the execution state and return to the interpreter.
1768 fr.deoptimize(thread);
1769 }
1770
1980 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1981 const char* reason_name, const char* reason_action) {
1982 LogTarget(Debug, deoptimization) lt;
1983 if (lt.is_enabled()) {
1984 LogStream ls(lt);
1985 bool is_osr = nm->is_osr_method();
1986 ls.print("cid=%4d %s level=%d",
1987 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
1988 ls.print(" %s", tm->name_and_sig_as_C_string());
1989 ls.print(" trap_bci=%d ", trap_bci);
1990 if (is_osr) {
1991 ls.print("osr_bci=%d ", nm->osr_entry_bci());
1992 }
1993 ls.print("%s ", reason_name);
1994 ls.print("%s ", reason_action);
1995 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
1996 pc, fr.pc() - nm->code_begin());
1997 }
1998 }
1999
2000 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2001 HandleMark hm(current);
2002
2003 // uncommon_trap() is called at the beginning of the uncommon trap
2004 // handler. Note this fact before we start generating temporary frames
2005 // that can confuse an asynchronous stack walker. This counter is
2006 // decremented at the end of unpack_frames().
2007
2008 current->inc_in_deopt_handler();
2009
2010 #if INCLUDE_JVMCI
2011 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2012 RegisterMap reg_map(current,
2013 RegisterMap::UpdateMap::include,
2014 RegisterMap::ProcessFrames::include,
2015 RegisterMap::WalkContinuation::skip);
2016 #else
2017 RegisterMap reg_map(current,
2018 RegisterMap::UpdateMap::skip,
2019 RegisterMap::ProcessFrames::include,
2020 RegisterMap::WalkContinuation::skip);
2056 #if INCLUDE_JVMCI
2057 jlong speculation = current->pending_failed_speculation();
2058 if (nm->is_compiled_by_jvmci()) {
2059 nm->update_speculation(current);
2060 } else {
2061 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2062 }
2063
2064 if (trap_bci == SynchronizationEntryBCI) {
2065 trap_bci = 0;
2066 current->set_pending_monitorenter(true);
2067 }
2068
2069 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2070 current->set_pending_transfer_to_interpreter(true);
2071 }
2072 #endif
2073
2074 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2075 // Record this event in the histogram.
2076 gather_statistics(reason, action, trap_bc);
2077
2078 // Ensure that we can record deopt. history:
2079 bool create_if_missing = ProfileTraps;
2080
2081 methodHandle profiled_method;
2082 #if INCLUDE_JVMCI
2083 if (nm->is_compiled_by_jvmci()) {
2084 profiled_method = methodHandle(current, nm->method());
2085 } else {
2086 profiled_method = trap_method;
2087 }
2088 #else
2089 profiled_method = trap_method;
2090 #endif
2091
2092 MethodData* trap_mdo =
2093 get_method_data(current, profiled_method, create_if_missing);
2094
2095 { // Log Deoptimization event for JFR, UL and event system
2096 Method* tm = trap_method();
2568 bool ignore_maybe_prior_recompile;
2569 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2570 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2571 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2572
2573 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2574 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2575
2576 query_update_method_data(trap_mdo, trap_bci,
2577 (DeoptReason)reason,
2578 update_total_counts,
2579 #if INCLUDE_JVMCI
2580 false,
2581 #endif
2582 nullptr,
2583 ignore_this_trap_count,
2584 ignore_maybe_prior_trap,
2585 ignore_maybe_prior_recompile);
2586 }
2587
2588 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2589 // Enable WXWrite: current function is called from methods compiled by C2 directly
2590 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2591
2592 // Still in Java no safepoints
2593 {
2594 // This enters VM and may safepoint
2595 uncommon_trap_inner(current, trap_request);
2596 }
2597 HandleMark hm(current);
2598 return fetch_unroll_info_helper(current, exec_mode);
2599 }
2600
2601 // Local derived constants.
2602 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2603 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2604 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2605
2606 //---------------------------trap_state_reason---------------------------------
2607 Deoptimization::DeoptReason
2608 Deoptimization::trap_state_reason(int trap_state) {
2609 // This assert provides the link between the width of DataLayout::trap_bits
2610 // and the encoding of "recorded" reasons. It ensures there are enough
2611 // bits to store all needed reasons in the per-BCI MDO profile.
2612 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2613 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2614 trap_state -= recompile_bit;
2615 if (trap_state == DS_REASON_MASK) {
2616 return Reason_many;
2617 } else {
2618 assert((int)Reason_none == 0, "state=0 => Reason_none");
2619 return (DeoptReason)trap_state;
2768 size_t len;
2769 if (unloaded_class_index < 0) {
2770 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2771 reason, action
2772 #if INCLUDE_JVMCI
2773 ,debug_id
2774 #endif
2775 );
2776 } else {
2777 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2778 reason, action, unloaded_class_index
2779 #if INCLUDE_JVMCI
2780 ,debug_id
2781 #endif
2782 );
2783 }
2784 return buf;
2785 }
2786
2787 juint Deoptimization::_deoptimization_hist
2788 [Deoptimization::Reason_LIMIT]
2789 [1 + Deoptimization::Action_LIMIT]
2790 [Deoptimization::BC_CASE_LIMIT]
2791 = {0};
2792
2793 enum {
2794 LSB_BITS = 8,
2795 LSB_MASK = right_n_bits(LSB_BITS)
2796 };
2797
2798 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2799 Bytecodes::Code bc) {
2800 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2801 assert(action >= 0 && action < Action_LIMIT, "oob");
2802 _deoptimization_hist[Reason_none][0][0] += 1; // total
2803 _deoptimization_hist[reason][0][0] += 1; // per-reason total
2804 juint* cases = _deoptimization_hist[reason][1+action];
2805 juint* bc_counter_addr = nullptr;
2806 juint bc_counter = 0;
2807 // Look for an unused counter, or an exact match to this BC.
2808 if (bc != Bytecodes::_illegal) {
2809 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2810 juint* counter_addr = &cases[bc_case];
2811 juint counter = *counter_addr;
2812 if ((counter == 0 && bc_counter_addr == nullptr)
2813 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2814 // this counter is either free or is already devoted to this BC
2815 bc_counter_addr = counter_addr;
2816 bc_counter = counter | bc;
2817 }
2818 }
2819 }
2820 if (bc_counter_addr == nullptr) {
2821 // Overflow, or no given bytecode.
2822 bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2823 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2824 }
2825 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2826 }
2827
2828 jint Deoptimization::total_deoptimization_count() {
2829 return _deoptimization_hist[Reason_none][0][0];
2830 }
2831
2832 // Get the deopt count for a specific reason and a specific action. If either
2833 // one of 'reason' or 'action' is null, the method returns the sum of all
2834 // deoptimizations with the specific 'action' or 'reason' respectively.
2835 // If both arguments are null, the method returns the total deopt count.
2836 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2837 if (reason_str == nullptr && action_str == nullptr) {
2838 return total_deoptimization_count();
2839 }
2840 juint counter = 0;
2841 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2842 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2843 for (int action = 0; action < Action_LIMIT; action++) {
2844 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2845 juint* cases = _deoptimization_hist[reason][1+action];
2846 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2847 counter += cases[bc_case] >> LSB_BITS;
2848 }
2849 }
2850 }
2851 }
2852 }
2853 return counter;
2854 }
2855
2856 void Deoptimization::print_statistics() {
2857 juint total = total_deoptimization_count();
2858 juint account = total;
2859 if (total != 0) {
2860 ttyLocker ttyl;
2861 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2862 tty->print_cr("Deoptimization traps recorded:");
2863 #define PRINT_STAT_LINE(name, r) \
2864 tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2865 PRINT_STAT_LINE("total", total);
2866 // For each non-zero entry in the histogram, print the reason,
2867 // the action, and (if specifically known) the type of bytecode.
2868 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2869 for (int action = 0; action < Action_LIMIT; action++) {
2870 juint* cases = _deoptimization_hist[reason][1+action];
2871 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2872 juint counter = cases[bc_case];
2873 if (counter != 0) {
2874 char name[1*K];
2875 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2876 if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2877 bc = Bytecodes::_illegal;
2878 os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
2879 trap_reason_name(reason),
2880 trap_action_name(action),
2881 Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2882 juint r = counter >> LSB_BITS;
2883 tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2884 account -= r;
2885 }
2886 }
2887 }
2888 }
2889 if (account != 0) {
2890 PRINT_STAT_LINE("unaccounted", account);
2891 }
2892 #undef PRINT_STAT_LINE
2893 if (xtty != nullptr) xtty->tail("statistics");
2894 }
2895 }
2896
2897 #else // COMPILER2_OR_JVMCI
2898
2899
2900 // Stubs for C1 only system.
2901 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2902 return false;
2903 }
2904
2905 const char* Deoptimization::trap_reason_name(int reason) {
2906 return "unknown";
2907 }
2908
2909 jint Deoptimization::total_deoptimization_count() {
2910 return 0;
2911 }
2912
2913 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2914 return 0;
2915 }
2916
2922 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2923 // no update
2924 }
2925
2926 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2927 return 0;
2928 }
2929
2930 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2931 Bytecodes::Code bc) {
2932 // no update
2933 }
2934
2935 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2936 int trap_state) {
2937 jio_snprintf(buf, buflen, "#%d", trap_state);
2938 return buf;
2939 }
2940
2941 #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.
1727 if (monitors != nullptr) {
1728 // Unlock in reverse order starting from most nested monitor.
1729 for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
1730 BasicObjectLock* src = monitors->at(j);
1731 if (src->obj() != nullptr) {
1732 ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1733 }
1734 }
1735 array->element(i)->free_monitors();
1736 #ifdef ASSERT
1737 array->element(i)->set_removed_monitors();
1738 #endif
1739 }
1740 }
1741 }
1742 #endif
1743
1744 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1745 assert(fr.can_be_deoptimized(), "checking frame type");
1746
1747 nmethod* nm = fr.cb()->as_nmethod_or_null();
1748 assert(nm != nullptr, "only compiled methods can deopt");
1749 DeoptAction action = (nm->is_not_entrant() ? Action_make_not_entrant : Action_none);
1750 ScopeDesc* cur_sd = nm->scope_desc_at(fr.pc());
1751 Bytecodes::Code bc = (cur_sd->bci() == -1 ? Bytecodes::_nop // deopt on method entry
1752 : cur_sd->method()->java_code_at(cur_sd->bci()));
1753 gather_statistics(nm, reason, action, bc);
1754
1755 if (LogCompilation && xtty != nullptr) {
1756 ttyLocker ttyl;
1757 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1758 nm->log_identity(xtty);
1759 xtty->end_head();
1760 for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1761 xtty->begin_elem("jvms bci='%d'", sd->bci());
1762 xtty->method(sd->method());
1763 xtty->end_elem();
1764 if (sd->is_top()) break;
1765 }
1766 xtty->tail("deoptimized");
1767 }
1768
1769 Continuation::notify_deopt(thread, fr.sp());
1770
1771 // Patch the compiled method so that when execution returns to it we will
1772 // deopt the execution state and return to the interpreter.
1773 fr.deoptimize(thread);
1774 }
1775
1985 static void log_deopt(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
1986 const char* reason_name, const char* reason_action) {
1987 LogTarget(Debug, deoptimization) lt;
1988 if (lt.is_enabled()) {
1989 LogStream ls(lt);
1990 bool is_osr = nm->is_osr_method();
1991 ls.print("cid=%4d %s level=%d",
1992 nm->compile_id(), (is_osr ? "osr" : " "), nm->comp_level());
1993 ls.print(" %s", tm->name_and_sig_as_C_string());
1994 ls.print(" trap_bci=%d ", trap_bci);
1995 if (is_osr) {
1996 ls.print("osr_bci=%d ", nm->osr_entry_bci());
1997 }
1998 ls.print("%s ", reason_name);
1999 ls.print("%s ", reason_action);
2000 ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
2001 pc, fr.pc() - nm->code_begin());
2002 }
2003 }
2004
2005 JRT_ENTRY_PROF(void, Deoptimization, uncommon_trap_inner, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
2006 HandleMark hm(current);
2007
2008 // uncommon_trap() is called at the beginning of the uncommon trap
2009 // handler. Note this fact before we start generating temporary frames
2010 // that can confuse an asynchronous stack walker. This counter is
2011 // decremented at the end of unpack_frames().
2012
2013 current->inc_in_deopt_handler();
2014
2015 #if INCLUDE_JVMCI
2016 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
2017 RegisterMap reg_map(current,
2018 RegisterMap::UpdateMap::include,
2019 RegisterMap::ProcessFrames::include,
2020 RegisterMap::WalkContinuation::skip);
2021 #else
2022 RegisterMap reg_map(current,
2023 RegisterMap::UpdateMap::skip,
2024 RegisterMap::ProcessFrames::include,
2025 RegisterMap::WalkContinuation::skip);
2061 #if INCLUDE_JVMCI
2062 jlong speculation = current->pending_failed_speculation();
2063 if (nm->is_compiled_by_jvmci()) {
2064 nm->update_speculation(current);
2065 } else {
2066 assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2067 }
2068
2069 if (trap_bci == SynchronizationEntryBCI) {
2070 trap_bci = 0;
2071 current->set_pending_monitorenter(true);
2072 }
2073
2074 if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2075 current->set_pending_transfer_to_interpreter(true);
2076 }
2077 #endif
2078
2079 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
2080 // Record this event in the histogram.
2081 gather_statistics(nm, reason, action, trap_bc);
2082
2083 // Ensure that we can record deopt. history:
2084 bool create_if_missing = ProfileTraps;
2085
2086 methodHandle profiled_method;
2087 #if INCLUDE_JVMCI
2088 if (nm->is_compiled_by_jvmci()) {
2089 profiled_method = methodHandle(current, nm->method());
2090 } else {
2091 profiled_method = trap_method;
2092 }
2093 #else
2094 profiled_method = trap_method;
2095 #endif
2096
2097 MethodData* trap_mdo =
2098 get_method_data(current, profiled_method, create_if_missing);
2099
2100 { // Log Deoptimization event for JFR, UL and event system
2101 Method* tm = trap_method();
2573 bool ignore_maybe_prior_recompile;
2574 assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2575 // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2576 bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2577
2578 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
2579 MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
2580
2581 query_update_method_data(trap_mdo, trap_bci,
2582 (DeoptReason)reason,
2583 update_total_counts,
2584 #if INCLUDE_JVMCI
2585 false,
2586 #endif
2587 nullptr,
2588 ignore_this_trap_count,
2589 ignore_maybe_prior_trap,
2590 ignore_maybe_prior_recompile);
2591 }
2592
2593 PROF_ENTRY(Deoptimization::UnrollBlock*, Deoptimization, uncommon_trap, Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode))
2594 // Enable WXWrite: current function is called from methods compiled by C2 directly
2595 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2596
2597 // Still in Java no safepoints
2598 {
2599 // This enters VM and may safepoint
2600 uncommon_trap_inner(current, trap_request);
2601 }
2602 HandleMark hm(current);
2603 return fetch_unroll_info_helper(current, exec_mode);
2604 PROF_END
2605
2606 // Local derived constants.
2607 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2608 const int DS_REASON_MASK = ((uint)DataLayout::trap_mask) >> 1;
2609 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2610
2611 //---------------------------trap_state_reason---------------------------------
2612 Deoptimization::DeoptReason
2613 Deoptimization::trap_state_reason(int trap_state) {
2614 // This assert provides the link between the width of DataLayout::trap_bits
2615 // and the encoding of "recorded" reasons. It ensures there are enough
2616 // bits to store all needed reasons in the per-BCI MDO profile.
2617 assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2618 int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2619 trap_state -= recompile_bit;
2620 if (trap_state == DS_REASON_MASK) {
2621 return Reason_many;
2622 } else {
2623 assert((int)Reason_none == 0, "state=0 => Reason_none");
2624 return (DeoptReason)trap_state;
2773 size_t len;
2774 if (unloaded_class_index < 0) {
2775 len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2776 reason, action
2777 #if INCLUDE_JVMCI
2778 ,debug_id
2779 #endif
2780 );
2781 } else {
2782 len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2783 reason, action, unloaded_class_index
2784 #if INCLUDE_JVMCI
2785 ,debug_id
2786 #endif
2787 );
2788 }
2789 return buf;
2790 }
2791
2792 juint Deoptimization::_deoptimization_hist
2793 [1 + 4 + 5] // total + online + archived
2794 [Deoptimization::Reason_LIMIT]
2795 [1 + Deoptimization::Action_LIMIT]
2796 [Deoptimization::BC_CASE_LIMIT]
2797 = {0};
2798
2799 enum {
2800 LSB_BITS = 8,
2801 LSB_MASK = right_n_bits(LSB_BITS)
2802 };
2803
2804 static void update(juint* cases, Bytecodes::Code bc) {
2805 juint* bc_counter_addr = nullptr;
2806 juint bc_counter = 0;
2807 // Look for an unused counter, or an exact match to this BC.
2808 if (bc != Bytecodes::_illegal) {
2809 for (int bc_case = 0; bc_case < Deoptimization::BC_CASE_LIMIT; bc_case++) {
2810 juint* counter_addr = &cases[bc_case];
2811 juint counter = *counter_addr;
2812 if ((counter == 0 && bc_counter_addr == nullptr)
2813 || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2814 // this counter is either free or is already devoted to this BC
2815 bc_counter_addr = counter_addr;
2816 bc_counter = counter | bc;
2817 }
2818 }
2819 }
2820 if (bc_counter_addr == nullptr) {
2821 // Overflow, or no given bytecode.
2822 bc_counter_addr = &cases[Deoptimization::BC_CASE_LIMIT-1];
2823 bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB
2824 }
2825 *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2826 }
2827
2828
2829 void Deoptimization::gather_statistics(nmethod* nm, DeoptReason reason, DeoptAction action,
2830 Bytecodes::Code bc) {
2831 assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2832 assert(action >= 0 && action < Action_LIMIT, "oob");
2833 _deoptimization_hist[0][Reason_none][0][0] += 1; // total
2834 _deoptimization_hist[0][reason][0][0] += 1; // per-reason total
2835
2836 update(_deoptimization_hist[0][reason][1+action], bc);
2837
2838 uint lvl = nm->comp_level() + (nm->is_scc() ? 4 : 0) + (nm->preloaded() ? 1 : 0);
2839 _deoptimization_hist[lvl][Reason_none][0][0] += 1; // total
2840 _deoptimization_hist[lvl][reason][0][0] += 1; // per-reason total
2841 update(_deoptimization_hist[lvl][reason][1+action], bc);
2842 }
2843
2844 jint Deoptimization::total_deoptimization_count() {
2845 return _deoptimization_hist[0][Reason_none][0][0];
2846 }
2847
2848 // Get the deopt count for a specific reason and a specific action. If either
2849 // one of 'reason' or 'action' is null, the method returns the sum of all
2850 // deoptimizations with the specific 'action' or 'reason' respectively.
2851 // If both arguments are null, the method returns the total deopt count.
2852 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2853 if (reason_str == nullptr && action_str == nullptr) {
2854 return total_deoptimization_count();
2855 }
2856 juint counter = 0;
2857 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2858 if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
2859 for (int action = 0; action < Action_LIMIT; action++) {
2860 if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
2861 juint* cases = _deoptimization_hist[0][reason][1+action];
2862 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2863 counter += cases[bc_case] >> LSB_BITS;
2864 }
2865 }
2866 }
2867 }
2868 }
2869 return counter;
2870 }
2871
2872 void Deoptimization::print_statistics() {
2873 ttyLocker ttyl;
2874 if (xtty != nullptr) xtty->head("statistics type='deoptimization'");
2875 tty->print_cr("Deoptimization traps recorded:");
2876 print_statistics_on(tty);
2877 if (xtty != nullptr) xtty->tail("statistics");
2878 }
2879
2880 void Deoptimization::print_statistics_on(const char* title, int lvl, outputStream* st) {
2881 juint total = _deoptimization_hist[lvl][Reason_none][0][0];
2882 juint account = total;
2883 #define PRINT_STAT_LINE(name, r) \
2884 st->print_cr(" %d (%4.1f%%) %s", (int)(r), ((r) == total ? 100.0 : (((r) * 100.0) / total)), name);
2885 if (total > 0) {
2886 st->print(" %s: ", title);
2887 PRINT_STAT_LINE("total", total);
2888 // For each non-zero entry in the histogram, print the reason,
2889 // the action, and (if specifically known) the type of bytecode.
2890 for (int reason = 0; reason < Reason_LIMIT; reason++) {
2891 for (int action = 0; action < Action_LIMIT; action++) {
2892 juint* cases = Deoptimization::_deoptimization_hist[lvl][reason][1+action];
2893 for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2894 juint counter = cases[bc_case];
2895 if (counter != 0) {
2896 Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2897 const char* bc_name = "other";
2898 if (bc_case == (BC_CASE_LIMIT-1) && bc == Bytecodes::_nop) {
2899 // overwritten
2900 } else if (Bytecodes::is_defined(bc)) {
2901 bc_name = Bytecodes::name(bc);
2902 }
2903 juint r = counter >> LSB_BITS;
2904 st->print_cr(" %-34s %16s %16s: " UINT32_FORMAT_W(5) " (%4.1f%%)",
2905 trap_reason_name(reason), trap_action_name(action), bc_name,
2906 r, (r * 100.0) / total);
2907 account -= r;
2908 }
2909 }
2910 }
2911 }
2912 if (account != 0) {
2913 PRINT_STAT_LINE("unaccounted", account);
2914 }
2915 #undef PRINT_STAT_LINE
2916 }
2917 }
2918
2919 void Deoptimization::print_statistics_on(outputStream* st) {
2920 // print_statistics_on("Total", 0, st);
2921 print_statistics_on("Tier1", 1, st);
2922 print_statistics_on("Tier2", 2, st);
2923 print_statistics_on("Tier3", 3, st);
2924 print_statistics_on("Tier4", 4, st);
2925
2926 print_statistics_on("SC Tier1", 5, st);
2927 print_statistics_on("SC Tier2", 6, st);
2928 print_statistics_on("SC Tier4", 8, st);
2929 print_statistics_on("SC Tier5 (preloaded)", 9, st);
2930 }
2931
2932 #else // COMPILER2_OR_JVMCI
2933
2934
2935 // Stubs for C1 only system.
2936 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2937 return false;
2938 }
2939
2940 const char* Deoptimization::trap_reason_name(int reason) {
2941 return "unknown";
2942 }
2943
2944 jint Deoptimization::total_deoptimization_count() {
2945 return 0;
2946 }
2947
2948 jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
2949 return 0;
2950 }
2951
2957 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2958 // no update
2959 }
2960
2961 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2962 return 0;
2963 }
2964
2965 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2966 Bytecodes::Code bc) {
2967 // no update
2968 }
2969
2970 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2971 int trap_state) {
2972 jio_snprintf(buf, buflen, "#%d", trap_state);
2973 return buf;
2974 }
2975
2976 #endif // COMPILER2_OR_JVMCI
2977
2978 #define DO_COUNTERS(macro) \
2979 macro(Deoptimization, fetch_unroll_info) \
2980 macro(Deoptimization, unpack_frames) \
2981 macro(Deoptimization, uncommon_trap_inner) \
2982 macro(Deoptimization, uncommon_trap)
2983
2984 #define INIT_COUNTER(sub, name) \
2985 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_RT, #sub "::" #name) \
2986 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_RT, #sub "::" #name "_count");
2987
2988 void Deoptimization::init_counters() {
2989 if (ProfileRuntimeCalls && UsePerfData) {
2990 EXCEPTION_MARK;
2991
2992 DO_COUNTERS(INIT_COUNTER)
2993
2994 if (HAS_PENDING_EXCEPTION) {
2995 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2996 }
2997 }
2998 }
2999 #undef INIT_COUNTER
3000
3001 #define PRINT_COUNTER(sub, name) { \
3002 jlong count = _perf_##sub##_##name##_count->get_value(); \
3003 if (count > 0) { \
3004 st->print_cr(" %-50s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
3005 _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
3006 _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
3007 count); \
3008 }}
3009
3010 void Deoptimization::print_counters_on(outputStream* st) {
3011 if (ProfileRuntimeCalls && UsePerfData) {
3012 DO_COUNTERS(PRINT_COUNTER)
3013 } else {
3014 st->print_cr(" Deoptimization: no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
3015 }
3016 }
3017
3018 #undef PRINT_COUNTER
3019 #undef DO_COUNTERS
|