netty5/common/src/main/java/io/netty/util/NettyRuntime.java

107 lines
4.2 KiB
Java
Raw Normal View History

Enable configuring available processors Motivation: In cases when an application is running in a container or is otherwise constrained to the number of processors that it is using, the JVM invocation Runtime#availableProcessors will not return the constrained value but rather the number of processors available to the virtual machine. Netty uses this number in sizing various resources. Additionally, some applications will constrain the number of threads that they are using independenly of the number of processors available on the system. Thus, applications should have a way to globally configure the number of processors. Modifications: Rather than invoking Runtime#availableProcessors, Netty should rely on a method that enables configuration when the JVM is started or by the application. This commit exposes a new class NettyRuntime for enabling such configuraiton. This value can only be set once. Its default value is Runtime#availableProcessors so that there is no visible change to existing applications, but enables configuring either a system property or configuring during application startup (e.g., based on settings used to configure the application). Additionally, we introduce the usage of forbidden-apis to prevent future uses of Runtime#availableProcessors from creeping. Future work should enable the bundled signatures and clean up uses of deprecated and other forbidden methods. Result: Netty can be configured to not use the underlying number of processors, but rather the constrained number of processors.
2017-01-16 18:36:32 +01:00
/*
* Copyright 2017 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.util;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.SystemPropertyUtil;
import java.util.Locale;
/**
* A utility class for wrapping calls to {@link Runtime}.
*/
public final class NettyRuntime {
/**
* Holder class for available processors to enable testing.
*/
static class AvailableProcessorsHolder {
private int availableProcessors;
/**
* Set the number of available processors.
*
* @param availableProcessors the number of available processors
* @throws IllegalArgumentException if the specified number of available processors is non-positive
* @throws IllegalStateException if the number of available processors is already configured
*/
synchronized void setAvailableProcessors(final int availableProcessors) {
ObjectUtil.checkPositive(availableProcessors, "availableProcessors");
if (this.availableProcessors != 0) {
final String message = String.format(
Locale.ROOT,
"availableProcessors is already set to [%d], rejecting [%d]",
this.availableProcessors,
availableProcessors);
throw new IllegalStateException(message);
}
this.availableProcessors = availableProcessors;
}
/**
* Get the configured number of available processors. The default is {@link Runtime#availableProcessors()}.
* This can be overridden by setting the system property "io.netty.availableProcessors" or by invoking
* {@link #setAvailableProcessors(int)} before any calls to this method.
*
* @return the configured number of available processors
*/
@SuppressForbidden(reason = "to obtain default number of available processors")
synchronized int availableProcessors() {
if (this.availableProcessors == 0) {
final int availableProcessors =
SystemPropertyUtil.getInt(
"io.netty.availableProcessors",
Runtime.getRuntime().availableProcessors());
setAvailableProcessors(availableProcessors);
}
return this.availableProcessors;
}
}
private static final AvailableProcessorsHolder holder = new AvailableProcessorsHolder();
/**
* Set the number of available processors.
*
* @param availableProcessors the number of available processors
* @throws IllegalArgumentException if the specified number of available processors is non-positive
* @throws IllegalStateException if the number of available processors is already configured
*/
@SuppressWarnings("unused,WeakerAccess") // this method is part of the public API
public static void setAvailableProcessors(final int availableProcessors) {
holder.setAvailableProcessors(availableProcessors);
}
/**
* Get the configured number of available processors. The default is {@link Runtime#availableProcessors()}. This
* can be overridden by setting the system property "io.netty.availableProcessors" or by invoking
* {@link #setAvailableProcessors(int)} before any calls to this method.
*
* @return the configured number of available processors
*/
public static int availableProcessors() {
return holder.availableProcessors();
}
/**
* No public constructor to prevent instances from being created.
*/
private NettyRuntime() {
}
}