Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/ch/EPollArrayWrapper.java
32288 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.IOException;28import java.security.AccessController;29import java.util.BitSet;30import java.util.HashMap;31import java.util.Map;32import sun.security.action.GetIntegerAction;3334/**35* Manipulates a native array of epoll_event structs on Linux:36*37* typedef union epoll_data {38* void *ptr;39* int fd;40* __uint32_t u32;41* __uint64_t u64;42* } epoll_data_t;43*44* struct epoll_event {45* __uint32_t events;46* epoll_data_t data;47* };48*49* The system call to wait for I/O events is epoll_wait(2). It populates an50* array of epoll_event structures that are passed to the call. The data51* member of the epoll_event structure contains the same data as was set52* when the file descriptor was registered to epoll via epoll_ctl(2). In53* this implementation we set data.fd to be the file descriptor that we54* register. That way, we have the file descriptor available when we55* process the events.56*/5758class EPollArrayWrapper {59// EPOLL_EVENTS60private static final int EPOLLIN = 0x001;6162// opcodes63private static final int EPOLL_CTL_ADD = 1;64private static final int EPOLL_CTL_DEL = 2;65private static final int EPOLL_CTL_MOD = 3;6667// Miscellaneous constants68private static final int SIZE_EPOLLEVENT = sizeofEPollEvent();69private static final int EVENT_OFFSET = 0;70private static final int DATA_OFFSET = offsetofData();71private static final int FD_OFFSET = DATA_OFFSET;72private static final int OPEN_MAX = IOUtil.fdLimit();73private static final int NUM_EPOLLEVENTS = Math.min(OPEN_MAX, 8192);7475// Special value to indicate that an update should be ignored76private static final byte KILLED = (byte)-1;7778// Initial size of arrays for fd registration changes79private static final int INITIAL_PENDING_UPDATE_SIZE = 64;8081// maximum size of updatesLow82private static final int MAX_UPDATE_ARRAY_SIZE = AccessController.doPrivileged(83new GetIntegerAction("sun.nio.ch.maxUpdateArraySize", Math.min(OPEN_MAX, 64*1024)));8485// The fd of the epoll driver86private final int epfd;8788// The epoll_event array for results from epoll_wait89private final AllocatedNativeObject pollArray;9091// Base address of the epoll_event array92private final long pollArrayAddress;9394// The fd of the interrupt line going out95private int outgoingInterruptFD;9697// The fd of the interrupt line coming in98private int incomingInterruptFD;99100// The index of the interrupt FD101private int interruptedIndex;102103// Number of updated pollfd entries104int updated;105106// object to synchronize fd registration changes107private final Object updateLock = new Object();108109// number of file descriptors with registration changes pending110private int updateCount;111112// file descriptors with registration changes pending113private int[] updateDescriptors = new int[INITIAL_PENDING_UPDATE_SIZE];114115// events for file descriptors with registration changes pending, indexed116// by file descriptor and stored as bytes for efficiency reasons. For117// file descriptors higher than MAX_UPDATE_ARRAY_SIZE (unlimited case at118// least) then the update is stored in a map.119private final byte[] eventsLow = new byte[MAX_UPDATE_ARRAY_SIZE];120private Map<Integer,Byte> eventsHigh;121122// Used by release and updateRegistrations to track whether a file123// descriptor is registered with epoll.124private final BitSet registered = new BitSet();125126127EPollArrayWrapper() throws IOException {128// creates the epoll file descriptor129epfd = epollCreate();130131// the epoll_event array passed to epoll_wait132int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT;133pollArray = new AllocatedNativeObject(allocationSize, true);134pollArrayAddress = pollArray.address();135136// eventHigh needed when using file descriptors > 64k137if (OPEN_MAX > MAX_UPDATE_ARRAY_SIZE)138eventsHigh = new HashMap<>();139}140141void initInterrupt(int fd0, int fd1) {142outgoingInterruptFD = fd1;143incomingInterruptFD = fd0;144epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);145}146147void putEventOps(int i, int event) {148int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;149pollArray.putInt(offset, event);150}151152void putDescriptor(int i, int fd) {153int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;154pollArray.putInt(offset, fd);155}156157int getEventOps(int i) {158int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;159return pollArray.getInt(offset);160}161162int getDescriptor(int i) {163int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;164return pollArray.getInt(offset);165}166167/**168* Returns {@code true} if updates for the given key (file169* descriptor) are killed.170*/171private boolean isEventsHighKilled(Integer key) {172assert key >= MAX_UPDATE_ARRAY_SIZE;173Byte value = eventsHigh.get(key);174return (value != null && value == KILLED);175}176177/**178* Sets the pending update events for the given file descriptor. This179* method has no effect if the update events is already set to KILLED,180* unless {@code force} is {@code true}.181*/182private void setUpdateEvents(int fd, byte events, boolean force) {183if (fd < MAX_UPDATE_ARRAY_SIZE) {184if ((eventsLow[fd] != KILLED) || force) {185eventsLow[fd] = events;186}187} else {188Integer key = Integer.valueOf(fd);189if (!isEventsHighKilled(key) || force) {190eventsHigh.put(key, Byte.valueOf(events));191}192}193}194195/**196* Returns the pending update events for the given file descriptor.197*/198private byte getUpdateEvents(int fd) {199if (fd < MAX_UPDATE_ARRAY_SIZE) {200return eventsLow[fd];201} else {202Byte result = eventsHigh.get(Integer.valueOf(fd));203// result should never be null204return result.byteValue();205}206}207208/**209* Update the events for a given file descriptor210*/211void setInterest(int fd, int mask) {212synchronized (updateLock) {213// record the file descriptor and events214int oldCapacity = updateDescriptors.length;215if (updateCount == oldCapacity) {216int newCapacity = oldCapacity + INITIAL_PENDING_UPDATE_SIZE;217int[] newDescriptors = new int[newCapacity];218System.arraycopy(updateDescriptors, 0, newDescriptors, 0, oldCapacity);219updateDescriptors = newDescriptors;220}221updateDescriptors[updateCount++] = fd;222223// events are stored as bytes for efficiency reasons224byte b = (byte)mask;225assert (b == mask) && (b != KILLED);226setUpdateEvents(fd, b, false);227}228}229230/**231* Add a file descriptor232*/233void add(int fd) {234// force the initial update events to 0 as it may be KILLED by a235// previous registration.236synchronized (updateLock) {237assert !registered.get(fd);238setUpdateEvents(fd, (byte)0, true);239}240}241242/**243* Remove a file descriptor244*/245void remove(int fd) {246synchronized (updateLock) {247// kill pending and future update for this file descriptor248setUpdateEvents(fd, KILLED, false);249250// remove from epoll251if (registered.get(fd)) {252epollCtl(epfd, EPOLL_CTL_DEL, fd, 0);253registered.clear(fd);254}255}256}257258/**259* Close epoll file descriptor and free poll array260*/261void closeEPollFD() throws IOException {262FileDispatcherImpl.closeIntFD(epfd);263pollArray.free();264}265266int poll(long timeout) throws IOException {267updateRegistrations();268updated = epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);269for (int i=0; i<updated; i++) {270if (getDescriptor(i) == incomingInterruptFD) {271interruptedIndex = i;272interrupted = true;273break;274}275}276return updated;277}278279/**280* Update the pending registrations.281*/282private void updateRegistrations() {283synchronized (updateLock) {284int j = 0;285while (j < updateCount) {286int fd = updateDescriptors[j];287short events = getUpdateEvents(fd);288boolean isRegistered = registered.get(fd);289int opcode = 0;290291if (events != KILLED) {292if (isRegistered) {293opcode = (events != 0) ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;294} else {295opcode = (events != 0) ? EPOLL_CTL_ADD : 0;296}297if (opcode != 0) {298epollCtl(epfd, opcode, fd, events);299if (opcode == EPOLL_CTL_ADD) {300registered.set(fd);301} else if (opcode == EPOLL_CTL_DEL) {302registered.clear(fd);303}304}305}306j++;307}308updateCount = 0;309}310}311312// interrupt support313private boolean interrupted = false;314315public void interrupt() {316interrupt(outgoingInterruptFD);317}318319public int interruptedIndex() {320return interruptedIndex;321}322323boolean interrupted() {324return interrupted;325}326327void clearInterrupted() {328interrupted = false;329}330331static {332IOUtil.load();333init();334}335336private native int epollCreate();337private native void epollCtl(int epfd, int opcode, int fd, int events);338private native int epollWait(long pollAddress, int numfds, long timeout,339int epfd) throws IOException;340private static native int sizeofEPollEvent();341private static native int offsetofData();342private static native void interrupt(int fd);343private static native void init();344}345346347