Skip to content

perf(mux): read TCP_INFO only when an access log needs it - #1598

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tcp-info-sampling
Sep 26, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tcp-info-sampling

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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 reads TCP_INFO (PeakEWMA is fed from the connect time, lib/src/backends.rs). Yet:

  • the H2 frontend was sampled at the top of every Mux pass (ready, timeout, shutting_down), whether or not the pass logged anything, about five per H2 request;
  • the first ready() after the TLS handshake paid one more: the sample attributed to https.rs is the recursive self.ready(session) re-entering the new Mux, i.e. the same per-pass sample, so this PR removes it without touching https.rs;
  • Mux::close sampled the frontend unconditionally, before checking whether any stream still owed a log.

Change

  • Endpoint gains a required local_rtt(). The H2 core asks for the frontend RTT through it in snapshot_rtts, i.e. only when it logs a stream, as it already asked for server_rtt through Endpoint::peer_rtt.
  • H2Shell hands its core a ShellEndpoint, built per core call from disjoint borrows of the shell's fields, which answers local_rtt from the shell's own socket: the first stream logged in a pass samples, later streams of the pass reuse it, 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 actually logs.
  • ConnectionH2::client_rtt and Mux::refresh_client_rtt are removed. Every mux TCP_INFO read goes through protocol::mux::sample_rtt, which counts reads under cfg(test).
  • The simulator's SimEndpoint answers local_rtt with None, as it does peer_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.md and LIFECYCLE.md are updated.

Measurement

Release build (--no-default-features --features crypto-ring,opentelemetry,splice,simd), one worker, python3 -m http.server backend on loopback, curl, N=20, identical config on both sides, 1-minute load average below 8. LD_PRELOAD interposer counting getsockopt(SOL_TCP, TCP_INFO) with a backtrace per call; intentrace -p on the worker as cross-check (ANSI codes stripped, anchored on the syscall name).

scenario before (e49d78ab) after intentrace before → after
H1, 20 sequential connections 2.80 / req 1.85 / req 2.00 → 1.00
H2, 20 sequential connections 7.85 / req 1.85 / req 6.00 → 1.00
H2, 20 streams multiplexed on 1 connection 5.60 / req 1.80 / req 3.15 → 1.00

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_rtt cells are - on both sides of the change. client_rtt is present on every line before and after.

Prior art

HAProxy reads TCP_INFO on demand, when a sample fetch is evaluated, never per event (haproxy 0ceb8c65): get_tcp_info in src/tcp_sample.c:321-339, smp_fetch_fc_rtt at :345-356, smp_fetch_bc_rtt at :373-385, keyword table at :610-613; the only getsockopt(TCP_INFO) in src/ is tcp_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_rtt present 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.py forwards their old names.

Local battery: build, cargo +nightly fmt --check, clippy --all-targets -D warnings, tests for sozu-lib, sozu, sozu-command-lib, sozu-e2e (fuzz skipped), sim test and clippy under --cfg tokio_unstable, the four check_doc_citations.py invocations, and cargo doc with -D warnings: all green.

Closes #1590

Signed-off-by: Florentin Dubois [email protected]

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
FlorentinDUBOIS merged commit f165a3d into main Sep 26, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/tcp-info-sampling branch September 26, 2026 22:16
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]>
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.

perf(mux): getsockopt(TCP_INFO) is sampled every pass instead of once per access log

1 participant