Repository navigation
Description
Problem
getsockopt(TCP_INFO) is issued far more often than any consumer needs it. Measured on a release build of b5169d44 (python http.server backend on loopback, curl, N=20, one worker, LD_PRELOAD backtrace interposer on getsockopt, intentrace -p as cross-check):
| scenario | TCP_INFO per request (interposer) |
needed |
|---|---|---|
| H1, 20 sequential connections | 2.95 | 2 (client_rtt + server_rtt of the access log) |
| H2, 20 sequential connections | 7.80 | 2 |
| H2, 20 requests multiplexed on one connection | 5.70 | ≤ 2 |
The only consumer of those values is the access log (client_rtt / server_rtt, command/src/logging/access_logs.rs). No metric and no load-balancing cost reads TCP_INFO: PeakEWMA is fed from the connect time (lib/src/backends.rs:312).
Attribution of the surplus, relocated on e49d78ab:
- One sample per
Muxpass, whether or not the pass emits a log.Mux::refresh_client_rtt(lib/src/protocol/mux/mod.rs:1594) runs at the top ofSessionState::ready(:1622),Mux::timeout_inner(:2944) andMux::shutting_down_inner(:3321). An H2 request takes about five passes, so it pays about five samples for the oneclient_rttits access log prints. - The first pass after the TLS handshake pays one too. The
TCP_INFOattributed tolib/src/https.rs:1109is not inupgrade(): it is the recursiveself.ready(session)that re-enters the newMux, and therefore case 1 again. Fixing case 1 removes it without touchinghttps.rs. Mux::closesamples the frontend unconditionally (mod.rs:1778) before looking for a stream that still owes an access log. On a normal close every stream has already been logged, so the sample is thrown away: one per connection, H1 and H2.
Proposal
Take the frontend sample lazily: at most once per pass, at the first access log the pass emits, and never in a pass that emits none. The H2 core still never touches a socket: it asks for the value through its Endpoint, the way it already asks for server_rtt through Endpoint::peer_rtt, and the shell that owns the socket answers and memoizes it for the rest of the pass. Mux::close samples only when it finds a stream to log.
What the access log reports does not change meaning: several H2 streams finishing in one pass still share one client_rtt, and the value is now taken when the first of them is logged instead of at the top of the pass.
Prior art: HAProxy reads TCP_INFO on demand, when a fc_rtt / bc_rtt sample fetch is evaluated (src/tcp_sample.c, get_tcp_info), never on a per-event cadence.
Expected: H1 2.95 → 2, H2 7.80 → 2, multiplexed H2 5.70 → at most 2 per request.
Activity
perf(mux): read TCP_INFO only when an access log needs it
perf(mux): read TCP_INFO only when an access log needs it (#1598)