MyCatMemoryAllocator.java
5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package io.mycat.buffer;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.internal.PlatformDependent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.concurrent.*;
/**
* Netty Direct Memory 分配器,为mycat提供内存池管理功能
*
* @author zagnix
* @create 2017-01-18 11:01
*/
public class MyCatMemoryAllocator implements ByteBufAllocator {
private static final Logger LOGGER = LoggerFactory.getLogger(MyCatMemoryAllocator.class);
public final ConcurrentHashMap<Long,ByteBuf> recycleMaps = new ConcurrentHashMap<>();
private final static MyCatMemoryAllocator INSTANCE =
new MyCatMemoryAllocator(Runtime.getRuntime().availableProcessors()*2);
/** netty memory pool alloctor*/
private final PooledByteBufAllocator alloc;
/**arena 的数量,一般设置cpu cores*2 */
private final int numberOfArenas;
/** ChunkSize 大小 = pageSize << maxOrder */
private final int chunkSize;
/**页大小*/
private final int pageSize;
/**
* numberOfArenas 设置为处理器cores*2
* @param numberOfArenas
*/
public MyCatMemoryAllocator(int numberOfArenas){
this.numberOfArenas = numberOfArenas;
if (!PlatformDependent.hasUnsafe()) {
LOGGER.warn("Using direct memory, but sun.misc.Unsafe not available.");
}
boolean preferDirect = true;
this.pageSize = 8192*2;
int maxOrder = 11;
this.chunkSize = pageSize << maxOrder;
int numDirectArenas = numberOfArenas;
int numHeapArenas = 0;
/** for 4.1.x*/
this.alloc = new PooledByteBufAllocator(
preferDirect,
numHeapArenas,
numDirectArenas,
pageSize,
maxOrder,
512,
256,
64,
true);
/**for 5.0.x
this.alloc = new PooledByteBufAllocator(preferDirect);**/
}
public static MyCatMemoryAllocator getINSTANCE() {
return INSTANCE;
}
/**
* @return alloc
*/
public PooledByteBufAllocator getAlloc() {
return alloc;
}
/**
* Returns the number of arenas.
*
* @return Number of arenas.
*/
public int getNumberOfArenas() {
return numberOfArenas;
}
/**
* Returns the chunk size.
*
* @return Chunk size.
*/
public int getChunkSize() {
return chunkSize;
}
/**
* page Size
* @return page Size
*/
public int getPageSize() {
return pageSize;
}
@Override
public ByteBuf buffer() {
return alloc.buffer();
}
@Override
public ByteBuf buffer(int initialCapacity) {
return alloc.buffer(initialCapacity);
}
@Override
public ByteBuf buffer(int initialCapacity, int maxCapacity) {
return alloc.buffer(initialCapacity, maxCapacity);
}
@Override
public ByteBuf ioBuffer() {
return alloc.ioBuffer();
}
@Override
public ByteBuf ioBuffer(int initialCapacity) {
return alloc.ioBuffer(initialCapacity);
}
@Override
public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
return alloc.ioBuffer(initialCapacity, maxCapacity);
}
@Override
public ByteBuf heapBuffer() {
throw new UnsupportedOperationException("Heap buffer");
}
@Override
public ByteBuf heapBuffer(int initialCapacity) {
throw new UnsupportedOperationException("Heap buffer");
}
@Override
public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
throw new UnsupportedOperationException("Heap buffer");
}
@Override
public ByteBuf directBuffer() {
return alloc.directBuffer();
}
@Override
public ByteBuf directBuffer(int initialCapacity) {
return alloc.directBuffer(initialCapacity);
}
@Override
public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
return alloc.directBuffer(initialCapacity, maxCapacity);
}
@Override
public CompositeByteBuf compositeBuffer() {
return alloc.compositeBuffer();
}
@Override
public CompositeByteBuf compositeBuffer(int maxNumComponents) {
return alloc.compositeBuffer(maxNumComponents);
}
@Override
public CompositeByteBuf compositeHeapBuffer() {
throw new UnsupportedOperationException("Heap buffer");
}
@Override
public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
throw new UnsupportedOperationException("Heap buffer");
}
@Override
public CompositeByteBuf compositeDirectBuffer() {
return alloc.compositeDirectBuffer();
}
@Override
public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
return alloc.compositeDirectBuffer(maxNumComponents);
}
@Override
public boolean isDirectBufferPooled() {
return alloc.isDirectBufferPooled();
}
@Override
public int calculateNewCapacity(int i, int i1) {
return 0;
}
}