• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <bitset>
21 #include <list>
22 #include <vector>
23 
24 #include "barrier.h"
25 #include "base/histogram.h"
26 #include "base/mutex.h"
27 #include "base/macros.h"
28 #include "base/value_object.h"
29 #include "jni.h"
30 #include "reflective_handle_scope.h"
31 #include "suspend_reason.h"
32 #include "thread_state.h"
33 
34 namespace art HIDDEN {
35 namespace gc {
36 namespace collector {
37 class GarbageCollector;
38 }  // namespace collector
39 class GcPauseListener;
40 }  // namespace gc
41 class Closure;
42 class IsMarkedVisitor;
43 class RootVisitor;
44 class Thread;
45 class TimingLogger;
46 enum VisitRootFlags : uint8_t;
47 
48 class ThreadList {
49  public:
50   static constexpr uint32_t kMaxThreadId = 0xFFFF;
51   static constexpr uint32_t kInvalidThreadId = 0;
52   static constexpr uint32_t kMainThreadId = 1;
53   static constexpr uint64_t kDefaultThreadSuspendTimeout =
54       kIsDebugBuild ? 2'000'000'000ull : 4'000'000'000ull;
55   // We fail more aggressively in debug builds to catch potential issues early.
56   // The number of times we may retry when we find ourselves in a suspend-unfriendly state.
57   static constexpr int kMaxSuspendRetries = kIsDebugBuild ? 500 : 5000;
58   static constexpr useconds_t kThreadSuspendSleepUs = 100;
59 
60   explicit ThreadList(uint64_t thread_suspend_timeout_ns);
61   ~ThreadList();
62 
63   void ShutDown();
64 
65   void DumpForSigQuit(std::ostream& os)
66       REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_);
67   // For thread suspend timeout dumps.
68   EXPORT void Dump(std::ostream& os, bool dump_native_stack = true)
69       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
70   pid_t GetLockOwner();  // For SignalCatcher.
71 
72   // Thread suspension support.
73   EXPORT void ResumeAll()
74       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
75       UNLOCK_FUNCTION(Locks::mutator_lock_);
76   EXPORT bool Resume(Thread* thread, SuspendReason reason = SuspendReason::kInternal)
77       REQUIRES(!Locks::thread_suspend_count_lock_) WARN_UNUSED;
78 
79   // Suspends all other threads and gets exclusive access to the mutator lock.
80   // If long_suspend is true, then other threads who try to suspend will never timeout.
81   // long_suspend is currenly used for hprof since large heaps take a long time.
82   EXPORT void SuspendAll(const char* cause, bool long_suspend = false)
83       EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
84       REQUIRES(!Locks::thread_list_lock_,
85                !Locks::thread_suspend_count_lock_,
86                !Locks::mutator_lock_);
87 
88   // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
89   // else null. The peer is used to identify the thread to avoid races with the thread terminating.
90   EXPORT Thread* SuspendThreadByPeer(jobject peer, SuspendReason reason)
91       REQUIRES(!Locks::mutator_lock_,
92                !Locks::thread_list_lock_,
93                !Locks::thread_suspend_count_lock_);
94 
95   // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
96   // thread on success else null. The thread id is used to identify the thread to avoid races with
97   // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
98   // thread, that may be terminating. 'attempt_of_4' is zero if this is the only
99   // attempt, or 1..4 to try 4 times with fractional timeouts.
100   // TODO: Reconsider the use of thread_id, now that we have ThreadExitFlag.
101   Thread* SuspendThreadByThreadId(uint32_t thread_id, SuspendReason reason, int attempt_of_4 = 0)
102       REQUIRES(!Locks::mutator_lock_,
103                !Locks::thread_list_lock_,
104                !Locks::thread_suspend_count_lock_);
105 
106   // Find an existing thread (or self) by its thread id (not tid).
107   EXPORT Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_);
108 
109   // Find an existing thread (or self) by its tid (not thread id).
110   Thread* FindThreadByTid(int tid) REQUIRES(Locks::thread_list_lock_);
111 
112   // Does the thread list still contain the given thread, or one at the same address?
113   // Used by Monitor to provide (mostly accurate) debugging information.
114   bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_);
115 
116   // Run a checkpoint on all threads. Return the total number of threads for which the checkpoint
117   // function has been or will be called.
118   // Running threads are not suspended but run the checkpoint inside of the suspend check. The
119   // return value includes already suspended threads for b/24191051. Runs or requests the
120   // callback, if non-null, inside the thread_list_lock critical section after determining the
121   // runnable/suspended states of the threads. Does not wait for completion of the checkpoint
122   // function in running threads. If the caller holds the mutator lock, then all instances of the
123   // checkpoint function are run with the mutator lock. If the caller does not hold the mutator
124   // lock (see mutator_gc_coord.md) then, since the checkpoint code may not acquire or release the
125   // mutator lock, the checkpoint will have no way to access Java data.
126   // TODO: Is it possible to just require the mutator lock here?
127   EXPORT size_t RunCheckpoint(Closure* checkpoint_function,
128                        Closure* callback = nullptr,
129                        bool allow_lock_checking = true)
130       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
131 
132   // Convenience version of the above to disable lock checking inside Run function. Hopefully this
133   // and the third parameter above will eventually disappear.
134   size_t RunCheckpointUnchecked(Closure* checkpoint_function, Closure* callback = nullptr)
135       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_) {
136     return RunCheckpoint(checkpoint_function, callback, false);
137   }
138 
139   // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are
140   // suspended. This is used to ensure that the threads finish or aren't in the middle of an
141   // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by
142   // decrementing the empty checkpoint barrier count. This works even when the weak ref access is
143   // disabled. Only one concurrent use is currently supported.
144   void RunEmptyCheckpoint()
145       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
146 
147   // Used to flip thread roots from from-space refs to to-space refs. Used only by the concurrent
148   // moving collectors during a GC, and hence cannot be called from multiple threads concurrently.
149   //
150   // Briefly suspends all threads to atomically install a checkpoint-like thread_flip_visitor
151   // function to be run on each thread. Run flip_callback while threads are suspended.
152   // Thread_flip_visitors are run by each thread before it becomes runnable, or by us. We do not
153   // return until all thread_flip_visitors have been run.
154   void FlipThreadRoots(Closure* thread_flip_visitor,
155                        Closure* flip_callback,
156                        gc::collector::GarbageCollector* collector,
157                        gc::GcPauseListener* pause_listener)
158       REQUIRES(!Locks::mutator_lock_,
159                !Locks::thread_list_lock_,
160                !Locks::thread_suspend_count_lock_);
161 
162   // Iterates over all the threads.
163   EXPORT void ForEach(void (*callback)(Thread*, void*), void* context)
164       REQUIRES(Locks::thread_list_lock_);
165 
166   template<typename CallBack>
ForEach(CallBack cb)167   void ForEach(CallBack cb) REQUIRES(Locks::thread_list_lock_) {
168     ForEach([](Thread* t, void* ctx) REQUIRES(Locks::thread_list_lock_) {
169       (*reinterpret_cast<CallBack*>(ctx))(t);
170     }, &cb);
171   }
172 
173   // Add/remove current thread from list.
174   void Register(Thread* self)
175       REQUIRES(Locks::runtime_shutdown_lock_)
176       REQUIRES(!Locks::mutator_lock_,
177                !Locks::thread_list_lock_,
178                !Locks::thread_suspend_count_lock_);
179   void Unregister(Thread* self, bool should_run_callbacks)
180       REQUIRES(!Locks::mutator_lock_,
181                !Locks::thread_list_lock_,
182                !Locks::thread_suspend_count_lock_);
183 
184   // Wait until there are no Unregister() requests in flight. Only makes sense when we know that
185   // no new calls can be made. e.g. because we're the last thread.
186   void WaitForUnregisterToComplete(Thread* self) REQUIRES(Locks::thread_list_lock_);
187 
188   void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) const
189       REQUIRES_SHARED(Locks::mutator_lock_);
190 
191   void VisitRootsForSuspendedThreads(RootVisitor* visitor)
192       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
193       REQUIRES_SHARED(Locks::mutator_lock_);
194 
195   void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) const REQUIRES(Locks::mutator_lock_);
196 
197   EXPORT void SweepInterpreterCaches(IsMarkedVisitor* visitor) const
198       REQUIRES(Locks::mutator_lock_, !Locks::thread_list_lock_);
199 
200   // Return a copy of the thread list.
GetList()201   std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) {
202     return list_;
203   }
204 
Size()205   size_t Size() REQUIRES(Locks::thread_list_lock_) { return list_.size(); }
206 
CheckOnly1Thread(Thread * self)207   void CheckOnly1Thread(Thread* self) REQUIRES(!Locks::thread_list_lock_) {
208     MutexLock mu(self, *Locks::thread_list_lock_);
209     CHECK_EQ(Size(), 1u);
210   }
211 
212   void DumpNativeStacks(std::ostream& os)
213       REQUIRES(!Locks::thread_list_lock_);
214 
EmptyCheckpointBarrier()215   Barrier* EmptyCheckpointBarrier() {
216     return empty_checkpoint_barrier_.get();
217   }
218 
219   void WaitForOtherNonDaemonThreadsToExit(bool check_no_birth = true)
220       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
221                !Locks::mutator_lock_);
222 
223   // Wait for suspend barrier to reach zero. Return a string possibly containing diagnostic
224   // information on timeout, nothing on success.  The argument t specifies a thread to monitor for
225   // the diagnostic information. If 0 is passed, we return an empty string on timeout.  Normally
226   // the caller does not hold the mutator lock. See the comment at the call in
227   // RequestSynchronousCheckpoint for the only exception.
228   std::optional<std::string> WaitForSuspendBarrier(AtomicInteger* barrier,
229                                                    pid_t t = 0,
230                                                    int attempt_of_4 = 0)
231       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
232 
233  private:
234   uint32_t AllocThreadId(Thread* self);
235   void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_);
236 
237   void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack)
238       REQUIRES(!Locks::thread_list_lock_);
239 
240   void SuspendAllDaemonThreadsForShutdown()
241       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
242 
243   void ResumeAllInternal(Thread* self)
244       REQUIRES(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_)
245           UNLOCK_FUNCTION(Locks::mutator_lock_);
246 
247   // Helper to actually suspend a single thread. This is called with thread_list_lock_ held and
248   // the caller guarantees that *thread is valid until that is released.  We "release the mutator
249   // lock", by switching to self_state.  'attempt_of_4' is 0 if we only attempt once, and 1..4 if
250   // we are going to try 4 times with a quarter of the full timeout. 'func_name' is used only to
251   // identify ourselves for logging.
252   bool SuspendThread(Thread* self,
253                      Thread* thread,
254                      SuspendReason reason,
255                      ThreadState self_state,
256                      const char* func_name,
257                      int attempt_of_4) RELEASE(Locks::thread_list_lock_)
258       RELEASE_SHARED(Locks::mutator_lock_);
259 
260   void SuspendAllInternal(Thread* self, SuspendReason reason = SuspendReason::kInternal)
261       REQUIRES(!Locks::thread_list_lock_,
262                !Locks::thread_suspend_count_lock_,
263                !Locks::mutator_lock_);
264 
265   void AssertOtherThreadsAreSuspended(Thread* self)
266       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
267 
268   std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
269 
270   // The actual list of all threads.
271   std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
272 
273   // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll, and
274   // to ensure that only one SuspendAll ot FlipThreadRoots call is active at a time.  The value is
275   // always either 0 or 1. Thread_suspend_count_lock must be held continuously while these two
276   // functions modify suspend counts of all other threads and modify suspend_all_count_ .
277   int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
278 
279   // Number of threads unregistering, ~ThreadList blocks until this hits 0.
280   int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_);
281 
282   // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
283   // by mutator lock ensures no thread can read when another thread is modifying it.
284   Histogram<uint64_t> suspend_all_histogram_ GUARDED_BY(Locks::mutator_lock_);
285 
286   // Whether or not the current thread suspension is long.
287   bool long_suspend_;
288 
289   // Whether the shutdown function has been called. This is checked in the destructor. It is an
290   // error to destroy a ThreadList instance without first calling ShutDown().
291   bool shut_down_;
292 
293   // Thread suspension timeout in nanoseconds.
294   const uint64_t thread_suspend_timeout_ns_;
295 
296   std::unique_ptr<Barrier> empty_checkpoint_barrier_;
297 
298   friend class Thread;
299 
300   friend class Mutex;
301   friend class BaseMutex;
302 
303   DISALLOW_COPY_AND_ASSIGN(ThreadList);
304 };
305 
306 // Helper for suspending all threads and getting exclusive access to the mutator lock.
307 class ScopedSuspendAll : public ValueObject {
308  public:
309   EXPORT explicit ScopedSuspendAll(const char* cause, bool long_suspend = false)
310      EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
311      REQUIRES(!Locks::thread_list_lock_,
312               !Locks::thread_suspend_count_lock_,
313               !Locks::mutator_lock_);
314   // No REQUIRES(mutator_lock_) since the unlock function already asserts this.
315   EXPORT ~ScopedSuspendAll()
316       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
317       UNLOCK_FUNCTION(Locks::mutator_lock_);
318 };
319 
320 }  // namespace art
321 
322 #endif  // ART_RUNTIME_THREAD_LIST_H_
323