Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8260332: ParallelGC: Cooperative pretouch for oldgen expansion #2976

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

@amitdpawar
Copy link
Contributor

@amitdpawar amitdpawar commented Mar 12, 2021

In case of ParallelGC, oldgen expansion can happen during promotion. Expanding thread will touch the pages and can't request for task execution as this GC thread is already executing a task. The expanding thread holds the lock on "ExpandHeap_lock" to resize the oldgen and other threads may wait for their turn. This is a blocking call.

This patch changes this behavior by adding another constructor in "MutexLocker" class to enable non blocking or try_lock operation. This way one thread will acquire the lock and other threads can join pretouch work. Threads failed to acquire the lock will join pretouch only when task is marked ready by expanding thread.

Following minimum expansion size are seen during expansion.

  1. 512KB without largepages and without UseNUMA.
  2. 64MB without largepages and with UseNUMA,
  3. 2MB (on x86) with large pages and without UseNUMA,
  4. 64MB without large pages and with UseNUMA.

When Oldgen is expanding repeatedly with smaller size then this change wont help. For such cases, resize size should adapt to application demand to make use of this change. For example if application nature triggers 100 expansion with smaller sizes in same GC then it is better to increase the expansion size during each resize to reduce the number of resizes. If this patch is accepted then will plan to fix this case in another patch.

Jtreg all test passed.

Please review this change.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8260332: ParallelGC: Cooperative pretouch for oldgen expansion

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/2976/head:pull/2976
$ git checkout pull/2976

Update a local copy of the PR:
$ git checkout pull/2976
$ git pull https://git.openjdk.java.net/jdk pull/2976/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 2976

View PR using the GUI difftool:
$ git pr show -t 2976

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/2976.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Mar 12, 2021

👋 Welcome back amitdpawar! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr label Mar 12, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Mar 12, 2021

@amitdpawar The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot label Mar 12, 2021
@mlbridge
Copy link

@mlbridge mlbridge bot commented Mar 12, 2021

@mlbridge
Copy link

@mlbridge mlbridge bot commented Mar 13, 2021

Mailing list message from David Holmes on hotspot-dev:

Hi Amit,

On 13/03/2021 6:01 am, Amit Pawar wrote:

In case of ParallelGC, oldgen expansion can happen during promotion. Expanding thread will touch the pages and can't request for task execution as this GC thread is already executing a task. The expanding thread holds the lock on "ExpandHeap_lock" to resize the oldgen and other threads may wait for their turn. This is a blocking call.

This patch changes this behavior by adding another constructor in "MutexLocker" class to enable non blocking or try_lock operation. This way one thread will acquire the lock and other threads can join pretouch work. Threads failed to acquire the lock will join pretouch only when task is marked ready by expanding thread.

Sorry but I do not like the change to MutexLocker - a tryLock is only
maybe locking the mutex and the fact you have to return a value to
indicate whether it managed to lock or not, strikes me as a bad use of a
RAII style of object. I think explicit lock/try_lock/unlock would be
much clearer here.

Thanks,
David

@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented Mar 14, 2021

Thank you for your feedback. I have updated as per your suggestion and fixed build issues on some targets. Please check.

thread_num = Atomic::sub(&_n_threads, 1u);

if (thread_num == 0) {
Atomic::release_store(&_cur_addr, _end_addr);

This comment has been minimized.

@dholmes-ora

dholmes-ora Mar 14, 2021
Member

The release is unnecessary given the Atomic::sub.

This comment has been minimized.

@kimbarrett

kimbarrett Mar 15, 2021

Besides what David said, I think this is also confusing. I think it's to account for the claim (using fetch_and_add) unconditionally adding the chunk size to cur_addr, which could lead to overshoot. I think it would be clearer to prevent the overshoot from occurring.

This comment has been minimized.

@amitdpawar

amitdpawar Mar 16, 2021
Author Contributor

David, will update as suggested.

Kim, thanks for your feedback. Updating _cur_addr was not needed here but thought to keep the markers in clean state. Again, _cur_addr & _end_addr gets updated before the start of next pretouch resize so will remove this. If something breaks then will fix as suggested. I hope this should be OK.

@mlbridge
Copy link

@mlbridge mlbridge bot commented Mar 15, 2021

Mailing list message from Kim Barrett on hotspot-dev:

On Mar 12, 2021, at 3:01 PM, Amit Pawar <github.com+71302734+amitdpawar at openjdk.java.net> wrote:

In case of ParallelGC, oldgen expansion can happen during promotion. Expanding thread will touch the pages and can't request for task execution as this GC thread is already executing a task. The expanding thread holds the lock on "ExpandHeap_lock" to resize the oldgen and other threads may wait for their turn. This is a blocking call.

This patch changes this behavior by adding another constructor in "MutexLocker" class to enable non blocking or try_lock operation. This way one thread will acquire the lock and other threads can join pretouch work. Threads failed to acquire the lock will join pretouch only when task is marked ready by expanding thread.

Following minimum expansion size are seen during expansion.
1. 512KB without largepages and without UseNUMA.
2. 64MB without largepages and with UseNUMA,
3. 2MB (on x86) with large pages and without UseNUMA,
4. 64MB without large pages and with UseNUMA.

When Oldgen is expanding repeatedly with smaller size then this change wont help. For such cases, resize size should adapt to application demand to make use of this change. For example if application nature triggers 100 expansion with smaller sizes in same GC then it is better to increase the expansion size during each resize to reduce the number of resizes. If this patch is accepted then will plan to fix this case in another patch.

Sorry, but a change like this needs better motivation. What you say
above suggests this change doesn't actually help.

It's intentional that oldgen expansions aren't generally large, as the
oldgen shouldn't be grown unnecessarily. There are already parameters
such as MinHeapDeltaBytes to control and manipulate this.

It is also preferable to complete an expansion request quickly to make
the additional space available to other threads in the main allocation
path, rather than making them go to the expand path. Making expansions
larger could force more threads to take the slower expand path, which
doesn't seem like a win even if they then help with the pretouch part
of another thread's expansion. (And that also assumes UsePreTouch is
even enabled.)

So the followup change that you say is needed to make this one
profitable seems questionable.

The proposed change is also surprisingly large and intrusive for
something that seems like it should be very localized.

Jtreg all test passed.

A change like this needs a lot more testing than that, both functionally
and performance.

// Lets help expanding thread to pretouch the memory.
while (!pretouch()->is_task_done()) {
if (pretouch()->is_task_ready()) {
pretouch()->work(Thread::current()->osthread()->thread_id());

This comment has been minimized.

@kimbarrett

kimbarrett Mar 15, 2021

worker thread_id and os thread_id are entirely different things. This is another indication that reusing (abusing) PretouchTask in this way is a mistake.

This comment has been minimized.

@amitdpawar

amitdpawar Mar 16, 2021
Author Contributor

Is following change OK ?
pretouch()->work(static_cast<AbstractGangWorker*>(Thread::current())->id());

otherwise please suggest.

@@ -44,6 +46,7 @@ class PSOldGen : public CHeapObj<mtGC> {
// Performance Counters
PSGenerationCounters* _gen_counters;
SpaceCounters* _space_counters;
PretouchTask* _pretouch; // Used when old gen resized during scavenging.

This comment has been minimized.

@kimbarrett

kimbarrett Mar 15, 2021

I think abusing PretouchTask in this way, completely outside the workgang protocol, is confusing and shouldn't be done. There might be some code that could be shared between this use and PretouchTask, but if so then it should be factored out for such sharing, rather than mangling PretouchTask in the way being proposed.

This comment has been minimized.

@amitdpawar

amitdpawar Mar 16, 2021
Author Contributor

Thanks for your suggestion. It will be better if you can give some direction as G1GC also need similar fix during the expansion. I think G1 has array of regions to be touched during the expansion and that task needs to be shared across the threads. pretouch class can be put inside another class and that can be shared across the threads to synchronize the pretouch task and not sure whether that will be OK again. I explored this approach after your suggestion and thought to update after the feedback during the review process. Please suggest.

src/hotspot/share/gc/shared/pretouchTask.cpp Outdated Show resolved Hide resolved
thread_num = Atomic::sub(&_n_threads, 1u);

if (thread_num == 0) {
Atomic::release_store(&_cur_addr, _end_addr);

This comment has been minimized.

@kimbarrett

kimbarrett Mar 15, 2021

Besides what David said, I think this is also confusing. I think it's to account for the claim (using fetch_and_add) unconditionally adding the chunk size to cur_addr, which could lead to overshoot. I think it would be clearer to prevent the overshoot from occurring.

@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented Mar 16, 2021

Mailing list message from Kim Barrett on hotspot-dev:

On Mar 12, 2021, at 3:01 PM, Amit Pawar <github.com+71302734+amitdpawar at openjdk.java.net> wrote:
In case of ParallelGC, oldgen expansion can happen during promotion. Expanding thread will touch the pages and can't request for task execution as this GC thread is already executing a task. The expanding thread holds the lock on "ExpandHeap_lock" to resize the oldgen and other threads may wait for their turn. This is a blocking call.
This patch changes this behavior by adding another constructor in "MutexLocker" class to enable non blocking or try_lock operation. This way one thread will acquire the lock and other threads can join pretouch work. Threads failed to acquire the lock will join pretouch only when task is marked ready by expanding thread.
Following minimum expansion size are seen during expansion.

  1. 512KB without largepages and without UseNUMA.
  2. 64MB without largepages and with UseNUMA,
  3. 2MB (on x86) with large pages and without UseNUMA,
  4. 64MB without large pages and with UseNUMA.
    When Oldgen is expanding repeatedly with smaller size then this change wont help. For such cases, resize size should adapt to application demand to make use of this change. For example if application nature triggers 100 expansion with smaller sizes in same GC then it is better to increase the expansion size during each resize to reduce the number of resizes. If this patch is accepted then will plan to fix this case in another patch.

Sorry, but a change like this needs better motivation. What you say
above suggests this change doesn't actually help.

It's intentional that oldgen expansions aren't generally large, as the
oldgen shouldn't be grown unnecessarily. There are already parameters
such as MinHeapDeltaBytes to control and manipulate this.

It is also preferable to complete an expansion request quickly to make
the additional space available to other threads in the main allocation
path, rather than making them go to the expand path. Making expansions
larger could force more threads to take the slower expand path, which
doesn't seem like a win even if they then help with the pretouch part
of another thread's expansion. (And that also assumes UsePreTouch is
even enabled.)

So the followup change that you say is needed to make this one
profitable seems questionable.

The proposed change is also surprisingly large and intrusive for
something that seems like it should be very localized.

Jtreg all test passed.

A change like this needs a lot more testing than that, both functionally
and performance.

https://bugs.openjdk.java.net/browse/JDK-8254699 contains test results in XL file to show PreTouchParallelChunkSize was recently changed from 1GB to 4MB on Linux after testing various sizes. I have downloaded the same XL file and same is updated for Oldgen case during resize and it gives some rough idea about the improvement for this fix and follow up fix. Please check "PretouchOldgenDuringResize" sheet for "Co-operative Fix" and "Adaptive Resize Fix" columns.

PreTouchParallelChunkSize_TestResults_UpdatedForOldGenCase.xlsx

Running SPECJbb composite shows 30-40% reduction in GC pause time when old-gen expands upto ~1GB for UseNUMA case (I tested for minimum oldgen size to trigger the resize). Non UseNUMA case will show improvement only when resize expands more than minimum "PreTouchParallelChunkSize" size to let other thread participate in pretouch work. Two cases 1 & 3 (without UseNUMA and with/without hugpages) above didnt shows any improvement because of "PreTouchParallelChunkSize" limit and that is the reason why I suggested another fix.

The "Adaptive Resize Fix" column in the sheet is for next suggested fix and may possibly help to improve further. For server JVM, expansion size of 512KB, 2MB (hugepages) and 64MB looks good for first resize but later needs some attention I think. JVM flag "MinHeapDeltaBytes" needs to be known by the user and need to set it upfront. I think this can be consider for first resize in every GC and later dynamically go for higher size like double the previous size to adopt to application nature. This way it may help to reduce the GC pause time during the expansion. I thought to share my observation and my understanding could be wrong. So please check and suggest.

Again, thanks for your feedback.

@kimbarrett
Copy link

@kimbarrett kimbarrett commented Mar 22, 2021

https://bugs.openjdk.java.net/browse/JDK-8254699 contains test results in XL file to show PreTouchParallelChunkSize was recently changed from 1GB to 4MB on Linux after testing various sizes. I have downloaded the same XL file and same is updated for Oldgen case during resize and it gives some rough idea about the improvement for this fix and follow up fix. Please check "PretouchOldgenDuringResize" sheet for "Co-operative Fix" and "Adaptive Resize Fix" columns.

PreTouchParallelChunkSize_TestResults_UpdatedForOldGenCase.xlsx

I'm not sure how to interpret this. In particular, it says "Test done using November 27rd Openjdk build". That predates a number of recent changes in this area that seem relevant. Is that correct? Or is that stale and the data has been updated for a newer baseline.

Also, can you provide details about how this information is generated and collected? I would like to reproduce the results and do some other experiments.

I think that, as proposed, the change is making already messy code still more messy, and some refactoring is needed. I don't know what the details of that might look like yet; I'm still exploring and poking at the code. I also need to look at the corresponding part of G1 that you mentioned.

The "Adaptive Resize Fix" column in the sheet is for next suggested fix and may possibly help to improve further. For server JVM, expansion size of 512KB, 2MB (hugepages) and 64MB looks good for first resize but later needs some attention I think. JVM flag "MinHeapDeltaBytes" needs to be known by the user and need to set it upfront. I think this can be consider for first resize in every GC and later dynamically go for higher size like double the previous size to adopt to application nature. This way it may help to reduce the GC pause time during the expansion. I thought to share my observation and my understanding could be wrong. So please check and suggest.

I think something like this has a serious risk of growing the oldgen a lot more than needed, which may have serious downsides.

@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented Mar 22, 2021

Thank you Kim for your reply and please see my inline comments.

https://bugs.openjdk.java.net/browse/JDK-8254699 contains test results in XL file to show PreTouchParallelChunkSize was recently changed from 1GB to 4MB on Linux after testing various sizes. I have downloaded the same XL file and same is updated for Oldgen case during resize and it gives some rough idea about the improvement for this fix and follow up fix. Please check "PretouchOldgenDuringResize" sheet for "Co-operative Fix" and "Adaptive Resize Fix" columns.
PreTouchParallelChunkSize_TestResults_UpdatedForOldGenCase.xlsx

I'm not sure how to interpret this. In particular, it says "Test done using November 27rd Openjdk build". That predates a number of recent changes in this area that seem relevant. Is that correct? Or is that stale and the data has been updated for a newer baseline.

Sorry for the confusion. I didnt test again and pre-touch time taken with different chunk size per thread was already recorded in the spreadsheet and thought to use it for reference to reply to David feedback "A change like this needs a lot more testing than that, both functionally and performance.".

Also, can you provide details about how this information is generated and collected? I would like to reproduce the results and do some other experiments.

JDK-8254699 was fixed by me and test was done using SPECJbb composite with following command along with JVM flag "PreTouchParallelChunkSize" with values 1G (earlier default on x86) and 4M (current default on x86). To generate this info, "Ticks::now()" function was called to record the time taken for pretouch operation. The first table (please ignore resize related) in the sheet "SPECjbb_Summary" was created from the sheet "SPECjbb_128C256T_GCPretouchTime" that contains pretouch log dumps for SPECjbb composite tests.

Command (also mentioned in SPECjbb_128C256T_GCPretouchTime sheet):
-Xms24g -Xmx970g -Xmn960g -server -XX:+UseParallelGC -XX:+AlwaysPreTouch -XX:+UseLargePages -XX:LargePageSizeInBytes=2m -XX:MaxTenuringThreshold=15 -XX:+ScavengeBeforeFullGC -XX:+UseAdaptiveSizePolicy -XX:ParallelGCThreads=256 -XX:-UseBiasedLocking -XX:SurvivorRatio=200 -XX:TargetSurvivorRatio=95 -XX:+UseNUMA -XX:-UseNUMAInterleaving -XX:+UseTransparentHugePages -XX:-UseAdaptiveNUMAChunkSizing

I think that, as proposed, the change is making already messy code still more messy, and some refactoring is needed. I don't know what the details of that might look like yet; I'm still exploring and poking at the code. I also need to look at the corresponding part of G1 that you mentioned.

First, I thought to push this change for ParallelGC and next G1GC. Earlier ParallelGC pretouch was done with single threaded and later this was fixed by moving multithreaded pretouch common code from G1GC and ParallelGC to PretouchTask class. I thought to do it similarly for this case too.

The "Adaptive Resize Fix" column in the sheet is for next suggested fix and may possibly help to improve further. For server JVM, expansion size of 512KB, 2MB (hugepages) and 64MB looks good for first resize but later needs some attention I think. JVM flag "MinHeapDeltaBytes" needs to be known by the user and need to set it upfront. I think this can be consider for first resize in every GC and later dynamically go for higher size like double the previous size to adopt to application nature. This way it may help to reduce the GC pause time during the expansion. I thought to share my observation and my understanding could be wrong. So please check and suggest.

I think something like this has a serious risk of growing the oldgen a lot more than needed, which may have serious downsides.

I believe this depends on application runtime nature and it may expand as needed. If you still think that this change is not useful then please suggest. Will withdraw this PR.

Thanks,
Amit

@kimbarrett
Copy link

@kimbarrett kimbarrett commented Mar 29, 2021

PreTouchParallelChunkSize_TestResults_UpdatedForOldGenCase.xlsx

I'm not sure how to interpret this. In particular, it says "Test done using November 27rd Openjdk build". That predates a number of recent changes in this area that seem relevant. Is that correct? Or is that stale and the data has been updated for a newer baseline.

Sorry for the confusion. I didnt test again and pre-touch time taken with different chunk size per thread was already recorded in the spreadsheet and thought to use it for reference to reply to David feedback "A change like this needs a lot more testing than that, both functionally and performance.".

Getting any benefit from parallel pretouch here suggests we have many threads piling up on the expand mutex. Before JDK-8260045 (pushed together with JDK-8260044 on 2/12/2021) that would lead to "expansion storms", where several threads piled up on that mutex and serially entered and did a new expansion (with associated pretouch, and possibly of significant size). Getting rid of the expansion storms may alleviate the serial pretouch quite a bit. There may still be some benefit to cooperative parallization, but new measurements are needed to determine how much of a problem still exists.

If it is still worth addressing, I think the proposed approach has problems, as it makes messy complicated code messier and more complicated. I think some preliminary refactoring is needed. For example, splitting MutableSpace::initialize into a setup-pages part and set-the-range part. I've not explored in detail what's needed yet, pending new measurements. (I haven't had time to do those measurements yet myself.)

@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented Apr 5, 2021

Thanks Kim for your feedback.

I have updated the code and it looks good now. Please use "Full" diff to see all the changes.

Summary of changes: added new class in pretouchTask.hpp file for synchronization. Now co-operative pretouch is enabled for both ParallelGC and G1GC. For testing purpose, added new flag "UseMultithreadedPretouchForOldGen" and timing is enabled for old-gen pretouch case for measurements. Enabling this flag makes try_lock to be used instead of lock (which is current default).

This change will be useful only if PreTouchParallelChunkSize value is not very huge compared to old-gen expansion size. For Linux it is set to 4MB and for other OS it is still 1GB.

Testing SPECJbb composite on AMD EPYC machine shows following improvement.

  • Parallel GC: with UseNUMA, minimum expansion size is 64MB
    -- with 2MB hugepages, master branch time taken is ~9-10ms and current fix time taken is ~1-2ms`
    -- without hugepages, master branch time taken is ~14-17ms and current fix time taken is ~2-3ms

  • Parallel GC: without UseNUMA, minimum expansion size is 512KB so this change is not useful for this size. Sometime expansion goes upto 20MB then pretouch takes less time for such large size.

  • G1 GC: with UseNUMA, maximum expansion size is 32MB
    -- with 2MB hugepages, master branch time taken is ~5-7ms and current fix time taken is ~1-7ms` (higher time due to single thread activity as there was no other thread waiting to acquire the lock)

  • G1 GC: without UseNUMA, minimum expansion size 64KB and maximum expansion size is 32MB
    -- without hugepages, master branch time taken = ~9-11ms and current fix time taken ~2-9ms (same here too)

Please check.

…uch for

both ParallelGC and G1GC.
@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented Apr 7, 2021

Fixing runtime issue seen in Pre-submit tests. I somehow missed no safepoint check while acquiring the lock in G1 case. Minor issue. Thanks.

…h no

safepoint check as similar to earlier code.
@kimbarrett
Copy link

@kimbarrett kimbarrett commented Apr 27, 2021

Sorry to be slow to respond. I've done some prototyping of an alternative that I think is promising, but have been swamped with other work. It might be a while before I can really spend time on it again.

@amitdpawar
Copy link
Contributor Author

@amitdpawar amitdpawar commented May 4, 2021

Thanks for your reply and good to know that alternative implementation is possible. I will wait. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants