Log-Bucketed Latency Histogram
Prerequisites: Latency Measurement and Percentiles, Performance Counters Intro
Every thread of the AAPL feed handler times each update in nanoseconds, and a monitoring thread reports percentiles. Build the log-bucketed histogram the article calls an HDR histogram.
- Histogram: an array of counters, one per latency range. Recording a sample is one increment and the memory stays fixed. Sorting every kept sample is exact, but its memory grows with the run.
- Log-bucketed: the ranges widen as latencies grow, but a range is never wider than a fixed fraction of the values in it.
What to implement
process_commands(sub_bits, commands)returns one line perQUERY,COUNT,MAX, andBUCKET, in command order.sub_bitsis the precisionm, an integer from 0 to 10, shared by every histogram.- Histogram names are short words of uppercase letters and digits, such as
T1orALL. A histogram is created empty the first time a command names it. - In the test files, line 1 is
m, line 2 is the number of commands, and the commands follow one per line. - There are three parts. One solution must pass all eleven tests.
| Command | What it does | Prints |
|---|---|---|
RECORD h v | Add sample v to h | nothing |
QUERY h p | The p-th percentile of h | a bucket's hi, or EMPTY |
COUNT h | The number of samples in h | the count |
MAX h | The largest sample in h | the sample, or EMPTY |
BUCKET v | The bucket that holds v | index lo hi |
MERGE dst src | Add src into dst (Part 2) | nothing |
RESET h | Empty h (Part 2) | nothing |
RECORD_CO h v interval | Record v plus the missed samples (Part 3) | nothing |
Limits
- Latencies are integers from 0 to 1012 nanoseconds, about 17 minutes, so they need a 64-bit integer.
- At most 105 commands, and at most 1000 of them are
QUERYorMERGE.
The buckets
- Buckets are numbered 0, 1, 2, and so on in increasing order of value.
- Every value below 2m has a bucket to itself.
- For each
kfrommupward, the values from 2k to 2k+1 − 1 form one power-of-two range, cut into 2m buckets of equal width 2k−m. - Each range is twice as wide as the one below it and holds the same number of buckets, so the buckets are evenly spaced on a logarithmic scale.
For m = 2:
| Values | Buckets | Width of each bucket |
|---|---|---|
| 0 to 7 | 0 to 7 | 1 |
| 8 to 15 | 8 to 11 | 2 |
| 16 to 31 | 12 to 15 | 4 |
| 32 to 63 | 16 to 19 | 8 |
From a value to its bucket
The bucket of v comes from its highest set bit: the position k of the leftmost 1 in its binary form, so that 2k ≤ v < 2k+1.
- For an unsigned
v, C++20'sstd::bit_width(v)from<bit>returnsk + 1, and so does Python'sv.bit_length(). - Both return 0 for
v = 0, which givesk = -1.
shift = max(0, k - m) index = (shift << m) + (v >> shift)
- Shifting
vright byshiftkeeps its topm + 1bits. The leading bit is always 1, sov >> shiftlies between 2m and 2m+1 − 1, and its lowmbits pick one of the range's 2m buckets. shift << mskips the 2m buckets of each range below.- When
vis below 2m+1, the shift is 0 and the index isvitself. - Example,
m = 2andv = 21(binary10101):k = 4, soshift = 2,v >> 2 = 5, and the index is8 + 5 = 13.
From a bucket to its values
- Bucket
ibelow 2m holds only the valuei. - For
iof at least 2m:shift = (i >> m) - 1, the lowest value islo = (i - (shift << m)) << shift, and the highest ishi = lo + 2^shift - 1. - Example: bucket 13 for
m = 2hasshift = 2,lo = 5 << 2 = 20, andhi = 23.
Error bound and size
- Buckets of width 1 are exact.
- A wider bucket has width 2shift and starts at
lo, which is at least 2m · 2shift, so its width is at mostlo / 2^m. - A sample reported as its bucket's
hiis never below its true value and overshoots it by less than 1/2m of it. Withm = 7that is under 1/128 of the value, about 0.8 percent. - Values up to 1012 < 240 use at most (41 −
m) · 2m buckets, 31,744 whenm = 10, so a flat array of counts per histogram is small. shiftreaches 39, so computelo,hi, and2^shiftin 64-bit integers.
Percentiles
- Percentiles use the nearest-rank definition from the article, the same one as Sliding Window Percentiles.
- With
nsamples and an integerpfrom 0 to 100, thep-th percentile is the sample atrank = max(1, (p * n + 99) / 100)in integer division, counting the samples from 1 in ascending order.
Part 1: Record and query
RECORD h vadds 1 to the count ofv's bucket inh.QUERY h pcomputesrankfromh's sample count, walks the buckets from index 0 upward adding their counts, and stops at the first bucket where the running total reachesrank. It prints that bucket'shi.- That bucket holds the true
rank-th sample, so the printed value is an upper bound on it, within the relative error above. - On a histogram with no samples,
QUERYandMAXprintEMPTYandCOUNTprints 0. MAX hprints the largest sample exactly. The bucket counts cannot give it back, so keep it in a field next to them.BUCKET vnames no histogram and changes nothing.
Tests 1–4 cover this part.
Part 2: Merge across threads
Each thread records into its own histogram, so recording takes no lock and writes no cache line that another core reads. The monitoring thread combines them afterward.
MERGE dst srcaddssrc's count in every bucket todst's count in the same bucket, addssrc's sample count todst's, and setsdst's maximum to the larger of the two.srcis unchanged.dstandsrcare always different names, and either may be empty.RESET hsets every count to 0 and leaveshwith no maximum.
Notes:
- A sample's bucket depends only on its value, so a merge equals recording both histograms' samples into
dst, with the same error bound. - Exact samples merge only by keeping them all, and percentiles cannot be merged at all. In test 5, the merged p90 is not the average of the two threads' p90s.
- A monitor that reports once a second merges each thread's histogram into a total and resets it, so the next second starts empty.
Limits
- Merges can push counts past 231, and a histogram never holds more than 1015 samples. Store counts and sample counts in 64-bit integers, and compute
p * nin 64 bits too.
Tests 5–7 and 11 cover this part.
Part 3: Correct for coordinated omission
The load generator is meant to send one update every interval nanoseconds, but it waits for each response before sending the next. When a response takes v > interval, the updates due while it was outstanding are never sent. This is the article's coordinated omission.
RECORD_CO h v interval records v plus the latencies those missing updates would have seen:
- Assume each missing update would have completed when the slow one did. The update due
j * intervalnanoseconds after the slow one was sent would then have takenv - j * interval. - So record
v, thenv - interval,v - 2 * interval, and so on, for everyjof at least 1 withv - j * interval > 0. - A response that takes
intervalor less adds nothing beyondvitself.
Limits
intervalis between 1 and 1012.- The
RECORD_COcommands of one test add at most 106 samples between them.
Tests 8–10 cover this part.
2 20 BUCKET 5 BUCKET 8 BUCKET 9 BUCKET 21 BUCKET 100 RECORD A 5 RECORD A 9 RECORD A 21 RECORD A 22 RECORD A 100 COUNT A QUERY A 0 QUERY A 50 QUERY A 80 QUERY A 81 QUERY A 100 MAX A QUERY B 50 COUNT B MAX B
5 5 5 8 8 9 8 8 9 13 20 23 22 96 111 5 5 23 23 111 111 100 EMPTY 0 EMPTY
With m = 2, values 0 to 7 have one bucket each, so BUCKET 5 prints bucket 5, holding only 5. Values 8 to 15 share buckets of width 2, so 8 and 9 both land in bucket 8.
21 is binary 10101 with highest set bit 4, so the shift is 2 and the index is (2 << 2) + (21 >> 2) = 13, the bucket of 20 to 23.
100 is binary 1100100 with highest set bit 6, so the shift is 4 and the index is (4 << 2) + 6 = 22, the bucket of 96 to 111. Histogram A then holds five samples in buckets 5, 8, 13, 13, and 22.
QUERY A 50 has rank (50 * 5 + 99) / 100 = 3. The running counts 1, 2, 4 first reach 3 at bucket 13, so it prints 23, while the true third sample is 21.
QUERY A 81 has rank 5 and prints 111, the top of bucket 22. So does QUERY A 100, even though MAX A prints the exact maximum, 100. Histogram B was never recorded into, so it is empty.