Speed up flow tables and reclaim their memory#995
Open
MorganaFuture wants to merge 2 commits into
Open
Conversation
Replace the `BTreeMap` backing `FlowTable` with a `hashbrown::HashMap` for O(1) lookups at the flow counts front-facing guests reach in practice. At ~4M entries the benchmarks on oxidecomputer#779 show foldhash gets around 33ns versus ~316ns for `BTreeMap`. Each table seeds its own `foldhash` state from the kernel PRNG so a remote party cannot predict the hash sequence and force pathological collisions; under std/test we use a fixed seed for reproducibility. `dump` now sorts its output so flow listings (and the `opteadm` output built from them) stay stable despite the unordered map.
A `HashMap` keeps its high-water-mark allocation after entries are removed, so a churn spike would pin host memory long after the flows went away. Reclaim it on the periodic expiry pass: once a table drains to under a quarter full, shrink toward `2 * len` (with a small floor). Firing only on a deep drain keeps tables that sit near capacity from thrashing and bounds how often the rehash runs under the port lock. This is the "scale down" half of oxidecomputer#779; growth is already handled by the map's own resizing up to the table limit.
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.
Two related improvements toward #779, both internal to
FlowTable.Faster lookups. Swaps the
BTreeMapbackingFlowTablefor ahashbrown::HashMap, giving O(1) lookups at the flow counts front-facing guests reach in practice. The benchmarks gathered on #779 put foldhash gets at ~33ns versus ~316ns forBTreeMapat ~4M entries. Each table seeds its ownfoldhashstate from the kernel PRNG (random_get_pseudo_bytes) so the hash sequence can't be predicted and ground into worst-case collisions by remote traffic; std/test builds use a fixed seed for reproducibility.dumpnow sorts its output so flow listings (and theopteadmview built from them) stay stable despite the unordered map.Reclaim memory after churn. A
HashMapkeeps its high-water-mark allocation after entries are removed, so a churn spike would pin host memory long after the flows went away. The periodic expiry pass now shrinks a table toward2 * lenonce it drains to under a quarter full. Firing only on a deep drain keeps tables near capacity from thrashing and bounds how often the rehash runs under the port lock. This is the "scale down" half of #779; growth is already handled by the map's own resizing up to the table limit.This deliberately leaves the capacity limits and eviction policy untouched, since eviction/lifecycle is being reworked separately.
Tested:
cargo test -p opteand-p oxide-vpcpass; the no_std kernel build (--features engine,kernel) type-checks on the pinned nightly. I have not linked or run the xde kmod on a Helios host.