Java Code Examples for io.netty.util.NettyRuntime#availableProcessors()

The following examples show how to use io.netty.util.NettyRuntime#availableProcessors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
DefaultEventLoopGroupResource(
    int numEventLoops, String name, EventLoopGroupType eventLoopGroupType) {
  this.name = name;
  // See the implementation of MultithreadEventLoopGroup.  DEFAULT_EVENT_LOOP_THREADS there
  // defaults to NettyRuntime.availableProcessors() * 2.  We don't think we need that many
  // threads.  The overhead of a thread includes file descriptors and at least one chunk
  // allocation from PooledByteBufAllocator.  Here we reduce the default number of threads by
  // half.
  if (numEventLoops == 0 && System.getProperty("io.netty.eventLoopThreads") == null) {
    this.numEventLoops = NettyRuntime.availableProcessors();
  } else {
    this.numEventLoops = numEventLoops;
  }
  this.eventLoopGroupType = eventLoopGroupType;
}
 
Example 2
Source File: Netty4Utils.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Set the number of available processors that Netty uses for sizing various resources (e.g., thread pools).
 *
 * @param availableProcessors the number of available processors
 * @throws IllegalStateException if available processors was set previously and the specified value does not match the already-set value
 */
public static void setAvailableProcessors(final int availableProcessors) {
    // we set this to false in tests to avoid tests that randomly set processors from stepping on each other
    final boolean set = Booleans.parseBoolean(System.getProperty("es.set.netty.runtime.available.processors", "true"));
    if (!set) {
        return;
    }

    /*
     * This can be invoked twice, once from Netty4Transport and another time from Netty4HttpServerTransport; however,
     * Netty4Runtime#availableProcessors forbids settings the number of processors twice so we prevent double invocation here.
     */
    if (isAvailableProcessorsSet.compareAndSet(false, true)) {
        NettyRuntime.setAvailableProcessors(availableProcessors);
    } else if (availableProcessors != NettyRuntime.availableProcessors()) {
        /*
         * We have previously set the available processors yet either we are trying to set it to a different value now or there is a bug
         * in Netty and our previous value did not take, bail.
         */
        final String message = String.format(
                Locale.ROOT,
                "available processors value [%d] did not match current value [%d]",
                availableProcessors,
                NettyRuntime.availableProcessors());
        throw new IllegalStateException(message);
    }
}
 
Example 3
Source File: NettyRuntimeWrapper.java    From metron with Apache License 2.0 4 votes vote down vote up
public static int availableProcessors() {
  return NettyRuntime.availableProcessors();
}