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