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 Freeze {
  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     @Param({"1", "2", "3"})
 108     public int paramCount;
 109 
 110     @Param({"5", "10", "20", "100"})
 111     public int stackDepth;
 112 
 113     Continuation cont;
 114 
 115     @Setup(Level.Iteration)
 116     public void setup() {
 117         // System.out.println("pc = " + paramCount + " sd = " + stackDepth);
 118         cont = Yielder.continuation(paramCount, stackDepth, true);
 119     }
 120 
 121     /**
 122      * Creates and runs a continuation that yields at a given stack depth.
 123      */
 124     @Benchmark
 125     public void justYield() {
 126         cont.run();
 127         
 128         cont.something_something_1();
 129     }
 130 }