Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 REQUEST and its ACQUIRE, a thread has a pending request for that lock.
  • Wait: the time from the thread's REQUEST to its ACQUIRE. Each acquisition has one.
  • Hold: the time from that ACQUIRE to the same thread's RELEASE of 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.
CommandMeaningPrints
t thread REQUEST lockthread calls lock() on locknothing
t thread ACQUIRE locklock() returns, and thread now holds locknothing (Part 3: INVALID when rejected)
t thread RELEASE lockthread calls unlock() on locknothing (Part 3: INVALID when rejected)
t REPORTSummarize every lock seen so farone LOCK line per lock
t TOP lockName the threads that held and waited longest (Part 2)TOP lock holder hold waiter wait
t PCT lock pThe 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 ACQUIRE arrives, and a hold when its RELEASE arrives. Only finished waits and holds enter any total.
  • The timestamp on REPORT, TOP, and PCT only places the query in the trace. A query reads the totals as they stand after every earlier command.
  • Every REQUEST is 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 book while it requests pos.

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.

  1. A lock appears once an event names it. REPORT prints one line for each lock that has appeared.
  2. A lock that is held when the REPORT runs counts the acquisition but not the hold.
  3. A lock that has only been requested prints six zeros.
  4. Lines are sorted by total_wait, largest first. Locks with equal total_wait are sorted by name in ascending character order.
  5. A REPORT before any event prints nothing.

Output of REPORT: one line per lock.

LOCK lock acquisitions contended total_wait max_wait total_hold max_hold
  • acquisitions counts the lock's ACQUIRE events, and contended counts the ones whose wait was greater than 0.
  • total_wait and max_wait are the sum and the largest of the lock's finished waits.
  • total_hold and max_hold are 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:

  • holder is the thread with the largest hold total on lock, and hold is that total. waiter and wait are 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 hold prints as - 0, and the same rule applies to waiter wait.

PCT lock p prints PCT lock p value:

  • p is an integer from 0 to 100, and value is the p-th percentile of the lock's finished waits, uncontended zeros included.
  • Percentiles use the nearest-rank definition from Latency Measurement and Percentiles.
  • With n waits sorted in ascending order and numbered from 1, the p-th percentile is the wait at rank max(1, ceil(p * n / 100)), which equals max(1, (p * n + 99) / 100) in integer division.
  • Equal waits occupy separate ranks.

Guarantees

  • Every TOP names a lock that has appeared.
  • Every PCT names 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.

EventRejected whenReason
ACQUIREthe thread has no pending request, or its pending request is for a different lockNO_REQUEST
ACQUIREanother thread holds the lockALREADY_HELD
RELEASEthe thread does not hold the lock: the lock is free, held by another thread, or has never appearedNOT_HOLDER
  1. A rejected event changes nothing: no total, no holder, and no pending request.
  2. A rejected ACQUIRE therefore leaves the thread's request pending, and a later valid ACQUIRE measures the wait from the original REQUEST.
  3. A lock named only by rejected events has not appeared and is left out of reports.
  4. REQUEST is still always valid, and the guarantees on TOP and PCT still hold.

Output of a rejected event:

INVALID line n reason
  • n is the command's position in the trace, counting the first command as line 1.
  • reason comes from the table.

Tests 7–9 cover this part.

Example
Input
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
Output
LOCK book 3 2 850 550 800 400
LOCK log 1 0 0 0 50 50
Explanation

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.