blob: a7feba81e1a9cfc0c0354bde86e78627fd2d89ed [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 Light60fbefc2018-04-18 15:19:15 -070048#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010049#include "jni/jni_internal.h"
Alex Light0fa17862017-10-24 13:43:05 -070050#include "mirror/class-inl.h"
51#include "mirror/object_array-inl.h"
Alex Light0fa17862017-10-24 13:43:05 -070052#include "nativehelper/scoped_local_ref.h"
Alex Lighta4cdd362019-04-18 09:17:10 -070053#include "read_barrier_config.h"
Alex Light0fa17862017-10-24 13:43:05 -070054#include "runtime_callbacks.h"
55#include "scoped_thread_state_change-inl.h"
Alex Light3dacdd62019-03-12 15:45:47 +000056#include "scoped_thread_state_change.h"
Alex Light0fa17862017-10-24 13:43:05 -070057#include "thread-current-inl.h"
58#include "thread_list.h"
59#include "ti_phase.h"
60
61namespace openjdkjvmti {
62
63// 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 -070064// actually care about the method at this time (ie active frames had locals changed). For now we
65// just assume that if anything has changed any frame's locals we care about all methods. If nothing
66// has we only care about methods with active breakpoints on them. In the future we should probably
67// rewrite all of this to instead do this at the ShadowFrame or thread granularity.
68bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(art::ArtMethod* method) {
69 // Non-java-debuggable runtimes we need to assume that any method might not be debuggable and
70 // therefore potentially being inspected (due to inlines). If we are debuggable we rely hard on
71 // inlining not being done since we don't keep track of which methods get inlined where and simply
72 // look to see if the method is breakpointed.
73 return !art::Runtime::Current()->IsJavaDebuggable() ||
74 manager_->HaveLocalsChanged() ||
75 manager_->MethodHasBreakpoints(method);
Alex Light0fa17862017-10-24 13:43:05 -070076}
77
78bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) {
79 return !manager_->MethodHasBreakpoints(method);
80}
81
Alex Lightf2858632018-04-02 11:28:50 -070082bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion(
83 art::ArtMethod* method ATTRIBUTE_UNUSED) {
84 return true;
85}
86
Alex Light0fa17862017-10-24 13:43:05 -070087DeoptManager::DeoptManager()
Alex Light2ce6fc82017-12-18 16:42:36 -080088 : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock",
89 static_cast<art::LockLevel>(
90 art::LockLevel::kClassLinkerClassesLock + 1)),
Alex Light0fa17862017-10-24 13:43:05 -070091 deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_),
92 performing_deoptimization_(false),
93 global_deopt_count_(0),
94 deopter_count_(0),
Alex Lightf2858632018-04-02 11:28:50 -070095 breakpoint_status_lock_("JVMTI_BreakpointStatusLock",
96 static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)),
97 inspection_callback_(this),
David Srbeckyd25eb2c2018-07-19 12:17:04 +000098 set_local_variable_called_(false) { }
Alex Light0fa17862017-10-24 13:43:05 -070099
100void DeoptManager::Setup() {
101 art::ScopedThreadStateChange stsc(art::Thread::Current(),
102 art::ThreadState::kWaitingForDebuggerToAttach);
103 art::ScopedSuspendAll ssa("Add method Inspection Callback");
104 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
105 callbacks->AddMethodInspectionCallback(&inspection_callback_);
106}
107
108void DeoptManager::Shutdown() {
109 art::ScopedThreadStateChange stsc(art::Thread::Current(),
110 art::ThreadState::kWaitingForDebuggerToAttach);
111 art::ScopedSuspendAll ssa("remove method Inspection Callback");
112 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
113 callbacks->RemoveMethodInspectionCallback(&inspection_callback_);
114}
115
Alex Lightb2146942019-03-12 15:46:40 +0000116void DeoptManager::DumpDeoptInfo(art::Thread* self, std::ostream& stream) {
117 art::ScopedObjectAccess soa(self);
118 art::MutexLock mutll(self, *art::Locks::thread_list_lock_);
119 art::MutexLock mudsl(self, deoptimization_status_lock_);
120 art::MutexLock mubsl(self, breakpoint_status_lock_);
121 stream << "Deoptimizer count: " << deopter_count_ << "\n";
122 stream << "Global deopt count: " << global_deopt_count_ << "\n";
123 stream << "Can perform OSR: " << !set_local_variable_called_.load() << "\n";
124 for (const auto& [bp, loc] : this->breakpoint_status_) {
125 stream << "Breakpoint: " << bp->PrettyMethod() << " @ 0x" << std::hex << loc << "\n";
126 }
127 struct DumpThreadDeoptCount : public art::Closure {
128 public:
129 DumpThreadDeoptCount(std::ostream& stream, std::mutex& mu)
130 : cnt_(0), stream_(stream), mu_(mu) {}
131 void Run(art::Thread* self) override {
132 {
133 std::lock_guard<std::mutex> lg(mu_);
134 std::string name;
135 self->GetThreadName(name);
136 stream_ << "Thread " << name << " (id: " << std::dec << self->GetThreadId()
137 << ") force interpreter count " << self->ForceInterpreterCount() << "\n";
138 }
139 // Increment this after unlocking the mutex so we won't race its destructor.
140 cnt_++;
141 }
142
143 void WaitForCount(size_t threads) {
144 while (cnt_.load() != threads) {
145 sched_yield();
146 }
147 }
148
149 private:
150 std::atomic<size_t> cnt_;
151 std::ostream& stream_;
152 std::mutex& mu_;
153 };
154
155 std::mutex mu;
156 DumpThreadDeoptCount dtdc(stream, mu);
157 auto func = [](art::Thread* thread, void* ctx) {
158 reinterpret_cast<DumpThreadDeoptCount*>(ctx)->Run(thread);
159 };
160 art::Runtime::Current()->GetThreadList()->ForEach(func, &dtdc);
161}
162
Alex Light2ce6fc82017-12-18 16:42:36 -0800163void DeoptManager::FinishSetup() {
164 art::Thread* self = art::Thread::Current();
165 art::MutexLock mu(self, deoptimization_status_lock_);
166
167 art::Runtime* runtime = art::Runtime::Current();
168 // See if we need to do anything.
169 if (!runtime->IsJavaDebuggable()) {
170 // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be
171 // retrieved and they will all be best-effort.
172 if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) {
173 // We are still early enough to change the compiler options and get full JVMTI support.
174 LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to "
175 << "debuggable state. Please pass '--debuggable' to dex2oat and "
176 << "'-Xcompiler-option --debuggable' to dalvikvm in the future.";
177 DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!";
178 runtime->AddCompilerOption("--debuggable");
179 runtime->SetJavaDebuggable(true);
180 } else {
181 LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was "
182 << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion "
183 << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some "
184 << "functionality might not work properly.";
Alex Light60fbefc2018-04-18 15:19:15 -0700185 if (runtime->GetJit() == nullptr &&
186 runtime->GetJITOptions()->UseJitCompilation() &&
187 !runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
188 // If we don't have a jit we should try to start the jit for performance reasons. We only
189 // need to do this for late attach on non-debuggable processes because for debuggable
190 // processes we already rely on jit and we cannot force this jit to start if we are still in
191 // OnLoad since the runtime hasn't started up sufficiently. This is only expected to happen
192 // on userdebug/eng builds.
193 LOG(INFO) << "Attempting to start jit for openjdkjvmti plugin.";
Andreas Gampec6bd42a2018-11-07 13:39:41 -0800194 // Note: use rwx allowed = true, because if this is the system server, we will not be
195 // allowed to allocate any JIT code cache, anyways.
196 runtime->CreateJitCodeCache(/*rwx_memory_allowed=*/true);
Alex Light60fbefc2018-04-18 15:19:15 -0700197 runtime->CreateJit();
198 if (runtime->GetJit() == nullptr) {
199 LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be "
200 << "quite slow as it is running entirely in the interpreter. Try running "
201 << "'setenforce 0' and restarting this process.";
202 }
203 }
Alex Light2ce6fc82017-12-18 16:42:36 -0800204 }
205 runtime->DeoptimizeBootImage();
206 }
207}
208
Alex Light0fa17862017-10-24 13:43:05 -0700209bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
Alex Lightf2858632018-04-02 11:28:50 -0700210 art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700211 return MethodHasBreakpointsLocked(method);
212}
213
214bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
Alex Light0fa17862017-10-24 13:43:05 -0700215 auto elem = breakpoint_status_.find(method);
216 return elem != breakpoint_status_.end() && elem->second != 0;
217}
218
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000219void DeoptManager::RemoveDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700220 art::Thread* self = art::Thread::Current();
221 art::ScopedThreadSuspension sts(self, art::kSuspended);
222 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000223 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700224}
225
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000226void DeoptManager::AddDeoptimizeAllMethods() {
Alex Light0fa17862017-10-24 13:43:05 -0700227 art::Thread* self = art::Thread::Current();
228 art::ScopedThreadSuspension sts(self, art::kSuspended);
229 deoptimization_status_lock_.ExclusiveLock(self);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000230 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700231}
232
233void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
234 DCHECK(method->IsInvokable());
235 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
236 DCHECK(!method->IsNative()) << method->PrettyMethod();
237
238 art::Thread* self = art::Thread::Current();
239 method = method->GetCanonicalMethod();
240 bool is_default = method->IsDefault();
241
242 art::ScopedThreadSuspension sts(self, art::kSuspended);
243 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700244 {
245 breakpoint_status_lock_.ExclusiveLock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700246
Alex Lightf2858632018-04-02 11:28:50 -0700247 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
Alex Light0fa17862017-10-24 13:43:05 -0700248
Alex Lightf2858632018-04-02 11:28:50 -0700249 if (MethodHasBreakpointsLocked(method)) {
250 // Don't need to do anything extra.
251 breakpoint_status_[method]++;
252 // Another thread might be deoptimizing the very method we just added new breakpoints for.
253 // Wait for any deopts to finish before moving on.
254 breakpoint_status_lock_.ExclusiveUnlock(self);
255 WaitForDeoptimizationToFinish(self);
256 return;
257 }
258 breakpoint_status_[method] = 1;
259 breakpoint_status_lock_.ExclusiveUnlock(self);
Alex Light0fa17862017-10-24 13:43:05 -0700260 }
Alex Light0fa17862017-10-24 13:43:05 -0700261 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
262 if (instrumentation->IsForcedInterpretOnly()) {
263 // We are already interpreting everything so no need to do anything.
264 deoptimization_status_lock_.ExclusiveUnlock(self);
265 return;
266 } else if (is_default) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000267 AddDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700268 } else {
269 PerformLimitedDeoptimization(self, method);
270 }
271}
272
273void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
274 DCHECK(method->IsInvokable()) << method->PrettyMethod();
275 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
276 DCHECK(!method->IsNative()) << method->PrettyMethod();
277
278 art::Thread* self = art::Thread::Current();
279 method = method->GetCanonicalMethod();
280 bool is_default = method->IsDefault();
281
282 art::ScopedThreadSuspension sts(self, art::kSuspended);
283 // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
284 // need but since that is very heavy we will instead just use a condition variable to make sure we
285 // don't race with ourselves.
286 deoptimization_status_lock_.ExclusiveLock(self);
Alex Lightf2858632018-04-02 11:28:50 -0700287 bool is_last_breakpoint;
288 {
289 art::MutexLock mu(self, breakpoint_status_lock_);
Alex Light0fa17862017-10-24 13:43:05 -0700290
Alex Lightf2858632018-04-02 11:28:50 -0700291 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
292 DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
293 << "breakpoints present!";
294 breakpoint_status_[method] -= 1;
295 is_last_breakpoint = (breakpoint_status_[method] == 0);
296 }
Alex Light0fa17862017-10-24 13:43:05 -0700297 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
Alex Light0fa17862017-10-24 13:43:05 -0700298 if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
299 // We don't need to do anything since we are interpreting everything anyway.
300 deoptimization_status_lock_.ExclusiveUnlock(self);
301 return;
Alex Lightf2858632018-04-02 11:28:50 -0700302 } else if (is_last_breakpoint) {
Alex Light0fa17862017-10-24 13:43:05 -0700303 if (UNLIKELY(is_default)) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000304 RemoveDeoptimizeAllMethodsLocked(self);
Alex Light0fa17862017-10-24 13:43:05 -0700305 } else {
306 PerformLimitedUndeoptimization(self, method);
307 }
308 } else {
309 // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
310 // for any deopts to finish before moving on.
311 WaitForDeoptimizationToFinish(self);
312 }
313}
314
315void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
316 while (performing_deoptimization_) {
317 deoptimization_condition_.Wait(self);
318 }
319}
320
321void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
322 WaitForDeoptimizationToFinishLocked(self);
323 deoptimization_status_lock_.ExclusiveUnlock(self);
324}
325
Alex Light3b8aa772018-08-13 15:55:44 -0700326// Users should make sure that only gc-critical-section safe code is used while a
327// ScopedDeoptimizationContext exists.
Alex Light0fa17862017-10-24 13:43:05 -0700328class ScopedDeoptimizationContext : public art::ValueObject {
329 public:
330 ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
331 RELEASE(deopt->deoptimization_status_lock_)
332 ACQUIRE(art::Locks::mutator_lock_)
333 ACQUIRE(art::Roles::uninterruptible_)
Alex Light3b8aa772018-08-13 15:55:44 -0700334 : self_(self),
335 deopt_(deopt),
336 critical_section_(self_, "JVMTI Deoptimizing methods"),
337 uninterruptible_cause_(nullptr) {
Alex Light0fa17862017-10-24 13:43:05 -0700338 deopt_->WaitForDeoptimizationToFinishLocked(self_);
339 DCHECK(!deopt->performing_deoptimization_)
340 << "Already performing deoptimization on another thread!";
341 // Use performing_deoptimization_ to keep track of the lock.
342 deopt_->performing_deoptimization_ = true;
343 deopt_->deoptimization_status_lock_.Unlock(self_);
Alex Light3b8aa772018-08-13 15:55:44 -0700344 uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation,
345 art::gc::kCollectorTypeCriticalSection);
Alex Light0fa17862017-10-24 13:43:05 -0700346 art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
Andreas Gampe6e897762018-10-16 13:09:32 -0700347 /*long_suspend=*/ false);
Alex Light0fa17862017-10-24 13:43:05 -0700348 }
349
350 ~ScopedDeoptimizationContext()
351 RELEASE(art::Locks::mutator_lock_)
352 RELEASE(art::Roles::uninterruptible_) {
353 // Can be suspended again.
Alex Light3b8aa772018-08-13 15:55:44 -0700354 critical_section_.Exit(uninterruptible_cause_);
Alex Light0fa17862017-10-24 13:43:05 -0700355 // Release the mutator lock.
356 art::Runtime::Current()->GetThreadList()->ResumeAll();
357 // Let other threads know it's fine to proceed.
358 art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
359 deopt_->performing_deoptimization_ = false;
360 deopt_->deoptimization_condition_.Broadcast(self_);
361 }
362
363 private:
364 art::Thread* self_;
365 DeoptManager* deopt_;
Alex Light3b8aa772018-08-13 15:55:44 -0700366 art::gc::GCCriticalSection critical_section_;
Alex Light0fa17862017-10-24 13:43:05 -0700367 const char* uninterruptible_cause_;
368};
369
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000370void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700371 global_deopt_count_++;
372 if (global_deopt_count_ == 1) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000373 PerformGlobalDeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700374 } else {
375 WaitForDeoptimizationToFinish(self);
376 }
377}
378
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000379void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
Roland Levillainef012222017-06-21 16:28:06 +0100380 DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!";
Alex Light0fa17862017-10-24 13:43:05 -0700381 global_deopt_count_--;
382 if (global_deopt_count_ == 0) {
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000383 PerformGlobalUndeoptimization(self);
Alex Light0fa17862017-10-24 13:43:05 -0700384 } else {
385 WaitForDeoptimizationToFinish(self);
386 }
387}
388
389void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
390 ScopedDeoptimizationContext sdc(self, this);
391 art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
392}
393
394void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
395 ScopedDeoptimizationContext sdc(self, this);
396 art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
397}
398
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000399void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700400 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000401 art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
402 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700403}
404
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000405void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
Alex Light0fa17862017-10-24 13:43:05 -0700406 ScopedDeoptimizationContext sdc(self, this);
David Srbeckyd25eb2c2018-07-19 12:17:04 +0000407 art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
408 kDeoptManagerInstrumentationKey);
Alex Light0fa17862017-10-24 13:43:05 -0700409}
410
Alex Light3dacdd62019-03-12 15:45:47 +0000411jvmtiError DeoptManager::AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
412 art::Locks::thread_list_lock_->ExclusiveLock(soa.Self());
413 art::Thread* target = nullptr;
414 jvmtiError err = OK;
415 if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
416 art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
417 return err;
418 }
419 // We don't need additional locking here because we hold the Thread_list_lock_.
Alex Lightb2146942019-03-12 15:46:40 +0000420 if (target->IncrementForceInterpreterCount() == 1) {
Alex Light3dacdd62019-03-12 15:45:47 +0000421 struct DeoptClosure : public art::Closure {
422 public:
423 explicit DeoptClosure(DeoptManager* man) : man_(man) {}
424 void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) {
425 man_->DeoptimizeThread(self);
426 }
427
428 private:
429 DeoptManager* man_;
430 };
431 DeoptClosure c(this);
432 target->RequestSynchronousCheckpoint(&c);
433 } else {
434 art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self());
435 }
436 return OK;
437}
438
439jvmtiError DeoptManager::RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) {
440 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
441 art::Thread* target = nullptr;
442 jvmtiError err = OK;
443 if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) {
444 return err;
445 }
446 // We don't need additional locking here because we hold the Thread_list_lock_.
447 DCHECK_GT(target->ForceInterpreterCount(), 0u);
448 target->DecrementForceInterpreterCount();
449 return OK;
450}
Alex Light0fa17862017-10-24 13:43:05 -0700451
452void DeoptManager::RemoveDeoptimizationRequester() {
453 art::Thread* self = art::Thread::Current();
454 art::ScopedThreadStateChange sts(self, art::kSuspended);
455 deoptimization_status_lock_.ExclusiveLock(self);
456 DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
457 deopter_count_--;
458 if (deopter_count_ == 0) {
459 ScopedDeoptimizationContext sdc(self, this);
460 // TODO Give this a real key.
461 art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization("");
462 return;
463 } else {
464 deoptimization_status_lock_.ExclusiveUnlock(self);
465 }
466}
467
468void DeoptManager::AddDeoptimizationRequester() {
469 art::Thread* self = art::Thread::Current();
470 art::ScopedThreadStateChange stsc(self, art::kSuspended);
471 deoptimization_status_lock_.ExclusiveLock(self);
472 deopter_count_++;
473 if (deopter_count_ == 1) {
474 ScopedDeoptimizationContext sdc(self, this);
475 art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization();
476 return;
477 } else {
478 deoptimization_status_lock_.ExclusiveUnlock(self);
479 }
480}
481
482void DeoptManager::DeoptimizeThread(art::Thread* target) {
Alex Lighta4cdd362019-04-18 09:17:10 -0700483 // We might or might not be running on the target thread (self) so get Thread::Current
484 // directly.
485 art::gc::ScopedGCCriticalSection sgccs(art::Thread::Current(),
486 art::gc::GcCause::kGcCauseDebugger,
487 art::gc::CollectorType::kCollectorTypeDebugger);
Alex Light0fa17862017-10-24 13:43:05 -0700488 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target);
489}
490
Alex Light0e841182018-02-12 17:42:50 +0000491extern DeoptManager* gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700492DeoptManager* DeoptManager::Get() {
Alex Light0e841182018-02-12 17:42:50 +0000493 return gDeoptManager;
Alex Light0fa17862017-10-24 13:43:05 -0700494}
495
496} // namespace openjdkjvmti