1 /*
   2  * Copyright (c) 2017, 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 applications.jcstress;
  25 
  26 import jdk.test.lib.Utils;
  27 import jdk.test.lib.process.OutputAnalyzer;
  28 import jdk.test.lib.process.ProcessTools;
  29 
  30 import java.io.BufferedReader;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.io.PrintStream;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Calendar;
  38 import java.util.EnumSet;
  39 import java.util.function.Predicate;
  40 
  41 
  42 /**
  43  * @notest THIS IS NOT A TEST.
  44  * This is a test generator, should be run only when jsctress is changed
  45  *
  46  * @library /test/lib /
  47  * @run main applications.jcstress.TestGenerator
  48  */
  49 
  50 /**
  51  * Use jcstress test suite to generates jtreg tests in 'test.src' or current
  52  * directory. Used version is defined in JcstressRunner class.
  53  *
  54  * Each generated jtreg test file will contain several tests. Subdirectories are
  55  * used to allow running all tests from a file using command line. 'copy',
  56  * 'acqrel', 'fences', 'atomicity', 'seqcst.sync', 'seqcst.volatiles' and
  57  * 'other' tests will be generated.
  58  *
  59  * This generator depends on testlibrary, therefore it should be compiled and
  60  * added to classpath. One can replace @notest by @test in jtreg test
  61  * description above to run this class with jtreg.
  62  *
  63  * @see <a href=https://wiki.openjdk.java.net/display/CodeTools/jcstress>jcstress</a>
  64  */
  65 public class TestGenerator {
  66     private static final String COPYRIGHT;
  67     static {
  68         String years;
  69         final int firstYear = 2017;
  70         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
  71         if (firstYear < currentYear) {
  72             years = String.format("%d, %d", firstYear, currentYear);
  73         } else {
  74             years = "" + firstYear;
  75         }
  76         COPYRIGHT = String.format("/*\n" +
  77                 " * Copyright (c) %s, Oracle and/or its affiliates. All rights reserved.\n" +
  78                 " * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n" +
  79                 " *\n" +
  80                 " * This code is free software; you can redistribute it and/or modify it\n" +
  81                 " * under the terms of the GNU General Public License version 2 only, as\n" +
  82                 " * published by the Free Software Foundation.\n" +
  83                 " *\n" +
  84                 " * This code is distributed in the hope that it will be useful, but WITHOUT\n" +
  85                 " * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n" +
  86                 " * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n" +
  87                 " * version 2 for more details (a copy is included in the LICENSE file that\n" +
  88                 " * accompanied this code).\n" +
  89                 " *\n" +
  90                 " * You should have received a copy of the GNU General Public License version\n" +
  91                 " * 2 along with this work; if not, write to the Free Software Foundation,\n" +
  92                 " * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n" +
  93                 " *\n" +
  94                 " * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n" +
  95                 " * or visit www.oracle.com if you need additional information or have any\n" +
  96                 " * questions.\n" +
  97                 " */\n\n", years);
  98     }
  99 
 100     private static enum JcstressGroup {
 101         MEMEFFECTS("memeffects"),
 102         COPY("copy"),
 103         ACQREL("acqrel"),
 104         FENCES("fences"),
 105         ATOMICITY("atomicity"),
 106         SEQCST_SYNC("seqcst.sync"),
 107         SEQCST_VOLATILES("seqcst.volatiles"),
 108         OTHER("other", JcstressGroup.otherFilter());
 109 
 110         private final String groupName;
 111         private final Predicate<String> filter;
 112 
 113         private JcstressGroup(String groupName, Predicate<String> filter) {
 114             this.groupName = groupName;
 115             this.filter = filter;
 116         }
 117 
 118         private JcstressGroup(String groupName) {
 119             this(groupName, JcstressGroup.nameFilter(groupName));
 120         }
 121 
 122         private static Predicate<String> nameFilter(String group) {
 123             return s -> s.startsWith("org.openjdk.jcstress.tests." + group + ".");
 124         }
 125 
 126         private static Predicate<String> otherFilter() {
 127             return (s) -> {
 128                 for (JcstressGroup g : EnumSet.complementOf(EnumSet.of(OTHER))) {
 129                     if (g.filter.test(s)) {
 130                         return false;
 131                     }
 132                 }
 133                 return true;
 134             };
 135         }
 136     }
 137 
 138     public static String DESC_FORMAT = "\n"
 139             + "/**\n"
 140             + " * @test %1$s\n"
 141             + " * @library /test/lib /\n"
 142             + " * @run driver " + JcstressRunner.class.getName()
 143                     // verbose output
 144                     + " -v"
 145                     // test mode preset
 146                     + " -m default"
 147                     // test name
 148                     + " -t %1$s\n"
 149             + " */\n";
 150 
 151     public static void main(String[] args)  {
 152         Path path = JcstressRunner.pathToArtifact();
 153         Path output;
 154         try {
 155             output = Files.createTempFile("jcstress", ".out");
 156             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 157                     "-jar",
 158                     path.toAbsolutePath().toString(),
 159                     "-l");
 160             pb.redirectOutput(output.toFile());
 161             new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
 162         } catch (Exception e) {
 163             throw new Error("Can not get list of tests", e);
 164         }
 165         for (JcstressGroup group : JcstressGroup.values()) {
 166             try {
 167                 try (BufferedReader reader = Files.newBufferedReader(output)) {
 168                     // skip first 4 lines: name, -{80}, revision and empty line
 169                     for (int i = 0; i < 4; ++i) {
 170                         reader.readLine();
 171                     }
 172                     new TestGenerator(group).generate(reader);
 173                 }
 174             } catch (IOException e) {
 175                 throw new Error("Generating tests for " + group.name()
 176                         + " has failed", e);
 177             }
 178         }
 179         output.toFile().delete();
 180     }
 181 
 182     private final JcstressGroup group;
 183 
 184     private TestGenerator(JcstressGroup group) {
 185         this.group = group;
 186     }
 187 
 188     private void generate(BufferedReader reader) throws IOException {
 189         // array is needed to change value inside a lambda
 190         long[] count = {0L};
 191         String root = Utils.TEST_SRC;
 192         Path testFile = Paths.get(root)
 193                 .resolve(group.groupName)
 194                 .resolve("Test.java");
 195         File testDir = testFile.getParent().toFile();
 196         if (!testDir.mkdirs() && !testDir.exists()) {
 197             throw new Error("Can not create directories for "
 198                     + testFile.toString());
 199         }
 200 
 201         try (PrintStream ps = new PrintStream(testFile.toFile())) {
 202             ps.print(COPYRIGHT);
 203             ps.printf("/* DO NOT MODIFY THIS FILE. GENERATED BY %s */\n",
 204                     getClass().getName());
 205 
 206             reader.lines()
 207                   .filter(group.filter)
 208                   .forEach(s -> {
 209                       count[0]++;
 210                       ps.printf(DESC_FORMAT, s);
 211                   });
 212             ps.print('\n');
 213         }
 214         System.out.printf("%d tests generated in %s%n",
 215                 count[0], group.groupName);
 216     }
 217 }
 218