59052
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 8231358
|
|
27 |
* @run main/othervm Basic 32000 32000 nagle-off
|
|
28 |
* @run main/othervm Basic default default nagle-off
|
|
29 |
* @summary Basic test for Unix Domain and Inet socket and server socket channels
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.net.*;
|
|
34 |
import java.nio.ByteBuffer;
|
|
35 |
import java.nio.channels.*;
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.Path;
|
|
38 |
import java.util.LinkedList;
|
|
39 |
import java.util.List;
|
|
40 |
|
|
41 |
public class Basic {
|
|
42 |
static int sockRxBufsize, sockTxBufsize;
|
|
43 |
static boolean nagle;
|
|
44 |
|
|
45 |
public static void main(String args[]) throws Exception {
|
|
46 |
if (args.length != 3)
|
|
47 |
usage();
|
59054
|
48 |
if (!supported()) {
|
|
49 |
System.out.println("Unix domain channels not supported");
|
|
50 |
return;
|
|
51 |
}
|
59052
|
52 |
sockRxBufsize = getInt(args[0]);
|
|
53 |
sockTxBufsize = getInt(args[1]);
|
|
54 |
if (args[2].equals("nagle-on"))
|
|
55 |
nagle = true;
|
|
56 |
else if (args[2].equals("nagle-off"))
|
|
57 |
nagle = false;
|
|
58 |
|
|
59 |
warmup();
|
|
60 |
test(128, 1000);
|
|
61 |
test(8 * 1024, 10000);
|
|
62 |
test(16 * 1024, 10000);
|
|
63 |
test(32 * 1024, 10000);
|
59054
|
64 |
}
|
|
65 |
|
|
66 |
static boolean supported() {
|
|
67 |
try {
|
|
68 |
SocketChannel.open(StandardProtocolFamily.UNIX);
|
|
69 |
} catch (UnsupportedAddressTypeException e) {
|
|
70 |
return false;
|
|
71 |
} catch (Exception e) {
|
|
72 |
return true; // continue test to see what problem is
|
|
73 |
}
|
|
74 |
return true;
|
59052
|
75 |
}
|
|
76 |
|
|
77 |
static int getInt(String s) {
|
|
78 |
if (s.equalsIgnoreCase("default"))
|
|
79 |
return -1;
|
|
80 |
else
|
|
81 |
return Integer.parseInt(s);
|
|
82 |
}
|
|
83 |
|
|
84 |
static void usage() {
|
|
85 |
System.out.println("usage: java Basic " +
|
|
86 |
"<kernel sock read buf size> <kernel sock send buf size> {nagle-on|nagle-off}");
|
|
87 |
System.out.println("nagle setting only affects TCP sockets");
|
|
88 |
System.exit(-1);
|
|
89 |
}
|
|
90 |
|
|
91 |
static void warmup() throws Exception {
|
|
92 |
Server server = new Server(StandardProtocolFamily.UNIX, 1024);
|
|
93 |
Client client = new Client(server, 128, 100);
|
|
94 |
server.start();
|
|
95 |
client.run();
|
|
96 |
|
|
97 |
server = new Server(StandardProtocolFamily.INET, 1024);
|
|
98 |
client = new Client(server, 128, 100);
|
|
99 |
server.start();
|
|
100 |
client.run();
|
|
101 |
}
|
|
102 |
|
|
103 |
static void test(int bufsize, int nbufs) throws Exception {
|
|
104 |
long unix = testUnix(bufsize, nbufs);
|
|
105 |
long inet = testInet(bufsize, nbufs);
|
|
106 |
// expect unix to be faster (express as percentage of inet)
|
|
107 |
long percent = (unix * 100) / inet;
|
|
108 |
System.out.printf ("Unix elapsed time is %d%% of the INET time\n\n", percent);
|
|
109 |
}
|
|
110 |
|
|
111 |
static long testUnix(int bufsize, int nbufs) throws Exception {
|
|
112 |
Server server = new Server(StandardProtocolFamily.UNIX, bufsize);
|
|
113 |
Client client = new Client(server, bufsize, nbufs);
|
|
114 |
System.out.printf("Test (family=unix bufsize=%d, nbufs=%d) ", bufsize, nbufs);
|
|
115 |
server.start();
|
|
116 |
client.run();
|
|
117 |
long unix = client.elapsed();
|
|
118 |
int sbuf = client.sendSocketBufsize();
|
|
119 |
int rbuf = client.receiveSocketBufsize();
|
|
120 |
System.out.printf("completed in %d ns (sbuf=%d, rbuf=%d)\n", unix, sbuf, rbuf);
|
|
121 |
System.gc();
|
|
122 |
return unix;
|
|
123 |
}
|
|
124 |
|
|
125 |
static long testInet(int bufsize, int nbufs) throws Exception {
|
|
126 |
Server server = new Server(StandardProtocolFamily.INET, bufsize);
|
|
127 |
Client client = new Client(server, bufsize, nbufs);
|
|
128 |
System.out.printf("Test (family=inet bufsize=%d, nbufs=%d) ", bufsize, nbufs);
|
|
129 |
server.start();
|
|
130 |
client.run();
|
|
131 |
long inet = client.elapsed();
|
|
132 |
System.gc();
|
|
133 |
int sbuf = client.sendSocketBufsize();
|
|
134 |
int rbuf = client.receiveSocketBufsize();
|
|
135 |
System.out.printf("completed in %d ns (sbuf=%d, rbuf=%d)\n", inet, sbuf, rbuf);
|
|
136 |
return inet;
|
|
137 |
}
|
|
138 |
|
|
139 |
static void setNagle(SocketChannel chan, boolean value) throws IOException {
|
|
140 |
if (chan.getRemoteAddress() instanceof InetSocketAddress) {
|
|
141 |
chan.setOption(StandardSocketOptions.TCP_NODELAY, value);
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
static void setSendBufferSize(SocketChannel chan, int bufsize) throws IOException {
|
|
146 |
if (bufsize < 0)
|
|
147 |
return;
|
|
148 |
chan.setOption(StandardSocketOptions.SO_SNDBUF, bufsize);
|
|
149 |
}
|
|
150 |
|
|
151 |
static void setRecvBufferSize(SocketChannel chan, int bufsize) throws IOException {
|
|
152 |
if (bufsize < 0)
|
|
153 |
return;
|
|
154 |
chan.setOption(StandardSocketOptions.SO_RCVBUF, bufsize);
|
|
155 |
}
|
|
156 |
|
|
157 |
static class Server extends Thread {
|
|
158 |
|
|
159 |
ServerSocketChannel server;
|
|
160 |
SocketAddress address;
|
|
161 |
SocketChannel connection;
|
|
162 |
SelectionKey ckey;
|
|
163 |
List<ByteBuffer> toSend = new LinkedList<>();
|
|
164 |
final int bufsize;
|
|
165 |
|
|
166 |
Server(ProtocolFamily family, int bufsize) throws IOException {
|
|
167 |
setDaemon(true);
|
|
168 |
SocketAddress addr;
|
|
169 |
this.bufsize = bufsize;
|
|
170 |
if (family == StandardProtocolFamily.UNIX) {
|
|
171 |
server = ServerSocketChannel.open(family);
|
|
172 |
Path sockfile = Path.of("server.sock");
|
|
173 |
Files.deleteIfExists(sockfile);
|
|
174 |
addr = new UnixDomainSocketAddress(sockfile);
|
|
175 |
} else {
|
|
176 |
server = ServerSocketChannel.open();
|
|
177 |
addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
|
|
178 |
}
|
|
179 |
server.bind(addr);
|
|
180 |
address = server.getLocalAddress();
|
|
181 |
}
|
|
182 |
|
|
183 |
SocketAddress getAddress() {
|
|
184 |
return address;
|
|
185 |
}
|
|
186 |
|
|
187 |
public void run() {
|
|
188 |
try {
|
|
189 |
server.configureBlocking(false);
|
|
190 |
Selector sel = Selector.open();
|
|
191 |
server.register(sel, SelectionKey.OP_ACCEPT, server);
|
|
192 |
|
|
193 |
while (true) {
|
|
194 |
int n = sel.select();
|
|
195 |
var keys = sel.selectedKeys();
|
|
196 |
for (SelectionKey key : keys) {
|
|
197 |
if (key.isAcceptable()) {
|
|
198 |
if (connection != null)
|
|
199 |
throw new RuntimeException("One connection per server");
|
|
200 |
ServerSocketChannel server = (ServerSocketChannel)key.attachment();
|
|
201 |
connection = server.accept();
|
|
202 |
connection.configureBlocking(false);
|
|
203 |
setNagle(connection, nagle);
|
|
204 |
setSendBufferSize(connection, sockTxBufsize);
|
|
205 |
setRecvBufferSize(connection, sockRxBufsize);
|
|
206 |
ckey = connection.register(sel, SelectionKey.OP_READ, connection);
|
|
207 |
}
|
|
208 |
if (key.isReadable()) {
|
|
209 |
ByteBuffer buf = ByteBuffer.allocate(bufsize);
|
|
210 |
SocketChannel channel = (SocketChannel)key.attachment();
|
|
211 |
int m = channel.read(buf);
|
|
212 |
if (m == -1) {
|
|
213 |
channel.close();
|
|
214 |
server.close();
|
|
215 |
return;
|
|
216 |
} else {
|
|
217 |
buf.flip();
|
|
218 |
// ECHO
|
|
219 |
toSend.add(buf);
|
|
220 |
ckey.interestOpsOr(SelectionKey.OP_WRITE);
|
|
221 |
}
|
|
222 |
}
|
|
223 |
if (key.isWritable()) {
|
|
224 |
if (toSend.isEmpty()) {
|
|
225 |
ckey.interestOpsAnd(~SelectionKey.OP_WRITE);
|
|
226 |
} else {
|
|
227 |
ByteBuffer b = toSend.get(0);
|
|
228 |
connection.write(b);
|
|
229 |
if (b.remaining() == 0)
|
|
230 |
toSend.remove(0);
|
|
231 |
}
|
|
232 |
}
|
|
233 |
}
|
|
234 |
keys.clear();
|
|
235 |
}
|
|
236 |
} catch (IOException e) {
|
|
237 |
throw new RuntimeException(e);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
static void fill(ByteBuffer buf) {
|
|
243 |
int n = buf.remaining();
|
|
244 |
for (int i=0; i<n; i++) {
|
|
245 |
buf.put((byte)(i % 127));
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
static void check(ByteBuffer buf, int expected, int iteration) throws Exception {
|
|
250 |
if (buf.remaining() != expected)
|
|
251 |
throw new RuntimeException();
|
|
252 |
for (int i=0; i<expected; i++) {
|
|
253 |
int b = buf.get();
|
|
254 |
if (b != (i % 127)) {
|
|
255 |
System.out.printf("b = %d , i = %d\n", b, i);
|
|
256 |
throw new RuntimeException("Iteration " + Integer.toString(iteration));
|
|
257 |
}
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
// read until buf is full
|
|
262 |
|
|
263 |
static void readFully(SocketChannel chan, ByteBuffer buf) throws Exception {
|
|
264 |
int n = buf.remaining();
|
|
265 |
while (n > 0) {
|
|
266 |
int c = chan.read(buf);
|
|
267 |
if (c == -1)
|
|
268 |
throw new RuntimeException("EOF");
|
|
269 |
n -= c;
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
static class Client {
|
|
274 |
Server server;
|
|
275 |
int bufsize, nbufs, sbuf, rbuf;
|
|
276 |
long elapsed;
|
|
277 |
|
|
278 |
Client(Server server, int bufsize, int nbufs) {
|
|
279 |
this.server = server;
|
|
280 |
this.bufsize = bufsize;
|
|
281 |
this.nbufs = nbufs;
|
|
282 |
}
|
|
283 |
|
|
284 |
public void run() throws Exception {
|
|
285 |
SocketAddress remote = server.getAddress();
|
|
286 |
long start = System.nanoTime();
|
|
287 |
SocketChannel c = null;
|
|
288 |
String fam;
|
|
289 |
if (remote instanceof UnixDomainSocketAddress) {
|
|
290 |
c = SocketChannel.open(StandardProtocolFamily.UNIX);
|
|
291 |
fam = "unix";
|
|
292 |
} else {
|
|
293 |
c = SocketChannel.open();
|
|
294 |
fam = "inet";
|
|
295 |
}
|
|
296 |
setNagle(c, nagle);
|
|
297 |
c.connect(remote);
|
|
298 |
setSendBufferSize(c, sockTxBufsize);
|
|
299 |
setRecvBufferSize(c, sockRxBufsize);
|
|
300 |
ByteBuffer tx = ByteBuffer.allocate(bufsize);
|
|
301 |
ByteBuffer rx = ByteBuffer.allocate(bufsize);
|
|
302 |
fill(tx);
|
|
303 |
for (int i=0; i<nbufs; i++) {
|
|
304 |
tx.rewind();
|
|
305 |
c.write(tx);
|
|
306 |
rx.clear();
|
|
307 |
readFully(c, rx);
|
|
308 |
rx.flip();
|
|
309 |
check(rx, bufsize, i);
|
|
310 |
}
|
|
311 |
long end = System.nanoTime();
|
|
312 |
elapsed = end - start;
|
|
313 |
sbuf = c.getOption(StandardSocketOptions.SO_SNDBUF);
|
|
314 |
rbuf = c.getOption(StandardSocketOptions.SO_SNDBUF);
|
|
315 |
c.close();
|
|
316 |
}
|
|
317 |
|
|
318 |
long elapsed() {
|
|
319 |
return elapsed;
|
|
320 |
}
|
|
321 |
|
|
322 |
int receiveSocketBufsize() {return rbuf;}
|
|
323 |
int sendSocketBufsize() {return sbuf;}
|
|
324 |
}
|
|
325 |
|
|
326 |
}
|