Repository navigation
tcp: a connection is found by the slot its reactor gave it, not by its descriptor, so the table and the walks over it are as long as the reactor's own connections - #658
Open
MDA2AV wants to merge 1 commit into
Conversation
…s descriptor, so the table and the walks over it are as long as the reactor's own connections The tag of a recv or send carried the connection's descriptor, and the reactor found the connection in a table indexed by it. Descriptors are numbered across the process, so on every reactor that table was as long as the highest one open anywhere, and the tick's walk, made whenever a read or send timeout is set, went through all of it. A connection takes a slot in its reactor's table at accept and gives it back when it is recycled, and the tag carries the slot. One array, as long as the most connections the reactor has held at once, serves the lookup and both walks, the tick's and the stop's. The generation guards a slot's reuse as it did a descriptor's. The flush, buffer-return, deferred-shutdown and starved-recv entries carry the slot too, and the count of live connections is the slots used less the ones free. Reactor.EnqueueReturnQIncremental is internal now: its first argument is the slot, which no caller outside the runtime has.
This branch has not been deployed
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.
A reactor found a completion's connection in
_connections, an array indexed by file descriptor, because the operation's tag (user_data) carried the fd. Descriptors are numbered across the process, so on every reactor that array was as long as the highest one open anywhere, for the few that are its own: at a million connections over 64 reactors, 1M entries (8 MB) per reactor for about 15,600 connections.Finding one connection is one index, however long the array is. Going through all of them is not. The tick's walk visits every empty entry, four times a second and on every reactor at once, whenever a read or send timeout is set, and both are 60 s by default. #653 made that walk rarer, not cheaper. HttpArena's connections-ramp-h1 fits that: with 35 s timeouts, in the seconds before its first connection came due, genhttp-ioxide's p99.9 was 1.0-1.2 ms next to a p99 of 137-139 µs.
Now each connection takes a slot in its reactor's table when it is accepted, and the tag carries that.
What changes
_slotsreplaces_connections, for the lookup and for both walks, the tick's and the stop's. At accept a connection takes the last freed slot, or the next new one (Track). It leaves the table when the reactor lets go of it (Untrack) and gives the slot back when it is recycled, after the cancel and the close. The array is as long as the most connections the reactor has held at once.conn.ClientFd._tcpLiveis gone: what is accepted and not recycled yet is the slots handed out less the free ones.In state that is the count of slots handed out, the free ones (a
Stack<int>) and the slot on each connection, in place of_tcpLiveand the fd that travelled next to the connection through the submit and close methods.The table still doesn't shrink. After a burst has gone the walk goes through as many slots as the reactor held at its peak, where main's goes through as many as the process did.
API:
Reactor.EnqueueReturnQIncremental(int fd, ushort gen, ushort bid)was public and is internal, since its first argument is now the slot, which only the runtime has. Handlers give buffers back withTcpConnection.ReturnBufferandReturnBuffers, which pick the queue. GenHTTP, HttpArena's entries and the Playground don't call it. Package validation against 0.29.656, in strict mode, finds no other change.The docs that described the tag and the table by fd are updated (
learn/architecture,learn/dev-core,how-ioxide-does-h3).Measured
On the dev box, pinned to its E-cores, main and this branch in turns.
The walk, timed on
TcpSweepitself (optimized code) with both default timeouts, every connection idle with a read parked, and the caches overwritten in between, as they are between two ticks. Median of 200 ticks and their p99, each the middle one of three runs:What is left is looking at the connections themselves: about 90 ns each, out of memory, with 16 at once.
The request path,
bench/ab.shonTcp/Raw(2 reactors, 64 connections, 6 s runs): main 531,058 req/s and 3.730 µs of CPU per request, this branch 530,124 and 3.736, over 8 rounds. Main against a second checkout of itself: 529,458 and 3.739 against 530,602 and 3.733, over 6. No difference to see.The table at a million connections over 64 reactors: 16,384 entries, 128 KB, per reactor instead of 8 MB.
Not in this PR
Tests
ConnectionSlotTests(Unit, new), on a reactor that never runs: which slot a connection takes, that only its slot and generation together find it, that its slot goes to nobody before it is recycled and the last one freed goes first, growth, and the live count.SlotTests(E2E, new): slots stay low whatever the descriptors; connections one after another reuse the same few; the table grows under 1,300 open connections; under churn, with resets and handlers that flush and let go off the reactor, each client gets its own answer, in both recv modes. The cancels behind a failed send and behind a full recv queue reach their own connection's recv, which no test covered.HardeningTests(a recycled connection's stale recv completion) made sure the next connection got the same descriptor. That completion names the slot now, so the case is about that, without the descriptor filling. It still fails when generations repeat.27 mutants in a separate checkout, all caught: a slot never freed, freed before the recycle, never taken again, or taken first-freed first; an untracked connection still found; the generation ignored or repeating; the growth check off by one; the live count forgetting the recycled or what is off the table; a recv, send or sendmsg tagged with the descriptor, or submitted on the slot; the recycle's close or the stop's abort made on the slot; either walk missing the last slot; a flush, either kind of buffer return, a deferred shutdown or a starved recv queued under the descriptor; and either recv cancel naming it.
SlotTestswithHardeningTestspassed 12 of 12 repeat runs, and 8 of 8 pinned to two cores and to one.tlsmodule on this box)Overlap
git merge-tree --write-treeagainst every open PR: this adds no conflict, in files or in hunks.Program.cs. None uses a member this changes. tcp: TcpOptions.MaxConnections sheds an accept whose descriptor number reaches it, in both modes #363 (TcpOptions.MaxConnections, by descriptor number) compiles merged onto this branch; a cap by count would now be a comparison withTcpLive.