This is an archive of the discontinued LLVM Phabricator instance.

tsan: speed up pthread_setname_np
ClosedPublic

Authored by dvyukov on Nov 15 2021, 10:04 AM.

Details

Summary

pthread_setname_np does linear search over all thread descriptors
to map pthread_t to the thread descriptor. This has O(N^2) complexity
and becomes much worse in the new tsan runtime that keeps all ever
existed threads in the thread registry.
Replace linear search with direct access if pthread_setname_np
is called for the current thread (a very common case).

Diff Detail

Event Timeline

dvyukov requested review of this revision.Nov 15 2021, 10:04 AM
dvyukov created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 15 2021, 10:04 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka accepted this revision.Nov 15 2021, 10:20 AM
vitalybuka added inline comments.
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
2137

This part looks unrelated

This revision is now accepted and ready to land.Nov 15 2021, 10:20 AM
dvyukov updated this revision to Diff 387323.Nov 15 2021, 10:47 AM

removed semi-related part of the diff

dvyukov marked an inline comment as done.Nov 15 2021, 10:47 AM

PTAL

vitalybuka accepted this revision.Nov 15 2021, 12:58 PM
This revision was automatically updated to reflect the committed changes.

The new test is failing on our Arch64 bots:
https://lab.llvm.org/buildbot/#/builders/179/builds/1761

That one is a stage 2 failure but same thing happens on other 1 stage bots and in local builds.

Actual output:

starting transient threads...
starting persistent threads...
ThreadSanitizer: CHECK failed: tsan_rtl.cpp:327 "((addr + size)) <= ((TraceMemEnd()))" (0xf06200e03010, 0xf06200000000) (tid=2492946)
    #0 __tsan::CheckUnwind() /home/david.spickett/llvm-project/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp:357 (bench_threads.cpp.tmp+0x4a1b84)
    #1 __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) /home/david.spickett/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_termination.cpp:86 (bench_threads.cpp.tmp+0x430cc4)
    #2 __tsan::MapThreadTrace(unsigned long, unsigned long, char const*) /home/david.spickett/llvm-project/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp:327 (bench_threads.cpp.tmp+0x4a1b00)
    #3 __tsan::CreateThreadContext(unsigned int) /home/david.spickett/llvm-project/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp:82 (bench_threads.cpp.tmp+0x4a10c4)
    #4 __sanitizer::ThreadRegistry::CreateThread(unsigned long, bool, unsigned int, void*) /home/david.spickett/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_thread_registry.cpp:144 (bench_threads.cpp.tmp+0x42ea9c)
    #5 __tsan::ThreadCreate(__tsan::ThreadState*, unsigned long, unsigned long, bool) /home/david.spickett/llvm-project/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cpp:122 (bench_threads.cpp.tmp+0x4bb000)
    #6 pthread_create /home/david.spickett/llvm-project/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:1042 (bench_threads.cpp.tmp+0x4496bc)
    #7 bench() /home/david.spickett/llvm-project/compiler-rt/test/tsan/bench_threads.cpp:38 (bench_threads.cpp.tmp+0x4c307c)
    #8 main /home/david.spickett/llvm-project/compiler-rt/test/tsan/bench.h:23 (bench_threads.cpp.tmp+0x4c2e20)
    #9 __libc_start_main ??:? (libc.so.6+0x2408c)
    #10 _start ??:? (bench_threads.cpp.tmp+0x41ebc0)