ThreadSanitizer(TSan)主要用于测试data races(数据竞争)。 # 概要 - 使用TSan测试时,默认情况下,性能损失很大。 - 每发现一个data race,TSan需要写log,TSan内部使用ReportRace函数输出log。参考:[ReportRace](https://code.woboq.org/gcc/libsanitizer/tsan/tsan_rtl_report.cc.html#__tsan::ReportRace) - ReportRace内部是有锁的,所以,当data race很多时,性能下降很多。 - 可以使用TSan的report_bugs参数关闭ReportRace,减少对性能的影响。 # 源码 TSan源码: [https://code.woboq.org/gcc/libsanitizer/tsan/](https://code.woboq.org/gcc/libsanitizer/tsan/) # 报错 ## failed to restore the stack 有时ThreadSanitizer报告无法恢复堆栈,报错信息类似这样: ``` Previous read by thread 39: [failed to restore the stack] ``` 可以通过调整`history_size`值大小来解决。例如: ``` TSAN_OPTIONS="history_size=7" ``` `history_size`意义: > Per-thread history size, controls how many previous memory accesses are remembered per thread. Possible values are [0..7]. history_size=0 amounts to 32K memory accesses. Each next value doubles the amount of memory accesses, up to history_size=7 that amounts to 4M memory accesses. The default value is 2 (128K memory accesses). If you consistently see only one stack trace in reports, you may try increasing the value of the flag but keep in mind that it increases memory consumption. # 参考 - [Clang 13 documentation](https://clang.llvm.org/docs/ThreadSanitizer.html) - [ThreadSanitizerSuppressions](https://github.com/google/sanitizers/wiki/ThreadSanitizerSuppressions) - [ThreadSanitizerFlags](https://github.com/google/sanitizers/wiki/ThreadSanitizerFlags) - [ThreadSanitizer (TSan) v. 2](https://www.chromium.org/developers/testing/threadsanitizer-tsan-v2/)