1 /*
  2  * Copyright (c) 2022, 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.
  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  * @summary Stress test virtual threads with a variation of the Skynet 1M benchmark
 27  * @requires vm.continuations
 28  * @run main/othervm/timeout=300 -Xmx1400m Skynet
 29  */
 30 
 31 /*
 32  * @test
 33  * @requires vm.debug == true & vm.continuations
 34  * @requires vm.gc.Z
 35  * @run main/othervm/timeout=300 -XX:+UnlockDiagnosticVMOptions
 36  *     -XX:+ZVerifyOops -XX:ZCollectionInterval=0.01 -Xmx1400m Skynet
 37  */
 38 
 39 import java.util.concurrent.BlockingQueue;
 40 import java.util.concurrent.SynchronousQueue;
 41 import java.util.concurrent.ThreadFactory;
 42 
 43 public class Skynet {
 44     public static void main(String[] args) {
 45         int iterations = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
 46         for (int i = 0; i < iterations; i++) {
 47             skynet(1_000_000, 499999500000L);
 48         }
 49     }
 50 
 51     static void skynet(int num, long expected) {
 52         long start = System.currentTimeMillis();
 53         var chan = new Channel<Long>();
 54 
 55         Thread.startVirtualThread(() -> skynet(chan, 0, num, 10));
 56 
 57         long sum = chan.receive();
 58         long end = System.currentTimeMillis();
 59         System.out.format("Result: %d in %s ms%n", sum, (end-start));
 60         if (sum != expected)
 61             throw new AssertionError("unexpected result!");
 62     }
 63 
 64     static void skynet(Channel<Long> result, int num, int size, int div) {
 65         if (size == 1) {
 66             result.send((long)num);
 67         } else {
 68             var chan = new Channel<Long>();
 69             for (int i = 0; i < div; i++) {
 70                 int subNum = num + i * (size / div);
 71                 Thread.startVirtualThread(() -> skynet(chan, subNum, size / div, div));
 72             }
 73             long sum = 0;
 74             for (int i = 0; i < div; i++) {
 75                 sum += chan.receive();
 76             }
 77             result.send(sum);
 78         }
 79     }
 80 
 81     static class Channel<T> {
 82         private final BlockingQueue<T> q;
 83 
 84         Channel() {
 85             q = new SynchronousQueue<>();
 86         }
 87 
 88         void send(T e) {
 89             boolean interrupted = false;
 90             while (true) {
 91                 try {
 92                     q.put(e);
 93                     break;
 94                 } catch (InterruptedException x) {
 95                     interrupted = true;
 96                 }
 97             }
 98             if (interrupted)
 99                 Thread.currentThread().interrupt();
100         }
101 
102         T receive() {
103             boolean interrupted = false;
104             T e;
105             while (true) {
106                 try {
107                     e = q.take();
108                     break;
109                 } catch (InterruptedException x) {
110                     interrupted = true;
111                 }
112             }
113             if (interrupted)
114                 Thread.currentThread().interrupt();
115             return e;
116         }
117     }
118 }