8253478: (se) epoll Selector should use eventfd for wakeup instead of pipe
Reviewed-by: alanb
This commit is contained in:
parent
34eb8b344d
commit
a8073efeed
src/java.base/linux
test/micro/org/openjdk/bench/java/nio
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2005, 2021, 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
|
||||||
@ -58,9 +58,8 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
// address of poll array when polling with epoll_wait
|
// address of poll array when polling with epoll_wait
|
||||||
private final long pollArrayAddress;
|
private final long pollArrayAddress;
|
||||||
|
|
||||||
// file descriptors used for interrupt
|
// eventfd object used for interrupt
|
||||||
private final int fd0;
|
private final EventFD eventfd;
|
||||||
private final int fd1;
|
|
||||||
|
|
||||||
// maps file descriptor to selection key, synchronize on selector
|
// maps file descriptor to selection key, synchronize on selector
|
||||||
private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>();
|
private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>();
|
||||||
@ -80,17 +79,16 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
this.pollArrayAddress = EPoll.allocatePollArray(NUM_EPOLLEVENTS);
|
this.pollArrayAddress = EPoll.allocatePollArray(NUM_EPOLLEVENTS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
long fds = IOUtil.makePipe(false);
|
this.eventfd = new EventFD();
|
||||||
this.fd0 = (int) (fds >>> 32);
|
IOUtil.configureBlocking(IOUtil.newFD(eventfd.efd()), false);
|
||||||
this.fd1 = (int) fds;
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
EPoll.freePollArray(pollArrayAddress);
|
EPoll.freePollArray(pollArrayAddress);
|
||||||
FileDispatcherImpl.closeIntFD(epfd);
|
FileDispatcherImpl.closeIntFD(epfd);
|
||||||
throw ioe;
|
throw ioe;
|
||||||
}
|
}
|
||||||
|
|
||||||
// register one end of the socket pair for wakeups
|
// register the eventfd object for wakeups
|
||||||
EPoll.ctl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
|
EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureOpen() {
|
private void ensureOpen() {
|
||||||
@ -188,7 +186,7 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
for (int i=0; i<numEntries; i++) {
|
for (int i=0; i<numEntries; i++) {
|
||||||
long event = EPoll.getEvent(pollArrayAddress, i);
|
long event = EPoll.getEvent(pollArrayAddress, i);
|
||||||
int fd = EPoll.getDescriptor(event);
|
int fd = EPoll.getDescriptor(event);
|
||||||
if (fd == fd0) {
|
if (fd == eventfd.efd()) {
|
||||||
interrupted = true;
|
interrupted = true;
|
||||||
} else {
|
} else {
|
||||||
SelectionKeyImpl ski = fdToKey.get(fd);
|
SelectionKeyImpl ski = fdToKey.get(fd);
|
||||||
@ -218,8 +216,7 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
FileDispatcherImpl.closeIntFD(epfd);
|
FileDispatcherImpl.closeIntFD(epfd);
|
||||||
EPoll.freePollArray(pollArrayAddress);
|
EPoll.freePollArray(pollArrayAddress);
|
||||||
|
|
||||||
FileDispatcherImpl.closeIntFD(fd0);
|
eventfd.close();
|
||||||
FileDispatcherImpl.closeIntFD(fd1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -251,7 +248,7 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
synchronized (interruptLock) {
|
synchronized (interruptLock) {
|
||||||
if (!interruptTriggered) {
|
if (!interruptTriggered) {
|
||||||
try {
|
try {
|
||||||
IOUtil.write1(fd1, (byte)0);
|
eventfd.set();
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
throw new InternalError(ioe);
|
throw new InternalError(ioe);
|
||||||
}
|
}
|
||||||
@ -263,7 +260,7 @@ class EPollSelectorImpl extends SelectorImpl {
|
|||||||
|
|
||||||
private void clearInterrupt() throws IOException {
|
private void clearInterrupt() throws IOException {
|
||||||
synchronized (interruptLock) {
|
synchronized (interruptLock) {
|
||||||
IOUtil.drain(fd0);
|
eventfd.reset();
|
||||||
interruptTriggered = false;
|
interruptTriggered = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
73
src/java.base/linux/classes/sun/nio/ch/EventFD.java
Normal file
73
src/java.base/linux/classes/sun/nio/ch/EventFD.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021, 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
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Oracle designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sun.nio.ch;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Provides access to the Linux eventfd object.
|
||||||
|
*/
|
||||||
|
final class EventFD {
|
||||||
|
private final int efd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a blocking eventfd object with initial value zero.
|
||||||
|
*/
|
||||||
|
EventFD() throws IOException {
|
||||||
|
efd = eventfd0();
|
||||||
|
}
|
||||||
|
|
||||||
|
int efd() {
|
||||||
|
return efd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set() throws IOException {
|
||||||
|
set0(efd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() throws IOException {
|
||||||
|
IOUtil.drain(efd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() throws IOException {
|
||||||
|
FileDispatcherImpl.closeIntFD(efd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native int eventfd0() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the value 1 to the eventfd object as a long in the
|
||||||
|
* native byte order of the platform.
|
||||||
|
*
|
||||||
|
* @param the integral eventfd file descriptor
|
||||||
|
* @return the number of bytes written; should equal 8
|
||||||
|
*/
|
||||||
|
private static native int set0(int efd) throws IOException;
|
||||||
|
|
||||||
|
static {
|
||||||
|
IOUtil.load();
|
||||||
|
}
|
||||||
|
}
|
54
src/java.base/linux/native/libnio/ch/EventFD.c
Normal file
54
src/java.base/linux/native/libnio/ch/EventFD.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2021, 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
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Oracle designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/eventfd.h>
|
||||||
|
|
||||||
|
#include "jni.h"
|
||||||
|
#include "jni_util.h"
|
||||||
|
#include "jvm.h"
|
||||||
|
#include "jlong.h"
|
||||||
|
#include "nio.h"
|
||||||
|
#include "nio_util.h"
|
||||||
|
|
||||||
|
#include "sun_nio_ch_EventFD.h"
|
||||||
|
|
||||||
|
JNIEXPORT jint JNICALL
|
||||||
|
Java_sun_nio_ch_EventFD_eventfd0(JNIEnv *env, jclass klazz)
|
||||||
|
{
|
||||||
|
int efd = eventfd((uint64_t)0, 0);
|
||||||
|
if (efd == -1) {
|
||||||
|
JNU_ThrowIOExceptionWithLastError(env, "eventfd failed");
|
||||||
|
return IOS_THROWN;
|
||||||
|
}
|
||||||
|
return efd;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jint JNICALL
|
||||||
|
Java_sun_nio_ch_EventFD_set0(JNIEnv *env, jclass klazz, jint efd)
|
||||||
|
{
|
||||||
|
long one = 1L;
|
||||||
|
return convertReturnVal(env, write(efd, (void*)&one, sizeof(long)),
|
||||||
|
JNI_FALSE);
|
||||||
|
}
|
62
test/micro/org/openjdk/bench/java/nio/SelectorWakeup.java
Normal file
62
test/micro/org/openjdk/bench/java/nio/SelectorWakeup.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Oracle America, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name of Oracle nor the names of its contributors may be used
|
||||||
|
* to endorse or promote products derived from this software without
|
||||||
|
* specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.openjdk.bench.java.nio;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.Level;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Benchmark for the Selector wakeup mechanism. Intended primarily for the
|
||||||
|
* epoll(7)-based implementation on Linux.
|
||||||
|
*/
|
||||||
|
@State(Scope.Thread)
|
||||||
|
public class SelectorWakeup {
|
||||||
|
private Selector sel;
|
||||||
|
|
||||||
|
@Setup(Level.Iteration)
|
||||||
|
public void setup() throws IOException {
|
||||||
|
sel = Selector.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public int test() throws IOException {
|
||||||
|
return sel.wakeup().select();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user