GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add the AdaptivePoolingAllocator by chrisvest · Pull Request #13075 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Dec 24, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation:
The current PoolingBufferAllocator has a number of issues. The code has proven complex to maintain over the years, and has caused a steady trickle of bugs despite its age. It also makes use of ThreadLocals, which is problematic when used with VirtualThreads as there might be millions of such threads, making the memory usage of PoolThreadLocalCache prohibitively expensive.

Modification:
Instead of using ThreadLocals, the AdaptivePoolingAllocator reduces contention by striping access across a number of magazines. Each thread will have a preferred magazine to access, computed from their thread ID. We use StampedLocks to tryLock the magazine, and if the lock is not immediately available we will continue on to the next magazine. If there is too much contention (too many tryLocks that fail) then the number of magazines is increased, up to twice the number of CPU cores. This behavior is similar in spirit to Stripe64, though the implementation is quite different.

Each magazine does pointer-bump (using Buffer.split) allocations out of a chunk. The default chunk size is 128 KiB.
However, allocation sizes are continuously recorded in a sliding-window histogram. Every few thousand allocations to a magazine, the 99-percentile allocation size is computed from the histogram, and then multiplied by 10. If this value is greater than 128 KiB, then it will be used as the new preferred chunk size. From then on, chunks that are less than this, or over 50% larger than this, will be retired and the memory will be freed.

The maximum size we can track in the histograms is 1 MiB, and the minimum size is 8192 bytes. The sizes are bucketed into powers of two.
This keeps our memory spent on histograms low. This also means our max chunk size is 10 MiB.
We choose 8192 as the minimum bucket size because we also choose 128 KiB as the minimum chunk size, so sizes of 8192 and smaller will have no impact on the chunk size.
This in turn means we have 8 size buckets, and that allows the JIT to optimize the loops over these arrays quite well.
The sliding window also only keep 4 generations of histograms, and we use shorts for bucket counters since we rotate them every 1.024 to 65.534 allocations.
Because processing the histograms is relatively expensive, we adapt the frequency of this processing depending on whether the computed chunk size changed or not.

The allocator has a central queue where magazines can pluck available chunks. However, magazines also have a nextInLine field, which will be checked first - if it is empty we will instead take a chunk from the central queue, or allocate a new one if the queue was empty. When all of the buffers allocated from a chunk have been closed, then the chunk returns to the allocator. When this happens we first check if the nextInLine field of the originating magazine is null - if it is, we atomically return the chunk to that magazine. Otherwise the chunk is given to the central queue.

The dynamic adjustments we do to the chunk sizes and the number of magazines, in response to the workload, is why this allocator is called adaptive.

The adaptive allocator can be enabled as the Netty default allocator by setting the io.netty5.allocator.type system property to adaptive.

Result:
We have a new pooling allocator with many benefits:

  • It does not rely on thread locals.
  • It uses less memory than the existing pooling allocator in the practical applications that have been observed so far.
  • It is inherently robust to fragmentation as long as buffers are eventually closed.
  • It is faster and (perhaps surprisingly) appear to scale better to multi-threaded workloads than the existing allocator.

These are the benchmark results I get for on-heap buffers, from BufferAllocatorConcurrentBenchmark on an M1 Pro machine:

Benchmark                (size)   Mode  Cnt       Score      Error   Units
allocateReleaseAdaptive   00064  thrpt   20  134000.741 ± 2052.369  ops/ms
allocateReleasePooled     00064  thrpt   20   82131.491 ± 1149.161  ops/ms
allocateReleaseUnpooled   00064  thrpt   20      97.436 ±  109.054  ops/ms

And these are the results for off-heap buffers:

Benchmark                (size)   Mode  Cnt       Score      Error   Units
allocateReleaseAdaptive   00064  thrpt   20  238682.626 ± 8149.701  ops/ms
allocateReleasePooled     00064  thrpt   20   86741.222 ± 1431.465  ops/ms
allocateReleaseUnpooled   00064  thrpt   20    3950.274 ±   25.446  ops/ms

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jchrys, fukutu, reneleonhardt, and laosijikaichele reacted with thumbs up emoji He-Pin reacted with heart emoji
All reactions
  • 👍 4 reactions
  • ❤️ 1 reaction
@chrisvest
chrisvest requested review from ejona86, franz1981, normanmaurer and trustin December 24, 2022 06:52
@chrisvest

chrisvest commented Dec 24, 2022

Copy link
Copy Markdown
Member Author

One issue that's new here is that BufferLeakDetectionTest now takes 13 minutes to complete because there are so many combinations to go through…

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

github-advanced-security[bot]
github-advanced-security AI found potential problems Dec 24, 2022
View reviewed changes
Comment thread buffer/src/main/java/io/netty5/buffer/adapt/AdaptablePoolingAllocator.java Fixed

Uh oh!

There was an error while loading. Please reload this page.

franz1981
franz1981 reviewed Dec 24, 2022
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot wait to review and benchmark it!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

violetagg and jchrys reacted with thumbs up emoji chrisvest reacted with heart emoji
All reactions
  • 👍 2 reactions
  • ❤️ 1 reaction
@franz1981

franz1981 commented Dec 24, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This is a great step forward @chrisvest : the other thing (separated but still related) that would make for a better virtual thread adoption (running IN the Netty event loop, using it as a scheduler) would be to address the arenas locks: I will open it as a separate issue in the new year, explaining why/how it could happen and probably the easiest workaround would be to revert back using synchronized (that will block/pin temporary the carrier thread) but in an ideal world a proper fix should address it fully.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Dec 25, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Another issue with this worth pointing out: preferred chunk size is adapted on a per magazine basis, so if two different magazines compute very different preferred chunk sizes, and then share chunks via the central queue, then they might be unhappy with the chunks in the queue.

There are two ways, that I can see, around that: one is to remove the central queue and stop sharing chunks, but in turn we'll likely need magazines to be able to hold more than just one next-in-line chunk. Maybe that needs to be turned into a queue. The other way is merge the histograms from the different magazines in some way so we can compute a single preferred chunk size for everyone. This is probably an easier performance model to comprehend, but will need to be done without adding too much contention; both rotating histograms and obtaining the preferred chunk size need to remain fast. We also shouldn't allow a single very busy magazine to dominate the data feeding into the computation.

EDIT Solved this by computing chunk sizes locally, and then iterating all magazines and picking the max chunk size.

reneleonhardt reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jchrys
jchrys reviewed Dec 28, 2022
View reviewed changes
Comment thread common/src/main/java/io/netty5/util/internal/PlatformDependent.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

jchrys
jchrys reviewed Dec 30, 2022
View reviewed changes
Comment thread buffer/src/main/java/io/netty5/buffer/adapt/AdaptablePoolingAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch 2 times, most recently from 42b1124 to c6c3744 Compare December 30, 2022 23:05
@chrisvest

chrisvest commented Jan 1, 2023

Copy link
Copy Markdown
Member Author

These are the numbers with the latest optimizations, but this time using the UnsafeBuffer implementation because it responds better to them (it does fewer allocations and makes the numbers more predictable):

Benchmark                                                   (size)   Mode  Cnt       Score      Error   Units
BufferAllocatorConcurrentBenchmark.allocateReleaseAdaptive   00064  thrpt   20  238682.626 ± 8149.701  ops/ms
BufferAllocatorConcurrentBenchmark.allocateReleasePooled     00064  thrpt   20   86741.222 ± 1431.465  ops/ms
BufferAllocatorConcurrentBenchmark.allocateReleaseUnpooled   00064  thrpt   20    3950.274 ±   25.446  ops/ms
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch from 185c259 to 71a0792 Compare January 2, 2023 22:12
@normanmaurer
normanmaurer force-pushed the main branch from 0538f48 to 44f69d4 Compare January 9, 2023 09:38
@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch 2 times, most recently from b20110b to d3c3160 Compare January 20, 2023 15:35
@normanmaurer
normanmaurer force-pushed the main branch from 585adf1 to fc4714b Compare January 26, 2023 09:35
@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch from d3c3160 to d2b20bc Compare February 5, 2023 17:14
@chrisvest chrisvest mentioned this pull request Feb 5, 2023
Try a build with the adaptive allocator on-by-default #13193
Closed
@franz1981 franz1981 mentioned this pull request Feb 8, 2023
Avoid ThreadLocals to be more loom friendly #13191
Closed
franz1981
franz1981 reviewed Feb 8, 2023
View reviewed changes
Comment thread buffer/src/main/java/io/netty5/buffer/adapt/AdaptablePoolingAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

franz1981
franz1981 reviewed Feb 8, 2023
View reviewed changes
Comment thread buffer/src/main/java/io/netty5/buffer/adapt/AdaptablePoolingAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Feb 8, 2023

Copy link
Copy Markdown
Contributor

@chrisvest the results you shared with BufferAllocatorConcurrentBenchmark, how many threads are using?
I would like to check (and probably worth to report it recording some weak telemetry counters) when contention happens and how many attempts are required before allocating - to better understand what's going on

Said that; is a a wonderful model, way simpler to reason about if compared to the original one, already loving it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Feb 8, 2023

Copy link
Copy Markdown
Member Author

@franz1981 I used 8 threads for the concurrent benchmark. The default.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Feb 11, 2023

Copy link
Copy Markdown
Member Author

This might currently have some messed up lifecycle tracking, that will be confusing if there are leaks. I'll have to take a look at that before we merge this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest commented Feb 12, 2023
View reviewed changes
Comment thread buffer/src/main/java/io/netty5/buffer/adapt/AdaptablePoolingAllocator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch from d2b20bc to eeb35c2 Compare February 23, 2023 20:45
@chrisvest
chrisvest requested a review from jchrys February 23, 2023 20:50
chrisvest added 20 commits April 22, 2024 09:28
@chrisvest
Avoid calling tryExpandMagazine in the adaptive allocator, if we're…
ca1b93c
… not going to try allocating from a magazine anyway
@chrisvest
Make sure we don't record the chunk buffer splits in the lifecycle of…
6512e95
… buffers allocated from the adaptable pooling allocator
@chrisvest
Checkstyle fixes
9e48ce7
@chrisvest
Avoid integer overflow risk
6949db8
When allocating chunks, we make them 10 times the preferred chunk size, if this is greater than the minimum chunk size.
We need to limit the size of the buffers that we pool, so that we prevent this 10x multiplication from overflowing int.
It also makes some sense to not pool extremely large buffers.
We use the same max chunk size as the old PooledBufferAllocator.
This gives us a cut-off point of ~100 MiB, where we don't pool buffers if they are bigger than this.
@chrisvest
Correctly infer the max chunk size from the limits of hour size histo…
d892623
…gram
@chrisvest
Fix the histo bucket count calculation
8a490fa
Using a shift instead of a plus meant we got 128 buckets instead of the intended 8.
@chrisvest
Make histogram summation faster
8c015fa
@chrisvest
Make the central queue bounded and use JCTools
263970f
Also add javadocs and allow the central queue construction to be customized.
@chrisvest
Avoid expanding magazines twice on racing calls to tryExpandMagazines
be4b972
@chrisvest
Avoid touching the magazineExpandLock if we already have MAX_STRIPES
6db8e16
@chrisvest
Iterate through the magazines 3 times instead of twice
bdc7bfd
It does not show up in the benchmark that I can see, but checking the stamped locks a few more times should be cheaper than doing an unpooled allocation
@chrisvest
Add an HTTP/2 throughput benchmark
f71f1ad
We measure opening a stream, sending some data back and forth, and closing the stream.
The benchmark is parameterized over different transports, TLS on or off, and using the adaptive vs. pooled buffer allocators.
@chrisvest
Add support for running the Http2ThroughputBenchmark over local chann…
894a6e3
…el transport
@chrisvest
Make the Http2ThroughputBenchmark use the local transport by default
c021c96
@chrisvest
Fix a potential infinite recursion (and stack overflow) when dropping…
d679808
… a chunk buffer while the central queue is full
@chrisvest
Rename the AdaptablePoolingAllocator to AdaptivePoolingAllocator
347acaf
This is how we've been referring to it everywhere else all along.
Let's be consistent and lean into that name.
@chrisvest
Fix some documentation typos
9abdaea
@chrisvest
Move thread-id out of the loop
f2cab92
@chrisvest
Add build with the AdaptiveByteBufAllocator to the build matrix
c93916e
@chrisvest
Include the adaptive allocator in the testsuite permutations
f009dc8
@chrisvest
chrisvest force-pushed the 5x-adaptive-allocator branch from cf77eee to f009dc8 Compare April 22, 2024 16:28
chrisvest added 2 commits April 22, 2024 14:13
@chrisvest
Fix a leak detector false positive in AdaptivePoolingAllocator/PoolDrop
f7e2388
@chrisvest
Make the new test faster
891805b
It does not need to iterate itself several times to recreate the scenario.
@chrisvest

chrisvest commented Apr 23, 2024

Copy link
Copy Markdown
Member Author

This was merged into 4.x, so let's merge it here as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest merged commit 188ead1 into netty:main Apr 23, 2024
@chrisvest
chrisvest deleted the 5x-adaptive-allocator branch April 23, 2024 14:41
@normanmaurer

normanmaurer commented Apr 23, 2024

Copy link
Copy Markdown
Member

Thanks a lot ! This is awesome work

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost ghost mentioned this pull request Sep 3, 2024
Performance Degradation using AdaptiveByteBufAllocator #14272
Closed
@SteNicholas SteNicholas mentioned this pull request Mar 12, 2026
[CELEBORN-2280] Support celeborn.network.memory.allocator.type to specify netty memory allocator apache/celeborn#3625
Closed
SteNicholas added a commit to apache/celeborn that referenced this pull request Mar 14, 2026
@SteNicholas
[CELEBORN-2280] Support celeborn.network.memory.allocator.type to spe…
400e951
…cify netty memory allocator

### What changes were proposed in this pull request?

Support `celeborn.network.memory.allocator.type` to specify netty memory allocator including `AdaptiveByteBufAllocator `.

### Why are the changes needed?

Netty 4.2 introduces `AdaptiveByteBufAllocator` an auto-tuning pooling `ByteBufAllocator` which uses `AdaptivePoolingAllocator` added in netty/netty#13075.

### Does this PR resolve a correctness bug?

No.

### Does this PR introduce _any_ user-facing change?

Introduce `celeborn.network.memory.allocator.type` to specify netty memory allocator.

### How was this patch tested?

CI.

Closes #3625 from SteNicholas/CELEBORN-2280.

Authored-by: SteNicholas <[email protected]>
Signed-off-by: SteNicholas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 left review comments
@trustin trustin Awaiting requested review from trustin
@normanmaurer normanmaurer Awaiting requested review from normanmaurer
@ejona86 ejona86 Awaiting requested review from ejona86
+1 more reviewer
@jchrys jchrys jchrys approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@chrisvest @franz1981 @trustin @nkonev @normanmaurer @jchrys @github-advanced-security
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.