1 /* 2 * Copyright (C) 2011 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_THREAD_LIST_H_ 18 #define ART_RUNTIME_THREAD_LIST_H_ 19 20 #include "barrier.h" 21 #include "base/histogram.h" 22 #include "base/mutex.h" 23 #include "base/time_utils.h" 24 #include "base/value_object.h" 25 #include "jni.h" 26 #include "suspend_reason.h" 27 28 #include <bitset> 29 #include <list> 30 #include <vector> 31 32 namespace art { 33 namespace gc { 34 namespace collector { 35 class GarbageCollector; 36 } // namespace collector 37 class GcPauseListener; 38 } // namespace gc 39 class Closure; 40 class RootVisitor; 41 class Thread; 42 class TimingLogger; 43 enum VisitRootFlags : uint8_t; 44 45 class ThreadList { 46 public: 47 static constexpr uint32_t kMaxThreadId = 0xFFFF; 48 static constexpr uint32_t kInvalidThreadId = 0; 49 static constexpr uint32_t kMainThreadId = 1; 50 static constexpr uint64_t kDefaultThreadSuspendTimeout = MsToNs(kIsDebugBuild ? 50000 : 10000); 51 52 explicit ThreadList(uint64_t thread_suspend_timeout_ns); 53 ~ThreadList(); 54 55 void ShutDown(); 56 57 void DumpForSigQuit(std::ostream& os) 58 REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_); 59 // For thread suspend timeout dumps. 60 void Dump(std::ostream& os, bool dump_native_stack = true) 61 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 62 pid_t GetLockOwner(); // For SignalCatcher. 63 64 // Thread suspension support. 65 void ResumeAll() 66 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 67 UNLOCK_FUNCTION(Locks::mutator_lock_); 68 bool Resume(Thread* thread, SuspendReason reason = SuspendReason::kInternal) 69 REQUIRES(!Locks::thread_suspend_count_lock_) WARN_UNUSED; 70 71 // Suspends all threads and gets exclusive access to the mutator lock. 72 // If long_suspend is true, then other threads who try to suspend will never timeout. 73 // long_suspend is currenly used for hprof since large heaps take a long time. 74 void SuspendAll(const char* cause, bool long_suspend = false) 75 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) 76 REQUIRES(!Locks::thread_list_lock_, 77 !Locks::thread_suspend_count_lock_, 78 !Locks::mutator_lock_); 79 80 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success, 81 // else null. The peer is used to identify the thread to avoid races with the thread terminating. 82 // If the thread should be suspended then value of request_suspension should be true otherwise 83 // the routine will wait for a previous suspend request. If the suspension times out then *timeout 84 // is set to true. 85 Thread* SuspendThreadByPeer(jobject peer, 86 bool request_suspension, 87 SuspendReason reason, 88 bool* timed_out) 89 REQUIRES(!Locks::mutator_lock_, 90 !Locks::thread_list_lock_, 91 !Locks::thread_suspend_count_lock_); 92 93 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the 94 // thread on success else null. The thread id is used to identify the thread to avoid races with 95 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected 96 // thread, that may be terminating. If the suspension times out then *timeout is set to true. 97 Thread* SuspendThreadByThreadId(uint32_t thread_id, SuspendReason reason, bool* timed_out) 98 REQUIRES(!Locks::mutator_lock_, 99 !Locks::thread_list_lock_, 100 !Locks::thread_suspend_count_lock_); 101 102 // Find an existing thread (or self) by its thread id (not tid). 103 Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_); 104 105 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside 106 // of the suspend check. Returns how many checkpoints that are expected to run, including for 107 // already suspended threads for b/24191051. Run the callback, if non-null, inside the 108 // thread_list_lock critical section after determining the runnable/suspended states of the 109 // threads. 110 size_t RunCheckpoint(Closure* checkpoint_function, Closure* callback = nullptr) 111 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 112 113 // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are 114 // suspended. This is used to ensure that the threads finish or aren't in the middle of an 115 // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by 116 // decrementing the empty checkpoint barrier count. This works even when the weak ref access is 117 // disabled. Only one concurrent use is currently supported. 118 void RunEmptyCheckpoint() 119 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 120 121 size_t RunCheckpointOnRunnableThreads(Closure* checkpoint_function) 122 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 123 124 // Flip thread roots from from-space refs to to-space refs. Used by 125 // the concurrent copying collector. 126 size_t FlipThreadRoots(Closure* thread_flip_visitor, 127 Closure* flip_callback, 128 gc::collector::GarbageCollector* collector, 129 gc::GcPauseListener* pause_listener) 130 REQUIRES(!Locks::mutator_lock_, 131 !Locks::thread_list_lock_, 132 !Locks::thread_suspend_count_lock_); 133 134 // Suspends all threads 135 void SuspendAllForDebugger() 136 REQUIRES(!Locks::mutator_lock_, 137 !Locks::thread_list_lock_, 138 !Locks::thread_suspend_count_lock_); 139 140 void SuspendSelfForDebugger() 141 REQUIRES(!Locks::thread_suspend_count_lock_); 142 143 // Resume all threads 144 void ResumeAllForDebugger() 145 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 146 147 void UndoDebuggerSuspensions() 148 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 149 150 // Iterates over all the threads. 151 void ForEach(void (*callback)(Thread*, void*), void* context) 152 REQUIRES(Locks::thread_list_lock_); 153 154 // Add/remove current thread from list. 155 void Register(Thread* self) 156 REQUIRES(Locks::runtime_shutdown_lock_) 157 REQUIRES(!Locks::mutator_lock_, 158 !Locks::thread_list_lock_, 159 !Locks::thread_suspend_count_lock_); 160 void Unregister(Thread* self) 161 REQUIRES(!Locks::mutator_lock_, 162 !Locks::thread_list_lock_, 163 !Locks::thread_suspend_count_lock_); 164 165 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) const 166 REQUIRES_SHARED(Locks::mutator_lock_); 167 168 void VisitRootsForSuspendedThreads(RootVisitor* visitor) 169 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 170 REQUIRES_SHARED(Locks::mutator_lock_); 171 172 // Return a copy of the thread list. GetList()173 std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) { 174 return list_; 175 } 176 177 void DumpNativeStacks(std::ostream& os) 178 REQUIRES(!Locks::thread_list_lock_); 179 EmptyCheckpointBarrier()180 Barrier* EmptyCheckpointBarrier() { 181 return empty_checkpoint_barrier_.get(); 182 } 183 184 private: 185 uint32_t AllocThreadId(Thread* self); 186 void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_); 187 188 bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_); 189 bool Contains(pid_t tid) REQUIRES(Locks::thread_list_lock_); 190 size_t RunCheckpoint(Closure* checkpoint_function, bool includeSuspended) 191 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 192 193 void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack) 194 REQUIRES(!Locks::thread_list_lock_); 195 196 void SuspendAllDaemonThreadsForShutdown() 197 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 198 void WaitForOtherNonDaemonThreadsToExit() 199 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 200 201 void SuspendAllInternal(Thread* self, 202 Thread* ignore1, 203 Thread* ignore2 = nullptr, 204 SuspendReason reason = SuspendReason::kInternal) 205 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 206 207 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr) 208 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); 209 210 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_); 211 212 // The actual list of all threads. 213 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_); 214 215 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll. 216 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); 217 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_); 218 219 // Number of threads unregistering, ~ThreadList blocks until this hits 0. 220 int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_); 221 222 // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding 223 // by mutator lock ensures no thread can read when another thread is modifying it. 224 Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_); 225 226 // Whether or not the current thread suspension is long. 227 bool long_suspend_; 228 229 // Whether the shutdown function has been called. This is checked in the destructor. It is an 230 // error to destroy a ThreadList instance without first calling ShutDown(). 231 bool shut_down_; 232 233 // Thread suspension timeout in nanoseconds. 234 const uint64_t thread_suspend_timeout_ns_; 235 236 std::unique_ptr<Barrier> empty_checkpoint_barrier_; 237 238 friend class Thread; 239 240 DISALLOW_COPY_AND_ASSIGN(ThreadList); 241 }; 242 243 // Helper for suspending all threads and getting exclusive access to the mutator lock. 244 class ScopedSuspendAll : public ValueObject { 245 public: 246 explicit ScopedSuspendAll(const char* cause, bool long_suspend = false) 247 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_) 248 REQUIRES(!Locks::thread_list_lock_, 249 !Locks::thread_suspend_count_lock_, 250 !Locks::mutator_lock_); 251 // No REQUIRES(mutator_lock_) since the unlock function already asserts this. 252 ~ScopedSuspendAll() 253 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) 254 UNLOCK_FUNCTION(Locks::mutator_lock_); 255 }; 256 257 } // namespace art 258 259 #endif // ART_RUNTIME_THREAD_LIST_H_ 260