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

8254102: use ProcessHandle::pid instead of ManagementFactory::getRuntimeMXBean to get pid in tests #534

Closed
wants to merge 4 commits into from

Conversation

@iignatev
Copy link
Member

@iignatev iignatev commented Oct 6, 2020

Hi all,

could you please review this small cleanup which replaces ManagementFactory.getRuntimeMXBean().getName().split("@")[0] w/ ProcessHandle.current().pid() to get current process pid?

Thanks,
-- Igor


Progress

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

Testing

Linux x64 Windows x64 macOS x64
Build ✔️ (3/3 passed) (2/2 running) ✔️ (2/2 passed)
Test (tier1) (8/9 running) (8/9 running)

Issue

  • JDK-8254102: use ProcessHandle::pid instead of ManagementFactory::getRuntimeMXBean to get pid in tests

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/534/head:pull/534
$ git checkout pull/534

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Oct 6, 2020

👋 Welcome back iignatyev! 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.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Oct 6, 2020

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

  • build
  • core-libs
  • hotspot

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.

Loading

@iignatev iignatev marked this pull request as ready for review Oct 6, 2020
@openjdk openjdk bot added the rfr label Oct 6, 2020
@mlbridge
Copy link

@mlbridge mlbridge bot commented Oct 6, 2020

Webrevs

Loading

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

All of these changes can call ProcessHandle.current().toString() to return pid of the current process.

Loading

@@ -34,8 +30,7 @@
public static void main(String[] args) {
String cmd = null;
try {
String pidStr = ManagementFactory.getRuntimeMXBean().getName()
.split("@")[0];
String pidStr = String.valueOf(ProcessHandle.current().pid());
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
cmd = "taskkill.exe /F /PID " + pidStr;
Copy link
Contributor

@RogerRiggs RogerRiggs Oct 7, 2020

Choose a reason for hiding this comment

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

This can be simplified to ProcessHandle.current().toString(). It returns the pid of the process as a string.

Explicitly converting it to a string is not necessary. The "+" concatenation would convert the number to a string.

Loading

Copy link
Contributor

@shipilev shipilev Oct 7, 2020

Choose a reason for hiding this comment

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

Yes, can just have long pid in this case.

I don't see that ProcessHandle.toString is specified to return the string with pid, so relying on that is brittle. We might call Long.toString here directly, to avoid jumping through a few calls. But seeing how all this is a test code, that does not seem necessary.

Loading

Copy link
Member Author

@iignatev iignatev Oct 7, 2020

Choose a reason for hiding this comment

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

@RogerRiggs , as Aleksey pointed out ProcessHandle::toString isn't specified to return the string which contains only pid (in fact it's not specified at all), so I don't think we should rely on the current implementation.

although it doesn't matter much, I've removed explicit conversion here and replaced String::valueOf with Long::toString in other places.

Loading

Copy link
Contributor

@shipilev shipilev left a comment

I think this is fine, but you might consider a little improvement below.

Loading

@@ -34,8 +30,7 @@
public static void main(String[] args) {
String cmd = null;
try {
String pidStr = ManagementFactory.getRuntimeMXBean().getName()
.split("@")[0];
String pidStr = String.valueOf(ProcessHandle.current().pid());
String osName = System.getProperty("os.name");
if (osName.contains("Windows")) {
cmd = "taskkill.exe /F /PID " + pidStr;
Copy link
Contributor

@shipilev shipilev Oct 7, 2020

Choose a reason for hiding this comment

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

Yes, can just have long pid in this case.

I don't see that ProcessHandle.toString is specified to return the string with pid, so relying on that is brittle. We might call Long.toString here directly, to avoid jumping through a few calls. But seeing how all this is a test code, that does not seem necessary.

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Oct 7, 2020

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

8254102: use ProcessHandle::pid instead of ManagementFactory::getRuntimeMXBean to get pid in tests

Reviewed-by: rriggs, shade

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

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.

Loading

@openjdk openjdk bot added the ready label Oct 7, 2020
Copy link
Contributor

@shipilev shipilev left a comment

Still looks good to me.

Loading

@iignatev
Copy link
Member Author

@iignatev iignatev commented Oct 7, 2020

Roger, Aleksey, thanks for your reviews.

/integrate

Loading

@openjdk openjdk bot closed this Oct 7, 2020
@openjdk openjdk bot added integrated and removed ready rfr labels Oct 7, 2020
@openjdk
Copy link

@openjdk openjdk bot commented Oct 7, 2020

@iignatev Since your change was applied there have been 15 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

Pushed as commit 5a9bd41.

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

Loading

@iignatev iignatev deleted the 8254102 branch Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants