|
39 | 39 | import java.net.SocketAddress; |
40 | 40 | import java.nio.ByteBuffer; |
41 | 41 | import java.nio.channels.WritableByteChannel; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.List; |
42 | 45 |
|
43 | 46 | import static io.netty.channel.unix.Errors.ioResult; |
44 | 47 |
|
@@ -254,6 +257,57 @@ protected Object filterOutboundMessage(Object msg) { |
254 | 257 | return super.filterOutboundMessage(msg); |
255 | 258 | } |
256 | 259 |
|
| 260 | + /** |
| 261 | + * Collects and retains the messages backing a submitted iov prefix. Keep the non-empty NIO segment |
| 262 | + * counting aligned with {@link IovArray#add(ByteBuf, int, int)}. The readable ranges must still match |
| 263 | + * those used at submission; the shared iov memory itself may already have been reused. |
| 264 | + */ |
| 265 | + static final class IovBufferCollector implements ChannelOutboundBuffer.MessageProcessor { |
| 266 | + private List<ReferenceCounted> buffers; |
| 267 | + private int remaining; |
| 268 | + |
| 269 | + List<ReferenceCounted> collect(ChannelOutboundBuffer buffer, int count) { |
| 270 | + if (count == 0) { |
| 271 | + return Collections.emptyList(); |
| 272 | + } |
| 273 | + ByteBuf first = (ByteBuf) buffer.current(); |
| 274 | + // A single message or iov only needs one retained owner, even for a partial composite write. |
| 275 | + if (buffer.size() == 1 || count == 1 && first.isReadable()) { |
| 276 | + return Collections.<ReferenceCounted>singletonList(first.retain()); |
| 277 | + } |
| 278 | + buffers = new ArrayList<>(Math.min(count, buffer.size())); |
| 279 | + remaining = count; |
| 280 | + try { |
| 281 | + buffer.forEachFlushedMessage(this); |
| 282 | + return buffers; |
| 283 | + } catch (Exception e) { |
| 284 | + throw new IllegalStateException(e); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public boolean processMessage(Object msg) { |
| 290 | + ByteBuf buf = (ByteBuf) msg; |
| 291 | + if (buf.isReadable()) { |
| 292 | + buffers.add(buf.retain()); |
| 293 | + remaining = remainingIovs(buf, remaining); |
| 294 | + } |
| 295 | + return remaining != 0; |
| 296 | + } |
| 297 | + |
| 298 | + static int remainingIovs(ByteBuf buf, int remaining) { |
| 299 | + if (buf.nioBufferCount() == 1) { |
| 300 | + return remaining - 1; |
| 301 | + } |
| 302 | + for (ByteBuffer nioBuffer : buf.nioBuffers(buf.readerIndex(), buf.readableBytes())) { |
| 303 | + if (nioBuffer.remaining() != 0 && --remaining == 0) { |
| 304 | + break; |
| 305 | + } |
| 306 | + } |
| 307 | + return remaining; |
| 308 | + } |
| 309 | + } |
| 310 | + |
257 | 311 | protected class IoUringStreamUnsafe extends AbstractUringUnsafe { |
258 | 312 |
|
259 | 313 | private ByteBuf readBuffer; |
@@ -271,39 +325,27 @@ protected int scheduleWriteMultiple(ChannelOutboundBuffer in) { |
271 | 325 | IovArray iovArray = handler.iovArray(); |
272 | 326 | int offset = iovArray.count(); |
273 | 327 |
|
274 | | - IovArrayReferenceCollector collector = handler.iovArrayReferenceCollector(); |
275 | 328 | try { |
276 | | - try { |
277 | | - in.forEachFlushedMessage(filterWriteMultiple(collector)); |
278 | | - } catch (Exception e) { |
279 | | - // This should never happen, anyway fallback to single write. |
280 | | - return scheduleWriteSingle(in.current()); |
281 | | - } |
282 | | - long iovArrayAddress = iovArray.memoryAddress(offset); |
283 | | - int iovArrayLength = iovArray.count() - offset; |
284 | | - // Should not use sendmsg_zc, just use normal writev. |
285 | | - IoUringIoOps ops = IoUringIoOps.newWritev( |
286 | | - fd, (byte) 0, 0, iovArrayAddress, iovArrayLength, nextOpsId()); |
287 | | - |
288 | | - byte opCode = ops.opcode(); |
289 | | - // record(...) copies the collector's references into the slot, so the collector stays reusable. |
290 | | - writeTracker.recordStream(opCode, collector.referencesArray(), collector.referencesCount()); |
291 | | - writeId = registration.submit(ops); |
292 | | - writeOpCode = opCode; |
293 | | - if (writeId == 0) { |
294 | | - writeTracker.abandonStream(); |
295 | | - return 0; |
296 | | - } |
297 | | - return 1; |
298 | | - } finally { |
299 | | - // The slot copied the references it needs, and an exception must not leave the event loop's |
300 | | - // shared collector holding this write's buffers. |
301 | | - collector.reset(); |
| 329 | + in.forEachFlushedMessage(filterWriteMultiple(iovArray)); |
| 330 | + } catch (Exception e) { |
| 331 | + // This should never happen, anyway fallback to single write. |
| 332 | + return scheduleWriteSingle(in.current()); |
302 | 333 | } |
| 334 | + long iovArrayAddress = iovArray.memoryAddress(offset); |
| 335 | + int iovArrayLength = iovArray.count() - offset; |
| 336 | + // Should not use sendmsg_zc, just use normal writev. |
| 337 | + IoUringIoOps ops = IoUringIoOps.newWritev(fd, (byte) 0, 0, iovArrayAddress, iovArrayLength, nextOpsId()); |
| 338 | + byte opCode = ops.opcode(); |
| 339 | + writeId = submitWrite(ops); |
| 340 | + writeOpCode = opCode; |
| 341 | + if (writeId == 0) { |
| 342 | + return 0; |
| 343 | + } |
| 344 | + return 1; |
303 | 345 | } |
304 | 346 |
|
305 | | - protected ChannelOutboundBuffer.MessageProcessor filterWriteMultiple(IovArrayReferenceCollector collector) { |
306 | | - return collector; |
| 347 | + protected ChannelOutboundBuffer.MessageProcessor filterWriteMultiple(IovArray iovArray) { |
| 348 | + return iovArray; |
307 | 349 | } |
308 | 350 |
|
309 | 351 | @Override |
@@ -331,15 +373,9 @@ protected int scheduleWriteSingle(Object msg) { |
331 | 373 | ops = IoUringIoOps.newSend(fd, (byte) 0, 0, address, length, nextOpsId()); |
332 | 374 | } |
333 | 375 | byte opCode = ops.opcode(); |
334 | | - // A splice picks its own data to tell its two stages apart, so it never enters the channel-level |
335 | | - // slot array used for zero-copy writes. It still has to occupy this single slot though: the file and |
336 | | - // pipe descriptors it splices between have to outlive the SQE, and writeTracker.retainAll() |
337 | | - // only retains what was recorded here. |
338 | | - writeTracker.recordStream(opCode, (ReferenceCounted) msg); |
339 | | - writeId = registration.submit(ops); |
| 376 | + writeId = submitWrite(ops); |
340 | 377 | writeOpCode = opCode; |
341 | 378 | if (writeId == 0) { |
342 | | - writeTracker.abandonStream(); |
343 | 379 | return 0; |
344 | 380 | } |
345 | 381 | return 1; |
@@ -389,19 +425,32 @@ private int scheduleWriteFileRegion(int fd, IoRegistration registration, FileReg |
389 | 425 | long address = IoUring.memoryAddress(buf) + buf.readerIndex(); |
390 | 426 | int length = buf.readableBytes(); |
391 | 427 | IoUringIoOps ops = IoUringIoOps.newSend(fd, (byte) 0, 0, address, length, nextOpsId()); |
392 | | - byte opCode = ops.opcode(); |
393 | | - writeTracker.recordStream(opCode, buf); |
394 | | - writeId = registration.submit(ops); |
395 | | - writeOpCode = opCode; |
396 | | - if (writeId == 0) { |
397 | | - writeTracker.abandonStream(); |
398 | | - // Submission only fails when the registration is no longer valid (channel is |
399 | | - // being deregistered). Ending the slot above is the only cleanup needed here: |
400 | | - // unregistered() will release fileRegionChunkBuf and the outbound buffer will |
401 | | - // release the FileRegion -- mirroring the plain ByteBuf path above. |
402 | | - return 0; |
| 428 | + try { |
| 429 | + byte opCode = ops.opcode(); |
| 430 | + writeId = submitWrite(ops); |
| 431 | + writeOpCode = opCode; |
| 432 | + if (writeId == 0) { |
| 433 | + return 0; |
| 434 | + } |
| 435 | + return 1; |
| 436 | + } finally { |
| 437 | + if (writeId == 0) { |
| 438 | + releaseFileRegionChunkBuf(); |
| 439 | + } |
403 | 440 | } |
404 | | - return 1; |
| 441 | + } |
| 442 | + |
| 443 | + @Override |
| 444 | + List<ReferenceCounted> retainWriteBuffers(ChannelOutboundBuffer buffer) { |
| 445 | + if (currentWrite.opcode() == Native.IORING_OP_WRITEV) { |
| 446 | + return new IovBufferCollector().collect(buffer, currentWrite.len()); |
| 447 | + } |
| 448 | + Object msg = buffer.current(); |
| 449 | + // A generic FileRegion owns an independent chunk buffer until its SEND completes. |
| 450 | + if (msg instanceof ByteBuf || msg instanceof IoUringFileRegion) { |
| 451 | + return Collections.singletonList(((ReferenceCounted) msg).retain()); |
| 452 | + } |
| 453 | + return Collections.emptyList(); |
405 | 454 | } |
406 | 455 |
|
407 | 456 | private int calculateRecvFlags(boolean first) { |
@@ -702,8 +751,6 @@ boolean writeComplete0(byte op, int res, int flags, long data, int outstanding) |
702 | 751 | // See https://man7.org/linux/man-pages/man2/io_uring_enter.2.html section: IORING_OP_SEND_ZC |
703 | 752 | writeId = 0; |
704 | 753 | writeOpCode = 0; |
705 | | - // A completion that never went through the slot finds it inactive, which makes this a no-op. |
706 | | - writeTracker.completeStream(flags); |
707 | 754 | } |
708 | 755 | ChannelOutboundBuffer channelOutboundBuffer = unsafe().outboundBuffer(); |
709 | 756 | if (channelOutboundBuffer == null) { |
@@ -790,8 +837,6 @@ private void releaseFileRegionChunkBuf() { |
790 | 837 |
|
791 | 838 | @Override |
792 | 839 | public void unregistered() { |
793 | | - // Abandons the single slot through writeTracker.releaseAll() before the chunk buffer is |
794 | | - // dropped below, so a reference a shutdown retained on that buffer is released first. |
795 | 840 | super.unregistered(); |
796 | 841 | assert readBuffer == null; |
797 | 842 | releaseFileRegionChunkBuf(); |
|