sanitizer_thread_registry.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===-- sanitizer_thread_registry.cc --------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between sanitizer tools.
  9. //
  10. // General thread bookkeeping functionality.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_thread_registry.h"
  13. namespace __sanitizer {
  14. ThreadContextBase::ThreadContextBase(u32 tid)
  15. : tid(tid), unique_id(0), reuse_count(), os_id(0), user_id(0),
  16. status(ThreadStatusInvalid),
  17. detached(false), parent_tid(0), next(0) {
  18. name[0] = '\0';
  19. }
  20. ThreadContextBase::~ThreadContextBase() {
  21. // ThreadContextBase should never be deleted.
  22. CHECK(0);
  23. }
  24. void ThreadContextBase::SetName(const char *new_name) {
  25. name[0] = '\0';
  26. if (new_name) {
  27. internal_strncpy(name, new_name, sizeof(name));
  28. name[sizeof(name) - 1] = '\0';
  29. }
  30. }
  31. void ThreadContextBase::SetDead() {
  32. CHECK(status == ThreadStatusRunning ||
  33. status == ThreadStatusFinished);
  34. status = ThreadStatusDead;
  35. user_id = 0;
  36. OnDead();
  37. }
  38. void ThreadContextBase::SetJoined(void *arg) {
  39. // FIXME(dvyukov): print message and continue (it's user error).
  40. CHECK_EQ(false, detached);
  41. CHECK_EQ(ThreadStatusFinished, status);
  42. status = ThreadStatusDead;
  43. user_id = 0;
  44. OnJoined(arg);
  45. }
  46. void ThreadContextBase::SetFinished() {
  47. if (!detached)
  48. status = ThreadStatusFinished;
  49. OnFinished();
  50. }
  51. void ThreadContextBase::SetStarted(uptr _os_id, void *arg) {
  52. status = ThreadStatusRunning;
  53. os_id = _os_id;
  54. OnStarted(arg);
  55. }
  56. void ThreadContextBase::SetCreated(uptr _user_id, u64 _unique_id,
  57. bool _detached, u32 _parent_tid, void *arg) {
  58. status = ThreadStatusCreated;
  59. user_id = _user_id;
  60. unique_id = _unique_id;
  61. detached = _detached;
  62. // Parent tid makes no sense for the main thread.
  63. if (tid != 0)
  64. parent_tid = _parent_tid;
  65. OnCreated(arg);
  66. }
  67. void ThreadContextBase::Reset() {
  68. status = ThreadStatusInvalid;
  69. SetName(0);
  70. OnReset();
  71. }
  72. // ThreadRegistry implementation.
  73. const u32 ThreadRegistry::kUnknownTid = ~0U;
  74. ThreadRegistry::ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
  75. u32 thread_quarantine_size, u32 max_reuse)
  76. : context_factory_(factory),
  77. max_threads_(max_threads),
  78. thread_quarantine_size_(thread_quarantine_size),
  79. max_reuse_(max_reuse),
  80. mtx_(),
  81. n_contexts_(0),
  82. total_threads_(0),
  83. alive_threads_(0),
  84. max_alive_threads_(0),
  85. running_threads_(0) {
  86. threads_ = (ThreadContextBase **)MmapOrDie(max_threads_ * sizeof(threads_[0]),
  87. "ThreadRegistry");
  88. dead_threads_.clear();
  89. invalid_threads_.clear();
  90. }
  91. void ThreadRegistry::GetNumberOfThreads(uptr *total, uptr *running,
  92. uptr *alive) {
  93. BlockingMutexLock l(&mtx_);
  94. if (total) *total = n_contexts_;
  95. if (running) *running = running_threads_;
  96. if (alive) *alive = alive_threads_;
  97. }
  98. uptr ThreadRegistry::GetMaxAliveThreads() {
  99. BlockingMutexLock l(&mtx_);
  100. return max_alive_threads_;
  101. }
  102. u32 ThreadRegistry::CreateThread(uptr user_id, bool detached, u32 parent_tid,
  103. void *arg) {
  104. BlockingMutexLock l(&mtx_);
  105. u32 tid = kUnknownTid;
  106. ThreadContextBase *tctx = QuarantinePop();
  107. if (tctx) {
  108. tid = tctx->tid;
  109. } else if (n_contexts_ < max_threads_) {
  110. // Allocate new thread context and tid.
  111. tid = n_contexts_++;
  112. tctx = context_factory_(tid);
  113. threads_[tid] = tctx;
  114. } else {
  115. #ifndef SANITIZER_GO
  116. Report("%s: Thread limit (%u threads) exceeded. Dying.\n",
  117. SanitizerToolName, max_threads_);
  118. #else
  119. Printf("race: limit on %u simultaneously alive goroutines is exceeded,"
  120. " dying\n", max_threads_);
  121. #endif
  122. Die();
  123. }
  124. CHECK_NE(tctx, 0);
  125. CHECK_NE(tid, kUnknownTid);
  126. CHECK_LT(tid, max_threads_);
  127. CHECK_EQ(tctx->status, ThreadStatusInvalid);
  128. alive_threads_++;
  129. if (max_alive_threads_ < alive_threads_) {
  130. max_alive_threads_++;
  131. CHECK_EQ(alive_threads_, max_alive_threads_);
  132. }
  133. tctx->SetCreated(user_id, total_threads_++, detached,
  134. parent_tid, arg);
  135. return tid;
  136. }
  137. void ThreadRegistry::RunCallbackForEachThreadLocked(ThreadCallback cb,
  138. void *arg) {
  139. CheckLocked();
  140. for (u32 tid = 0; tid < n_contexts_; tid++) {
  141. ThreadContextBase *tctx = threads_[tid];
  142. if (tctx == 0)
  143. continue;
  144. cb(tctx, arg);
  145. }
  146. }
  147. u32 ThreadRegistry::FindThread(FindThreadCallback cb, void *arg) {
  148. BlockingMutexLock l(&mtx_);
  149. for (u32 tid = 0; tid < n_contexts_; tid++) {
  150. ThreadContextBase *tctx = threads_[tid];
  151. if (tctx != 0 && cb(tctx, arg))
  152. return tctx->tid;
  153. }
  154. return kUnknownTid;
  155. }
  156. ThreadContextBase *
  157. ThreadRegistry::FindThreadContextLocked(FindThreadCallback cb, void *arg) {
  158. CheckLocked();
  159. for (u32 tid = 0; tid < n_contexts_; tid++) {
  160. ThreadContextBase *tctx = threads_[tid];
  161. if (tctx != 0 && cb(tctx, arg))
  162. return tctx;
  163. }
  164. return 0;
  165. }
  166. static bool FindThreadContextByOsIdCallback(ThreadContextBase *tctx,
  167. void *arg) {
  168. return (tctx->os_id == (uptr)arg && tctx->status != ThreadStatusInvalid &&
  169. tctx->status != ThreadStatusDead);
  170. }
  171. ThreadContextBase *ThreadRegistry::FindThreadContextByOsIDLocked(uptr os_id) {
  172. return FindThreadContextLocked(FindThreadContextByOsIdCallback,
  173. (void *)os_id);
  174. }
  175. void ThreadRegistry::SetThreadName(u32 tid, const char *name) {
  176. BlockingMutexLock l(&mtx_);
  177. CHECK_LT(tid, n_contexts_);
  178. ThreadContextBase *tctx = threads_[tid];
  179. CHECK_NE(tctx, 0);
  180. CHECK_EQ(ThreadStatusRunning, tctx->status);
  181. tctx->SetName(name);
  182. }
  183. void ThreadRegistry::SetThreadNameByUserId(uptr user_id, const char *name) {
  184. BlockingMutexLock l(&mtx_);
  185. for (u32 tid = 0; tid < n_contexts_; tid++) {
  186. ThreadContextBase *tctx = threads_[tid];
  187. if (tctx != 0 && tctx->user_id == user_id &&
  188. tctx->status != ThreadStatusInvalid) {
  189. tctx->SetName(name);
  190. return;
  191. }
  192. }
  193. }
  194. void ThreadRegistry::DetachThread(u32 tid, void *arg) {
  195. BlockingMutexLock l(&mtx_);
  196. CHECK_LT(tid, n_contexts_);
  197. ThreadContextBase *tctx = threads_[tid];
  198. CHECK_NE(tctx, 0);
  199. if (tctx->status == ThreadStatusInvalid) {
  200. Report("%s: Detach of non-existent thread\n", SanitizerToolName);
  201. return;
  202. }
  203. tctx->OnDetached(arg);
  204. if (tctx->status == ThreadStatusFinished) {
  205. tctx->SetDead();
  206. QuarantinePush(tctx);
  207. } else {
  208. tctx->detached = true;
  209. }
  210. }
  211. void ThreadRegistry::JoinThread(u32 tid, void *arg) {
  212. BlockingMutexLock l(&mtx_);
  213. CHECK_LT(tid, n_contexts_);
  214. ThreadContextBase *tctx = threads_[tid];
  215. CHECK_NE(tctx, 0);
  216. if (tctx->status == ThreadStatusInvalid) {
  217. Report("%s: Join of non-existent thread\n", SanitizerToolName);
  218. return;
  219. }
  220. tctx->SetJoined(arg);
  221. QuarantinePush(tctx);
  222. }
  223. void ThreadRegistry::FinishThread(u32 tid) {
  224. BlockingMutexLock l(&mtx_);
  225. CHECK_GT(alive_threads_, 0);
  226. alive_threads_--;
  227. CHECK_GT(running_threads_, 0);
  228. running_threads_--;
  229. CHECK_LT(tid, n_contexts_);
  230. ThreadContextBase *tctx = threads_[tid];
  231. CHECK_NE(tctx, 0);
  232. CHECK_EQ(ThreadStatusRunning, tctx->status);
  233. tctx->SetFinished();
  234. if (tctx->detached) {
  235. tctx->SetDead();
  236. QuarantinePush(tctx);
  237. }
  238. }
  239. void ThreadRegistry::StartThread(u32 tid, uptr os_id, void *arg) {
  240. BlockingMutexLock l(&mtx_);
  241. running_threads_++;
  242. CHECK_LT(tid, n_contexts_);
  243. ThreadContextBase *tctx = threads_[tid];
  244. CHECK_NE(tctx, 0);
  245. CHECK_EQ(ThreadStatusCreated, tctx->status);
  246. tctx->SetStarted(os_id, arg);
  247. }
  248. void ThreadRegistry::QuarantinePush(ThreadContextBase *tctx) {
  249. dead_threads_.push_back(tctx);
  250. if (dead_threads_.size() <= thread_quarantine_size_)
  251. return;
  252. tctx = dead_threads_.front();
  253. dead_threads_.pop_front();
  254. CHECK_EQ(tctx->status, ThreadStatusDead);
  255. tctx->Reset();
  256. tctx->reuse_count++;
  257. if (max_reuse_ > 0 && tctx->reuse_count >= max_reuse_)
  258. return;
  259. invalid_threads_.push_back(tctx);
  260. }
  261. ThreadContextBase *ThreadRegistry::QuarantinePop() {
  262. if (invalid_threads_.size() == 0)
  263. return 0;
  264. ThreadContextBase *tctx = invalid_threads_.front();
  265. invalid_threads_.pop_front();
  266. return tctx;
  267. }
  268. } // namespace __sanitizer