1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2008 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_MONITOR_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_MONITOR_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <pthread.h> 21*795d594fSAndroid Build Coastguard Worker #include <stdint.h> 22*795d594fSAndroid Build Coastguard Worker #include <stdlib.h> 23*795d594fSAndroid Build Coastguard Worker 24*795d594fSAndroid Build Coastguard Worker #include <atomic> 25*795d594fSAndroid Build Coastguard Worker #include <iosfwd> 26*795d594fSAndroid Build Coastguard Worker #include <list> 27*795d594fSAndroid Build Coastguard Worker #include <vector> 28*795d594fSAndroid Build Coastguard Worker 29*795d594fSAndroid Build Coastguard Worker #include "base/allocator.h" 30*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h" 31*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 32*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h" 33*795d594fSAndroid Build Coastguard Worker #include "gc_root.h" 34*795d594fSAndroid Build Coastguard Worker #include "lock_word.h" 35*795d594fSAndroid Build Coastguard Worker #include "obj_ptr.h" 36*795d594fSAndroid Build Coastguard Worker #include "read_barrier_option.h" 37*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h" 38*795d594fSAndroid Build Coastguard Worker #include "thread_state.h" 39*795d594fSAndroid Build Coastguard Worker 40*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 41*795d594fSAndroid Build Coastguard Worker 42*795d594fSAndroid Build Coastguard Worker class ArtMethod; 43*795d594fSAndroid Build Coastguard Worker class IsMarkedVisitor; 44*795d594fSAndroid Build Coastguard Worker class LockWord; 45*795d594fSAndroid Build Coastguard Worker template<class T> class Handle; 46*795d594fSAndroid Build Coastguard Worker class StackVisitor; 47*795d594fSAndroid Build Coastguard Worker class Thread; 48*795d594fSAndroid Build Coastguard Worker using MonitorId = uint32_t; 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker namespace mirror { 51*795d594fSAndroid Build Coastguard Worker class Object; 52*795d594fSAndroid Build Coastguard Worker } // namespace mirror 53*795d594fSAndroid Build Coastguard Worker 54*795d594fSAndroid Build Coastguard Worker enum class LockReason { 55*795d594fSAndroid Build Coastguard Worker kForWait, 56*795d594fSAndroid Build Coastguard Worker kForLock, 57*795d594fSAndroid Build Coastguard Worker }; 58*795d594fSAndroid Build Coastguard Worker 59*795d594fSAndroid Build Coastguard Worker class Monitor { 60*795d594fSAndroid Build Coastguard Worker public: 61*795d594fSAndroid Build Coastguard Worker // The default number of spins that are done before thread suspension is used to forcibly inflate 62*795d594fSAndroid Build Coastguard Worker // a lock word. See Runtime::max_spins_before_thin_lock_inflation_. 63*795d594fSAndroid Build Coastguard Worker constexpr static size_t kDefaultMaxSpinsBeforeThinLockInflation = 50; 64*795d594fSAndroid Build Coastguard Worker 65*795d594fSAndroid Build Coastguard Worker static constexpr int kDefaultMonitorTimeoutMs = 500; 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker static constexpr int kMonitorTimeoutMinMs = 200; 68*795d594fSAndroid Build Coastguard Worker 69*795d594fSAndroid Build Coastguard Worker static constexpr int kMonitorTimeoutMaxMs = 1000; // 1 second 70*795d594fSAndroid Build Coastguard Worker 71*795d594fSAndroid Build Coastguard Worker ~Monitor(); 72*795d594fSAndroid Build Coastguard Worker 73*795d594fSAndroid Build Coastguard Worker static void Init(uint32_t lock_profiling_threshold, uint32_t stack_dump_lock_profiling_threshold); 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker // Return the thread id of the lock owner or 0 when there is no owner. 76*795d594fSAndroid Build Coastguard Worker EXPORT static uint32_t GetLockOwnerThreadId(ObjPtr<mirror::Object> obj) 77*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 78*795d594fSAndroid Build Coastguard Worker 79*795d594fSAndroid Build Coastguard Worker // NO_THREAD_SAFETY_ANALYSIS for mon->Lock. 80*795d594fSAndroid Build Coastguard Worker EXPORT static ObjPtr<mirror::Object> MonitorEnter(Thread* thread, 81*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> obj, 82*795d594fSAndroid Build Coastguard Worker bool trylock) 83*795d594fSAndroid Build Coastguard Worker EXCLUSIVE_LOCK_FUNCTION(obj.Ptr()) 84*795d594fSAndroid Build Coastguard Worker NO_THREAD_SAFETY_ANALYSIS 85*795d594fSAndroid Build Coastguard Worker REQUIRES(!Roles::uninterruptible_) 86*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 87*795d594fSAndroid Build Coastguard Worker 88*795d594fSAndroid Build Coastguard Worker // NO_THREAD_SAFETY_ANALYSIS for mon->Unlock. 89*795d594fSAndroid Build Coastguard Worker EXPORT static bool MonitorExit(Thread* thread, ObjPtr<mirror::Object> obj) 90*795d594fSAndroid Build Coastguard Worker NO_THREAD_SAFETY_ANALYSIS 91*795d594fSAndroid Build Coastguard Worker REQUIRES(!Roles::uninterruptible_) 92*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 93*795d594fSAndroid Build Coastguard Worker UNLOCK_FUNCTION(obj.Ptr()); 94*795d594fSAndroid Build Coastguard Worker Notify(Thread * self,ObjPtr<mirror::Object> obj)95*795d594fSAndroid Build Coastguard Worker static void Notify(Thread* self, ObjPtr<mirror::Object> obj) 96*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) { 97*795d594fSAndroid Build Coastguard Worker DoNotify(self, obj, false); 98*795d594fSAndroid Build Coastguard Worker } NotifyAll(Thread * self,ObjPtr<mirror::Object> obj)99*795d594fSAndroid Build Coastguard Worker static void NotifyAll(Thread* self, ObjPtr<mirror::Object> obj) 100*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) { 101*795d594fSAndroid Build Coastguard Worker DoNotify(self, obj, true); 102*795d594fSAndroid Build Coastguard Worker } 103*795d594fSAndroid Build Coastguard Worker 104*795d594fSAndroid Build Coastguard Worker // Object.wait(). Also called for class init. 105*795d594fSAndroid Build Coastguard Worker // NO_THREAD_SAFETY_ANALYSIS for mon->Wait. 106*795d594fSAndroid Build Coastguard Worker EXPORT static void Wait(Thread* self, 107*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> obj, 108*795d594fSAndroid Build Coastguard Worker int64_t ms, 109*795d594fSAndroid Build Coastguard Worker int32_t ns, 110*795d594fSAndroid Build Coastguard Worker bool interruptShouldThrow, 111*795d594fSAndroid Build Coastguard Worker ThreadState why) 112*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) NO_THREAD_SAFETY_ANALYSIS; 113*795d594fSAndroid Build Coastguard Worker 114*795d594fSAndroid Build Coastguard Worker static ThreadState FetchState(const Thread* thread, 115*795d594fSAndroid Build Coastguard Worker /* out */ ObjPtr<mirror::Object>* monitor_object, 116*795d594fSAndroid Build Coastguard Worker /* out */ uint32_t* lock_owner_tid) 117*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_suspend_count_lock_) 118*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 119*795d594fSAndroid Build Coastguard Worker 120*795d594fSAndroid Build Coastguard Worker // Used to implement JDWP's ThreadReference.CurrentContendedMonitor. 121*795d594fSAndroid Build Coastguard Worker EXPORT static ObjPtr<mirror::Object> GetContendedMonitor(Thread* thread) 122*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 123*795d594fSAndroid Build Coastguard Worker 124*795d594fSAndroid Build Coastguard Worker // Calls 'callback' once for each lock held in the single stack frame represented by 125*795d594fSAndroid Build Coastguard Worker // the current state of 'stack_visitor'. 126*795d594fSAndroid Build Coastguard Worker // The abort_on_failure flag allows to not die when the state of the runtime is unorderly. This 127*795d594fSAndroid Build Coastguard Worker // is necessary when we have already aborted but want to dump the stack as much as we can. 128*795d594fSAndroid Build Coastguard Worker EXPORT static void VisitLocks(StackVisitor* stack_visitor, 129*795d594fSAndroid Build Coastguard Worker void (*callback)(ObjPtr<mirror::Object>, void*), 130*795d594fSAndroid Build Coastguard Worker void* callback_context, 131*795d594fSAndroid Build Coastguard Worker bool abort_on_failure = true) REQUIRES_SHARED(Locks::mutator_lock_); 132*795d594fSAndroid Build Coastguard Worker 133*795d594fSAndroid Build Coastguard Worker static bool IsValidLockWord(LockWord lock_word); 134*795d594fSAndroid Build Coastguard Worker 135*795d594fSAndroid Build Coastguard Worker template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> 136*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> GetObject() REQUIRES_SHARED(Locks::mutator_lock_); 137*795d594fSAndroid Build Coastguard Worker 138*795d594fSAndroid Build Coastguard Worker void SetObject(ObjPtr<mirror::Object> object) REQUIRES_SHARED(Locks::mutator_lock_); 139*795d594fSAndroid Build Coastguard Worker 140*795d594fSAndroid Build Coastguard Worker // Provides no memory ordering guarantees. GetOwner()141*795d594fSAndroid Build Coastguard Worker Thread* GetOwner() const REQUIRES_SHARED(Locks::mutator_lock_) { 142*795d594fSAndroid Build Coastguard Worker return owner_.load(std::memory_order_relaxed); 143*795d594fSAndroid Build Coastguard Worker } 144*795d594fSAndroid Build Coastguard Worker 145*795d594fSAndroid Build Coastguard Worker int32_t GetHashCode(); 146*795d594fSAndroid Build Coastguard Worker 147*795d594fSAndroid Build Coastguard Worker // Is the monitor currently locked? Debug only, provides no memory ordering guarantees. 148*795d594fSAndroid Build Coastguard Worker bool IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!monitor_lock_); 149*795d594fSAndroid Build Coastguard Worker HasHashCode()150*795d594fSAndroid Build Coastguard Worker bool HasHashCode() const { 151*795d594fSAndroid Build Coastguard Worker return hash_code_.load(std::memory_order_relaxed) != 0; 152*795d594fSAndroid Build Coastguard Worker } 153*795d594fSAndroid Build Coastguard Worker GetMonitorId()154*795d594fSAndroid Build Coastguard Worker MonitorId GetMonitorId() const { 155*795d594fSAndroid Build Coastguard Worker return monitor_id_; 156*795d594fSAndroid Build Coastguard Worker } 157*795d594fSAndroid Build Coastguard Worker 158*795d594fSAndroid Build Coastguard Worker // Inflate the lock on obj. May fail to inflate for spurious reasons, always re-check. 159*795d594fSAndroid Build Coastguard Worker // attempt_of_4 is in 1..4 inclusive or 0. A non-zero value indicates that we are retrying 160*795d594fSAndroid Build Coastguard Worker // up to 4 times, and should only abort on 4. Zero means we are only trying once, with the 161*795d594fSAndroid Build Coastguard Worker // full suspend timeout instead of a quarter. 162*795d594fSAndroid Build Coastguard Worker static void InflateThinLocked(Thread* self, 163*795d594fSAndroid Build Coastguard Worker Handle<mirror::Object> obj, 164*795d594fSAndroid Build Coastguard Worker LockWord lock_word, 165*795d594fSAndroid Build Coastguard Worker uint32_t hash_code, 166*795d594fSAndroid Build Coastguard Worker int attempt_of_4 = 0) REQUIRES_SHARED(Locks::mutator_lock_); 167*795d594fSAndroid Build Coastguard Worker 168*795d594fSAndroid Build Coastguard Worker // Try to deflate the monitor associated with obj. Only called when we logically hold 169*795d594fSAndroid Build Coastguard Worker // mutator_lock_ exclusively. ImageWriter calls this without actually invoking SuspendAll, but 170*795d594fSAndroid Build Coastguard Worker // it is already entirely single-threaded. 171*795d594fSAndroid Build Coastguard Worker EXPORT static bool Deflate(Thread* self, ObjPtr<mirror::Object> obj) 172*795d594fSAndroid Build Coastguard Worker REQUIRES(Locks::mutator_lock_); 173*795d594fSAndroid Build Coastguard Worker 174*795d594fSAndroid Build Coastguard Worker #ifndef __LP64__ new(size_t size)175*795d594fSAndroid Build Coastguard Worker void* operator new(size_t size) { 176*795d594fSAndroid Build Coastguard Worker // Align Monitor* as per the monitor ID field size in the lock word. 177*795d594fSAndroid Build Coastguard Worker void* result; 178*795d594fSAndroid Build Coastguard Worker int error = posix_memalign(&result, LockWord::kMonitorIdAlignment, size); 179*795d594fSAndroid Build Coastguard Worker CHECK_EQ(error, 0) << strerror(error); 180*795d594fSAndroid Build Coastguard Worker return result; 181*795d594fSAndroid Build Coastguard Worker } 182*795d594fSAndroid Build Coastguard Worker delete(void * ptr)183*795d594fSAndroid Build Coastguard Worker void operator delete(void* ptr) { 184*795d594fSAndroid Build Coastguard Worker free(ptr); 185*795d594fSAndroid Build Coastguard Worker } 186*795d594fSAndroid Build Coastguard Worker #endif 187*795d594fSAndroid Build Coastguard Worker 188*795d594fSAndroid Build Coastguard Worker private: 189*795d594fSAndroid Build Coastguard Worker Monitor(Thread* self, Thread* owner, ObjPtr<mirror::Object> obj, int32_t hash_code) 190*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 191*795d594fSAndroid Build Coastguard Worker Monitor(Thread* self, Thread* owner, ObjPtr<mirror::Object> obj, int32_t hash_code, MonitorId id) 192*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 193*795d594fSAndroid Build Coastguard Worker 194*795d594fSAndroid Build Coastguard Worker // Install the monitor into its object, may fail if another thread installs a different monitor 195*795d594fSAndroid Build Coastguard Worker // first. Monitor remains in the same logical state as before, i.e. held the same # of times. 196*795d594fSAndroid Build Coastguard Worker bool Install(Thread* self) 197*795d594fSAndroid Build Coastguard Worker REQUIRES(!monitor_lock_) 198*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 199*795d594fSAndroid Build Coastguard Worker 200*795d594fSAndroid Build Coastguard Worker // Links a thread into a monitor's wait set. The monitor lock must be held by the caller of this 201*795d594fSAndroid Build Coastguard Worker // routine. 202*795d594fSAndroid Build Coastguard Worker void AppendToWaitSet(Thread* thread) REQUIRES(monitor_lock_); 203*795d594fSAndroid Build Coastguard Worker 204*795d594fSAndroid Build Coastguard Worker // Unlinks a thread from a monitor's wait set. The monitor lock must be held by the caller of 205*795d594fSAndroid Build Coastguard Worker // this routine. 206*795d594fSAndroid Build Coastguard Worker void RemoveFromWaitSet(Thread* thread) REQUIRES(monitor_lock_); 207*795d594fSAndroid Build Coastguard Worker 208*795d594fSAndroid Build Coastguard Worker // Release the monitor lock and signal a waiting thread that has been notified and now needs the 209*795d594fSAndroid Build Coastguard Worker // lock. Assumes the monitor lock is held exactly once, and the owner_ field has been reset to 210*795d594fSAndroid Build Coastguard Worker // null. Caller may be suspended (Wait) or runnable (MonitorExit). 211*795d594fSAndroid Build Coastguard Worker void SignalWaiterAndReleaseMonitorLock(Thread* self) RELEASE(monitor_lock_); 212*795d594fSAndroid Build Coastguard Worker 213*795d594fSAndroid Build Coastguard Worker // Changes the shape of a monitor from thin to fat, preserving the internal lock state. The 214*795d594fSAndroid Build Coastguard Worker // calling thread must own the lock or the owner must be suspended. There's a race with other 215*795d594fSAndroid Build Coastguard Worker // threads inflating the lock, installing hash codes and spurious failures. The caller should 216*795d594fSAndroid Build Coastguard Worker // re-read the lock word following the call. 217*795d594fSAndroid Build Coastguard Worker static void Inflate(Thread* self, Thread* owner, ObjPtr<mirror::Object> obj, int32_t hash_code) 218*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) 219*795d594fSAndroid Build Coastguard Worker NO_THREAD_SAFETY_ANALYSIS; // For m->Install(self) 220*795d594fSAndroid Build Coastguard Worker 221*795d594fSAndroid Build Coastguard Worker void LogContentionEvent(Thread* self, 222*795d594fSAndroid Build Coastguard Worker uint32_t wait_ms, 223*795d594fSAndroid Build Coastguard Worker uint32_t sample_percent, 224*795d594fSAndroid Build Coastguard Worker ArtMethod* owner_method, 225*795d594fSAndroid Build Coastguard Worker uint32_t owner_dex_pc) 226*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 227*795d594fSAndroid Build Coastguard Worker 228*795d594fSAndroid Build Coastguard Worker static void FailedUnlock(ObjPtr<mirror::Object> obj, 229*795d594fSAndroid Build Coastguard Worker uint32_t expected_owner_thread_id, 230*795d594fSAndroid Build Coastguard Worker uint32_t found_owner_thread_id, 231*795d594fSAndroid Build Coastguard Worker Monitor* mon) 232*795d594fSAndroid Build Coastguard Worker REQUIRES(!Locks::thread_list_lock_) 233*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 234*795d594fSAndroid Build Coastguard Worker 235*795d594fSAndroid Build Coastguard Worker // Try to lock without blocking, returns true if we acquired the lock. 236*795d594fSAndroid Build Coastguard Worker // If spin is true, then we spin for a short period before failing. 237*795d594fSAndroid Build Coastguard Worker bool TryLock(Thread* self, bool spin = false) 238*795d594fSAndroid Build Coastguard Worker TRY_ACQUIRE(true, monitor_lock_) 239*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 240*795d594fSAndroid Build Coastguard Worker 241*795d594fSAndroid Build Coastguard Worker template<LockReason reason = LockReason::kForLock> 242*795d594fSAndroid Build Coastguard Worker void Lock(Thread* self) 243*795d594fSAndroid Build Coastguard Worker ACQUIRE(monitor_lock_) 244*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 245*795d594fSAndroid Build Coastguard Worker 246*795d594fSAndroid Build Coastguard Worker bool Unlock(Thread* thread) 247*795d594fSAndroid Build Coastguard Worker RELEASE(monitor_lock_) 248*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 249*795d594fSAndroid Build Coastguard Worker 250*795d594fSAndroid Build Coastguard Worker static void DoNotify(Thread* self, ObjPtr<mirror::Object> obj, bool notify_all) 251*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) NO_THREAD_SAFETY_ANALYSIS; // For mon->Notify. 252*795d594fSAndroid Build Coastguard Worker 253*795d594fSAndroid Build Coastguard Worker void Notify(Thread* self) 254*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_) 255*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 256*795d594fSAndroid Build Coastguard Worker 257*795d594fSAndroid Build Coastguard Worker void NotifyAll(Thread* self) 258*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_) 259*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 260*795d594fSAndroid Build Coastguard Worker 261*795d594fSAndroid Build Coastguard Worker static std::string PrettyContentionInfo(const std::string& owner_name, 262*795d594fSAndroid Build Coastguard Worker pid_t owner_tid, 263*795d594fSAndroid Build Coastguard Worker ArtMethod* owners_method, 264*795d594fSAndroid Build Coastguard Worker uint32_t owners_dex_pc, 265*795d594fSAndroid Build Coastguard Worker size_t num_waiters) 266*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 267*795d594fSAndroid Build Coastguard Worker 268*795d594fSAndroid Build Coastguard Worker // Wait on a monitor until timeout, interrupt, or notification. Used for Object.wait() and 269*795d594fSAndroid Build Coastguard Worker // (somewhat indirectly) Thread.sleep() and Thread.join(). 270*795d594fSAndroid Build Coastguard Worker // 271*795d594fSAndroid Build Coastguard Worker // If another thread calls Thread.interrupt(), we throw InterruptedException and return 272*795d594fSAndroid Build Coastguard Worker // immediately if one of the following are true: 273*795d594fSAndroid Build Coastguard Worker // - blocked in wait(), wait(long), or wait(long, int) methods of Object 274*795d594fSAndroid Build Coastguard Worker // - blocked in join(), join(long), or join(long, int) methods of Thread 275*795d594fSAndroid Build Coastguard Worker // - blocked in sleep(long), or sleep(long, int) methods of Thread 276*795d594fSAndroid Build Coastguard Worker // Otherwise, we set the "interrupted" flag. 277*795d594fSAndroid Build Coastguard Worker // 278*795d594fSAndroid Build Coastguard Worker // Checks to make sure that "ns" is in the range 0-999999 (i.e. fractions of a millisecond) and 279*795d594fSAndroid Build Coastguard Worker // throws the appropriate exception if it isn't. 280*795d594fSAndroid Build Coastguard Worker // 281*795d594fSAndroid Build Coastguard Worker // The spec allows "spurious wakeups", and recommends that all code using Object.wait() do so in 282*795d594fSAndroid Build Coastguard Worker // a loop. This appears to derive from concerns about pthread_cond_wait() on multiprocessor 283*795d594fSAndroid Build Coastguard Worker // systems. Some commentary on the web casts doubt on whether these can/should occur. 284*795d594fSAndroid Build Coastguard Worker // 285*795d594fSAndroid Build Coastguard Worker // Since we're allowed to wake up "early", we clamp extremely long durations to return at the end 286*795d594fSAndroid Build Coastguard Worker // of the 32-bit time epoch. 287*795d594fSAndroid Build Coastguard Worker void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why) 288*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_) 289*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 290*795d594fSAndroid Build Coastguard Worker 291*795d594fSAndroid Build Coastguard Worker // Translates the provided method and pc into its declaring class' source file and line number. 292*795d594fSAndroid Build Coastguard Worker static void TranslateLocation(ArtMethod* method, uint32_t pc, 293*795d594fSAndroid Build Coastguard Worker const char** source_file, 294*795d594fSAndroid Build Coastguard Worker int32_t* line_number) 295*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 296*795d594fSAndroid Build Coastguard Worker 297*795d594fSAndroid Build Coastguard Worker // Provides no memory ordering guarantees. 298*795d594fSAndroid Build Coastguard Worker uint32_t GetOwnerThreadId() REQUIRES(!Locks::thread_list_lock_) 299*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 300*795d594fSAndroid Build Coastguard Worker 301*795d594fSAndroid Build Coastguard Worker // Set locking_method_ and locking_dex_pc_ corresponding to owner's current stack. 302*795d594fSAndroid Build Coastguard Worker // owner is either self or suspended. 303*795d594fSAndroid Build Coastguard Worker void SetLockingMethod(Thread* owner) REQUIRES(monitor_lock_) 304*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 305*795d594fSAndroid Build Coastguard Worker 306*795d594fSAndroid Build Coastguard Worker // The same, but without checking for a proxy method. Currently requires owner == self. 307*795d594fSAndroid Build Coastguard Worker void SetLockingMethodNoProxy(Thread* owner) REQUIRES(monitor_lock_) 308*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 309*795d594fSAndroid Build Coastguard Worker 310*795d594fSAndroid Build Coastguard Worker // Support for systrace output of monitor operations. 311*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static void AtraceMonitorLock(Thread* self, 312*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> obj, 313*795d594fSAndroid Build Coastguard Worker bool is_wait) 314*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 315*795d594fSAndroid Build Coastguard Worker static void AtraceMonitorLockImpl(Thread* self, 316*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> obj, 317*795d594fSAndroid Build Coastguard Worker bool is_wait) 318*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_); 319*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static void AtraceMonitorUnlock(); 320*795d594fSAndroid Build Coastguard Worker 321*795d594fSAndroid Build Coastguard Worker static uint32_t lock_profiling_threshold_; 322*795d594fSAndroid Build Coastguard Worker static uint32_t stack_dump_lock_profiling_threshold_; 323*795d594fSAndroid Build Coastguard Worker static bool capture_method_eagerly_; 324*795d594fSAndroid Build Coastguard Worker 325*795d594fSAndroid Build Coastguard Worker // Holding the monitor N times is represented by holding monitor_lock_ N times. 326*795d594fSAndroid Build Coastguard Worker Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 327*795d594fSAndroid Build Coastguard Worker 328*795d594fSAndroid Build Coastguard Worker // Pretend to unlock monitor lock. FakeUnlockMonitorLock()329*795d594fSAndroid Build Coastguard Worker void FakeUnlockMonitorLock() RELEASE(monitor_lock_) NO_THREAD_SAFETY_ANALYSIS {} 330*795d594fSAndroid Build Coastguard Worker 331*795d594fSAndroid Build Coastguard Worker // Number of threads either waiting on the condition or waiting on a contended 332*795d594fSAndroid Build Coastguard Worker // monitor acquisition. Prevents deflation. 333*795d594fSAndroid Build Coastguard Worker std::atomic<size_t> num_waiters_; 334*795d594fSAndroid Build Coastguard Worker 335*795d594fSAndroid Build Coastguard Worker // Which thread currently owns the lock? monitor_lock_ only keeps the tid. 336*795d594fSAndroid Build Coastguard Worker // Only set while holding monitor_lock_. Non-locking readers only use it to 337*795d594fSAndroid Build Coastguard Worker // compare to self or for debugging. 338*795d594fSAndroid Build Coastguard Worker std::atomic<Thread*> owner_; 339*795d594fSAndroid Build Coastguard Worker 340*795d594fSAndroid Build Coastguard Worker // Owner's recursive lock depth. Owner_ non-null, and lock_count_ == 0 ==> held once. 341*795d594fSAndroid Build Coastguard Worker unsigned int lock_count_ GUARDED_BY(monitor_lock_); 342*795d594fSAndroid Build Coastguard Worker 343*795d594fSAndroid Build Coastguard Worker // Owner's recursive lock depth is given by monitor_lock_.GetDepth(). 344*795d594fSAndroid Build Coastguard Worker 345*795d594fSAndroid Build Coastguard Worker // What object are we part of. This is a weak root. Do not access 346*795d594fSAndroid Build Coastguard Worker // this directly, use GetObject() to read it so it will be guarded 347*795d594fSAndroid Build Coastguard Worker // by a read barrier. 348*795d594fSAndroid Build Coastguard Worker GcRoot<mirror::Object> obj_; 349*795d594fSAndroid Build Coastguard Worker 350*795d594fSAndroid Build Coastguard Worker // Threads currently waiting on this monitor. 351*795d594fSAndroid Build Coastguard Worker Thread* wait_set_ GUARDED_BY(monitor_lock_); 352*795d594fSAndroid Build Coastguard Worker 353*795d594fSAndroid Build Coastguard Worker // Threads that were waiting on this monitor, but are now contending on it. 354*795d594fSAndroid Build Coastguard Worker Thread* wake_set_ GUARDED_BY(monitor_lock_); 355*795d594fSAndroid Build Coastguard Worker 356*795d594fSAndroid Build Coastguard Worker // Stored object hash code, generated lazily by GetHashCode. 357*795d594fSAndroid Build Coastguard Worker AtomicInteger hash_code_; 358*795d594fSAndroid Build Coastguard Worker 359*795d594fSAndroid Build Coastguard Worker // Data structure used to remember the method and dex pc of a recent holder of the 360*795d594fSAndroid Build Coastguard Worker // lock. Used for tracing and contention reporting. Setting these is expensive, since it 361*795d594fSAndroid Build Coastguard Worker // involves a partial stack walk. We set them only as follows, to minimize the cost: 362*795d594fSAndroid Build Coastguard Worker // - If tracing is enabled, they are needed immediately when we first notice contention, so we 363*795d594fSAndroid Build Coastguard Worker // set them unconditionally when a monitor is acquired. 364*795d594fSAndroid Build Coastguard Worker // - If contention reporting is enabled, we use the lock_owner_request_ field to have the 365*795d594fSAndroid Build Coastguard Worker // contending thread request them. The current owner then sets them when releasing the monitor, 366*795d594fSAndroid Build Coastguard Worker // making them available when the contending thread acquires the monitor. 367*795d594fSAndroid Build Coastguard Worker // - If tracing and contention reporting are enabled, we do both. This usually prevents us from 368*795d594fSAndroid Build Coastguard Worker // switching between reporting the end and beginning of critical sections for contention logging 369*795d594fSAndroid Build Coastguard Worker // when tracing is enabled. We expect that tracing overhead is normally much higher than for 370*795d594fSAndroid Build Coastguard Worker // contention logging, so the added cost should be small. It also minimizes glitches when 371*795d594fSAndroid Build Coastguard Worker // enabling and disabling traces. 372*795d594fSAndroid Build Coastguard Worker // We're tolerant of missing information. E.g. when tracing is initially turned on, we may 373*795d594fSAndroid Build Coastguard Worker // not have the lock holder information if the holder acquired the lock with tracing off. 374*795d594fSAndroid Build Coastguard Worker // 375*795d594fSAndroid Build Coastguard Worker // We make this data unconditionally atomic; for contention logging all accesses are in fact 376*795d594fSAndroid Build Coastguard Worker // protected by the monitor, but for tracing, reads are not. Writes are always 377*795d594fSAndroid Build Coastguard Worker // protected by the monitor. 378*795d594fSAndroid Build Coastguard Worker // 379*795d594fSAndroid Build Coastguard Worker // The fields are always accessed without memory ordering. We store a checksum, and reread if 380*795d594fSAndroid Build Coastguard Worker // the checksum doesn't correspond to the values. This results in values that are correct with 381*795d594fSAndroid Build Coastguard Worker // very high probability, but not certainty. 382*795d594fSAndroid Build Coastguard Worker // 383*795d594fSAndroid Build Coastguard Worker // If we need lock_owner information for a certain thread for contenion logging, we store its 384*795d594fSAndroid Build Coastguard Worker // tid in lock_owner_request_. To satisfy the request, we store lock_owner_tid_, 385*795d594fSAndroid Build Coastguard Worker // lock_owner_method_, and lock_owner_dex_pc_ and the corresponding checksum while holding the 386*795d594fSAndroid Build Coastguard Worker // monitor. 387*795d594fSAndroid Build Coastguard Worker // 388*795d594fSAndroid Build Coastguard Worker // At all times, either lock_owner_ is zero, the checksum is valid, or a thread is actively 389*795d594fSAndroid Build Coastguard Worker // in the process of establishing one of those states. Only one thread at a time can be actively 390*795d594fSAndroid Build Coastguard Worker // establishing such a state, since writes are protected by the monitor. 391*795d594fSAndroid Build Coastguard Worker std::atomic<Thread*> lock_owner_; // *lock_owner_ may no longer exist! 392*795d594fSAndroid Build Coastguard Worker std::atomic<ArtMethod*> lock_owner_method_; 393*795d594fSAndroid Build Coastguard Worker std::atomic<uint32_t> lock_owner_dex_pc_; 394*795d594fSAndroid Build Coastguard Worker std::atomic<uintptr_t> lock_owner_sum_; 395*795d594fSAndroid Build Coastguard Worker 396*795d594fSAndroid Build Coastguard Worker // Request lock owner save method and dex_pc. Written asynchronously. 397*795d594fSAndroid Build Coastguard Worker std::atomic<Thread*> lock_owner_request_; 398*795d594fSAndroid Build Coastguard Worker 399*795d594fSAndroid Build Coastguard Worker // Compute method, dex pc, and tid "checksum". 400*795d594fSAndroid Build Coastguard Worker uintptr_t LockOwnerInfoChecksum(ArtMethod* m, uint32_t dex_pc, Thread* t); 401*795d594fSAndroid Build Coastguard Worker 402*795d594fSAndroid Build Coastguard Worker // Set owning method, dex pc, and tid. owner_ field is set and points to current thread. 403*795d594fSAndroid Build Coastguard Worker void SetLockOwnerInfo(ArtMethod* method, uint32_t dex_pc, Thread* t) 404*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_); 405*795d594fSAndroid Build Coastguard Worker 406*795d594fSAndroid Build Coastguard Worker // Get owning method and dex pc for the given thread, if available. 407*795d594fSAndroid Build Coastguard Worker void GetLockOwnerInfo(/*out*/ArtMethod** method, /*out*/uint32_t* dex_pc, Thread* t); 408*795d594fSAndroid Build Coastguard Worker 409*795d594fSAndroid Build Coastguard Worker // Do the same, while holding the monitor. There are no concurrent updates. 410*795d594fSAndroid Build Coastguard Worker void GetLockOwnerInfoLocked(/*out*/ArtMethod** method, /*out*/uint32_t* dex_pc, 411*795d594fSAndroid Build Coastguard Worker uint32_t thread_id) 412*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_); 413*795d594fSAndroid Build Coastguard Worker 414*795d594fSAndroid Build Coastguard Worker // We never clear lock_owner method and dex pc. Since it often reflects 415*795d594fSAndroid Build Coastguard Worker // ownership when we last detected contention, it may be inconsistent with owner_ 416*795d594fSAndroid Build Coastguard Worker // and not 100% reliable. For lock contention monitoring, in the absence of tracing, 417*795d594fSAndroid Build Coastguard Worker // there is a small risk that the current owner may finish before noticing the request, 418*795d594fSAndroid Build Coastguard Worker // or the information will be overwritten by another intervening request and monitor 419*795d594fSAndroid Build Coastguard Worker // release, so it's also not 100% reliable. But if we report information at all, it 420*795d594fSAndroid Build Coastguard Worker // should generally (modulo accidental checksum matches) pertain to to an acquisition of the 421*795d594fSAndroid Build Coastguard Worker // right monitor by the right thread, so it's extremely unlikely to be seriously misleading. 422*795d594fSAndroid Build Coastguard Worker // Since we track threads by a pointer to the Thread structure, there is a small chance we may 423*795d594fSAndroid Build Coastguard Worker // confuse threads allocated at the same exact address, if a contending thread dies before 424*795d594fSAndroid Build Coastguard Worker // we inquire about it. 425*795d594fSAndroid Build Coastguard Worker 426*795d594fSAndroid Build Coastguard Worker // Check for and act on a pending lock_owner_request_ 427*795d594fSAndroid Build Coastguard Worker void CheckLockOwnerRequest(Thread* self) 428*795d594fSAndroid Build Coastguard Worker REQUIRES(monitor_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 429*795d594fSAndroid Build Coastguard Worker 430*795d594fSAndroid Build Coastguard Worker void MaybeEnableTimeout() REQUIRES(Locks::mutator_lock_); 431*795d594fSAndroid Build Coastguard Worker 432*795d594fSAndroid Build Coastguard Worker // The denser encoded version of this monitor as stored in the lock word. 433*795d594fSAndroid Build Coastguard Worker MonitorId monitor_id_; 434*795d594fSAndroid Build Coastguard Worker 435*795d594fSAndroid Build Coastguard Worker #ifdef __LP64__ 436*795d594fSAndroid Build Coastguard Worker // Free list for monitor pool. 437*795d594fSAndroid Build Coastguard Worker Monitor* next_free_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); 438*795d594fSAndroid Build Coastguard Worker #endif 439*795d594fSAndroid Build Coastguard Worker 440*795d594fSAndroid Build Coastguard Worker friend class MonitorInfo; 441*795d594fSAndroid Build Coastguard Worker friend class MonitorList; 442*795d594fSAndroid Build Coastguard Worker friend class MonitorPool; 443*795d594fSAndroid Build Coastguard Worker friend class mirror::Object; 444*795d594fSAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(Monitor); 445*795d594fSAndroid Build Coastguard Worker }; 446*795d594fSAndroid Build Coastguard Worker 447*795d594fSAndroid Build Coastguard Worker class MonitorList { 448*795d594fSAndroid Build Coastguard Worker public: 449*795d594fSAndroid Build Coastguard Worker MonitorList(); 450*795d594fSAndroid Build Coastguard Worker ~MonitorList(); 451*795d594fSAndroid Build Coastguard Worker 452*795d594fSAndroid Build Coastguard Worker void Add(Monitor* m) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!monitor_list_lock_); 453*795d594fSAndroid Build Coastguard Worker 454*795d594fSAndroid Build Coastguard Worker void SweepMonitorList(IsMarkedVisitor* visitor) 455*795d594fSAndroid Build Coastguard Worker REQUIRES(!monitor_list_lock_) REQUIRES_SHARED(Locks::mutator_lock_); 456*795d594fSAndroid Build Coastguard Worker void DisallowNewMonitors() REQUIRES(!monitor_list_lock_); 457*795d594fSAndroid Build Coastguard Worker void AllowNewMonitors() REQUIRES(!monitor_list_lock_); 458*795d594fSAndroid Build Coastguard Worker void BroadcastForNewMonitors() REQUIRES(!monitor_list_lock_); 459*795d594fSAndroid Build Coastguard Worker // Returns how many monitors were deflated. 460*795d594fSAndroid Build Coastguard Worker size_t DeflateMonitors() REQUIRES(!monitor_list_lock_) REQUIRES(Locks::mutator_lock_); 461*795d594fSAndroid Build Coastguard Worker EXPORT size_t Size() REQUIRES(!monitor_list_lock_); 462*795d594fSAndroid Build Coastguard Worker 463*795d594fSAndroid Build Coastguard Worker using Monitors = std::list<Monitor*, TrackingAllocator<Monitor*, kAllocatorTagMonitorList>>; 464*795d594fSAndroid Build Coastguard Worker 465*795d594fSAndroid Build Coastguard Worker private: 466*795d594fSAndroid Build Coastguard Worker // During sweeping we may free an object and on a separate thread have an object created using 467*795d594fSAndroid Build Coastguard Worker // the newly freed memory. That object may then have its lock-word inflated and a monitor created. 468*795d594fSAndroid Build Coastguard Worker // If we allow new monitor registration during sweeping this monitor may be incorrectly freed as 469*795d594fSAndroid Build Coastguard Worker // the object wasn't marked when sweeping began. 470*795d594fSAndroid Build Coastguard Worker bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_); 471*795d594fSAndroid Build Coastguard Worker Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 472*795d594fSAndroid Build Coastguard Worker ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_); 473*795d594fSAndroid Build Coastguard Worker Monitors list_ GUARDED_BY(monitor_list_lock_); 474*795d594fSAndroid Build Coastguard Worker 475*795d594fSAndroid Build Coastguard Worker friend class Monitor; 476*795d594fSAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(MonitorList); 477*795d594fSAndroid Build Coastguard Worker }; 478*795d594fSAndroid Build Coastguard Worker 479*795d594fSAndroid Build Coastguard Worker // Collects information about the current state of an object's monitor. 480*795d594fSAndroid Build Coastguard Worker // This is very unsafe, and must only be called when all threads are suspended. 481*795d594fSAndroid Build Coastguard Worker // For use only by the JDWP implementation. 482*795d594fSAndroid Build Coastguard Worker class MonitorInfo { 483*795d594fSAndroid Build Coastguard Worker public: MonitorInfo()484*795d594fSAndroid Build Coastguard Worker MonitorInfo() : owner_(nullptr), entry_count_(0) {} 485*795d594fSAndroid Build Coastguard Worker MonitorInfo(const MonitorInfo&) = default; 486*795d594fSAndroid Build Coastguard Worker MonitorInfo& operator=(const MonitorInfo&) = default; 487*795d594fSAndroid Build Coastguard Worker EXPORT explicit MonitorInfo(ObjPtr<mirror::Object> o) REQUIRES(Locks::mutator_lock_); 488*795d594fSAndroid Build Coastguard Worker 489*795d594fSAndroid Build Coastguard Worker Thread* owner_; 490*795d594fSAndroid Build Coastguard Worker size_t entry_count_; 491*795d594fSAndroid Build Coastguard Worker std::vector<Thread*> waiters_; 492*795d594fSAndroid Build Coastguard Worker }; 493*795d594fSAndroid Build Coastguard Worker 494*795d594fSAndroid Build Coastguard Worker } // namespace art 495*795d594fSAndroid Build Coastguard Worker 496*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_MONITOR_H_ 497