2012-12-05 22:11:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.buffer;
|
|
|
|
|
2013-03-05 09:55:24 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2013-04-03 05:08:01 +02:00
|
|
|
import io.netty.util.internal.SystemPropertyUtil;
|
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|
|
|
|
2013-04-03 05:08:01 +02:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(PooledByteBufAllocator.class);
|
|
|
|
|
2013-06-14 05:14:45 +02:00
|
|
|
private static final int DEFAULT_NUM_HEAP_ARENA;
|
|
|
|
private static final int DEFAULT_NUM_DIRECT_ARENA;
|
|
|
|
|
2013-04-03 05:08:01 +02:00
|
|
|
private static final int DEFAULT_PAGE_SIZE;
|
|
|
|
private static final int DEFAULT_MAX_ORDER; // 8192 << 11 = 16 MiB per chunk
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
private static final int MIN_PAGE_SIZE = 4096;
|
|
|
|
private static final int MAX_CHUNK_SIZE = (int) (((long) Integer.MAX_VALUE + 1) / 2);
|
|
|
|
|
2013-04-03 05:08:01 +02:00
|
|
|
static {
|
|
|
|
int defaultPageSize = SystemPropertyUtil.getInt("io.netty.allocator.pageSize", 8192);
|
|
|
|
Throwable pageSizeFallbackCause = null;
|
|
|
|
try {
|
|
|
|
validateAndCalculatePageShifts(defaultPageSize);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
pageSizeFallbackCause = t;
|
|
|
|
defaultPageSize = 8192;
|
|
|
|
}
|
|
|
|
DEFAULT_PAGE_SIZE = defaultPageSize;
|
|
|
|
|
|
|
|
int defaultMaxOrder = SystemPropertyUtil.getInt("io.netty.allocator.maxOrder", 11);
|
|
|
|
Throwable maxOrderFallbackCause = null;
|
|
|
|
try {
|
|
|
|
validateAndCalculateChunkSize(DEFAULT_PAGE_SIZE, defaultMaxOrder);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
maxOrderFallbackCause = t;
|
|
|
|
defaultMaxOrder = 11;
|
|
|
|
}
|
|
|
|
DEFAULT_MAX_ORDER = defaultMaxOrder;
|
|
|
|
|
2013-06-14 05:14:45 +02:00
|
|
|
// Determine reasonable default for nHeapArena and nDirectArena.
|
|
|
|
// Assuming each arena has 3 chunks, the pool should not consume more than 50% of max memory.
|
|
|
|
final Runtime runtime = Runtime.getRuntime();
|
|
|
|
final int defaultChunkSize = DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER;
|
|
|
|
DEFAULT_NUM_HEAP_ARENA = Math.max(0,
|
|
|
|
SystemPropertyUtil.getInt(
|
|
|
|
"io.netty.allocator.numHeapArenas",
|
|
|
|
(int) Math.min(
|
|
|
|
runtime.availableProcessors(),
|
|
|
|
Runtime.getRuntime().maxMemory() / defaultChunkSize / 2 / 3)));
|
|
|
|
DEFAULT_NUM_DIRECT_ARENA = Math.max(0,
|
|
|
|
SystemPropertyUtil.getInt(
|
|
|
|
"io.netty.allocator.numDirectArenas",
|
|
|
|
(int) Math.min(
|
|
|
|
runtime.availableProcessors(),
|
|
|
|
PlatformDependent.maxDirectMemory() / defaultChunkSize / 2 / 3)));
|
|
|
|
|
2013-04-03 05:08:01 +02:00
|
|
|
if (logger.isDebugEnabled()) {
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.numHeapArenas: {}", DEFAULT_NUM_HEAP_ARENA);
|
|
|
|
logger.debug("-Dio.netty.allocator.numDirectArenas: {}", DEFAULT_NUM_DIRECT_ARENA);
|
2013-04-03 05:08:01 +02:00
|
|
|
if (pageSizeFallbackCause == null) {
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.pageSize: {}", DEFAULT_PAGE_SIZE);
|
2013-04-03 05:08:01 +02:00
|
|
|
} else {
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.pageSize: {}", DEFAULT_PAGE_SIZE, pageSizeFallbackCause);
|
2013-04-03 05:08:01 +02:00
|
|
|
}
|
|
|
|
if (maxOrderFallbackCause == null) {
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.maxOrder: {}", DEFAULT_MAX_ORDER);
|
2013-04-03 05:08:01 +02:00
|
|
|
} else {
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.maxOrder: {}", DEFAULT_MAX_ORDER, maxOrderFallbackCause);
|
2013-04-03 05:08:01 +02:00
|
|
|
}
|
2013-07-02 02:23:29 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.chunkSize: {}", DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER);
|
2013-04-03 05:08:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:55:24 +01:00
|
|
|
public static final PooledByteBufAllocator DEFAULT =
|
|
|
|
new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
private final PoolArena<byte[]>[] heapArenas;
|
|
|
|
private final PoolArena<ByteBuffer>[] directArenas;
|
|
|
|
|
|
|
|
final ThreadLocal<PoolThreadCache> threadCache = new ThreadLocal<PoolThreadCache>() {
|
|
|
|
private final AtomicInteger index = new AtomicInteger();
|
|
|
|
@Override
|
|
|
|
protected PoolThreadCache initialValue() {
|
2013-04-27 01:55:16 +02:00
|
|
|
final int idx = index.getAndIncrement();
|
|
|
|
final PoolArena<byte[]> heapArena;
|
|
|
|
final PoolArena<ByteBuffer> directArena;
|
|
|
|
|
|
|
|
if (heapArenas != null) {
|
|
|
|
heapArena = heapArenas[Math.abs(idx % heapArenas.length)];
|
|
|
|
} else {
|
|
|
|
heapArena = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directArenas != null) {
|
|
|
|
directArena = directArenas[Math.abs(idx % directArenas.length)];
|
|
|
|
} else {
|
|
|
|
directArena = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PoolThreadCache(heapArena, directArena);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public PooledByteBufAllocator() {
|
|
|
|
this(false);
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:55:24 +01:00
|
|
|
public PooledByteBufAllocator(boolean preferDirect) {
|
|
|
|
this(preferDirect, DEFAULT_NUM_HEAP_ARENA, DEFAULT_NUM_DIRECT_ARENA, DEFAULT_PAGE_SIZE, DEFAULT_MAX_ORDER);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public PooledByteBufAllocator(int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
|
|
|
this(false, nHeapArena, nDirectArena, pageSize, maxOrder);
|
|
|
|
}
|
|
|
|
|
2013-04-27 01:55:16 +02:00
|
|
|
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
|
|
|
super(preferDirect);
|
2012-12-19 09:35:32 +01:00
|
|
|
|
|
|
|
final int chunkSize = validateAndCalculateChunkSize(pageSize, maxOrder);
|
|
|
|
|
2013-04-27 01:55:16 +02:00
|
|
|
if (nHeapArena < 0) {
|
|
|
|
throw new IllegalArgumentException("nHeapArena: " + nHeapArena + " (expected: >= 0)");
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
2013-04-27 01:55:16 +02:00
|
|
|
if (nDirectArena < 0) {
|
|
|
|
throw new IllegalArgumentException("nDirectArea: " + nDirectArena + " (expected: >= 0)");
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int pageShifts = validateAndCalculatePageShifts(pageSize);
|
|
|
|
|
2013-04-27 01:55:16 +02:00
|
|
|
if (nHeapArena > 0) {
|
|
|
|
heapArenas = newArenaArray(nHeapArena);
|
|
|
|
for (int i = 0; i < heapArenas.length; i ++) {
|
|
|
|
heapArenas[i] = new PoolArena.HeapArena(this, pageSize, maxOrder, pageShifts, chunkSize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
heapArenas = null;
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
2013-05-30 20:24:11 +02:00
|
|
|
if (nDirectArena > 0) {
|
2013-04-27 01:55:16 +02:00
|
|
|
directArenas = newArenaArray(nDirectArena);
|
|
|
|
for (int i = 0; i < directArenas.length; i ++) {
|
|
|
|
directArenas[i] = new PoolArena.DirectArena(this, pageSize, maxOrder, pageShifts, chunkSize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
directArenas = null;
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static <T> PoolArena<T>[] newArenaArray(int size) {
|
|
|
|
return new PoolArena[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int validateAndCalculatePageShifts(int pageSize) {
|
|
|
|
if (pageSize < MIN_PAGE_SIZE) {
|
|
|
|
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: 4096+)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure pageSize is power of 2.
|
|
|
|
boolean found1 = false;
|
|
|
|
int pageShifts = 0;
|
|
|
|
for (int i = pageSize; i != 0 ; i >>= 1) {
|
|
|
|
if ((i & 1) != 0) {
|
|
|
|
if (!found1) {
|
|
|
|
found1 = true;
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!found1) {
|
|
|
|
pageShifts ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pageShifts;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int validateAndCalculateChunkSize(int pageSize, int maxOrder) {
|
|
|
|
if (maxOrder > 14) {
|
|
|
|
throw new IllegalArgumentException("maxOrder: " + maxOrder + " (expected: 0-14)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the resulting chunkSize does not overflow.
|
|
|
|
int chunkSize = pageSize;
|
|
|
|
for (int i = maxOrder; i > 0; i --) {
|
|
|
|
if (chunkSize > MAX_CHUNK_SIZE / 2) {
|
|
|
|
throw new IllegalArgumentException(String.format(
|
|
|
|
"pageSize (%d) << maxOrder (%d) must not exceed %d", pageSize, maxOrder, MAX_CHUNK_SIZE));
|
|
|
|
}
|
|
|
|
chunkSize <<= 1;
|
|
|
|
}
|
|
|
|
return chunkSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
|
|
|
|
PoolThreadCache cache = threadCache.get();
|
2013-04-27 01:55:16 +02:00
|
|
|
PoolArena<byte[]> heapArena = cache.heapArena;
|
2013-12-04 11:03:32 +01:00
|
|
|
|
|
|
|
ByteBuf buf;
|
2013-04-27 01:55:16 +02:00
|
|
|
if (heapArena != null) {
|
2013-12-04 11:03:32 +01:00
|
|
|
buf = heapArena.allocate(cache, initialCapacity, maxCapacity);
|
2013-04-27 01:55:16 +02:00
|
|
|
} else {
|
2013-12-04 11:03:32 +01:00
|
|
|
buf = new UnpooledHeapByteBuf(this, initialCapacity, maxCapacity);
|
2013-04-27 01:55:16 +02:00
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
|
|
|
|
return toLeakAwareBuffer(buf);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
|
|
|
|
PoolThreadCache cache = threadCache.get();
|
2013-04-27 01:55:16 +02:00
|
|
|
PoolArena<ByteBuffer> directArena = cache.directArena;
|
2013-12-04 11:03:32 +01:00
|
|
|
|
|
|
|
ByteBuf buf;
|
2013-04-27 01:55:16 +02:00
|
|
|
if (directArena != null) {
|
2013-12-04 11:03:32 +01:00
|
|
|
buf = directArena.allocate(cache, initialCapacity, maxCapacity);
|
2013-04-27 01:55:16 +02:00
|
|
|
} else {
|
2013-05-01 04:14:21 +02:00
|
|
|
if (PlatformDependent.hasUnsafe()) {
|
2013-12-04 11:03:32 +01:00
|
|
|
buf = new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity);
|
2013-05-01 04:14:21 +02:00
|
|
|
} else {
|
2013-12-04 11:03:32 +01:00
|
|
|
buf = new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
|
2013-05-01 04:14:21 +02:00
|
|
|
}
|
2013-04-27 01:55:16 +02:00
|
|
|
}
|
2013-12-04 11:03:32 +01:00
|
|
|
|
|
|
|
return toLeakAwareBuffer(buf);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
2013-08-16 21:53:47 +02:00
|
|
|
@Override
|
|
|
|
public boolean isDirectBufferPooled() {
|
|
|
|
return directArenas != null;
|
|
|
|
}
|
|
|
|
|
2013-11-08 09:53:57 +01:00
|
|
|
// Too noisy at the moment.
|
|
|
|
//
|
|
|
|
// public String toString() {
|
|
|
|
// StringBuilder buf = new StringBuilder();
|
|
|
|
// buf.append(heapArenas.length);
|
|
|
|
// buf.append(" heap arena(s):");
|
|
|
|
// buf.append(StringUtil.NEWLINE);
|
|
|
|
// for (PoolArena<byte[]> a: heapArenas) {
|
|
|
|
// buf.append(a);
|
|
|
|
// }
|
|
|
|
// buf.append(directArenas.length);
|
|
|
|
// buf.append(" direct arena(s):");
|
|
|
|
// buf.append(StringUtil.NEWLINE);
|
|
|
|
// for (PoolArena<ByteBuffer> a: directArenas) {
|
|
|
|
// buf.append(a);
|
|
|
|
// }
|
|
|
|
// return buf.toString();
|
|
|
|
// }
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|