Repository navigation
perf(mux): read TCP_INFO only when an access log needs it - #1598
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 26, 2026 20:55
The access log's client_rtt and server_rtt are the only consumers of getsockopt(TCP_INFO) in the tree, yet the H2 frontend was sampled at the top of every Mux pass (ready, timeout, shutting_down, including the first ready() after the TLS handshake) whether or not the pass logged, and Mux::close sampled the frontend before looking for a stream still owing a log. The H2 core now asks for the frontend value through Endpoint::local_rtt, as it already asked for server_rtt through Endpoint::peer_rtt. H2Shell answers it through ShellEndpoint, built per core call from disjoint borrows of its own fields: the first stream logged in a pass samples the shell's socket, later streams of the pass reuse that value, and Mux::expire_client_rtt forgets it at the next pass without a syscall. The core still holds no OS handle. Mux::close samples only for a stream it logs. ConnectionH2::client_rtt and Mux::refresh_client_rtt are removed, and every mux TCP_INFO read goes through protocol::mux::sample_rtt, which counts reads under cfg(test). Endpoint gains a required local_rtt; an implementor with no local socket answers None. The logged value keeps its meaning: streams finishing in one pass still share one client_rtt, now taken when the first of them is logged. Release build, one worker, python backend, 20 requests, LD_PRELOAD interposer on getsockopt(TCP_INFO), per request: H1 2.80 -> 1.85, H2 7.85 -> 1.85, 20 multiplexed H2 streams 5.60 -> 1.80 (intentrace -p: 2.00 -> 1.00, 6.00 -> 1.00, 3.15 -> 1.00). The two tests that pinned the removed carried field are rewritten to pin the lazy contract, and check_doc_citations.py forwards their old names, which the CHANGELOG keeps. The sim's Endpoint answers local_rtt with None, as it does peer_rtt. Closes #1590 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
perf/tcp-info-sampling
branch
from
September 26, 2026 22:15
2ff67f5 to
04a6524
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
## Problem The access log's `client_rtt` and `server_rtt` each cost one `getsockopt(TCP_INFO)` per request. The H1 log sites (`H1::Complete`, `H1::Upgrade`, `H1::EarlyHint`) and `Endpoint::peer_rtt` read their socket at every line, and `Mux::expire_client_rtt` forgot the H2 frontend sample at every pass (#1598 made the read lazy, not rarer). On an H1 keep-alive connection that was 2.00 of the worker's 11.55 syscalls per request. ## Change Operator decision: accept one sample per connection. - `memoized_rtt` (`lib/src/protocol/mux/mod.rs`) keeps a connection's first sample in a `Cell<Option<Option<Duration>>>` on the connection (`ConnectionH1::rtt`, new field; `H2Shell::local_rtt`, existing field) and answers every later request from it. `Connection::rtt` dispatches H1/H2. - Every mux RTT read goes through it: the three H1 log sites, `EndpointServer::peer_rtt`, `EndpointClient::peer_rtt`, `ShellEndpoint::local_rtt` and `Mux::close`. A backend connection keeps its sample across keep-alive pool reuse, since the `Connection` carries it. - `Mux::expire_client_rtt` and `H2Shell::expire_local_rtt` are removed. - Pipe (TCP/WebSocket) and the TCP frontend already log once per session and are unchanged. After a WebSocket upgrade, `Pipe::log_request` still reads `socket_rtt` once, at the end of the session. That is one read per session, left out of scope. No syscall added anywhere, no allocation added (one `Cell` of 16 bytes per `ConnectionH1`), no I/O path touched. ## Sampling point, and why Hypothesis tested: "`tcpi_rtt` is 0 before the first data ACK". **Refuted** on Linux 7.2.7, loopback. A Python probe reading `TCP_INFO` (`tcpi_rtt`, `tcpi_rttvar`): - straight out of `accept`: 34 / 27 / 29 µs over three trials. This is the SYN/SYN-ACK measurement. - straight out of a blocking `connect`: 80 / 30 / 33 µs. - after 1st/2nd/3rd request on the same connection: 34 → 33 → 1301 µs. The SRTT keeps drifting. So a sample at `accept` would be valid. The chosen point is still **the connection's first access log**: - frontend: the first request completed on the connection; - backend: the first response logged through it. The lazy read is free on a connection that never logs. A backend dial is still in flight when its non-blocking `connect(2)` returns. Semantics, now documented: **"RTT sampled at the connection's first request"**, repeated on every line of that connection. The value is frozen and no longer follows the kernel's SRTT. The two cells of one line come from two different instants. ## Measurement `intentrace -p` on the worker. Release build `--no-default-features --features jemallocator,crypto-aws-lc-rs`, one worker, python `http.server` backend on loopback, `curl`, 20 requests on one client connection (plus warmup). Base `58550dd4`, runs interleaved base/after, two runs per cell, 1-minute load average 21–48 (shared host): | scenario | syscalls/req before | after | `getsockopt`/req before | after | |---|---|---|---|---| | H1 keep-alive, plaintext | 11.55, 11.55 | 9.65, 9.65 | 2.00 | 0.10 | | H1 keep-alive, TLS | 11.85, 11.85 | 9.95, 9.95 | 2.00 | 0.10 | | H2, 20 streams multiplexed on 1 connection | 12.35, 12.20 | 11.35, 11.30 | 2.00 | 1.05 | - **H1: −1.90 syscalls/req.** The remaining 0.10 is the two per-connection reads, one per side. - **H2 multiplexed: −1.00.** Sozu dials one backend H1 connection per concurrent stream (20 `connect`s in the window), so the 20 backend reads remain, one per connection. Only the 19 repeated frontend reads disappear. - **Access logs, after:** every one of the 27 lines per run carries both `client_rtt` and `server_rtt`, with no `-` cell. There are 4 distinct (client, server) pairs, one per client connection (readiness probe, two warmups, the measured one). Before: 27 distinct pairs. ## Tests Seen red on `58550dd4` before the fix, green after: - `keep_alive_requests_read_tcp_info_once_per_connection` (`h1.rs`, new). It drives 20 keep-alive requests through the real `H1::Complete` site of `ConnectionH1::writable`, with the backend recycled by `end_stream`/`start_stream` as a `Mux` does. It asserts two things: - 2 `TCP_INFO` reads in total. Red: `left: 40, right: 2`. - Both RTT cells are present on all 20 access lines, with identical values. - `a_mux_pass_keeps_the_connection_sample_and_reads_none_without_a_log` (`mod.rs`). This rewrites `a_mux_pass_forgets_the_previous_sample_and_reads_none_without_a_log`. Red: `left: None, right: Some(Some(4321s))`. Also rewritten: `snapshot_rtts_samples_the_frontend_once_per_pass` became `snapshot_rtts_samples_the_frontend_once_per_connection` (`h2.rs`). Its documented red (the memo hit removed from `memoized_rtt`) was checked: `left: 2, right: 1`. The old names are forwarded in `check_doc_citations.py` `RENAMED_TESTS`, because the CHANGELOG cites them. ## Docs - `doc/configure.md` "When each cell is measured": **Changed** RTT semantics in access logs. - `doc/observability.md`. - `doc/h2_mux_internals.md`: new "One sample per connection" section, with the measurements. - `lib/src/protocol/mux/LIFECYCLE.md`. - `CHANGELOG.md` (Changed). My line shifts in `h1.rs`, `h2.rs` and `mod.rs` moved 27 `file.rs:NNN` citations and three pinned blocks. They are re-anchored mechanically from a line diff, in both `LIFECYCLE.md`, `doc/testing.md` and `doc/h2_mux_internals.md`, and `--base origin/main` confirms every cited line's text is unchanged. ## Conflicts with open PRs - `git merge-tree` against #1628 and #1629: clean. The new test uses `"test-cluster".into()`, so it compiles with #1629's `ClusterId` too. - Against #1630: three pinned `h2.rs:NNNN` fence numbers in `doc/h2_mux_internals.md` / `LIFECYCLE.md` conflict, numbers only. Whichever PR merges second re-anchors them. - `kawa_h1/editor.rs` and `HttpContext` are untouched. ## Commands run - `cargo build --locked` - `cargo build --all-features --locked` - `cargo clippy --all-targets --locked -- -D warnings` - `cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings` - `cargo +nightly fmt --all -- --check` - `check_doc_citations.py` (tree, `--base origin/main`, `--self-test`, broken-fixture `--root` exit 1 with its failure report) - `cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8`: 2160 passed, 0 failed. Results: - The full battery passed on the pre-amend tree. - The final amend changed only a test literal (`to_owned()` → `into()`). After it I re-ran fmt, both clippy cells, the citation runs and the new test. - A first battery run had one failure: `tests::tests::test_async` (client1 99/100 responses) under `--test-threads=8` at load average 40+. I ran it 3 times in isolation on this branch and 3 times on `58550dd4`: 6/6 green. The second full battery run was green. Protocol/security impact: none. No framing, parsing, readiness or I/O change. The only change is when an access-log value is read. Signed-off-by: Florentin Dubois <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getsockopt(TCP_INFO)was issued far more often than anything consumes it. The only consumer of those values is the access log (client_rtt/server_rtt); no metric, histogram or load-balancing cost readsTCP_INFO(PeakEWMAis fed from the connect time,lib/src/backends.rs). Yet:Muxpass (ready,timeout,shutting_down), whether or not the pass logged anything, about five per H2 request;ready()after the TLS handshake paid one more: the sample attributed tohttps.rsis the recursiveself.ready(session)re-entering the newMux, i.e. the same per-pass sample, so this PR removes it without touchinghttps.rs;Mux::closesampled the frontend unconditionally, before checking whether any stream still owed a log.Change
Endpointgains a requiredlocal_rtt(). The H2 core asks for the frontend RTT through it insnapshot_rtts, i.e. only when it logs a stream, as it already asked forserver_rttthroughEndpoint::peer_rtt.H2Shellhands its core aShellEndpoint, built per core call from disjoint borrows of the shell's fields, which answerslocal_rttfrom the shell's own socket: the first stream logged in a pass samples, later streams of the pass reuse it, andMux::expire_client_rttforgets it at the next pass without a syscall. The core still holds no OS handle.Mux::closesamples only for a stream it actually logs.ConnectionH2::client_rttandMux::refresh_client_rttare removed. Every muxTCP_INFOread goes throughprotocol::mux::sample_rtt, which counts reads undercfg(test).SimEndpointanswerslocal_rttwithNone, as it doespeer_rtt.The logged value keeps its meaning: streams finishing in one pass still share one
client_rtt(#1339 Q11); it is now the SRTT when the first of them is logged rather than at the top of the pass.doc/configure.md,doc/observability.md,doc/h2_mux_internals.mdandLIFECYCLE.mdare updated.Measurement
Release build (
--no-default-features --features crypto-ring,opentelemetry,splice,simd), one worker,python3 -m http.serverbackend on loopback,curl, N=20, identical config on both sides, 1-minute load average below 8.LD_PRELOADinterposer countinggetsockopt(SOL_TCP, TCP_INFO)with a backtrace per call;intentrace -pon the worker as cross-check (ANSI codes stripped, anchored on the syscall name).e49d78ab)Worker totals (intentrace, per request): H1 27.00 → 25.85, H2 62.10 → 57.05, multiplexed 25.05 → 22.90. Every remaining read is an access-log cell; the figure is just under 2 because the python backend closes its connection after each response, so a few
server_rttcells are-on both sides of the change.client_rttis present on every line before and after.Prior art
HAProxy reads
TCP_INFOon demand, when a sample fetch is evaluated, never per event (haproxy0ceb8c65):get_tcp_infoinsrc/tcp_sample.c:321-339,smp_fetch_fc_rttat:345-356,smp_fetch_bc_rttat:373-385, keyword table at:610-613; the onlygetsockopt(TCP_INFO)insrc/istcp_get_info,src/proto_tcp.c:969-979.Tests
Each seen red before green:
snapshot_rtts_samples_the_frontend_once_per_pass(h2.rs): two streams in one pass cost one read and share one value; the next pass reads again.a_pass_that_logs_nothing_reads_no_tcp_info(h2.rs): zero reads without a log, zero for a backend H2 connection.streams_reaped_in_one_pass_log_one_present_client_rtt(h2.rs): two real access-log lines,client_rttpresent and identical, one read.a_mux_pass_forgets_the_previous_sample_and_reads_none_without_a_log(mod.rs).mux_close_reads_the_frontend_rtt_only_for_a_stream_it_logs(mod.rs).the_h1_access_log_carries_the_frontend_rtt(h1.rs).The two tests that pinned the removed field (
snapshot_rtts_reports_the_carried_pass_sample_to_every_stream,a_mux_pass_refreshes_the_carried_client_rtt) are rewritten into the first and fourth above;check_doc_citations.pyforwards their old names.Local battery: build,
cargo +nightly fmt --check, clippy--all-targets -D warnings, tests forsozu-lib,sozu,sozu-command-lib,sozu-e2e(fuzz skipped), sim test and clippy under--cfg tokio_unstable, the fourcheck_doc_citations.pyinvocations, andcargo docwith-D warnings: all green.Closes #1590
Signed-off-by: Florentin Dubois [email protected]