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> |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 33 | #include <iosfwd> |
| 34 | #include <mutex> |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 35 | |
| 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 Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 42 | #include "dex/dex_file_annotations.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 43 | #include "dex/modifiers.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 44 | #include "events-inl.h" |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame^] | 45 | #include "gc/collector_type.h" |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 46 | #include "gc/heap.h" |
| 47 | #include "gc/scoped_gc_critical_section.h" |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 48 | #include "jit/jit.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 49 | #include "jni/jni_internal.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 50 | #include "mirror/class-inl.h" |
| 51 | #include "mirror/object_array-inl.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 52 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame^] | 53 | #include "read_barrier_config.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 54 | #include "runtime_callbacks.h" |
| 55 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 56 | #include "scoped_thread_state_change.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 57 | #include "thread-current-inl.h" |
| 58 | #include "thread_list.h" |
| 59 | #include "ti_phase.h" |
| 60 | |
| 61 | namespace openjdkjvmti { |
| 62 | |
| 63 | // 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] | 64 | // 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. |
| 68 | bool 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 Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) { |
| 79 | return !manager_->MethodHasBreakpoints(method); |
| 80 | } |
| 81 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 82 | bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion( |
| 83 | art::ArtMethod* method ATTRIBUTE_UNUSED) { |
| 84 | return true; |
| 85 | } |
| 86 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 87 | DeoptManager::DeoptManager() |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 88 | : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock", |
| 89 | static_cast<art::LockLevel>( |
| 90 | art::LockLevel::kClassLinkerClassesLock + 1)), |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 91 | deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_), |
| 92 | performing_deoptimization_(false), |
| 93 | global_deopt_count_(0), |
| 94 | deopter_count_(0), |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 95 | breakpoint_status_lock_("JVMTI_BreakpointStatusLock", |
| 96 | static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)), |
| 97 | inspection_callback_(this), |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 98 | set_local_variable_called_(false) { } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 99 | |
| 100 | void 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 | |
| 108 | void 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 Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 116 | void 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 Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 163 | void 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 Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 185 | 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 Gampe | c6bd42a | 2018-11-07 13:39:41 -0800 | [diff] [blame] | 194 | // 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 Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 197 | 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 Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 204 | } |
| 205 | runtime->DeoptimizeBootImage(); |
| 206 | } |
| 207 | } |
| 208 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 209 | bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) { |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 210 | art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 211 | return MethodHasBreakpointsLocked(method); |
| 212 | } |
| 213 | |
| 214 | bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 215 | auto elem = breakpoint_status_.find(method); |
| 216 | return elem != breakpoint_status_.end() && elem->second != 0; |
| 217 | } |
| 218 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 219 | void DeoptManager::RemoveDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 220 | art::Thread* self = art::Thread::Current(); |
| 221 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 222 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 223 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 224 | } |
| 225 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 226 | void DeoptManager::AddDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 227 | art::Thread* self = art::Thread::Current(); |
| 228 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 229 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 230 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void 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 Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 244 | { |
| 245 | breakpoint_status_lock_.ExclusiveLock(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 246 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 247 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 248 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 249 | 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 Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 260 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 261 | 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 Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 267 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 268 | } else { |
| 269 | PerformLimitedDeoptimization(self, method); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void 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 Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 287 | bool is_last_breakpoint; |
| 288 | { |
| 289 | art::MutexLock mu(self, breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 290 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 291 | 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 Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 297 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 298 | 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 Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 302 | } else if (is_last_breakpoint) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 303 | if (UNLIKELY(is_default)) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 304 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 305 | } 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 | |
| 315 | void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) { |
| 316 | while (performing_deoptimization_) { |
| 317 | deoptimization_condition_.Wait(self); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) { |
| 322 | WaitForDeoptimizationToFinishLocked(self); |
| 323 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 324 | } |
| 325 | |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 326 | // Users should make sure that only gc-critical-section safe code is used while a |
| 327 | // ScopedDeoptimizationContext exists. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 328 | class 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 Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 334 | : self_(self), |
| 335 | deopt_(deopt), |
| 336 | critical_section_(self_, "JVMTI Deoptimizing methods"), |
| 337 | uninterruptible_cause_(nullptr) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 338 | 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 Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 344 | uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation, |
| 345 | art::gc::kCollectorTypeCriticalSection); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 346 | art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods", |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 347 | /*long_suspend=*/ false); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | ~ScopedDeoptimizationContext() |
| 351 | RELEASE(art::Locks::mutator_lock_) |
| 352 | RELEASE(art::Roles::uninterruptible_) { |
| 353 | // Can be suspended again. |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 354 | critical_section_.Exit(uninterruptible_cause_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 355 | // 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 Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 366 | art::gc::GCCriticalSection critical_section_; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 367 | const char* uninterruptible_cause_; |
| 368 | }; |
| 369 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 370 | void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 371 | global_deopt_count_++; |
| 372 | if (global_deopt_count_ == 1) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 373 | PerformGlobalDeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 374 | } else { |
| 375 | WaitForDeoptimizationToFinish(self); |
| 376 | } |
| 377 | } |
| 378 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 379 | void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) { |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame] | 380 | 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] | 381 | global_deopt_count_--; |
| 382 | if (global_deopt_count_ == 0) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 383 | PerformGlobalUndeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 384 | } else { |
| 385 | WaitForDeoptimizationToFinish(self); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 390 | ScopedDeoptimizationContext sdc(self, this); |
| 391 | art::Runtime::Current()->GetInstrumentation()->Deoptimize(method); |
| 392 | } |
| 393 | |
| 394 | void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 395 | ScopedDeoptimizationContext sdc(self, this); |
| 396 | art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method); |
| 397 | } |
| 398 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 399 | void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 400 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 401 | art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything( |
| 402 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 405 | void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 406 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 407 | art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything( |
| 408 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 411 | jvmtiError 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 Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 420 | if (target->IncrementForceInterpreterCount() == 1) { |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 421 | 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 | |
| 439 | jvmtiError 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 Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 451 | |
| 452 | void 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 | |
| 468 | void 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 | |
| 482 | void DeoptManager::DeoptimizeThread(art::Thread* target) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame^] | 483 | // 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 Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 488 | art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target); |
| 489 | } |
| 490 | |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 491 | extern DeoptManager* gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 492 | DeoptManager* DeoptManager::Get() { |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 493 | return gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | } // namespace openjdkjvmti |