test/hotspot/jtreg/vmTestbase/nsk/monitoring/LockTest/LockTest001/LockTest001.java
changeset 49958 cc29d7717e3a
equal deleted inserted replaced
49957:746229cc1ab0 49958:cc29d7717e3a
       
     1 /*
       
     2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. 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  *
       
    27  * @summary converted from VM Testbase nsk/monitoring/LockTest/LockTest001.
       
    28  * VM Testbase keywords: [monitoring, vm6]
       
    29  *
       
    30  * @library /vmTestbase
       
    31  *          /test/lib
       
    32  * @run driver jdk.test.lib.FileInstaller . .
       
    33  * @run main/othervm nsk.monitoring.LockTest.LockTest001.LockTest001
       
    34  */
       
    35 
       
    36 package nsk.monitoring.LockTest.LockTest001;
       
    37 
       
    38 import java.lang.management.ThreadInfo;
       
    39 import java.lang.management.ManagementFactory;
       
    40 
       
    41 import nsk.share.TestFailure;
       
    42 
       
    43 public class LockTest001
       
    44 {
       
    45   private Object syncObj = new Object();
       
    46 
       
    47   public LockTest001()
       
    48   {  }
       
    49 
       
    50   public void test()
       
    51   {
       
    52 
       
    53     int localVar0, localVar1, localVar2, localVar3, localVar4, localVar5, localVar6, localVar7, localVar8, localVar9;
       
    54     int localVar10, localVar11, localVar12, localVar13, localVar14, localVar15, localVar16, localVar17, localVar18, localVar19;
       
    55     int localVar20, localVar21, localVar22, localVar23, localVar24, localVar25, localVar26, localVar27, localVar28, localVar29;
       
    56     int localVar30, localVar31, localVar32, localVar33, localVar34, localVar35, localVar36, localVar37, localVar38, localVar39;
       
    57     int localVar40, localVar41, localVar42, localVar43, localVar44, localVar45, localVar46, localVar47, localVar48, localVar49;
       
    58 
       
    59     String threadName = Thread.currentThread().getName();
       
    60     System.out.println(threadName + ": Entering test()");
       
    61 
       
    62     synchronized (syncObj)
       
    63     {
       
    64       int n = 0;
       
    65       for (int i = 0; i < 10000000; i++)
       
    66         n += i;
       
    67       logMonitors((n & 1) >>> (n & 1));
       
    68     } // end sync
       
    69 
       
    70     logMonitors(1);
       
    71 
       
    72     synchronized (syncObj)
       
    73     {
       
    74       logMonitors(2);
       
    75     }
       
    76     logMonitors(3);
       
    77 
       
    78     System.out.println(threadName + ": Leaving test()");
       
    79     return;
       
    80   }
       
    81 
       
    82   public static void logMonitors(int n)
       
    83   {
       
    84     String threadName = Thread.currentThread().getName();
       
    85     java.lang.management.MonitorInfo[] monitors = java.lang.management.ManagementFactory.getThreadMXBean()
       
    86                                                   .getThreadInfo(new long[] { Thread.currentThread().getId() }, true, true)[0].getLockedMonitors();
       
    87     System.out.println(threadName + ": Checkpoint #" + n + ", number of monitors: " + monitors.length);
       
    88     if ((n == 3) && (monitors.length > 0))
       
    89       System.out.println(threadName + ": MONITOR HELD AFTER EXITING SYNCHRONIZED BLOCK!");
       
    90     for (int i = 0; i < monitors.length; i++)
       
    91     {
       
    92       java.lang.management.MonitorInfo monitor = monitors[i];
       
    93       System.out.println(threadName + ": MonitorInfo[" + i + "]=" + monitor.toString() + ", StackDepth=" + monitor.getLockedStackDepth()
       
    94                          + ", frame=" + ((monitor.getLockedStackFrame() == null) ? "No Frame" : monitor.getLockedStackFrame().toString()));
       
    95     }
       
    96     return;
       
    97   }
       
    98 
       
    99   public static void main(String[] argv)
       
   100   {
       
   101     LockTest001 lt = new LockTest001();
       
   102     RunLockTest rlt = new RunLockTest(lt, 10000);
       
   103     Thread t = new Thread(rlt);
       
   104     System.out.println("main: About to start first thread: " + t.getName());
       
   105     t.start();
       
   106 
       
   107     synchronized (rlt)
       
   108     {
       
   109       while (!rlt.finishedTest)
       
   110       {
       
   111         try
       
   112         {  rlt.wait();  }
       
   113         catch (Exception e)
       
   114         {  }
       
   115       }
       
   116     }
       
   117 
       
   118     rlt = new RunLockTest(lt, 0);
       
   119     t = new Thread(rlt);
       
   120     System.out.println("main: About to start second thread: " + t.getName());
       
   121     t.start();
       
   122 
       
   123     // amount of time to wait for the thread. In case of buggy behavior, the thread will hang forever.
       
   124     // but long enough to complete test on busy boxes
       
   125     long endTime = System.currentTimeMillis() + 5 * 60 * 1000;
       
   126 
       
   127     boolean isError = true;
       
   128 
       
   129     while (System.currentTimeMillis() < endTime)
       
   130     {
       
   131       if (t.getState() == Thread.State.TERMINATED)
       
   132       {
       
   133           isError = false;
       
   134           break;
       
   135       }
       
   136       try
       
   137       {  Thread.currentThread().sleep(1000);  }
       
   138       catch (Exception e)
       
   139       {  }
       
   140       ThreadInfo[] tis = ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true, true);
       
   141       if (tis.length == 0)
       
   142         System.out.println("Unable to find the thread " + t.getName());
       
   143       else
       
   144       {
       
   145         ThreadInfo ti = tis[0];
       
   146         if (ti == null)
       
   147           System.out.println("Unable to get info for thread: " + t.getName());
       
   148         else
       
   149           System.out.println(t.getName() + " state: " + ti.getThreadState().toString() +
       
   150                              ", LockName: " + ti.getLockName() + ", LockOwnerName: " + ti.getLockOwnerName());
       
   151       }
       
   152     }
       
   153 
       
   154     if (!isError)
       
   155         System.out.println("TEST PASSED");
       
   156     else
       
   157         throw new TestFailure("TEST FAILED");
       
   158     return;
       
   159   }
       
   160 
       
   161   public static class RunLockTest implements Runnable
       
   162   {
       
   163     public LockTest001 lt;
       
   164     public boolean finishedTest = false;
       
   165     public long sleepLength;
       
   166 
       
   167     public RunLockTest(LockTest001 _lt, long _sleepLength)
       
   168     {
       
   169       lt = _lt;
       
   170       sleepLength = _sleepLength;
       
   171     }
       
   172 
       
   173     public void run()
       
   174     {
       
   175       lt.test();
       
   176       synchronized (this)
       
   177       {
       
   178         finishedTest = true;
       
   179         this.notify();
       
   180       }
       
   181       if (sleepLength > 0)
       
   182       {
       
   183         try
       
   184         {  Thread.currentThread().sleep(sleepLength);  }
       
   185         catch (Exception e)
       
   186         {  }
       
   187       }
       
   188       System.out.println(Thread.currentThread().getName() + ": exiting");
       
   189       return;
       
   190     }
       
   191   }
       
   192 
       
   193 }