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.access.JavaLangAccess;
 29 import jdk.internal.access.SharedSecrets;
 30 import jdk.internal.vm.ContinuationSupport;
 31 
 32 /**
 33  * Default PollerProvider for Linux.
 34  */
 35 class DefaultPollerProvider extends PollerProvider {
 36     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 37 
 38     private static final boolean USE_IOURING;
 39     private static final boolean USE_IORING_OP_READ;
 40     private static final boolean USE_IORING_OP_WRITE;
 41     static {
 42         String s = System.getProperty("jdk.io_uring");
 43         if ("".equals(s) || Boolean.parseBoolean(s)) {
 44             USE_IOURING = true;
 45             s = System.getProperty("jdk.io_uring.read");
 46             USE_IORING_OP_READ = "".equals(s) || Boolean.parseBoolean(s);
 47             s = System.getProperty("jdk.io_uring.write");
 48             USE_IORING_OP_WRITE = "".equals(s) || Boolean.parseBoolean(s);
 49         } else {
 50             USE_IOURING = false;
 51             USE_IORING_OP_READ = false;
 52             USE_IORING_OP_WRITE = false;
 53         }
 54     }
 55 
 56     DefaultPollerProvider(Poller.Mode mode) {
 57         super(mode);
 58     }
 59 
 60     DefaultPollerProvider() {
 61         this(ContinuationSupport.isSupported() && !JLA.isCustomDefaultVirtualThreadScheduler()
 62                 ? Poller.Mode.VTHREAD_POLLERS
 63                 : Poller.Mode.SYSTEM_THREADS);
 64     }
 65 
 66     @Override
 67     int defaultReadPollers() {
 68         int ncpus = Runtime.getRuntime().availableProcessors();
 69         return switch (pollerMode()) {
 70             case SYSTEM_THREADS  -> Math.max(Integer.highestOneBit(ncpus / 4), 1);
 71             case VTHREAD_POLLERS -> Math.min(Integer.highestOneBit(ncpus), 32);
 72             default              -> super.defaultReadPollers();
 73         };
 74     }
 75 
 76     @Override
 77     Poller readPoller(boolean subPoller) throws IOException {
 78         Poller.Mode mode = pollerMode();
 79         if (USE_IOURING) {
 80             // read poller is system thread in SYSTEM_THREADS mode
 81             boolean supportReadOps = USE_IORING_OP_READ
 82                     && ((mode == Poller.Mode.SYSTEM_THREADS) || subPoller);
 83             return new IoUringPoller(mode, subPoller, true, supportReadOps);
 84         } else {
 85             return new EPollPoller(mode, subPoller, true);
 86         }
 87     }
 88 
 89     @Override
 90     Poller writePoller(boolean subPoller) throws IOException {
 91         Poller.Mode mode = pollerMode();
 92         if (USE_IOURING) {
 93             // write poller is system thread in SYSTEM_THREADS and POLLER_PER_CARRIER modes
 94             boolean supportWriteOps = USE_IORING_OP_WRITE
 95                     && ((mode != Poller.Mode.VTHREAD_POLLERS) || subPoller);
 96             return new IoUringPoller(mode, subPoller, false, supportWriteOps);
 97         } else {
 98             return new EPollPoller(mode, subPoller, false);
 99         }
100     }
101 
102     @Override
103     boolean supportReadOps() {
104         return USE_IORING_OP_READ;
105     }
106 
107     @Override
108     boolean supportWriteOps() {
109         return USE_IORING_OP_WRITE;
110     }
111 }