[PATCH] D100348: [hwasan] Fix lock contention on thread creation.
Evgenii Stepanov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 12 15:22:44 PDT 2021
eugenis created this revision.
eugenis added a reviewer: vitalybuka.
eugenis requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.
Do not hold the free/live thread list lock longer than necessary.
This change speeds up the following benchmark 10x.
constexpr int kTopThreads = 50;
constexpr int kChildThreads = 20;
constexpr int kChildIterations = 8;
void Thread() {
for (int i = 0; i < kChildIterations; ++i) {
std::vector<std::thread> threads;
for (int i = 0; i < kChildThreads; ++i)
threads.emplace_back([](){});
for (auto& t : threads)
t.join();
}
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < kTopThreads; ++i)
threads.emplace_back(Thread);
for (auto& t : threads)
t.join();
}
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D100348
Files:
compiler-rt/lib/hwasan/hwasan_thread_list.h
Index: compiler-rt/lib/hwasan/hwasan_thread_list.h
===================================================================
--- compiler-rt/lib/hwasan/hwasan_thread_list.h
+++ compiler-rt/lib/hwasan/hwasan_thread_list.h
@@ -86,17 +86,22 @@
}
Thread *CreateCurrentThread() {
- Thread *t;
+ Thread *t = nullptr;
{
SpinMutexLock l(&list_mutex_);
if (!free_list_.empty()) {
t = free_list_.back();
free_list_.pop_back();
- uptr start = (uptr)t - ring_buffer_size_;
- internal_memset((void *)start, 0, ring_buffer_size_ + sizeof(Thread));
- } else {
- t = AllocThread();
}
+ }
+ if (t) {
+ uptr start = (uptr)t - ring_buffer_size_;
+ internal_memset((void *)start, 0, ring_buffer_size_ + sizeof(Thread));
+ } else {
+ t = AllocThread();
+ }
+ {
+ SpinMutexLock l(&list_mutex_);
live_list_.push_back(t);
}
t->Init((uptr)t - ring_buffer_size_, ring_buffer_size_);
@@ -124,10 +129,12 @@
void ReleaseThread(Thread *t) {
RemoveThreadStats(t);
t->Destroy();
- SpinMutexLock l(&list_mutex_);
- RemoveThreadFromLiveList(t);
- free_list_.push_back(t);
DontNeedThread(t);
+ {
+ SpinMutexLock l(&list_mutex_);
+ RemoveThreadFromLiveList(t);
+ free_list_.push_back(t);
+ }
}
Thread *GetThreadByBufferAddress(uptr p) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100348.336979.patch
Type: text/x-patch
Size: 1387 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210412/ed70b710/attachment.bin>
More information about the llvm-commits
mailing list