Skip to content

perf(mux): sample the access-log RTT once per connection - #1634

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tcp-info-per-connection
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tcp-info-per-connection

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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 connects 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

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]

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the perf/tcp-info-per-connection branch 2 times, most recently from b90603d to 07fb780 Compare September 27, 2026 23:51
client_rtt and server_rtt each cost one getsockopt(TCP_INFO) per
request: the H1 access-log sites and Endpoint::peer_rtt read their
socket at every log line, and Mux::expire_client_rtt forgot the H2
frontend sample at every pass. On an H1 keep-alive connection that was
two of the worker's 11.55 syscalls per request.

Each connection now reads its own socket once. memoized_rtt
(lib/src/protocol/mux/mod.rs) stores the first sample in the
connection (ConnectionH1::rtt, H2Shell::local_rtt), and Connection::rtt
answers every later ask from it: the H1 log sites, EndpointServer and
EndpointClient::peer_rtt, ShellEndpoint::local_rtt and Mux::close. A
backend connection keeps its sample across keep-alive pool reuse.
Mux::expire_client_rtt and H2Shell::expire_local_rtt are removed. Pipe
and TCP already logged once per session and are unchanged.

The sample is taken at the connection's first access log, not at
accept: Linux already reports the handshake RTT straight out of accept
(27-34 us on loopback, and likewise after connect), so an earlier read
would be valid, but the lazy one is free on a connection that never
logs, and a backend dial is still in flight when its non-blocking
connect(2) returns. Operator-visible: both cells now mean "the SRTT at
the connection's first request", repeated on every line of that
connection; doc/configure.md, doc/observability.md,
doc/h2_mux_internals.md, LIFECYCLE.md and CHANGELOG.md say so.

intentrace -p, release build, 20 requests on one client connection:
H1 keep-alive 11.55 -> 9.65 syscalls/request (getsockopt 2.00 -> 0.10),
H1 over TLS 11.85 -> 9.95, 20 multiplexed H2 streams 12.35 -> 11.35
(getsockopt 2.00 -> 1.05; one backend connection per concurrent stream).

keep_alive_requests_read_tcp_info_once_per_connection (h1.rs) and
a_mux_pass_keeps_the_connection_sample_and_reads_none_without_a_log
(mod.rs) were seen red on 58550dd (40 reads instead of 2; the pass
forgot the sample). The once-per-pass H2 test is rewritten into
snapshot_rtts_samples_the_frontend_once_per_connection, forwarded in
check_doc_citations.py, and the line citations this changeset moved
are re-anchored.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the perf/tcp-info-per-connection branch from 07fb780 to cc59abf Compare September 28, 2026 01:21
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 41ccc1b into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/tcp-info-per-connection branch September 28, 2026 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant