Skip to content

8319055: JCMD should not buffer the whole output of commands #23405

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

Closed
wants to merge 8 commits into from

Conversation

alexmenkov
Copy link

@alexmenkov alexmenkov commented Jan 31, 2025

The fix implements streaming output support for attach protocol.
See JBS issue for evaluation, summary of the changes in the 1st comment.
Testing: tier1..4,hs-tier5-svc


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-8319055: JCMD should not buffer the whole output of commands (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23405

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@alexmenkov
Copy link
Author

The changes:
Attach API protocol:

  • attach operation options are added for ATTACH_API_V2 requests.
    The options are passed as part of command name; old clients old clients enquere command without options, new client adds options only if target VM support them, so there is no need to bump attach api version;
  • "getversion" command supports new "options" argument. with this argument VM reports supported options.
    The only supported option for now is "streaming" which allows client to control streaming/buffered output mode.

AttachListener:

  • implemented attachStream class to support both streaming and buffered output;
  • all attach command handler are updated to use attachStream instead of bufferedStream and start streaming (if enabled) after parsing command arguments;
  • default streaming mode is controlled by "jdk.attach.vm.streaming" system properties; this value is used when clients does not specify "streaming" option in the request (for example when tools from older releases attach to the current VM). For safety now default value is "false" (i.e. buffered output). The plan is to make the default "true" later;
  • platform implementations (windows/posix) updated accordingly shared code changes;

DCmd framework:

  • added a way to start streaming output after command/argument parsing, but before executing the command;

Client side:

  • added option support;
  • added system property "jdk.attach.allowStreamingOutput" to set streaming mode;
    For now default value is "false" (i.e. to turn on streaming mode user need something like "JAVA_TOOL_OPTIONS=-Djdk.attach.allowStreamingOutput=true jcmd")
    The plan is to make the default "true" after fixing tests which fail with streaming output (or maybe add command line option for jcmd and other tools).

Test CodeHeapAnalyticsParams.java

  • 'Compiler.CodeHeap_Analytics' jcmd command is the only command which thrown AttachOperationFailedException exception if argument is out of range (and this causes jcmd exits with exit code 1).
    All other jcmd commands just writes exception message in the output.
    Now 'Compiler.CodeHeap_Analytics' works like other commands, so the test is updated to expect exception message.

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 31, 2025

👋 Welcome back amenkov! 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 Jan 31, 2025

@alexmenkov 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:

8319055: JCMD should not buffer the whole output of commands

Reviewed-by: stuefe, jsjolen

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 515 new commits pushed to the master branch:

  • 248c373: 8351266: JFR: -XX:StartFlightRecording:report-on-exit
  • 03ef79c: 8346470: Improve WriteBarrier JMH to have old-to-young refs
  • b50fe9b: 8280818: Expand bug8033699.java to iterate over all LaFs
  • 771e160: 8351323: Parameterize compiler and linker flags for iconv
  • 0ff1c08: 8349984: (jdeps) jdeps can use String.repeat instead of String.replaceAll
  • 37ec796: 8351500: G1: NUMA migrations cause crashes in region allocation
  • 4e51a8c: 8307513: C2: intrinsify Math.max(long,long) and Math.min(long,long)
  • 7e3bc81: 8351216: ZGC: Store NUMA node count
  • 82eb780: 8351349: GSSUtil.createSubject has outdated access control context and policy related text
  • c3db667: 8351542: LIBMANAGEMENT_OPTIMIZATION remove special optimization settings
  • ... and 505 more: https://git.openjdk.org/jdk/compare/d985b31cbb5646c526e1a68a7547f26f56d37607...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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Jan 31, 2025

@alexmenkov The following labels will be automatically applied to this pull request:

  • hotspot-runtime
  • serviceability

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

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

mlbridge bot commented Jan 31, 2025

Webrevs

@AlanBateman
Copy link
Contributor

Do you expect there will be follow up work at some point for the commands that upcall to Java and produce a byte[] to return to the tool?

@alexmenkov
Copy link
Author

Do you expect there will be follow up work at some point for the commands that upcall to Java and produce a byte[] to return to the tool?

I don't see much need in the functionality now (streaming output is useful for commands which produce lengthy output or take long time to execute), but we can add it if desired - need to wrap native writer to OutputStream and use the stream by java handlers (instead of byte buffer) to write command output.

@AlanBateman
Copy link
Contributor

Do you expect there will be follow up work at some point for the commands that upcall to Java and produce a byte[] to return to the tool?

I don't see much need in the functionality now (streaming output is useful for commands which produce lengthy output or take long time to execute), but we can add it if desired - need to wrap native writer to OutputStream and use the stream by java handlers (instead of byte buffer) to write command output.

Thread.dump_to_file is an example that needs this. There will be many more in the future. So while not for this PR we should we thinking about a follow-up to put in the infrastructure to support this.

@tstuefe
Copy link
Member

tstuefe commented Feb 4, 2025

@alexmenkov Oh, this is great, many thanks for doing this work! Good explanations too. I had a cursory view over the changes. Will do a more thorough review, and play around with this, when I am back home.

With streaming, what is the expected behavior if a command takes too long? Jcmd stops with timeout, and the hotspot JVM then discards the results?

Unless the tests are easy to fix, I don't see any problem with just running them in old buffered mode.

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

Hi Alex,

Thank you for this. It's very good to have this feature. I'm still reading the code, but I'm submitting these comments as a first round.

I didn't really understand what makes this streaming. The old protocol first sent out the result, and then the data, has this changed?

Thanks,
Johan

@alexmenkov
Copy link
Author

Do you expect there will be follow up work at some point for the commands that upcall to Java and produce a byte[] to return to the tool?

I don't see much need in the functionality now (streaming output is useful for commands which produce lengthy output or take long time to execute), but we can add it if desired - need to wrap native writer to OutputStream and use the stream by java handlers (instead of byte buffer) to write command output.

Thread.dump_to_file is an example that needs this. There will be many more in the future. So while not for this PR we should we thinking about a follow-up to put in the infrastructure to support this.

We have JDK-8336723 to implement streaming output for commands implemented in java. This PR is lower level and provides functionality required for JDK-8336723

@alexmenkov
Copy link
Author

With streaming, what is the expected behavior if a command takes too long? Jcmd stops with timeout, and the hotspot JVM then discards the results?

When a tool is run interactively (I mean not as part of a testing) there is no timeouts - the tools forward any output from the target VM to stdout until communication channel (socket/pipe) is closed.
With streaming the tool just prints command output earlier.
If jcmd is killed while the command is in progress, the VM completes the command anyway (we don't have a way to stop it), results are discarded.

Unless the tests are easy to fix, I don't see any problem with just running them in old buffered mode.

Yes, this is the plan for the next step - fix the tests of disable streaming output.

@alexmenkov
Copy link
Author

I didn't really understand what makes this streaming. The old protocol first sent out the result, and then the data, has this changed?

The protocol works as before.
All command handlers are updated to set the result code earlier (after argument parsing, but before command execution).
After the result code is set, it's sent to the tool and the rest of output (data) are send directly to the tool without buffering.

@jdksjolen
Copy link
Contributor

jdksjolen commented Feb 10, 2025

I didn't really understand what makes this streaming. The old protocol first sent out the result, and then the data, has this changed?

The protocol works as before. All command handlers are updated to set the result code earlier (after argument parsing, but before command execution). After the result code is set, it's sent to the tool and the rest of output (data) are send directly to the tool without buffering.

Aha, I understand. Before, result code meant "Parsing OK, Command Execution OK", now it means "Parsing OK, starting Command Execution". That's fine by me.

Edit: I'll perform another review round soon!

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

Hi,

Thanks for waiting.

I understand, I think, more or less what this does on the VM side now. I think that we can simplify the code for the cases when we set an error and don't write any error message.

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

Hi Alex,

I'm generally happy with this code. I've got a few more comments, but nothing major.

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

Hi,

This looks good to me now. Thank you!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 7, 2025
Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

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

+1

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Mar 11, 2025
@alexmenkov
Copy link
Author

alexmenkov commented Mar 11, 2025

Added ThreadBlockInVM in complete() method to do the same as old implementation.
It solves macos-aarch64 failure of test/hotspot/jtreg/runtime/NMT/VirtualAllocCommitUncommitRecommit.java test in GHA

@alexmenkov
Copy link
Author

@jdksjolen , @tstuefe can you re-review with the last commit please

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

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

Looks still ok

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 13, 2025
@alexmenkov
Copy link
Author

Thanks for the review
/integrate

@openjdk
Copy link

openjdk bot commented Mar 13, 2025

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

  • 248c373: 8351266: JFR: -XX:StartFlightRecording:report-on-exit
  • 03ef79c: 8346470: Improve WriteBarrier JMH to have old-to-young refs
  • b50fe9b: 8280818: Expand bug8033699.java to iterate over all LaFs
  • 771e160: 8351323: Parameterize compiler and linker flags for iconv
  • 0ff1c08: 8349984: (jdeps) jdeps can use String.repeat instead of String.replaceAll
  • 37ec796: 8351500: G1: NUMA migrations cause crashes in region allocation
  • 4e51a8c: 8307513: C2: intrinsify Math.max(long,long) and Math.min(long,long)
  • 7e3bc81: 8351216: ZGC: Store NUMA node count
  • 82eb780: 8351349: GSSUtil.createSubject has outdated access control context and policy related text
  • c3db667: 8351542: LIBMANAGEMENT_OPTIMIZATION remove special optimization settings
  • ... and 505 more: https://git.openjdk.org/jdk/compare/d985b31cbb5646c526e1a68a7547f26f56d37607...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 13, 2025
@openjdk openjdk bot closed this Mar 13, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Mar 13, 2025
@openjdk
Copy link

openjdk bot commented Mar 13, 2025

@alexmenkov Pushed as commit cd1be91.

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

@alexmenkov alexmenkov deleted the attach_streaming branch March 13, 2025 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants