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 /**
  30  *  java -XX:+UnlockDiagnosticVMOptions -XX:+UseNewCode -XX:-DetectLocksInCompiledFrames \
  31  *    -jar benchmarks.jar oscillate -p minDepth=5 -p maxDepth=10 -p repeat=100
  32  */
  33 
  34 @BenchmarkMode(Mode.AverageTime)
  35 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  36 @State(Scope.Benchmark)
  37 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  38 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  39 @Fork(1)
  40 public class Oscillation {
  41     static final ContinuationScope SCOPE = new ContinuationScope() { };
  42 
  43     /**
  44      * A task that oscillates between a minimum and maximum stack depth, yielding
  45      * at the maximum stack until continued.
  46      */
  47     static class Wave implements Runnable {
  48         private final int min;
  49         private final int max;
  50 
  51         private enum Mode { GROW, SHRINK }
  52         private Mode mode;
  53 
  54         Wave(int min, int max) {
  55             if (min < 0)
  56                 throw new IllegalArgumentException("negative min");
  57             if (max <= min)
  58                 throw new IllegalArgumentException("max must be greater than min");
  59             this.min = min;
  60             this.max = max;
  61             this.mode = Mode.GROW;
  62         }
  63 
  64         private void run(int depth) {
  65             if (depth == max) {
  66                 Continuation.yield(SCOPE);
  67                 mode = Mode.SHRINK;
  68             } else if (depth == min) {
  69                 while (true) {
  70                     mode = Mode.GROW;
  71                     run(depth + 1);
  72                 }
  73             } else if (mode == Mode.GROW) {
  74                 run(depth + 1);
  75             }
  76         }
  77 
  78         @Override
  79         public void run() {
  80             run(0);
  81         }
  82     }
  83 
  84     @Param({"2", "3", "4"})
  85     public int minDepth;
  86 
  87     @Param({"5", "6", "7", "8"})
  88     public int maxDepth;
  89 
  90     @Param({"10", "100", "1000"})
  91     public int repeat;
  92 
  93     /**
  94      * Creates and runs a continuation that oscillates between a minimum and
  95      * maximum stack depth, yielding at the maximum stack until continued.
  96      *
  97      * Useful to measure freeze and thaw, also to compare full stack vs. lazy copy.
  98      */
  99     @Benchmark
 100     public void oscillate() {
 101         Continuation cont = new Continuation(SCOPE, new Wave(minDepth, maxDepth));
 102         for (int i=0; i<repeat; i++) {
 103             cont.run();
 104         }
 105     }
 106 }