Test6912517.java revision 11707:ad7af1afda7a
1/*
2 * Copyright 2009 D.E. Shaw.  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
26 * @bug 6912517
27 * @summary JIT bug compiles out (and stops running) code that needs to be run.  Causes NPE.
28 *
29 * @run main/othervm -Xbatch -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops
30 *      compiler.c2.Test6912517
31 */
32
33package compiler.c2;
34
35/**
36 * Highlights a bug with the JIT compiler.
37 * @author Matt Bruce m b r u c e __\at/__ g m a i l DOT c o m
38 */
39public class Test6912517 implements Runnable
40{
41    private final Thread myThread;
42    private Thread       myInitialThread;
43    private boolean      myShouldCheckThreads;
44
45    /**
46     * Sets up the running thread, and starts it.
47     */
48    public Test6912517(int id)
49    {
50        myThread = new Thread(this);
51        myThread.setName("Runner: " + id);
52        myThread.start();
53        myShouldCheckThreads = false;
54    }
55
56    /**
57     * @param shouldCheckThreads the shouldCheckThreads to set
58     */
59    public void setShouldCheckThreads(boolean shouldCheckThreads)
60    {
61        myShouldCheckThreads = shouldCheckThreads;
62    }
63
64    /**
65     * Starts up the two threads with enough delay between them for JIT to
66     * kick in.
67     * @param args
68     * @throws InterruptedException
69     */
70    public static void main(String[] args) throws InterruptedException
71    {
72        // let this run for a bit, so the "run" below is JITTed.
73        for (int id = 0; id < 20; id++) {
74            System.out.println("Starting thread: " + id);
75            Test6912517 bug = new Test6912517(id);
76            bug.setShouldCheckThreads(true);
77            Thread.sleep(2500);
78        }
79    }
80
81    /**
82     * @see java.lang.Runnable#run()
83     */
84    public void run()
85    {
86        long runNumber = 0;
87        while (true) {
88            // run hot for a little while, give JIT time to kick in to this loop.
89            // then run less hot.
90            if (runNumber > 15000) {
91                try {
92                    Thread.sleep(5);
93                }
94                catch (InterruptedException e) {
95                    e.printStackTrace();
96                }
97            }
98            runNumber++;
99            ensureProperCallingThread();
100        }
101    }
102
103    private void ensureProperCallingThread()
104    {
105        // this should never be null.  but with the JIT bug, it will be.
106        // JIT BUG IS HERE ==>>>>>
107        if (myShouldCheckThreads) {
108            if (myInitialThread == null) {
109                myInitialThread = Thread.currentThread();
110            }
111            else if (myInitialThread != Thread.currentThread()) {
112                System.out.println("Not working: " + myInitialThread.getName());
113            }
114        }
115    }
116}
117