changeset 4:8b63b057274d

generator: extract ResultGenerator.
author shade
date Mon, 08 Jul 2013 17:41:48 +0400
parents 9c120c32c0cd
children b064dcacc1f0
files generator/src/main/java/org/openjdk/jcstress/TraceGenMain.java generator/src/main/java/org/openjdk/jcstress/generator/ResultGenerator.java generator/src/main/java/org/openjdk/jcstress/generator/TestGenerator.java generator/src/main/java/org/openjdk/jcstress/generator/Utils.java generator/src/main/java/org/openjdk/jcstress/tracer/TraceGen.java
diffstat 5 files changed, 226 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/generator/src/main/java/org/openjdk/jcstress/TraceGenMain.java	Mon Jul 08 17:33:08 2013 +0400
+++ b/generator/src/main/java/org/openjdk/jcstress/TraceGenMain.java	Mon Jul 08 17:41:48 2013 +0400
@@ -24,6 +24,7 @@
  */
 package org.openjdk.jcstress;
 
+import org.openjdk.jcstress.generator.TestGenerator;
 import org.openjdk.jcstress.tracer.TraceGen;
 
 import java.io.FileNotFoundException;
@@ -31,7 +32,11 @@
 public class TraceGenMain {
 
     public static void main(String[] args) throws FileNotFoundException {
-        new TraceGen(2).generate();
+        if (args.length >= 2) {
+            new TraceGen(2, args[0], args[1]).generate();
+        } else {
+            throw new IllegalStateException("Please provide the destination dir");
+        }
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/generator/src/main/java/org/openjdk/jcstress/generator/ResultGenerator.java	Mon Jul 08 17:41:48 2013 +0400
@@ -0,0 +1,160 @@
+/*
+ * 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.generator;
+
+import java.io.FileNotFoundException;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.Set;
+
+public class ResultGenerator {
+    private final Set<String> generatedResults = new HashSet<String>();
+    private final String srcRoot;
+
+    public ResultGenerator(String srcRoot) {
+        this.srcRoot = srcRoot;
+    }
+
+    public String generateResult(TestGenerator.Types types) {
+        String name = "";
+        for (Class k : types.all()) {
+            if (k == boolean.class) name += "X";
+            if (k == byte.class) name += "B";
+            if (k == short.class) name += "S";
+            if (k == char.class) name += "C";
+            if (k == int.class) name += "I";
+            if (k == long.class) name += "L";
+            if (k == float.class) name += "F";
+            if (k == double.class) name += "D";
+        }
+        name += "_Result";
+
+        // already generated
+        if (!generatedResults.add(name))
+            return name;
+
+        String pathname = Utils.ensureDir(srcRoot + "/" + "org.openjdk.jcstress.infra.results.".replaceAll("\\.", "/"));
+
+        PrintWriter pw = null;
+        try {
+            pw = new PrintWriter(pathname + "/" + name + ".java");
+        } catch (FileNotFoundException e) {
+            throw new IllegalStateException(e);
+        }
+
+        pw.println("package org.openjdk.jcstress.infra.results;");
+        pw.println("");
+        pw.println("import java.io.Serializable;");
+        pw.println("");
+        pw.println("public class " + name + " implements Serializable {");
+
+        {
+            int n = 1;
+            for (Class k : types.all()) {
+                pw.println("    @sun.misc.Contended");
+                pw.println("    public " + k.getSimpleName() + " r" + n + ";");
+                pw.println();
+                n++;
+            }
+        }
+
+        pw.println("    public int hashCode() {");
+        pw.println("        int result = 0;");
+        {
+            int n = 1;
+            for (Class k : types.all()) {
+                if (k == boolean.class) {
+                    pw.println("        result = 31*result + (r" + n + " ? 1 : 0);");
+                }
+                if (k == byte.class || k == short.class || k == char.class || k == int.class) {
+                    pw.println("        result = 31*result + r" + n + ";");
+                }
+                if (k == long.class) {
+                    pw.println("        result = 31*result + (int) (r" + n + " ^ (r" + n + " >>> 32));");
+                }
+                if (k == double.class) {
+                    pw.println("        result = 31*result + (int) (Double.doubleToLongBits(r" + n + ") ^ (Double.doubleToLongBits(r" + n + ") >>> 32));");
+                }
+                if (k == float.class) {
+                    pw.println("        result = 31*result + (int) (Float.floatToIntBits(r" + n + ") ^ (Float.floatToIntBits(r" + n + ") >>> 32));");
+                }
+                n++;
+            }
+        }
+        pw.println("        return result;");
+        pw.println("    }");
+        pw.println();
+        pw.println("    public boolean equals(Object o) {");
+        pw.println("        if (this == o) return true;");
+        pw.println("        if (o == null || getClass() != o.getClass()) return false;");
+        pw.println();
+        pw.println("        " + name + " that = (" + name + ") o;\n");
+
+        {
+            int n = 1;
+            for (Class k : types.all()) {
+                if (k == boolean.class || k == byte.class || k == short.class || k == char.class
+                        || k == int.class || k == long.class) {
+                    pw.println("        if (r" + n + " != that.r" + n + ") return false;");
+                }
+                if (k == double.class) {
+                    pw.println("        if (Double.compare(r" + n + ", that.r" + n + ") != 0) return false;");
+                }
+                if (k == float.class) {
+                    pw.println("        if (Float.compare(r" + n + ", that.r" + n + ") != 0) return false;");
+                }
+                n++;
+            }
+        }
+
+        pw.println("        return true;");
+        pw.println("    }");
+
+        pw.println("    public String toString() {");
+        pw.print("        return \"[\" + ");
+
+        {
+            int n = 1;
+            for (Class k : types.all()) {
+                if (n != 1)
+                    pw.print(" + \", \" + ");
+                if (k == char.class) {
+                    pw.print("(r" + n + " + 0)");
+                } else {
+                    pw.print("r" + n);
+                }
+                n++;
+            }
+            pw.println("+ \"]\";");
+        }
+
+        pw.println("    }");
+
+        pw.println("}");
+        pw.close();
+
+        return name;
+    }
+}
\ No newline at end of file
--- a/generator/src/main/java/org/openjdk/jcstress/generator/TestGenerator.java	Mon Jul 08 17:33:08 2013 +0400
+++ b/generator/src/main/java/org/openjdk/jcstress/generator/TestGenerator.java	Mon Jul 08 17:41:48 2013 +0400
@@ -24,11 +24,8 @@
  */
 package org.openjdk.jcstress.generator;
 
-import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.PrintWriter;
-import java.util.HashSet;
-import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
@@ -43,12 +40,13 @@
     private final String srcRoot;
     private final String resRoot;
 
-    private final Set<String> generatedResults = new HashSet<String>();
+    private final ResultGenerator resultGenerator;
     private PrintWriter resourceWriter;
 
     public TestGenerator(String srcRoot, String resRoot) {
         this.srcRoot = srcRoot;
         this.resRoot = resRoot;
+        this.resultGenerator = new ResultGenerator(srcRoot);
     }
 
     public void run() throws FileNotFoundException {
@@ -56,7 +54,7 @@
     }
 
     public void generateMemoryEffects() throws FileNotFoundException {
-        resourceWriter = new PrintWriter(ensureDir(resRoot + "/org/openjdk/jcstress/desc/") + "/memeffects.xml");
+        resourceWriter = new PrintWriter(Utils.ensureDir(resRoot + "/org/openjdk/jcstress/desc/") + "/memeffects.xml");
         resourceWriter.println("<testsuite>");
 
         for (Class<?> varType : Types.SUPPORTED_PRIMITIVES) {
@@ -105,131 +103,7 @@
         resourceWriter.close();
     }
 
-    public String ensureDir(String path) {
-        File file = new File(path);
-        if (!file.exists()) {
-            file.mkdirs();
-        }
-        return file.getAbsolutePath();
-    }
-
-    public String generateResult(Types types) throws FileNotFoundException {
-        String name = "";
-        for (Class k : types.all()) {
-            if (k == boolean.class) name += "X";
-            if (k == byte.class)    name += "B";
-            if (k == short.class)   name += "S";
-            if (k == char.class)    name += "C";
-            if (k == int.class)     name += "I";
-            if (k == long.class)    name += "L";
-            if (k == float.class)   name += "F";
-            if (k == double.class)  name += "D";
-        }
-        name += "_Result";
-
-        // already generated
-        if (!generatedResults.add(name))
-            return name;
-
-        String pathname = ensureDir(srcRoot + "/" + "org.openjdk.jcstress.infra.results.".replaceAll("\\.", "/"));
-
-        PrintWriter pw = new PrintWriter(pathname + "/" + name + ".java");
-
-        pw.println("package org.openjdk.jcstress.infra.results;");
-        pw.println("");
-        pw.println("import java.io.Serializable;");
-        pw.println("");
-        pw.println("public class " + name + " implements Serializable {");
-
-        {
-            int n = 1;
-            for (Class k : types.all()) {
-                pw.println("    @sun.misc.Contended");
-                pw.println("    public " + k.getSimpleName() + " r" + n + ";");
-                pw.println();
-                n++;
-            }
-        }
-
-        pw.println("    public int hashCode() {");
-        pw.println("        int result = 0;");
-        {
-            int n = 1;
-            for (Class k : types.all()) {
-                if (k == boolean.class) {
-                    pw.println("        result = 31*result + (r" + n + " ? 1 : 0);");
-                }
-                if (k == byte.class || k == short.class || k == char.class || k == int.class) {
-                    pw.println("        result = 31*result + r" + n + ";");
-                }
-                if (k == long.class) {
-                    pw.println("        result = 31*result + (int) (r" + n + " ^ (r" + n + " >>> 32));");
-                }
-                if (k == double.class) {
-                    pw.println("        result = 31*result + (int) (Double.doubleToLongBits(r" + n + ") ^ (Double.doubleToLongBits(r" + n + ") >>> 32));");
-                }
-                if (k == float.class) {
-                    pw.println("        result = 31*result + (int) (Float.floatToIntBits(r" + n + ") ^ (Float.floatToIntBits(r" + n + ") >>> 32));");
-                }
-                n++;
-            }
-        }
-        pw.println("        return result;");
-        pw.println("    }");
-        pw.println();
-        pw.println("    public boolean equals(Object o) {");
-        pw.println("        if (this == o) return true;");
-        pw.println("        if (o == null || getClass() != o.getClass()) return false;");
-        pw.println();
-        pw.println("        " + name + " that = (" + name + ") o;\n");
-
-        {
-            int n = 1;
-            for (Class k : types.all()) {
-                if (k == boolean.class || k == byte.class || k == short.class || k == char.class
-                        || k == int.class || k == long.class ) {
-                    pw.println("        if (r" + n + " != that.r" + n + ") return false;");
-                }
-                if (k == double.class) {
-                    pw.println("        if (Double.compare(r" + n + ", that.r" + n + ") != 0) return false;");
-                }
-                if (k == float.class) {
-                    pw.println("        if (Float.compare(r" + n + ", that.r" + n + ") != 0) return false;");
-                }
-                n++;
-            }
-        }
-
-        pw.println("        return true;");
-        pw.println("    }");
-
-        pw.println("    public String toString() {");
-        pw.print("        return \"[\" + ");
-
-        {
-            int n = 1;
-            for (Class k : types.all()) {
-                if (n != 1)
-                    pw.print(" + \", \" + ");
-                if (k == char.class) {
-                    pw.print("(r" + n + " + 0)");
-                } else {
-                    pw.print("r" + n);
-                }
-                n++;
-            }
-            pw.println("+ \"]\";");
-        }
-
-        pw.println("    }");
-
-        pw.println("}");
-        pw.close();
-
-        return name;
-    }
-
-    public void generate(Types types, Primitive prim, String klass, String pkg) throws FileNotFoundException {
+   public void generate(Types types, Primitive prim, String klass, String pkg) throws FileNotFoundException {
 
         resourceWriter.println("    <test name=\"" + pkg + "." + klass + "\">\n" +
                 "        <contributed-by>Aleksey Shipilev (aleksey.shipilev@oracle.com)</contributed-by>\n" +
@@ -256,9 +130,9 @@
                 "        </unmatched>\n" +
                 "    </test>");
 
-        String resultName = generateResult(types);
+        String resultName = resultGenerator.generateResult(types);
 
-        String pathname = ensureDir(srcRoot + "/" + pkg.replaceAll("\\.", "/"));
+        String pathname = Utils.ensureDir(srcRoot + "/" + pkg.replaceAll("\\.", "/"));
 
         PrintWriter pw = new PrintWriter(pathname + "/" + klass + ".java");
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/generator/src/main/java/org/openjdk/jcstress/generator/Utils.java	Mon Jul 08 17:41:48 2013 +0400
@@ -0,0 +1,37 @@
+/*
+ * 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.generator;
+
+import java.io.File;
+
+public class Utils {
+    public static String ensureDir(String path) {
+        File file = new File(path);
+        if (!file.exists()) {
+            file.mkdirs();
+        }
+        return file.getAbsolutePath();
+    }
+}
\ No newline at end of file
--- a/generator/src/main/java/org/openjdk/jcstress/tracer/TraceGen.java	Mon Jul 08 17:33:08 2013 +0400
+++ b/generator/src/main/java/org/openjdk/jcstress/tracer/TraceGen.java	Mon Jul 08 17:41:48 2013 +0400
@@ -24,14 +24,24 @@
  */
 package org.openjdk.jcstress.tracer;
 
+import org.openjdk.jcstress.generator.ResultGenerator;
+import org.openjdk.jcstress.generator.TestGenerator;
+
+import java.io.PrintWriter;
 import java.util.*;
 
 public class TraceGen {
 
     private final int vars;
+    private final String srcDir;
+    private final String resDir;
+    private final ResultGenerator resultGenerator;
 
-    public TraceGen(int vars) {
+    public TraceGen(int vars, String srcDir, String resDir) {
         this.vars = vars;
+        this.srcDir = srcDir;
+        this.resDir = resDir;
+        this.resultGenerator = new ResultGenerator(srcDir);
     }
 
     public void generate() {
@@ -93,10 +103,16 @@
     }
 
     private void emit(MultiTrace mt, Set<Map<Integer, String>> results) {
+
+        PrintWriter pw = new PrintWriter(System.out, true);
+
         System.out.println("Processing " + mt);
         for (Map<Integer, String> o : results) {
             System.out.println(o);
         }
+
+        String resultName = resultGenerator.generateResult(new TestGenerator.Types(int.class));
+
         System.out.println();
     }