1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.util.internal.MathUtil;
20 import io.netty.util.internal.ObjectUtil;
21
22 import java.util.Objects;
23
24
25
26
27
28 public final class IoUringBufferRingConfig {
29 private final short bgId;
30 private final short bufferRingSize;
31 private final int batchSize;
32 private final int maxUnreleasedBuffers;
33 private final boolean incremental;
34 private final IoUringBufferRingAllocator allocator;
35 private final boolean batchAllocation;
36
37
38
39
40
41
42
43
44
45
46
47 @Deprecated
48 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int maxUnreleasedBuffers,
49 IoUringBufferRingAllocator allocator) {
50 this(bgId, bufferRingSize, bufferRingSize / 2, maxUnreleasedBuffers,
51 IoUring.isRegisterBufferRingIncSupported(), allocator);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int batchSize, int maxUnreleasedBuffers,
69 boolean incremental, IoUringBufferRingAllocator allocator) {
70 this(bgId, bufferRingSize, batchSize, maxUnreleasedBuffers, incremental, allocator, false);
71 }
72
73 private IoUringBufferRingConfig(short bgId, short bufferRingSize, int batchSize, int maxUnreleasedBuffers,
74 boolean incremental, IoUringBufferRingAllocator allocator, boolean batchAllocation) {
75 this.bgId = (short) ObjectUtil.checkPositiveOrZero(bgId, "bgId");
76 this.bufferRingSize = checkBufferRingSize(bufferRingSize);
77 this.batchSize = MathUtil.findNextPositivePowerOfTwo(
78 ObjectUtil.checkInRange(batchSize, 1, bufferRingSize, "batchSize"));
79 this.maxUnreleasedBuffers = ObjectUtil.checkInRange(
80 maxUnreleasedBuffers, bufferRingSize, Integer.MAX_VALUE, "maxUnreleasedBuffers");
81 if (incremental && !IoUring.isRegisterBufferRingIncSupported()) {
82 throw new IllegalArgumentException("Incremental buffer ring is not supported");
83 }
84 this.incremental = incremental;
85 this.allocator = ObjectUtil.checkNotNull(allocator, "allocator");
86 this.batchAllocation = batchAllocation;
87 }
88
89
90
91
92
93
94 public short bufferGroupId() {
95 return bgId;
96 }
97
98
99
100
101
102
103 public short bufferRingSize() {
104 return bufferRingSize;
105 }
106
107
108
109
110
111
112 public int batchSize() {
113 return batchSize;
114 }
115
116
117
118
119
120
121
122
123 @Deprecated
124 public int maxUnreleasedBuffers() {
125 return maxUnreleasedBuffers;
126 }
127
128
129
130
131
132
133 public IoUringBufferRingAllocator allocator() {
134 return allocator;
135 }
136
137
138
139
140
141
142 public boolean isBatchAllocation() {
143 return batchAllocation;
144 }
145
146
147
148
149
150
151
152
153 public boolean isIncremental() {
154 return incremental;
155 }
156
157 private static short checkBufferRingSize(short bufferRingSize) {
158 if (bufferRingSize < 1) {
159 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: > 0)");
160 }
161
162 boolean isPowerOfTwo = (bufferRingSize & (bufferRingSize - 1)) == 0;
163 if (!isPowerOfTwo) {
164 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: power of 2)");
165 }
166 return bufferRingSize;
167 }
168
169 @Override
170 public boolean equals(Object o) {
171 if (o == null || getClass() != o.getClass()) {
172 return false;
173 }
174 IoUringBufferRingConfig that = (IoUringBufferRingConfig) o;
175 return bgId == that.bgId;
176 }
177
178 @Override
179 public int hashCode() {
180 return Objects.hashCode(bgId);
181 }
182
183 public static Builder builder() {
184 return new Builder();
185 }
186
187 public static final class Builder {
188 private short bgId = -1;
189 private short bufferRingSize = -1;
190 private int batchSize = -1;
191 private boolean incremental = IoUring.isRegisterBufferRingIncSupported();
192 private IoUringBufferRingAllocator allocator;
193 private boolean batchAllocation;
194
195
196
197
198
199
200
201 public Builder bufferGroupId(short bgId) {
202 this.bgId = bgId;
203 return this;
204 }
205
206
207
208
209
210
211
212 public Builder bufferRingSize(short bufferRingSize) {
213 this.bufferRingSize = bufferRingSize;
214 return this;
215 }
216
217
218
219
220
221
222
223 public Builder batchSize(int batchSize) {
224 this.batchSize = batchSize;
225 return this;
226 }
227
228
229
230
231
232
233 public Builder allocator(IoUringBufferRingAllocator allocator) {
234 this.allocator = allocator;
235 return this;
236 }
237
238
239
240
241
242
243
244
245
246 public Builder batchAllocation(boolean batchAllocation) {
247 this.batchAllocation = batchAllocation;
248 return this;
249 }
250
251
252
253
254
255
256
257
258
259 public Builder incremental(boolean incremental) {
260 this.incremental = incremental;
261 return this;
262 }
263
264
265
266
267
268
269 public IoUringBufferRingConfig build() {
270 return new IoUringBufferRingConfig(
271 bgId, bufferRingSize, batchSize, Integer.MAX_VALUE, incremental, allocator, batchAllocation);
272 }
273 }
274 }