8307425: Socket input stream read burns CPU cycles with back-to-back poll(0) calls

Reviewed-by: alanb
This commit is contained in:
Oli Gillespie 2023-05-05 19:36:35 +00:00 committed by Alan Bateman
parent e2b1013f11
commit 73ac710533
3 changed files with 22 additions and 2 deletions

View file

@ -78,6 +78,9 @@ import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions; import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.IPAddressUtil; import sun.net.util.IPAddressUtil;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
/** /**
* An implementation of DatagramChannels. * An implementation of DatagramChannels.
*/ */
@ -497,7 +500,12 @@ class DatagramChannelImpl
if (nanos == 0) { if (nanos == 0) {
millis = -1; millis = -1;
} else { } 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); Net.poll(getFD(), event, millis);
} }

View file

@ -182,6 +182,11 @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp
millis = -1; millis = -1;
} else { } else {
millis = 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(fd, event, millis); Net.poll(fd, event, millis);
} }

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,8 @@ package sun.nio.ch;
import java.nio.channels.Channel; import java.nio.channels.Channel;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS;
/** /**
@ -90,6 +92,11 @@ public interface SelChImpl extends Channel {
millis = -1; millis = -1;
} else { } else {
millis = 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); Net.poll(getFD(), event, millis);
} }