Lock Contention Report
Prerequisites: Lock Contention Debugging, Mutexes, Lock Scope, and Contention, Latency Measurement and Percentiles
The AAPL feed handler, two strategy threads, and an audit thread share a few mutexes: book guards the order book, pos the position, and log the log buffer.
Lock Contention Debugging instruments the acquire by reading the clock before and after lock(). A profiler built into the mutex wrapper records those two moments as events, adds a third when the thread calls unlock(), and writes them all to a trace.
Replay the trace to show which lock the threads waited on, for how long, and which thread held it.
- Pending request: between its
REQUESTand itsACQUIRE, a thread has a pending request for that lock. - Wait: the time from the thread's
REQUESTto itsACQUIRE. Each acquisition has one. - Hold: the time from that
ACQUIREto the same thread'sRELEASEof the lock. - Contended: an acquisition whose wait is greater than 0.
What to implement
process_commands(commands)returns the printed lines in command order.- Each command is one line that starts with a timestamp
t, an integer number of nanoseconds. Timestamps never decrease from one command to the next, and commands with equal timestamps happen in line order. - Thread and lock names are 1 to 16 lowercase letters, digits, or underscores, so they never collide with the uppercase keywords.
- In the test files, the first line is the number of commands and the commands follow one per line.
- There are three parts. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
t thread REQUEST lock | thread calls lock() on lock | nothing |
t thread ACQUIRE lock | lock() returns, and thread now holds lock | nothing (Part 3: INVALID when rejected) |
t thread RELEASE lock | thread calls unlock() on lock | nothing (Part 3: INVALID when rejected) |
t REPORT | Summarize every lock seen so far | one LOCK line per lock |
t TOP lock | Name the threads that held and waited longest (Part 2) | TOP lock holder hold waiter wait |
t PCT lock p | The p-th percentile wait (Part 2) | PCT lock p value |
Timing rules
- The profiler stamps an acquire that found the lock free with the same time on both events, so an uncontended acquisition has a wait of exactly 0.
- A wait is finished when its
ACQUIREarrives, and a hold when itsRELEASEarrives. Only finished waits and holds enter any total. - The timestamp on
REPORT,TOP, andPCTonly places the query in the trace. A query reads the totals as they stand after every earlier command. - Every
REQUESTis valid: the thread has no pending request and does not hold the lock it requests. - A thread can hold several locks at once, for example
bookwhile it requestspos.
Limits
- At most 105 commands, and timestamps lie between 0 and 1012.
- Waits, holds, and their totals can pass 231, so store times and totals in 64-bit integers (
std::int64_t).
Part 1: The per-lock report
In Parts 1 and 2 the trace is consistent: every ACQUIRE follows a REQUEST by the same thread for the same lock and finds the lock free, and every RELEASE comes from the thread that holds the lock.
- A lock appears once an event names it.
REPORTprints one line for each lock that has appeared. - A lock that is held when the
REPORTruns counts the acquisition but not the hold. - A lock that has only been requested prints six zeros.
- Lines are sorted by
total_wait, largest first. Locks with equaltotal_waitare sorted by name in ascending character order. - A
REPORTbefore any event prints nothing.
Output of REPORT: one line per lock.
LOCK lock acquisitions contended total_wait max_wait total_hold max_hold
acquisitionscounts the lock'sACQUIREevents, andcontendedcounts the ones whose wait was greater than 0.total_waitandmax_waitare the sum and the largest of the lock's finished waits.total_holdandmax_holdare the same for its finished holds.
Tests 1–3 cover this part.
Part 2: Top threads and wait percentiles
A thread's hold total on a lock is the sum of its finished holds of that lock. Its wait total is the sum of its finished waits for that lock.
TOP lock prints TOP lock holder hold waiter wait:
holderis the thread with the largest hold total onlock, andholdis that total.waiterandwaitare the same for wait totals.- A tie goes to the smaller name.
- Only a positive total qualifies. When no thread has a positive hold total,
holder holdprints as- 0, and the same rule applies towaiter wait.
PCT lock p prints PCT lock p value:
pis an integer from 0 to 100, andvalueis thep-th percentile of the lock's finished waits, uncontended zeros included.- Percentiles use the nearest-rank definition from Latency Measurement and Percentiles.
- With
nwaits sorted in ascending order and numbered from 1, thep-th percentile is the wait at rankmax(1, ceil(p * n / 100)), which equalsmax(1, (p * n + 99) / 100)in integer division. - Equal waits occupy separate ranks.
Guarantees
- Every
TOPnames a lock that has appeared. - Every
PCTnames a lock with at least one finished wait.
Tests 4–6 and 10 cover this part.
Part 3: Rejecting inconsistent events
A profiler that writes events into per-thread buffers and merges them later can lose or misorder some of them. Replaying such a trace without checks produces impossible numbers, such as a lock with two holders.
From this part on, the consistency guarantee of Part 1 no longer holds. Check each ACQUIRE and RELEASE against the rules below, in table order, and reject it at the first rule it breaks.
| Event | Rejected when | Reason |
|---|---|---|
ACQUIRE | the thread has no pending request, or its pending request is for a different lock | NO_REQUEST |
ACQUIRE | another thread holds the lock | ALREADY_HELD |
RELEASE | the thread does not hold the lock: the lock is free, held by another thread, or has never appeared | NOT_HOLDER |
- A rejected event changes nothing: no total, no holder, and no pending request.
- A rejected
ACQUIREtherefore leaves the thread's request pending, and a later validACQUIREmeasures the wait from the originalREQUEST. - A lock named only by rejected events has not appeared and is left out of reports.
REQUESTis still always valid, and the guarantees onTOPandPCTstill hold.
Output of a rejected event:
INVALID line n reason
nis the command's position in the trace, counting the first command as line 1.reasoncomes from the table.
Tests 7–9 cover this part.
13 0 feed REQUEST book 0 feed ACQUIRE book 100 s1 REQUEST book 150 s2 REQUEST book 400 feed RELEASE book 400 s1 ACQUIRE book 450 feed REQUEST log 450 feed ACQUIRE log 500 feed RELEASE log 700 s1 RELEASE book 700 s2 ACQUIRE book 800 s2 RELEASE book 900 REPORT
LOCK book 3 2 850 550 800 400 LOCK log 1 0 0 0 50 50
feed takes book at time 0 with no wait. s1 asks for it at 100 and s2 at 150, but feed holds it until 400, so s1 waits 300 ns. s1 then holds book until 700, so s2 waits 550 ns.
That gives book three acquisitions, two of them contended, and a total wait of 300 + 550 = 850 with a maximum of 550. Its holds are 400, 300, and 100, for a total of 800 and a maximum of 400.
log has one uncontended acquisition, held for 50 ns. book is printed first because its total wait, 850, is larger than the 0 of log.