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