GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — io_uring: count only real I/O completions in run() return value · netty/netty@3c50d6c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • IoUringIoHandler.java
  • transport-native-io_uring/src/test/java/io/netty/channel/uring
    • IoUringIoHandlerWakeupEventCountTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringIoHandler.java‎

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringIoHandler.java
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public final class IoUringIoHandler implements IoHandler {
8787
private static final int KERNEL_TIMESPEC_TV_NSEC_FIELD = 8;
8888

8989
private final ThreadAwareExecutor executor;
90+
private int ioCompletions;
9091

9192
IoUringIoHandler(ThreadAwareExecutor executor, IoUringIoHandlerConfig config) {
9293
// Ensure that we load all native bits as otherwise it may fail when try to use native methods in IovArray
@@ -175,17 +176,17 @@ public int run(IoHandlerContext context) {
175176
submitAndClearNow(submissionQueue);
176177
}
177178

178-
int processed;
179+
ioCompletions = 0;
179180
if (context.shouldReportActiveIoTime()) {
180181
// Timer starts after the blocking wait, around the processing of completions.
181182
long activeIoStartTimeNanos = System.nanoTime();
182-
processed = processCompletionsAndHandleOverflow(submissionQueue, completionQueue, this::handle);
183+
processCompletionsAndHandleOverflow(submissionQueue, completionQueue, this::handle);
183184
long activeIoEndTimeNanos = System.nanoTime();
184185
context.reportActiveIoTime(activeIoEndTimeNanos - activeIoStartTimeNanos);
185186
} else {
186-
processed = processCompletionsAndHandleOverflow(submissionQueue, completionQueue, this::handle);
187+
processCompletionsAndHandleOverflow(submissionQueue, completionQueue, this::handle);
187188
}
188-
return processed;
189+
return ioCompletions;
189190
}
190191

191192
private boolean needSubmit(int sqFlags) {
@@ -292,6 +293,7 @@ private void handle(int res, int flags, long udata, ByteBuffer extraCqeData) {
292293
// Just return
293294
return;
294295
}
296+
ioCompletions++;
295297
DefaultIoUringIoRegistration registration = registrations.get(id);
296298
if (registration == null) {
297299
logger.debug("ignoring {} completion for unknown registration (id={}, res={})",
Collapse file

‎transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringIoHandlerWakeupEventCountTest.java‎

Copy file name to clipboard
+271Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.channel.uring;
17+
18+
import io.netty.channel.IoEvent;
19+
import io.netty.channel.IoHandler;
20+
import io.netty.channel.IoHandlerContext;
21+
import io.netty.channel.IoHandlerFactory;
22+
import io.netty.channel.IoRegistration;
23+
import io.netty.util.concurrent.ThreadAwareExecutor;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.Timeout;
27+
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
import java.util.concurrent.atomic.AtomicReference;
31+
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
34+
35+
public class IoUringIoHandlerWakeupEventCountTest {
36+
37+
@BeforeAll
38+
public static void loadJNI() {
39+
assumeTrue(IoUring.isAvailable());
40+
}
41+
42+
/**
43+
* Verify that waking up the handler via wakeup() (which writes to the internal eventfd)
44+
* does NOT count as a real I/O event in the return value of run().
45+
*/
46+
@Test
47+
@Timeout(5)
48+
public void testWakeupDoesNotCountAsIoEvent() throws Exception {
49+
final IoHandlerFactory factory = IoUringIoHandler.newFactory();
50+
final AtomicReference<Thread> handlerThreadRef = new AtomicReference<>();
51+
final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() {
52+
@Override
53+
public boolean isExecutorThread(Thread thread) {
54+
return thread == handlerThreadRef.get();
55+
}
56+
57+
@Override
58+
public void execute(Runnable command) {
59+
command.run();
60+
}
61+
});
62+
handler.initialize();
63+
64+
final AtomicInteger runResult = new AtomicInteger(-1);
65+
final CountDownLatch handlerRunning = new CountDownLatch(1);
66+
final AtomicReference<Throwable> failure = new AtomicReference<>();
67+
68+
Thread handlerThread = new Thread(() -> {
69+
handlerThreadRef.set(Thread.currentThread());
70+
handlerRunning.countDown();
71+
try {
72+
int result = handler.run(new IoHandlerContext() {
73+
@Override
74+
public boolean canBlock() {
75+
return true;
76+
}
77+
78+
@Override
79+
public long delayNanos(long currentTimeNanos) {
80+
return Long.MAX_VALUE;
81+
}
82+
83+
@Override
84+
public long deadlineNanos() {
85+
return -1L;
86+
}
87+
});
88+
runResult.set(result);
89+
} catch (Throwable t) {
90+
failure.set(t);
91+
}
92+
});
93+
handlerThread.start();
94+
95+
// Wait for the handler thread to be running and blocked in io_uring_enter.
96+
handlerRunning.await();
97+
// Small delay to ensure io_uring_enter is entered.
98+
Thread.sleep(100);
99+
100+
// Wakeup from the test thread (not the executor thread) -- writes to eventfd.
101+
handler.wakeup();
102+
103+
handlerThread.join();
104+
105+
Throwable t = failure.get();
106+
if (t != null) {
107+
throw new AssertionError("Handler thread failed", t);
108+
}
109+
110+
// The fix: eventfd wakeup should NOT be counted as a real I/O event.
111+
assertEquals(0, runResult.get(), "wakeup via eventfd should not be counted as an I/O event");
112+
113+
handler.prepareToDestroy();
114+
handler.destroy();
115+
}
116+
117+
/**
118+
* Verify that actual channel I/O (a NOP submitted to a registered handle)
119+
* IS counted in the return value of run().
120+
*/
121+
@Test
122+
@Timeout(5)
123+
public void testRealIoEventIsCounted() throws Exception {
124+
final IoHandlerFactory factory = IoUringIoHandler.newFactory();
125+
final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() {
126+
@Override
127+
public boolean isExecutorThread(Thread thread) {
128+
return true;
129+
}
130+
131+
@Override
132+
public void execute(Runnable command) {
133+
command.run();
134+
}
135+
});
136+
handler.initialize();
137+
138+
final AtomicReference<IoEvent> eventRef = new AtomicReference<>();
139+
IoRegistration registration = handler.register(new IoUringIoHandle() {
140+
@Override
141+
public void handle(IoRegistration registration, IoEvent ioEvent) {
142+
eventRef.set(ioEvent);
143+
}
144+
145+
@Override
146+
public void close() {
147+
// Noop
148+
}
149+
});
150+
151+
// Submit a NOP -- this will produce a completion that counts as real I/O.
152+
registration.submit(new IoUringIoOps(Native.IORING_OP_NOP, (byte) 0,
153+
(short) 0, -1, 0, 0, 0, 0, (short) 0, (short) 0, (short) 0, 0, 0));
154+
155+
// Non-blocking run: io_uring processes the NOP completion.
156+
int handled = handler.run(new IoHandlerContext() {
157+
@Override
158+
public boolean canBlock() {
159+
return false;
160+
}
161+
162+
@Override
163+
public long delayNanos(long currentTimeNanos) {
164+
return 0;
165+
}
166+
167+
@Override
168+
public long deadlineNanos() {
169+
return 0;
170+
}
171+
});
172+
173+
// Real I/O on a registered handle must be counted.
174+
assertEquals(1, handled, "real I/O event on a registered handle should be counted");
175+
176+
registration.cancel();
177+
handler.prepareToDestroy();
178+
handler.destroy();
179+
}
180+
181+
/**
182+
* Verify that when both a wakeup and real I/O occur simultaneously,
183+
* only the real I/O event is counted.
184+
*/
185+
@Test
186+
@Timeout(5)
187+
public void testWakeupPlusRealIoCountsOnlyRealIo() throws Exception {
188+
final IoHandlerFactory factory = IoUringIoHandler.newFactory();
189+
final AtomicReference<Thread> handlerThreadRef = new AtomicReference<>();
190+
final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() {
191+
@Override
192+
public boolean isExecutorThread(Thread thread) {
193+
return thread == handlerThreadRef.get();
194+
}
195+
196+
@Override
197+
public void execute(Runnable command) {
198+
command.run();
199+
}
200+
});
201+
handler.initialize();
202+
203+
final AtomicReference<IoEvent> eventRef = new AtomicReference<>();
204+
IoRegistration registration = handler.register(new IoUringIoHandle() {
205+
@Override
206+
public void handle(IoRegistration registration, IoEvent ioEvent) {
207+
eventRef.set(ioEvent);
208+
}
209+
210+
@Override
211+
public void close() {
212+
// Noop
213+
}
214+
});
215+
216+
final AtomicInteger runResult = new AtomicInteger(-1);
217+
final CountDownLatch handlerRunning = new CountDownLatch(1);
218+
final AtomicReference<Throwable> failure = new AtomicReference<>();
219+
220+
Thread handlerThread = new Thread(() -> {
221+
handlerThreadRef.set(Thread.currentThread());
222+
// Submit a NOP on the handler thread (executor thread).
223+
registration.submit(new IoUringIoOps(Native.IORING_OP_NOP, (byte) 0,
224+
(short) 0, -1, 0, 0, 0, 0, (short) 0, (short) 0, (short) 0, 0, 0));
225+
handlerRunning.countDown();
226+
try {
227+
int result = handler.run(new IoHandlerContext() {
228+
@Override
229+
public boolean canBlock() {
230+
return true;
231+
}
232+
233+
@Override
234+
public long delayNanos(long currentTimeNanos) {
235+
return Long.MAX_VALUE;
236+
}
237+
238+
@Override
239+
public long deadlineNanos() {
240+
return -1L;
241+
}
242+
});
243+
runResult.set(result);
244+
} catch (Throwable t) {
245+
failure.set(t);
246+
}
247+
});
248+
handlerThread.start();
249+
250+
handlerRunning.await();
251+
Thread.sleep(100);
252+
253+
// Trigger wakeup from the test thread.
254+
handler.wakeup();
255+
256+
handlerThread.join();
257+
258+
Throwable t = failure.get();
259+
if (t != null) {
260+
throw new AssertionError("Handler thread failed", t);
261+
}
262+
263+
// Only the real I/O event (NOP) should be counted, not the eventfd wakeup.
264+
assertEquals(1, runResult.get(),
265+
"only real I/O events should be counted, not the internal eventfd wakeup");
266+
267+
registration.cancel();
268+
handler.prepareToDestroy();
269+
handler.destroy();
270+
}
271+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.