1 /* |
|
2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.nio.ch; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.security.AccessController; |
|
30 import java.util.BitSet; |
|
31 import java.util.HashMap; |
|
32 import java.util.Map; |
|
33 import sun.security.action.GetIntegerAction; |
|
34 |
|
35 /** |
|
36 * Manipulates a native array of epoll_event structs on Linux: |
|
37 * |
|
38 * typedef union epoll_data { |
|
39 * void *ptr; |
|
40 * int fd; |
|
41 * __uint32_t u32; |
|
42 * __uint64_t u64; |
|
43 * } epoll_data_t; |
|
44 * |
|
45 * struct epoll_event { |
|
46 * __uint32_t events; |
|
47 * epoll_data_t data; |
|
48 * }; |
|
49 * |
|
50 * The system call to wait for I/O events is epoll_wait(2). It populates an |
|
51 * array of epoll_event structures that are passed to the call. The data |
|
52 * member of the epoll_event structure contains the same data as was set |
|
53 * when the file descriptor was registered to epoll via epoll_ctl(2). In |
|
54 * this implementation we set data.fd to be the file descriptor that we |
|
55 * register. That way, we have the file descriptor available when we |
|
56 * process the events. |
|
57 */ |
|
58 |
|
59 class EPollArrayWrapper { |
|
60 // EPOLL_EVENTS |
|
61 private static final int EPOLLIN = 0x001; |
|
62 |
|
63 // opcodes |
|
64 private static final int EPOLL_CTL_ADD = 1; |
|
65 private static final int EPOLL_CTL_DEL = 2; |
|
66 private static final int EPOLL_CTL_MOD = 3; |
|
67 |
|
68 // Miscellaneous constants |
|
69 private static final int SIZE_EPOLLEVENT = sizeofEPollEvent(); |
|
70 private static final int EVENT_OFFSET = 0; |
|
71 private static final int DATA_OFFSET = offsetofData(); |
|
72 private static final int FD_OFFSET = DATA_OFFSET; |
|
73 private static final int OPEN_MAX = IOUtil.fdLimit(); |
|
74 private static final int NUM_EPOLLEVENTS = Math.min(OPEN_MAX, 8192); |
|
75 |
|
76 // Special value to indicate that an update should be ignored |
|
77 private static final byte KILLED = (byte)-1; |
|
78 |
|
79 // Initial size of arrays for fd registration changes |
|
80 private static final int INITIAL_PENDING_UPDATE_SIZE = 64; |
|
81 |
|
82 // maximum size of updatesLow |
|
83 private static final int MAX_UPDATE_ARRAY_SIZE = AccessController.doPrivileged( |
|
84 new GetIntegerAction("sun.nio.ch.maxUpdateArraySize", Math.min(OPEN_MAX, 64*1024))); |
|
85 |
|
86 // The fd of the epoll driver |
|
87 private final int epfd; |
|
88 |
|
89 // The epoll_event array for results from epoll_wait |
|
90 private final AllocatedNativeObject pollArray; |
|
91 |
|
92 // Base address of the epoll_event array |
|
93 private final long pollArrayAddress; |
|
94 |
|
95 // The fd of the interrupt line going out |
|
96 private final int outgoingInterruptFD; |
|
97 |
|
98 // Number of updated pollfd entries |
|
99 private int updated; |
|
100 |
|
101 // object to synchronize fd registration changes |
|
102 private final Object updateLock = new Object(); |
|
103 |
|
104 // number of file descriptors with registration changes pending |
|
105 private int updateCount; |
|
106 |
|
107 // file descriptors with registration changes pending |
|
108 private int[] updateDescriptors = new int[INITIAL_PENDING_UPDATE_SIZE]; |
|
109 |
|
110 // events for file descriptors with registration changes pending, indexed |
|
111 // by file descriptor and stored as bytes for efficiency reasons. For |
|
112 // file descriptors higher than MAX_UPDATE_ARRAY_SIZE (unlimited case at |
|
113 // least) then the update is stored in a map. |
|
114 private final byte[] eventsLow = new byte[MAX_UPDATE_ARRAY_SIZE]; |
|
115 private final Map<Integer,Byte> eventsHigh = new HashMap<>(); |
|
116 |
|
117 // Used by release and updateRegistrations to track whether a file |
|
118 // descriptor is registered with epoll. |
|
119 private final BitSet registered = new BitSet(); |
|
120 |
|
121 |
|
122 EPollArrayWrapper(int fd0, int fd1) throws IOException { |
|
123 // creates the epoll file descriptor |
|
124 epfd = epollCreate(); |
|
125 |
|
126 // the epoll_event array passed to epoll_wait |
|
127 int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT; |
|
128 pollArray = new AllocatedNativeObject(allocationSize, true); |
|
129 pollArrayAddress = pollArray.address(); |
|
130 |
|
131 outgoingInterruptFD = fd1; |
|
132 epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN); |
|
133 } |
|
134 |
|
135 void putEventOps(int i, int event) { |
|
136 int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET; |
|
137 pollArray.putInt(offset, event); |
|
138 } |
|
139 |
|
140 void putDescriptor(int i, int fd) { |
|
141 int offset = SIZE_EPOLLEVENT * i + FD_OFFSET; |
|
142 pollArray.putInt(offset, fd); |
|
143 } |
|
144 |
|
145 int getEventOps(int i) { |
|
146 int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET; |
|
147 return pollArray.getInt(offset); |
|
148 } |
|
149 |
|
150 int getDescriptor(int i) { |
|
151 int offset = SIZE_EPOLLEVENT * i + FD_OFFSET; |
|
152 return pollArray.getInt(offset); |
|
153 } |
|
154 |
|
155 /** |
|
156 * Returns {@code true} if updates for the given key (file |
|
157 * descriptor) are killed. |
|
158 */ |
|
159 private boolean isEventsHighKilled(Integer key) { |
|
160 assert key >= MAX_UPDATE_ARRAY_SIZE; |
|
161 Byte value = eventsHigh.get(key); |
|
162 return (value != null && value == KILLED); |
|
163 } |
|
164 |
|
165 /** |
|
166 * Sets the pending update events for the given file descriptor. This |
|
167 * method has no effect if the update events is already set to KILLED, |
|
168 * unless {@code force} is {@code true}. |
|
169 */ |
|
170 private void setUpdateEvents(int fd, byte events, boolean force) { |
|
171 if (fd < MAX_UPDATE_ARRAY_SIZE) { |
|
172 if ((eventsLow[fd] != KILLED) || force) { |
|
173 eventsLow[fd] = events; |
|
174 } |
|
175 } else { |
|
176 Integer key = Integer.valueOf(fd); |
|
177 if (!isEventsHighKilled(key) || force) { |
|
178 eventsHigh.put(key, Byte.valueOf(events)); |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 /** |
|
184 * Returns the pending update events for the given file descriptor. |
|
185 */ |
|
186 private byte getUpdateEvents(int fd) { |
|
187 if (fd < MAX_UPDATE_ARRAY_SIZE) { |
|
188 return eventsLow[fd]; |
|
189 } else { |
|
190 Byte result = eventsHigh.get(Integer.valueOf(fd)); |
|
191 // result should never be null |
|
192 return result.byteValue(); |
|
193 } |
|
194 } |
|
195 |
|
196 /** |
|
197 * Update the events for a given file descriptor |
|
198 */ |
|
199 void setInterest(int fd, int mask) { |
|
200 synchronized (updateLock) { |
|
201 // record the file descriptor and events |
|
202 int oldCapacity = updateDescriptors.length; |
|
203 if (updateCount == oldCapacity) { |
|
204 int newCapacity = oldCapacity + INITIAL_PENDING_UPDATE_SIZE; |
|
205 int[] newDescriptors = new int[newCapacity]; |
|
206 System.arraycopy(updateDescriptors, 0, newDescriptors, 0, oldCapacity); |
|
207 updateDescriptors = newDescriptors; |
|
208 } |
|
209 updateDescriptors[updateCount++] = fd; |
|
210 |
|
211 // events are stored as bytes for efficiency reasons |
|
212 byte b = (byte)mask; |
|
213 assert (b == mask) && (b != KILLED); |
|
214 setUpdateEvents(fd, b, false); |
|
215 } |
|
216 } |
|
217 |
|
218 /** |
|
219 * Add a file descriptor |
|
220 */ |
|
221 void add(int fd) { |
|
222 // force the initial update events to 0 as it may be KILLED by a |
|
223 // previous registration. |
|
224 synchronized (updateLock) { |
|
225 assert !registered.get(fd); |
|
226 setUpdateEvents(fd, (byte)0, true); |
|
227 } |
|
228 } |
|
229 |
|
230 /** |
|
231 * Remove a file descriptor |
|
232 */ |
|
233 void remove(int fd) { |
|
234 synchronized (updateLock) { |
|
235 // kill pending and future update for this file descriptor |
|
236 setUpdateEvents(fd, KILLED, false); |
|
237 |
|
238 // remove from epoll |
|
239 if (registered.get(fd)) { |
|
240 epollCtl(epfd, EPOLL_CTL_DEL, fd, 0); |
|
241 registered.clear(fd); |
|
242 } |
|
243 } |
|
244 } |
|
245 |
|
246 /** |
|
247 * Close epoll file descriptor and free poll array |
|
248 */ |
|
249 void close() throws IOException { |
|
250 FileDispatcherImpl.closeIntFD(epfd); |
|
251 pollArray.free(); |
|
252 } |
|
253 |
|
254 int poll(long timeout) throws IOException { |
|
255 updateRegistrations(); |
|
256 return epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd); |
|
257 } |
|
258 |
|
259 /** |
|
260 * Update the pending registrations. |
|
261 */ |
|
262 private void updateRegistrations() { |
|
263 synchronized (updateLock) { |
|
264 int j = 0; |
|
265 while (j < updateCount) { |
|
266 int fd = updateDescriptors[j]; |
|
267 short events = getUpdateEvents(fd); |
|
268 boolean isRegistered = registered.get(fd); |
|
269 int opcode = 0; |
|
270 |
|
271 if (events != KILLED) { |
|
272 if (isRegistered) { |
|
273 opcode = (events != 0) ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; |
|
274 } else { |
|
275 opcode = (events != 0) ? EPOLL_CTL_ADD : 0; |
|
276 } |
|
277 if (opcode != 0) { |
|
278 epollCtl(epfd, opcode, fd, events); |
|
279 if (opcode == EPOLL_CTL_ADD) { |
|
280 registered.set(fd); |
|
281 } else if (opcode == EPOLL_CTL_DEL) { |
|
282 registered.clear(fd); |
|
283 } |
|
284 } |
|
285 } |
|
286 j++; |
|
287 } |
|
288 updateCount = 0; |
|
289 } |
|
290 } |
|
291 |
|
292 public void interrupt() { |
|
293 interrupt(outgoingInterruptFD); |
|
294 } |
|
295 |
|
296 static { |
|
297 IOUtil.load(); |
|
298 init(); |
|
299 } |
|
300 |
|
301 private native int epollCreate(); |
|
302 private native void epollCtl(int epfd, int opcode, int fd, int events); |
|
303 private native int epollWait(long pollAddress, int numfds, long timeout, |
|
304 int epfd) throws IOException; |
|
305 private static native int sizeofEPollEvent(); |
|
306 private static native int offsetofData(); |
|
307 private static native void interrupt(int fd); |
|
308 private static native void init(); |
|
309 } |
|