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

8322142: JFR: Periodic tasks aren't orphaned between recordings #17114

Closed

Conversation

carterkozak
Copy link
Contributor

@carterkozak carterkozak commented Dec 14, 2023

This issue is described on the jfr-dev mailing list here:
https://mail.openjdk.org/pipermail/hotspot-jfr-dev/2023-December/005697.html
With a minimal reproducer here:
https://github.com/carterkozak/periodic-event-repro

Currently a draft because I don't have a ticket for this issue, and I haven't had a chance to add a test for this. I have verified that a build with this change resolves the bug I encountered.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8322142: JFR: Periodic tasks aren't orphaned between recordings (Bug - P2)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/17114/head:pull/17114
$ git checkout pull/17114

Update a local copy of the PR:
$ git checkout pull/17114
$ git pull https://git.openjdk.org/jdk.git pull/17114/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 17114

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/17114.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 14, 2023

👋 Welcome back carterkozak! 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
Copy link

openjdk bot commented Dec 14, 2023

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

  • hotspot-jfr

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-jfr hotspot-jfr-dev@openjdk.org label Dec 14, 2023
@carterkozak carterkozak changed the title JFR Periodic tasks aren't orphaned between recordings 8322142: JFR Periodic tasks aren't orphaned between recordings Dec 15, 2023
@egahlin
Copy link
Member

egahlin commented Dec 15, 2023

I need to look more into this, and check things out manually, but what if BatchManager holds a SequencedSet of batches instead (LinkedHashSet).

When active sorted task are iterated and getTask() doesn't return null, the batch is added to batches field. If it already exists, nothing happens, otherwise it is inserted.

I guess we could end up with two batches with same period, but maybe the period could be the key. A TreeMap.

@carterkozak
Copy link
Contributor Author

I'm not certain what cases the BatchManager is optimized for, but I think the path forward depends quite a bit on that. array/ArrayList is difficult to outperform when inputs are small. TreeMap and LinkedHashSet both involve traversing many more pointers. I'm not sure how we want to weigh the benefit of reusing Batch objects between event disablement and reenablement against the higher traversal cost.

If we did move from ArrayList to a SequencedSet, instead of adding the orphaned flag or re-adding the task, we could update the existing null check along these lines:

-                if (batch == null) {
+                if (batch == null || !batches.contains(batch)) {
                    batch = findBatch(task.getPeriod());

@egahlin
Copy link
Member

egahlin commented Dec 15, 2023

I'm not certain what cases the BatchManager is optimized for, but I think the path forward depends quite a bit on that. array/ArrayList is difficult to outperform when inputs are small. TreeMap and LinkedHashSet both involve traversing many more pointers. I'm not sure how we want to weigh the benefit of reusing Batch objects between event disablement and reenablement against the higher traversal cost.

If we did move from ArrayList to a SequencedSet, instead of adding the orphaned flag or re-adding the task, we could update the existing null check along these lines:

-                if (batch == null) {
+                if (batch == null || !batches.contains(batch)) {
                    batch = findBatch(task.getPeriod());

Someone might put 10 000 user-defined events in there.

The purpose of the reuse is not reduce allocation, but to make the period stable. Flipping back and forth should not reset things. I need to run with logging to see if it looks OK. Hard to simulate in the brain.

@egahlin
Copy link
Member

egahlin commented Dec 18, 2023

I don't think we have a complexity issue anymore. I was thinkings tasks/events, but batches should at most be 25.

How would this work? If a batch with the same period exists, we reuse it, otherwise we insert it.

                     batch = findBatch(task.getPeriod());
+                } else {
+                    insertBatch(batch);
                 }
                 batch.add(task);
             }
+    private void insertBatch(Batch batch) {
+        for (Batch b : batches) {
+            if (b.getPeriod() == batch.getPeriod()) {
+                return; // Batch already exists
+            }
+        }
+        batches.add(batch);
+    }
+

@carterkozak
Copy link
Contributor Author

batches should at most be 25

That makes sense, I'd thought you had meant 10,000 events with unique periods :-)

+    private void insertBatch(Batch batch) {
+        for (Batch b : batches) {
+            if (b.getPeriod() == batch.getPeriod()) {
+                return; // Batch already exists
+            }
+        }
+        batches.add(batch);
+    }

I don't think it's correct to assume b.getPeriod() == batch.getPeriod() b is the same instance as batch, though. Consider the following scenario:

  1. Event A is enabled at a 10 second interval
  2. Event A is disabled (the batch is removed from the BatchManager)
  3. Event B is enabled at a 10 second interval (this inserts a new Batch with the same 10 second period)
  4. Event A is enabled again (still using the 10 second period).

In this scenario, Event A will have a Batch reference with the same period as the batch used by Event B, so insertBatches will return without changing the state of the BatchManager. In cases where the period duplicates an existing batch, I think we need register events to the existing batch.

Perhaps we should update Batch findBatch(long period) to Batch findOrInsertBatch(long period, @Nullable Batch maybeCachedBatch) where an existing registered batch is returned if one exists, otherwise maybeCachedBatch is inserted and returned if non-null, falling back to instantiating a new batch (the current behavior when an existing batch cannot be found). What do you think?

Sorry for slow responses today, I've been away from the computer -- I should have time to update this tomorrow.

Cached batches from taks objects may be re-added.
@egahlin
Copy link
Member

egahlin commented Jan 2, 2024

Looks reasonable, but maybe this method signature would be easier to understand for someone that doesn't know the history?

private Batch findBatch(long period, Batch oldBatch) {
  ...
}

@carterkozak
Copy link
Contributor Author

Sounds good to me, I've applied your suggestion.

I'd appreciate if you could give me some guidance on adding a test for this fix. I may be able fit my reproducer (updated with a 1ms period) into the format of the tests in jdk.jfr.api.flightrecorder, but being a periodic test it will require some form of sleep/wait, and be fairly specific to this issue. Please let me know what you think the best path forward is, and I'll put something together :-)

Thanks for your help!

@egahlin
Copy link
Member

egahlin commented Jan 2, 2024

I'd appreciate if you could give me some guidance on adding a test for this fix.

It's hard to test in a reliable way. I would not add a test. Time is better spent fixing other issues than hunting down false positives.

@carterkozak carterkozak marked this pull request as ready for review January 2, 2024 20:44
@carterkozak
Copy link
Contributor Author

That works for me, thank you!

@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 2, 2024
@mlbridge
Copy link

mlbridge bot commented Jan 2, 2024

Webrevs

Copy link
Member

@egahlin egahlin left a comment

Choose a reason for hiding this comment

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

Could you update the title of PR so it matches the bug? I think a ':' is missing after "JFR".

I can sponsor the change.

After it has been integrated into main, it can be backported to JDK 21 and 22.

@carterkozak carterkozak changed the title 8322142: JFR Periodic tasks aren't orphaned between recordings 8322142: JFR: Periodic tasks aren't orphaned between recordings Jan 3, 2024
@openjdk
Copy link

openjdk bot commented Jan 3, 2024

@carterkozak This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8322142: JFR: Periodic tasks aren't orphaned between recordings

Reviewed-by: egahlin

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 96 new commits pushed to the master branch:

  • a8e4229: 8322783: prioritize /etc/os-release over /etc/SuSE-release in hs_err/info output
  • cbe329b: 8321713: Harmonize executeTestJvm with create[Limited]TestJavaProcessBuilder
  • 06dd735: 8322766: Micro bench SSLHandshake should use default algorithms
  • 9ab29f8: 8321718: ProcessTools.executeProcess calls waitFor before logging
  • ba426d6: 8322841: Parallel: Remove unused using-declaration in MutableNUMASpace
  • 18cdc90: 8322801: RISC-V: The riscv path of the debian sysroot had been changed
  • fcf8368: 8322248: Fix inconsistent wording in ElementFilter.typesIn
  • a678416: 8322805: Eliminate -Wparentheses warnings in x86 code
  • 122bc77: 8322758: Eliminate -Wparentheses warnings in C2 code
  • e9e694f: 8320275: assert(_chunk->bitmap().at(index)) failed: Bit not set at index
  • ... and 86 more: https://git.openjdk.org/jdk/compare/692be577385844bf00a01ff10e390e014191569f...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@egahlin) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 3, 2024
@carterkozak
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jan 3, 2024
@openjdk
Copy link

openjdk bot commented Jan 3, 2024

@carterkozak
Your change (at version 4202392) is now ready to be sponsored by a Committer.

@egahlin
Copy link
Member

egahlin commented Jan 3, 2024

/sponsor

@openjdk
Copy link

openjdk bot commented Jan 3, 2024

Going to push as commit 1551928.
Since your change was applied there have been 99 commits pushed to the master branch:

  • b67b71c: 8320707: Virtual thread test updates
  • 7eb25ec: 8322853: Should use ConditionalMutexLocker in NativeHeapTrimmerThread::print_state
  • 539da24: 8322779: C1: Remove the unused counter 'totalInstructionNodes'
  • a8e4229: 8322783: prioritize /etc/os-release over /etc/SuSE-release in hs_err/info output
  • cbe329b: 8321713: Harmonize executeTestJvm with create[Limited]TestJavaProcessBuilder
  • 06dd735: 8322766: Micro bench SSLHandshake should use default algorithms
  • 9ab29f8: 8321718: ProcessTools.executeProcess calls waitFor before logging
  • ba426d6: 8322841: Parallel: Remove unused using-declaration in MutableNUMASpace
  • 18cdc90: 8322801: RISC-V: The riscv path of the debian sysroot had been changed
  • fcf8368: 8322248: Fix inconsistent wording in ElementFilter.typesIn
  • ... and 89 more: https://git.openjdk.org/jdk/compare/692be577385844bf00a01ff10e390e014191569f...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 3, 2024
@openjdk openjdk bot closed this Jan 3, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jan 3, 2024
@openjdk
Copy link

openjdk bot commented Jan 3, 2024

@egahlin @carterkozak Pushed as commit 1551928.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@carterkozak
Copy link
Contributor Author

Thanks again @egahlin!

After it has been integrated into main, it can be backported to JDK 21 and 22.

Would you like me to issue the backport commands? I'm not terribly familiar with the process, but I suspect jdk21u-dev and jdk22 are the correct targets?

@egahlin
Copy link
Member

egahlin commented Jan 3, 2024

Thanks again @egahlin!

After it has been integrated into main, it can be backported to JDK 21 and 22.

Would you like me to issue the backport commands? I'm not terribly familiar with the process, but I suspect jdk21u-dev and jdk22 are the correct targets?

Let's wait for the change to run through testing before backporting to JDK 22.

If everything looks fine in a couple of weeks, we can also backport to JDK 21. Not sure about the targets, I will have to look it up.

@egahlin
Copy link
Member

egahlin commented Jan 4, 2024

/backport jdk22

@openjdk
Copy link

openjdk bot commented Jan 4, 2024

@egahlin the backport was successfully created on the branch backport-egahlin-15519285 in my personal fork of openjdk/jdk22. To create a pull request with this backport targeting openjdk/jdk22:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 15519285 from the openjdk/jdk repository.

The commit being backported was authored by Carter Kozak on 3 Jan 2024 and was reviewed by Erik Gahlin.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk22:

$ git fetch https://github.com/openjdk-bots/jdk22.git backport-egahlin-15519285:backport-egahlin-15519285
$ git checkout backport-egahlin-15519285
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk22.git backport-egahlin-15519285

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-jfr hotspot-jfr-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

2 participants