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.util.internal.MathUtil;
19 import io.netty.util.internal.ObjectUtil;
20
21 import java.util.Objects;
22
23
24
25
26
27 public final class IoUringBufferRingConfig {
28 private final short bgId;
29 private final short bufferRingSize;
30 private final int batchSize;
31 private final int maxUnreleasedBuffers;
32 private final boolean incremental;
33 private final IoUringBufferRingAllocator allocator;
34
35
36
37
38
39
40
41
42
43
44
45
46 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int maxUnreleasedBuffers,
47 IoUringBufferRingAllocator allocator) {
48 this(bgId, bufferRingSize, bufferRingSize / 2, maxUnreleasedBuffers,
49 IoUring.isRegisterBufferRingIncSupported(), allocator);
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int batchSize, int maxUnreleasedBuffers,
67 boolean incremental, IoUringBufferRingAllocator allocator) {
68 this.bgId = (short) ObjectUtil.checkPositiveOrZero(bgId, "bgId");
69 this.bufferRingSize = checkBufferRingSize(bufferRingSize);
70 this.batchSize = MathUtil.findNextPositivePowerOfTwo(
71 ObjectUtil.checkInRange(batchSize, 1, bufferRingSize, "batchSize"));
72 this.maxUnreleasedBuffers = ObjectUtil.checkInRange(
73 maxUnreleasedBuffers, bufferRingSize, Integer.MAX_VALUE, "maxUnreleasedBuffers");
74 if (incremental && !IoUring.isRegisterBufferRingIncSupported()) {
75 throw new IllegalArgumentException("Incremental buffer ring is not supported");
76 }
77 this.incremental = incremental;
78 this.allocator = ObjectUtil.checkNotNull(allocator, "allocator");
79 }
80
81
82
83
84
85
86 public short bufferGroupId() {
87 return bgId;
88 }
89
90
91
92
93
94
95 public short bufferRingSize() {
96 return bufferRingSize;
97 }
98
99
100
101
102
103
104 public int batchSize() {
105 return batchSize;
106 }
107
108
109
110
111
112
113
114 public int maxUnreleasedBuffers() {
115 return maxUnreleasedBuffers;
116 }
117
118
119
120
121
122
123 public IoUringBufferRingAllocator allocator() {
124 return allocator;
125 }
126
127
128
129
130
131
132
133
134 public boolean isIncremental() {
135 return incremental;
136 }
137
138 private static short checkBufferRingSize(short bufferRingSize) {
139 if (bufferRingSize < 1) {
140 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: > 0)");
141 }
142
143 boolean isPowerOfTwo = (bufferRingSize & (bufferRingSize - 1)) == 0;
144 if (!isPowerOfTwo) {
145 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: power of 2)");
146 }
147 return bufferRingSize;
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (o == null || getClass() != o.getClass()) {
153 return false;
154 }
155 IoUringBufferRingConfig that = (IoUringBufferRingConfig) o;
156 return bgId == that.bgId;
157 }
158
159 @Override
160 public int hashCode() {
161 return Objects.hashCode(bgId);
162 }
163 }