1 /*
   2  * Copyright (c) 2018, 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 package org.openjdk.benchmarks.cont;
  25 
  26 import java.util.concurrent.TimeUnit;
  27 import org.openjdk.jmh.annotations.*;
  28 
  29 @BenchmarkMode(Mode.AverageTime)
  30 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  31 @State(Scope.Thread)
  32 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  33 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  34 @Fork(1)
  35 public class OneShot {
  36     static final ContinuationScope SCOPE = new ContinuationScope() { };
  37 
  38     static class Arg {
  39         volatile int field;
  40     }
  41 
  42     /**
  43      * A recursive task that optionally yields when the stack gets to a specific
  44      * depth. If continued after yielding, it runs to completion.
  45      */
  46     static class Yielder implements Runnable {
  47         private final int paramCount;
  48         private final int maxDepth;
  49         private final boolean yieldAtLimit;
  50 
  51         private Yielder(int paramCount, int maxDepth, boolean yieldAtLimit) {
  52             if (paramCount < 1 || paramCount > 3)
  53                 throw new IllegalArgumentException();
  54             this.paramCount = paramCount;
  55             this.maxDepth = maxDepth;
  56             this.yieldAtLimit = yieldAtLimit;
  57         }
  58 
  59         @Override
  60         public void run() {
  61             switch (paramCount) {
  62                 case 1: run1(maxDepth); break;
  63                 case 2: run2(maxDepth, new Arg()); break;
  64                 case 3: run3(maxDepth, new Arg(), new Arg()); break;
  65                 default: throw new Error("should not happen");
  66             }
  67         }
  68 
  69         private void run1(int depth) {
  70             if (depth > 0) {
  71                 run1(depth - 1);
  72             } if (depth == 0) {
  73                 if (yieldAtLimit) Continuation.yield(SCOPE);
  74             }
  75         }
  76 
  77         private void run2(int depth, Arg arg2) {
  78             if (depth > 0) {
  79                 run2(depth - 1, arg2);
  80             } if (depth == 0) {
  81                 if (yieldAtLimit) Continuation.yield(SCOPE);
  82             } else {
  83                 // never executed
  84                 arg2.field = 0;
  85             }
  86         }
  87 
  88         private void run3(int depth, Arg arg2, Arg arg3) {
  89             if (depth > 0) {
  90                 run3(depth - 1, arg2, arg3);
  91             } if (depth == 0) {
  92                 if (yieldAtLimit) Continuation.yield(SCOPE);
  93             } else {
  94                 // never executed
  95                 arg2.field = 0;
  96                 arg3.field = 0;
  97             }
  98         }
  99 
 100         static Continuation continuation(int paramCount, int maxDepth,
 101                                          boolean yieldAtLimit) {
 102             Runnable task = new Yielder(paramCount, maxDepth, yieldAtLimit);
 103             return new Continuation(SCOPE, 2000, task);
 104         }
 105     }
 106 
 107     /**
 108      * A recursive task that optionally yields before and/or after each call.
 109      */
 110     static class Stepper implements Runnable {
 111         private final int paramCount;
 112         private final int maxDepth;
 113         private final boolean yieldBefore;
 114         private final boolean yieldAfter;
 115 
 116         private Stepper(int paramCount,
 117                         int maxDepth,
 118                         boolean yieldBefore,
 119                         boolean yieldAfter) {
 120             this.paramCount = paramCount;
 121             this.maxDepth = maxDepth;
 122             this.yieldBefore = yieldBefore;
 123             this.yieldAfter = yieldAfter;
 124         }
 125 
 126         public void run() {
 127             switch (paramCount) {
 128                 case 1: run1(maxDepth); break;
 129                 case 2: run2(maxDepth, new Arg()); break;
 130                 case 3: run3(maxDepth, new Arg(), new Arg()); break;
 131                 default: throw new Error("should not happen");
 132             }
 133         }
 134 
 135         private void run1(int depth) {
 136             if (depth > 0) {
 137                 if (yieldBefore) Continuation.yield(SCOPE);
 138                 run1(depth - 1);
 139                 if (yieldAfter) Continuation.yield(SCOPE);
 140             }
 141         }
 142 
 143         private void run2(int depth, Arg arg2) {
 144             if (depth > 0) {
 145                 if (yieldBefore) Continuation.yield(SCOPE);
 146                 run2(depth - 1, arg2);
 147                 if (yieldAfter) Continuation.yield(SCOPE);
 148             } else if (depth < 0) {
 149                 // never executed
 150                 arg2.field = 0;
 151             }
 152         }
 153 
 154         private void run3(int depth, Arg arg2, Arg arg3) {
 155             if (depth > 0) {
 156                 if (yieldBefore) Continuation.yield(SCOPE);
 157                 run3(depth - 1, arg2, arg3);
 158                 if (yieldAfter) Continuation.yield(SCOPE);
 159             } else if (depth < 0) {
 160                 // never executed
 161                 arg2.field = 0;
 162                 arg3.field = 0;
 163             }
 164         }
 165 
 166         static Continuation continuation(int paramCount, int maxDepth,
 167                                          boolean yieldBefore, boolean yieldAfter) {
 168             Runnable task = new Stepper(paramCount, maxDepth, yieldBefore, yieldAfter);
 169             return new Continuation(SCOPE, task);
 170         }
 171     }
 172 
 173     @Param({"1", "2", "3"})
 174     public int paramCount;
 175 
 176     @Param({"5", "10", "20", "100"})
 177     public int stackDepth;
 178 
 179     /**
 180      * Creates and run continuation that does not yield.
 181      */
 182     @Benchmark
 183     public void noYield() {
 184         Continuation cont = Yielder.continuation(paramCount, stackDepth, false);
 185         cont.run();
 186         if (!cont.isDone())
 187             throw new RuntimeException("continuation not done???");
 188     }
 189 
 190     /**
 191      * Creates and runs a continuation that yields at a given stack depth.
 192      */
 193     @Benchmark
 194     public void yield() {
 195         Continuation cont = Yielder.continuation(paramCount, stackDepth, true);
 196         cont.run();
 197         if (cont.isDone())
 198             throw new RuntimeException("continuation done???");
 199     }
 200 
 201     /**
 202      * Creates and runs a continuation that yields at a given stack depth, it is
 203      * then continued to run to completion.
 204      */
 205     @Benchmark
 206     public void yieldThenContinue() {
 207         Continuation cont = Yielder.continuation(paramCount, stackDepth, true);
 208         cont.run();
 209         cont.run();  // continue
 210         if (!cont.isDone())
 211             throw new RuntimeException("continuation not done???");
 212     }
 213 
 214     /**
 215      * Creates and runs a continuation to the given stack depth, yielding before
 216      * each call.
 217      */
 218     @Benchmark
 219     public void yieldBeforeEachCall() {
 220         Continuation cont = Stepper.continuation(paramCount, stackDepth, true, false);
 221         while (!cont.isDone()) {
 222             cont.run();
 223         }
 224     }
 225 
 226     /**
 227      * Creates and runs a continuation to the given stack depth, yielding after
 228      * each call.
 229      */
 230     @Benchmark
 231     public void yieldAfterEachCall() {
 232         Continuation cont = Stepper.continuation(paramCount, stackDepth, false, true);
 233         while (!cont.isDone()) {
 234             cont.run();
 235         }
 236     }
 237 
 238     /**
 239      * Creates and runs a continuation to the given stack depth, yielding before
 240      * and after each call.
 241      */
 242     @Benchmark
 243     public void yieldBeforeAndAfterEachCall() {
 244         Continuation cont = Stepper.continuation(paramCount, stackDepth, true, true);
 245         while (!cont.isDone()) {
 246             cont.run();
 247         }
 248     }
 249 }