blob: f843054681f7b245a6ebe2ec63ceed4a24482cd6 [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>
33
34#include "deopt_manager.h"
35
36#include "art_jvmti.h"
37#include "art_method-inl.h"
38#include "base/enums.h"
39#include "base/mutex-inl.h"
40#include "dex_file_annotations.h"
41#include "events-inl.h"
42#include "jni_internal.h"
43#include "mirror/class-inl.h"
44#include "mirror/object_array-inl.h"
45#include "modifiers.h"
46#include "nativehelper/scoped_local_ref.h"
47#include "runtime_callbacks.h"
48#include "scoped_thread_state_change-inl.h"
49#include "thread-current-inl.h"
50#include "thread_list.h"
51#include "ti_phase.h"
52
53namespace openjdkjvmti {
54
55// TODO We should make this much more selective in the future so we only return true when we
56// actually care about the method (i.e. had locals changed, have breakpoints, etc.). For now though
57// we can just assume that we care we are loaded at all.
58//
59// Even if we don't keep track of this at the method level we might want to keep track of it at the
60// level of enabled capabilities.
61bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(
62 art::ArtMethod* method ATTRIBUTE_UNUSED) {
63 return true;
64}
65
66bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) {
67 return !manager_->MethodHasBreakpoints(method);
68}
69
70DeoptManager::DeoptManager()
71 : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock"),
72 deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_),
73 performing_deoptimization_(false),
74 global_deopt_count_(0),
75 deopter_count_(0),
76 inspection_callback_(this) { }
77
78void DeoptManager::Setup() {
79 art::ScopedThreadStateChange stsc(art::Thread::Current(),
80 art::ThreadState::kWaitingForDebuggerToAttach);
81 art::ScopedSuspendAll ssa("Add method Inspection Callback");
82 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
83 callbacks->AddMethodInspectionCallback(&inspection_callback_);
84}
85
86void DeoptManager::Shutdown() {
87 art::ScopedThreadStateChange stsc(art::Thread::Current(),
88 art::ThreadState::kWaitingForDebuggerToAttach);
89 art::ScopedSuspendAll ssa("remove method Inspection Callback");
90 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
91 callbacks->RemoveMethodInspectionCallback(&inspection_callback_);
92}
93
94bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) {
95 art::MutexLock lk(art::Thread::Current(), deoptimization_status_lock_);
96 return MethodHasBreakpointsLocked(method);
97}
98
99bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) {
100 if (deopter_count_ == 0) {
101 return false;
102 }
103 auto elem = breakpoint_status_.find(method);
104 return elem != breakpoint_status_.end() && elem->second != 0;
105}
106
107void DeoptManager::RemoveDeoptimizeAllMethods() {
108 art::Thread* self = art::Thread::Current();
109 art::ScopedThreadSuspension sts(self, art::kSuspended);
110 deoptimization_status_lock_.ExclusiveLock(self);
111 RemoveDeoptimizeAllMethodsLocked(self);
112}
113
114void DeoptManager::AddDeoptimizeAllMethods() {
115 art::Thread* self = art::Thread::Current();
116 art::ScopedThreadSuspension sts(self, art::kSuspended);
117 deoptimization_status_lock_.ExclusiveLock(self);
118 AddDeoptimizeAllMethodsLocked(self);
119}
120
121void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) {
122 DCHECK(method->IsInvokable());
123 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
124 DCHECK(!method->IsNative()) << method->PrettyMethod();
125
126 art::Thread* self = art::Thread::Current();
127 method = method->GetCanonicalMethod();
128 bool is_default = method->IsDefault();
129
130 art::ScopedThreadSuspension sts(self, art::kSuspended);
131 deoptimization_status_lock_.ExclusiveLock(self);
132
133 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
134
135 if (MethodHasBreakpointsLocked(method)) {
136 // Don't need to do anything extra.
137 breakpoint_status_[method]++;
138 // Another thread might be deoptimizing the very method we just added new breakpoints for. Wait
139 // for any deopts to finish before moving on.
140 WaitForDeoptimizationToFinish(self);
141 return;
142 }
143 breakpoint_status_[method] = 1;
144 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
145 if (instrumentation->IsForcedInterpretOnly()) {
146 // We are already interpreting everything so no need to do anything.
147 deoptimization_status_lock_.ExclusiveUnlock(self);
148 return;
149 } else if (is_default) {
150 AddDeoptimizeAllMethodsLocked(self);
151 } else {
152 PerformLimitedDeoptimization(self, method);
153 }
154}
155
156void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) {
157 DCHECK(method->IsInvokable()) << method->PrettyMethod();
158 DCHECK(!method->IsProxyMethod()) << method->PrettyMethod();
159 DCHECK(!method->IsNative()) << method->PrettyMethod();
160
161 art::Thread* self = art::Thread::Current();
162 method = method->GetCanonicalMethod();
163 bool is_default = method->IsDefault();
164
165 art::ScopedThreadSuspension sts(self, art::kSuspended);
166 // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might
167 // need but since that is very heavy we will instead just use a condition variable to make sure we
168 // don't race with ourselves.
169 deoptimization_status_lock_.ExclusiveLock(self);
170
171 DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request";
172 DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without "
173 << "breakpoints present!";
174 auto instrumentation = art::Runtime::Current()->GetInstrumentation();
175 breakpoint_status_[method] -= 1;
176 if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) {
177 // We don't need to do anything since we are interpreting everything anyway.
178 deoptimization_status_lock_.ExclusiveUnlock(self);
179 return;
180 } else if (breakpoint_status_[method] == 0) {
181 if (UNLIKELY(is_default)) {
182 RemoveDeoptimizeAllMethodsLocked(self);
183 } else {
184 PerformLimitedUndeoptimization(self, method);
185 }
186 } else {
187 // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait
188 // for any deopts to finish before moving on.
189 WaitForDeoptimizationToFinish(self);
190 }
191}
192
193void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) {
194 while (performing_deoptimization_) {
195 deoptimization_condition_.Wait(self);
196 }
197}
198
199void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) {
200 WaitForDeoptimizationToFinishLocked(self);
201 deoptimization_status_lock_.ExclusiveUnlock(self);
202}
203
204class ScopedDeoptimizationContext : public art::ValueObject {
205 public:
206 ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt)
207 RELEASE(deopt->deoptimization_status_lock_)
208 ACQUIRE(art::Locks::mutator_lock_)
209 ACQUIRE(art::Roles::uninterruptible_)
210 : self_(self), deopt_(deopt), uninterruptible_cause_(nullptr) {
211 deopt_->WaitForDeoptimizationToFinishLocked(self_);
212 DCHECK(!deopt->performing_deoptimization_)
213 << "Already performing deoptimization on another thread!";
214 // Use performing_deoptimization_ to keep track of the lock.
215 deopt_->performing_deoptimization_ = true;
216 deopt_->deoptimization_status_lock_.Unlock(self_);
217 art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods",
218 /*long_suspend*/ false);
219 uninterruptible_cause_ = self_->StartAssertNoThreadSuspension("JVMTI deoptimizing methods");
220 }
221
222 ~ScopedDeoptimizationContext()
223 RELEASE(art::Locks::mutator_lock_)
224 RELEASE(art::Roles::uninterruptible_) {
225 // Can be suspended again.
226 self_->EndAssertNoThreadSuspension(uninterruptible_cause_);
227 // Release the mutator lock.
228 art::Runtime::Current()->GetThreadList()->ResumeAll();
229 // Let other threads know it's fine to proceed.
230 art::MutexLock lk(self_, deopt_->deoptimization_status_lock_);
231 deopt_->performing_deoptimization_ = false;
232 deopt_->deoptimization_condition_.Broadcast(self_);
233 }
234
235 private:
236 art::Thread* self_;
237 DeoptManager* deopt_;
238 const char* uninterruptible_cause_;
239};
240
241void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) {
242 global_deopt_count_++;
243 if (global_deopt_count_ == 1) {
244 PerformGlobalDeoptimization(self);
245 } else {
246 WaitForDeoptimizationToFinish(self);
247 }
248}
249
250void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) {
251 DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existant global deoptimization!";
252 global_deopt_count_--;
253 if (global_deopt_count_ == 0) {
254 PerformGlobalUndeoptimization(self);
255 } else {
256 WaitForDeoptimizationToFinish(self);
257 }
258}
259
260void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) {
261 ScopedDeoptimizationContext sdc(self, this);
262 art::Runtime::Current()->GetInstrumentation()->Deoptimize(method);
263}
264
265void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) {
266 ScopedDeoptimizationContext sdc(self, this);
267 art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method);
268}
269
270void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) {
271 ScopedDeoptimizationContext sdc(self, this);
272 art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything(
273 kDeoptManagerInstrumentationKey);
274}
275
276void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) {
277 ScopedDeoptimizationContext sdc(self, this);
278 art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything(
279 kDeoptManagerInstrumentationKey);
280}
281
282
283void DeoptManager::RemoveDeoptimizationRequester() {
284 art::Thread* self = art::Thread::Current();
285 art::ScopedThreadStateChange sts(self, art::kSuspended);
286 deoptimization_status_lock_.ExclusiveLock(self);
287 DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present";
288 deopter_count_--;
289 if (deopter_count_ == 0) {
290 ScopedDeoptimizationContext sdc(self, this);
291 // TODO Give this a real key.
292 art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization("");
293 return;
294 } else {
295 deoptimization_status_lock_.ExclusiveUnlock(self);
296 }
297}
298
299void DeoptManager::AddDeoptimizationRequester() {
300 art::Thread* self = art::Thread::Current();
301 art::ScopedThreadStateChange stsc(self, art::kSuspended);
302 deoptimization_status_lock_.ExclusiveLock(self);
303 deopter_count_++;
304 if (deopter_count_ == 1) {
305 ScopedDeoptimizationContext sdc(self, this);
306 art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization();
307 return;
308 } else {
309 deoptimization_status_lock_.ExclusiveUnlock(self);
310 }
311}
312
313void DeoptManager::DeoptimizeThread(art::Thread* target) {
314 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target);
315}
316
317extern DeoptManager gDeoptManager;
318DeoptManager* DeoptManager::Get() {
319 return &gDeoptManager;
320}
321
322} // namespace openjdkjvmti