Skip to content

Commit 3cf7f35

Browse files
committed
Make maximum number of C API copies configurable
1 parent 77e9264 commit 3cf7f35

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/NativeLibraryLocator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242

4343
import static com.oracle.graal.python.nodes.StringLiterals.J_NATIVE;
4444
import static com.oracle.graal.python.nodes.StringLiterals.T_BASE_PREFIX;
45+
import static com.oracle.graal.python.nodes.StringLiterals.J_MAX_CAPI_COPIES;
4546
import static com.oracle.graal.python.nodes.StringLiterals.T_PREFIX;
4647
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4748

4849
import java.io.IOException;
4950
import java.util.concurrent.atomic.AtomicInteger;
5051

52+
import com.oracle.graal.python.PythonLanguage;
5153
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ApiInitException;
5254
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ImportException;
5355
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -58,6 +60,7 @@
5860
import com.oracle.graal.python.util.BiFunction;
5961
import com.oracle.truffle.api.TruffleFile;
6062
import com.oracle.truffle.api.TruffleLanguage.Env;
63+
import com.oracle.truffle.api.TruffleLogger;
6164

6265
/**
6366
* Given a GraalPy virtual environment, this class helps prepare that environment so that multiple
@@ -67,7 +70,9 @@
6770
* guaranteed to work with the matching GraalPy version.
6871
*/
6972
public final class NativeLibraryLocator {
70-
private static final int MAX_CEXT_COPIES = 64;
73+
static final TruffleLogger LOGGER = PythonLanguage.getLogger("NativeLibraryLocator");
74+
75+
private static final int MAX_CEXT_COPIES = Integer.getInteger(J_MAX_CAPI_COPIES, 64);
7176

7277
/**
7378
* Bitset for which copied C extension to use when {@link PythonOptions#IsolateNativeModules} is
@@ -144,6 +149,11 @@ public void close() {
144149
* count}.
145150
*/
146151
public static void replicate(TruffleFile venvDirectory, PythonContext context, int count) throws IOException, InterruptedException {
152+
if (count > MAX_CEXT_COPIES) {
153+
LOGGER.warning(() -> String.format("The current limit for concurrent Python contexts accessing the Python C API is %d, " +
154+
"but we are preparing %d copies. The extra copies will only be used if a different value " +
155+
"of the system property %s is set.", MAX_CEXT_COPIES, count, J_MAX_CAPI_COPIES));
156+
}
147157
String suffix = context.getSoAbi().toJavaStringUncached();
148158
TruffleFile capiLibrary = context.getPublicTruffleFileRelaxed(context.getCAPIHome()).resolve(PythonContext.getSupportLibName("python-" + J_NATIVE));
149159
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/SharedObject.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.io.IOException;
4444
import java.io.OutputStream;
4545

46-
import com.oracle.graal.python.PythonLanguage;
4746
import com.oracle.graal.python.runtime.PythonContext;
4847
import com.oracle.truffle.api.TruffleFile;
4948
import com.oracle.truffle.api.TruffleLogger;
@@ -72,7 +71,7 @@ static SharedObject open(TruffleFile file, PythonContext context) throws IOExcep
7271
}
7372
}
7473

75-
protected static final TruffleLogger LOGGER = PythonLanguage.getLogger("NativeLibraryLocator");
74+
protected static final TruffleLogger LOGGER = NativeLibraryLocator.LOGGER;
7675

7776
protected static final class LoggingOutputStream extends OutputStream {
7877
private final StringBuilder sb = new StringBuilder();
@@ -88,8 +87,8 @@ public void write(byte[] b, int off, int len) throws IOException {
8887
}
8988

9089
@Override
91-
public void flush() throws IOException {
92-
LOGGER.fine(() -> sb.toString());
90+
public void flush() {
91+
LOGGER.fine(sb::toString);
9392
sb.setLength(0);
9493
}
9594
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.nodes;
4242

4343
import static com.oracle.graal.python.builtins.objects.str.StringUtils.cat;
44+
import static com.oracle.graal.python.nodes.StringLiterals.J_MAX_CAPI_COPIES;
4445
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
4546

4647
import com.oracle.graal.python.builtins.PythonOS;
@@ -1672,7 +1673,7 @@ public abstract class ErrorMessages {
16721673
public static final TruffleString SYS_PREFIX_MUST_BE_STRING_NOT_P_FOR_CAPI_ISOLATION = tsLiteral("The sys.prefix must be a str, not '%p' when the `IsolateNativeModules' option is used, because it is the base path for searching the relocated C API. Refer to https://www.graalvm.org/latest/reference-manual/python/Native-Extensions for details on native module isolation.");
16731674
public static final TruffleString SYS_PREFIX_MUST_POINT_TO_A_VENV_FOR_CAPI_ISOLATION = tsLiteral("The sys.prefix must point to a venv, not be identical to sys.base_prefix when the `IsolateNativeModules' option is used, because it is the base path for searching and creating the relocated C API and extension modules. Refer to https://www.graalvm.org/latest/reference-manual/python/Native-Extensions for details on native module isolation.");
16741675
public static final TruffleString CAPI_ISOLATION_CAPPED_AT_D = tsLiteral(
1675-
"There is no available slot for C API isolation. The current limit for concurrent Python contexts accessing the Python C API is %d.");
1676+
"There is no available slot for C API isolation. The current limit for concurrent Python contexts accessing the Python C API is %d. This can be changed with the" + J_MAX_CAPI_COPIES + " System property.");
16761677
public static final TruffleString SYS_PREFIX_MUST_BE_STRING_NOT_P_FOR_CAPI_ISOLATION = tsLiteral(
16771678
"The sys.prefix must be a str, not '%p' when the `IsolateNativeModules' option is used, because it is the base path for searching the relocated C API. " +
16781679
"Refer to https://www.graalvm.org/latest/reference-manual/python/Native-Extensions for details on native module isolation.");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/StringLiterals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ public abstract class StringLiterals {
160160
public static final TruffleString T_VISIT = tsLiteral("visit");
161161
public static final TruffleString T_PREFIX = tsLiteral("prefix");
162162
public static final TruffleString T_BASE_PREFIX = tsLiteral("base_prefix");
163+
public static final String J_MAX_CAPI_COPIES = "python.MaximumNumberOfCextCopies";
163164
}

0 commit comments

Comments
 (0)