Repository navigation
Conversation
|
Note that producerConsumer is mainly there for the gc.alloc.rate.norm. The runtimes are coarse, but get the point across; I ran with -Dforks=1 -DwarmupIterations=5 -DmeasureIterations=3 |
|
@ejona86 I like the idea... one thing I am not sure about is how this will work in the scenario where we allocate in one thread and recycle in another thread. In the old implementation it was possible while still get a good "hit count". How do we account for this here ? |
|
@mosesn @bryce-anderson @jasontedor @Scottmitch @vkostyukov @carl-mastrangelo I would like to get your thoughts on this as well. @vkostyukov as you mentioned you disabled the recycler in finagle in #5968 would it be possible to test this one as well ? |
This handles that case by the recycling thread receiving "ownership" of the recycled instance (no pushLater). If that receiving thread needs to allocate in the future, it can use the recycled instance. So if coordinating between Netty threads, it should work well. If the recycling thread won't ever need instances (it's not a producer as well) or there's an imbalance, then no (or limited) reuse will be performed. This is when the 1% sampling kicks in and predominantly uses non-recyclable instances, effectively as if the Recycler wasn't being used. Note that this is opposite to dropHandle() which drops objects when recycling; they are instead created with the inability to be recycled. It would be possible to have a larger shared pool to put things in when a particular generation's list grows too long, or something. But it would need synchronization and Java's GC seems better suited to that case. I didn't entirely follow how |
|
Based on the forewarning by @ejona86 I haven't read the implementation in depth so I can't comment on it one way or the other. In general this sounds like an improvement since it attempts to prevent unnecessary generation migration, but it's hard to say how much. In my limited experience, microbenchmarks haven't done a great job representing how changes that can affect GC behavior will affect real applications. If we think that recyclers are here to stay then let me know and I'll sink the time into a thorough review. Fwiw, we just disabled recyclers (twitter/finagle@b8523a4) as @vkostyukov noted on issue #5968. Maybe he's interested in running some benchmarks of the patch and seeing how they do. |
|
As Bryce mentioned, we decided to disable Recyclers in Finagle as we observed they come at a non-trivial CPU cost. Disabling them increased RPS/core score in our internal Finagle simulations (with a small penalty in allocations/req) - see this comment for more details. I'd be happy to try these changes on our internal setup. Will update this thread when I know more. |
|
@vkostyukov, thanks for offering to try out this change. It'll be useful to know if you used CMS or G1. |
|
@ejona86 yeah thats what I was talking about... I think it can "frequently" happen that allocations happen from a non |
I agree it can happen and isn't necessarily rare, although I do question if it happens frequently enough to make a difference in performance, especially since the code would need some slower paths to handle it. Did the previous Recycler implementation optimize that case? The benchmark makes it seem like not, but the code is not quite clear without me looking deeper. |
|
@ejona86 optimised before for the use case no... handled it yes ;). I just wanted to mention it and it may be not a big deal as you suggested. |
|
@ejona86 could you incorporate the requested changes and @vkostyukov could you do some testings with this ? |
544b5f7 to
5728c6e
Compare
|
I rebased past #8064, but the resulting file should be the same. I did not squash. |
|
@ejona86 @bryce-anderson @vkostyukov @mosesn how can me move this forward ? Any takers ? |
|
Can one of the admins verify this patch? |
|
@netty-bot test this please |
|
@ejona86 can you rebase one last time ? |
Motivation: Recycler is hard to tune and reduces performance in same cases. Inspired by the simplicity and large performance gains seen with Go's sync.Pool, which only reuses objects between GCs, it seemed a more conservative approach could be applied to Recycler. Note that this looks nothing like sync.Pool, as Recycler has _very_ different trade-offs and environment. Modifications: Reimplement Recycler as a weak reference free list, with low rate of recycler-enabled objects and heuristics for reusing older objects. Add a nanobenchmark to better contrast the implementation options, especially with the 'gc' profiler. Result: A Recycler that can improve performance while worst-case would have neglible impact to GC, memory, and CPU use.
5f7d61a to
7436ae1
Compare
|
Rebased past 338ef96 (although I threw away the change to Recycler, because my new implementation didn't have the bug) and squashed commits. |
|
@ejona86 hi,Man, what is your final solution? There is too much content above, I suggest using soft references to solve this problem, don't you think? |
|
don't say more,look this code : private DefaultHandle[] elements ; it was used to save object , it's Strong Reference . thougth it's cache , why not use SoftReference instead it ? such as private SoftReference>[] elements , we can in case of OOM ERROR |
…ementions of an Object pool Motivation: At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052. Modifications: - Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation. - Wrap the Recycler into an ObjectPool and return it for now Result: Preparation for different ObjectPool implementations
…ementions of an Object pool (#9715) Motivation: At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052. Modifications: - Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation. - Wrap the Recycler into an ObjectPool and return it for now Result: Preparation for different ObjectPool implementations
…ementions of an Object pool (#9715) Motivation: At the moment we directly extend the Recycler base class in our code which makes it hard to experiment with different Object pool implementation. It would be nice to be able to switch from one to another by using a system property in the future. This would also allow to more easily test things like #8052. Modifications: - Introduce ObjectPool class with static method that we now use internally to obtain an ObjectPool implementation. - Wrap the Recycler into an ObjectPool and return it for now Result: Preparation for different ObjectPool implementations
|
@ejona86 let me try to push this over the finish line ... /cc @chrisvest any thoughts on this ? |
|
@normanmaurer, I'd be fine rebasing it or the like. It's more of a question "does anyone want it" and "does the code look fine." |
|
@ejona86 i really like the idea and would like to experiment with it |
|
FWIW, G1 is also a lot more common now and there are situations with G1 where the "do no harm" principle doesn't hold. But it still holds promise to be better than the current recycler. |
|
Let me take this over and do some experimental integration |
chrisvest
left a comment
There was a problem hiding this comment.
Had a look at the code. Some parts have me stratching my head, but overall this is a lot simpler that the existing Recycler. Mostly because it no longer attempts to recycle objects across threads. I suspect the current implementation has bugs in that area anyway (e.g. #9640), so I think it's worth to explore the approach given here.
This needs a rebase to fix some conflicts, and more conflicts are in the pipeline with #11037.
| maxId = parent.currentGenId.get(); | ||
| if (maxId == genMaxId[i - 1]) { | ||
| // This is the first thread to detect the new generation | ||
| parent.currentGenId.incrementAndGet(); |
There was a problem hiding this comment.
The check and the increment are not atomic, so isn't this racy?
There was a problem hiding this comment.
It'd be quite fair to use a compareAndSet() here to reduce mental burden and worries.
But I don't think the race matters much as few DefaultHandle()s would be allocated within the race window. It mainly "wastes" an id. I do agree that any DefaultHandles()s allocated during the race window will be handled less than ideal, as they might not be treated as eden in some threads.
When writing the code, I thought of currentGenId as just a clock. The clock needs to tick frequently enough to separate generations, but if it runs too frequently the code would react reasonably. It was so long ago that I can't be certain, but I think I might have purposefully avoided CAS here to help prevent myself from assuming a nice, orderly progression to avoid overly relying on "clean" interactions between threads and make sure I handled "when things go wrong" cases.
There was a problem hiding this comment.
Ok. If it's fine for them race here, then I think it'd be good to call that out in a comment.
| elements[size] = item; | ||
| this.size = size + 1; | ||
| node.next = genHead[i]; | ||
| genHead[i] = node.selfRef; |
There was a problem hiding this comment.
The array business is not obvious to me. Here we are relying on genMaxId.length being one less than genHead.length, which feels shaky.
There was a problem hiding this comment.
They are parallel arrays and need to have that relationship; they are very closely related. We could call it out more in the documentation, if that would help.
There was a problem hiding this comment.
The genMaxId field javadoc comment could be expanded to explain how these two arrays work together. I think that would be useful as this code is a little unusual :)
| } | ||
| handle.hasBeenRecycled = true; | ||
| if ((allocationCount++ & 63) == 0) { | ||
| // Create recyclable objects at a low rate. If they are helpful, they will accumulate |
| // that won't be reused." While more sophisticated methods of following the | ||
| // rule are possible, the class uses a simple approach of making ~1% of the | ||
| // new objects be recyclable. If recycling is helping, the recyclable | ||
| // objects will accumulate. |
|
I really would like to get something like this in for netty 5. |
|
netty5 think use Linux new feature IO_Uring? java aio isn't really asynchronous
…---Original---
From: "Norman ***@***.***>
Date: Wed, Aug 25, 2021 21:31 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [netty/netty] RFC: Reimplement Recycler to reduce impact of pathological cases (#8052)
I really would like to get something like this in for netty 5.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
|
@NickleHuang please don't hi-jack issues... Also see https://github.com/netty/netty-incubator-transport-io_uring |
|
cool。I'm looking forward to it
…---Original---
From: "Norman ***@***.***>
Date: Wed, Aug 25, 2021 22:00 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [netty/netty] RFC: Reimplement Recycler to reduce impact of pathological cases (#8052)
@NickleHuang please don't hi-jack issues... Also see https://github.com/netty/netty-incubator-transport-io_uring
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
|
@normanmaurer, want me to rebase this on 4.1 to avoid the conflicts, or do we think we'll just make the change in 5.0? |
|
Maybe just rebase on 5.0 . |
Also amend it to prevent stalls on benchmark shut down.
Motivation: We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler. The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about. It is unwise to leave suspicous-looking code unaddressed. Concrete issues, like #11864, should also be addressed. Modification: The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references. The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity. There is also no more scavenging. It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads. Each thread now has a local pool (an MPSC queue). Calls to get() and recycle() in the same thread will just operate on the local pool queue. Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from. We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler. The constructors have been modified to deprecate the many configurations that are no longer relevant. A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter. The queues will grow and shrink in units of this chunk size. The benchmark from #8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises. Performance is roughly the same, and allocation rate is down a notch in all the benchmarks. Result: The Recycler code is much easier to understand.
Motivation: We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler. The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about. It is unwise to leave suspicous-looking code unaddressed. Concrete issues, like #11864, should also be addressed. Modification: The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references. The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity. There is also no more scavenging. It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads. Each thread now has a local pool (an MPSC queue). Calls to get() and recycle() in the same thread will just operate on the local pool queue. Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from. We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler. The constructors have been modified to deprecate the many configurations that are no longer relevant. A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter. The queues will grow and shrink in units of this chunk size. The benchmark from #8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises. Performance is roughly the same, and allocation rate is down a notch in all the benchmarks. Result: The Recycler code is much easier to understand.
Motivation: We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler. The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about. It is unwise to leave suspicous-looking code unaddressed. Concrete issues, like netty#11864, should also be addressed. Modification: The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references. The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity. There is also no more scavenging. It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads. Each thread now has a local pool (an MPSC queue). Calls to get() and recycle() in the same thread will just operate on the local pool queue. Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from. We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler. The constructors have been modified to deprecate the many configurations that are no longer relevant. A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter. The queues will grow and shrink in units of this chunk size. The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises. Performance is roughly the same, and allocation rate is down a notch in all the benchmarks. Result: The Recycler code is much easier to understand.
Motivation: We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler. The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about. It is unwise to leave suspicous-looking code unaddressed. Concrete issues, like netty#11864, should also be addressed. Modification: The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references. The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity. There is also no more scavenging. It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads. Each thread now has a local pool (an MPSC queue). Calls to get() and recycle() in the same thread will just operate on the local pool queue. Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from. We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler. The constructors have been modified to deprecate the many configurations that are no longer relevant. A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter. The queues will grow and shrink in units of this chunk size. The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises. Performance is roughly the same, and allocation rate is down a notch in all the benchmarks. Result: The Recycler code is much easier to understand.
|
Let me close this for now |
Motivation: We have in the past gotten reports of weird errors that we haven't been able to reproduce, but which could be explained by bugs in the Recycler. The reason is that the Recycler was mixing concurrency, and interactions with the GC, in ways that were difficult to reason about. It is unwise to leave suspicous-looking code unaddressed. Concrete issues, like netty#11864, should also be addressed. Modification: The Recycler has been rewritten to use a much simpler algorithm, and now completely avoids the use of weak references. The WeakOrderQueue is gone, and there is no longer a concept of a shared max capacity - only thread-local capacity. There is also no more scavenging. It turns out that other code was relying on the ability to forget references to objects they acquired from a Recycler, and this is hard to support if there is a shared pool for moving handles between threads. Each thread now has a local pool (an MPSC queue). Calls to get() and recycle() in the same thread will just operate on the local pool queue. Calls to recycle() from a thread that did not get() that object (the object was acquired in another thread) will rely on the multi-producer safety of the queue to directly release it back to the local pool that the object came from. We rely on setting an upper bound for the queue sizes, and the fallible offer() method, for restricting the max thread-local capacity of the recycler. The constructors have been modified to deprecate the many configurations that are no longer relevant. A new configuration parameter, the queue chunk-size, is exposed as a constructor parameter. The queues will grow and shrink in units of this chunk size. The benchmark from netty#8052 is copied over and updated, and shows that these changes help overall, with no hidden surprises. Performance is roughly the same, and allocation rate is down a notch in all the benchmarks. Result: The Recycler code is much easier to understand.
Motivation:
Recycler is hard to tune and reduces performance in same cases. Inspired by the
simplicity and large performance gains seen with Go's sync.Pool, which only
reuses objects between GCs, it seemed a more conservative approach could be
applied to Recycler. Note that this looks nothing like sync.Pool, as Recycler
has very different trade-offs and environment.
Modifications:
Reimplement Recycler as a weak reference free list, with low rate of
recycler-enabled objects and heuristics for reusing older objects. Add a
nanobenchmark to better contrast the implementation options, especially with
the 'gc' profiler.
Result:
A Recycler that can improve performance while worst-case would have neglible
impact to GC, memory, and CPU use.
There's no rush for this and it wouldn't bother me to close it. I couldn't find the issue where we were talking about disabling the Recycler. This PR should not be considered an argument against doing that; this was a "fun thing" that had interesting enough results I thought I should share. I honestly don't know if it's worth anyone's time to look into this as a "solution" to Recycler.
I've only run some nanobenchmarks against this. The "generational" behavior in particular is unproven, although it looks interesting. It's possible it is unnecessary and just a single LIFO linked list would be enough, but I went ahead with it because it was the more conservative approach and shouldn't be any worse than the single list approach.
I was a bit worried about whether having so many weak references could cause GC slowdowns; weak refs aren't something I tend to see at large scale. But I think many such concerns are addressed by the 1% sampling.
Although it didn't show up in the code at all, I should note it is really easy to add statistics to the linked list like its length or how reused the objects were, and then have that available after a GC threw the list away by keeping the stats in the WeakRef.
The new implementation doesn't need to use ObjectCleaner 😄.