Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/thread/PrintTest.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.Test;24import org.testng.Assert;2526import jdk.test.lib.process.OutputAnalyzer;2728import jdk.test.lib.dcmd.CommandExecutor;29import jdk.test.lib.dcmd.JMXExecutor;3031import java.util.concurrent.BrokenBarrierException;32import java.util.concurrent.CyclicBarrier;33import java.util.concurrent.locks.ReentrantLock;34import java.util.regex.Pattern;3536/*37* @test38* @summary Test of diagnostic command Thread.print39* @library /test/lib40* @modules java.base/jdk.internal.misc41* java.compiler42* java.management43* jdk.internal.jvmstat/sun.jvmstat.monitor44* @run testng PrintTest45*/46public class PrintTest {47protected boolean jucLocks = false;4849CyclicBarrier readyBarrier = new CyclicBarrier(3);50CyclicBarrier doneBarrier = new CyclicBarrier(3);5152private void waitForBarrier(CyclicBarrier b) {53try {54b.await();55} catch (InterruptedException | BrokenBarrierException e) {56Assert.fail("Test error: Caught unexpected exception:", e);57}58}5960class MonitorThread extends Thread {61Object lock = new Object();6263public void run() {64/* Hold lock on "lock" to show up in thread dump */65synchronized (lock) {66/* Signal that we're ready for thread dump */67waitForBarrier(readyBarrier);6869/* Released when the thread dump has been taken */70waitForBarrier(doneBarrier);71}72}73}7475class LockThread extends Thread {76ReentrantLock lock = new ReentrantLock();7778public void run() {79/* Hold lock "lock" to show up in thread dump */80lock.lock();8182/* Signal that we're ready for thread dump */83waitForBarrier(readyBarrier);8485/* Released when the thread dump has been taken */86waitForBarrier(doneBarrier);8788lock.unlock();89}90}9192public void run(CommandExecutor executor) {93MonitorThread mThread = new MonitorThread();94mThread.start();95LockThread lThread = new LockThread();96lThread.start();9798/* Wait for threads to get ready */99waitForBarrier(readyBarrier);100101/* Execute */102OutputAnalyzer output = executor.execute("Thread.print" + (jucLocks ? " -l=true" : ""));103104/* Signal that we've got the thread dump */105waitForBarrier(doneBarrier);106107/*108* Example output (trimmed) with arrows indicating the rows we are looking for:109*110* ...111* "Thread-2" #24 prio=5 os_prio=0 tid=0x00007f913411f800 nid=0x4fc9 waiting on condition [0x00007f91fbffe000]112* java.lang.Thread.State: WAITING (parking)113* at sun.misc.Unsafe.park(Native Method)114* - parking to wait for <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)115* at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)116* at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)117* at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)118* at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)119* at Print.waitForBarrier(Print.java:26)120* at Print.access$000(Print.java:18)121* at Print$LockThread.run(Print.java:58)122*123* --> Locked ownable synchronizers:124* --> - <0x000000071a294930> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)125*126* "Thread-1" #23 prio=5 os_prio=0 tid=0x00007f913411e800 nid=0x4fc8 waiting on condition [0x00007f9200113000]127* java.lang.Thread.State: WAITING (parking)128* at sun.misc.Unsafe.park(Native Method)129* - parking to wait for <0x000000071a0868a8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)130* at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)131* at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)132* at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234)133* at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)134* at Print.waitForBarrier(Print.java:26)135* at Print.access$000(Print.java:18)136* at Print$MonitorThread.run(Print.java:42)137* --> - locked <0x000000071a294390> (a java.lang.Object)138*139* Locked ownable synchronizers:140* - None141*142* "MainThread" #22 prio=5 os_prio=0 tid=0x00007f923015b000 nid=0x4fc7 in Object.wait() [0x00007f9200840000]143* java.lang.Thread.State: WAITING (on object monitor)144* at java.lang.Object.wait(Native Method)145* - waiting on <0x000000071a70ad98> (a java.lang.UNIXProcess)146* at java.lang.Object.wait(Object.java:502)147* at java.lang.UNIXProcess.waitFor(UNIXProcess.java:397)148* - locked <0x000000071a70ad98> (a java.lang.UNIXProcess)149* at jdk.test.lib.dcmd.JcmdExecutor.executeImpl(JcmdExecutor.java:32)150* at jdk.test.lib.dcmd.CommandExecutor.execute(CommandExecutor.java:24)151* --> at Print.run(Print.java:74)152* at Print.file(Print.java:112)153* ...154155*/156output.shouldMatch(".*at " + Pattern.quote(PrintTest.class.getName()) + "\\.run.*");157output.shouldMatch(".*- locked <0x\\p{XDigit}+> \\(a " + Pattern.quote(mThread.lock.getClass().getName()) + "\\).*");158159String jucLockPattern1 = ".*Locked ownable synchronizers:.*";160String jucLockPattern2 = ".*- <0x\\p{XDigit}+> \\(a " + Pattern.quote(lThread.lock.getClass().getName()) + ".*";161162if (jucLocks) {163output.shouldMatch(jucLockPattern1);164output.shouldMatch(jucLockPattern2);165} else {166output.shouldNotMatch(jucLockPattern1);167output.shouldNotMatch(jucLockPattern2);168}169}170171@Test172public void jmx() {173run(new JMXExecutor());174}175}176177178