Skip to content

Raise the default defrag test latency limit from 5ms to 20ms - #245

Open
madolson wants to merge 1 commit into
unstablefrom
fix/defrag-tracking-latency
Open

madolson wants to merge 1 commit into
unstablefrom
fix/defrag-tracking-latency

Conversation

@madolson

@madolson madolson commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

The active defrag tests that use the default 5ms latency limit fail on about 1 in 200 test-ubuntu-latest jobs, for example "Active Defrag tracking table: standalone" with Expected 9 <= 5. The cause is the runner, not defrag. Starvation protection makes each cycle about 1.5 times as long as the wait before it, so a few-ms scheduling stall shows up as a defrag cycle over 5ms. A stall that lands mid-cycle adds straight to the cycle time. This raises the default to 20ms, above the noise seen in CI and still well below what a stage that ignores its deadline produces.

AI generated details

Problem

computeDefragCycleUs (src/defrag.c:1100) sizes the next duty cycle as D = P * W / (100 - P), where W is the time since the previous cycle ended. perform_defrag_test sets active-defrag-cycle-max 60 (tests/unit/memefficiency.tcl:194), so D can reach 1.5 * W. The latency monitor around the cycle (src/defrag.c:1170-1196) measures wall time, so a stall inside the cycle counts too. Both cases fail the 5ms limit when every stage honors its deadline.

It is not specific to the tracking table. CI logs print Validating max latency (N) for every defrag test, which makes the distribution visible. Across 518 failed jemalloc jobs since the tracking-table test landed (2026-10-14), 76 of 8,166 samples from 5ms-limit tests exceeded 5ms. They came from every test except big list: stream 22, main dictionary 14, tracking table 12, big hash 8, big zset 15, big set 5. The highest was 11ms. 58 jobs failed on it, 13 of them test-ubuntu-latest, out of about 2,800 test-ubuntu-latest jobs. Every outlier is a burst of 1 to 3 cycles inside one second, which fits a stall and not a stage that loops past its deadline.

Repro

A debug log of latency and dutyCycleUs per cycle, plus a script that SIGSTOPs the server for 6ms every 100 to 300ms, fails 3 of 6 tracking-table runs:

Validating max latency (6) is LT 5
Validating max latency (7) is LT 5
latency=6540us dutyCycleUs=500     <- stop landed mid-cycle
latency=6955us dutyCycleUs=6954    <- starvation protection stretched the cycle
latency=7499us dutyCycleUs=7497

With 4ms stops the maximum is 5ms and nothing fails. Running the full ./runtest --clients 16 pinned to 4 cores next to the test, like a 4-vCPU runner, failed 2 of 16 runs with 13ms and 14ms. Plain busy-loop burners on the same cores did not reproduce it (12 runs, max 2ms), since wakeup preemption favors the server over CPU-bound loops.

Why 20ms

20ms is above the CI maximum (11ms) and the local oversubscribed maximum (14ms). It still catches a deadline regression. With the deadline checks in defragTrackingTable disabled, the test fails with Expected 79 <= 20. The tradeoff is that a stage overrunning by 5 to 20ms per cycle would no longer be caught. valkey-io#2553 lowered the limit from 40 to 5 after its hit-threshold change.

Alternatives considered:

  • Ignore single-spike bursts. Rejected: a stage that defrags one large object synchronously also produces one spike per pass, which is what Refactor scanLaterList to fix latency issue valkey-io/valkey#2514 fixed.
  • Lower active-defrag-cycle-max in the test. Rejected: this only reduces starvation amplification, does nothing for in-cycle stalls, and slows every defrag test.
  • --no-latency in CI. Rejected: it turns the check off everywhere.

The big list test keeps its explicit latency 5. It had 0 outliers in 1,036 CI samples.

Testing

With 6ms stops and this change, 4 of 6 runs measured 7ms and all passed.

This was generated by AI but verified, with love, by a human.

Defrag's starvation protection sizes each duty cycle from the time since
the previous one ended, at up to 1.5x the wait with active-defrag-cycle-max
60. A few-ms scheduling stall on a shared CI runner therefore turns into a
defrag cycle longer than 5ms even when every stage honors its deadline, and
a stall landing mid-cycle adds directly to it. Every test using the default
limit flakes this way, failing about 1 in 200 test-ubuntu-latest jobs. 20ms
stays above the observed noise (max 11ms in CI) while still catching a stage
that ignores its deadline.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson added the needs-review Staged work waiting on madolson's review label Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review Staged work waiting on madolson's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant