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" |
| 43 | #include "jni_internal.h" |
| 44 | #include "mirror/class-inl.h" |
| 45 | #include "mirror/object_array-inl.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 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 | |
| 53 | namespace 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. |
| 61 | bool JvmtiMethodInspectionCallback::IsMethodBeingInspected( |
| 62 | art::ArtMethod* method ATTRIBUTE_UNUSED) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) { |
| 67 | return !manager_->MethodHasBreakpoints(method); |
| 68 | } |
| 69 | |
| 70 | DeoptManager::DeoptManager() |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 71 | : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock", |
| 72 | static_cast<art::LockLevel>( |
| 73 | art::LockLevel::kClassLinkerClassesLock + 1)), |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 74 | deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_), |
| 75 | performing_deoptimization_(false), |
| 76 | global_deopt_count_(0), |
| 77 | deopter_count_(0), |
| 78 | inspection_callback_(this) { } |
| 79 | |
| 80 | void DeoptManager::Setup() { |
| 81 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 82 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 83 | art::ScopedSuspendAll ssa("Add method Inspection Callback"); |
| 84 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 85 | callbacks->AddMethodInspectionCallback(&inspection_callback_); |
| 86 | } |
| 87 | |
| 88 | void DeoptManager::Shutdown() { |
| 89 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 90 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 91 | art::ScopedSuspendAll ssa("remove method Inspection Callback"); |
| 92 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 93 | callbacks->RemoveMethodInspectionCallback(&inspection_callback_); |
| 94 | } |
| 95 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 96 | void DeoptManager::FinishSetup() { |
| 97 | art::Thread* self = art::Thread::Current(); |
| 98 | art::MutexLock mu(self, deoptimization_status_lock_); |
| 99 | |
| 100 | art::Runtime* runtime = art::Runtime::Current(); |
| 101 | // See if we need to do anything. |
| 102 | if (!runtime->IsJavaDebuggable()) { |
| 103 | // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be |
| 104 | // retrieved and they will all be best-effort. |
| 105 | if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) { |
| 106 | // We are still early enough to change the compiler options and get full JVMTI support. |
| 107 | LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to " |
| 108 | << "debuggable state. Please pass '--debuggable' to dex2oat and " |
| 109 | << "'-Xcompiler-option --debuggable' to dalvikvm in the future."; |
| 110 | DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!"; |
| 111 | runtime->AddCompilerOption("--debuggable"); |
| 112 | runtime->SetJavaDebuggable(true); |
| 113 | } else { |
| 114 | LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was " |
| 115 | << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion " |
| 116 | << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some " |
| 117 | << "functionality might not work properly."; |
| 118 | } |
| 119 | runtime->DeoptimizeBootImage(); |
| 120 | } |
| 121 | } |
| 122 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 123 | bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) { |
| 124 | art::MutexLock lk(art::Thread::Current(), deoptimization_status_lock_); |
| 125 | return MethodHasBreakpointsLocked(method); |
| 126 | } |
| 127 | |
| 128 | bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) { |
| 129 | if (deopter_count_ == 0) { |
| 130 | return false; |
| 131 | } |
| 132 | auto elem = breakpoint_status_.find(method); |
| 133 | return elem != breakpoint_status_.end() && elem->second != 0; |
| 134 | } |
| 135 | |
| 136 | void DeoptManager::RemoveDeoptimizeAllMethods() { |
| 137 | art::Thread* self = art::Thread::Current(); |
| 138 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 139 | deoptimization_status_lock_.ExclusiveLock(self); |
| 140 | RemoveDeoptimizeAllMethodsLocked(self); |
| 141 | } |
| 142 | |
| 143 | void DeoptManager::AddDeoptimizeAllMethods() { |
| 144 | art::Thread* self = art::Thread::Current(); |
| 145 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 146 | deoptimization_status_lock_.ExclusiveLock(self); |
| 147 | AddDeoptimizeAllMethodsLocked(self); |
| 148 | } |
| 149 | |
| 150 | void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) { |
| 151 | DCHECK(method->IsInvokable()); |
| 152 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 153 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 154 | |
| 155 | art::Thread* self = art::Thread::Current(); |
| 156 | method = method->GetCanonicalMethod(); |
| 157 | bool is_default = method->IsDefault(); |
| 158 | |
| 159 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 160 | deoptimization_status_lock_.ExclusiveLock(self); |
| 161 | |
| 162 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
| 163 | |
| 164 | if (MethodHasBreakpointsLocked(method)) { |
| 165 | // Don't need to do anything extra. |
| 166 | breakpoint_status_[method]++; |
| 167 | // Another thread might be deoptimizing the very method we just added new breakpoints for. Wait |
| 168 | // for any deopts to finish before moving on. |
| 169 | WaitForDeoptimizationToFinish(self); |
| 170 | return; |
| 171 | } |
| 172 | breakpoint_status_[method] = 1; |
| 173 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
| 174 | if (instrumentation->IsForcedInterpretOnly()) { |
| 175 | // We are already interpreting everything so no need to do anything. |
| 176 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 177 | return; |
| 178 | } else if (is_default) { |
| 179 | AddDeoptimizeAllMethodsLocked(self); |
| 180 | } else { |
| 181 | PerformLimitedDeoptimization(self, method); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) { |
| 186 | DCHECK(method->IsInvokable()) << method->PrettyMethod(); |
| 187 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 188 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 189 | |
| 190 | art::Thread* self = art::Thread::Current(); |
| 191 | method = method->GetCanonicalMethod(); |
| 192 | bool is_default = method->IsDefault(); |
| 193 | |
| 194 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 195 | // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might |
| 196 | // need but since that is very heavy we will instead just use a condition variable to make sure we |
| 197 | // don't race with ourselves. |
| 198 | deoptimization_status_lock_.ExclusiveLock(self); |
| 199 | |
| 200 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
| 201 | DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without " |
| 202 | << "breakpoints present!"; |
| 203 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
| 204 | breakpoint_status_[method] -= 1; |
| 205 | if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) { |
| 206 | // We don't need to do anything since we are interpreting everything anyway. |
| 207 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 208 | return; |
| 209 | } else if (breakpoint_status_[method] == 0) { |
| 210 | if (UNLIKELY(is_default)) { |
| 211 | RemoveDeoptimizeAllMethodsLocked(self); |
| 212 | } else { |
| 213 | PerformLimitedUndeoptimization(self, method); |
| 214 | } |
| 215 | } else { |
| 216 | // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait |
| 217 | // for any deopts to finish before moving on. |
| 218 | WaitForDeoptimizationToFinish(self); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) { |
| 223 | while (performing_deoptimization_) { |
| 224 | deoptimization_condition_.Wait(self); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) { |
| 229 | WaitForDeoptimizationToFinishLocked(self); |
| 230 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 231 | } |
| 232 | |
| 233 | class ScopedDeoptimizationContext : public art::ValueObject { |
| 234 | public: |
| 235 | ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt) |
| 236 | RELEASE(deopt->deoptimization_status_lock_) |
| 237 | ACQUIRE(art::Locks::mutator_lock_) |
| 238 | ACQUIRE(art::Roles::uninterruptible_) |
| 239 | : self_(self), deopt_(deopt), uninterruptible_cause_(nullptr) { |
| 240 | deopt_->WaitForDeoptimizationToFinishLocked(self_); |
| 241 | DCHECK(!deopt->performing_deoptimization_) |
| 242 | << "Already performing deoptimization on another thread!"; |
| 243 | // Use performing_deoptimization_ to keep track of the lock. |
| 244 | deopt_->performing_deoptimization_ = true; |
| 245 | deopt_->deoptimization_status_lock_.Unlock(self_); |
| 246 | art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods", |
| 247 | /*long_suspend*/ false); |
| 248 | uninterruptible_cause_ = self_->StartAssertNoThreadSuspension("JVMTI deoptimizing methods"); |
| 249 | } |
| 250 | |
| 251 | ~ScopedDeoptimizationContext() |
| 252 | RELEASE(art::Locks::mutator_lock_) |
| 253 | RELEASE(art::Roles::uninterruptible_) { |
| 254 | // Can be suspended again. |
| 255 | self_->EndAssertNoThreadSuspension(uninterruptible_cause_); |
| 256 | // Release the mutator lock. |
| 257 | art::Runtime::Current()->GetThreadList()->ResumeAll(); |
| 258 | // Let other threads know it's fine to proceed. |
| 259 | art::MutexLock lk(self_, deopt_->deoptimization_status_lock_); |
| 260 | deopt_->performing_deoptimization_ = false; |
| 261 | deopt_->deoptimization_condition_.Broadcast(self_); |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | art::Thread* self_; |
| 266 | DeoptManager* deopt_; |
| 267 | const char* uninterruptible_cause_; |
| 268 | }; |
| 269 | |
| 270 | void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) { |
| 271 | global_deopt_count_++; |
| 272 | if (global_deopt_count_ == 1) { |
| 273 | PerformGlobalDeoptimization(self); |
| 274 | } else { |
| 275 | WaitForDeoptimizationToFinish(self); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) { |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame^] | 280 | 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] | 281 | global_deopt_count_--; |
| 282 | if (global_deopt_count_ == 0) { |
| 283 | PerformGlobalUndeoptimization(self); |
| 284 | } else { |
| 285 | WaitForDeoptimizationToFinish(self); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 290 | ScopedDeoptimizationContext sdc(self, this); |
| 291 | art::Runtime::Current()->GetInstrumentation()->Deoptimize(method); |
| 292 | } |
| 293 | |
| 294 | void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 295 | ScopedDeoptimizationContext sdc(self, this); |
| 296 | art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method); |
| 297 | } |
| 298 | |
| 299 | void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) { |
| 300 | ScopedDeoptimizationContext sdc(self, this); |
| 301 | art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything( |
| 302 | kDeoptManagerInstrumentationKey); |
| 303 | } |
| 304 | |
| 305 | void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) { |
| 306 | ScopedDeoptimizationContext sdc(self, this); |
| 307 | art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything( |
| 308 | kDeoptManagerInstrumentationKey); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | void DeoptManager::RemoveDeoptimizationRequester() { |
| 313 | art::Thread* self = art::Thread::Current(); |
| 314 | art::ScopedThreadStateChange sts(self, art::kSuspended); |
| 315 | deoptimization_status_lock_.ExclusiveLock(self); |
| 316 | DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present"; |
| 317 | deopter_count_--; |
| 318 | if (deopter_count_ == 0) { |
| 319 | ScopedDeoptimizationContext sdc(self, this); |
| 320 | // TODO Give this a real key. |
| 321 | art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization(""); |
| 322 | return; |
| 323 | } else { |
| 324 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void DeoptManager::AddDeoptimizationRequester() { |
| 329 | art::Thread* self = art::Thread::Current(); |
| 330 | art::ScopedThreadStateChange stsc(self, art::kSuspended); |
| 331 | deoptimization_status_lock_.ExclusiveLock(self); |
| 332 | deopter_count_++; |
| 333 | if (deopter_count_ == 1) { |
| 334 | ScopedDeoptimizationContext sdc(self, this); |
| 335 | art::Runtime::Current()->GetInstrumentation()->EnableDeoptimization(); |
| 336 | return; |
| 337 | } else { |
| 338 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void DeoptManager::DeoptimizeThread(art::Thread* target) { |
| 343 | art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target); |
| 344 | } |
| 345 | |
Orion Hodson | 70f4415 | 2018-02-08 09:28:21 +0000 | [diff] [blame] | 346 | extern DeoptManager gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 347 | DeoptManager* DeoptManager::Get() { |
Orion Hodson | 70f4415 | 2018-02-08 09:28:21 +0000 | [diff] [blame] | 348 | return &gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | } // namespace openjdkjvmti |