1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. 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 
  25 /* @test TestSelectiveBarrierFlags
  26  * @summary Test selective barrier enabling works, by aggressively compiling HelloWorld with combinations
  27  *          of barrier flags
  28  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  29  * @library /test/lib
  30  * @run driver TestSelectiveBarrierFlags -Xint
  31  * @run driver TestSelectiveBarrierFlags -Xbatch -XX:CompileThreshold=100 -XX:TieredStopAtLevel=1
  32  * @run driver TestSelectiveBarrierFlags -Xbatch -XX:CompileThreshold=100 -XX:-TieredCompilation -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
  33  */
  34 
  35 import java.util.*;
  36 import java.util.concurrent.*;
  37 
  38 import jdk.test.lib.process.ProcessTools;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 public class TestSelectiveBarrierFlags {
  42 
  43     public static void main(String[] args) throws Exception {
  44         String[][] opts = {
  45                 new String[] { "ShenandoahLoadRefBarrier" },
  46                 new String[] { "ShenandoahSATBBarrier", "ShenandoahStoreValEnqueueBarrier" },
  47                 new String[] { "ShenandoahCASBarrier" },
  48                 new String[] { "ShenandoahCloneBarrier" },
  49         };
  50 
  51         int size = 1;
  52         for (String[] l : opts) {
  53             size *= (l.length + 1);
  54         }
  55 
  56         ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  57 
  58         for (int c = 0; c < size; c++) {
  59             int t = c;
  60 
  61             List<String> conf = new ArrayList<>();
  62             conf.addAll(Arrays.asList(args));
  63             conf.add("-Xmx128m");
  64             conf.add("-XX:+UnlockDiagnosticVMOptions");
  65             conf.add("-XX:+UnlockExperimentalVMOptions");
  66             conf.add("-XX:+UseShenandoahGC");
  67             conf.add("-XX:ShenandoahGCMode=passive");
  68 
  69             StringBuilder sb = new StringBuilder();
  70             for (String[] l : opts) {
  71                 // Make a choice which flag to select from the group.
  72                 // Zero means no flag is selected from the group.
  73                 int choice = t % (l.length + 1);
  74                 for (int e = 0; e < l.length; e++) {
  75                     conf.add("-XX:" + ((choice == (e + 1)) ? "+" : "-") + l[e]);
  76                 }
  77                 t = t / (l.length + 1);
  78             }
  79 
  80             conf.add("TestSelectiveBarrierFlags$Test");
  81 
  82             pool.submit(() -> {
  83                 try {
  84                     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(conf.toArray(new String[0]));
  85                     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  86                     output.shouldHaveExitValue(0);
  87                 } catch (Exception e) {
  88                     e.printStackTrace();
  89                     System.exit(1);
  90                 }
  91             });
  92         }
  93 
  94         pool.shutdown();
  95         pool.awaitTermination(1, TimeUnit.HOURS);
  96     }
  97 
  98     public static class Test {
  99         public static void main(String... args) {
 100             System.out.println("HelloWorld");
 101         }
 102     }
 103 
 104 }