Skip to content

Commit 7dcc386

Browse files
vyMichael-Mc-Mahon
authored andcommitted
8354024: [JMH] Create ephemeral UnixDomainSocketAddress provider with thread-safe close semantics
Reviewed-by: michaelm
1 parent 85db463 commit 7dcc386

File tree

2 files changed

+24
-54
lines changed

2 files changed

+24
-54
lines changed

test/micro/org/openjdk/bench/java/net/SocketChannelConnectionSetup.java

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import java.net.UnixDomainSocketAddress;
2828
import java.nio.channels.ServerSocketChannel;
2929
import java.nio.channels.SocketChannel;
30-
import java.nio.file.*;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
3132
import java.util.concurrent.TimeUnit;
32-
import java.util.concurrent.atomic.AtomicInteger;
3333

3434
import org.openjdk.jmh.annotations.*;
3535
import org.openjdk.jmh.runner.Runner;
@@ -49,55 +49,31 @@
4949
public class SocketChannelConnectionSetup {
5050

5151
private ServerSocketChannel ssc;
52-
private SocketChannel s1, s2;
53-
54-
private static volatile String tempDir;
55-
private static final AtomicInteger count = new AtomicInteger(0);
56-
private volatile Path socket;
57-
58-
@Param({"inet", "unix"})
59-
private volatile String family;
6052

61-
static {
62-
try {
63-
Path p = Files.createTempDirectory("readWriteTest");
64-
tempDir = p.toString();
65-
} catch (IOException e) {
66-
tempDir = null;
67-
}
68-
}
53+
private Path sscFilePath;
6954

70-
private ServerSocketChannel getServerSocketChannel() throws IOException {
71-
if (family.equals("inet"))
72-
return getInetServerSocketChannel();
73-
else if (family.equals("unix"))
74-
return getUnixServerSocketChannel();
75-
throw new InternalError();
76-
}
77-
78-
79-
private ServerSocketChannel getInetServerSocketChannel() throws IOException {
80-
return ServerSocketChannel.open().bind(null);
81-
}
55+
private SocketChannel s1, s2;
8256

83-
private ServerSocketChannel getUnixServerSocketChannel() throws IOException {
84-
int next = count.incrementAndGet();
85-
socket = Paths.get(tempDir, Integer.toString(next));
86-
UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);
87-
return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);
88-
}
57+
@Param({"INET", "UNIX"})
58+
private String family;
8959

9060
@Setup(Level.Trial)
9161
public void beforeRun() throws IOException {
92-
ssc = getServerSocketChannel();
62+
StandardProtocolFamily typedFamily = StandardProtocolFamily.valueOf(family);
63+
ssc = ServerSocketChannel.open(typedFamily).bind(null);
64+
// Record the UDS file path right after binding, as the socket may be
65+
// closed later due to a failure, and subsequent calls to `getPath()`
66+
// will throw.
67+
sscFilePath = ssc.getLocalAddress() instanceof UnixDomainSocketAddress udsChannel
68+
? udsChannel.getPath()
69+
: null;
9370
}
9471

9572
@TearDown(Level.Trial)
96-
public void afterRun() throws IOException {
73+
public void afterRun() throws Exception {
9774
ssc.close();
98-
if (family.equals("unix")) {
99-
Files.deleteIfExists(socket);
100-
Files.deleteIfExists(Path.of(tempDir));
75+
if (sscFilePath != null) {
76+
Files.delete(sscFilePath);
10177
}
10278
}
10379

test/micro/org/openjdk/bench/java/net/UnixSocketChannelReadWrite.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,18 @@
4747
public class UnixSocketChannelReadWrite {
4848

4949
private ServerSocketChannel ssc;
50+
private Path sscFilePath;
5051
private SocketChannel s1, s2;
5152
private ReadThread rt;
5253
private ByteBuffer bb = ByteBuffer.allocate(1);
5354

54-
private volatile Path socket;
55-
56-
private ServerSocketChannel getServerSocketChannel() throws IOException {
57-
socket = Files.createTempDirectory(UnixSocketChannelReadWrite.class.getSimpleName()).resolve("sock");
58-
UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);
59-
ServerSocketChannel c = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
60-
c.bind(addr);
61-
return c;
62-
}
63-
6455
@Setup(Level.Trial)
6556
public void beforeRun() throws IOException {
66-
ssc = getServerSocketChannel();
57+
ssc = ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(null);
58+
// Record the UDS file path right after binding, as the socket may be
59+
// closed later due to a failure, and subsequent calls to `getPath()`
60+
// will throw.
61+
sscFilePath = ((UnixDomainSocketAddress) ssc.getLocalAddress()).getPath();
6762
s1 = SocketChannel.open(ssc.getLocalAddress());
6863
s2 = ssc.accept();
6964

@@ -79,8 +74,7 @@ public void afterRun() throws IOException, InterruptedException {
7974
s1.close();
8075
s2.close();
8176
ssc.close();
82-
Files.delete(socket);
83-
Files.delete(socket.getParent());
77+
Files.delete(sscFilePath);
8478
rt.join();
8579
}
8680

0 commit comments

Comments
 (0)