blob: dad896eb5abed7774946e820cb79106690c54082 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_LIST_H_
18#define ART_RUNTIME_THREAD_LIST_H_
Elliott Hughes8daa0922011-09-11 13:46:25 -070019
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070020#include "barrier.h"
Mathieu Chartier70a596d2014-12-17 14:56:47 -080021#include "base/histogram.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Mathieu Chartier4f55e222015-09-04 13:26:21 -070023#include "base/value_object.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070024#include "jni.h"
Alex Light55eccdf2019-10-07 13:51:13 +000025#include "reflective_handle_scope.h"
Alex Light46f93402017-06-29 11:59:50 -070026#include "suspend_reason.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027
28#include <bitset>
29#include <list>
Hiroshi Yamauchia82769c2016-12-02 17:01:51 -080030#include <vector>
Elliott Hughes8daa0922011-09-11 13:46:25 -070031
32namespace art {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080033namespace gc {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080034namespace collector {
35class GarbageCollector;
36} // namespace collector
37class GcPauseListener;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080038} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039class Closure;
Andreas Gampe513061a2017-06-01 09:17:34 -070040class RootVisitor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041class Thread;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070042class TimingLogger;
Andreas Gampe513061a2017-06-01 09:17:34 -070043enum VisitRootFlags : uint8_t;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070044
Elliott Hughes8daa0922011-09-11 13:46:25 -070045class ThreadList {
46 public:
Mathieu Chartier3fceaf52017-01-22 13:33:40 -080047 static constexpr uint32_t kMaxThreadId = 0xFFFF;
48 static constexpr uint32_t kInvalidThreadId = 0;
49 static constexpr uint32_t kMainThreadId = 1;
Andreas Gampe3a8ab362019-05-14 09:46:23 -070050 static constexpr uint64_t kDefaultThreadSuspendTimeout =
51 kIsDebugBuild ? 50'000'000'000ull : 10'000'000'000ull;
Elliott Hughes8daa0922011-09-11 13:46:25 -070052
Mathieu Chartier3fceaf52017-01-22 13:33:40 -080053 explicit ThreadList(uint64_t thread_suspend_timeout_ns);
Elliott Hughes8daa0922011-09-11 13:46:25 -070054 ~ThreadList();
55
Andreas Gampec4bed162017-05-01 13:46:24 -070056 void ShutDown();
57
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 void DumpForSigQuit(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -070059 REQUIRES(!Locks::thread_list_lock_, !Locks::mutator_lock_);
Ian Rogers7b078e82014-09-10 14:44:24 -070060 // For thread suspend timeout dumps.
Nicolas Geoffray6ee49712018-03-30 14:39:05 +000061 void Dump(std::ostream& os, bool dump_native_stack = true)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063 pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes8daa0922011-09-11 13:46:25 -070064
Elliott Hughes8d768a92011-09-14 16:35:25 -070065 // Thread suspension support.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 void ResumeAll()
Mathieu Chartierb56200b2015-10-29 10:41:51 -070067 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
68 UNLOCK_FUNCTION(Locks::mutator_lock_);
Alex Light88fd7202017-06-30 08:31:59 -070069 bool Resume(Thread* thread, SuspendReason reason = SuspendReason::kInternal)
70 REQUIRES(!Locks::thread_suspend_count_lock_) WARN_UNUSED;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071
Roland Levillainaf290312018-02-27 20:02:17 +000072 // Suspends all threads and gets exclusive access to the mutator lock.
Mathieu Chartier4f55e222015-09-04 13:26:21 -070073 // If long_suspend is true, then other threads who try to suspend will never timeout.
74 // long_suspend is currenly used for hprof since large heaps take a long time.
Mathieu Chartierbf44d422015-06-02 11:42:18 -070075 void SuspendAll(const char* cause, bool long_suspend = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
Mathieu Chartier4f55e222015-09-04 13:26:21 -070077 REQUIRES(!Locks::thread_list_lock_,
78 !Locks::thread_suspend_count_lock_,
79 !Locks::mutator_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070080
81 // Suspend a thread using a peer, typically used by the debugger. Returns the thread on success,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 // else null. The peer is used to identify the thread to avoid races with the thread terminating.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083 // If the thread should be suspended then value of request_suspension should be true otherwise
84 // the routine will wait for a previous suspend request. If the suspension times out then *timeout
85 // is set to true.
Alex Light46f93402017-06-29 11:59:50 -070086 Thread* SuspendThreadByPeer(jobject peer,
87 bool request_suspension,
88 SuspendReason reason,
Brian Carlstromba32de42014-08-27 23:43:46 -070089 bool* timed_out)
Mathieu Chartierb56200b2015-10-29 10:41:51 -070090 REQUIRES(!Locks::mutator_lock_,
91 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -070092 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -070093
94 // Suspend a thread using its thread id, typically used by lock/monitor inflation. Returns the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070095 // thread on success else null. The thread id is used to identify the thread to avoid races with
Ian Rogersd9c4fc92013-10-01 19:45:43 -070096 // the thread terminating. Note that as thread ids are recycled this may not suspend the expected
97 // thread, that may be terminating. If the suspension times out then *timeout is set to true.
Alex Light46f93402017-06-29 11:59:50 -070098 Thread* SuspendThreadByThreadId(uint32_t thread_id, SuspendReason reason, bool* timed_out)
Mathieu Chartierb56200b2015-10-29 10:41:51 -070099 REQUIRES(!Locks::mutator_lock_,
100 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700101 !Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700102
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700103 // Find an existing thread (or self) by its thread id (not tid).
104 Thread* FindThreadByThreadId(uint32_t thread_id) REQUIRES(Locks::thread_list_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700105
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700106 // Run a checkpoint on threads, running threads are not suspended but run the checkpoint inside
Mathieu Chartier10d25082015-10-28 18:36:09 -0700107 // of the suspend check. Returns how many checkpoints that are expected to run, including for
Hiroshi Yamauchifebd0cf2016-09-14 19:31:25 -0700108 // already suspended threads for b/24191051. Run the callback, if non-null, inside the
109 // thread_list_lock critical section after determining the runnable/suspended states of the
110 // threads.
111 size_t RunCheckpoint(Closure* checkpoint_function, Closure* callback = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700113
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700114 // Run an empty checkpoint on threads. Wait until threads pass the next suspend point or are
115 // suspended. This is used to ensure that the threads finish or aren't in the middle of an
116 // in-flight mutator heap access (eg. a read barrier.) Runnable threads will respond by
117 // decrementing the empty checkpoint barrier count. This works even when the weak ref access is
118 // disabled. Only one concurrent use is currently supported.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800119 void RunEmptyCheckpoint()
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700120 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
121
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800122 // Flip thread roots from from-space refs to to-space refs. Used by
123 // the concurrent copying collector.
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700124 size_t FlipThreadRoots(Closure* thread_flip_visitor,
125 Closure* flip_callback,
Andreas Gampe6e644452017-05-09 16:30:27 -0700126 gc::collector::GarbageCollector* collector,
127 gc::GcPauseListener* pause_listener)
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700128 REQUIRES(!Locks::mutator_lock_,
129 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 !Locks::thread_suspend_count_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800131
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 // Suspends all threads
133 void SuspendAllForDebugger()
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700134 REQUIRES(!Locks::mutator_lock_,
135 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137
138 void SuspendSelfForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 REQUIRES(!Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140
Sebastien Hertz253fa552014-10-14 17:27:15 +0200141 // Resume all threads
142 void ResumeAllForDebugger()
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz253fa552014-10-14 17:27:15 +0200144
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 void UndoDebuggerSuspensions()
Mathieu Chartier90443472015-07-16 20:32:27 -0700146 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700147
Elliott Hughesf8349362012-06-18 15:00:06 -0700148 // Iterates over all the threads.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 void ForEach(void (*callback)(Thread*, void*), void* context)
Mathieu Chartier90443472015-07-16 20:32:27 -0700150 REQUIRES(Locks::thread_list_lock_);
Elliott Hughes47fce012011-10-25 18:37:19 -0700151
Alex Lightc14ec8f2019-07-18 16:08:41 -0700152 template<typename CallBack>
153 void ForEach(CallBack cb) REQUIRES(Locks::thread_list_lock_) {
154 ForEach([](Thread* t, void* ctx) REQUIRES(Locks::thread_list_lock_) {
155 (*reinterpret_cast<CallBack*>(ctx))(t);
156 }, &cb);
157 }
158
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 // Add/remove current thread from list.
160 void Register(Thread* self)
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700161 REQUIRES(Locks::runtime_shutdown_lock_)
162 REQUIRES(!Locks::mutator_lock_,
163 !Locks::thread_list_lock_,
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 !Locks::thread_suspend_count_lock_);
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700165 void Unregister(Thread* self)
166 REQUIRES(!Locks::mutator_lock_,
167 !Locks::thread_list_lock_,
168 !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700169
Andreas Gampe585da952016-12-02 14:52:29 -0800170 void VisitRoots(RootVisitor* visitor, VisitRootFlags flags) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700172
Mathieu Chartierf8a86b92016-06-14 17:08:47 -0700173 void VisitRootsForSuspendedThreads(RootVisitor* visitor)
174 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierf8a86b92016-06-14 17:08:47 -0700176
Alex Light55eccdf2019-10-07 13:51:13 +0000177 void VisitReflectiveTargets(ReflectiveValueVisitor* visitor) const REQUIRES(Locks::mutator_lock_);
178
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179 // Return a copy of the thread list.
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 std::list<Thread*> GetList() REQUIRES(Locks::thread_list_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 return list_;
182 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700183
Mathieu Chartier590fee92013-09-13 13:46:47 -0700184 void DumpNativeStacks(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -0700185 REQUIRES(!Locks::thread_list_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700186
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700187 Barrier* EmptyCheckpointBarrier() {
188 return empty_checkpoint_barrier_.get();
189 }
190
Elliott Hughes8daa0922011-09-11 13:46:25 -0700191 private:
Ian Rogerscfaa4552012-11-26 21:00:08 -0800192 uint32_t AllocThreadId(Thread* self);
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 void ReleaseThreadId(Thread* self, uint32_t id) REQUIRES(!Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700194
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 bool Contains(Thread* thread) REQUIRES(Locks::thread_list_lock_);
196 bool Contains(pid_t tid) REQUIRES(Locks::thread_list_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800197 size_t RunCheckpoint(Closure* checkpoint_function, bool includeSuspended)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700199
Nicolas Geoffrayd3c59652016-03-17 09:35:04 +0000200 void DumpUnattachedThreads(std::ostream& os, bool dump_native_stack)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 REQUIRES(!Locks::thread_list_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700202
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800203 void SuspendAllDaemonThreadsForShutdown()
Mathieu Chartier90443472015-07-16 20:32:27 -0700204 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 void WaitForOtherNonDaemonThreadsToExit()
Mathieu Chartier90443472015-07-16 20:32:27 -0700206 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700207
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700208 void SuspendAllInternal(Thread* self,
209 Thread* ignore1,
210 Thread* ignore2 = nullptr,
Alex Light46f93402017-06-29 11:59:50 -0700211 SuspendReason reason = SuspendReason::kInternal)
Mathieu Chartier90443472015-07-16 20:32:27 -0700212 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Yu Lieac44242015-06-29 10:50:03 +0800213
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700214 void AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2 = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700215 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700216
Chao-ying Fu9e369312014-05-21 11:20:52 -0700217 std::bitset<kMaxThreadId> allocated_ids_ GUARDED_BY(Locks::allocated_thread_ids_lock_);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700218
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 // The actual list of all threads.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700220 std::list<Thread*> list_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700221
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 // Ongoing suspend all requests, used to ensure threads added to list_ respect SuspendAll.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700223 int suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
224 int debug_suspend_all_count_ GUARDED_BY(Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700225
Mathieu Chartier91e56692015-03-03 13:51:04 -0800226 // Number of threads unregistering, ~ThreadList blocks until this hits 0.
227 int unregistering_count_ GUARDED_BY(Locks::thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700228
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800229 // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
230 // by mutator lock ensures no thread can read when another thread is modifying it.
231 Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
232
Mathieu Chartierbf44d422015-06-02 11:42:18 -0700233 // Whether or not the current thread suspension is long.
234 bool long_suspend_;
235
Andreas Gampec4bed162017-05-01 13:46:24 -0700236 // Whether the shutdown function has been called. This is checked in the destructor. It is an
237 // error to destroy a ThreadList instance without first calling ShutDown().
238 bool shut_down_;
239
Mathieu Chartier3fceaf52017-01-22 13:33:40 -0800240 // Thread suspension timeout in nanoseconds.
241 const uint64_t thread_suspend_timeout_ns_;
242
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700243 std::unique_ptr<Barrier> empty_checkpoint_barrier_;
244
Elliott Hughes8daa0922011-09-11 13:46:25 -0700245 friend class Thread;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700246
247 DISALLOW_COPY_AND_ASSIGN(ThreadList);
248};
249
Roland Levillainaf290312018-02-27 20:02:17 +0000250// Helper for suspending all threads and getting exclusive access to the mutator lock.
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700251class ScopedSuspendAll : public ValueObject {
252 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700253 explicit ScopedSuspendAll(const char* cause, bool long_suspend = false)
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700254 EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_)
255 REQUIRES(!Locks::thread_list_lock_,
256 !Locks::thread_suspend_count_lock_,
257 !Locks::mutator_lock_);
258 // No REQUIRES(mutator_lock_) since the unlock function already asserts this.
259 ~ScopedSuspendAll()
Mathieu Chartierb56200b2015-10-29 10:41:51 -0700260 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
261 UNLOCK_FUNCTION(Locks::mutator_lock_);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700262};
263
Elliott Hughes8daa0922011-09-11 13:46:25 -0700264} // namespace art
265
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700266#endif // ART_RUNTIME_THREAD_LIST_H_