View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util;
18  
19  import io.netty.util.internal.ObjectUtil;
20  import io.netty.util.internal.SystemPropertyUtil;
21  
22  import java.util.Locale;
23  
24  /**
25   * A utility class for wrapping calls to {@link Runtime}.
26   */
27  public final class NettyRuntime {
28  
29      /**
30       * Holder class for available processors to enable testing.
31       */
32      static class AvailableProcessorsHolder {
33  
34          private int availableProcessors;
35  
36          /**
37           * Set the number of available processors.
38           *
39           * @param availableProcessors the number of available processors
40           * @throws IllegalArgumentException if the specified number of available processors is non-positive
41           * @throws IllegalStateException    if the number of available processors is already configured
42           */
43          synchronized void setAvailableProcessors(final int availableProcessors) {
44              ObjectUtil.checkPositive(availableProcessors, "availableProcessors");
45              if (this.availableProcessors != 0) {
46                  final String message = String.format(
47                          Locale.ROOT,
48                          "availableProcessors is already set to [%d], rejecting [%d]",
49                          this.availableProcessors,
50                          availableProcessors);
51                  throw new IllegalStateException(message);
52              }
53              this.availableProcessors = availableProcessors;
54          }
55  
56          /**
57           * Get the configured number of available processors. The default is {@link Runtime#availableProcessors()}.
58           * This can be overridden by setting the system property "io.netty.availableProcessors" or by invoking
59           * {@link #setAvailableProcessors(int)} before any calls to this method.
60           *
61           * @return the configured number of available processors
62           */
63          @SuppressForbidden(reason = "to obtain default number of available processors")
64          synchronized int availableProcessors() {
65              if (this.availableProcessors == 0) {
66                  final int availableProcessors =
67                          SystemPropertyUtil.getInt(
68                                  "io.netty.availableProcessors",
69                                  Runtime.getRuntime().availableProcessors());
70                  setAvailableProcessors(availableProcessors);
71              }
72              return this.availableProcessors;
73          }
74      }
75  
76      private static final AvailableProcessorsHolder holder = new AvailableProcessorsHolder();
77  
78      /**
79       * Set the number of available processors.
80       *
81       * @param availableProcessors the number of available processors
82       * @throws IllegalArgumentException if the specified number of available processors is non-positive
83       * @throws IllegalStateException    if the number of available processors is already configured
84       */
85      @SuppressWarnings("unused,WeakerAccess") // this method is part of the public API
86      public static void setAvailableProcessors(final int availableProcessors) {
87          holder.setAvailableProcessors(availableProcessors);
88      }
89  
90      /**
91       * Get the configured number of available processors. The default is {@link Runtime#availableProcessors()}. This
92       * can be overridden by setting the system property "io.netty.availableProcessors" or by invoking
93       * {@link #setAvailableProcessors(int)} before any calls to this method.
94       *
95       * @return the configured number of available processors
96       */
97      public static int availableProcessors() {
98          return holder.availableProcessors();
99      }
100 
101     /**
102      * No public constructor to prevent instances from being created.
103      */
104     private NettyRuntime() {
105     }
106 }