1 /*
  2  * Copyright (c) 2017, 2023, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package sun.nio.ch;
 26 
 27 import java.io.IOException;
 28 import jdk.internal.vm.ContinuationSupport;
 29 
 30 /**
 31  * Default PollerProvider for Linux.
 32  */
 33 class DefaultPollerProvider extends PollerProvider {
 34     private static final boolean USE_IOURING;
 35     private static final boolean USE_IORING_OP_READ;
 36     private static final boolean USE_IORING_OP_WRITE;
 37     static {
 38         String s = System.getProperty("jdk.io_uring");
 39         if ("".equals(s) || Boolean.parseBoolean(s)) {
 40             USE_IOURING = true;
 41             s = System.getProperty("jdk.io_uring.read");
 42             USE_IORING_OP_READ = "".equals(s) || Boolean.parseBoolean(s);
 43             s = System.getProperty("jdk.io_uring.write");
 44             USE_IORING_OP_WRITE = "".equals(s) || Boolean.parseBoolean(s);
 45         } else {
 46             USE_IOURING = false;
 47             USE_IORING_OP_READ = false;
 48             USE_IORING_OP_WRITE = false;
 49         }
 50     }
 51 
 52     DefaultPollerProvider(Poller.Mode mode) {
 53         super(mode);
 54     }
 55 
 56     DefaultPollerProvider() {
 57         this(ContinuationSupport.isSupported()
 58                 ? Poller.Mode.VTHREAD_POLLERS
 59                 : Poller.Mode.SYSTEM_THREADS);
 60     }
 61 
 62     @Override
 63     int defaultReadPollers() {
 64         int ncpus = Runtime.getRuntime().availableProcessors();
 65         return switch (pollerMode()) {
 66             case SYSTEM_THREADS  -> Math.max(Integer.highestOneBit(ncpus / 4), 1);
 67             case VTHREAD_POLLERS -> Math.min(Integer.highestOneBit(ncpus), 32);
 68             default              -> super.defaultReadPollers();
 69         };
 70     }
 71 
 72     @Override
 73     Poller readPoller(boolean subPoller) throws IOException {
 74         Poller.Mode mode = pollerMode();
 75         if (USE_IOURING) {
 76             // read poller is system thread in SYSTEM_THREADS mode
 77             boolean supportReadOps = USE_IORING_OP_READ
 78                     && ((mode == Poller.Mode.SYSTEM_THREADS) || subPoller);
 79             return new IoUringPoller(mode, subPoller, true, supportReadOps);
 80         } else {
 81             return new EPollPoller(mode, subPoller, true);
 82         }
 83     }
 84 
 85     @Override
 86     Poller writePoller(boolean subPoller) throws IOException {
 87         Poller.Mode mode = pollerMode();
 88         if (USE_IOURING) {
 89             // write poller is system thread in SYSTEM_THREADS and POLLER_PER_CARRIER modes
 90             boolean supportWriteOps = USE_IORING_OP_WRITE
 91                     && ((mode != Poller.Mode.VTHREAD_POLLERS) || subPoller);
 92             return new IoUringPoller(mode, subPoller, false, supportWriteOps);
 93         } else {
 94             return new EPollPoller(mode, subPoller, false);
 95         }
 96     }
 97 
 98     @Override
 99     boolean supportReadOps() {
100         return USE_IORING_OP_READ;
101     }
102 
103     @Override
104     boolean supportWriteOps() {
105         return USE_IORING_OP_WRITE;
106     }
107 }