View Javadoc
1   /*
2    * Copyright 2024 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.util.internal.PlatformDependent;
19  
20  import java.util.StringJoiner;
21  
22  /**
23   * Completion queue implementation for io_uring.
24   */
25  final class CompletionQueue {
26  
27      //these offsets are used to access specific properties
28      //CQE (https://github.com/axboe/liburing/blob/master/src/include/liburing/io_uring.h#L162)
29      private static final int CQE_USER_DATA_FIELD = 0;
30      private static final int CQE_RES_FIELD = 8;
31      private static final int CQE_FLAGS_FIELD = 12;
32  
33      private static final long CQE_SIZE = 16;
34  
35      //these unsigned integer pointers(shared with the kernel) will be changed by the kernel
36      private final long kHeadAddress;
37      private final long kTailAddress;
38  
39      private final long completionQueueArrayAddress;
40  
41      final int ringSize;
42      final long ringAddress;
43      final int ringFd;
44      final int ringEntries;
45      final int ringCapacity;
46  
47      private final int ringMask;
48      private int ringHead;
49  
50      CompletionQueue(long kHeadAddress, long kTailAddress, long kRingMaskAddress, long kRingEntriesAddress,
51                      long kOverflowAddress, long completionQueueArrayAddress, int ringSize, long ringAddress,
52                      int ringFd, int ringCapacity) {
53          this.kHeadAddress = kHeadAddress;
54          this.kTailAddress = kTailAddress;
55          this.completionQueueArrayAddress = completionQueueArrayAddress;
56          this.ringSize = ringSize;
57          this.ringAddress = ringAddress;
58          this.ringFd = ringFd;
59          this.ringCapacity = ringCapacity;
60  
61          ringEntries = PlatformDependent.getIntVolatile(kRingEntriesAddress);
62          ringMask = PlatformDependent.getIntVolatile(kRingMaskAddress);
63          ringHead = PlatformDependent.getIntVolatile(kHeadAddress);
64      }
65  
66      /**
67       * Returns {@code true} if any completion event is ready to be processed by
68       * {@link #process(CompletionCallback)}, {@code false} otherwise.
69       */
70      boolean hasCompletions() {
71          return ringHead != PlatformDependent.getIntVolatile(kTailAddress);
72      }
73  
74      int count() {
75          return PlatformDependent.getIntVolatile(kTailAddress) - ringHead;
76      }
77  
78      /**
79       * Process the completion events in the {@link CompletionQueue} and return the number of processed
80       * events.
81       */
82      int process(CompletionCallback callback) {
83          int tail = PlatformDependent.getIntVolatile(kTailAddress);
84          try {
85              int i = 0;
86              while (ringHead != tail) {
87                  long cqeAddress = completionQueueArrayAddress + (ringHead & ringMask) * CQE_SIZE;
88  
89                  long udata = PlatformDependent.getLong(cqeAddress + CQE_USER_DATA_FIELD);
90                  int res = PlatformDependent.getInt(cqeAddress + CQE_RES_FIELD);
91                  int flags = PlatformDependent.getInt(cqeAddress + CQE_FLAGS_FIELD);
92  
93                  ringHead++;
94  
95                  i++;
96                  if (!callback.handle(res, flags, udata)) {
97                      // Stop processing. as the callback can not handle any more completions for now,
98                      break;
99                  }
100             }
101             return i;
102         } finally {
103             // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
104             PlatformDependent.putIntOrdered(kHeadAddress, ringHead);
105         }
106     }
107 
108     @Override
109     public String toString() {
110         StringJoiner sb = new StringJoiner(", ", "CompletionQueue [", "]");
111         int tail = PlatformDependent.getIntVolatile(kTailAddress);
112         int head = ringHead;
113         while (head != tail) {
114             long cqeAddress = completionQueueArrayAddress + (ringHead & ringMask) * CQE_SIZE;
115             long udata = PlatformDependent.getLong(cqeAddress + CQE_USER_DATA_FIELD);
116             int res = PlatformDependent.getInt(cqeAddress + CQE_RES_FIELD);
117             int flags = PlatformDependent.getInt(cqeAddress + CQE_FLAGS_FIELD);
118 
119             sb.add("(res=" + res).add(", flags=" + flags).add(", udata=" + udata).add(")");
120             head++;
121         }
122         return sb.toString();
123     }
124 }