1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/membars/DekkerTest.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,163 @@ 1.4 +/* 1.5 + * Copyright 2013 SAP AG. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8007898 1.30 + * @summary Incorrect optimization of Memory Barriers in Matcher::post_store_load_barrier(). 1.31 + * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM DekkerTest 1.32 + * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM DekkerTest 1.33 + * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM DekkerTest 1.34 + * @author Martin Doerr martin DOT doerr AT sap DOT com 1.35 + * 1.36 + * Run 3 times since the failure is intermittent. 1.37 + */ 1.38 + 1.39 +public class DekkerTest { 1.40 + 1.41 + /* 1.42 + Read After Write Test (basically a simple Dekker test with volatile variables) 1.43 + Derived from the original jcstress test, available at: 1.44 + http://hg.openjdk.java.net/code-tools/jcstress/file/6c339a5aa00d/ 1.45 + tests-custom/src/main/java/org/openjdk/jcstress/tests/volatiles/DekkerTest.java 1.46 + */ 1.47 + 1.48 + static final int ITERATIONS = 1000000; 1.49 + 1.50 + static class TestData { 1.51 + public volatile int a; 1.52 + public volatile int b; 1.53 + } 1.54 + 1.55 + static class ResultData { 1.56 + public int a; 1.57 + public int b; 1.58 + } 1.59 + 1.60 + TestData[] testDataArray; 1.61 + ResultData[] results; 1.62 + 1.63 + volatile boolean start; 1.64 + 1.65 + public DekkerTest() { 1.66 + testDataArray = new TestData[ITERATIONS]; 1.67 + results = new ResultData[ITERATIONS]; 1.68 + for (int i = 0; i < ITERATIONS; ++i) { 1.69 + testDataArray[i] = new TestData(); 1.70 + results[i] = new ResultData(); 1.71 + } 1.72 + start = false; 1.73 + } 1.74 + 1.75 + public void reset() { 1.76 + for (int i = 0; i < ITERATIONS; ++i) { 1.77 + testDataArray[i].a = 0; 1.78 + testDataArray[i].b = 0; 1.79 + results[i].a = 0; 1.80 + results[i].b = 0; 1.81 + } 1.82 + start = false; 1.83 + } 1.84 + 1.85 + int actor1(TestData t) { 1.86 + t.a = 1; 1.87 + return t.b; 1.88 + } 1.89 + 1.90 + int actor2(TestData t) { 1.91 + t.b = 1; 1.92 + return t.a; 1.93 + } 1.94 + 1.95 + class Runner1 extends Thread { 1.96 + public void run() { 1.97 + do {} while (!start); 1.98 + for (int i = 0; i < ITERATIONS; ++i) { 1.99 + results[i].a = actor1(testDataArray[i]); 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + class Runner2 extends Thread { 1.105 + public void run() { 1.106 + do {} while (!start); 1.107 + for (int i = 0; i < ITERATIONS; ++i) { 1.108 + results[i].b = actor2(testDataArray[i]); 1.109 + } 1.110 + } 1.111 + } 1.112 + 1.113 + void testRunner() { 1.114 + Thread thread1 = new Runner1(); 1.115 + Thread thread2 = new Runner2(); 1.116 + thread1.start(); 1.117 + thread2.start(); 1.118 + do {} while (!thread1.isAlive()); 1.119 + do {} while (!thread2.isAlive()); 1.120 + start = true; 1.121 + Thread.yield(); 1.122 + try { 1.123 + thread1.join(); 1.124 + thread2.join(); 1.125 + } catch (InterruptedException e) { 1.126 + System.out.println("interrupted!"); 1.127 + System.exit(1); 1.128 + } 1.129 + } 1.130 + 1.131 + boolean printResult() { 1.132 + int[] count = new int[4]; 1.133 + for (int i = 0; i < ITERATIONS; ++i) { 1.134 + int event_kind = (results[i].a << 1) + results[i].b; 1.135 + ++count[event_kind]; 1.136 + } 1.137 + if (count[0] == 0 && count[3] == 0) { 1.138 + System.out.println("[not interesting]"); 1.139 + return false; // not interesting 1.140 + } 1.141 + String error = (count[0] == 0) ? " ok" : " disallowed!"; 1.142 + System.out.println("[0,0] " + count[0] + error); 1.143 + System.out.println("[0,1] " + count[1]); 1.144 + System.out.println("[1,0] " + count[2]); 1.145 + System.out.println("[1,1] " + count[3]); 1.146 + return (count[0] != 0); 1.147 + } 1.148 + 1.149 + public static void main(String args[]) { 1.150 + DekkerTest test = new DekkerTest(); 1.151 + final int runs = 30; 1.152 + int failed = 0; 1.153 + for (int c = 0; c < runs; ++c) { 1.154 + test.testRunner(); 1.155 + if (test.printResult()) { 1.156 + failed++; 1.157 + } 1.158 + test.reset(); 1.159 + } 1.160 + if (failed > 0) { 1.161 + throw new InternalError("FAILED. Got " + failed + " failed ITERATIONS"); 1.162 + } 1.163 + System.out.println("PASSED."); 1.164 + } 1.165 + 1.166 +}