View Javadoc
1   /*
2    * Copyright 2025 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.ObjectUtil;
19  
20  /**
21   * Configuration class for an {@link IoUringIoHandler},
22   * managing the settings for a {@link RingBuffer} and its io_uring file descriptor.
23   *
24   * <h3>Option Map</h3>
25   * These options are used exclusively during the initialization of the {@link IoUringIoHandler}
26   * to configure the associated io_uring instance.
27   *
28   * <p>
29   * The {@link IoUringIoHandlerConfiguration} class provides the following configurable options:
30   * </p>
31   *
32   * <table border="1" cellspacing="0" cellpadding="6">
33   *   <caption>Available Configuration Options</caption>
34   *   <thead>
35   *     <tr>
36   *       <th>Setter Method</th>
37   *       <th>Description</th>
38   *     </tr>
39   *   </thead>
40   *   <tbody>
41   *     <tr>
42   *       <td>{@link IoUringIoHandlerConfiguration#setRingSize}</td>
43   *       <td>Sets the size of the submission queue for the io_uring instance.</td>
44   *     </tr>
45   *     <tr>
46   *       <td>{@link IoUringIoHandlerConfiguration#setMaxBoundedWorker}</td>
47   *       <td>Defines the maximum number of bounded io_uring worker threads.</td>
48   *     </tr>
49   *     <tr>
50   *       <td>{@link IoUringIoHandlerConfiguration#setMaxUnboundedWorker}</td>
51   *       <td>Defines the maximum number of unbounded io_uring worker threads.</td>
52   *     </tr>
53   *   </tbody>
54   * </table>
55   */
56  
57  public final class IoUringIoHandlerConfiguration {
58      private int ringSize = Native.DEFAULT_RING_SIZE;
59  
60      private int maxBoundedWorker;
61  
62      private int maxUnboundedWorker;
63  
64      /**
65       * Return the ring size of the io_uring instance.
66       * @return the ring size of the io_uring instance.
67       */
68      public int getRingSize() {
69          return ringSize;
70      }
71  
72      /**
73       * Return the maximum number of bounded iowq worker threads.
74       * @return the maximum number of bounded iowq worker threads.
75       */
76      public int getMaxBoundedWorker() {
77          return maxBoundedWorker;
78      }
79  
80      /**
81       * Return the maximum number of unbounded iowq worker threads.
82       * @return the maximum number of unbounded iowq worker threads.
83       */
84      public int getMaxUnboundedWorker() {
85          return maxUnboundedWorker;
86      }
87  
88      /**
89       * Set the ring size of the io_uring instance.
90       * @param ringSize the ring size of the io_uring instance.
91       * @return reference to this, so the API can be used fluently
92       */
93      public IoUringIoHandlerConfiguration setRingSize(int ringSize) {
94          this.ringSize = ObjectUtil.checkPositive(ringSize, "ringSize");
95          return this;
96      }
97  
98      /**
99       * Set the maximum number of bounded iowq worker threads.
100      * @param maxBoundedWorker the maximum number of bounded iowq worker threads,
101      *                         or 0 for the Linux kernel default
102      * @return reference to this, so the API can be used fluently
103      */
104     public IoUringIoHandlerConfiguration setMaxBoundedWorker(int maxBoundedWorker) {
105         this.maxBoundedWorker = ObjectUtil.checkPositiveOrZero(maxBoundedWorker, "maxBoundedWorker");
106         return this;
107     }
108 
109     /**
110      * Set the maximum number of unbounded iowq worker threads.
111      * @param maxUnboundedWorker the maximum number of unbounded iowq worker threads,
112      *                           of 0 for the Linux kernel default
113      * @return reference to this, so the API can be used fluently
114      */
115     public IoUringIoHandlerConfiguration setMaxUnboundedWorker(int maxUnboundedWorker) {
116         this.maxUnboundedWorker = ObjectUtil.checkPositiveOrZero(maxUnboundedWorker, "maxUnboundedWorker");
117         return this;
118     }
119 
120     boolean needRegisterIowqMaxWorker() {
121         return maxBoundedWorker > 0 || maxUnboundedWorker > 0;
122     }
123 }