1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /* @test
  25  * @summary Test selective barrier enabling works, by aggressively compiling HelloWorld with combinations
  26  *          of barrier flags
  27  * @library /test/lib
  28  * @run main/othervm TestSelectiveBarrierFlags -Xint
  29  * @run main/othervm TestSelectiveBarrierFlags -Xbatch -XX:CompileThreshold=100 -XX:TieredStopAtLevel=1
  30  * @run main/othervm TestSelectiveBarrierFlags -Xbatch -XX:CompileThreshold=100 -XX:-TieredCompilation -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
  31  */
  32 
  33 import java.util.*;
  34 import java.util.concurrent.*;
  35 import jdk.test.lib.process.ProcessTools;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 
  38 public class TestSelectiveBarrierFlags {
  39 
  40     public static void main(String[] args) throws Exception {
  41         String[][] opts = {
  42             new String[]{ "ShenandoahKeepAliveBarrier" },
  43             new String[]{ "ShenandoahWriteBarrier" },
  44             new String[]{ "ShenandoahReadBarrier" },
  45             // StoreValRead+SATB are actually compatible, but we need to protect against
  46             // StorveValEnqueue+SATB. TODO: Make it better.
  47             new String[]{ "ShenandoahSATBBarrier", "ShenandoahStoreValReadBarrier", "ShenandoahStoreValEnqueueBarrier" },
  48             new String[]{ "ShenandoahCASBarrier" },
  49             new String[]{ "ShenandoahAcmpBarrier" },
  50             new String[]{ "ShenandoahCloneBarrier" },
  51         };
  52 
  53         int size = 1;
  54         for (String[] l : opts) {
  55             size *= (l.length + 1);
  56         }
  57 
  58         ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  59 
  60         for (int c = 0; c < size; c++) {
  61             int t = c;
  62 
  63             List<String> conf = new ArrayList<>();
  64             conf.addAll(Arrays.asList(args));
  65             conf.add("-Xmx128m");
  66             conf.add("-XX:+UnlockDiagnosticVMOptions");
  67             conf.add("-XX:+UnlockExperimentalVMOptions");
  68             conf.add("-XX:+UseShenandoahGC");
  69             conf.add("-XX:ShenandoahGCHeuristics=passive");
  70 
  71             StringBuilder sb = new StringBuilder();
  72             for (String[] l : opts) {
  73                 // Make a choice which flag to select from the group.
  74                 // Zero means no flag is selected from the group.
  75                 int choice = t % (l.length + 1);
  76                 for (int e = 0; e < l.length; e++) {
  77                   conf.add("-XX:" + ((choice == (e + 1)) ? "+" : "-") + l[e]);
  78                 }
  79                 t = t / (l.length + 1);
  80             }
  81 
  82             conf.add("TestSelectiveBarrierFlags$Test");
  83 
  84             pool.submit(() -> {
  85                 try {
  86                     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(conf.toArray(new String[0]));
  87                     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  88                     output.shouldHaveExitValue(0);
  89                 } catch (Exception e) {
  90                     e.printStackTrace();
  91                     System.exit(1);
  92                 }
  93             });
  94         }
  95 
  96         pool.shutdown();
  97         pool.awaitTermination(1, TimeUnit.HOURS);
  98     }
  99 
 100     public static class Test {
 101         public static void main(String... args) {
 102             System.out.println("HelloWorld");
 103         }
 104     }
 105 
 106 }