Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 1 | /* 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" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 40 | #include "dex/dex_file_annotations.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 41 | #include "dex/modifiers.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 42 | #include "events-inl.h" |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 43 | #include "gc/heap.h" |
| 44 | #include "gc/scoped_gc_critical_section.h" |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 45 | #include "jit/jit.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 46 | #include "jni/jni_internal.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 47 | #include "mirror/class-inl.h" |
| 48 | #include "mirror/object_array-inl.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 49 | #include "nativehelper/scoped_local_ref.h" |
| 50 | #include "runtime_callbacks.h" |
| 51 | #include "scoped_thread_state_change-inl.h" |
| 52 | #include "thread-current-inl.h" |
| 53 | #include "thread_list.h" |
| 54 | #include "ti_phase.h" |
| 55 | |
| 56 | namespace openjdkjvmti { |
| 57 | |
| 58 | // TODO We should make this much more selective in the future so we only return true when we |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 59 | // actually care about the method at this time (ie active frames had locals changed). For now we |
| 60 | // just assume that if anything has changed any frame's locals we care about all methods. If nothing |
| 61 | // has we only care about methods with active breakpoints on them. In the future we should probably |
| 62 | // rewrite all of this to instead do this at the ShadowFrame or thread granularity. |
| 63 | bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(art::ArtMethod* method) { |
| 64 | // Non-java-debuggable runtimes we need to assume that any method might not be debuggable and |
| 65 | // therefore potentially being inspected (due to inlines). If we are debuggable we rely hard on |
| 66 | // inlining not being done since we don't keep track of which methods get inlined where and simply |
| 67 | // look to see if the method is breakpointed. |
| 68 | return !art::Runtime::Current()->IsJavaDebuggable() || |
| 69 | manager_->HaveLocalsChanged() || |
| 70 | manager_->MethodHasBreakpoints(method); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) { |
| 74 | return !manager_->MethodHasBreakpoints(method); |
| 75 | } |
| 76 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 77 | bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion( |
| 78 | art::ArtMethod* method ATTRIBUTE_UNUSED) { |
| 79 | return true; |
| 80 | } |
| 81 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 82 | DeoptManager::DeoptManager() |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 83 | : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock", |
| 84 | static_cast<art::LockLevel>( |
| 85 | art::LockLevel::kClassLinkerClassesLock + 1)), |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 86 | deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_), |
| 87 | performing_deoptimization_(false), |
| 88 | global_deopt_count_(0), |
| 89 | deopter_count_(0), |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 90 | breakpoint_status_lock_("JVMTI_BreakpointStatusLock", |
| 91 | static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)), |
| 92 | inspection_callback_(this), |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 93 | set_local_variable_called_(false) { } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 94 | |
| 95 | void DeoptManager::Setup() { |
| 96 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 97 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 98 | art::ScopedSuspendAll ssa("Add method Inspection Callback"); |
| 99 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 100 | callbacks->AddMethodInspectionCallback(&inspection_callback_); |
| 101 | } |
| 102 | |
| 103 | void DeoptManager::Shutdown() { |
| 104 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 105 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 106 | art::ScopedSuspendAll ssa("remove method Inspection Callback"); |
| 107 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 108 | callbacks->RemoveMethodInspectionCallback(&inspection_callback_); |
| 109 | } |
| 110 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 111 | void DeoptManager::FinishSetup() { |
| 112 | art::Thread* self = art::Thread::Current(); |
| 113 | art::MutexLock mu(self, deoptimization_status_lock_); |
| 114 | |
| 115 | art::Runtime* runtime = art::Runtime::Current(); |
| 116 | // See if we need to do anything. |
| 117 | if (!runtime->IsJavaDebuggable()) { |
| 118 | // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be |
| 119 | // retrieved and they will all be best-effort. |
| 120 | if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) { |
| 121 | // We are still early enough to change the compiler options and get full JVMTI support. |
| 122 | LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to " |
| 123 | << "debuggable state. Please pass '--debuggable' to dex2oat and " |
| 124 | << "'-Xcompiler-option --debuggable' to dalvikvm in the future."; |
| 125 | DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!"; |
| 126 | runtime->AddCompilerOption("--debuggable"); |
| 127 | runtime->SetJavaDebuggable(true); |
| 128 | } else { |
| 129 | LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was " |
| 130 | << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion " |
| 131 | << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some " |
| 132 | << "functionality might not work properly."; |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 133 | if (runtime->GetJit() == nullptr && |
| 134 | runtime->GetJITOptions()->UseJitCompilation() && |
| 135 | !runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
| 136 | // If we don't have a jit we should try to start the jit for performance reasons. We only |
| 137 | // need to do this for late attach on non-debuggable processes because for debuggable |
| 138 | // processes we already rely on jit and we cannot force this jit to start if we are still in |
| 139 | // OnLoad since the runtime hasn't started up sufficiently. This is only expected to happen |
| 140 | // on userdebug/eng builds. |
| 141 | LOG(INFO) << "Attempting to start jit for openjdkjvmti plugin."; |
Andreas Gampe | c6bd42a | 2018-11-07 13:39:41 -0800 | [diff] [blame] | 142 | // Note: use rwx allowed = true, because if this is the system server, we will not be |
| 143 | // allowed to allocate any JIT code cache, anyways. |
| 144 | runtime->CreateJitCodeCache(/*rwx_memory_allowed=*/true); |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 145 | runtime->CreateJit(); |
| 146 | if (runtime->GetJit() == nullptr) { |
| 147 | LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be " |
| 148 | << "quite slow as it is running entirely in the interpreter. Try running " |
| 149 | << "'setenforce 0' and restarting this process."; |
| 150 | } |
| 151 | } |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 152 | } |
| 153 | runtime->DeoptimizeBootImage(); |
| 154 | } |
| 155 | } |
| 156 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 157 | bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) { |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 158 | art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 159 | return MethodHasBreakpointsLocked(method); |
| 160 | } |
| 161 | |
| 162 | bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 163 | auto elem = breakpoint_status_.find(method); |
| 164 | return elem != breakpoint_status_.end() && elem->second != 0; |
| 165 | } |
| 166 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 167 | void DeoptManager::RemoveDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 168 | art::Thread* self = art::Thread::Current(); |
| 169 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 170 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 171 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 172 | } |
| 173 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 174 | void DeoptManager::AddDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 175 | art::Thread* self = art::Thread::Current(); |
| 176 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 177 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 178 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) { |
| 182 | DCHECK(method->IsInvokable()); |
| 183 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 184 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 185 | |
| 186 | art::Thread* self = art::Thread::Current(); |
| 187 | method = method->GetCanonicalMethod(); |
| 188 | bool is_default = method->IsDefault(); |
| 189 | |
| 190 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 191 | deoptimization_status_lock_.ExclusiveLock(self); |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 192 | { |
| 193 | breakpoint_status_lock_.ExclusiveLock(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 194 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 195 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 196 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 197 | if (MethodHasBreakpointsLocked(method)) { |
| 198 | // Don't need to do anything extra. |
| 199 | breakpoint_status_[method]++; |
| 200 | // Another thread might be deoptimizing the very method we just added new breakpoints for. |
| 201 | // Wait for any deopts to finish before moving on. |
| 202 | breakpoint_status_lock_.ExclusiveUnlock(self); |
| 203 | WaitForDeoptimizationToFinish(self); |
| 204 | return; |
| 205 | } |
| 206 | breakpoint_status_[method] = 1; |
| 207 | breakpoint_status_lock_.ExclusiveUnlock(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 208 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 209 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
| 210 | if (instrumentation->IsForcedInterpretOnly()) { |
| 211 | // We are already interpreting everything so no need to do anything. |
| 212 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 213 | return; |
| 214 | } else if (is_default) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 215 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 216 | } else { |
| 217 | PerformLimitedDeoptimization(self, method); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) { |
| 222 | DCHECK(method->IsInvokable()) << method->PrettyMethod(); |
| 223 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 224 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 225 | |
| 226 | art::Thread* self = art::Thread::Current(); |
| 227 | method = method->GetCanonicalMethod(); |
| 228 | bool is_default = method->IsDefault(); |
| 229 | |
| 230 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 231 | // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might |
| 232 | // need but since that is very heavy we will instead just use a condition variable to make sure we |
| 233 | // don't race with ourselves. |
| 234 | deoptimization_status_lock_.ExclusiveLock(self); |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 235 | bool is_last_breakpoint; |
| 236 | { |
| 237 | art::MutexLock mu(self, breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 238 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 239 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
| 240 | DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without " |
| 241 | << "breakpoints present!"; |
| 242 | breakpoint_status_[method] -= 1; |
| 243 | is_last_breakpoint = (breakpoint_status_[method] == 0); |
| 244 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 245 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 246 | if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) { |
| 247 | // We don't need to do anything since we are interpreting everything anyway. |
| 248 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 249 | return; |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 250 | } else if (is_last_breakpoint) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 251 | if (UNLIKELY(is_default)) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 252 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 253 | } else { |
| 254 | PerformLimitedUndeoptimization(self, method); |
| 255 | } |
| 256 | } else { |
| 257 | // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait |
| 258 | // for any deopts to finish before moving on. |
| 259 | WaitForDeoptimizationToFinish(self); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) { |
| 264 | while (performing_deoptimization_) { |
| 265 | deoptimization_condition_.Wait(self); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) { |
| 270 | WaitForDeoptimizationToFinishLocked(self); |
| 271 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 272 | } |
| 273 | |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 274 | // Users should make sure that only gc-critical-section safe code is used while a |
| 275 | // ScopedDeoptimizationContext exists. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 276 | class ScopedDeoptimizationContext : public art::ValueObject { |
| 277 | public: |
| 278 | ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt) |
| 279 | RELEASE(deopt->deoptimization_status_lock_) |
| 280 | ACQUIRE(art::Locks::mutator_lock_) |
| 281 | ACQUIRE(art::Roles::uninterruptible_) |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 282 | : self_(self), |
| 283 | deopt_(deopt), |
| 284 | critical_section_(self_, "JVMTI Deoptimizing methods"), |
| 285 | uninterruptible_cause_(nullptr) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 286 | deopt_->WaitForDeoptimizationToFinishLocked(self_); |
| 287 | DCHECK(!deopt->performing_deoptimization_) |
| 288 | << "Already performing deoptimization on another thread!"; |
| 289 | // Use performing_deoptimization_ to keep track of the lock. |
| 290 | deopt_->performing_deoptimization_ = true; |
| 291 | deopt_->deoptimization_status_lock_.Unlock(self_); |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 292 | uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation, |
| 293 | art::gc::kCollectorTypeCriticalSection); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 294 | art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods", |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 295 | /*long_suspend=*/ false); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | ~ScopedDeoptimizationContext() |
| 299 | RELEASE(art::Locks::mutator_lock_) |
| 300 | RELEASE(art::Roles::uninterruptible_) { |
| 301 | // Can be suspended again. |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 302 | critical_section_.Exit(uninterruptible_cause_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 303 | // Release the mutator lock. |
| 304 | art::Runtime::Current()->GetThreadList()->ResumeAll(); |
| 305 | // Let other threads know it's fine to proceed. |
| 306 | art::MutexLock lk(self_, deopt_->deoptimization_status_lock_); |
| 307 | deopt_->performing_deoptimization_ = false; |
| 308 | deopt_->deoptimization_condition_.Broadcast(self_); |
| 309 | } |
| 310 | |
| 311 | private: |
| 312 | art::Thread* self_; |
| 313 | DeoptManager* deopt_; |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 314 | art::gc::GCCriticalSection critical_section_; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 315 | const char* uninterruptible_cause_; |
| 316 | }; |
| 317 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 318 | void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 319 | global_deopt_count_++; |
| 320 | if (global_deopt_count_ == 1) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 321 | PerformGlobalDeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 322 | } else { |
| 323 | WaitForDeoptimizationToFinish(self); |
| 324 | } |
| 325 | } |
| 326 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 327 | void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) { |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame] | 328 | DCHECK_GT(global_deopt_count_, 0u) << "Request to remove non-existent global deoptimization!"; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 329 | global_deopt_count_--; |
| 330 | if (global_deopt_count_ == 0) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 331 | PerformGlobalUndeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 332 | } else { |
| 333 | WaitForDeoptimizationToFinish(self); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 338 | ScopedDeoptimizationContext sdc(self, this); |
| 339 | art::Runtime::Current()->GetInstrumentation()->Deoptimize(method); |
| 340 | } |
| 341 | |
| 342 | void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 343 | ScopedDeoptimizationContext sdc(self, this); |
| 344 | art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method); |
| 345 | } |
| 346 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 347 | void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 348 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 349 | art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything( |
| 350 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 351 | } |
| 352 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 353 | void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 354 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 355 | art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything( |
| 356 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
| 360 | void DeoptManager::RemoveDeoptimizationRequester() { |
| 361 | art::Thread* self = art::Thread::Current(); |
| 362 | art::ScopedThreadStateChange sts(self, art::kSuspended); |
| 363 | deoptimization_status_lock_.ExclusiveLock(self); |
| 364 | DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present"; |
| 365 | deopter_count_--; |
| 366 | if (deopter_count_ == 0) { |
| 367 | ScopedDeoptimizationContext sdc(self, this); |
| 368 | // TODO Give this a real key. |
| 369 | art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization(""); |
| 370 | return; |
| 371 | } else { |
| 372 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void DeoptManager::AddDeoptimizationRequester() { |
| 377 | art::Thread* self = art::Thread::Current(); |
| 378 | art::ScopedThreadStateChange stsc(self, art::kSuspended); |
| 379 | deoptimization_status_lock_.ExclusiveLock(self); |
| 380 | deopter_count_++; |
| 381 | if (deopter_count_ == 1) { |
| 382 | ScopedDeoptimizationContext sdc(self, this); |
| 383 | art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization(); |
| 384 | return; |
| 385 | } else { |
| 386 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void DeoptManager::DeoptimizeThread(art::Thread* target) { |
| 391 | art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target); |
| 392 | } |
| 393 | |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 394 | extern DeoptManager* gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 395 | DeoptManager* DeoptManager::Get() { |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 396 | return gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | } // namespace openjdkjvmti |