Skip to content

Commit 8a012bd

Browse files
committed
[GR-59326] Ensure ForkJoin commonPool parallelism always respects NativeImageOptions.NumberOfThreads.
PullRequest: graal/19118
2 parents 0f833fd + 530ce0a commit 8a012bd

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ At runtime, premain runtime options are set along with main class' arguments in
1313
* (GR-58000) Support for `GetStringUTFLengthAsLong` added in JNI_VERSION_24 ([JDK-8328877](https://bugs.openjdk.org/browse/JDK-8328877))
1414
* (GR-58383) The length of the printed stack trace when using `-XX:MissingRegistrationReportingMode=Warn` can now be set with `-XX:MissingRegistrationWarnContextLines=` and its default length is now 8.
1515
* (GR-58914) `ActiveProcessorCount` must be set at isolate or VM creation time.
16+
* (GR-59326) Ensure builder ForkJoin commonPool parallelism always respects NativeImageOptions.NumberOfThreads.
1617

1718
## GraalVM for JDK 23 (Internal Version 24.1.0)
1819
* (GR-51520) The old class initialization strategy, which was deprecated in GraalVM for JDK 22, is removed. The option `StrictImageHeap` no longer has any effect.

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageOptions.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
import com.oracle.graal.pointsto.reports.ReportUtils;
3939
import com.oracle.svm.core.SubstrateOptions;
4040
import com.oracle.svm.core.option.APIOption;
41+
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue;
4142
import com.oracle.svm.core.option.BundleMember;
4243
import com.oracle.svm.core.option.HostedOptionKey;
43-
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue;
4444
import com.oracle.svm.core.option.SubstrateOptionsParser;
4545
import com.oracle.svm.core.util.InterruptImageBuilding;
4646
import com.oracle.svm.core.util.UserError;
@@ -214,28 +214,26 @@ public static int getActualNumberOfThreads() {
214214
}
215215

216216
public static void setCommonPoolParallelism(OptionValues optionValues) {
217-
if (NativeImageOptions.NumberOfThreads.hasBeenSet(optionValues)) {
218-
/*
219-
* The main thread always helps to process tasks submitted to the common pool (e.g., see
220-
* ForkJoinPool#awaitTermination()), so subtract one from the number of threads. The
221-
* common pool can be disabled "by setting the parallelism property to zero" (see
222-
* ForkJoinPool's javadoc).
223-
*/
224-
int numberOfCommonPoolThreads = NativeImageOptions.NumberOfThreads.getValue(optionValues) - 1;
225-
String commonPoolParallelismProperty = "java.util.concurrent.ForkJoinPool.common.parallelism";
226-
assert System.getProperty(commonPoolParallelismProperty) == null : commonPoolParallelismProperty + " already set";
227-
System.setProperty(commonPoolParallelismProperty, "" + numberOfCommonPoolThreads);
228-
int actualCommonPoolParallelism = ForkJoinPool.commonPool().getParallelism();
229-
/*
230-
* getParallelism() returns at least 1, even in single-threaded mode where common pool
231-
* is disabled.
232-
*/
233-
boolean isSingleThreadedMode = numberOfCommonPoolThreads == 0 && actualCommonPoolParallelism == 1;
234-
if (!isSingleThreadedMode && actualCommonPoolParallelism != numberOfCommonPoolThreads) {
235-
String warning = "Failed to set parallelism of common pool (actual parallelism is %s).".formatted(actualCommonPoolParallelism);
236-
assert false : warning;
237-
LogUtils.warning(warning);
238-
}
217+
/*
218+
* The main thread always helps to process tasks submitted to the common pool (e.g., see
219+
* ForkJoinPool#awaitTermination()), so subtract one from the number of threads. The common
220+
* pool can be disabled "by setting the parallelism property to zero" (see ForkJoinPool's
221+
* javadoc).
222+
*/
223+
int numberOfCommonPoolThreads = NativeImageOptions.NumberOfThreads.getValueOrDefault(optionValues.getMap()) - 1;
224+
String commonPoolParallelismProperty = "java.util.concurrent.ForkJoinPool.common.parallelism";
225+
assert System.getProperty(commonPoolParallelismProperty) == null : commonPoolParallelismProperty + " already set";
226+
System.setProperty(commonPoolParallelismProperty, "" + numberOfCommonPoolThreads);
227+
int actualCommonPoolParallelism = ForkJoinPool.commonPool().getParallelism();
228+
/*
229+
* getParallelism() returns at least 1, even in single-threaded mode where common pool is
230+
* disabled.
231+
*/
232+
boolean isSingleThreadedMode = numberOfCommonPoolThreads == 0 && actualCommonPoolParallelism == 1;
233+
if (!isSingleThreadedMode && actualCommonPoolParallelism != numberOfCommonPoolThreads) {
234+
String warning = "Failed to set parallelism of common pool (actual parallelism is %s).".formatted(actualCommonPoolParallelism);
235+
assert false : warning;
236+
LogUtils.warning(warning);
239237
}
240238
}
241239

0 commit comments

Comments
 (0)