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 java.util.Arrays;
 29 import java.util.Map;
 30 import java.util.concurrent.ConcurrentHashMap;
 31 import java.util.concurrent.Executor;
 32 import java.util.concurrent.Executors;
 33 import java.util.concurrent.ThreadFactory;
 34 import java.util.concurrent.locks.LockSupport;
 35 import java.util.function.BooleanSupplier;
 36 import jdk.internal.misc.InnocuousThread;
 37 import sun.security.action.GetPropertyAction;
 38 
 39 /**
 40  * Polls file descriptors. Virtual threads invoke the poll method to park
 41  * until a given file descriptor is ready for I/O.
 42  */
 43 abstract class Poller {
 44     private static final Pollers POLLERS;
 45     static {
 46         try {
 47             var pollers = new Pollers();
 48             pollers.start();
 49             POLLERS = pollers;
 50         } catch (IOException ioe) {
 51             throw new ExceptionInInitializerError(ioe);
 52         }
 53     }
 54 
 55     // maps file descriptors to parked Thread
 56     private final Map<Integer, Thread> map = new ConcurrentHashMap<>();
 57 
 58     /**
 59      * Poller mode.
 60      */
 61     enum Mode {
 62         /**
 63          * ReadPoller and WritePoller are dedicated platform threads that block waiting
 64          * for events and unpark virtual threads when file descriptors are ready for I/O.
 65          */
 66         SYSTEM_THREADS,
 67 
 68         /**
 69          * ReadPoller and WritePoller threads are virtual threads that poll for events,
 70          * yielding between polls and unparking virtual threads when file descriptors are
 71          * ready for I/O. If there are no events then the poller threads park until there
 72          * are I/O events to poll. This mode helps to integrate polling with virtual
 73          * thread scheduling. The approach is similar to the default scheme in "User-level
 74          * Threading: Have Your Cake and Eat It Too" by Karsten and Barghi 2020
 75          * (https://dl.acm.org/doi/10.1145/3379483).
 76          */
 77         VTHREAD_POLLERS
 78     }
 79 
 80     /**
 81      * Initialize a Poller.
 82      */
 83     protected Poller() {
 84     }
 85 
 86     /**
 87      * Returns the poller's file descriptor, used when the read and write poller threads
 88      * are virtual threads.
 89      *
 90      * @throws UnsupportedOperationException if not supported
 91      */
 92     int fdVal() {
 93         throw new UnsupportedOperationException();
 94     }
 95 
 96     /**
 97      * Register the file descriptor. The registration is "one shot", meaning it should
 98      * be polled at most once.
 99      */
100     abstract void implRegister(int fdVal) throws IOException;
101 
102     /**
103      * Deregister the file descriptor.
104      * @param polled true if the file descriptor has already been polled
105      */
106     abstract void implDeregister(int fdVal, boolean polled);
107 
108     /**
109      * Poll for events. The {@link #polled(int)} method is invoked for each
110      * polled file descriptor.
111      *
112      * @param timeout if positive then block for up to {@code timeout} milliseconds,
113      *     if zero then don't block, if -1 then block indefinitely
114      * @return the number of file descriptors polled
115      */
116     abstract int poll(int timeout) throws IOException;
117 
118     /**
119      * Callback by the poll method when a file descriptor is polled.
120      */
121     final void polled(int fdVal) {
122         wakeup(fdVal);
123     }
124 
125     /**
126      * Parks the current thread until a file descriptor is ready for the given op.
127      * @param fdVal the file descriptor
128      * @param event POLLIN or POLLOUT
129      * @param nanos the waiting time or 0 to wait indefinitely
130      * @param supplier supplies a boolean to indicate if the enclosing object is open
131      */
132     static void poll(int fdVal, int event, long nanos, BooleanSupplier supplier)
133         throws IOException
134     {
135         assert nanos >= 0L;
136         if (event == Net.POLLIN) {
137             POLLERS.readPoller(fdVal).poll(fdVal, nanos, supplier);
138         } else if (event == Net.POLLOUT) {
139             POLLERS.writePoller(fdVal).poll(fdVal, nanos, supplier);
140         } else {
141             assert false;
142         }
143     }
144 
145     /**
146      * If there is a thread polling the given file descriptor for the given event then
147      * the thread is unparked.
148      */
149     static void stopPoll(int fdVal, int event) {
150         if (event == Net.POLLIN) {
151             POLLERS.readPoller(fdVal).wakeup(fdVal);
152         } else if (event == Net.POLLOUT) {
153             POLLERS.writePoller(fdVal).wakeup(fdVal);
154         } else {
155             throw new IllegalArgumentException();
156         }
157     }
158 
159     /**
160      * If there are any threads polling the given file descriptor then they are unparked.
161      */
162     static void stopPoll(int fdVal) {
163         stopPoll(fdVal, Net.POLLIN);
164         stopPoll(fdVal, Net.POLLOUT);
165     }
166 
167     /**
168      * Parks the current thread until a file descriptor is ready.
169      */
170     private void poll(int fdVal, long nanos, BooleanSupplier supplier) throws IOException {
171         register(fdVal);
172         try {
173             boolean isOpen = supplier.getAsBoolean();
174             if (isOpen) {
175                 if (nanos > 0) {
176                     LockSupport.parkNanos(nanos);
177                 } else {
178                     LockSupport.park();
179                 }
180             }
181         } finally {
182             deregister(fdVal);
183         }
184     }
185 
186     /**
187      * Registers the file descriptor to be polled at most once when the file descriptor
188      * is ready for I/O.
189      */
190     private void register(int fdVal) throws IOException {
191         Thread previous = map.put(fdVal, Thread.currentThread());
192         assert previous == null;
193         implRegister(fdVal);
194     }
195 
196     /**
197      * Deregister the file descriptor so that the file descriptor is not polled.
198      */
199     private void deregister(int fdVal) {
200         Thread previous = map.remove(fdVal);
201         boolean polled = (previous == null);
202         assert polled || previous == Thread.currentThread();
203         implDeregister(fdVal, polled);
204     }
205 
206     /**
207      * Unparks any thread that is polling the given file descriptor.
208      */
209     private void wakeup(int fdVal) {
210         Thread t = map.remove(fdVal);
211         if (t != null) {
212             LockSupport.unpark(t);
213         }
214     }
215 
216     /**
217      * Master polling loop. The {@link #polled(int)} method is invoked for each file
218      * descriptor that is polled.
219      */
220     private void pollerLoop() {
221         try {
222             for (;;) {
223                 poll(-1);
224             }
225         } catch (Exception e) {
226             e.printStackTrace();
227         }
228     }
229 
230     /**
231      * Sub-poller polling loop. The {@link #polled(int)} method is invoked for each file
232      * descriptor that is polled.
233      *
234      * The sub-poller registers its file descriptor with the master poller to park until
235      * there are events to poll. When unparked, it does non-blocking polls and parks
236      * again when there are no more events. The sub-poller yields after each poll to help
237      * with fairness and to avoid re-registering with the master poller where possible.
238      */
239     private void subPollerLoop(Poller masterPoller) {
240         assert Thread.currentThread().isVirtual();
241         try {
242             int polled = 0;
243             for (;;) {
244                 if (polled == 0) {
245                     masterPoller.poll(fdVal(), 0, () -> true);  // park
246                 } else {
247                     Thread.yield();
248                 }
249                 polled = poll(0);
250             }
251         } catch (Exception e) {
252             e.printStackTrace();
253         }
254     }
255 
256     /**
257      * The Pollers used for read and write events.
258      */
259     private static class Pollers {
260         private final PollerProvider provider;
261         private final Poller.Mode pollerMode;
262         private final Poller masterPoller;
263         private final Poller[] readPollers;
264         private final Poller[] writePollers;
265 
266         // used by start method to executor is kept alive
267         private Executor executor;
268 
269         /**
270          * Creates the Poller instances based on configuration.
271          */
272         Pollers() throws IOException {
273             PollerProvider provider = PollerProvider.provider();
274             Poller.Mode mode;
275             String s = GetPropertyAction.privilegedGetProperty("jdk.pollerMode");
276             if (s != null) {
277                 if (s.equalsIgnoreCase(Mode.SYSTEM_THREADS.name()) || s.equals("1")) {
278                     mode = Mode.SYSTEM_THREADS;
279                 } else if (s.equalsIgnoreCase(Mode.VTHREAD_POLLERS.name()) || s.equals("2")) {
280                     mode = Mode.VTHREAD_POLLERS;
281                 } else {
282                     throw new RuntimeException("Can't parse '" + s + "' as polling mode");
283                 }
284             } else {
285                 mode = provider.defaultPollerMode();
286             }
287 
288             // vthread poller mode needs a master poller
289             Poller masterPoller = (mode == Mode.VTHREAD_POLLERS)
290                     ? provider.readPoller(false)
291                     : null;
292 
293             // read pollers (or sub-pollers)
294             int readPollerCount = pollerCount("jdk.readPollers", provider.defaultReadPollers(mode));
295             Poller[] readPollers = new Poller[readPollerCount];
296             for (int i = 0; i < readPollerCount; i++) {
297                 readPollers[i] = provider.readPoller(mode == Mode.VTHREAD_POLLERS);
298             }
299 
300             // write pollers (or sub-pollers)
301             int writePollerCount = pollerCount("jdk.writePollers", provider.defaultWritePollers(mode));
302             Poller[] writePollers = new Poller[writePollerCount];
303             for (int i = 0; i < writePollerCount; i++) {
304                 writePollers[i] = provider.writePoller(mode == Mode.VTHREAD_POLLERS);
305             }
306 
307             this.provider = provider;
308             this.pollerMode = mode;
309             this.masterPoller = masterPoller;
310             this.readPollers = readPollers;
311             this.writePollers = writePollers;
312         }
313 
314         /**
315          * Starts the Poller threads.
316          */
317         void start() {
318             if (pollerMode == Mode.VTHREAD_POLLERS) {
319                 startPlatformThread("MasterPoller", masterPoller::pollerLoop);
320                 ThreadFactory factory = Thread.ofVirtual()
321                         .inheritInheritableThreadLocals(false)
322                         .name("SubPoller-", 0)
323                         .uncaughtExceptionHandler((t, e) -> e.printStackTrace())
324                         .factory();
325                 executor = Executors.newThreadPerTaskExecutor(factory);
326                 Arrays.stream(readPollers).forEach(p -> {
327                     executor.execute(() -> p.subPollerLoop(masterPoller));
328                 });
329                 Arrays.stream(writePollers).forEach(p -> {
330                     executor.execute(() -> p.subPollerLoop(masterPoller));
331                 });
332             } else {
333                 Arrays.stream(readPollers).forEach(p -> {
334                     startPlatformThread("Read-Poller", p::pollerLoop);
335                 });
336                 Arrays.stream(writePollers).forEach(p -> {
337                     startPlatformThread("Write-Poller", p::pollerLoop);
338                 });
339             }
340         }
341 
342         /**
343          * Returns the read poller for the given file descriptor.
344          */
345         Poller readPoller(int fdVal) {
346             int index = provider.fdValToIndex(fdVal, readPollers.length);
347             return readPollers[index];
348         }
349 
350         /**
351          * Returns the write poller for the given file descriptor.
352          */
353         Poller writePoller(int fdVal) {
354             int index = provider.fdValToIndex(fdVal, writePollers.length);
355             return writePollers[index];
356         }
357 
358         /**
359          * Reads the given property name to get the poller count. If the property is
360          * set then the value must be a power of 2. Returns 1 if the property is not
361          * set.
362          * @throws IllegalArgumentException if the property is set to a value that
363          * is not a power of 2.
364          */
365         private static int pollerCount(String propName, int defaultCount) {
366             String s = GetPropertyAction.privilegedGetProperty(propName);
367             int count = (s != null) ? Integer.parseInt(s) : defaultCount;
368 
369             // check power of 2
370             if (count != Integer.highestOneBit(count)) {
371                 String msg = propName + " is set to a value that is not a power of 2";
372                 throw new IllegalArgumentException(msg);
373             }
374             return count;
375         }
376 
377         /**
378          * Starts a platform thread to run the given task.
379          */
380         private void startPlatformThread(String name, Runnable task) {
381             try {
382                 Thread thread = InnocuousThread.newSystemThread(name, task);
383                 thread.setDaemon(true);
384                 thread.setUncaughtExceptionHandler((t, e) -> e.printStackTrace());
385                 thread.start();
386             } catch (Exception e) {
387                 throw new InternalError(e);
388             }
389         }
390     }
391 }