test/jdk/java/nio/channels/unixdomain/Basic.java
branchunixdomainchannels
changeset 59052 15e9a570c6e6
child 59054 42bcf3042cd8
equal deleted inserted replaced
59048:4c3eb05c0701 59052:15e9a570c6e6
       
     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();
       
    48         sockRxBufsize = getInt(args[0]);
       
    49         sockTxBufsize = getInt(args[1]);
       
    50         if (args[2].equals("nagle-on"))
       
    51             nagle = true;
       
    52         else if (args[2].equals("nagle-off"))
       
    53             nagle = false;
       
    54 
       
    55         warmup();
       
    56         test(128, 1000);
       
    57         test(8 * 1024, 10000);
       
    58         test(16 * 1024, 10000);
       
    59         test(32 * 1024, 10000);
       
    60         // repeat
       
    61         System.out.println("---- Repeating all tests again -----");
       
    62         test(128, 1000);
       
    63         test(16 * 1024, 10000);
       
    64         test(32 * 1024, 10000);
       
    65     }
       
    66 
       
    67     static int getInt(String s) {
       
    68         if (s.equalsIgnoreCase("default"))
       
    69             return -1;
       
    70         else
       
    71             return Integer.parseInt(s);
       
    72     }
       
    73 
       
    74     static void usage() {
       
    75         System.out.println("usage: java Basic " +
       
    76             "<kernel sock read buf size> <kernel sock send buf size> {nagle-on|nagle-off}");
       
    77         System.out.println("nagle setting only affects TCP sockets");
       
    78         System.exit(-1);
       
    79     }
       
    80 
       
    81     static void warmup() throws Exception {
       
    82         Server server = new Server(StandardProtocolFamily.UNIX, 1024);
       
    83         Client client = new Client(server, 128, 100);
       
    84         server.start();
       
    85         client.run();
       
    86 
       
    87         server = new Server(StandardProtocolFamily.INET, 1024);
       
    88         client = new Client(server, 128, 100);
       
    89         server.start();
       
    90         client.run();
       
    91     }
       
    92 
       
    93     static void test(int bufsize, int nbufs) throws Exception {
       
    94         long unix = testUnix(bufsize, nbufs);
       
    95         long inet = testInet(bufsize, nbufs);
       
    96         // expect unix to be faster (express as percentage of inet)
       
    97         long percent = (unix * 100) / inet;
       
    98         System.out.printf ("Unix elapsed time is %d%% of the INET time\n\n", percent);
       
    99     }
       
   100 
       
   101     static long testUnix(int bufsize, int nbufs) throws Exception {
       
   102         Server server = new Server(StandardProtocolFamily.UNIX, bufsize);
       
   103         Client client = new Client(server, bufsize, nbufs);
       
   104         System.out.printf("Test (family=unix bufsize=%d, nbufs=%d) ", bufsize, nbufs);
       
   105         server.start();
       
   106         client.run();
       
   107         long unix = client.elapsed();
       
   108         int sbuf = client.sendSocketBufsize();
       
   109         int rbuf = client.receiveSocketBufsize();
       
   110         System.out.printf("completed in %d ns (sbuf=%d, rbuf=%d)\n", unix, sbuf, rbuf);
       
   111         System.gc();
       
   112         return unix;
       
   113     }
       
   114 
       
   115     static long testInet(int bufsize, int nbufs) throws Exception {
       
   116         Server server = new Server(StandardProtocolFamily.INET, bufsize);
       
   117         Client client = new Client(server, bufsize, nbufs);
       
   118         System.out.printf("Test (family=inet bufsize=%d, nbufs=%d) ", bufsize, nbufs);
       
   119         server.start();
       
   120         client.run();
       
   121         long inet = client.elapsed();
       
   122         System.gc();
       
   123         int sbuf = client.sendSocketBufsize();
       
   124         int rbuf = client.receiveSocketBufsize();
       
   125         System.out.printf("completed in %d ns (sbuf=%d, rbuf=%d)\n", inet, sbuf, rbuf);
       
   126         return inet;
       
   127     }
       
   128 
       
   129     static void setNagle(SocketChannel chan, boolean value) throws IOException {
       
   130         if (chan.getRemoteAddress() instanceof InetSocketAddress) {
       
   131             chan.setOption(StandardSocketOptions.TCP_NODELAY, value);
       
   132         }
       
   133     }
       
   134 
       
   135     static void setSendBufferSize(SocketChannel chan, int bufsize) throws IOException {
       
   136         if (bufsize < 0)
       
   137             return;
       
   138         chan.setOption(StandardSocketOptions.SO_SNDBUF, bufsize);
       
   139     }
       
   140 
       
   141     static void setRecvBufferSize(SocketChannel chan, int bufsize) throws IOException {
       
   142         if (bufsize < 0)
       
   143             return;
       
   144         chan.setOption(StandardSocketOptions.SO_RCVBUF, bufsize);
       
   145     }
       
   146 
       
   147     static class Server extends Thread {
       
   148 
       
   149         ServerSocketChannel server;
       
   150         SocketAddress address;
       
   151         SocketChannel connection;
       
   152         SelectionKey ckey;
       
   153         List<ByteBuffer> toSend = new LinkedList<>();
       
   154         final int bufsize;
       
   155 
       
   156         Server(ProtocolFamily family, int bufsize) throws IOException {
       
   157             setDaemon(true);
       
   158             SocketAddress addr;
       
   159             this.bufsize = bufsize;
       
   160             if (family == StandardProtocolFamily.UNIX) {
       
   161                 server = ServerSocketChannel.open(family);
       
   162                 Path sockfile = Path.of("server.sock");
       
   163                 Files.deleteIfExists(sockfile);
       
   164                 addr = new UnixDomainSocketAddress(sockfile);
       
   165             } else {
       
   166                 server = ServerSocketChannel.open();
       
   167                 addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
       
   168             }
       
   169             server.bind(addr);
       
   170             address = server.getLocalAddress();
       
   171         }
       
   172 
       
   173         SocketAddress getAddress() {
       
   174             return address;
       
   175         }
       
   176 
       
   177         public void  run() {
       
   178             try {
       
   179                 server.configureBlocking(false);
       
   180                 Selector sel = Selector.open();
       
   181                 server.register(sel, SelectionKey.OP_ACCEPT, server);
       
   182 
       
   183                 while (true) {
       
   184                     int n = sel.select();
       
   185                     var keys = sel.selectedKeys();
       
   186                     for (SelectionKey key : keys) {
       
   187                         if (key.isAcceptable()) {
       
   188                             if (connection != null)
       
   189                                 throw new RuntimeException("One connection per server");
       
   190                             ServerSocketChannel server = (ServerSocketChannel)key.attachment();
       
   191                             connection = server.accept();
       
   192                             connection.configureBlocking(false);
       
   193                             setNagle(connection, nagle);
       
   194                             setSendBufferSize(connection, sockTxBufsize);
       
   195                             setRecvBufferSize(connection, sockRxBufsize);
       
   196                             ckey = connection.register(sel, SelectionKey.OP_READ, connection);
       
   197                         }
       
   198                         if (key.isReadable()) {
       
   199                             ByteBuffer buf = ByteBuffer.allocate(bufsize);
       
   200                             SocketChannel channel = (SocketChannel)key.attachment();
       
   201                             int m = channel.read(buf);
       
   202                             if (m == -1) {
       
   203                                 channel.close();
       
   204                                 server.close();
       
   205                                 return;
       
   206                             } else {
       
   207                                 buf.flip();
       
   208                                 // ECHO
       
   209                                 toSend.add(buf);
       
   210                                 ckey.interestOpsOr(SelectionKey.OP_WRITE);
       
   211                             }
       
   212                         }
       
   213                         if (key.isWritable()) {
       
   214                             if (toSend.isEmpty()) {
       
   215                                 ckey.interestOpsAnd(~SelectionKey.OP_WRITE);
       
   216                             } else {
       
   217                                 ByteBuffer b = toSend.get(0);
       
   218                                 connection.write(b);
       
   219                                 if (b.remaining() == 0)
       
   220                                     toSend.remove(0);
       
   221                             }
       
   222                         }
       
   223                     }
       
   224                     keys.clear();
       
   225                 }
       
   226             } catch (IOException e) {
       
   227                 throw new RuntimeException(e);
       
   228             }
       
   229         }
       
   230     }
       
   231 
       
   232     static void fill(ByteBuffer buf) {
       
   233         int n = buf.remaining();
       
   234         for (int i=0; i<n; i++) {
       
   235             buf.put((byte)(i % 127));
       
   236         }
       
   237     }
       
   238 
       
   239     static void check(ByteBuffer buf, int expected, int iteration) throws Exception {
       
   240         if (buf.remaining() != expected)
       
   241             throw new RuntimeException();
       
   242         for (int i=0; i<expected; i++) {
       
   243             int b = buf.get();
       
   244             if (b != (i % 127)) {
       
   245                 System.out.printf("b = %d , i = %d\n", b, i);
       
   246                 throw new RuntimeException("Iteration " + Integer.toString(iteration));
       
   247             }
       
   248         }
       
   249     }
       
   250 
       
   251 // read until buf is full
       
   252 
       
   253     static void readFully(SocketChannel chan, ByteBuffer buf) throws Exception {
       
   254         int n = buf.remaining();
       
   255         while (n > 0) {
       
   256             int c = chan.read(buf);
       
   257             if (c == -1)
       
   258                 throw new RuntimeException("EOF");
       
   259             n -= c;
       
   260         }
       
   261     }
       
   262 
       
   263     static class Client {
       
   264         Server server;
       
   265         int bufsize, nbufs, sbuf, rbuf;
       
   266         long elapsed;
       
   267 
       
   268         Client(Server server, int bufsize, int nbufs) {
       
   269             this.server = server;
       
   270             this.bufsize = bufsize;
       
   271             this.nbufs = nbufs;
       
   272         }
       
   273 
       
   274         public void run() throws Exception {
       
   275             SocketAddress remote = server.getAddress();
       
   276             long start = System.nanoTime();
       
   277             SocketChannel c = null;
       
   278             String fam;
       
   279             if (remote instanceof UnixDomainSocketAddress) {
       
   280                 c = SocketChannel.open(StandardProtocolFamily.UNIX);
       
   281                 fam = "unix";
       
   282             } else {
       
   283                 c = SocketChannel.open();
       
   284                 fam = "inet";
       
   285             }
       
   286             setNagle(c, nagle);
       
   287             c.connect(remote);
       
   288             setSendBufferSize(c, sockTxBufsize);
       
   289             setRecvBufferSize(c, sockRxBufsize);
       
   290             ByteBuffer tx = ByteBuffer.allocate(bufsize);
       
   291             ByteBuffer rx = ByteBuffer.allocate(bufsize);
       
   292             fill(tx);
       
   293             for (int i=0; i<nbufs; i++) {
       
   294                 tx.rewind();
       
   295                 c.write(tx);
       
   296                 rx.clear();
       
   297                 readFully(c, rx);
       
   298                 rx.flip();
       
   299                 check(rx, bufsize, i);
       
   300             }
       
   301             long end = System.nanoTime();
       
   302             elapsed = end - start;
       
   303             sbuf = c.getOption(StandardSocketOptions.SO_SNDBUF);
       
   304             rbuf = c.getOption(StandardSocketOptions.SO_SNDBUF);
       
   305             c.close();
       
   306         }
       
   307 
       
   308         long elapsed() {
       
   309             return elapsed;
       
   310         }
       
   311 
       
   312         int receiveSocketBufsize() {return rbuf;}
       
   313         int sendSocketBufsize() {return sbuf;}
       
   314     }
       
   315 
       
   316 }