Skip to content

Commit

Permalink
8307425: Socket input stream read burns CPU cycles with back-to-back …
Browse files Browse the repository at this point in the history
…poll(0) calls

Reviewed-by: alanb
  • Loading branch information
olivergillespie authored and Alan Bateman committed May 5, 2023
1 parent e2b1013 commit 73ac710
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.IPAddressUtil;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
* An implementation of DatagramChannels.
*/
Expand Down Expand Up @@ -497,7 +500,12 @@ public void park(int event, long nanos) throws IOException {
if (nanos == 0) {
millis = -1;
} else {
millis = TimeUnit.NANOSECONDS.toMillis(nanos);
millis = NANOSECONDS.toMillis(nanos);
if (nanos > MILLISECONDS.toNanos(millis)) {
// Round up any excess nanos to the nearest millisecond to
// avoid parking for less than requested.
millis++;
}
}
Net.poll(getFD(), event, millis);
}
Expand Down
5 changes: 5 additions & 0 deletions src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ private void park(FileDescriptor fd, int event, long nanos) throws IOException {
millis = -1;
} else {
millis = NANOSECONDS.toMillis(nanos);
if (nanos > MILLISECONDS.toNanos(millis)) {
// Round up any excess nanos to the nearest millisecond to
// avoid parking for less than requested.
millis++;
}
}
Net.poll(fd, event, millis);
}
Expand Down
9 changes: 8 additions & 1 deletion src/java.base/share/classes/sun/nio/ch/SelChImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,6 +28,8 @@
import java.nio.channels.Channel;
import java.io.FileDescriptor;
import java.io.IOException;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

/**
Expand Down Expand Up @@ -90,6 +92,11 @@ default void park(int event, long nanos) throws IOException {
millis = -1;
} else {
millis = NANOSECONDS.toMillis(nanos);
if (nanos > MILLISECONDS.toNanos(millis)) {
// Round up any excess nanos to the nearest millisecond to
// avoid parking for less than requested.
millis++;
}
}
Net.poll(getFD(), event, millis);
}
Expand Down

3 comments on commit 73ac710

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@dfuch
Copy link
Member

@dfuch dfuch commented on 73ac710 May 17, 2023

Choose a reason for hiding this comment

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

/backport jdk20u

@openjdk
Copy link

@openjdk openjdk bot commented on 73ac710 May 17, 2023

Choose a reason for hiding this comment

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

@dfuch the backport was successfully created on the branch dfuch-backport-73ac7105 in my personal fork of openjdk/jdk20u. To create a pull request with this backport targeting openjdk/jdk20u: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 73ac7105 from the openjdk/jdk repository.

The commit being backported was authored by Oli Gillespie on 5 May 2023 and was reviewed by Alan Bateman.

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/jdk20u:

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

Please sign in to comment.