Skip to content

Commit d19f7d6

Browse files
committed
[GR-47818] Socket optimizations
PullRequest: graalpython/2957
2 parents 06ffd18 + 9424628 commit d19f7d6

File tree

20 files changed

+663
-442
lines changed

20 files changed

+663
-442
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/SocketTests.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import static org.junit.Assert.assertThat;
8686
import static org.junit.Assert.assertTrue;
8787
import static org.junit.Assert.fail;
88+
import static org.junit.Assume.assumeNoException;
8889
import static org.junit.Assume.assumeTrue;
8990

9091
import java.io.File;
@@ -110,7 +111,6 @@
110111

111112
import com.oracle.graal.python.builtins.PythonOS;
112113
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
113-
import com.oracle.graal.python.runtime.PosixConstants;
114114
import com.oracle.graal.python.runtime.PosixConstants.MandatoryIntConstant;
115115
import com.oracle.graal.python.runtime.PosixSupportLibrary;
116116
import com.oracle.graal.python.runtime.PosixSupportLibrary.AcceptResult;
@@ -122,6 +122,7 @@
122122
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
123123
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
124124
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
125+
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidUnixSocketPathException;
125126
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
126127
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
127128
import com.oracle.graal.python.runtime.PosixSupportLibrary.SelectResult;
@@ -186,7 +187,7 @@ public void fillUniversalSockAddrInet6() {
186187
public void fillUniversalSockAddrUnix() {
187188
assumeTrue("native".equals(backendName));
188189
byte[] path = new byte[]{65, 0};
189-
UnixSockAddr addr = new UnixSockAddr(path, 0, path.length);
190+
UnixSockAddr addr = new UnixSockAddr(path);
190191
checkUsa(addr, createUsa(addr));
191192
}
192193

@@ -207,7 +208,7 @@ public void dgramUnboundGetsocknameInet6() throws PosixException {
207208
public void dgramUnboundGetsocknameUnix() throws PosixException {
208209
assumeTrue("native".equals(backendName));
209210
int s = createSocket(AF_UNIX.value, SOCK_DGRAM.value, 0);
210-
checkUsa(new UnixSockAddr(new byte[0], 0, 0), lib.getsockname(posixSupport, s));
211+
checkUsa(new UnixSockAddr(new byte[0]), lib.getsockname(posixSupport, s));
211212
}
212213

213214
@Test
@@ -469,7 +470,13 @@ public void streamWriteReadUnix() throws PosixException, IOException {
469470
srv.bind(unixSockAddr);
470471
srv.listen(5);
471472
Socket cli = new Socket(AF_UNIX.value, SOCK_STREAM.value);
472-
cli.connect(createUsa(unixSockAddr));
473+
UniversalSockAddr addr = null;
474+
try {
475+
addr = lib.createUniversalSockAddrUnix(posixSupport, unixSockAddr);
476+
} catch (InvalidUnixSocketPathException e) {
477+
assumeNoException(e);
478+
}
479+
cli.connect(addr);
473480

474481
Socket c = new Socket(srv.acceptFd(UNIX_SOCK_ADDR_UNNAMED), AF_UNIX.value, SOCK_STREAM.value);
475482

@@ -1021,7 +1028,6 @@ private byte[] getNulTerminatedTempFileName() throws IOException {
10211028
f.delete();
10221029
f.deleteOnExit();
10231030
byte[] fileNameBytes = f.getAbsolutePath().getBytes();
1024-
assumeTrue(fileNameBytes.length + 1 <= PosixConstants.SIZEOF_STRUCT_SOCKADDR_UN_SUN_PATH.value);
10251031
return Arrays.copyOf(fileNameBytes, fileNameBytes.length + 1);
10261032
}
10271033

@@ -1079,7 +1085,19 @@ private int createSocket(int family, int type, int protocol) throws PosixExcepti
10791085
}
10801086

10811087
private UniversalSockAddr createUsa(FamilySpecificSockAddr src) {
1082-
return lib.createUniversalSockAddr(posixSupport, src);
1088+
if (src instanceof Inet4SockAddr inet4SockAddr) {
1089+
return lib.createUniversalSockAddrInet4(posixSupport, inet4SockAddr);
1090+
} else if (src instanceof Inet6SockAddr inet6SockAddr) {
1091+
return lib.createUniversalSockAddrInet6(posixSupport, inet6SockAddr);
1092+
} else if (src instanceof UnixSockAddr unixSockAddr) {
1093+
try {
1094+
return lib.createUniversalSockAddrUnix(posixSupport, unixSockAddr);
1095+
} catch (InvalidUnixSocketPathException e) {
1096+
throw new RuntimeException(e);
1097+
}
1098+
} else {
1099+
throw new AssertionError("Unexpected subclass of FamilySpecificSockAddr: " + src.getClass().getName());
1100+
}
10831101
}
10841102

10851103
private int getIntSockOpt(int socket, int level, int option) throws PosixException {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SocketModuleBuiltins.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import com.oracle.graal.python.runtime.PosixSupportLibrary;
109109
import com.oracle.graal.python.runtime.PosixSupportLibrary.AddrInfoCursor;
110110
import com.oracle.graal.python.runtime.PosixSupportLibrary.AddrInfoCursorLibrary;
111-
import com.oracle.graal.python.runtime.PosixSupportLibrary.FamilySpecificSockAddr;
112111
import com.oracle.graal.python.runtime.PosixSupportLibrary.GetAddrInfoException;
113112
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
114113
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
@@ -276,16 +275,16 @@ TruffleString doGeneric(VirtualFrame frame,
276275
}
277276
}
278277

279-
@Builtin(name = "gethostbyaddr", minNumOfPositionalArgs = 1, numOfPositionalOnlyArgs = 1, parameterNames = {"ip"})
280-
@ArgumentClinic(name = "ip", conversion = ArgumentClinic.ClinicConversion.TString)
278+
@Builtin(name = "gethostbyaddr", minNumOfPositionalArgs = 1)
281279
@GenerateNodeFactory
282-
public abstract static class GetHostByAddrNode extends PythonUnaryClinicBuiltinNode {
280+
public abstract static class GetHostByAddrNode extends PythonUnaryBuiltinNode {
283281
@Specialization
284-
Object doGeneric(VirtualFrame frame, TruffleString ip,
282+
Object doGeneric(VirtualFrame frame, Object ip,
285283
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
286284
@CachedLibrary(limit = "1") AddrInfoCursorLibrary addrInfoCursorLib,
287285
@CachedLibrary(limit = "1") UniversalSockAddrLibrary sockAddrLibrary,
288286
@Bind("this") Node inliningTarget,
287+
@Cached("createIdnaConverter()") IdnaFromStringOrBytesConverterNode idnaConverter,
289288
@Cached SocketNodes.SetIpAddrNode setIpAddrNode,
290289
@Cached SequenceStorageNodes.AppendNode appendNode,
291290
@Cached SocketNodes.MakeIpAddrNode makeIpAddrNode,
@@ -298,7 +297,7 @@ Object doGeneric(VirtualFrame frame, TruffleString ip,
298297
* might want to use the legacy API in the future
299298
*/
300299
auditNode.audit(inliningTarget, "socket.gethostbyaddr", ip);
301-
UniversalSockAddr addr = setIpAddrNode.execute(frame, ip, AF_UNSPEC.value);
300+
UniversalSockAddr addr = setIpAddrNode.execute(frame, idnaConverter.execute(frame, ip), AF_UNSPEC.value);
302301
int family = sockAddrLibrary.getFamily(addr);
303302
try {
304303
Object[] getnameinfoResult = posixLib.getnameinfo(getPosixSupport(), addr, NI_NAMEREQD.value);
@@ -333,9 +332,9 @@ Object doGeneric(VirtualFrame frame, TruffleString ip,
333332
}
334333
}
335334

336-
@Override
337-
protected ArgumentClinicProvider getArgumentClinic() {
338-
return SocketModuleBuiltinsClinicProviders.GetHostByAddrNodeClinicProviderGen.INSTANCE;
335+
@NeverDefault
336+
protected static IdnaFromStringOrBytesConverterNode createIdnaConverter() {
337+
return IdnaFromStringOrBytesConverterNode.create("gethostbyname", 1);
339338
}
340339
}
341340

@@ -352,7 +351,7 @@ TruffleString getHostByName(VirtualFrame frame, Object nameObj,
352351
@Cached SocketNodes.SetIpAddrNode setIpAddrNode,
353352
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode,
354353
@Cached PythonObjectFactory factory) {
355-
TruffleString name = idnaConverter.execute(frame, nameObj);
354+
byte[] name = idnaConverter.execute(frame, nameObj);
356355
auditNode.audit(inliningTarget, "socket.gethostbyname", factory.createTuple(new Object[]{nameObj}));
357356
UniversalSockAddr addr = setIpAddrNode.execute(frame, name, AF_INET.value);
358357
Inet4SockAddr inet4SockAddr = addrLib.asInet4SockAddr(addr);
@@ -464,7 +463,7 @@ Object getServByPort(int port, Object protocolNameObj,
464463
try {
465464
gil.release(true);
466465
try {
467-
UniversalSockAddr addr = posixLib.createUniversalSockAddr(getPosixSupport(), new Inet4SockAddr(port, INADDR_ANY.value));
466+
UniversalSockAddr addr = posixLib.createUniversalSockAddrInet4(getPosixSupport(), new Inet4SockAddr(port, INADDR_ANY.value));
468467
int flags = 0;
469468
if (protocolName != null && equalNode.execute(protocolName, T_UDP, TS_ENCODING)) {
470469
flags |= NI_DGRAM.value;
@@ -561,19 +560,19 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, int flags,
561560
gil.acquire();
562561
}
563562

564-
FamilySpecificSockAddr queryAddr;
563+
UniversalSockAddr queryAddr;
565564
if (family == AF_INET.value) {
566565
if (addrLen != 2) {
567566
throw raise(OSError, ErrorMessages.IPV4_MUST_BE_2_TUPLE);
568567
}
569-
queryAddr = new Inet4SockAddr(port, sockAddrLibrary.asInet4SockAddr(resolvedAddr).getAddress());
568+
queryAddr = posixLib.createUniversalSockAddrInet4(getPosixSupport(), new Inet4SockAddr(port, sockAddrLibrary.asInet4SockAddr(resolvedAddr).getAddress()));
570569
} else if (family == AF_INET6.value) {
571-
queryAddr = new Inet6SockAddr(port, sockAddrLibrary.asInet6SockAddr(resolvedAddr).getAddress(), flowinfo, scopeid);
570+
queryAddr = posixLib.createUniversalSockAddrInet6(getPosixSupport(), new Inet6SockAddr(port, sockAddrLibrary.asInet6SockAddr(resolvedAddr).getAddress(), flowinfo, scopeid));
572571
} else {
573572
throw raise(OSError, ErrorMessages.UNKNOWN_FAMILY);
574573
}
575574

576-
Object[] getnameinfo = posixLib.getnameinfo(getPosixSupport(), posixLib.createUniversalSockAddr(getPosixSupport(), queryAddr), flags);
575+
Object[] getnameinfo = posixLib.getnameinfo(getPosixSupport(), queryAddr, flags);
577576
TruffleString host = posixLib.getPathAsString(getPosixSupport(), getnameinfo[0]);
578577
TruffleString service = posixLib.getPathAsString(getPosixSupport(), getnameinfo[1]);
579578
return factory.createTuple(new Object[]{host, service});
@@ -620,7 +619,7 @@ Object getAddrInfo(VirtualFrame frame, Object hostObject, Object portObject, int
620619
@Cached PythonObjectFactory factory) {
621620
Object host = null;
622621
if (hostObject != PNone.NONE) {
623-
host = posixLib.createPathFromString(getPosixSupport(), idna.execute(frame, hostObject));
622+
host = posixLib.createPathFromBytes(getPosixSupport(), idna.execute(frame, hostObject));
624623
}
625624

626625
Object port;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ThreadModuleBuiltins.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.modules;
4242

4343
import static com.oracle.graal.python.builtins.objects.thread.AbstractPythonLock.TIMEOUT_MAX;
44+
import static com.oracle.graal.python.nodes.BuiltinNames.J_EXIT;
4445
import static com.oracle.graal.python.nodes.BuiltinNames.J__THREAD;
4546
import static com.oracle.graal.python.nodes.BuiltinNames.T__THREAD;
4647
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
@@ -62,6 +63,7 @@
6263
import com.oracle.graal.python.builtins.objects.thread.PThread;
6364
import com.oracle.graal.python.builtins.objects.thread.PThreadLocal;
6465
import com.oracle.graal.python.nodes.ErrorMessages;
66+
import com.oracle.graal.python.nodes.PRaiseNode;
6567
import com.oracle.graal.python.nodes.WriteUnraisableNode;
6668
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
6769
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
@@ -72,6 +74,7 @@
7274
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7375
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
7476
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
77+
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinObjectProfile;
7578
import com.oracle.graal.python.runtime.GilNode;
7679
import com.oracle.graal.python.runtime.PythonContext;
7780
import com.oracle.graal.python.runtime.exception.PException;
@@ -256,20 +259,20 @@ long start(VirtualFrame frame, Object cls, Object callable, Object args, Object
256259
// connected as a caller which is incorrect. However, the thread-local
257260
// 'topframeref' is initialized with EMPTY which will be picked up.
258261
callNode.execute(null, callable, arguments, keywords);
262+
} catch (PythonThreadKillException e) {
263+
return;
264+
} catch (PException e) {
265+
if (!IsBuiltinObjectProfile.profileObjectUncached(e.getUnreifiedException(), PythonBuiltinClassType.SystemExit)) {
266+
WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), IN_THREAD_STARTED_BY, callable);
267+
}
259268
} finally {
260-
// the catch blocks run ofter the gil is released, so we decrement the
261-
// threadcount here while still protected by the gil
262269
try {
263270
curCount = lib.getIntOrDefault(threadModule, THREAD_COUNT, 1);
264271
} catch (UnexpectedResultException ure) {
265272
throw CompilerDirectives.shouldNotReachHere();
266273
}
267274
lib.putInt(threadModule, THREAD_COUNT, curCount - 1);
268275
}
269-
} catch (PythonThreadKillException e) {
270-
return;
271-
} catch (PException e) {
272-
WriteUnraisableNode.getUncached().execute(e.getUnreifiedException(), IN_THREAD_STARTED_BY, callable);
273276
}
274277
}, env.getContext(), context.getThreadGroup());
275278

@@ -312,4 +315,15 @@ protected ArgumentClinicProvider getArgumentClinic() {
312315
return ThreadModuleBuiltinsClinicProviders.InterruptMainThreadNodeClinicProviderGen.INSTANCE;
313316
}
314317
}
318+
319+
@Builtin(name = J_EXIT)
320+
@Builtin(name = "exit_thread")
321+
@GenerateNodeFactory
322+
abstract static class ExitNode extends PythonBuiltinNode {
323+
@Specialization
324+
Object exit(
325+
@Cached PRaiseNode raiseNode) {
326+
throw raiseNode.raiseSystemExit(PNone.NONE);
327+
}
328+
}
315329
}

0 commit comments

Comments
 (0)