Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk20u Public archive

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

Backport-of: 73ac710533a45bf5ba17f308aa49556b877b8bf9
  • Loading branch information
dfuch committed May 19, 2023
1 parent 511ba72 commit 2302454
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

1 comment on commit 2302454

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.