Path: blob/master/test/hotspot/jtreg/compiler/membars/DekkerTest.java
41149 views
/*1* Copyright (c) 2013 SAP SE. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key stress randomness26* @bug 800789827* @summary Incorrect optimization of Memory Barriers in Matcher::post_store_load_barrier().28* @run main/othervm -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions29* -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM30* compiler.membars.DekkerTest31* @run main/othervm -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions32* -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM33* compiler.membars.DekkerTest34* @run main/othervm -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions35* -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+StressGCM -XX:+StressLCM36* compiler.membars.DekkerTest37* @author Martin Doerr martin DOT doerr AT sap DOT com38*39* Run 3 times since the failure is intermittent.40*/4142package compiler.membars;4344public class DekkerTest {4546/*47Read After Write Test (basically a simple Dekker test with volatile variables)48Derived from the original jcstress test, available at:49http://hg.openjdk.java.net/code-tools/jcstress/file/6c339a5aa00d/50tests-custom/src/main/java/org/openjdk/jcstress/tests/volatiles/DekkerTest.java51*/5253static final int ITERATIONS = 1000000;5455static class TestData {56public volatile int a;57public volatile int b;58}5960static class ResultData {61public int a;62public int b;63}6465TestData[] testDataArray;66ResultData[] results;6768volatile boolean start;6970public DekkerTest() {71testDataArray = new TestData[ITERATIONS];72results = new ResultData[ITERATIONS];73for (int i = 0; i < ITERATIONS; ++i) {74testDataArray[i] = new TestData();75results[i] = new ResultData();76}77start = false;78}7980public void reset() {81for (int i = 0; i < ITERATIONS; ++i) {82testDataArray[i].a = 0;83testDataArray[i].b = 0;84results[i].a = 0;85results[i].b = 0;86}87start = false;88}8990int actor1(TestData t) {91t.a = 1;92return t.b;93}9495int actor2(TestData t) {96t.b = 1;97return t.a;98}99100class Runner1 extends Thread {101public void run() {102do {} while (!start);103for (int i = 0; i < ITERATIONS; ++i) {104results[i].a = actor1(testDataArray[i]);105}106}107}108109class Runner2 extends Thread {110public void run() {111do {} while (!start);112for (int i = 0; i < ITERATIONS; ++i) {113results[i].b = actor2(testDataArray[i]);114}115}116}117118void testRunner() {119Thread thread1 = new Runner1();120Thread thread2 = new Runner2();121thread1.start();122thread2.start();123do {} while (!thread1.isAlive());124do {} while (!thread2.isAlive());125start = true;126Thread.yield();127try {128thread1.join();129thread2.join();130} catch (InterruptedException e) {131System.out.println("interrupted!");132System.exit(1);133}134}135136boolean printResult() {137int[] count = new int[4];138for (int i = 0; i < ITERATIONS; ++i) {139int event_kind = (results[i].a << 1) + results[i].b;140++count[event_kind];141}142if (count[0] == 0 && count[3] == 0) {143System.out.println("[not interesting]");144return false; // not interesting145}146String error = (count[0] == 0) ? " ok" : " disallowed!";147System.out.println("[0,0] " + count[0] + error);148System.out.println("[0,1] " + count[1]);149System.out.println("[1,0] " + count[2]);150System.out.println("[1,1] " + count[3]);151return (count[0] != 0);152}153154public static void main(String args[]) {155DekkerTest test = new DekkerTest();156final int runs = 30;157int failed = 0;158for (int c = 0; c < runs; ++c) {159test.testRunner();160if (test.printResult()) {161failed++;162}163test.reset();164}165if (failed > 0) {166throw new InternalError("FAILED. Got " + failed + " failed ITERATIONS");167}168System.out.println("PASSED.");169}170171}172173174