This is an archive of the discontinued LLVM Phabricator instance.

[hwasan] Fix lock contention on thread creation.
ClosedPublic

Authored by eugenis on Apr 12 2021, 3:22 PM.

Details

Summary

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();

}

Diff Detail

Event Timeline

eugenis requested review of this revision.Apr 12 2021, 3:22 PM
eugenis created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2021, 3:22 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka accepted this revision.Apr 14 2021, 12:37 PM
vitalybuka added inline comments.
compiler-rt/lib/hwasan/hwasan_thread_list.h
92

unrelated to the patch but maybe free_list_ and live_list_ could have each own mutex

137

{} redundant in this case

This revision is now accepted and ready to land.Apr 14 2021, 12:37 PM
eugenis updated this revision to Diff 337524.Apr 14 2021, 12:41 PM
eugenis marked an inline comment as done.

addressed comments

compiler-rt/lib/hwasan/hwasan_thread_list.h
92

I've actually tried this and could not see a measurable perf improvement.

vitalybuka added inline comments.Apr 14 2021, 3:01 PM
compiler-rt/lib/hwasan/hwasan_thread_list.h
92

To my taste it would be cleaner that way: different resources rely on different mutexes.
This bug will less likely to happen in the first place :)

eugenis updated this revision to Diff 337575.Apr 14 2021, 3:50 PM

split locks for live and free lists

vitalybuka accepted this revision.Apr 14 2021, 3:56 PM
This revision was automatically updated to reflect the committed changes.