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