Example usage for io.netty.channel SelectStrategy BUSY_WAIT

List of usage examples for io.netty.channel SelectStrategy BUSY_WAIT

Introduction

In this page you can find the example usage for io.netty.channel SelectStrategy BUSY_WAIT.

Prototype

int BUSY_WAIT

To view the source code for io.netty.channel SelectStrategy BUSY_WAIT.

Click Source Link

Document

Indicates the IO loop to poll for new events without blocking.

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  . ja v a2 s  .  co  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);
    }
}