Skip to content

Conversation

jrajahalme
Copy link
Member

@jrajahalme jrajahalme commented Dec 27, 2024

Reduce allocations in policy map computation by:

  • Change the container/bitlpm.Trie type to use Key[K] as a type constraint instead of the stored key type. This allows the actual key type to be stored by value, reducing needed memory allocations and indirection.

  • Remove the Iterator interface type and return the iterators by value so that they can be stored in the caller's stack instead of in the heap. This change makes the use of the explicit iterators faster than the callback based alternatives.

  • Remove the tracking of MapStateOwners made redundant by the change of FQDN identities to not mutate their labels (#32769)

    • Mutation of reserved:host identity is still possible and is handled by not issuing incremental deletion for the change and informing the caller (ipcache) to trigger forced policy regenerations in this case.
  • Preallocate policy MapState map using the size of the previous policy map.

These changes speed up the CIDR deny policy benchmark
(BenchmarkRegenerateCIDRDenyPolicyRules) by ~36%, reduces the memory
needed by ~67%, and reduces the number of heap allocations by ~85%.

These changes bring the BenchmarkRegenerateCIDRDenyPolicyRules in v1.17 to be ~10000x faster than in v1.16, with 99,5% reduction in memory consumption (99,6% reduction in the number of heap allocations).

BenchmarkRegenerateCIDRPolicyRules (without the deny rules) is now ~3.6x faster than v1.16, with 80% reduction in memory consumption (99% reduction in the number of heap allocations).

@jrajahalme jrajahalme added sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. needs-backport/1.17 This PR / issue needs backporting to the v1.17 branch labels Dec 27, 2024
@jrajahalme jrajahalme requested review from a team as code owners December 27, 2024 00:21
@maintainer-s-little-helper maintainer-s-little-helper bot added dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. labels Dec 27, 2024
@jrajahalme jrajahalme requested review from nathanjsweet and removed request for derailed December 27, 2024 00:21
@jrajahalme jrajahalme added release-note/misc This PR makes changes that have no direct user impact. release-blocker/1.17 This issue will prevent the release of the next version of Cilium. labels Dec 27, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Dec 27, 2024
@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme changed the title bitlpm: reduce allocs by avoiding using interface types bit: reduce allocs by avoiding use of interface types Dec 27, 2024
@jrajahalme
Copy link
Member Author

small cleanup

@jrajahalme
Copy link
Member Author

/test

@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme changed the title bit: reduce allocs by avoiding use of interface types bitlpm: reduce allocs by avoiding use of interface types Dec 31, 2024
@jrajahalme
Copy link
Member Author

/test

@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme changed the title bitlpm: reduce allocs by avoiding use of interface types policy: reduce allocs by avoiding use of interface types, retire MapStateOwners Jan 2, 2025
@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme force-pushed the bitlpm-reduce-allocs branch from 44fdb85 to f788f4a Compare January 2, 2025 07:14
@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme force-pushed the bitlpm-reduce-allocs branch from f788f4a to c06c59c Compare January 6, 2025 11:26
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 15, 2025
@jrajahalme jrajahalme added this pull request to the merge queue Jan 15, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 15, 2025
@jrajahalme jrajahalme added this pull request to the merge queue Jan 16, 2025
Use Key[K] as a type constraint, rather than the type of the stored key,
or the key parameter. With this change the generic type K within the type
and function parameters is the actual key type, rather than an interface,
avoiding indirection and taking less space.

Nil checks can no longer performed on the key parameter as it is no
longer an interface. If 'K' is a pointer type, its implementations of
Key[K] interface functions must accept nil receiver and perform nil
checks itself. If 'K' is small it should be passed by value for better
performance as this avoids memory indirections.

This change alone speeds up the CIDR deny policy benchmark
(BenchmarkRegenerateCIDRDenyPolicyRules): by ~12%, reduces the memory
needed by ~10%, and reduces the number of heap allocations by ~50%.

This change is also needed for removal of the Iterator interface in a
later commit.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Remove the Iterator interface and return the implementation of the
iterator directly by value. This removes a heap allocation for each
iteration, as the iterator is allocated in the stack of the calling
function.

This change speeds up the CIDR deny policy benchmark
(BenchmarkRegenerateCIDRDenyPolicyRules) by ~12%, reduces the memory
needed by ~13%, and reduces the number of heap allocations by ~65%.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Fix mispelling of 'mapStateEntry' and commenta in LPMAncestors().

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
The set of MapStateEntry owners was added to keep track of which
selectors need a specific MapStateEntry with a specific identity to be
present, so that when an identity is incrementally deleted from one
selector, the MapStateEntry is actually deleted when the identity is
removed from all the selectors that had it. Now that selector cache is
transactional we can rely on any deleted identity to be removed from all
selectors within the same incremental map change. Thus we no longer need
to track the MapStateOwners.

This change speeds up the CIDR deny policy benchmark
(BenchmarkRegenerateCIDRDenyPolicyRules) by ~10%, reduces the memory
needed by ~25%, and reduces the number of heap allocations by ~8%.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
The host identity can be mutated, e.g., when the kube-apiserver is
removed from the local host. When this happens, the
'reserved:kube-apiserver' label is removed from the host identity (1),
causing any selectors selecting the 'reserved:kube-apiserver' label to no
longer select the host identity (1). Since we no longer have any
reference counting (such as owner tracking) in the policy map state, we
must block the incremental deletion of any such mutated identities to keep
traffic to/from that identity allowed in case some other selector may be
still allowing such traffic.

For this strategy to work, the entity mutating the host identity must
also trigger forced policy regenerations for all endpoints so that the
policy map entries with the host identity are removed if actually not
required due to any other selectors.

To implement this in a generic was we add a "mutated" boolean return
value to SelectorCache.UpdateIdentities function. This function is
already explicitly aware of such mutated identities. Instead of informing
the SelectorCache user's of a deleted identity in this case, we simply
just update the cached selector that no longer selects the mutated
identity and return "mutated = true" to inform the caller to trigger
forced policy updates on all endpoints when this happens.

IPCache metadata logic is changed to make note of the new "mutated"
return value and trigger policy updates when 'true' is returned.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 16, 2025
Reduce allocations and memory use by preallocating the mapstate map with
the capacity of the size of the previous map. This minimizes allocations
for policy updates, but also means that the policy map shrinks only after
the 2nd policy recompute.

This change speeds up the CIDR deny policy benchmark
(BenchmarkRegenerateCIDRDenyPolicyRules) by ~9%, reduces the memory
needed by ~44%, and reduces the number of heap allocations by ~4%.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Remove unused 'epLabels' parameter from 'policyDistillery.distillPolicy()'.

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
@jrajahalme jrajahalme force-pushed the bitlpm-reduce-allocs branch from 6b191c4 to bd65c20 Compare January 16, 2025 11:40
@jrajahalme
Copy link
Member Author

Fixed conflict with main.

@jrajahalme
Copy link
Member Author

/test

@jrajahalme jrajahalme enabled auto-merge January 16, 2025 11:41
@jrajahalme jrajahalme added this pull request to the merge queue Jan 16, 2025
Merged via the queue into cilium:main with commit 2c3347b Jan 16, 2025
105 of 109 checks passed
@jrajahalme jrajahalme deleted the bitlpm-reduce-allocs branch January 16, 2025 13:18
@rastislavs rastislavs mentioned this pull request Jan 21, 2025
45 tasks
@rastislavs rastislavs added backport-pending/1.17 The backport for Cilium 1.17.x for this PR is in progress. and removed needs-backport/1.17 This PR / issue needs backporting to the v1.17 branch labels Jan 21, 2025
@github-actions github-actions bot added backport-done/1.17 The backport for Cilium 1.17.x for this PR is done. and removed backport-pending/1.17 The backport for Cilium 1.17.x for this PR is in progress. labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-done/1.17 The backport for Cilium 1.17.x for this PR is done. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-blocker/1.17 This issue will prevent the release of the next version of Cilium. release-note/misc This PR makes changes that have no direct user impact. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants