Tencent / TencentKona-8

Tencent Kona is a no-cost, production-ready distribution of the Open Java Development Kit (OpenJDK), Long-term support(LTS) with quarterly updates. Tencent Kona serves as the default JDK internally at Tencent Cloud for cloud computing and other Java applications.
Other
938 stars 146 forks source link

Transplant jmh test Oscillation.java from Loom #118

Closed shuyuHU328 closed 1 year ago

shuyuHU328 commented 1 year ago

Main changes

  1. Create a standalone Maven project to meet the needs of JMH
  2. Remove the import of jdk.internal.vm.Continuation, jdk.internal.vm.ContinuationScope, which is no need for KonaFiber
  3. Add a new method terminateNow() in class Oscillation
  4. Modified the method run(int depth) to prevent JVM crashs

Detailed diff output

diff --git a/demo/fiber/oscillation_test/src/main/java/org/example/Oscillation.java b/demo/fiber/oscillation_test/src/main/java/org/example/Oscillation.java
index d79d0a5fb7..6dc175f739 100644
--- a/demo/fiber/oscillation_test/src/main/java/org/example/Oscillation.java
+++ b/demo/fiber/oscillation_test/src/main/java/org/example/Oscillation.java
@@ -37,7 +37,8 @@ import java.util.concurrent.TimeUnit;
 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
 @Fork(1)
 public class Oscillation {
-    static final ContinuationScope SCOPE = new ContinuationScope(){};
+    static final ContinuationScope SCOPE = new ContinuationScope() {
+    };

     /**
      * A task that oscillates between a minimum and maximum stack depth, yielding
@@ -47,8 +48,10 @@ public class Oscillation {
         private final int min;
         private final int max;

-        private enum Mode { GROW, SHRINK }
+        private enum Mode {GROW, SHRINK}
+
         private Mode mode;
+        private boolean terminate;

         Wave(int min, int max) {
             if (min < 0)
@@ -57,6 +60,7 @@ public class Oscillation {
                 throw new IllegalArgumentException("max must be greater than min");
             this.min = min;
             this.max = max;
+            this.terminate = false;
             this.mode = Mode.GROW;
         }

@@ -68,6 +72,9 @@ public class Oscillation {
                 while (true) {
                     mode = Mode.GROW;
                     run(depth + 1);
+                    if (this.terminate) {
+                        break;
+                    }
                 }
             } else if (mode == Mode.GROW) {
                 run(depth + 1);
@@ -78,6 +85,10 @@ public class Oscillation {
         public void run() {
             run(0);
         }
+
+        public void terminateNow() {
+            this.terminate = true;
+        }
     }

     @Param({"2", "3", "4"})
@@ -86,20 +97,23 @@ public class Oscillation {
     @Param({"5", "6", "7", "8"})
     public int maxDepth;

-    @Param({"10", "100","1000"})
+    @Param({"10", "100", "1000"})
     public int repeat;

     /**
      * Creates and runs a continuation that oscillates between a minimum and
      * maximum stack depth, yielding at the maximum stack until continued.
-     *
+     * <p>
      * Useful to measure freeze and thaw, also to compare full stack vs. lazy copy.
      */
     @Benchmark
     public void oscillate() {
-        Continuation cont = new Continuation(SCOPE, new Wave(minDepth, maxDepth));
-        for (int i=0; i<repeat; i++) {
+        Wave wave = new Wave(minDepth, maxDepth);
+        Continuation cont = new Continuation(SCOPE, wave);
+        for (int i = 0; i < repeat; i++) {
             cont.run();
         }
+        wave.terminateNow();
+        cont.run();
     }
 }
tencent-adm commented 1 year ago

CLA assistant check
All committers have signed the CLA.