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