hotspot/test/compiler/6912517/Test.java
changeset 4580 db05951a970c
child 5547 f4b087cbb361
equal deleted inserted replaced
4579:62478db2dc78 4580:db05951a970c
       
     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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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 Test
       
    30  */
       
    31 
       
    32 /**
       
    33  * Highlights a bug with the JIT compiler.
       
    34  * @author Matt Bruce m b r u c e __\at/__ g m a i l DOT c o m
       
    35  */
       
    36 public class Test implements Runnable
       
    37 {
       
    38     private final Thread myThread;
       
    39     private Thread       myInitialThread;
       
    40     private boolean      myShouldCheckThreads;
       
    41 
       
    42     /**
       
    43      * Sets up the running thread, and starts it.
       
    44      */
       
    45     public Test(int id)
       
    46     {
       
    47         myThread = new Thread(this);
       
    48         myThread.setName("Runner: " + id);
       
    49         myThread.start();
       
    50         myShouldCheckThreads = false;
       
    51     }
       
    52 
       
    53     /**
       
    54      * @param shouldCheckThreads the shouldCheckThreads to set
       
    55      */
       
    56     public void setShouldCheckThreads(boolean shouldCheckThreads)
       
    57     {
       
    58         myShouldCheckThreads = shouldCheckThreads;
       
    59     }
       
    60 
       
    61     /**
       
    62      * Starts up the two threads with enough delay between them for JIT to
       
    63      * kick in.
       
    64      * @param args
       
    65      * @throws InterruptedException
       
    66      */
       
    67     public static void main(String[] args) throws InterruptedException
       
    68     {
       
    69         // let this run for a bit, so the "run" below is JITTed.
       
    70         for (int id = 0; id < 20; id++) {
       
    71             System.out.println("Starting thread: " + id);
       
    72             Test bug = new Test(id);
       
    73             bug.setShouldCheckThreads(true);
       
    74             Thread.sleep(2500);
       
    75         }
       
    76     }
       
    77 
       
    78     /**
       
    79      * @see java.lang.Runnable#run()
       
    80      */
       
    81     public void run()
       
    82     {
       
    83         long runNumber = 0;
       
    84         while (true) {
       
    85             // run hot for a little while, give JIT time to kick in to this loop.
       
    86             // then run less hot.
       
    87             if (runNumber > 15000) {
       
    88                 try {
       
    89                     Thread.sleep(5);
       
    90                 }
       
    91                 catch (InterruptedException e) {
       
    92                     e.printStackTrace();
       
    93                 }
       
    94             }
       
    95             runNumber++;
       
    96             ensureProperCallingThread();
       
    97         }
       
    98     }
       
    99 
       
   100     private void ensureProperCallingThread()
       
   101     {
       
   102         // this should never be null.  but with the JIT bug, it will be.
       
   103         // JIT BUG IS HERE ==>>>>>
       
   104         if (myShouldCheckThreads) {
       
   105             if (myInitialThread == null) {
       
   106                 myInitialThread = Thread.currentThread();
       
   107             }
       
   108             else if (myInitialThread != Thread.currentThread()) {
       
   109                 System.out.println("Not working: " + myInitialThread.getName());
       
   110             }
       
   111         }
       
   112     }
       
   113 }