blob: bf1b4f07149656f5ff8d0970f69d06ea29712a11 [file] [log] [blame]
Alex Light0fa17862017-10-24 13:43:05 -07001/* Copyright (C) 2017 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include <functional>
Alex Lightb2146942019-03-12 15:46:40 +000033#include <iosfwd>
34#include <mutex>
Alex Light0fa17862017-10-24 13:43:05 -070035
36#include "deopt_manager.h"
37
38#include "art_jvmti.h"
39#include "art_method-inl.h"
40#include "base/enums.h"
41#include "base/mutex-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file_annotations.h"
David Sehr8c0961f2018-01-23 16:11:38 -080043#include "dex/modifiers.h"
Alex Light0fa17862017-10-24 13:43:05 -070044#include "events-inl.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070045#include "gc/collector_type.h"
Alex Light3b8aa772018-08-13 15:55:44 -070046#include "gc/heap.h"
47#include "gc/scoped_gc_critical_section.h"
Alex Light40607862019-05-06 18:16:24 +000048#include "instrumentation.h"
Alex Light60fbefc2018-04-18 15:19:15 -070049#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010050#include "jni/jni_internal.h"
Alex Light0fa17862017-10-24 13:43:05 -070051#include "mirror/class-inl.h"
52#include "mirror/object_array-inl.h"
Alex Light0fa17862017-10-24 13:43:05 -070053#include "nativehelper/scoped_local_ref.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070054#include "read_barrier_config.h"
Alex Light0fa17862017-10-24 13:43:05 -070055#include "runtime_callbacks.h"
56#include "scoped_thread_state_change-inl.h"
Alex Light3dacdd62019-03-12 15:45:47 +000057#include "scoped_thread_state_change.h"
Alex Light0fa17862017-10-24 13:43:05 -070058#include "thread-current-inl.h"
59#include "thread_list.h"
60#include "ti_phase.h"
61
62namespace openjdkjvmti {
63
64// TODO We should make this much more selective in the future so we only return true when we
Alex Lightf2858632018-04-02 11:28:50 -070065// actually care about the method at this time (ie active frames had locals changed). For now we
66// just assume that if anything has changed any frame's locals we care about all methods. If nothing
67// has we only care about methods with active breakpoints on them. In the future we should probably
68// rewrite all of this to instead do this at the ShadowFrame or thread granularity.
69bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(art::ArtMethod* method) {
70 // Non-java-debuggable runtimes we need to assume that any method might not be debuggable and
71 // therefore potentially being inspected (due to inlines). If we are debuggable we rely hard on
72 // inlining not being done since we don't keep track of which methods get inlined where and simply
73 // look to see if the method is breakpointed.
74 return !art::Runtime::Current()->IsJavaDebuggable() ||
75 manager_->HaveLocalsChanged() ||
76 manager_->MethodHasBreakpoints(method);
Alex Light0fa17862017-10-24 13:43:05 -070077}
78
79bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) {
80 return !manager_->MethodHasBreakpoints(method);
81}
82
Alex Lightf2858632018-04-02 11:28:50 -070083bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion(
84 art::ArtMethod* method ATTRIBUTE_UNUSED) {
85 return true;
86}
87
Alex Light0fa17862017-10-24 13:43:05 -070088DeoptManager::DeoptManager()
Alex Light2ce6fc82017-12-18 16:42:36 -080089 : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock",
90 static_cast<art::LockLevel>(
91 art::LockLevel::kClassLinkerClassesLock + 1)),
Alex Light0fa17862017-10-24 13:43:05 -070092 deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_),
93 performing_deoptimization_(false),
94 global_deopt_count_(0),
95 deopter_count_(0),
Alex Lightf2858632018-04-02 11:28:50 -070096 breakpoint_status_lock_("JVMTI_BreakpointStatusLock",
97 static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)),
98 inspection_callback_(this),
David Srbeckyd25eb2c2018-07-19 12:17:04 +000099 set_local_variable_called_(false) { }
Alex Light0fa17862017-10-24 13:43:05 -0700100
101void DeoptManager::Setup() {
102 art::ScopedThreadStateChange stsc(art::Thread::Current(),
103 art::ThreadState::kWaitingForDebuggerToAttach);
104 art::ScopedSuspendAll ssa("Add method Inspection Callback");
105 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
106 callbacks->AddMethodInspectionCallback(&inspection_callback_);
107}
108
109void DeoptManager::Shutdown() {
110 art::ScopedThreadStateChange stsc(art::Thread::Current(),
111 art::ThreadState::kWaitingForDebuggerToAttach);
112 art::ScopedSuspendAll ssa("remove method Inspection Callback");
113 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
114 callbacks->RemoveMethodInspectionCallback(&inspection_callback_);
115}
116
Alex Lightb2146942019-03-12 15:46:40 +0000117void DeoptManager::DumpDeoptInfo(art::Thread* self, std::ostream& stream) {
118 art::ScopedObjectAccess soa(self);
119 art::MutexLock mutll(self, *art::Locks::thread_list_lock_);
120 art::MutexLock mudsl(self, deoptimization_status_lock_);
121 art::MutexLock mubsl(self, breakpoint_status_lock_);
122 stream << "Deoptimizer count: " << deopter_count_ << "\n";
123 stream << "Global deopt count: " << global_deopt_count_ << "\n";
124 stream << "Can perform OSR: " << !set_local_variable_called_.load() << "\n";
125 for (const auto& [bp, loc] : this->breakpoint_status_) {
126 stream << "Breakpoint: " << bp->PrettyMethod() << " @ 0x" << std::hex << loc << "\n";
127 }
128 struct DumpThreadDeoptCount : public art::Closure {
129 public:
130 DumpThreadDeoptCount(std::ostream& stream, std::mutex& mu)
131 : cnt_(0), stream_(stream), mu_(mu) {}
132 void Run(art::Thread* self) override {
133 {
134 std::lock_guard<std::mutex> lg(mu_);
135 std::string name;
136 self->GetThreadName(name);
137 stream_ << "Thread " << name << " (id: " << std::dec << self->GetThreadId()
138 << ") force interpreter count " << self->ForceInterpreterCount() << "\n";
139 }
140 // Increment this after unlocking the mutex so we won't race its destructor.
141 cnt_++;
142 }
143
144 void WaitForCount(size_t threads) {
145 while (cnt_.load() != threads) {
146 sched_yield();
147 }
148 }
149
150 private:
151 std::atomic<size_t> cnt_;
152 std::ostream& stream_;
153 std::mutex& mu_;
154 };
155
156 std::mutex mu;
157 DumpThreadDeoptCount dtdc(stream, mu);
158 auto func = [](art::Thread* thread, void* ctx) {
159 reinterpret_cast<DumpThreadDeoptCount*>(ctx)->Run(thread);
160 };
161 art::Runtime::Current()->GetThreadList()->ForEach(func, &dtdc);
162}
163
Alex Light2ce6fc82017-12-18 16:42:36 -0800164void DeoptManager::FinishSetup() {
165 art::Thread* self = art::Thread::Current();
166 art::MutexLock mu(self, deoptimization_status_lock_);
167
168 art::Runtime* runtime = art::Runtime::Current();
169 // See if we need to do anything.
170 if (!runtime->IsJavaDebuggable()) {
171 // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be
172 // retrieved and they will all be best-effort.
173 if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) {
174 // We are still early enough to change the compiler options and get full JVMTI support.
175 LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to "
176 << "debuggable state. Please pass '--debuggable' to dex2oat and "
177 << "'-Xcompiler-option --debuggable' to dalvikvm in the future.";
178 DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!";
179 runtime->AddCompilerOption("--debuggable");
180 runtime->SetJavaDebuggable(true);
181 } else {
182 LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was "
183 << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion "
184 << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some "
185 << "functionality might not work properly.";
Alex Light60fbefc2018-04-18 15:19:15 -0700186 if (runtime->GetJit() == nullptr &&
187 runtime->GetJITOptions()->UseJitCompilation() &&
188 !runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
189 // If we don't have a jit we should try to start the jit for performance reasons. We only
190 // need to do this for late attach on non-debuggable processes because for debuggable
191 // processes we already rely on jit and we cannot force this jit to start if we are still in
192 // OnLoad since the runtime hasn't started up sufficiently. This is only expected to happen
193 // on userdebug/eng builds.
194 LOG(INFO) << "Attempting to start jit for openjdkjvmti plugin.";
Andreas Gampec6bd42a2018-11-07 13:39:41 -0800195 // Note: use rwx allowed = true, because if this is the system server, we will not be
196 // allowed to allocate any JIT code cache, anyways.
197 runtime->CreateJitCodeCache(/*rwx_memory_allowed=*/true);
Alex Light60fbefc2018-04-18 15:19:15 -0700198 runtime->CreateJit();
199 if (runtime->GetJit() == nullptr) {
200 LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be "
201 << "quite slow as it is running entirely in the interpreter. Try running "
202 << "'setenforce 0' and restarting this process.";
203 }
204 }
Alex Light2ce6fc82017-12-18 16:42:36 -0800205 }
206 runtime->DeoptimizeBootImage();
207 }
208}
209
Alex Light0fa17862017-10-24 13:43:05 -0700210bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
Alex Lightf2858632018-04-02 11:28:50 -0700211 art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700212 return MethodHasBreakpointsLocked(method);
213}
214
215bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
Alex Light0fa17862017-10-24 13:43:05 -0700216 auto elem = breakpoint_status_.find(method);
217 return elem != breakpoint_status_.end() && elem->second != 0;
218}
219
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000220void DeoptManager::RemoveDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700221 art::Thread* self = art::Thread::Current();
222 art::ScopedThreadSuspension sts(self, art::kSuspended);
223 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000224 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700225}
226
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000227void DeoptManager::AddDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700228 art::Thread* self = art::Thread::Current();
229 art::ScopedThreadSuspension sts(self, art::kSuspended);
230 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000231 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700232}
233
234void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
235 DCHECK(method->IsInvokable());
236 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
237 DCHECK(!method->IsNative()) << method->PrettyMethod();
238
239 art::Thread* self = art::Thread::Current();
240 method = method->GetCanonicalMethod();
241 bool is_default = method->IsDefault();
242
243 art::ScopedThreadSuspension sts(self, art::kSuspended);
244 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700245 {
246 breakpoint_status_lock_.ExclusiveLock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700247
Alex Lightf2858632018-04-02 11:28:50 -0700248 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
Alex Light0fa17862017-10-24 13:43:05 -0700249
Alex Lightf2858632018-04-02 11:28:50 -0700250 if (MethodHasBreakpointsLocked(method)) {
251 // Don't need to do anything extra.
252 breakpoint_status_[method]++;
253 // Another thread might be deoptimizing the very method we just added new breakpoints for.
254 // Wait for any deopts to finish before moving on.
255 breakpoint_status_lock_.ExclusiveUnlock(self);
256 WaitForDeoptimizationToFinish(self);
257 return;
258 }
259 breakpoint_status_[method] = 1;
260 breakpoint_status_lock_.ExclusiveUnlock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700261 }
Alex Light0fa17862017-10-24 13:43:05 -0700262 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
263 if (instrumentation->IsForcedInterpretOnly()) {
264 // We are already interpreting everything so no need to do anything.
265 deoptimization_status_lock_.ExclusiveUnlock(self);
266 return;
267 } else if (is_default) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000268 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700269 } else {
270 PerformLimitedDeoptimization(self, method);
271 }
272}
273
274void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
275 DCHECK(method->IsInvokable()) << method->PrettyMethod();
276 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
277 DCHECK(!method->IsNative()) << method->PrettyMethod();
278
279 art::Thread* self = art::Thread::Current();
280 method = method->GetCanonicalMethod();
281 bool is_default = method->IsDefault();
282
283 art::ScopedThreadSuspension sts(self, art::kSuspended);
284 // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
285 // need but since that is very heavy we will instead just use a condition variable to make sure we
286 // don't race with ourselves.
287 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700288 bool is_last_breakpoint;
289 {
290 art::MutexLock mu(self, breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700291
Alex Lightf2858632018-04-02 11:28:50 -0700292 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
293 DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
294 << "breakpoints present!";
295 breakpoint_status_[method] -= 1;
296 is_last_breakpoint = (breakpoint_status_[method] == 0);
297 }
Alex Light0fa17862017-10-24 13:43:05 -0700298 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
Alex Light0fa17862017-10-24 13:43:05 -0700299 if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
300 // We don't need to do anything since we are interpreting everything anyway.
301 deoptimization_status_lock_.ExclusiveUnlock(self);
302 return;
Alex Lightf2858632018-04-02 11:28:50 -0700303 } else if (is_last_breakpoint) {
Alex Light0fa17862017-10-24 13:43:05 -0700304 if (UNLIKELY(is_default)) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000305 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700306 } else {
307 PerformLimitedUndeoptimization(self, method);
308 }
309 } else {
310 // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
311 // for any deopts to finish before moving on.
312 WaitForDeoptimizationToFinish(self);
313 }
314}
315
316void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
317 while (performing_deoptimization_) {
318 deoptimization_condition_.Wait(self);
319 }
320}
321
322void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
323 WaitForDeoptimizationToFinishLocked(self);
324 deoptimization_status_lock_.ExclusiveUnlock(self);
325}
326
Alex Light3b8aa772018-08-13 15:55:44 -0700327// Users should make sure that only gc-critical-section safe code is used while a
328// ScopedDeoptimizationContext exists.
Alex Light0fa17862017-10-24 13:43:05 -0700329class ScopedDeoptimizationContext : public art::ValueObject {
330 public:
331 ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
332 RELEASE(deopt->deoptimization_status_lock_)
333 ACQUIRE(art::Locks::mutator_lock_)
334 ACQUIRE(art::Roles::uninterruptible_)
Alex Light3b8aa772018-08-13 15:55:44 -0700335 : self_(self),
336 deopt_(deopt),
337 critical_section_(self_, "JVMTI Deoptimizing methods"),
338 uninterruptible_cause_(nullptr) {
Alex Light0fa17862017-10-24 13:43:05 -0700339 deopt_->WaitForDeoptimizationToFinishLocked(self_);
340 DCHECK(!deopt->performing_deoptimization_)
341 << "Already performing deoptimization on another thread!";
342 // Use performing_deoptimization_ to keep track of the lock.
343 deopt_->performing_deoptimization_ = true;
344 deopt_->deoptimization_status_lock_.Unlock(self_);
Alex Light3b8aa772018-08-13 15:55:44 -0700345 uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation,
346 art::gc::kCollectorTypeCriticalSection);
Alex Light0fa17862017-10-24 13:43:05 -0700347 art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
Andreas Gampe6e897762018-10-16 13:09:32 -0700348 /*long_suspend=*/ false);
Alex Light0fa17862017-10-24 13:43:05 -0700349 }
350
351 ~ScopedDeoptimizationContext()
352 RELEASE(art::Locks::mutator_lock_)
353 RELEASE(art::Roles::uninterruptible_) {
354 // Can be suspended again.
Alex Light3b8aa772018-08-13 15:55:44 -0700355 critical_section_.Exit(uninterruptible_cause_);
Alex Light0fa17862017-10-24 13:43:05 -0700356 // Release the mutator lock.
357 art::Runtime::Current()->GetThreadList()->ResumeAll();
358 // Let other threads know it's fine to proceed.
359 art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
360 deopt_->performing_deoptimization_ = false;
361 deopt_->deoptimization_condition_.Broadcast(self_);
362 }
363
364 private:
365 art::Thread* self_;
366 DeoptManager* deopt_;
Alex Light3b8aa772018-08-13 15:55:44 -0700367 art::gc::GCCriticalSection critical_section_;
Alex Light0fa17862017-10-24 13:43:05 -0700368 const char* uninterruptible_cause_;
369};
370
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000371void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700372 global_deopt_count_++;
373 if (global_deopt_count_ == 1) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000374 PerformGlobalDeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700375 } else {
376 WaitForDeoptimizationToFinish(self);
377 }
378}
379
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000380void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
Roland Levillainef012222017-06-21 16:28:06 +0100381 DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!";
Alex Light0fa17862017-10-24 13:43:05 -0700382 global_deopt_count_--;
383 if (global_deopt_count_ == 0) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000384 PerformGlobalUndeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700385 } else {
386 WaitForDeoptimizationToFinish(self);
387 }
388}
389
390void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
391 ScopedDeoptimizationContext sdc(self, this);
392 art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
393}
394
395void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
396 ScopedDeoptimizationContext sdc(self, this);
397 art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
398}
399
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000400void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700401 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000402 art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
403 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700404}
405
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000406void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700407 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000408 art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
409 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700410}
411
Alex Light3dacdd62019-03-12 15:45:47 +0000412jvmtiError DeoptManager::AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
413 art::Locks::thread_list_lock_->ExclusiveLock(soa.Self());
414 art::Thread* target = nullptr;
415 jvmtiError err = OK;
416 if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
417 art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
418 return err;
419 }
420 // We don't need additional locking here because we hold the Thread_list_lock_.
Alex Lightb2146942019-03-12 15:46:40 +0000421 if (target->IncrementForceInterpreterCount() == 1) {
Alex Light3dacdd62019-03-12 15:45:47 +0000422 struct DeoptClosure : public art::Closure {
423 public:
Orion Hodson26ab2702020-07-29 09:54:10 +0100424 explicit DeoptClosure(DeoptManager* manager) : manager_(manager) {}
Alex Light3dacdd62019-03-12 15:45:47 +0000425 void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Orion Hodson26ab2702020-07-29 09:54:10 +0100426 manager_->DeoptimizeThread(self);
Alex Light3dacdd62019-03-12 15:45:47 +0000427 }
428
429 private:
Orion Hodson26ab2702020-07-29 09:54:10 +0100430 DeoptManager* manager_;
Alex Light3dacdd62019-03-12 15:45:47 +0000431 };
432 DeoptClosure c(this);
433 target->RequestSynchronousCheckpoint(&c);
434 } else {
435 art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
436 }
437 return OK;
438}
439
440jvmtiError DeoptManager::RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
441 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
442 art::Thread* target = nullptr;
443 jvmtiError err = OK;
444 if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
445 return err;
446 }
447 // We don't need additional locking here because we hold the Thread_list_lock_.
448 DCHECK_GT(target->ForceInterpreterCount(), 0u);
449 target->DecrementForceInterpreterCount();
450 return OK;
451}
Alex Light0fa17862017-10-24 13:43:05 -0700452
453void DeoptManager::RemoveDeoptimizationRequester() {
454 art::Thread* self = art::Thread::Current();
455 art::ScopedThreadStateChange sts(self, art::kSuspended);
456 deoptimization_status_lock_.ExclusiveLock(self);
457 DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
458 deopter_count_--;
459 if (deopter_count_ == 0) {
460 ScopedDeoptimizationContext sdc(self, this);
461 // TODO Give this a real key.
462 art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization("");
463 return;
464 } else {
465 deoptimization_status_lock_.ExclusiveUnlock(self);
466 }
467}
468
469void DeoptManager::AddDeoptimizationRequester() {
470 art::Thread* self = art::Thread::Current();
471 art::ScopedThreadStateChange stsc(self, art::kSuspended);
472 deoptimization_status_lock_.ExclusiveLock(self);
473 deopter_count_++;
474 if (deopter_count_ == 1) {
475 ScopedDeoptimizationContext sdc(self, this);
Alex Light40607862019-05-06 18:16:24 +0000476 art::instrumentation::Instrumentation* instrumentation =
477 art::Runtime::Current()->GetInstrumentation();
478 // Enable deoptimization
479 instrumentation->EnableDeoptimization();
480 // Tell instrumentation we will be deopting single threads.
481 instrumentation->EnableSingleThreadDeopt();
Alex Light0fa17862017-10-24 13:43:05 -0700482 } else {
483 deoptimization_status_lock_.ExclusiveUnlock(self);
484 }
485}
486
487void DeoptManager::DeoptimizeThread(art::Thread* target) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700488 // We might or might not be running on the target thread (self) so get Thread::Current
489 // directly.
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000490 art::ScopedThreadSuspension sts(art::Thread::Current(), art::kSuspended);
Alex Lighta4cdd362019-04-18 09:17:10 -0700491 art::gc::ScopedGCCriticalSection sgccs(art::Thread::Current(),
492 art::gc::GcCause::kGcCauseDebugger,
493 art::gc::CollectorType::kCollectorTypeDebugger);
Nicolas Geoffraye91e7952020-01-23 10:15:56 +0000494 art::ScopedSuspendAll ssa("Instrument thread stack");
Alex Light0fa17862017-10-24 13:43:05 -0700495 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target);
496}
497
Alex Light0e841182018-02-12 17:42:50 +0000498extern DeoptManager* gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700499DeoptManager* DeoptManager::Get() {
Alex Light0e841182018-02-12 17:42:50 +0000500 return gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700501}
502
503} // namespace openjdkjvmti