use of com.firenio.buffer.ByteBufAllocatorGroup in project baseio by generallycloud.
the class NioEventLoopGroup method doStart.
@Override
protected void doStart() throws Exception {
this.channelIds = new RingSequence(0x1000, Integer.MAX_VALUE);
if (isEnableMemoryPool() && getAllocatorGroup() == null) {
if (!Native.EPOLL_AVAILABLE && Unsafe.UNSAFE_BUF_AVAILABLE) {
throw new IllegalArgumentException("Java(Nio) mode can not use unsafe memory");
}
if (memoryCapacity == 0) {
memoryCapacity = Runtime.getRuntime().maxMemory() / 64;
}
int memoryTypeId = preferHeap ? Unsafe.BUF_HEAP : Unsafe.getMemoryTypeId();
this.memoryCapacity = calcSuitableMemoryCapacity(memoryCapacity);
this.allocatorGroup = new ByteBufAllocatorGroup(getEventLoopSize(), memoryCapacity, memoryUnit, memoryTypeId);
}
Util.start(getAllocatorGroup());
super.doStart();
}
use of com.firenio.buffer.ByteBufAllocatorGroup in project baseio by generallycloud.
the class TestExpansion method testExpansion.
@Test
public void testExpansion() throws Exception {
Options.setBufAutoExpansion(true);
ByteBufAllocatorGroup g = new ByteBufAllocatorGroup(1, 8, 1, Unsafe.BUF_DIRECT);
g.start();
PooledByteBufAllocator alloc = g.getAllocator(0);
ByteBuf buf1 = alloc.allocate(2);
buf1.expansion(6);
buf1.release();
ByteBuf buf2 = alloc.allocate(2);
buf2.expansion(4);
buf2.release();
Assert.assertTrue(alloc.getState().buf == 0);
g.stop();
}
use of com.firenio.buffer.ByteBufAllocatorGroup in project baseio by generallycloud.
the class TestAllocUtil method heap.
public static PooledByteBufAllocator heap(int cap) throws Exception {
ByteBufAllocatorGroup group = new ByteBufAllocatorGroup(1, cap, 1, Unsafe.BUF_HEAP);
Util.start(group);
return group.getAllocator(0);
}
use of com.firenio.buffer.ByteBufAllocatorGroup in project baseio by generallycloud.
the class TestShowMemoryServlet method doAccept.
@Override
protected void doAccept(Channel ch, HttpFrame f) throws Exception {
TestWebSocketChatServlet chatServlet = context.getBean(TestWebSocketChatServlet.class);
TestWebSocketRumpetrollServlet rumpetrollServlet = context.getBean(TestWebSocketRumpetrollServlet.class);
WebSocketMsgAdapter chatMsgAdapter = chatServlet.getMsgAdapter();
WebSocketMsgAdapter rumpetrollMsgAdapter = rumpetrollServlet.getMsgAdapter();
ChannelContext context = ch.getContext();
String kill = f.getRequestParam("kill");
if (!Util.isNullOrBlank(kill)) {
Integer id = Integer.valueOf(kill, 16);
Channel close_ch = CountChannelListener.chs.get(id);
if (close_ch != null) {
close_ch.getEventLoop().schedule(new DelayTask(10) {
@Override
public void run() {
Util.close(close_ch);
}
});
}
}
BigDecimal time = new BigDecimal(Util.now_f() - context.getStartupTime());
BigDecimal anHour = new BigDecimal(60 * 60 * 1000);
BigDecimal hour = time.divide(anHour, 3, RoundingMode.HALF_UP);
NioEventLoopGroup group = ch.getEventLoop().getGroup();
ByteBufAllocatorGroup allocator = group.getAllocatorGroup();
String allocatorDes = "unpooled";
if (allocator != null) {
StringBuilder builder = new StringBuilder();
String[] res = allocator.toDebugString();
for (int i = 0; i < res.length; i++) {
builder.append("<BR/>\n");
builder.append(res[i]);
}
allocatorDes = builder.toString();
}
int M = 1024 * 1024;
int eventLoopSize = group.getEventLoopSize();
int SERVER_MEMORY_POOL_UNIT = group.getMemoryUnit();
long SERVER_MEMORY_POOL_CAPACITY = group.getMemoryCapacity();
double MEMORY_POOL_SIZE = SERVER_MEMORY_POOL_CAPACITY / (M * 1d);
MEMORY_POOL_SIZE = new BigDecimal(MEMORY_POOL_SIZE).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
Runtime runtime = Runtime.getRuntime();
StringBuilder builder = new StringBuilder(HttpUtil.HTML_HEADER);
builder.append(" <div style=\"margin-left:20px;\">\n");
builder.append("服务器内存使用情况:</BR>\n");
builder.append("虚拟机占用内存:");
builder.append(runtime.totalMemory() / M);
builder.append("M;\n</BR>已占用内存:");
builder.append((runtime.totalMemory() - runtime.freeMemory()) / M);
builder.append("M;\n</BR>空闲内存:");
builder.append(runtime.freeMemory() / M);
builder.append("M;\n</BR>内存池大小:");
builder.append(MEMORY_POOL_SIZE);
builder.append("M;\n</BR>内存池状态:");
builder.append(allocatorDes);
builder.append("\n</BR>聊天室(WebSocket)客户端数量:");
builder.append(chatMsgAdapter.getClientSize());
builder.append("\n</BR>小蝌蚪(WebSocket)客户端数量:");
builder.append(rumpetrollMsgAdapter.getClientSize());
builder.append("\n</BR>服务器当前连接数(io-channel):");
builder.append(CountChannelListener.chs.size());
for (Channel s : CountChannelListener.chs.values()) {
builder.append("\n</BR>");
builder.append(s);
builder.append(", opened: ");
builder.append(DateUtil.get().formatYyyy_MM_dd_HH_mm_ss(new Date(s.getCreationTime())));
}
builder.append(";\n</BR>服务运行时间:");
builder.append(hour + "H;");
builder.append(" </div>\n");
builder.append(HttpUtil.HTML_POWER_BY);
builder.append(HttpUtil.HTML_BOTTOM);
f.setString(builder.toString(), ch);
f.setContentType(HttpContentType.text_html_utf8);
ch.writeAndFlush(f);
}
use of com.firenio.buffer.ByteBufAllocatorGroup in project baseio by generallycloud.
the class TestAllocUtil method direct.
public static PooledByteBufAllocator direct(int cap) throws Exception {
ByteBufAllocatorGroup group = new ByteBufAllocatorGroup(1, cap, 1, Unsafe.BUF_DIRECT);
Util.start(group);
return group.getAllocator(0);
}
Aggregations