CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 5 of 7 · 2 min read

Tools

In plain words: A building inspector does not wait for a fire to find a fault in the wiring. Tools can likewise find concurrency faults by watching which accesses and locks occur, with no need for the unlucky timing. The analogy stops in one place: an inspector sees the wiring, and a tool sees only the paths that the test run actually took.

ThreadSanitizer (TSan) is a compiler feature that is built into GCC and Clang. The program is compiled with -fsanitize=thread -g, and TSan records every memory access and every lock, and reports a pair of unordered conflicting accesses, with the stack of each. On the racy counter:

WARNING: ThreadSanitizer: data race
  Read of size 4 at 0x... by thread T2:
  Previous write of size 4 at 0x... by thread T1:
  Location is global 'counter' of size 4
SUMMARY: ThreadSanitizer: data race race.c:4 in f

The line number and the variable are named, even though the run itself might have produced a correct total. With the mutex added, the same run printed only counter 200000 and no warning. TSan also found a lock-order inversion in a program where one thread locks A then B and another locks B then A, run one after the other, so that no deadlock happened:

WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock)
  Cycle in lock order graph: M0 => M1 => M0

It warned about the circle of waits that page 7 drew, before any deadlock occurred. The cost is a slowdown. The counter program took 0.017 s normally and 0.198 s under TSan, about 12 times longer, and uses more memory. Use it in testing, not in production.

Helgrind is a tool of Valgrind that checks a compiled program without rebuilding it (valgrind --tool=helgrind ./prog). On the same lock-order program, it reported lock order "0x10C040 before 0x10C080" violated. It is slower than TSan, and it works on programs that cannot be recompiled.

Neither tool proves the absence of bugs. They report what the run did, so a path never taken in the test is never checked. Good practice: run the test suite under TSan in the build system, with tests that use several threads and many iterations.

ThreadSanitizer

ThreadSanitizer is a dynamic analysis tool, built into the compiler, that instruments a program to record memory accesses and synchronisation, and that reports data races and lock-order inversions found during a run, with the code locations involved.

Example: compiling with gcc -g -fsanitize=thread and running prints a data-race warning naming the global variable and the line of the unlocked increment.

Check yourself: 1. How did TSan find the lock-order problem without a deadlock? 2. What is the cost of using it? 3. What can the tools not do?