DirectByteBufferPool.java
5.3 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
package io.mycat.buffer;
import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.nio.ch.DirectBuffer;
/**
* DirectByteBuffer池,可以分配任意指定大小的DirectByteBuffer,用完需要归还
* @author wuzhih
* @author zagnix
*/
@SuppressWarnings("restriction")
public class DirectByteBufferPool implements BufferPool{
private static final Logger LOGGER = LoggerFactory.getLogger(DirectByteBufferPool.class);
public static final String LOCAL_BUF_THREAD_PREX = "$_";
private ByteBufferPage[] allPages;
private final int chunkSize;
// private int prevAllocatedPage = 0;
//private AtomicInteger prevAllocatedPage;
private AtomicLong prevAllocatedPage;
private final int pageSize;
private final short pageCount;
private final int conReadBuferChunk ;
/**
* 记录对线程ID->该线程的所使用Direct Buffer的size
*/
private final ConcurrentHashMap<Long,Long> memoryUsage;
public DirectByteBufferPool(int pageSize, short chunkSize, short pageCount,int conReadBuferChunk) {
allPages = new ByteBufferPage[pageCount];
this.chunkSize = chunkSize;
this.pageSize = pageSize;
this.pageCount = pageCount;
this.conReadBuferChunk = conReadBuferChunk;
//prevAllocatedPage = new AtomicInteger(0);
prevAllocatedPage = new AtomicLong(0);
for (int i = 0; i < pageCount; i++) {
allPages[i] = new ByteBufferPage(ByteBuffer.allocateDirect(pageSize), chunkSize);
}
memoryUsage = new ConcurrentHashMap<>();
}
public BufferArray allocateArray() {
return new BufferArray(this);
}
/**
* TODO 当页不够时,考虑扩展内存池的页的数量...........
* @param buffer
* @return
*/
public ByteBuffer expandBuffer(ByteBuffer buffer){
int oldCapacity = buffer.capacity();
int newCapacity = oldCapacity << 1;
ByteBuffer newBuffer = allocate(newCapacity);
if(newBuffer != null){
int newPosition = buffer.position();
buffer.flip();
newBuffer.put(buffer);
newBuffer.position(newPosition);
recycle(buffer);
return newBuffer;
}
return null;
}
public ByteBuffer allocate(int size) {
final int theChunkCount = size / chunkSize + (size % chunkSize == 0 ? 0 : 1);
int selectedPage = (int)(prevAllocatedPage.incrementAndGet() % allPages.length);
ByteBuffer byteBuf = allocateBuffer(theChunkCount, 0, selectedPage);
if (byteBuf == null) {
byteBuf = allocateBuffer(theChunkCount, selectedPage, allPages.length);
}
final long threadId = Thread.currentThread().getId();
if(byteBuf !=null){
if (memoryUsage.containsKey(threadId)){
memoryUsage.put(threadId,memoryUsage.get(threadId)+byteBuf.capacity());
}else {
memoryUsage.put(threadId,(long)byteBuf.capacity());
}
}
if(byteBuf==null){
return ByteBuffer.allocate(size);
}
return byteBuf;
}
public void recycle(ByteBuffer theBuf) {
if(theBuf !=null && (!(theBuf instanceof DirectBuffer) )){
theBuf.clear();
return;
}
final long size = theBuf.capacity();
boolean recycled = false;
DirectBuffer thisNavBuf = (DirectBuffer) theBuf;
int chunkCount = theBuf.capacity() / chunkSize;
DirectBuffer parentBuf = (DirectBuffer) thisNavBuf.attachment();
int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / chunkSize);
for (int i = 0; i < allPages.length; i++) {
if ((recycled = allPages[i].recycleBuffer((ByteBuffer) parentBuf, startChunk,
chunkCount) == true)) {
break;
}
}
final long threadId = Thread.currentThread().getId();
if (memoryUsage.containsKey(threadId)) {
memoryUsage.put(threadId, memoryUsage.get(threadId) - size);
}
if (recycled == false) {
LOGGER.warn("warning ,not recycled buffer " + theBuf);
}
}
private ByteBuffer allocateBuffer(int theChunkCount, int startPage, int endPage) {
for (int i = startPage; i < endPage; i++) {
ByteBuffer buffer = allPages[i].allocatChunk(theChunkCount);
if (buffer != null) {
prevAllocatedPage.getAndSet(i);
return buffer;
}
}
return null;
}
public int getChunkSize() {
return chunkSize;
}
@Override
public ConcurrentHashMap<Long,Long> getNetDirectMemoryUsage() {
return memoryUsage;
}
public int getPageSize() {
return pageSize;
}
public short getPageCount() {
return pageCount;
}
public long capacity() {
return (long) pageSize * pageCount;
}
public long size(){
return (long) pageSize * chunkSize * pageCount;
}
//TODO
public int getSharedOptsCount(){
return 0;
}
public ByteBufferPage[] getAllPages() {
return allPages;
}
public int getConReadBuferChunk() {
return conReadBuferChunk;
}
}