xref: /aosp_15_r20/art/openjdkjvmti/deopt_manager.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2017 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include <functional>
33*795d594fSAndroid Build Coastguard Worker #include <iosfwd>
34*795d594fSAndroid Build Coastguard Worker #include <mutex>
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker #include "deopt_manager.h"
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker #include "art_jvmti.h"
39*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
40*795d594fSAndroid Build Coastguard Worker #include "base/mutex-inl.h"
41*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
42*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_annotations.h"
43*795d594fSAndroid Build Coastguard Worker #include "dex/modifiers.h"
44*795d594fSAndroid Build Coastguard Worker #include "events-inl.h"
45*795d594fSAndroid Build Coastguard Worker #include "gc/collector_type.h"
46*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
47*795d594fSAndroid Build Coastguard Worker #include "gc/scoped_gc_critical_section.h"
48*795d594fSAndroid Build Coastguard Worker #include "instrumentation.h"
49*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
50*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
51*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
52*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
53*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
54*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
55*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file_manager.h"
56*795d594fSAndroid Build Coastguard Worker #include "read_barrier_config.h"
57*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h"
58*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
59*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change.h"
60*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
61*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
62*795d594fSAndroid Build Coastguard Worker #include "ti_phase.h"
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker static constexpr const char* kInstrumentationKey = "JVMTI_DeoptRequester";
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker // We could make this much more selective in the future so we only return true when we
69*795d594fSAndroid Build Coastguard Worker // actually care about the method at this time (ie active frames had locals changed). For now we
70*795d594fSAndroid Build Coastguard Worker // just assume that if anything has changed any frame's locals we care about all methods. This only
71*795d594fSAndroid Build Coastguard Worker // impacts whether we are able to OSR or not so maybe not really important to maintain frame
72*795d594fSAndroid Build Coastguard Worker // specific information.
HaveLocalsChanged()73*795d594fSAndroid Build Coastguard Worker bool JvmtiMethodInspectionCallback::HaveLocalsChanged() {
74*795d594fSAndroid Build Coastguard Worker   return manager_->HaveLocalsChanged();
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
DeoptManager()77*795d594fSAndroid Build Coastguard Worker DeoptManager::DeoptManager()
78*795d594fSAndroid Build Coastguard Worker   : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock",
79*795d594fSAndroid Build Coastguard Worker                                 static_cast<art::LockLevel>(
80*795d594fSAndroid Build Coastguard Worker                                     art::LockLevel::kClassLinkerClassesLock + 1)),
81*795d594fSAndroid Build Coastguard Worker     deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_),
82*795d594fSAndroid Build Coastguard Worker     performing_deoptimization_(false),
83*795d594fSAndroid Build Coastguard Worker     global_deopt_count_(0),
84*795d594fSAndroid Build Coastguard Worker     deopter_count_(0),
85*795d594fSAndroid Build Coastguard Worker     breakpoint_status_lock_("JVMTI_BreakpointStatusLock",
86*795d594fSAndroid Build Coastguard Worker                             static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)),
87*795d594fSAndroid Build Coastguard Worker     inspection_callback_(this),
88*795d594fSAndroid Build Coastguard Worker     set_local_variable_called_(false) { }
89*795d594fSAndroid Build Coastguard Worker 
Setup()90*795d594fSAndroid Build Coastguard Worker void DeoptManager::Setup() {
91*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange stsc(art::Thread::Current(),
92*795d594fSAndroid Build Coastguard Worker                                     art::ThreadState::kWaitingForDebuggerToAttach);
93*795d594fSAndroid Build Coastguard Worker   art::ScopedSuspendAll ssa("Add method Inspection Callback");
94*795d594fSAndroid Build Coastguard Worker   art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
95*795d594fSAndroid Build Coastguard Worker   callbacks->AddMethodInspectionCallback(&inspection_callback_);
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker 
DumpDeoptInfo(art::Thread * self,std::ostream & stream)98*795d594fSAndroid Build Coastguard Worker void DeoptManager::DumpDeoptInfo(art::Thread* self, std::ostream& stream) {
99*795d594fSAndroid Build Coastguard Worker   art::ScopedObjectAccess soa(self);
100*795d594fSAndroid Build Coastguard Worker   art::MutexLock mutll(self, *art::Locks::thread_list_lock_);
101*795d594fSAndroid Build Coastguard Worker   art::MutexLock mudsl(self, deoptimization_status_lock_);
102*795d594fSAndroid Build Coastguard Worker   art::MutexLock mubsl(self, breakpoint_status_lock_);
103*795d594fSAndroid Build Coastguard Worker   stream << "Deoptimizer count: " << deopter_count_ << "\n";
104*795d594fSAndroid Build Coastguard Worker   stream << "Global deopt count: " << global_deopt_count_ << "\n";
105*795d594fSAndroid Build Coastguard Worker   stream << "Can perform OSR: " << !set_local_variable_called_.load() << "\n";
106*795d594fSAndroid Build Coastguard Worker   for (const auto& [bp, loc] : this->breakpoint_status_) {
107*795d594fSAndroid Build Coastguard Worker     stream << "Breakpoint: " << bp->PrettyMethod() << " @ 0x" << std::hex << loc << "\n";
108*795d594fSAndroid Build Coastguard Worker   }
109*795d594fSAndroid Build Coastguard Worker   struct DumpThreadDeoptCount : public art::Closure {
110*795d594fSAndroid Build Coastguard Worker    public:
111*795d594fSAndroid Build Coastguard Worker     DumpThreadDeoptCount(std::ostream& stream, std::mutex& mu)
112*795d594fSAndroid Build Coastguard Worker         : cnt_(0), stream_(stream), mu_(mu) {}
113*795d594fSAndroid Build Coastguard Worker     void Run(art::Thread* self) override {
114*795d594fSAndroid Build Coastguard Worker       {
115*795d594fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> lg(mu_);
116*795d594fSAndroid Build Coastguard Worker         std::string name;
117*795d594fSAndroid Build Coastguard Worker         self->GetThreadName(name);
118*795d594fSAndroid Build Coastguard Worker         stream_ << "Thread " << name << " (id: " << std::dec << self->GetThreadId()
119*795d594fSAndroid Build Coastguard Worker                 << ") force interpreter count " << self->ForceInterpreterCount() << "\n";
120*795d594fSAndroid Build Coastguard Worker       }
121*795d594fSAndroid Build Coastguard Worker       // Increment this after unlocking the mutex so we won't race its destructor.
122*795d594fSAndroid Build Coastguard Worker       cnt_++;
123*795d594fSAndroid Build Coastguard Worker     }
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker     void WaitForCount(size_t threads) {
126*795d594fSAndroid Build Coastguard Worker       while (cnt_.load() != threads) {
127*795d594fSAndroid Build Coastguard Worker         sched_yield();
128*795d594fSAndroid Build Coastguard Worker       }
129*795d594fSAndroid Build Coastguard Worker     }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker    private:
132*795d594fSAndroid Build Coastguard Worker     std::atomic<size_t> cnt_;
133*795d594fSAndroid Build Coastguard Worker     std::ostream& stream_;
134*795d594fSAndroid Build Coastguard Worker     std::mutex& mu_;
135*795d594fSAndroid Build Coastguard Worker   };
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker   std::mutex mu;
138*795d594fSAndroid Build Coastguard Worker   DumpThreadDeoptCount dtdc(stream, mu);
139*795d594fSAndroid Build Coastguard Worker   auto func = [](art::Thread* thread, void* ctx) {
140*795d594fSAndroid Build Coastguard Worker     reinterpret_cast<DumpThreadDeoptCount*>(ctx)->Run(thread);
141*795d594fSAndroid Build Coastguard Worker   };
142*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetThreadList()->ForEach(func, &dtdc);
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker 
FinishSetup()145*795d594fSAndroid Build Coastguard Worker void DeoptManager::FinishSetup() {
146*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
147*795d594fSAndroid Build Coastguard Worker   art::Runtime* runtime = art::Runtime::Current();
148*795d594fSAndroid Build Coastguard Worker   if (runtime->IsJavaDebuggable()) {
149*795d594fSAndroid Build Coastguard Worker     return;
150*795d594fSAndroid Build Coastguard Worker   }
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker   // See if we can enable all JVMTI functions.
153*795d594fSAndroid Build Coastguard Worker   if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) {
154*795d594fSAndroid Build Coastguard Worker     // We are still early enough to change the compiler options and get full JVMTI support.
155*795d594fSAndroid Build Coastguard Worker     LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to "
156*795d594fSAndroid Build Coastguard Worker               << "debuggable state. Please pass '--debuggable' to dex2oat and "
157*795d594fSAndroid Build Coastguard Worker               << "'-Xcompiler-option --debuggable' to dalvikvm in the future.";
158*795d594fSAndroid Build Coastguard Worker     DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!";
159*795d594fSAndroid Build Coastguard Worker     art::ScopedSuspendAll ssa(__FUNCTION__);
160*795d594fSAndroid Build Coastguard Worker     // TODO check if we need to hold deoptimization_status_lock_ here.
161*795d594fSAndroid Build Coastguard Worker     art::MutexLock mu(self, deoptimization_status_lock_);
162*795d594fSAndroid Build Coastguard Worker     runtime->AddCompilerOption("--debuggable");
163*795d594fSAndroid Build Coastguard Worker     runtime->SetRuntimeDebugState(art::Runtime::RuntimeDebugState::kJavaDebuggableAtInit);
164*795d594fSAndroid Build Coastguard Worker     runtime->DeoptimizeBootImage();
165*795d594fSAndroid Build Coastguard Worker     return;
166*795d594fSAndroid Build Coastguard Worker   }
167*795d594fSAndroid Build Coastguard Worker 
168*795d594fSAndroid Build Coastguard Worker   // Runtime has already started in non-debuggable mode. Only kArtTiVersion agents can be
169*795d594fSAndroid Build Coastguard Worker   // retrieved and they will all be best-effort.
170*795d594fSAndroid Build Coastguard Worker   LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was "
171*795d594fSAndroid Build Coastguard Worker                << "loaded too late to change runtime state to support all capabilities. Only "
172*795d594fSAndroid Build Coastguard Worker                << "kArtTiVersion (0x" << std::hex << kArtTiVersion << ") environments are "
173*795d594fSAndroid Build Coastguard Worker                << "available. Some functionality might not work properly.";
174*795d594fSAndroid Build Coastguard Worker 
175*795d594fSAndroid Build Coastguard Worker   // Transition the runtime to debuggable:
176*795d594fSAndroid Build Coastguard Worker   // 1. Wait for any background verification tasks to finish. We don't support
177*795d594fSAndroid Build Coastguard Worker   // background verification after moving to debuggable state.
178*795d594fSAndroid Build Coastguard Worker   runtime->GetOatFileManager().WaitForBackgroundVerificationTasksToFinish();
179*795d594fSAndroid Build Coastguard Worker 
180*795d594fSAndroid Build Coastguard Worker   // Do the transition in ScopedJITSuspend, so we don't start any JIT compilations
181*795d594fSAndroid Build Coastguard Worker   // before the transition to debuggable is finished.
182*795d594fSAndroid Build Coastguard Worker   art::jit::ScopedJitSuspend suspend_jit;
183*795d594fSAndroid Build Coastguard Worker   art::ScopedSuspendAll ssa(__FUNCTION__);
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   // 2. Discard any JITed code that was generated before, since they would be
186*795d594fSAndroid Build Coastguard Worker   // compiled without debug support.
187*795d594fSAndroid Build Coastguard Worker   art::jit::Jit* jit = runtime->GetJit();
188*795d594fSAndroid Build Coastguard Worker   if (jit != nullptr) {
189*795d594fSAndroid Build Coastguard Worker     jit->GetCodeCache()->InvalidateAllCompiledCode();
190*795d594fSAndroid Build Coastguard Worker     jit->GetCodeCache()->TransitionToDebuggable();
191*795d594fSAndroid Build Coastguard Worker     jit->GetJitCompiler()->SetDebuggableCompilerOption(true);
192*795d594fSAndroid Build Coastguard Worker   }
193*795d594fSAndroid Build Coastguard Worker 
194*795d594fSAndroid Build Coastguard Worker   // 3. Change the state to JavaDebuggable, so that debug features can be
195*795d594fSAndroid Build Coastguard Worker   // enabled from now on.
196*795d594fSAndroid Build Coastguard Worker   runtime->SetRuntimeDebugState(art::Runtime::RuntimeDebugState::kJavaDebuggable);
197*795d594fSAndroid Build Coastguard Worker 
198*795d594fSAndroid Build Coastguard Worker   // 4. Update all entrypoints to avoid using any AOT code.
199*795d594fSAndroid Build Coastguard Worker   runtime->GetInstrumentation()->UpdateEntrypointsForDebuggable();
200*795d594fSAndroid Build Coastguard Worker }
201*795d594fSAndroid Build Coastguard Worker 
MethodHasBreakpoints(art::ArtMethod * method)202*795d594fSAndroid Build Coastguard Worker bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
203*795d594fSAndroid Build Coastguard Worker   art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_);
204*795d594fSAndroid Build Coastguard Worker   return MethodHasBreakpointsLocked(method);
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker 
MethodHasBreakpointsLocked(art::ArtMethod * method)207*795d594fSAndroid Build Coastguard Worker bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
208*795d594fSAndroid Build Coastguard Worker   auto elem = breakpoint_status_.find(method);
209*795d594fSAndroid Build Coastguard Worker   return elem != breakpoint_status_.end() && elem->second != 0;
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker 
RemoveDeoptimizeAllMethods()212*795d594fSAndroid Build Coastguard Worker void DeoptManager::RemoveDeoptimizeAllMethods() {
213*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
214*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
215*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
216*795d594fSAndroid Build Coastguard Worker   RemoveDeoptimizeAllMethodsLocked(self);
217*795d594fSAndroid Build Coastguard Worker }
218*795d594fSAndroid Build Coastguard Worker 
AddDeoptimizeAllMethods()219*795d594fSAndroid Build Coastguard Worker void DeoptManager::AddDeoptimizeAllMethods() {
220*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
221*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
222*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
223*795d594fSAndroid Build Coastguard Worker   AddDeoptimizeAllMethodsLocked(self);
224*795d594fSAndroid Build Coastguard Worker }
225*795d594fSAndroid Build Coastguard Worker 
AddMethodBreakpoint(art::ArtMethod * method)226*795d594fSAndroid Build Coastguard Worker void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
227*795d594fSAndroid Build Coastguard Worker   DCHECK(method->IsInvokable());
228*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
229*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsNative()) << method->PrettyMethod();
230*795d594fSAndroid Build Coastguard Worker 
231*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
232*795d594fSAndroid Build Coastguard Worker   method = method->GetCanonicalMethod();
233*795d594fSAndroid Build Coastguard Worker   bool is_default = method->IsDefault();
234*795d594fSAndroid Build Coastguard Worker 
235*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
236*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
237*795d594fSAndroid Build Coastguard Worker   {
238*795d594fSAndroid Build Coastguard Worker     breakpoint_status_lock_.ExclusiveLock(self);
239*795d594fSAndroid Build Coastguard Worker 
240*795d594fSAndroid Build Coastguard Worker     DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker     if (MethodHasBreakpointsLocked(method)) {
243*795d594fSAndroid Build Coastguard Worker       // Don't need to do anything extra.
244*795d594fSAndroid Build Coastguard Worker       breakpoint_status_[method]++;
245*795d594fSAndroid Build Coastguard Worker       // Another thread might be deoptimizing the very method we just added new breakpoints for.
246*795d594fSAndroid Build Coastguard Worker       // Wait for any deopts to finish before moving on.
247*795d594fSAndroid Build Coastguard Worker       breakpoint_status_lock_.ExclusiveUnlock(self);
248*795d594fSAndroid Build Coastguard Worker       WaitForDeoptimizationToFinish(self);
249*795d594fSAndroid Build Coastguard Worker       return;
250*795d594fSAndroid Build Coastguard Worker     }
251*795d594fSAndroid Build Coastguard Worker     breakpoint_status_[method] = 1;
252*795d594fSAndroid Build Coastguard Worker     breakpoint_status_lock_.ExclusiveUnlock(self);
253*795d594fSAndroid Build Coastguard Worker   }
254*795d594fSAndroid Build Coastguard Worker   auto instrumentation = art::Runtime::Current()->GetInstrumentation();
255*795d594fSAndroid Build Coastguard Worker   if (instrumentation->IsForcedInterpretOnly()) {
256*795d594fSAndroid Build Coastguard Worker     // We are already interpreting everything so no need to do anything.
257*795d594fSAndroid Build Coastguard Worker     deoptimization_status_lock_.ExclusiveUnlock(self);
258*795d594fSAndroid Build Coastguard Worker     return;
259*795d594fSAndroid Build Coastguard Worker   } else if (is_default) {
260*795d594fSAndroid Build Coastguard Worker     AddDeoptimizeAllMethodsLocked(self);
261*795d594fSAndroid Build Coastguard Worker   } else {
262*795d594fSAndroid Build Coastguard Worker     PerformLimitedDeoptimization(self, method);
263*795d594fSAndroid Build Coastguard Worker   }
264*795d594fSAndroid Build Coastguard Worker }
265*795d594fSAndroid Build Coastguard Worker 
RemoveMethodBreakpoint(art::ArtMethod * method)266*795d594fSAndroid Build Coastguard Worker void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
267*795d594fSAndroid Build Coastguard Worker   DCHECK(method->IsInvokable()) << method->PrettyMethod();
268*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
269*795d594fSAndroid Build Coastguard Worker   DCHECK(!method->IsNative()) << method->PrettyMethod();
270*795d594fSAndroid Build Coastguard Worker 
271*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
272*795d594fSAndroid Build Coastguard Worker   method = method->GetCanonicalMethod();
273*795d594fSAndroid Build Coastguard Worker   bool is_default = method->IsDefault();
274*795d594fSAndroid Build Coastguard Worker 
275*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadSuspension sts(self, art::ThreadState::kSuspended);
276*795d594fSAndroid Build Coastguard Worker   // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
277*795d594fSAndroid Build Coastguard Worker   // need but since that is very heavy we will instead just use a condition variable to make sure we
278*795d594fSAndroid Build Coastguard Worker   // don't race with ourselves.
279*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
280*795d594fSAndroid Build Coastguard Worker   bool is_last_breakpoint;
281*795d594fSAndroid Build Coastguard Worker   {
282*795d594fSAndroid Build Coastguard Worker     art::MutexLock mu(self, breakpoint_status_lock_);
283*795d594fSAndroid Build Coastguard Worker 
284*795d594fSAndroid Build Coastguard Worker     DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
285*795d594fSAndroid Build Coastguard Worker     DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
286*795d594fSAndroid Build Coastguard Worker                                               << "breakpoints present!";
287*795d594fSAndroid Build Coastguard Worker     breakpoint_status_[method] -= 1;
288*795d594fSAndroid Build Coastguard Worker     is_last_breakpoint = (breakpoint_status_[method] == 0);
289*795d594fSAndroid Build Coastguard Worker   }
290*795d594fSAndroid Build Coastguard Worker   auto instrumentation = art::Runtime::Current()->GetInstrumentation();
291*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
292*795d594fSAndroid Build Coastguard Worker     // We don't need to do anything since we are interpreting everything anyway.
293*795d594fSAndroid Build Coastguard Worker     deoptimization_status_lock_.ExclusiveUnlock(self);
294*795d594fSAndroid Build Coastguard Worker     return;
295*795d594fSAndroid Build Coastguard Worker   } else if (is_last_breakpoint) {
296*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(is_default)) {
297*795d594fSAndroid Build Coastguard Worker       RemoveDeoptimizeAllMethodsLocked(self);
298*795d594fSAndroid Build Coastguard Worker     } else {
299*795d594fSAndroid Build Coastguard Worker       PerformLimitedUndeoptimization(self, method);
300*795d594fSAndroid Build Coastguard Worker     }
301*795d594fSAndroid Build Coastguard Worker   } else {
302*795d594fSAndroid Build Coastguard Worker     // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
303*795d594fSAndroid Build Coastguard Worker     // for any deopts to finish before moving on.
304*795d594fSAndroid Build Coastguard Worker     WaitForDeoptimizationToFinish(self);
305*795d594fSAndroid Build Coastguard Worker   }
306*795d594fSAndroid Build Coastguard Worker }
307*795d594fSAndroid Build Coastguard Worker 
WaitForDeoptimizationToFinishLocked(art::Thread * self)308*795d594fSAndroid Build Coastguard Worker void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
309*795d594fSAndroid Build Coastguard Worker   while (performing_deoptimization_) {
310*795d594fSAndroid Build Coastguard Worker     deoptimization_condition_.Wait(self);
311*795d594fSAndroid Build Coastguard Worker   }
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker 
WaitForDeoptimizationToFinish(art::Thread * self)314*795d594fSAndroid Build Coastguard Worker void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
315*795d594fSAndroid Build Coastguard Worker   WaitForDeoptimizationToFinishLocked(self);
316*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveUnlock(self);
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker 
319*795d594fSAndroid Build Coastguard Worker // Users should make sure that only gc-critical-section safe code is used while a
320*795d594fSAndroid Build Coastguard Worker // ScopedDeoptimizationContext exists.
321*795d594fSAndroid Build Coastguard Worker class ScopedDeoptimizationContext : public art::ValueObject {
322*795d594fSAndroid Build Coastguard Worker  public:
ScopedDeoptimizationContext(art::Thread * self,DeoptManager * deopt)323*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
324*795d594fSAndroid Build Coastguard Worker       RELEASE(deopt->deoptimization_status_lock_)
325*795d594fSAndroid Build Coastguard Worker       ACQUIRE(art::Locks::mutator_lock_)
326*795d594fSAndroid Build Coastguard Worker       ACQUIRE(art::Roles::uninterruptible_)
327*795d594fSAndroid Build Coastguard Worker       : self_(self),
328*795d594fSAndroid Build Coastguard Worker         deopt_(deopt),
329*795d594fSAndroid Build Coastguard Worker         critical_section_(self_, "JVMTI Deoptimizing methods"),
330*795d594fSAndroid Build Coastguard Worker         uninterruptible_cause_(nullptr) {
331*795d594fSAndroid Build Coastguard Worker     deopt_->WaitForDeoptimizationToFinishLocked(self_);
332*795d594fSAndroid Build Coastguard Worker     DCHECK(!deopt->performing_deoptimization_)
333*795d594fSAndroid Build Coastguard Worker         << "Already performing deoptimization on another thread!";
334*795d594fSAndroid Build Coastguard Worker     // Use performing_deoptimization_ to keep track of the lock.
335*795d594fSAndroid Build Coastguard Worker     deopt_->performing_deoptimization_ = true;
336*795d594fSAndroid Build Coastguard Worker     deopt_->deoptimization_status_lock_.Unlock(self_);
337*795d594fSAndroid Build Coastguard Worker     uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation,
338*795d594fSAndroid Build Coastguard Worker                                                      art::gc::kCollectorTypeCriticalSection);
339*795d594fSAndroid Build Coastguard Worker     art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
340*795d594fSAndroid Build Coastguard Worker                                                          /*long_suspend=*/ false);
341*795d594fSAndroid Build Coastguard Worker   }
342*795d594fSAndroid Build Coastguard Worker 
343*795d594fSAndroid Build Coastguard Worker   ~ScopedDeoptimizationContext()
RELEASE(art::Locks::mutator_lock_)344*795d594fSAndroid Build Coastguard Worker       RELEASE(art::Locks::mutator_lock_)
345*795d594fSAndroid Build Coastguard Worker       RELEASE(art::Roles::uninterruptible_) {
346*795d594fSAndroid Build Coastguard Worker     // Can be suspended again.
347*795d594fSAndroid Build Coastguard Worker     critical_section_.Exit(uninterruptible_cause_);
348*795d594fSAndroid Build Coastguard Worker     // Release the mutator lock.
349*795d594fSAndroid Build Coastguard Worker     art::Runtime::Current()->GetThreadList()->ResumeAll();
350*795d594fSAndroid Build Coastguard Worker     // Let other threads know it's fine to proceed.
351*795d594fSAndroid Build Coastguard Worker     art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
352*795d594fSAndroid Build Coastguard Worker     deopt_->performing_deoptimization_ = false;
353*795d594fSAndroid Build Coastguard Worker     deopt_->deoptimization_condition_.Broadcast(self_);
354*795d594fSAndroid Build Coastguard Worker   }
355*795d594fSAndroid Build Coastguard Worker 
356*795d594fSAndroid Build Coastguard Worker  private:
357*795d594fSAndroid Build Coastguard Worker   art::Thread* self_;
358*795d594fSAndroid Build Coastguard Worker   DeoptManager* deopt_;
359*795d594fSAndroid Build Coastguard Worker   art::gc::GCCriticalSection critical_section_;
360*795d594fSAndroid Build Coastguard Worker   const char* uninterruptible_cause_;
361*795d594fSAndroid Build Coastguard Worker };
362*795d594fSAndroid Build Coastguard Worker 
AddDeoptimizeAllMethodsLocked(art::Thread * self)363*795d594fSAndroid Build Coastguard Worker void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
364*795d594fSAndroid Build Coastguard Worker   global_deopt_count_++;
365*795d594fSAndroid Build Coastguard Worker   if (global_deopt_count_ == 1) {
366*795d594fSAndroid Build Coastguard Worker     PerformGlobalDeoptimization(self);
367*795d594fSAndroid Build Coastguard Worker   } else {
368*795d594fSAndroid Build Coastguard Worker     WaitForDeoptimizationToFinish(self);
369*795d594fSAndroid Build Coastguard Worker   }
370*795d594fSAndroid Build Coastguard Worker }
371*795d594fSAndroid Build Coastguard Worker 
Shutdown()372*795d594fSAndroid Build Coastguard Worker void DeoptManager::Shutdown() {
373*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
374*795d594fSAndroid Build Coastguard Worker   art::Runtime* runtime = art::Runtime::Current();
375*795d594fSAndroid Build Coastguard Worker 
376*795d594fSAndroid Build Coastguard Worker   // Do the transition in ScopedJITSuspend, so we don't start any JIT compilations
377*795d594fSAndroid Build Coastguard Worker   // before the transition to debuggable is finished.
378*795d594fSAndroid Build Coastguard Worker   art::jit::ScopedJitSuspend suspend_jit;
379*795d594fSAndroid Build Coastguard Worker 
380*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange sts(self, art::ThreadState::kSuspended);
381*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
382*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext sdc(self, this);
383*795d594fSAndroid Build Coastguard Worker 
384*795d594fSAndroid Build Coastguard Worker   art::RuntimeCallbacks* callbacks = runtime->GetRuntimeCallbacks();
385*795d594fSAndroid Build Coastguard Worker   callbacks->RemoveMethodInspectionCallback(&inspection_callback_);
386*795d594fSAndroid Build Coastguard Worker 
387*795d594fSAndroid Build Coastguard Worker   if (runtime->IsShuttingDown(self)) {
388*795d594fSAndroid Build Coastguard Worker     return;
389*795d594fSAndroid Build Coastguard Worker   }
390*795d594fSAndroid Build Coastguard Worker 
391*795d594fSAndroid Build Coastguard Worker   // If we attach a debugger to a non-debuggable runtime, we switch the runtime to debuggable to
392*795d594fSAndroid Build Coastguard Worker   // provide a consistent (though still best effort) support. Since we are detaching the debugger
393*795d594fSAndroid Build Coastguard Worker   // now, switch it back to non-debuggable if there are no other debugger / profiling tools are
394*795d594fSAndroid Build Coastguard Worker   // active.
395*795d594fSAndroid Build Coastguard Worker   runtime->GetInstrumentation()->DisableDeoptimization(kInstrumentationKey,
396*795d594fSAndroid Build Coastguard Worker                                                        /*try_switch_to_non_debuggable=*/true);
397*795d594fSAndroid Build Coastguard Worker   runtime->GetInstrumentation()->DisableDeoptimization(kDeoptManagerInstrumentationKey,
398*795d594fSAndroid Build Coastguard Worker                                                        /*try_switch_to_non_debuggable=*/true);
399*795d594fSAndroid Build Coastguard Worker }
400*795d594fSAndroid Build Coastguard Worker 
RemoveDeoptimizeAllMethodsLocked(art::Thread * self)401*795d594fSAndroid Build Coastguard Worker void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
402*795d594fSAndroid Build Coastguard Worker   DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!";
403*795d594fSAndroid Build Coastguard Worker   global_deopt_count_--;
404*795d594fSAndroid Build Coastguard Worker   if (global_deopt_count_ == 0) {
405*795d594fSAndroid Build Coastguard Worker     PerformGlobalUndeoptimization(self);
406*795d594fSAndroid Build Coastguard Worker   } else {
407*795d594fSAndroid Build Coastguard Worker     WaitForDeoptimizationToFinish(self);
408*795d594fSAndroid Build Coastguard Worker   }
409*795d594fSAndroid Build Coastguard Worker }
410*795d594fSAndroid Build Coastguard Worker 
PerformLimitedDeoptimization(art::Thread * self,art::ArtMethod * method)411*795d594fSAndroid Build Coastguard Worker void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
412*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext sdc(self, this);
413*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
414*795d594fSAndroid Build Coastguard Worker }
415*795d594fSAndroid Build Coastguard Worker 
PerformLimitedUndeoptimization(art::Thread * self,art::ArtMethod * method)416*795d594fSAndroid Build Coastguard Worker void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
417*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext sdc(self, this);
418*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
419*795d594fSAndroid Build Coastguard Worker }
420*795d594fSAndroid Build Coastguard Worker 
PerformGlobalDeoptimization(art::Thread * self)421*795d594fSAndroid Build Coastguard Worker void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
422*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext sdc(self, this);
423*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
424*795d594fSAndroid Build Coastguard Worker       kDeoptManagerInstrumentationKey);
425*795d594fSAndroid Build Coastguard Worker }
426*795d594fSAndroid Build Coastguard Worker 
PerformGlobalUndeoptimization(art::Thread * self)427*795d594fSAndroid Build Coastguard Worker void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
428*795d594fSAndroid Build Coastguard Worker   ScopedDeoptimizationContext sdc(self, this);
429*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
430*795d594fSAndroid Build Coastguard Worker       kDeoptManagerInstrumentationKey);
431*795d594fSAndroid Build Coastguard Worker }
432*795d594fSAndroid Build Coastguard Worker 
AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked & soa,jthread jtarget)433*795d594fSAndroid Build Coastguard Worker jvmtiError DeoptManager::AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
434*795d594fSAndroid Build Coastguard Worker   art::Locks::thread_list_lock_->ExclusiveLock(soa.Self());
435*795d594fSAndroid Build Coastguard Worker   art::Thread* target = nullptr;
436*795d594fSAndroid Build Coastguard Worker   jvmtiError err = OK;
437*795d594fSAndroid Build Coastguard Worker   if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
438*795d594fSAndroid Build Coastguard Worker     art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
439*795d594fSAndroid Build Coastguard Worker     return err;
440*795d594fSAndroid Build Coastguard Worker   }
441*795d594fSAndroid Build Coastguard Worker   // We don't need additional locking here because we hold the Thread_list_lock_.
442*795d594fSAndroid Build Coastguard Worker   if (target->IncrementForceInterpreterCount() == 1) {
443*795d594fSAndroid Build Coastguard Worker     struct DeoptClosure : public art::Closure {
444*795d594fSAndroid Build Coastguard Worker      public:
445*795d594fSAndroid Build Coastguard Worker       explicit DeoptClosure(DeoptManager* manager) : manager_(manager) {}
446*795d594fSAndroid Build Coastguard Worker       void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
447*795d594fSAndroid Build Coastguard Worker         manager_->DeoptimizeThread(self);
448*795d594fSAndroid Build Coastguard Worker       }
449*795d594fSAndroid Build Coastguard Worker 
450*795d594fSAndroid Build Coastguard Worker      private:
451*795d594fSAndroid Build Coastguard Worker       DeoptManager* manager_;
452*795d594fSAndroid Build Coastguard Worker     };
453*795d594fSAndroid Build Coastguard Worker     DeoptClosure c(this);
454*795d594fSAndroid Build Coastguard Worker     target->RequestSynchronousCheckpoint(&c);
455*795d594fSAndroid Build Coastguard Worker   } else {
456*795d594fSAndroid Build Coastguard Worker     art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
457*795d594fSAndroid Build Coastguard Worker   }
458*795d594fSAndroid Build Coastguard Worker   return OK;
459*795d594fSAndroid Build Coastguard Worker }
460*795d594fSAndroid Build Coastguard Worker 
RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked & soa,jthread jtarget)461*795d594fSAndroid Build Coastguard Worker jvmtiError DeoptManager::RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
462*795d594fSAndroid Build Coastguard Worker   art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
463*795d594fSAndroid Build Coastguard Worker   art::Thread* target = nullptr;
464*795d594fSAndroid Build Coastguard Worker   jvmtiError err = OK;
465*795d594fSAndroid Build Coastguard Worker   if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
466*795d594fSAndroid Build Coastguard Worker     return err;
467*795d594fSAndroid Build Coastguard Worker   }
468*795d594fSAndroid Build Coastguard Worker   // We don't need additional locking here because we hold the Thread_list_lock_.
469*795d594fSAndroid Build Coastguard Worker   DCHECK_GT(target->ForceInterpreterCount(), 0u);
470*795d594fSAndroid Build Coastguard Worker   target->DecrementForceInterpreterCount();
471*795d594fSAndroid Build Coastguard Worker   return OK;
472*795d594fSAndroid Build Coastguard Worker }
473*795d594fSAndroid Build Coastguard Worker 
474*795d594fSAndroid Build Coastguard Worker 
RemoveDeoptimizationRequester()475*795d594fSAndroid Build Coastguard Worker void DeoptManager::RemoveDeoptimizationRequester() {
476*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
477*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange sts(self, art::ThreadState::kSuspended);
478*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
479*795d594fSAndroid Build Coastguard Worker   DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
480*795d594fSAndroid Build Coastguard Worker   deopter_count_--;
481*795d594fSAndroid Build Coastguard Worker   if (deopter_count_ == 0) {
482*795d594fSAndroid Build Coastguard Worker     ScopedDeoptimizationContext sdc(self, this);
483*795d594fSAndroid Build Coastguard Worker     art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization(
484*795d594fSAndroid Build Coastguard Worker         kInstrumentationKey, /*try_switch_to_non_debuggable=*/false);
485*795d594fSAndroid Build Coastguard Worker     return;
486*795d594fSAndroid Build Coastguard Worker   } else {
487*795d594fSAndroid Build Coastguard Worker     deoptimization_status_lock_.ExclusiveUnlock(self);
488*795d594fSAndroid Build Coastguard Worker   }
489*795d594fSAndroid Build Coastguard Worker }
490*795d594fSAndroid Build Coastguard Worker 
AddDeoptimizationRequester()491*795d594fSAndroid Build Coastguard Worker void DeoptManager::AddDeoptimizationRequester() {
492*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
493*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadStateChange stsc(self, art::ThreadState::kSuspended);
494*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveLock(self);
495*795d594fSAndroid Build Coastguard Worker   deopter_count_++;
496*795d594fSAndroid Build Coastguard Worker   if (deopter_count_ == 1) {
497*795d594fSAndroid Build Coastguard Worker     // When we add a deoptimization requester, we should enable entry / exit hooks. We only call
498*795d594fSAndroid Build Coastguard Worker     // this in debuggable runtimes and hence it won't be necessary to update entrypoints but we
499*795d594fSAndroid Build Coastguard Worker     // still need to inform instrumentation that we need to actually run entry / exit hooks. Though
500*795d594fSAndroid Build Coastguard Worker     // entrypoints are capable of running entry / exit hooks they won't run them unless enabled.
501*795d594fSAndroid Build Coastguard Worker     ScopedDeoptimizationContext sdc(self, this);
502*795d594fSAndroid Build Coastguard Worker     art::Runtime::Current()->GetInstrumentation()->EnableEntryExitHooks(kInstrumentationKey);
503*795d594fSAndroid Build Coastguard Worker     return;
504*795d594fSAndroid Build Coastguard Worker   }
505*795d594fSAndroid Build Coastguard Worker   deoptimization_status_lock_.ExclusiveUnlock(self);
506*795d594fSAndroid Build Coastguard Worker }
507*795d594fSAndroid Build Coastguard Worker 
DeoptimizeThread(art::Thread * target)508*795d594fSAndroid Build Coastguard Worker void DeoptManager::DeoptimizeThread(art::Thread* target) {
509*795d594fSAndroid Build Coastguard Worker   // We might or might not be running on the target thread (self) so get Thread::Current
510*795d594fSAndroid Build Coastguard Worker   // directly.
511*795d594fSAndroid Build Coastguard Worker   art::ScopedThreadSuspension sts(art::Thread::Current(), art::ThreadState::kSuspended);
512*795d594fSAndroid Build Coastguard Worker   art::gc::ScopedGCCriticalSection sgccs(art::Thread::Current(),
513*795d594fSAndroid Build Coastguard Worker                                          art::gc::GcCause::kGcCauseDebugger,
514*795d594fSAndroid Build Coastguard Worker                                          art::gc::CollectorType::kCollectorTypeDebugger);
515*795d594fSAndroid Build Coastguard Worker   art::ScopedSuspendAll ssa("Instrument thread stack");
516*795d594fSAndroid Build Coastguard Worker   // Prepare the stack so methods can be deoptimized as and when required.
517*795d594fSAndroid Build Coastguard Worker   // This by itself doesn't cause any methods to deoptimize but enables
518*795d594fSAndroid Build Coastguard Worker   // deoptimization on demand.
519*795d594fSAndroid Build Coastguard Worker   art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target,
520*795d594fSAndroid Build Coastguard Worker                                                                        /* force_deopt= */ false);
521*795d594fSAndroid Build Coastguard Worker }
522*795d594fSAndroid Build Coastguard Worker 
523*795d594fSAndroid Build Coastguard Worker extern DeoptManager* gDeoptManager;
Get()524*795d594fSAndroid Build Coastguard Worker DeoptManager* DeoptManager::Get() {
525*795d594fSAndroid Build Coastguard Worker   return gDeoptManager;
526*795d594fSAndroid Build Coastguard Worker }
527*795d594fSAndroid Build Coastguard Worker 
528*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
529