changeset 229:2fdbd9f2813c

Optimize test runner: coalesce hot zeroing loops.
author shade
date Tue, 10 May 2016 14:40:04 +0300
parents c8a4201fd8e9
children f6734c9839bc
files jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java tests-custom/src/main/java/org/openjdk/jcstress/tests/sample/SampleTest.java
diffstat 2 files changed, 99 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Sat May 07 14:58:17 2016 +0300
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Tue May 10 14:40:04 2016 +0300
@@ -378,71 +378,42 @@
         pw.println("            Pair[] pairs = holder.pairs;");
         pw.println("            int len = pairs.length;");
 
-        if (info.getArbiter() != null) {
-            pw.println();
-            pw.println("             for (Pair p : pairs) {");
-            if (info.getState().equals(info.getTest())) {
-                emitMethod(pw, info.getArbiter(), "                p.s." + info.getArbiter().getSimpleName(), "p.s", "p.r", true);
-            } else {
-                emitMethod(pw, info.getArbiter(), "                test." + info.getArbiter().getSimpleName(), "p.s", "p.r", true);
-            }
-            pw.println("            }");
-        }
-        pw.println();
-        pw.println("            for (Pair p : pairs) {");
-        pw.println("                counter.record(p.r);");
-        pw.println("            }");
-        pw.println();
-        pw.println("            int newLen = holder.hasLaggedWorkers ? Math.max(control.minStride, Math.min(len * 2, control.maxStride)) : len;");
         pw.println();
         pw.println("            for (Pair p : pairs) {");
         pw.println("                " + r + " r = p.r;");
+        pw.println("                " + s + " s = p.s;");
+        if (info.getArbiter() != null) {
+            if (info.getState().equals(info.getTest())) {
+                emitMethod(pw, info.getArbiter(), "                s." + info.getArbiter().getSimpleName(), "s", "r", true);
+            } else {
+                emitMethod(pw, info.getArbiter(), "                test." + info.getArbiter().getSimpleName(), "s", "r", true);
+            }
+        }
+        pw.println("                counter.record(r);");
 
         for (VariableElement var : ElementFilter.fieldsIn(info.getResult().getEnclosedElements())) {
             pw.print("                r." + var.getSimpleName().toString() + " = ");
             String type = var.asType().toString();
-            switch (type) {
-                case "int":
-                case "long":
-                case "short":
-                case "byte":
-                case "char":
-                    pw.print("0");
-                    break;
-                case "double":
-                    pw.print("0D");
-                    break;
-                case "float":
-                    pw.print("0F");
-                    break;
-                case "boolean":
-                    pw.print("false");
-                    break;
-                case "java.lang.String":
-                    pw.print("\"\"");
-                    break;
-                default:
-                    throw new GenerationException("Unable to handle @" + Result.class.getSimpleName() + " field of type " + type, var);
-            }
+            pw.print(getDefaultVal(var, type));
             pw.println(";");
         }
 
+        pw.println("                p.s = new " + s + "();");
         pw.println("            }");
         pw.println();
+        pw.println("            int newLen = holder.hasLaggedWorkers ? Math.max(control.minStride, Math.min(len * 2, control.maxStride)) : len;");
+        pw.println();
         pw.println("            Pair[] newPairs = pairs;");
         pw.println("            if (newLen > len) {");
         pw.println("                newPairs = Arrays.copyOf(pairs, newLen);");
         pw.println("                for (int c = len; c < newLen; c++) {");
         pw.println("                    Pair p = new Pair();");
         pw.println("                    p.r = new " + r + "();");
+        pw.println("                    p.s = new " + s + "();");
         pw.println("                    newPairs[c] = p;");
         pw.println("                }");
         pw.println("            }");
         pw.println();
-        pw.println("            for (Pair p : newPairs) {");
-        pw.println("                p.s = new " + s + "();");
-        pw.println("            }");
-        pw.println();
         pw.println("            version.set(new StateHolder<>(control.isStopped, newPairs, " + actorsCount + "));");
         pw.println("        }");
 
@@ -503,6 +474,34 @@
         pw.close();
     }
 
+    private String getDefaultVal(VariableElement var, String type) {
+        String val;
+        switch (type) {
+            case "int":
+            case "long":
+            case "short":
+            case "byte":
+            case "char":
+                val = "0";
+                break;
+            case "double":
+                val = "0D";
+                break;
+            case "float":
+                val = "0F";
+                break;
+            case "boolean":
+                val = "false";
+                break;
+            case "java.lang.String":
+                val = "\"\"";
+                break;
+            default:
+                throw new GenerationException("Unable to handle @" + Result.class.getSimpleName() + " field of type " + type, var);
+        }
+        return val;
+    }
+
     private void generateTermination(TestInfo info) {
         if (info.getSignal() == null) {
             throw new GenerationException("@" + JCStressTest.class.getSimpleName() + " with mode=" + Mode.Termination +
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests-custom/src/main/java/org/openjdk/jcstress/tests/sample/SampleTest.java	Tue May 10 14:40:04 2016 +0300
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jcstress.tests.sample;
+
+import org.openjdk.jcstress.annotations.*;
+import org.openjdk.jcstress.infra.results.IntResult4;
+
+import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
+
+@JCStressTest
+@Description("Sample test")
+@Outcome(expect = ACCEPTABLE,  desc = "All acceptable")
+@State
+public class SampleTest {
+
+    public int a;
+    public int b;
+
+    @Actor
+    public void actor1(IntResult4 r) {
+        r.r1 = b;
+    }
+
+    @Actor
+    public void actor2(IntResult4 r) {
+        r.r2 = a;
+    }
+
+    @Arbiter
+    public void arbiter(IntResult4 r) {
+        r.r3 = a;
+        r.r4 = b;
+    }
+
+}