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

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 per QUERY, COUNT, MAX, and BUCKET, in command order.
  • sub_bits is the precision m, an integer from 0 to 10, shared by every histogram.
  • Histogram names are short words of uppercase letters and digits, such as T1 or ALL. 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.
CommandWhat it doesPrints
RECORD h vAdd sample v to hnothing
QUERY h pThe p-th percentile of ha bucket's hi, or EMPTY
COUNT hThe number of samples in hthe count
MAX hThe largest sample in hthe sample, or EMPTY
BUCKET vThe bucket that holds vindex lo hi
MERGE dst srcAdd src into dst (Part 2)nothing
RESET hEmpty h (Part 2)nothing
RECORD_CO h v intervalRecord 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 QUERY or MERGE.

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 k from m upward, 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:

ValuesBucketsWidth of each bucket
0 to 70 to 71
8 to 158 to 112
16 to 3112 to 154
32 to 6316 to 198

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's std::bit_width(v) from <bit> returns k + 1, and so does Python's v.bit_length().
  • Both return 0 for v = 0, which gives k = -1.
shift = max(0, k - m)
index = (shift << m) + (v >> shift)
  • Shifting v right by shift keeps its top m + 1 bits. The leading bit is always 1, so v >> shift lies between 2m and 2m+1 − 1, and its low m bits pick one of the range's 2m buckets.
  • shift << m skips the 2m buckets of each range below.
  • When v is below 2m+1, the shift is 0 and the index is v itself.
  • Example, m = 2 and v = 21 (binary 10101): k = 4, so shift = 2, v >> 2 = 5, and the index is 8 + 5 = 13.

From a bucket to its values

  • Bucket i below 2m holds only the value i.
  • For i of at least 2m: shift = (i >> m) - 1, the lowest value is lo = (i - (shift << m)) << shift, and the highest is hi = lo + 2^shift - 1.
  • Example: bucket 13 for m = 2 has shift = 2, lo = 5 << 2 = 20, and hi = 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 most lo / 2^m.
  • A sample reported as its bucket's hi is never below its true value and overshoots it by less than 1/2m of it. With m = 7 that 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 when m = 10, so a flat array of counts per histogram is small.
  • shift reaches 39, so compute lo, hi, and 2^shift in 64-bit integers.

Percentiles

  • Percentiles use the nearest-rank definition from the article, the same one as Sliding Window Percentiles.
  • With n samples and an integer p from 0 to 100, the p-th percentile is the sample at rank = max(1, (p * n + 99) / 100) in integer division, counting the samples from 1 in ascending order.

Part 1: Record and query

  1. RECORD h v adds 1 to the count of v's bucket in h.
  2. QUERY h p computes rank from h's sample count, walks the buckets from index 0 upward adding their counts, and stops at the first bucket where the running total reaches rank. It prints that bucket's hi.
  3. That bucket holds the true rank-th sample, so the printed value is an upper bound on it, within the relative error above.
  4. On a histogram with no samples, QUERY and MAX print EMPTY and COUNT prints 0.
  5. MAX h prints the largest sample exactly. The bucket counts cannot give it back, so keep it in a field next to them.
  6. BUCKET v names 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.

  1. MERGE dst src adds src's count in every bucket to dst's count in the same bucket, adds src's sample count to dst's, and sets dst's maximum to the larger of the two.
  2. src is unchanged. dst and src are always different names, and either may be empty.
  3. RESET h sets every count to 0 and leaves h with 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 * n in 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:

  1. Assume each missing update would have completed when the slow one did. The update due j * interval nanoseconds after the slow one was sent would then have taken v - j * interval.
  2. So record v, then v - interval, v - 2 * interval, and so on, for every j of at least 1 with v - j * interval > 0.
  3. A response that takes interval or less adds nothing beyond v itself.

Limits

  • interval is between 1 and 1012.
  • The RECORD_CO commands of one test add at most 106 samples between them.

Tests 8–10 cover this part.

Example
Input
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
Output
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
Explanation

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.