Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_RUNTIME_MONITOR_H_
     18 #define ART_RUNTIME_MONITOR_H_
     19 
     20 #include <pthread.h>
     21 #include <stdint.h>
     22 
     23 #include <iosfwd>
     24 #include <list>
     25 #include <vector>
     26 
     27 #include "base/mutex.h"
     28 #include "root_visitor.h"
     29 #include "thread_state.h"
     30 
     31 namespace art {
     32 
     33 /*
     34  * Monitor shape field. Used to distinguish thin locks from fat locks.
     35  */
     36 #define LW_SHAPE_THIN 0
     37 #define LW_SHAPE_FAT 1
     38 
     39 /*
     40  * Hash state field.  Used to signify that an object has had its
     41  * identity hash code exposed or relocated.
     42  */
     43 #define LW_HASH_STATE_UNHASHED 0
     44 #define LW_HASH_STATE_HASHED 1
     45 #define LW_HASH_STATE_HASHED_AND_MOVED 3
     46 #define LW_HASH_STATE_MASK 0x3
     47 #define LW_HASH_STATE_SHIFT 1
     48 #define LW_HASH_STATE(x) (((x) >> LW_HASH_STATE_SHIFT) & LW_HASH_STATE_MASK)
     49 
     50 /*
     51  * Lock owner field.  Contains the thread id of the thread currently
     52  * holding the lock.
     53  */
     54 #define LW_LOCK_OWNER_MASK 0xffff
     55 #define LW_LOCK_OWNER_SHIFT 3
     56 #define LW_LOCK_OWNER(x) (((x) >> LW_LOCK_OWNER_SHIFT) & LW_LOCK_OWNER_MASK)
     57 
     58 namespace mirror {
     59   class ArtMethod;
     60   class Object;
     61 }  // namespace mirror
     62 class Thread;
     63 class StackVisitor;
     64 
     65 class Monitor {
     66  public:
     67   ~Monitor();
     68 
     69   static bool IsSensitiveThread();
     70   static void Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)());
     71 
     72   static uint32_t GetThinLockId(uint32_t raw_lock_word)
     73       NO_THREAD_SAFETY_ANALYSIS;  // Reading lock owner without holding lock is racy.
     74 
     75   static void MonitorEnter(Thread* thread, mirror::Object* obj)
     76       EXCLUSIVE_LOCK_FUNCTION(monitor_lock_)
     77       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     78   static bool MonitorExit(Thread* thread, mirror::Object* obj)
     79       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
     80       UNLOCK_FUNCTION(monitor_lock_);
     81 
     82   static void Notify(Thread* self, mirror::Object* obj)
     83       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     84   static void NotifyAll(Thread* self, mirror::Object* obj)
     85       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     86   static void Wait(Thread* self, mirror::Object* obj, int64_t ms, int32_t ns,
     87                    bool interruptShouldThrow, ThreadState why)
     88       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     89 
     90   static void DescribeWait(std::ostream& os, const Thread* thread)
     91       LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
     92       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     93 
     94   // Used to implement JDWP's ThreadReference.CurrentContendedMonitor.
     95   static mirror::Object* GetContendedMonitor(Thread* thread);
     96 
     97   // Calls 'callback' once for each lock held in the single stack frame represented by
     98   // the current state of 'stack_visitor'.
     99   static void VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
    100                          void* callback_context)
    101       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    102 
    103   static bool IsValidLockWord(int32_t lock_word);
    104 
    105   mirror::Object* GetObject();
    106 
    107  private:
    108   explicit Monitor(Thread* owner, mirror::Object* obj)
    109       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    110 
    111   void AppendToWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
    112   void RemoveFromWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
    113 
    114   static void Inflate(Thread* self, mirror::Object* obj)
    115       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    116 
    117   void LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent,
    118                           const char* owner_filename, uint32_t owner_line_number)
    119       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    120 
    121   static void FailedUnlock(mirror::Object* obj, Thread* expected_owner, Thread* found_owner, Monitor* mon)
    122       LOCKS_EXCLUDED(Locks::thread_list_lock_)
    123       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    124 
    125   void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(monitor_lock_);
    126   bool Unlock(Thread* thread, bool for_wait) UNLOCK_FUNCTION(monitor_lock_);
    127 
    128   void Notify(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
    129   void NotifyWithLock(Thread* self)
    130       EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_)
    131       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    132 
    133   void NotifyAll(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
    134   void NotifyAllWithLock()
    135       EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_)
    136       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    137 
    138 
    139   void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why)
    140       NO_THREAD_SAFETY_ANALYSIS;
    141   void WaitWithLock(Thread* self, int64_t ms, int32_t ns, bool interruptShouldThrow, ThreadState why)
    142       EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_)
    143       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    144 
    145   // Translates the provided method and pc into its declaring class' source file and line number.
    146   void TranslateLocation(const mirror::ArtMethod* method, uint32_t pc,
    147                          const char*& source_file, uint32_t& line_number) const
    148       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    149 
    150   static bool (*is_sensitive_thread_hook_)();
    151   static uint32_t lock_profiling_threshold_;
    152 
    153   Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
    154 
    155   // Which thread currently owns the lock?
    156   Thread* volatile owner_;
    157 
    158   // Owner's recursive lock depth.
    159   int lock_count_ GUARDED_BY(monitor_lock_);
    160 
    161   // What object are we part of (for debugging).
    162   mirror::Object* const obj_;
    163 
    164   // Threads currently waiting on this monitor.
    165   Thread* wait_set_ GUARDED_BY(monitor_lock_);
    166 
    167   // Method and dex pc where the lock owner acquired the lock, used when lock
    168   // sampling is enabled. locking_method_ may be null if the lock is currently
    169   // unlocked, or if the lock is acquired by the system when the stack is empty.
    170   const mirror::ArtMethod* locking_method_ GUARDED_BY(monitor_lock_);
    171   uint32_t locking_dex_pc_ GUARDED_BY(monitor_lock_);
    172 
    173   friend class MonitorInfo;
    174   friend class MonitorList;
    175   friend class mirror::Object;
    176   DISALLOW_COPY_AND_ASSIGN(Monitor);
    177 };
    178 
    179 class MonitorList {
    180  public:
    181   MonitorList();
    182   ~MonitorList();
    183 
    184   void Add(Monitor* m);
    185   void SweepMonitorList(IsMarkedTester is_marked, void* arg)
    186       SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
    187   void DisallowNewMonitors();
    188   void AllowNewMonitors();
    189  private:
    190   bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_);
    191   Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
    192   ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_);
    193   std::list<Monitor*> list_ GUARDED_BY(monitor_list_lock_);
    194 
    195   friend class Monitor;
    196   DISALLOW_COPY_AND_ASSIGN(MonitorList);
    197 };
    198 
    199 // Collects information about the current state of an object's monitor.
    200 // This is very unsafe, and must only be called when all threads are suspended.
    201 // For use only by the JDWP implementation.
    202 class MonitorInfo {
    203  public:
    204   explicit MonitorInfo(mirror::Object* o) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
    205 
    206   Thread* owner;
    207   size_t entry_count;
    208   std::vector<Thread*> waiters;
    209 
    210  private:
    211   DISALLOW_COPY_AND_ASSIGN(MonitorInfo);
    212 };
    213 
    214 }  // namespace art
    215 
    216 #endif  // ART_RUNTIME_MONITOR_H_
    217