|
31 | 31 | import java.nio.file.Paths; |
32 | 32 | import java.text.SimpleDateFormat; |
33 | 33 | import java.util.Date; |
34 | | -import java.util.concurrent.CompletableFuture; |
35 | 34 | import java.util.concurrent.ForkJoinPool; |
36 | 35 |
|
37 | 36 | import org.graalvm.collections.EconomicMap; |
|
47 | 46 | import com.oracle.svm.core.util.UserError; |
48 | 47 | import com.oracle.svm.hosted.classinitialization.ClassInitializationOptions; |
49 | 48 | import com.oracle.svm.hosted.util.CPUType; |
| 49 | +import com.oracle.svm.util.LogUtils; |
50 | 50 | import com.oracle.svm.util.StringUtil; |
51 | 51 |
|
52 | 52 | import jdk.graal.compiler.options.Option; |
@@ -185,59 +185,50 @@ public static CStandards getCStandard() { |
185 | 185 | } |
186 | 186 | } |
187 | 187 |
|
| 188 | + private static int availableProcessors() { |
| 189 | + return Runtime.getRuntime().availableProcessors(); |
| 190 | + } |
| 191 | + |
188 | 192 | /** |
189 | 193 | * Configures the number of threads of the common pool. |
190 | 194 | */ |
191 | 195 | private static final String PARALLELISM_OPTION_NAME = "parallelism"; |
192 | 196 | @APIOption(name = PARALLELISM_OPTION_NAME)// |
193 | 197 | @Option(help = "The maximum number of threads the build process is allowed to use.")// |
194 | | - public static final HostedOptionKey<Integer> NumberOfThreads = new HostedOptionKey<>(Math.max(1, Math.min(Runtime.getRuntime().availableProcessors(), 32)), key -> { |
| 198 | + public static final HostedOptionKey<Integer> NumberOfThreads = new HostedOptionKey<>(Math.min(availableProcessors(), 32), key -> { |
195 | 199 | int numberOfThreads = key.getValue(); |
196 | 200 | if (numberOfThreads < 1) { |
197 | 201 | throw UserError.abort("The number of threads was set to %s. Please set the '--%s' option to at least 1.", numberOfThreads, PARALLELISM_OPTION_NAME); |
198 | 202 | } |
| 203 | + int numAvailableProcessors = availableProcessors(); |
| 204 | + if (numberOfThreads > numAvailableProcessors) { |
| 205 | + LogUtils.warning("Specified parallelism exceeds the number of available processors (%d).", numAvailableProcessors); |
| 206 | + } |
199 | 207 | }); |
200 | 208 |
|
201 | 209 | public static int getActualNumberOfThreads() { |
202 | | - int commonThreadParallelism = ForkJoinPool.getCommonPoolParallelism(); |
203 | | - if (NumberOfThreads.getValue() == 1) { |
204 | | - /* |
205 | | - * Overriding zero parallelism must ensure at least one worker, but due to other |
206 | | - * backward compatibility constraints, it ensures two. |
207 | | - */ |
208 | | - assert commonThreadParallelism == 2 : "The common pool with zero parallelism has two worker threads."; |
209 | | - /* The common pool with zero parallelism has no actual threads. */ |
210 | | - commonThreadParallelism = 0; |
| 210 | + String commonParallelismProperty = System.getProperty("java.util.concurrent.ForkJoinPool.common.parallelism"); |
| 211 | + if (commonParallelismProperty != null) { |
| 212 | + return Integer.parseInt(commonParallelismProperty); |
| 213 | + } else { |
| 214 | + return NumberOfThreads.getValue(); |
211 | 215 | } |
212 | | - /* |
213 | | - * Main thread plus common pool threads. setCommonPoolParallelism() asserts that this number |
214 | | - * matches NumberOfThreads. |
215 | | - */ |
216 | | - return 1 + commonThreadParallelism; |
217 | 216 | } |
218 | 217 |
|
219 | | - /** |
220 | | - * The --parallelism flag controls the number of threads used during native image builds. |
221 | | - * <p> |
222 | | - * There are two ways to apply this value to set the thread count in the common pool: using |
223 | | - * system properties (though this approach is strongly discouraged in recent JDK versions), and |
224 | | - * using the {@link ForkJoinPool#setParallelism} method. |
225 | | - * <p> |
226 | | - * By default, the common pool's parallelism level (without explicitly setting it via system |
227 | | - * properties or method) equals the number of available processors minus one. |
228 | | - * <p> |
229 | | - * If zero parallelism is desired, the flag should be set to {@code 1} (main thread only). While |
230 | | - * this value can be assigned to the common pool, note that when using |
231 | | - * {@link CompletableFuture}, the minimal effective parallelism becomes two due to its internal |
232 | | - * pool requirements (see CompletableFuture#ASYNC_POOL). Therefore, the actual thread count must |
233 | | - * be incremented by one. |
234 | | - */ |
235 | 218 | public static void setCommonPoolParallelism(OptionValues optionValues) { |
236 | | - int numberOfCommonPoolThreads = NativeImageOptions.NumberOfThreads.getValueOrDefault(optionValues.getMap()); |
237 | | - if (numberOfCommonPoolThreads == 1) { |
238 | | - numberOfCommonPoolThreads += 1; |
| 219 | + int targetParallelism = Math.max(1, NumberOfThreads.getValueOrDefault(optionValues.getMap()) - 1); |
| 220 | + if (ForkJoinPool.commonPool().getParallelism() == targetParallelism) { |
| 221 | + /* Nothing to do. */ |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + try { |
| 226 | + ForkJoinPool.commonPool().setParallelism(targetParallelism); |
| 227 | + } catch (UnsupportedOperationException e) { |
| 228 | + if (NumberOfThreads.hasBeenSet(optionValues)) { |
| 229 | + LogUtils.warning("Could not apply the '--parallelism' option due to the `java.util.concurrent.ForkJoinPool.common.parallelism` system property being set as the same time."); |
| 230 | + } |
239 | 231 | } |
240 | | - ForkJoinPool.commonPool().setParallelism(numberOfCommonPoolThreads); |
241 | 232 | } |
242 | 233 |
|
243 | 234 | @Option(help = "Deprecated, option no longer has any effect", deprecated = true, deprecationMessage = "Please use '--parallelism' instead.")// |
|
0 commit comments