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 | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 48 | #include "instrumentation.h" |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 49 | #include "jit/jit.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 50 | #include "jni/jni_internal.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 51 | #include "mirror/class-inl.h" |
| 52 | #include "mirror/object_array-inl.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 53 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 54 | #include "read_barrier_config.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 55 | #include "runtime_callbacks.h" |
| 56 | #include "scoped_thread_state_change-inl.h" |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 57 | #include "scoped_thread_state_change.h" |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 58 | #include "thread-current-inl.h" |
| 59 | #include "thread_list.h" |
| 60 | #include "ti_phase.h" |
| 61 | |
| 62 | namespace openjdkjvmti { |
| 63 | |
| 64 | // 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] | 65 | // actually care about the method at this time (ie active frames had locals changed). For now we |
| 66 | // just assume that if anything has changed any frame's locals we care about all methods. If nothing |
| 67 | // has we only care about methods with active breakpoints on them. In the future we should probably |
| 68 | // rewrite all of this to instead do this at the ShadowFrame or thread granularity. |
| 69 | bool JvmtiMethodInspectionCallback::IsMethodBeingInspected(art::ArtMethod* method) { |
| 70 | // Non-java-debuggable runtimes we need to assume that any method might not be debuggable and |
| 71 | // therefore potentially being inspected (due to inlines). If we are debuggable we rely hard on |
| 72 | // inlining not being done since we don't keep track of which methods get inlined where and simply |
| 73 | // look to see if the method is breakpointed. |
| 74 | return !art::Runtime::Current()->IsJavaDebuggable() || |
| 75 | manager_->HaveLocalsChanged() || |
| 76 | manager_->MethodHasBreakpoints(method); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool JvmtiMethodInspectionCallback::IsMethodSafeToJit(art::ArtMethod* method) { |
| 80 | return !manager_->MethodHasBreakpoints(method); |
| 81 | } |
| 82 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 83 | bool JvmtiMethodInspectionCallback::MethodNeedsDebugVersion( |
| 84 | art::ArtMethod* method ATTRIBUTE_UNUSED) { |
| 85 | return true; |
| 86 | } |
| 87 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 88 | DeoptManager::DeoptManager() |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 89 | : deoptimization_status_lock_("JVMTI_DeoptimizationStatusLock", |
| 90 | static_cast<art::LockLevel>( |
| 91 | art::LockLevel::kClassLinkerClassesLock + 1)), |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 92 | deoptimization_condition_("JVMTI_DeoptimizationCondition", deoptimization_status_lock_), |
| 93 | performing_deoptimization_(false), |
| 94 | global_deopt_count_(0), |
| 95 | deopter_count_(0), |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 96 | breakpoint_status_lock_("JVMTI_BreakpointStatusLock", |
| 97 | static_cast<art::LockLevel>(art::LockLevel::kAbortLock + 1)), |
| 98 | inspection_callback_(this), |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 99 | set_local_variable_called_(false) { } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 100 | |
| 101 | void DeoptManager::Setup() { |
| 102 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 103 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 104 | art::ScopedSuspendAll ssa("Add method Inspection Callback"); |
| 105 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 106 | callbacks->AddMethodInspectionCallback(&inspection_callback_); |
| 107 | } |
| 108 | |
| 109 | void DeoptManager::Shutdown() { |
| 110 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 111 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 112 | art::ScopedSuspendAll ssa("remove method Inspection Callback"); |
| 113 | art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks(); |
| 114 | callbacks->RemoveMethodInspectionCallback(&inspection_callback_); |
| 115 | } |
| 116 | |
Alex Light | b214694 | 2019-03-12 15:46:40 +0000 | [diff] [blame] | 117 | void DeoptManager::DumpDeoptInfo(art::Thread* self, std::ostream& stream) { |
| 118 | art::ScopedObjectAccess soa(self); |
| 119 | art::MutexLock mutll(self, *art::Locks::thread_list_lock_); |
| 120 | art::MutexLock mudsl(self, deoptimization_status_lock_); |
| 121 | art::MutexLock mubsl(self, breakpoint_status_lock_); |
| 122 | stream << "Deoptimizer count: " << deopter_count_ << "\n"; |
| 123 | stream << "Global deopt count: " << global_deopt_count_ << "\n"; |
| 124 | stream << "Can perform OSR: " << !set_local_variable_called_.load() << "\n"; |
| 125 | for (const auto& [bp, loc] : this->breakpoint_status_) { |
| 126 | stream << "Breakpoint: " << bp->PrettyMethod() << " @ 0x" << std::hex << loc << "\n"; |
| 127 | } |
| 128 | struct DumpThreadDeoptCount : public art::Closure { |
| 129 | public: |
| 130 | DumpThreadDeoptCount(std::ostream& stream, std::mutex& mu) |
| 131 | : cnt_(0), stream_(stream), mu_(mu) {} |
| 132 | void Run(art::Thread* self) override { |
| 133 | { |
| 134 | std::lock_guard<std::mutex> lg(mu_); |
| 135 | std::string name; |
| 136 | self->GetThreadName(name); |
| 137 | stream_ << "Thread " << name << " (id: " << std::dec << self->GetThreadId() |
| 138 | << ") force interpreter count " << self->ForceInterpreterCount() << "\n"; |
| 139 | } |
| 140 | // Increment this after unlocking the mutex so we won't race its destructor. |
| 141 | cnt_++; |
| 142 | } |
| 143 | |
| 144 | void WaitForCount(size_t threads) { |
| 145 | while (cnt_.load() != threads) { |
| 146 | sched_yield(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | std::atomic<size_t> cnt_; |
| 152 | std::ostream& stream_; |
| 153 | std::mutex& mu_; |
| 154 | }; |
| 155 | |
| 156 | std::mutex mu; |
| 157 | DumpThreadDeoptCount dtdc(stream, mu); |
| 158 | auto func = [](art::Thread* thread, void* ctx) { |
| 159 | reinterpret_cast<DumpThreadDeoptCount*>(ctx)->Run(thread); |
| 160 | }; |
| 161 | art::Runtime::Current()->GetThreadList()->ForEach(func, &dtdc); |
| 162 | } |
| 163 | |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 164 | void DeoptManager::FinishSetup() { |
| 165 | art::Thread* self = art::Thread::Current(); |
| 166 | art::MutexLock mu(self, deoptimization_status_lock_); |
| 167 | |
| 168 | art::Runtime* runtime = art::Runtime::Current(); |
| 169 | // See if we need to do anything. |
| 170 | if (!runtime->IsJavaDebuggable()) { |
| 171 | // See if we can enable all JVMTI functions. If this is false, only kArtTiVersion agents can be |
| 172 | // retrieved and they will all be best-effort. |
| 173 | if (PhaseUtil::GetPhaseUnchecked() == JVMTI_PHASE_ONLOAD) { |
| 174 | // We are still early enough to change the compiler options and get full JVMTI support. |
| 175 | LOG(INFO) << "Openjdkjvmti plugin loaded on a non-debuggable runtime. Changing runtime to " |
| 176 | << "debuggable state. Please pass '--debuggable' to dex2oat and " |
| 177 | << "'-Xcompiler-option --debuggable' to dalvikvm in the future."; |
| 178 | DCHECK(runtime->GetJit() == nullptr) << "Jit should not be running yet!"; |
| 179 | runtime->AddCompilerOption("--debuggable"); |
| 180 | runtime->SetJavaDebuggable(true); |
| 181 | } else { |
| 182 | LOG(WARNING) << "Openjdkjvmti plugin was loaded on a non-debuggable Runtime. Plugin was " |
| 183 | << "loaded too late to change runtime state to DEBUGGABLE. Only kArtTiVersion " |
| 184 | << "(0x" << std::hex << kArtTiVersion << ") environments are available. Some " |
| 185 | << "functionality might not work properly."; |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 186 | if (runtime->GetJit() == nullptr && |
| 187 | runtime->GetJITOptions()->UseJitCompilation() && |
| 188 | !runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
| 189 | // If we don't have a jit we should try to start the jit for performance reasons. We only |
| 190 | // need to do this for late attach on non-debuggable processes because for debuggable |
| 191 | // processes we already rely on jit and we cannot force this jit to start if we are still in |
| 192 | // OnLoad since the runtime hasn't started up sufficiently. This is only expected to happen |
| 193 | // on userdebug/eng builds. |
| 194 | LOG(INFO) << "Attempting to start jit for openjdkjvmti plugin."; |
Andreas Gampe | c6bd42a | 2018-11-07 13:39:41 -0800 | [diff] [blame] | 195 | // Note: use rwx allowed = true, because if this is the system server, we will not be |
| 196 | // allowed to allocate any JIT code cache, anyways. |
| 197 | runtime->CreateJitCodeCache(/*rwx_memory_allowed=*/true); |
Alex Light | 60fbefc | 2018-04-18 15:19:15 -0700 | [diff] [blame] | 198 | runtime->CreateJit(); |
| 199 | if (runtime->GetJit() == nullptr) { |
| 200 | LOG(WARNING) << "Could not start jit for openjdkjvmti plugin. This process might be " |
| 201 | << "quite slow as it is running entirely in the interpreter. Try running " |
| 202 | << "'setenforce 0' and restarting this process."; |
| 203 | } |
| 204 | } |
Alex Light | 2ce6fc8 | 2017-12-18 16:42:36 -0800 | [diff] [blame] | 205 | } |
| 206 | runtime->DeoptimizeBootImage(); |
| 207 | } |
| 208 | } |
| 209 | |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 210 | bool DeoptManager::MethodHasBreakpoints(art::ArtMethod* method) { |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 211 | art::MutexLock lk(art::Thread::Current(), breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 212 | return MethodHasBreakpointsLocked(method); |
| 213 | } |
| 214 | |
| 215 | bool DeoptManager::MethodHasBreakpointsLocked(art::ArtMethod* method) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 216 | auto elem = breakpoint_status_.find(method); |
| 217 | return elem != breakpoint_status_.end() && elem->second != 0; |
| 218 | } |
| 219 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 220 | void DeoptManager::RemoveDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 221 | art::Thread* self = art::Thread::Current(); |
| 222 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 223 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 224 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 225 | } |
| 226 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 227 | void DeoptManager::AddDeoptimizeAllMethods() { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 228 | art::Thread* self = art::Thread::Current(); |
| 229 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 230 | deoptimization_status_lock_.ExclusiveLock(self); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 231 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void DeoptManager::AddMethodBreakpoint(art::ArtMethod* method) { |
| 235 | DCHECK(method->IsInvokable()); |
| 236 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 237 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 238 | |
| 239 | art::Thread* self = art::Thread::Current(); |
| 240 | method = method->GetCanonicalMethod(); |
| 241 | bool is_default = method->IsDefault(); |
| 242 | |
| 243 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 244 | deoptimization_status_lock_.ExclusiveLock(self); |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 245 | { |
| 246 | breakpoint_status_lock_.ExclusiveLock(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 247 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 248 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 249 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 250 | if (MethodHasBreakpointsLocked(method)) { |
| 251 | // Don't need to do anything extra. |
| 252 | breakpoint_status_[method]++; |
| 253 | // Another thread might be deoptimizing the very method we just added new breakpoints for. |
| 254 | // Wait for any deopts to finish before moving on. |
| 255 | breakpoint_status_lock_.ExclusiveUnlock(self); |
| 256 | WaitForDeoptimizationToFinish(self); |
| 257 | return; |
| 258 | } |
| 259 | breakpoint_status_[method] = 1; |
| 260 | breakpoint_status_lock_.ExclusiveUnlock(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 261 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 262 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
| 263 | if (instrumentation->IsForcedInterpretOnly()) { |
| 264 | // We are already interpreting everything so no need to do anything. |
| 265 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 266 | return; |
| 267 | } else if (is_default) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 268 | AddDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 269 | } else { |
| 270 | PerformLimitedDeoptimization(self, method); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void DeoptManager::RemoveMethodBreakpoint(art::ArtMethod* method) { |
| 275 | DCHECK(method->IsInvokable()) << method->PrettyMethod(); |
| 276 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
| 277 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
| 278 | |
| 279 | art::Thread* self = art::Thread::Current(); |
| 280 | method = method->GetCanonicalMethod(); |
| 281 | bool is_default = method->IsDefault(); |
| 282 | |
| 283 | art::ScopedThreadSuspension sts(self, art::kSuspended); |
| 284 | // Ideally we should do a ScopedSuspendAll right here to get the full mutator_lock_ that we might |
| 285 | // need but since that is very heavy we will instead just use a condition variable to make sure we |
| 286 | // don't race with ourselves. |
| 287 | deoptimization_status_lock_.ExclusiveLock(self); |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 288 | bool is_last_breakpoint; |
| 289 | { |
| 290 | art::MutexLock mu(self, breakpoint_status_lock_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 291 | |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 292 | DCHECK_GT(deopter_count_, 0u) << "unexpected deotpimization request"; |
| 293 | DCHECK(MethodHasBreakpointsLocked(method)) << "Breakpoint on a method was removed without " |
| 294 | << "breakpoints present!"; |
| 295 | breakpoint_status_[method] -= 1; |
| 296 | is_last_breakpoint = (breakpoint_status_[method] == 0); |
| 297 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 298 | auto instrumentation = art::Runtime::Current()->GetInstrumentation(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 299 | if (UNLIKELY(instrumentation->IsForcedInterpretOnly())) { |
| 300 | // We don't need to do anything since we are interpreting everything anyway. |
| 301 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 302 | return; |
Alex Light | f285863 | 2018-04-02 11:28:50 -0700 | [diff] [blame] | 303 | } else if (is_last_breakpoint) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 304 | if (UNLIKELY(is_default)) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 305 | RemoveDeoptimizeAllMethodsLocked(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 306 | } else { |
| 307 | PerformLimitedUndeoptimization(self, method); |
| 308 | } |
| 309 | } else { |
| 310 | // Another thread might be deoptimizing the very methods we just removed breakpoints from. Wait |
| 311 | // for any deopts to finish before moving on. |
| 312 | WaitForDeoptimizationToFinish(self); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void DeoptManager::WaitForDeoptimizationToFinishLocked(art::Thread* self) { |
| 317 | while (performing_deoptimization_) { |
| 318 | deoptimization_condition_.Wait(self); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void DeoptManager::WaitForDeoptimizationToFinish(art::Thread* self) { |
| 323 | WaitForDeoptimizationToFinishLocked(self); |
| 324 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 325 | } |
| 326 | |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 327 | // Users should make sure that only gc-critical-section safe code is used while a |
| 328 | // ScopedDeoptimizationContext exists. |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 329 | class ScopedDeoptimizationContext : public art::ValueObject { |
| 330 | public: |
| 331 | ScopedDeoptimizationContext(art::Thread* self, DeoptManager* deopt) |
| 332 | RELEASE(deopt->deoptimization_status_lock_) |
| 333 | ACQUIRE(art::Locks::mutator_lock_) |
| 334 | ACQUIRE(art::Roles::uninterruptible_) |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 335 | : self_(self), |
| 336 | deopt_(deopt), |
| 337 | critical_section_(self_, "JVMTI Deoptimizing methods"), |
| 338 | uninterruptible_cause_(nullptr) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 339 | deopt_->WaitForDeoptimizationToFinishLocked(self_); |
| 340 | DCHECK(!deopt->performing_deoptimization_) |
| 341 | << "Already performing deoptimization on another thread!"; |
| 342 | // Use performing_deoptimization_ to keep track of the lock. |
| 343 | deopt_->performing_deoptimization_ = true; |
| 344 | deopt_->deoptimization_status_lock_.Unlock(self_); |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 345 | uninterruptible_cause_ = critical_section_.Enter(art::gc::kGcCauseInstrumentation, |
| 346 | art::gc::kCollectorTypeCriticalSection); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 347 | art::Runtime::Current()->GetThreadList()->SuspendAll("JMVTI Deoptimizing methods", |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 348 | /*long_suspend=*/ false); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | ~ScopedDeoptimizationContext() |
| 352 | RELEASE(art::Locks::mutator_lock_) |
| 353 | RELEASE(art::Roles::uninterruptible_) { |
| 354 | // Can be suspended again. |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 355 | critical_section_.Exit(uninterruptible_cause_); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 356 | // Release the mutator lock. |
| 357 | art::Runtime::Current()->GetThreadList()->ResumeAll(); |
| 358 | // Let other threads know it's fine to proceed. |
| 359 | art::MutexLock lk(self_, deopt_->deoptimization_status_lock_); |
| 360 | deopt_->performing_deoptimization_ = false; |
| 361 | deopt_->deoptimization_condition_.Broadcast(self_); |
| 362 | } |
| 363 | |
| 364 | private: |
| 365 | art::Thread* self_; |
| 366 | DeoptManager* deopt_; |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 367 | art::gc::GCCriticalSection critical_section_; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 368 | const char* uninterruptible_cause_; |
| 369 | }; |
| 370 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 371 | void DeoptManager::AddDeoptimizeAllMethodsLocked(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 372 | global_deopt_count_++; |
| 373 | if (global_deopt_count_ == 1) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 374 | PerformGlobalDeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 375 | } else { |
| 376 | WaitForDeoptimizationToFinish(self); |
| 377 | } |
| 378 | } |
| 379 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 380 | void DeoptManager::RemoveDeoptimizeAllMethodsLocked(art::Thread* self) { |
Roland Levillain | ef01222 | 2017-06-21 16:28:06 +0100 | [diff] [blame] | 381 | 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] | 382 | global_deopt_count_--; |
| 383 | if (global_deopt_count_ == 0) { |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 384 | PerformGlobalUndeoptimization(self); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 385 | } else { |
| 386 | WaitForDeoptimizationToFinish(self); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void DeoptManager::PerformLimitedDeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 391 | ScopedDeoptimizationContext sdc(self, this); |
| 392 | art::Runtime::Current()->GetInstrumentation()->Deoptimize(method); |
| 393 | } |
| 394 | |
| 395 | void DeoptManager::PerformLimitedUndeoptimization(art::Thread* self, art::ArtMethod* method) { |
| 396 | ScopedDeoptimizationContext sdc(self, this); |
| 397 | art::Runtime::Current()->GetInstrumentation()->Undeoptimize(method); |
| 398 | } |
| 399 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 400 | void DeoptManager::PerformGlobalDeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 401 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 402 | art::Runtime::Current()->GetInstrumentation()->DeoptimizeEverything( |
| 403 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 404 | } |
| 405 | |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 406 | void DeoptManager::PerformGlobalUndeoptimization(art::Thread* self) { |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 407 | ScopedDeoptimizationContext sdc(self, this); |
David Srbecky | d25eb2c | 2018-07-19 12:17:04 +0000 | [diff] [blame] | 408 | art::Runtime::Current()->GetInstrumentation()->UndeoptimizeEverything( |
| 409 | kDeoptManagerInstrumentationKey); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 412 | jvmtiError DeoptManager::AddDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) { |
| 413 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
| 414 | art::Thread* target = nullptr; |
| 415 | jvmtiError err = OK; |
| 416 | if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) { |
| 417 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
| 418 | return err; |
| 419 | } |
| 420 | // 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] | 421 | if (target->IncrementForceInterpreterCount() == 1) { |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 422 | struct DeoptClosure : public art::Closure { |
| 423 | public: |
Orion Hodson | 26ab270 | 2020-07-29 09:54:10 +0100 | [diff] [blame^] | 424 | explicit DeoptClosure(DeoptManager* manager) : manager_(manager) {} |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 425 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Orion Hodson | 26ab270 | 2020-07-29 09:54:10 +0100 | [diff] [blame^] | 426 | manager_->DeoptimizeThread(self); |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private: |
Orion Hodson | 26ab270 | 2020-07-29 09:54:10 +0100 | [diff] [blame^] | 430 | DeoptManager* manager_; |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 431 | }; |
| 432 | DeoptClosure c(this); |
| 433 | target->RequestSynchronousCheckpoint(&c); |
| 434 | } else { |
| 435 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
| 436 | } |
| 437 | return OK; |
| 438 | } |
| 439 | |
| 440 | jvmtiError DeoptManager::RemoveDeoptimizeThreadMethods(art::ScopedObjectAccessUnchecked& soa, jthread jtarget) { |
| 441 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 442 | art::Thread* target = nullptr; |
| 443 | jvmtiError err = OK; |
| 444 | if (!ThreadUtil::GetNativeThread(jtarget, soa, &target, &err)) { |
| 445 | return err; |
| 446 | } |
| 447 | // We don't need additional locking here because we hold the Thread_list_lock_. |
| 448 | DCHECK_GT(target->ForceInterpreterCount(), 0u); |
| 449 | target->DecrementForceInterpreterCount(); |
| 450 | return OK; |
| 451 | } |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 452 | |
| 453 | void DeoptManager::RemoveDeoptimizationRequester() { |
| 454 | art::Thread* self = art::Thread::Current(); |
| 455 | art::ScopedThreadStateChange sts(self, art::kSuspended); |
| 456 | deoptimization_status_lock_.ExclusiveLock(self); |
| 457 | DCHECK_GT(deopter_count_, 0u) << "Removing deoptimization requester without any being present"; |
| 458 | deopter_count_--; |
| 459 | if (deopter_count_ == 0) { |
| 460 | ScopedDeoptimizationContext sdc(self, this); |
| 461 | // TODO Give this a real key. |
| 462 | art::Runtime::Current()->GetInstrumentation()->DisableDeoptimization(""); |
| 463 | return; |
| 464 | } else { |
| 465 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void DeoptManager::AddDeoptimizationRequester() { |
| 470 | art::Thread* self = art::Thread::Current(); |
| 471 | art::ScopedThreadStateChange stsc(self, art::kSuspended); |
| 472 | deoptimization_status_lock_.ExclusiveLock(self); |
| 473 | deopter_count_++; |
| 474 | if (deopter_count_ == 1) { |
| 475 | ScopedDeoptimizationContext sdc(self, this); |
Alex Light | 4060786 | 2019-05-06 18:16:24 +0000 | [diff] [blame] | 476 | art::instrumentation::Instrumentation* instrumentation = |
| 477 | art::Runtime::Current()->GetInstrumentation(); |
| 478 | // Enable deoptimization |
| 479 | instrumentation->EnableDeoptimization(); |
| 480 | // Tell instrumentation we will be deopting single threads. |
| 481 | instrumentation->EnableSingleThreadDeopt(); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 482 | } else { |
| 483 | deoptimization_status_lock_.ExclusiveUnlock(self); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void DeoptManager::DeoptimizeThread(art::Thread* target) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 488 | // We might or might not be running on the target thread (self) so get Thread::Current |
| 489 | // directly. |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 490 | art::ScopedThreadSuspension sts(art::Thread::Current(), art::kSuspended); |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 491 | art::gc::ScopedGCCriticalSection sgccs(art::Thread::Current(), |
| 492 | art::gc::GcCause::kGcCauseDebugger, |
| 493 | art::gc::CollectorType::kCollectorTypeDebugger); |
Nicolas Geoffray | e91e795 | 2020-01-23 10:15:56 +0000 | [diff] [blame] | 494 | art::ScopedSuspendAll ssa("Instrument thread stack"); |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 495 | art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(target); |
| 496 | } |
| 497 | |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 498 | extern DeoptManager* gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 499 | DeoptManager* DeoptManager::Get() { |
Alex Light | 0e84118 | 2018-02-12 17:42:50 +0000 | [diff] [blame] | 500 | return gDeoptManager; |
Alex Light | 0fa1786 | 2017-10-24 13:43:05 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | } // namespace openjdkjvmti |