changeset 80:ec966433b6be

Yak shaving: move some shared fields out of Runner to ControlHolder.
author shade
date Wed, 12 Mar 2014 23:57:52 +0400
parents fe3db9567f0d
children 4366e13c0e2a
files jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor1_Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Arbiter1_Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor3_Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor4_Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/ControlHolder.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Runner.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/TerminationRunner.java
diffstat 8 files changed, 83 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor1_Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor1_Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -90,16 +90,16 @@
         }
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            Counter<R> runResult = run(time);
+            Counter<R> runResult = internalRun();
 
             dump(testName, runResult);
         }
@@ -112,13 +112,10 @@
     }
 
 
-    private Counter<R> run(int time) {
-
+    private Counter<R> internalRun() {
         @SuppressWarnings("unchecked")
         final S[] poison = (S[]) new Object[0];
 
-        ControlHolder controlHolder = new ControlHolder(minStride, maxStride, shouldYield);
-
         Collection<Future<?>> tasks = new ArrayList<Future<?>>();
 
         final AtomicReference<StateHolder<S, R>> version = new AtomicReference<StateHolder<S, R>>();
@@ -127,14 +124,14 @@
         final Counter<R> counter = Counters.newCounter((Class<R>) test.newResult().getClass());
 
         @SuppressWarnings("unchecked")
-        S[] newStride = (S[]) new Object[minStride];
-        for (int c = 0; c < minStride; c++) {
+        S[] newStride = (S[]) new Object[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newStride[c] = test.newState();
         }
 
         @SuppressWarnings("unchecked")
-        R[] newResult = (R[]) new Result[minStride];
-        for (int c = 0; c < minStride; c++) {
+        R[] newResult = (R[]) new Result[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newResult[c] = test.newResult();
         }
 
@@ -144,7 +141,7 @@
         AtomicInteger epoch = new AtomicInteger();
 
         tasks.add(pool.submit(
-                new ActorBase<Actor1_Test<S, R>, S, R>(1, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor1_Test<S, R>, S, R>(1, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work1(Actor1_Test<S, R> test, S state, R result) {
                         test.actor1(state, result);
@@ -153,12 +150,12 @@
         ));
 
         try {
-            TimeUnit.MILLISECONDS.sleep(time);
+            TimeUnit.MILLISECONDS.sleep(control.time);
         } catch (InterruptedException e) {
             // do nothing
         }
 
-        controlHolder.isStopped = true;
+        control.isStopped = true;
 
         waitFor(testName, tasks);
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Arbiter1_Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Arbiter1_Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -88,16 +88,16 @@
         }
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            Counter<R> runResult = run(time);
+            Counter<R> runResult = internalRun();
 
             dump(testName, runResult);
         }
@@ -110,12 +110,10 @@
         return 3;
     }
 
-    public Counter<R> run(int time) {
+    public Counter<R> internalRun() {
         @SuppressWarnings("unchecked")
         final S[] poison = (S[]) new Object[0];
 
-        ControlHolder controlHolder = new ControlHolder(minStride, maxStride, shouldYield);
-
         Collection<Future<?>> tasks = new ArrayList<Future<?>>();
 
         final AtomicReference<StateHolder<S, R>> version = new AtomicReference<StateHolder<S, R>>();
@@ -124,14 +122,14 @@
         final Counter<R> counter = Counters.newCounter((Class<R>) test.newResult().getClass());
 
         @SuppressWarnings("unchecked")
-        S[] newStride = (S[]) new Object[minStride];
-        for (int c = 0; c < minStride; c++) {
+        S[] newStride = (S[]) new Object[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newStride[c] = test.newState();
         }
 
         @SuppressWarnings("unchecked")
-        R[] newResult = (R[]) new Result[minStride];
-        for (int c = 0; c < minStride; c++) {
+        R[] newResult = (R[]) new Result[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newResult[c] = test.newResult();
         }
 
@@ -141,7 +139,7 @@
         AtomicInteger epoch = new AtomicInteger();
 
         Future<?> a1 = pool.submit(
-                new ActorBase<Actor2_Arbiter1_Test<S, R>, S, R>(1, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor2_Arbiter1_Test<S, R>, S, R>(1, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work1(Actor2_Arbiter1_Test<S, R> test, S state, R result) {
                         test.actor1(state, result);
@@ -156,7 +154,7 @@
         tasks.add(a1);
 
         Future<?> a2 = pool.submit(
-                new ActorBase<Actor2_Arbiter1_Test<S, R>, S, R>(2, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor2_Arbiter1_Test<S, R>, S, R>(2, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work2(Actor2_Arbiter1_Test<S, R> test, S state, R result) {
                         test.actor2(state, result);
@@ -171,12 +169,12 @@
         tasks.add(a2);
 
         try {
-            TimeUnit.MILLISECONDS.sleep(time);
+            TimeUnit.MILLISECONDS.sleep(control.time);
         } catch (InterruptedException e) {
             // do nothing
         }
 
-        controlHolder.isStopped = true;
+        control.isStopped = true;
 
         waitFor(testName, tasks);
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor2_Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -94,16 +94,16 @@
         }
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            Counter<R> runResult = run(time);
+            Counter<R> runResult = run(control.time);
 
             dump(testName, runResult);
         }
@@ -120,8 +120,6 @@
         @SuppressWarnings("unchecked")
         final S[] poison = (S[]) new Object[0];
 
-        ControlHolder controlHolder = new ControlHolder(minStride, maxStride, shouldYield);
-
         Collection<Future<?>> tasks = new ArrayList<Future<?>>();
 
         final AtomicReference<StateHolder<S, R>> version = new AtomicReference<StateHolder<S, R>>();
@@ -130,14 +128,14 @@
         final Counter<R> counter = Counters.newCounter((Class<R>) test.newResult().getClass());
 
         @SuppressWarnings("unchecked")
-        S[] newStride = (S[]) new Object[minStride];
-        for (int c = 0; c < minStride; c++) {
+        S[] newStride = (S[]) new Object[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newStride[c] = test.newState();
         }
 
         @SuppressWarnings("unchecked")
-        R[] newResult = (R[]) new Result[minStride];
-        for (int c = 0; c < minStride; c++) {
+        R[] newResult = (R[]) new Result[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newResult[c] = test.newResult();
         }
 
@@ -147,7 +145,7 @@
         AtomicInteger epoch = new AtomicInteger();
 
         tasks.add(pool.submit(
-                new ActorBase<Actor2_Test<S, R>, S, R>(1, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor2_Test<S, R>, S, R>(1, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work1(Actor2_Test<S, R> test, S state, R result) {
                         test.actor1(state, result);
@@ -156,7 +154,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor2_Test<S, R>, S, R>(2, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor2_Test<S, R>, S, R>(2, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work2(Actor2_Test<S, R> test, S state, R result) {
                         test.actor2(state, result);
@@ -170,7 +168,7 @@
             // do nothing
         }
 
-        controlHolder.isStopped = true;
+        control.isStopped = true;
 
         waitFor(testName, tasks);
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor3_Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor3_Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -94,16 +94,16 @@
         }
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            Counter<R> runResult = run(time);
+            Counter<R> runResult = internalRun();
 
             dump(testName, runResult);
         }
@@ -115,13 +115,11 @@
         return 3;
     }
 
-    private Counter<R> run(int time) {
+    private Counter<R> internalRun() {
 
         @SuppressWarnings("unchecked")
         final S[] poison = (S[]) new Object[0];
 
-        ControlHolder controlHolder = new ControlHolder(minStride, maxStride, shouldYield);
-
         Collection<Future<?>> tasks = new ArrayList<Future<?>>();
 
         final AtomicReference<StateHolder<S, R>> version = new AtomicReference<StateHolder<S, R>>();
@@ -130,14 +128,14 @@
         final Counter<R> counter = Counters.newCounter((Class<R>) test.newResult().getClass());
 
         @SuppressWarnings("unchecked")
-        S[] newStride = (S[]) new Object[minStride];
-        for (int c = 0; c < minStride; c++) {
+        S[] newStride = (S[]) new Object[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newStride[c] = test.newState();
         }
 
         @SuppressWarnings("unchecked")
-        R[] newResult = (R[]) new Result[minStride];
-        for (int c = 0; c < minStride; c++) {
+        R[] newResult = (R[]) new Result[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newResult[c] = test.newResult();
         }
 
@@ -147,7 +145,7 @@
         AtomicInteger epoch = new AtomicInteger();
 
         tasks.add(pool.submit(
-                new ActorBase<Actor3_Test<S, R>, S, R>(1, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor3_Test<S, R>, S, R>(1, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work1(Actor3_Test<S, R> test, S state, R result) {
                         test.actor1(state, result);
@@ -156,7 +154,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor3_Test<S, R>, S, R>(2, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor3_Test<S, R>, S, R>(2, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work2(Actor3_Test<S, R> test, S state, R result) {
                         test.actor2(state, result);
@@ -165,7 +163,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor3_Test<S, R>, S, R>(3, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor3_Test<S, R>, S, R>(3, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work3(Actor3_Test<S, R> test, S state, R result) {
                         test.actor3(state, result);
@@ -174,12 +172,12 @@
         ));
 
         try {
-            TimeUnit.MILLISECONDS.sleep(time);
+            TimeUnit.MILLISECONDS.sleep(control.time);
         } catch (InterruptedException e) {
             // do nothing
         }
 
-        controlHolder.isStopped = true;
+        control.isStopped = true;
 
         waitFor(testName, tasks);
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor4_Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Actor4_Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -96,16 +96,16 @@
         }
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            Counter<R> runResult = run(time);
+            Counter<R> runResult = internalRun();
 
             dump(testName, runResult);
         }
@@ -117,13 +117,11 @@
         return 4;
     }
 
-    private Counter<R> run(int time) {
+    private Counter<R> internalRun() {
 
         @SuppressWarnings("unchecked")
         final S[] poison = (S[]) new Object[0];
 
-        ControlHolder controlHolder = new ControlHolder(minStride, maxStride, shouldYield);
-
         Collection<Future<?>> tasks = new ArrayList<Future<?>>();
 
         final AtomicReference<StateHolder<S, R>> version = new AtomicReference<StateHolder<S, R>>();
@@ -132,14 +130,14 @@
         final Counter<R> counter = Counters.newCounter((Class<R>) test.newResult().getClass());
 
         @SuppressWarnings("unchecked")
-        S[] newStride = (S[]) new Object[minStride];
-        for (int c = 0; c < minStride; c++) {
+        S[] newStride = (S[]) new Object[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newStride[c] = test.newState();
         }
 
         @SuppressWarnings("unchecked")
-        R[] newResult = (R[]) new Result[minStride];
-        for (int c = 0; c < minStride; c++) {
+        R[] newResult = (R[]) new Result[control.minStride];
+        for (int c = 0; c < control.minStride; c++) {
             newResult[c] = test.newResult();
         }
 
@@ -149,7 +147,7 @@
         AtomicInteger epoch = new AtomicInteger();
 
         tasks.add(pool.submit(
-                new ActorBase<Actor4_Test<S, R>, S, R>(1, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor4_Test<S, R>, S, R>(1, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work1(Actor4_Test<S, R> test, S state, R result) {
                         test.actor1(state, result);
@@ -158,7 +156,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor4_Test<S, R>, S, R>(2, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor4_Test<S, R>, S, R>(2, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work2(Actor4_Test<S, R> test, S state, R result) {
                         test.actor2(state, result);
@@ -167,7 +165,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor4_Test<S, R>, S, R>(3, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor4_Test<S, R>, S, R>(3, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work3(Actor4_Test<S, R> test, S state, R result) {
                         test.actor3(state, result);
@@ -176,7 +174,7 @@
         ));
 
         tasks.add(pool.submit(
-                new ActorBase<Actor4_Test<S, R>, S, R>(4, test, version, epoch, counter, controlHolder, poison) {
+                new ActorBase<Actor4_Test<S, R>, S, R>(4, test, version, epoch, counter, control, poison) {
                     @Override
                     protected void work4(Actor4_Test<S, R> test, S state, R result) {
                         test.actor4(state, result);
@@ -185,12 +183,12 @@
         ));
 
         try {
-            TimeUnit.MILLISECONDS.sleep(time);
+            TimeUnit.MILLISECONDS.sleep(control.time);
         } catch (InterruptedException e) {
             // do nothing
         }
 
-        controlHolder.isStopped = true;
+        control.isStopped = true;
 
         waitFor(testName, tasks);
 
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/ControlHolder.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/ControlHolder.java	Wed Mar 12 23:57:52 2014 +0400
@@ -24,18 +24,28 @@
  */
 package org.openjdk.jcstress.infra.runners;
 
+import org.openjdk.jcstress.Options;
+
 /**
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
 public class ControlHolder {
     volatile boolean isStopped;
     final boolean shouldYield;
+    final boolean verbose;
     final int minStride;
     final int maxStride;
+    final int time;
+    final int iters;
+    final int deoptRatio;
 
-    public ControlHolder(int minStride, int maxStride, boolean shouldYield) {
-        this.shouldYield = shouldYield;
-        this.minStride = minStride;
-        this.maxStride = maxStride;
+    public ControlHolder(Options opts) {
+        time = opts.getTime();
+        minStride = opts.getMinStride();
+        maxStride = opts.getMaxStride();
+        iters = opts.getIterations();
+        shouldYield = opts.shouldYield();
+        verbose = opts.isVerbose();
+        deoptRatio = opts.deoptRatio();
     }
 }
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Runner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/Runner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -50,32 +50,17 @@
  * @author Aleksey Shipilev (aleksey.shipilev@oracle.com)
  */
 public abstract class Runner {
-    protected final int time;
-    protected final int iters;
-    protected final int minStride, maxStride;
-    protected final int deoptRatio;
-    protected final boolean verbose;
+    protected final ControlHolder control;
     protected final TestResultCollector collector;
     protected final ExecutorService pool;
-    protected volatile boolean shouldYield;
-
-    protected volatile boolean isStopped;
-
     protected final PrintWriter testLog;
 
     public Runner(Options opts, TestResultCollector collector, ExecutorService pool) throws FileNotFoundException, JAXBException {
         this.collector = collector;
         this.pool = pool;
+        this.control = new ControlHolder(opts);
 
-        time = opts.getTime();
-        minStride = opts.getMinStride();
-        maxStride = opts.getMaxStride();
-        iters = opts.getIterations();
-        shouldYield = opts.shouldYield();
-        verbose = opts.isVerbose();
-        deoptRatio = opts.deoptRatio();
-
-        if (verbose) {
+        if (control.verbose) {
             testLog = new PrintWriter(System.out, true);
         } else {
             testLog = new PrintWriter(new NullOutputStream(), true);
@@ -163,7 +148,7 @@
                 }
             }
 
-            if (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) > Math.max(time, 60*1000)) {
+            if (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) > Math.max(control.time, 60*1000)) {
                 dumpFailure(testName, Status.TIMEOUT_ERROR);
                 return;
             }
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/TerminationRunner.java	Wed Mar 12 23:42:12 2014 +0400
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/TerminationRunner.java	Wed Mar 12 23:57:52 2014 +0400
@@ -59,16 +59,16 @@
         HashCounter<Outcome> results = new HashCounter<Outcome>();
 
         testLog.print("Iterations ");
-        for (int c = 0; c < iters; c++) {
+        for (int c = 0; c < control.iters; c++) {
             try {
-                VMSupport.tryDeoptimizeAllInfra(deoptRatio);
+                VMSupport.tryDeoptimizeAllInfra(control.deoptRatio);
             } catch (NoClassDefFoundError err) {
                 // gracefully "handle"
             }
 
             testLog.print(".");
             testLog.flush();
-            run(time, results);
+            run(results);
 
             dump(testName, results);
 
@@ -97,8 +97,8 @@
         ERROR,
     }
 
-    private void run(int time, Counter<Outcome> results) {
-        long target = System.currentTimeMillis() + time;
+    private void run(Counter<Outcome> results) {
+        long target = System.currentTimeMillis() + control.time;
         while (System.currentTimeMillis() < target) {
 
             final Holder<S> holder = new Holder<S>();