Example usage for io.netty.channel.epoll EpollEventLoopGroup next

List of usage examples for io.netty.channel.epoll EpollEventLoopGroup next

Introduction

In this page you can find the example usage for io.netty.channel.epoll EpollEventLoopGroup next.

Prototype

@Override
    public EventLoop next() 

Source Link

Usage

From source file:org.apache.bookkeeper.util.EventLoopUtil.java

License:Apache License

private static EventLoopGroup getEventLoopGroup(ThreadFactory threadFactory, int numThreads,
        boolean enableBusyWait) {
    if (!SystemUtils.IS_OS_LINUX) {
        return new NioEventLoopGroup(numThreads, threadFactory);
    }/*  ww  w. j av  a 2 s . c o  m*/

    try {
        if (!enableBusyWait) {
            // Regular Epoll based event loop
            return new EpollEventLoopGroup(numThreads, threadFactory);
        }

        // With low latency setting, put the Netty event loop on busy-wait loop to reduce cost of
        // context switches
        EpollEventLoopGroup eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory,
                () -> (selectSupplier, hasTasks) -> SelectStrategy.BUSY_WAIT);

        // Enable CPU affinity on IO threads
        for (int i = 0; i < numThreads; i++) {
            eventLoopGroup.next().submit(() -> {
                try {
                    CpuAffinity.acquireCore();
                } catch (Throwable t) {
                    log.warn("Failed to acquire CPU core for thread {}", Thread.currentThread().getName(),
                            t.getMessage(), t);
                }
            });
        }

        return eventLoopGroup;
    } catch (ExceptionInInitializerError | NoClassDefFoundError | UnsatisfiedLinkError e) {
        log.warn("Could not use Netty Epoll event loop: {}", e.getMessage());
        return new NioEventLoopGroup(numThreads, threadFactory);
    }
}