ByteBufferArena.java
6.62 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
213
214
package io.mycat.buffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* 仿照Netty的思路,针对MyCat内存缓冲策略优化
* ByteBufferArena维护着锁还有所有list
*
* @author Hash Zhang
* @version 1.0
* @time 17:19 2016/5/17
* @see @https://github.com/netty/netty
*/
public class ByteBufferArena implements BufferPool {
private static final Logger LOGGER = LoggerFactory.getLogger(ByteBufferChunkList.class);
private final ByteBufferChunkList q[];
private final AtomicInteger chunkCount = new AtomicInteger(0);
private final AtomicInteger failCount = new AtomicInteger(0);
private static final int FAIL_THRESHOLD = 1000;
private final int pageSize;
private final int chunkSize;
private final AtomicLong capacity;
private final AtomicLong size;
private final ConcurrentHashMap<Thread, Integer> sharedOptsCount;
/**
* 记录对线程ID->该线程的所使用Direct Buffer的size
*/
private final ConcurrentHashMap<Long,Long> memoryUsage;
private final int conReadBuferChunk;
public ByteBufferArena(int chunkSize, int pageSize, int chunkCount, int conReadBuferChunk) {
try {
this.chunkSize = chunkSize;
this.pageSize = pageSize;
this.chunkCount.set(chunkCount);
this.conReadBuferChunk = conReadBuferChunk;
q = new ByteBufferChunkList[6];
q[5] = new ByteBufferChunkList(100, Integer.MAX_VALUE, chunkSize, pageSize, 0);
q[4] = new ByteBufferChunkList(75, 100, chunkSize, pageSize, 0);
q[3] = new ByteBufferChunkList(50, 100, chunkSize, pageSize, 0);
q[2] = new ByteBufferChunkList(25, 75, chunkSize, pageSize, 0);
q[1] = new ByteBufferChunkList(1, 50, chunkSize, pageSize, 0);
q[0] = new ByteBufferChunkList(Integer.MIN_VALUE, 25, chunkSize, pageSize, chunkCount);
q[0].nextList = q[1];
q[1].nextList = q[2];
q[2].nextList = q[3];
q[3].nextList = q[4];
q[4].nextList = q[5];
q[5].nextList = null;
q[5].prevList = q[4];
q[4].prevList = q[3];
q[3].prevList = q[2];
q[2].prevList = q[1];
q[1].prevList = q[0];
q[0].prevList = null;
capacity = new AtomicLong(6 * chunkCount * chunkSize);
size = new AtomicLong(6 * chunkCount * chunkSize);
sharedOptsCount = new ConcurrentHashMap<>();
memoryUsage = new ConcurrentHashMap<>();
} finally {
}
}
@Override
public ByteBuffer allocate(int reqCapacity) {
try {
ByteBuffer byteBuffer = null;
int i = 0, count = 0;
while (byteBuffer == null) {
if (i > 5) {
i = 0;
count = failCount.incrementAndGet();
if (count > FAIL_THRESHOLD) {
try {
expand();
} finally {
}
}
}
byteBuffer = q[i].allocate(reqCapacity);
i++;
}
// if (count > 0) {
// System.out.println("count: " + count);
// System.out.println(failCount.get());
// }
// printList();
capacity.addAndGet(-reqCapacity);
final Thread thread = Thread.currentThread();
final long threadId = thread.getId();
if (memoryUsage.containsKey(threadId)){
memoryUsage.put(threadId,memoryUsage.get(thread.getId())+reqCapacity);
}else {
memoryUsage.put(threadId, (long) reqCapacity);
}
if (sharedOptsCount.contains(thread)) {
int currentCount = sharedOptsCount.get(thread);
currentCount++;
sharedOptsCount.put(thread,currentCount);
} else{
sharedOptsCount.put(thread,0);
}
return byteBuffer;
} finally {
}
}
private void expand() {
LOGGER.warn("Current Buffer Size is not enough! Expanding Byte buffer!");
ByteBufferChunk byteBufferChunk = new ByteBufferChunk(pageSize, chunkSize);
q[0].byteBufferChunks.add(byteBufferChunk);
failCount.set(0);
}
@Override
public void recycle(ByteBuffer byteBuffer) {
final long size = byteBuffer != null?byteBuffer.capacity():0;
try {
int i;
for (i = 0; i < 6; i++) {
if (q[i].free(byteBuffer)) {
break;
}
}
if (i > 5) {
LOGGER.warn("This ByteBuffer is not maintained in ByteBufferArena!");
return;
}
final Thread thread = Thread.currentThread();
final long threadId = thread.getId();
if (memoryUsage.containsKey(threadId)){
memoryUsage.put(threadId,memoryUsage.get(thread.getId())-size);
}
if (sharedOptsCount.contains(thread)) {
int currentCount = sharedOptsCount.get(thread);
currentCount--;
sharedOptsCount.put(thread,currentCount);
} else{
sharedOptsCount.put(thread,0);
}
capacity.addAndGet(byteBuffer.capacity());
return;
} finally {
}
}
private void printList() {
for (int i = 0; i < 6; i++) {
System.out.println(i + ":" + q[i].byteBufferChunks.toString());
}
}
@Override
public long capacity() {
return capacity.get();
}
@Override
public long size() {
return size.get();
}
@Override
public int getConReadBuferChunk() {
return conReadBuferChunk;
}
@Override
public int getSharedOptsCount() {
final Set<Integer> integers = (Set<Integer>) sharedOptsCount.values();
int count = 0;
for(int i : integers){
count += i;
}
return count;
}
/**
* 这里pageSize就是DirectByteBuffer的chunksize
* @return
*/
@Override
public int getChunkSize() {
return pageSize;
}
@Override
public ConcurrentHashMap<Long, Long> getNetDirectMemoryUsage() {
return memoryUsage;
}
@Override
public BufferArray allocateArray() {
return new BufferArray(this);
}
}