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;
|
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
import io.netty.util.concurrent.FastThreadLocal;
|
2016-11-22 16:18:23 +01:00
|
|
|
import io.netty.util.concurrent.FastThreadLocalThread;
|
2013-03-05 09:55:24 +01:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2016-04-06 09:25:22 +02:00
|
|
|
import io.netty.util.internal.StringUtil;
|
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;
|
2015-05-13 17:15:06 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
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
|
2014-03-01 15:47:03 +01:00
|
|
|
private static final int DEFAULT_TINY_CACHE_SIZE;
|
|
|
|
private static final int DEFAULT_SMALL_CACHE_SIZE;
|
|
|
|
private static final int DEFAULT_NORMAL_CACHE_SIZE;
|
|
|
|
private static final int DEFAULT_MAX_CACHED_BUFFER_CAPACITY;
|
|
|
|
private static final int DEFAULT_CACHE_TRIM_INTERVAL;
|
2016-11-22 16:18:23 +01:00
|
|
|
private static final boolean DEFAULT_USE_CACHE_FOR_ALL_THREADS;
|
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();
|
2015-06-17 09:39:54 +02:00
|
|
|
|
|
|
|
// Use 2 * cores by default to reduce condition as we use 2 * cores for the number of EventLoops
|
|
|
|
// in NIO and EPOLL as well. If we choose a smaller number we will run into hotspots as allocation and
|
|
|
|
// deallocation needs to be synchronized on the PoolArena.
|
|
|
|
// See https://github.com/netty/netty/issues/3888
|
|
|
|
final int defaultMinNumArena = runtime.availableProcessors() * 2;
|
2013-06-14 05:14:45 +02:00
|
|
|
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(
|
2015-06-17 09:39:54 +02:00
|
|
|
defaultMinNumArena,
|
|
|
|
runtime.maxMemory() / defaultChunkSize / 2 / 3)));
|
2013-06-14 05:14:45 +02:00
|
|
|
DEFAULT_NUM_DIRECT_ARENA = Math.max(0,
|
|
|
|
SystemPropertyUtil.getInt(
|
|
|
|
"io.netty.allocator.numDirectArenas",
|
|
|
|
(int) Math.min(
|
2015-06-17 09:39:54 +02:00
|
|
|
defaultMinNumArena,
|
2013-06-14 05:14:45 +02:00
|
|
|
PlatformDependent.maxDirectMemory() / defaultChunkSize / 2 / 3)));
|
|
|
|
|
2014-03-01 15:47:03 +01:00
|
|
|
// cache sizes
|
|
|
|
DEFAULT_TINY_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.tinyCacheSize", 512);
|
|
|
|
DEFAULT_SMALL_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.smallCacheSize", 256);
|
|
|
|
DEFAULT_NORMAL_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.normalCacheSize", 64);
|
|
|
|
|
|
|
|
// 32 kb is the default maximum capacity of the cached buffer. Similar to what is explained in
|
|
|
|
// 'Scalable memory allocation using jemalloc'
|
|
|
|
DEFAULT_MAX_CACHED_BUFFER_CAPACITY = SystemPropertyUtil.getInt(
|
|
|
|
"io.netty.allocator.maxCachedBufferCapacity", 32 * 1024);
|
|
|
|
|
|
|
|
// the number of threshold of allocations when cached entries will be freed up if not frequently used
|
|
|
|
DEFAULT_CACHE_TRIM_INTERVAL = SystemPropertyUtil.getInt(
|
|
|
|
"io.netty.allocator.cacheTrimInterval", 8192);
|
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
DEFAULT_USE_CACHE_FOR_ALL_THREADS = SystemPropertyUtil.getBoolean(
|
|
|
|
"io.netty.allocator.useCacheForAllThreads", true);
|
|
|
|
|
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);
|
2014-03-01 15:47:03 +01:00
|
|
|
logger.debug("-Dio.netty.allocator.tinyCacheSize: {}", DEFAULT_TINY_CACHE_SIZE);
|
|
|
|
logger.debug("-Dio.netty.allocator.smallCacheSize: {}", DEFAULT_SMALL_CACHE_SIZE);
|
|
|
|
logger.debug("-Dio.netty.allocator.normalCacheSize: {}", DEFAULT_NORMAL_CACHE_SIZE);
|
|
|
|
logger.debug("-Dio.netty.allocator.maxCachedBufferCapacity: {}", DEFAULT_MAX_CACHED_BUFFER_CAPACITY);
|
2014-05-30 11:43:40 +02:00
|
|
|
logger.debug("-Dio.netty.allocator.cacheTrimInterval: {}", DEFAULT_CACHE_TRIM_INTERVAL);
|
2017-01-24 14:26:23 +01:00
|
|
|
logger.debug("-Dio.netty.allocator.useCacheForAllThreads: {}", DEFAULT_USE_CACHE_FOR_ALL_THREADS);
|
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;
|
2014-03-01 15:47:03 +01:00
|
|
|
private final int tinyCacheSize;
|
|
|
|
private final int smallCacheSize;
|
|
|
|
private final int normalCacheSize;
|
2015-05-13 17:15:06 +02:00
|
|
|
private final List<PoolArenaMetric> heapArenaMetrics;
|
|
|
|
private final List<PoolArenaMetric> directArenaMetrics;
|
2015-05-25 21:00:24 +02:00
|
|
|
private final PoolThreadLocalCache threadCache;
|
2012-12-05 22:11:48 +01:00
|
|
|
|
|
|
|
public PooledByteBufAllocator() {
|
|
|
|
this(false);
|
|
|
|
}
|
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
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
|
|
|
}
|
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
2012-12-05 22:11:48 +01:00
|
|
|
public PooledByteBufAllocator(int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
|
|
|
this(false, nHeapArena, nDirectArena, pageSize, maxOrder);
|
|
|
|
}
|
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
/**
|
|
|
|
* @deprecated use
|
|
|
|
* {@link PooledByteBufAllocator#PooledByteBufAllocator(boolean, int, int, int, int, int, int, int, boolean)}
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2013-04-27 01:55:16 +02:00
|
|
|
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
2014-03-01 15:47:03 +01:00
|
|
|
this(preferDirect, nHeapArena, nDirectArena, pageSize, maxOrder,
|
|
|
|
DEFAULT_TINY_CACHE_SIZE, DEFAULT_SMALL_CACHE_SIZE, DEFAULT_NORMAL_CACHE_SIZE);
|
|
|
|
}
|
2012-12-19 09:35:32 +01:00
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
/**
|
|
|
|
* @deprecated use
|
|
|
|
* {@link PooledByteBufAllocator#PooledByteBufAllocator(boolean, int, int, int, int, int, int, int, boolean)}
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2014-03-01 15:47:03 +01:00
|
|
|
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
|
|
|
|
int tinyCacheSize, int smallCacheSize, int normalCacheSize) {
|
2016-11-22 16:18:23 +01:00
|
|
|
this(preferDirect, nHeapArena, nDirectArena, pageSize, maxOrder, tinyCacheSize, smallCacheSize,
|
|
|
|
normalCacheSize, DEFAULT_USE_CACHE_FOR_ALL_THREADS);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
|
|
|
|
int tinyCacheSize, int smallCacheSize, int normalCacheSize,
|
|
|
|
boolean useCacheForAllThreads) {
|
2014-03-01 15:47:03 +01:00
|
|
|
super(preferDirect);
|
2016-11-22 16:18:23 +01:00
|
|
|
threadCache = new PoolThreadLocalCache(useCacheForAllThreads);
|
2014-03-01 15:47:03 +01:00
|
|
|
this.tinyCacheSize = tinyCacheSize;
|
|
|
|
this.smallCacheSize = smallCacheSize;
|
|
|
|
this.normalCacheSize = normalCacheSize;
|
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);
|
2015-05-13 17:15:06 +02:00
|
|
|
List<PoolArenaMetric> metrics = new ArrayList<PoolArenaMetric>(heapArenas.length);
|
2013-04-27 01:55:16 +02:00
|
|
|
for (int i = 0; i < heapArenas.length; i ++) {
|
2015-05-13 17:15:06 +02:00
|
|
|
PoolArena.HeapArena arena = new PoolArena.HeapArena(this, pageSize, maxOrder, pageShifts, chunkSize);
|
|
|
|
heapArenas[i] = arena;
|
|
|
|
metrics.add(arena);
|
2013-04-27 01:55:16 +02:00
|
|
|
}
|
2015-05-13 17:15:06 +02:00
|
|
|
heapArenaMetrics = Collections.unmodifiableList(metrics);
|
2013-04-27 01:55:16 +02:00
|
|
|
} else {
|
|
|
|
heapArenas = null;
|
2015-05-13 17:15:06 +02:00
|
|
|
heapArenaMetrics = Collections.emptyList();
|
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);
|
2015-05-13 17:15:06 +02:00
|
|
|
List<PoolArenaMetric> metrics = new ArrayList<PoolArenaMetric>(directArenas.length);
|
2013-04-27 01:55:16 +02:00
|
|
|
for (int i = 0; i < directArenas.length; i ++) {
|
2015-05-13 17:15:06 +02:00
|
|
|
PoolArena.DirectArena arena = new PoolArena.DirectArena(
|
|
|
|
this, pageSize, maxOrder, pageShifts, chunkSize);
|
|
|
|
directArenas[i] = arena;
|
|
|
|
metrics.add(arena);
|
2013-04-27 01:55:16 +02:00
|
|
|
}
|
2015-05-13 17:15:06 +02:00
|
|
|
directArenaMetrics = Collections.unmodifiableList(metrics);
|
2013-04-27 01:55:16 +02:00
|
|
|
} else {
|
|
|
|
directArenas = null;
|
2015-05-13 17:15:06 +02:00
|
|
|
directArenaMetrics = Collections.emptyList();
|
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) {
|
2016-04-12 19:34:54 +02:00
|
|
|
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")");
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
2014-03-17 16:32:39 +01:00
|
|
|
if ((pageSize & pageSize - 1) != 0) {
|
|
|
|
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
2014-03-17 16:32:39 +01:00
|
|
|
|
|
|
|
// Logarithm base 2. At this point we know that pageSize is a power of two.
|
|
|
|
return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2016-05-23 11:59:55 +02:00
|
|
|
buf = UnsafeByteBufUtil.newUnsafeDirectByteBuf(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
|
|
|
}
|
|
|
|
|
2015-11-07 23:31:45 +01:00
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default number of heap areanas - System Property: io.netty.allocator.numHeapArenas - default 2 * cores
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultNumHeapArena() {
|
|
|
|
return DEFAULT_NUM_HEAP_ARENA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default numer of direct arenas - System Property: io.netty.allocator.numDirectArenas - default 2 * cores
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultNumDirectArena() {
|
|
|
|
return DEFAULT_NUM_DIRECT_ARENA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default buffer page size - System Property: io.netty.allocator.pageSize - default 8192
|
|
|
|
*/
|
|
|
|
public static int defaultPageSize() {
|
|
|
|
return DEFAULT_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default maximum order - System Property: io.netty.allocator.maxOrder - default 11
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultMaxOrder() {
|
|
|
|
return DEFAULT_MAX_ORDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default tiny cache size - System Property: io.netty.allocator.tinyCacheSize - default 512
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultTinyCacheSize() {
|
|
|
|
return DEFAULT_TINY_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default small cache size - System Property: io.netty.allocator.smallCacheSize - default 256
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultSmallCacheSize() {
|
|
|
|
return DEFAULT_SMALL_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 16:18:23 +01:00
|
|
|
* Default normal cache size - System Property: io.netty.allocator.normalCacheSize - default 64
|
2015-11-07 23:31:45 +01:00
|
|
|
*/
|
|
|
|
public static int defaultNormalCacheSize() {
|
|
|
|
return DEFAULT_NORMAL_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
2013-08-16 21:53:47 +02:00
|
|
|
@Override
|
|
|
|
public boolean isDirectBufferPooled() {
|
|
|
|
return directArenas != null;
|
|
|
|
}
|
|
|
|
|
2014-03-01 15:47:03 +01:00
|
|
|
/**
|
|
|
|
* Returns {@code true} if the calling {@link Thread} has a {@link ThreadLocal} cache for the allocated
|
|
|
|
* buffers.
|
|
|
|
*/
|
2014-04-09 11:07:14 +02:00
|
|
|
@Deprecated
|
2014-03-01 15:47:03 +01:00
|
|
|
public boolean hasThreadLocalCache() {
|
2014-06-17 11:37:58 +02:00
|
|
|
return threadCache.isSet();
|
2014-03-01 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free all cached buffers for the calling {@link Thread}.
|
|
|
|
*/
|
2014-04-09 11:07:14 +02:00
|
|
|
@Deprecated
|
2014-03-01 15:47:03 +01:00
|
|
|
public void freeThreadLocalCache() {
|
2014-06-17 11:37:58 +02:00
|
|
|
threadCache.remove();
|
2014-03-01 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
2014-06-09 02:18:46 +02:00
|
|
|
final class PoolThreadLocalCache extends FastThreadLocal<PoolThreadCache> {
|
2016-11-22 16:18:23 +01:00
|
|
|
private final boolean useCacheForAllThreads;
|
|
|
|
|
|
|
|
PoolThreadLocalCache(boolean useCacheForAllThreads) {
|
|
|
|
this.useCacheForAllThreads = useCacheForAllThreads;
|
|
|
|
}
|
2014-03-01 15:47:03 +01:00
|
|
|
|
|
|
|
@Override
|
2016-03-14 17:25:43 +01:00
|
|
|
protected synchronized PoolThreadCache initialValue() {
|
|
|
|
final PoolArena<byte[]> heapArena = leastUsedArena(heapArenas);
|
|
|
|
final PoolArena<ByteBuffer> directArena = leastUsedArena(directArenas);
|
2014-03-01 15:47:03 +01:00
|
|
|
|
2016-11-22 16:18:23 +01:00
|
|
|
if (useCacheForAllThreads || Thread.currentThread() instanceof FastThreadLocalThread) {
|
|
|
|
return new PoolThreadCache(
|
|
|
|
heapArena, directArena, tinyCacheSize, smallCacheSize, normalCacheSize,
|
|
|
|
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL);
|
|
|
|
}
|
|
|
|
// No caching for non FastThreadLocalThreads.
|
|
|
|
return new PoolThreadCache(heapArena, directArena, 0, 0, 0, 0, 0);
|
2014-03-01 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
@Override
|
2016-03-14 17:25:43 +01:00
|
|
|
protected void onRemoval(PoolThreadCache threadCache) {
|
|
|
|
threadCache.free();
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T> PoolArena<T> leastUsedArena(PoolArena<T>[] arenas) {
|
|
|
|
if (arenas == null || arenas.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolArena<T> minArena = arenas[0];
|
|
|
|
for (int i = 1; i < arenas.length; i++) {
|
|
|
|
PoolArena<T> arena = arenas[i];
|
|
|
|
if (arena.numThreadCaches.get() < minArena.numThreadCaches.get()) {
|
|
|
|
minArena = arena;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return minArena;
|
2014-03-01 15:47:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:15:06 +02:00
|
|
|
/**
|
|
|
|
* Return the number of heap arenas.
|
|
|
|
*/
|
|
|
|
public int numHeapArenas() {
|
|
|
|
return heapArenaMetrics.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of direct arenas.
|
|
|
|
*/
|
|
|
|
public int numDirectArenas() {
|
|
|
|
return directArenaMetrics.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a {@link List} of all heap {@link PoolArenaMetric}s that are provided by this pool.
|
|
|
|
*/
|
|
|
|
public List<PoolArenaMetric> heapArenas() {
|
|
|
|
return heapArenaMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a {@link List} of all direct {@link PoolArenaMetric}s that are provided by this pool.
|
|
|
|
*/
|
|
|
|
public List<PoolArenaMetric> directArenas() {
|
|
|
|
return directArenaMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of thread local caches used by this {@link PooledByteBufAllocator}.
|
|
|
|
*/
|
|
|
|
public int numThreadLocalCaches() {
|
2016-03-14 17:25:43 +01:00
|
|
|
PoolArena<?>[] arenas = heapArenas != null ? heapArenas : directArenas;
|
|
|
|
if (arenas == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int total = 0;
|
2017-02-04 00:08:46 +01:00
|
|
|
for (PoolArena<?> arena : arenas) {
|
|
|
|
total += arena.numThreadCaches.get();
|
2016-03-14 17:25:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
2015-05-13 17:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the tiny cache.
|
|
|
|
*/
|
|
|
|
public int tinyCacheSize() {
|
|
|
|
return tinyCacheSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the small cache.
|
|
|
|
*/
|
|
|
|
public int smallCacheSize() {
|
|
|
|
return smallCacheSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the normal cache.
|
|
|
|
*/
|
|
|
|
public int normalCacheSize() {
|
|
|
|
return normalCacheSize;
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:00:24 +02:00
|
|
|
final PoolThreadCache threadCache() {
|
|
|
|
return threadCache.get();
|
|
|
|
}
|
|
|
|
|
2016-04-06 09:25:22 +02:00
|
|
|
/**
|
|
|
|
* Returns the status of the allocator (which contains all metrics) as string. Be aware this may be expensive
|
|
|
|
* and so should not called too frequently.
|
|
|
|
*/
|
|
|
|
public String dumpStats() {
|
|
|
|
int heapArenasLen = heapArenas == null ? 0 : heapArenas.length;
|
|
|
|
StringBuilder buf = new StringBuilder(512)
|
|
|
|
.append(heapArenasLen)
|
|
|
|
.append(" heap arena(s):")
|
|
|
|
.append(StringUtil.NEWLINE);
|
|
|
|
if (heapArenasLen > 0) {
|
|
|
|
for (PoolArena<byte[]> a: heapArenas) {
|
|
|
|
buf.append(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int directArenasLen = directArenas == null ? 0 : directArenas.length;
|
|
|
|
|
|
|
|
buf.append(directArenasLen)
|
|
|
|
.append(" direct arena(s):")
|
|
|
|
.append(StringUtil.NEWLINE);
|
|
|
|
if (directArenasLen > 0) {
|
|
|
|
for (PoolArena<ByteBuffer> a: directArenas) {
|
|
|
|
buf.append(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.toString();
|
|
|
|
}
|
2012-12-05 22:11:48 +01:00
|
|
|
}
|