-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProcessEpoll.java
More file actions
410 lines (360 loc) · 13.8 KB
/
Copy pathProcessEpoll.java
File metadata and controls
410 lines (360 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.nuprocess.linux;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import com.zaxxer.nuprocess.NuProcess;
import com.zaxxer.nuprocess.internal.BaseEventProcessor;
import com.zaxxer.nuprocess.internal.LibC;
import static com.zaxxer.nuprocess.internal.LibC.WIFEXITED;
import static com.zaxxer.nuprocess.internal.LibC.WEXITSTATUS;
import static com.zaxxer.nuprocess.internal.LibC.WIFSIGNALED;
import static com.zaxxer.nuprocess.internal.LibC.WTERMSIG;
/**
* @author Brett Wooldridge
*/
class ProcessEpoll extends BaseEventProcessor<LinuxProcess>
{
private final int epoll;
private final EpollEvent triggeredEvent;
private final List<LinuxProcess> deadPool;
// see comment in #cleanupProcess for explanation on dynamicDeadpoolPollInterval
private int dynamicDeadpoolPollInterval = DEADPOOL_POLL_INTERVAL;
private LinuxProcess process;
ProcessEpoll()
{
this(LINGER_ITERATIONS);
}
ProcessEpoll(LinuxProcess process)
{
this(-1);
this.process = process;
registerProcess(process);
checkAndSetRunning();
}
private ProcessEpoll(int lingerIterations)
{
super(lingerIterations);
epoll = LibEpoll.epoll_create(1024);
if (epoll < 0) {
int errno = Native.getLastError();
throw new RuntimeException("Unable to create kqueue, errno: " + errno);
}
triggeredEvent = new EpollEvent();
deadPool = new LinkedList<>();
}
// ************************************************************************
// IEventProcessor methods
// ************************************************************************
@Override
public void registerProcess(LinuxProcess process)
{
if (shutdown) {
return;
}
int stdinFd = Integer.MIN_VALUE;
int stdoutFd = Integer.MIN_VALUE;
int stderrFd = Integer.MIN_VALUE;
try {
stdinFd = process.getStdin().acquire();
stdoutFd = process.getStdout().acquire();
stderrFd = process.getStderr().acquire();
pidToProcessMap.put(process.getPid(), process);
fildesToProcessMap.put(stdinFd, process);
fildesToProcessMap.put(stdoutFd, process);
fildesToProcessMap.put(stderrFd, process);
EpollEvent event = process.getEpollEvent();
event.setEvents(LibEpoll.EPOLLIN);
event.setFileDescriptor(stdoutFd);
int rc = LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_ADD, stdoutFd, event.getPointer());
if (rc == -1) {
int errno = Native.getLastError();
throw new RuntimeException("Unable to register new events to epoll, errno: " + errno);
}
event.setEvents(LibEpoll.EPOLLIN);
event.setFileDescriptor(stderrFd);
rc = LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_ADD, stderrFd, event.getPointer());
if (rc == -1) {
int errno = Native.getLastError();
throw new RuntimeException("Unable to register new events to epoll, errno: " + errno);
}
}
finally {
if (stdinFd != Integer.MIN_VALUE) {
process.getStdin().release();
}
if (stdoutFd != Integer.MIN_VALUE) {
process.getStdout().release();
}
if (stderrFd != Integer.MIN_VALUE) {
process.getStderr().release();
}
}
}
@Override
public void queueWrite(LinuxProcess process)
{
if (shutdown) {
return;
}
try {
int stdin = process.getStdin().acquire();
if (stdin == -1) {
return;
}
EpollEvent event = process.getEpollEvent();
event.setEvents(LibEpoll.EPOLLOUT | LibEpoll.EPOLLONESHOT | LibEpoll.EPOLLRDHUP | LibEpoll.EPOLLHUP);
event.setFileDescriptor(stdin);
int rc = LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_MOD, stdin, event.getPointer());
if (rc == -1) {
LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_DEL, stdin, event.getPointer());
rc = LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_ADD, stdin, event.getPointer());
if (rc == -1) {
int errno = Native.getLastError();
throw new RuntimeException("Unable to register new event to epoll queue, errno: " + errno);
}
}
}
finally {
process.getStdin().release();
}
}
@Override
public void run()
{
super.run();
if (process != null) {
// For synchronous execution, wait until the deadpool is drained. This is necessary to ensure
// the handler's onExit is called before LinuxProcess.run returns.
waitForDeadPool();
}
}
@Override
public void closeStdin(LinuxProcess process)
{
try {
int stdin = process.getStdin().acquire();
if (stdin != -1) {
fildesToProcessMap.remove(stdin);
LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_DEL, stdin, null);
}
} finally {
process.getStdin().release();
}
}
@Override
public boolean process()
{
int stdinFd = Integer.MIN_VALUE;
int stdoutFd = Integer.MIN_VALUE;
int stderrFd = Integer.MIN_VALUE;
LinuxProcess linuxProcess = null;
try {
int nev = LibEpoll.epoll_wait(epoll, triggeredEvent.getPointer(), 1, dynamicDeadpoolPollInterval);
// see comment in #cleanupProcess for explanation on dynamicDeadpoolPollInterval
dynamicDeadpoolPollInterval = Math.min(DEADPOOL_POLL_INTERVAL, 2 * dynamicDeadpoolPollInterval);
if (nev == -1) {
int errno = Native.getLastError();
if (errno == LibC.EINTR) {
// Signals received while in epoll_wait can interrupt the call and, per the documentation for
// SA_RESTART, it will not be restarted automatically. When that happens, we manually restart
// epoll_wait by returning true, which ensures BaseEventProcessor.run() calls process() again.
// This matches how the JVM internals handle EINTR during epoll_wait.
// See https://github.com/JetBrains/jdk8u_jdk/blob/94318f9185757cc33d2b8d527d36be26ac6b7582/src/solaris/native/sun/nio/ch/nio_util.h#L33-L37
return true;
}
throw new RuntimeException("Error waiting for epoll, errno: " + errno);
}
if (nev == 0) {
return false;
}
EpollEvent epEvent = triggeredEvent;
int ident = epEvent.getFileDescriptor();
int events = epEvent.getEvents();
linuxProcess = fildesToProcessMap.get(ident);
if (linuxProcess == null) {
return true;
}
stdinFd = linuxProcess.getStdin().acquire();
stdoutFd = linuxProcess.getStdout().acquire();
stderrFd = linuxProcess.getStderr().acquire();
if ((events & LibEpoll.EPOLLIN) != 0) { // stdout/stderr data available to read
if (ident == stdoutFd) {
linuxProcess.readStdout(NuProcess.BUFFER_CAPACITY, stdoutFd);
}
else if (ident == stderrFd) {
linuxProcess.readStderr(NuProcess.BUFFER_CAPACITY, stderrFd);
}
}
else if ((events & LibEpoll.EPOLLOUT) != 0) { // Room in stdin pipe available to write
if (stdinFd != -1) {
if (linuxProcess.writeStdin(NuProcess.BUFFER_CAPACITY, stdinFd)) {
epEvent.setEvents(LibEpoll.EPOLLOUT | LibEpoll.EPOLLONESHOT | LibEpoll.EPOLLRDHUP | LibEpoll.EPOLLHUP);
LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_MOD, ident, epEvent.getPointer());
}
}
}
if ((events & LibEpoll.EPOLLHUP) != 0 || (events & LibEpoll.EPOLLRDHUP) != 0 || (events & LibEpoll.EPOLLERR) != 0) {
LibEpoll.epoll_ctl(epoll, LibEpoll.EPOLL_CTL_DEL, ident, null);
if (ident == stdoutFd) {
linuxProcess.readStdout(-1, stdoutFd);
}
else if (ident == stderrFd) {
linuxProcess.readStderr(-1, stderrFd);
}
else if (ident == stdinFd) {
linuxProcess.closeStdin(true);
}
}
if (linuxProcess.isSoftExit()) {
cleanupProcess(linuxProcess, stdinFd, stdoutFd, stderrFd);
}
return true;
}
finally {
if (linuxProcess != null) {
if (stdinFd != Integer.MIN_VALUE) {
linuxProcess.getStdin().release();
}
if (stdoutFd != Integer.MIN_VALUE) {
linuxProcess.getStdout().release();
}
if (stderrFd != Integer.MIN_VALUE) {
linuxProcess.getStderr().release();
}
}
checkDeadPool();
}
}
/**
* Closes the {@code eventpoll} file descriptor.
*
* @since 1.3
*/
@Override
protected void close()
{
LibC.close(epoll);
}
// ************************************************************************
// Private methods
// ************************************************************************
private void cleanupProcess(LinuxProcess linuxProcess, int stdinFd, int stdoutFd, int stderrFd)
{
// we detected a close of a stream, this means that the process is going to be closed soon or is already closed
// in the absence of other EPoll events this means that we might wait for the full DEADPOOL_POLL_INTERVAL
// before we do waitpid check again. With dynamic poll interval, we check again in the intervals of
// 1, 2, 4, 8, ... DEADPOOL_POLL_INTERVAL ms. This allows us to catch exit much sooner.
dynamicDeadpoolPollInterval = 1;
pidToProcessMap.remove(linuxProcess.getPid());
fildesToProcessMap.remove(stdinFd);
fildesToProcessMap.remove(stdoutFd);
fildesToProcessMap.remove(stderrFd);
IntByReference ret = new IntByReference();
int rc = LibC.waitpid(linuxProcess.getPid(), ret, LibC.WNOHANG);
if (rc == 0) {
deadPool.add(linuxProcess);
}
else if (rc < 0) {
linuxProcess.onExit((Native.getLastError() == LibC.ECHILD) ? Integer.MAX_VALUE : Integer.MIN_VALUE);
}
else {
handleExit(linuxProcess, ret.getValue());
}
}
private void checkDeadPool()
{
if (deadPool.isEmpty()) {
return;
}
IntByReference ret = new IntByReference();
Iterator<LinuxProcess> iterator = deadPool.iterator();
while (iterator.hasNext()) {
LinuxProcess process = iterator.next();
int rc = LibC.waitpid(process.getPid(), ret, LibC.WNOHANG);
if (rc == 0) {
continue;
}
iterator.remove();
if (rc < 0) {
process.onExit((Native.getLastError() == LibC.ECHILD) ? Integer.MAX_VALUE : Integer.MIN_VALUE);
continue;
}
handleExit(process, ret.getValue());
}
}
private void handleExit(final LinuxProcess process, int status)
{
if (WIFEXITED(status)) {
status = WEXITSTATUS(status);
if (status == 127) {
process.onExit(Integer.MIN_VALUE);
}
else {
process.onExit(status);
}
}
else if (WIFSIGNALED(status)) {
process.onExit(WTERMSIG(status));
}
else {
process.onExit(Integer.MIN_VALUE);
}
}
/**
* Loops until the {@link #deadPool} is empty, using a backoff sleep to progressively increase the poll
* interval the longer the process takes to terminate.
* <p>
* {@code waitpid} does not offer a timeout variant. Callers have two options:
* <ul>
* <li>Use {@code WNOHANG} and have the call return immediately, whether the process has terminated
* or not, and use the return code to tell the difference</li>
* <li>Don't use {@code WNOHANG} and have the call block until the process terminates</li>
* </ul>
* To avoid the possibility of a misbehaving process hanging the JVM indefinitely, this loop uses a Java-
* based sleep to wait between checks. The sleep interval ramps up each time the loop runs. The ramp-up
* is intended to minimize the penalty imposed on well-behaved processes, which will generally only loop
* once or twice before terminating.
* <p>
* This loop will wait up to the {@link #LINGER_TIME_MS configured linger timeout} for the process to
* terminate. At that point, if the process still hasn't terminated, it is abandoned.
*/
private void waitForDeadPool()
{
long sleepInterval = 0L;
long timeout = System.currentTimeMillis() + LINGER_TIME_MS;
while (true) {
checkDeadPool();
if (deadPool.isEmpty() || System.currentTimeMillis() > timeout) {
break;
}
if (sleepInterval > 0L) { // This gives 2 checks in a row before the first sleep
try {
Thread.sleep(sleepInterval);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
// 0 -> 1 -> 3 -> 7 -> 15 -> 31 -> 63 -> 127, etc
sleepInterval = (sleepInterval * 2L) + 1L;
}
}
}