1 /*
   2  * Copyright (c) 2015, 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 import com.oracle.java.testlibrary.OutputAnalyzer;
  25 import org.testng.annotations.Test;
  26 
  27 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  28 import com.oracle.java.testlibrary.dcmd.JMXExecutor;
  29 
  30 import java.util.concurrent.BrokenBarrierException;
  31 import java.util.concurrent.CyclicBarrier;
  32 import java.util.concurrent.locks.ReentrantLock;
  33 import java.util.regex.Pattern;
  34 
  35 /*
  36  * @test
  37  * @library /testlibrary
  38  * @build com.oracle.java.testlibrary.*
  39  * @build com.oracle.java.testlibrary.dcmd.*
  40  * @run testng PrintTest
  41  */
  42 public class PrintTest {
  43     protected boolean jucLocks = false;
  44 
  45     CyclicBarrier readyBarrier = new CyclicBarrier(3);
  46     CyclicBarrier doneBarrier = new CyclicBarrier(3);
  47 
  48     private void waitForBarrier(CyclicBarrier b) {
  49         try {
  50             b.await();
  51         } catch (InterruptedException | BrokenBarrierException e) {
  52             throw new RuntimeException("Test error: Caught unexpected exception:", e);
  53         }
  54     }
  55 
  56     class MonitorThread extends Thread {
  57         Object lock = new Object();
  58 
  59         public void run() {
  60             /* Hold lock on "lock" to show up in thread dump */
  61             synchronized (lock) {
  62                 /* Signal that we're ready for thread dump */
  63                 waitForBarrier(readyBarrier);
  64 
  65                 /* Released when the thread dump has been taken */
  66                 waitForBarrier(doneBarrier);
  67             }
  68         }
  69     }
  70 
  71     class LockThread extends Thread {
  72         ReentrantLock lock = new ReentrantLock();
  73 
  74         public void run() {
  75             /* Hold lock "lock" to show up in thread dump */
  76             lock.lock();
  77 
  78             /* Signal that we're ready for thread dump */
  79             waitForBarrier(readyBarrier);
  80 
  81             /* Released when the thread dump has been taken */
  82             waitForBarrier(doneBarrier);
  83 
  84             lock.unlock();
  85         }
  86     }
  87 
  88     public void run(CommandExecutor executor) {
  89         MonitorThread mThread = new MonitorThread();
  90         mThread.start();
  91         LockThread lThread = new LockThread();
  92         lThread.start();
  93 
  94         /* Wait for threads to get ready */
  95         waitForBarrier(readyBarrier);
  96 
  97         /* Execute */
  98         OutputAnalyzer output = executor.execute("Thread.print" + (jucLocks ? " -l=true" : ""));
  99 
 100         /* Signal that we've got the thread dump */
 101         waitForBarrier(doneBarrier);
 102 
 103         /*
 104          * Example output (trimmed) with arrows indicating the rows we are looking for:
 105          *
 106          *     ...
 107          *     "Thread-2" #24 prio=5 os_prio=0 tid=0x00007f913411f800 nid=0x4fc9 waiting on condition [0x00007f91fbffe000]
 108          *        java.lang.Thread.State: WAITING (parking)
 109          *       at sun.misc.Unsafe.park(Native Method)
 110          *       - parking to wait for  <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
 111          *       at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
 112          *       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
 113          *       at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)
 114          *       at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
 115          *       at Print.waitForBarrier(Print.java:26)
 116          *       at Print.access$000(Print.java:18)
 117          *       at Print$LockThread.run(Print.java:58)
 118          *
 119          * -->    Locked ownable synchronizers:
 120          * -->    - <0x000000071a294930> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
 121          *
 122          *     "Thread-1" #23 prio=5 os_prio=0 tid=0x00007f913411e800 nid=0x4fc8 waiting on condition [0x00007f9200113000]
 123          *        java.lang.Thread.State: WAITING (parking)
 124          *       at sun.misc.Unsafe.park(Native Method)
 125          *       - parking to wait for  <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
 126          *       at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
 127          *       at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
 128          *       at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)
 129          *       at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
 130          *       at Print.waitForBarrier(Print.java:26)
 131          *       at Print.access$000(Print.java:18)
 132          *       at Print$MonitorThread.run(Print.java:42)
 133          * -->   - locked <0x000000071a294390> (a java.lang.Object)
 134          *
 135          *        Locked ownable synchronizers:
 136          *       - None
 137          *
 138          *     "MainThread" #22 prio=5 os_prio=0 tid=0x00007f923015b000 nid=0x4fc7 in Object.wait() [0x00007f9200840000]
 139          *        java.lang.Thread.State: WAITING (on object monitor)
 140          *       at java.lang.Object.wait(Native Method)
 141          *       - waiting on <0x000000071a70ad98> (a java.lang.UNIXProcess)
 142          *       at java.lang.Object.wait(Object.java:502)
 143          *        at java.lang.UNIXProcess.waitFor(UNIXProcess.java:397)
 144          *        - locked <0x000000071a70ad98> (a java.lang.UNIXProcess)
 145          *        at com.oracle.java.testlibrary.dcmd.JcmdExecutor.executeImpl(JcmdExecutor.java:32)
 146          *       at com.oracle.java.testlibrary.dcmd.CommandExecutor.execute(CommandExecutor.java:24)
 147          * -->   at Print.run(Print.java:74)
 148          *       at Print.file(Print.java:112)
 149          *     ...
 150 
 151          */
 152         output.shouldMatch(".*at " + Pattern.quote(PrintTest.class.getName()) + "\\.run.*");
 153         output.shouldMatch(".*- locked <0x\\p{XDigit}+> \\(a " + Pattern.quote(mThread.lock.getClass().getName()) + "\\).*");
 154 
 155         String jucLockPattern1 = ".*Locked ownable synchronizers:.*";
 156         String jucLockPattern2 = ".*- <0x\\p{XDigit}+> \\(a " + Pattern.quote(lThread.lock.getClass().getName()) + ".*";
 157 
 158         if (jucLocks) {
 159             output.shouldMatch(jucLockPattern1);
 160             output.shouldMatch(jucLockPattern2);
 161         } else {
 162             output.shouldNotMatch(jucLockPattern1);
 163             output.shouldNotMatch(jucLockPattern2);
 164         }
 165     }
 166 
 167     @Test
 168     public void jmx() {
 169         run(new JMXExecutor());
 170     }
 171 }