1 /* 2 * Copyright (c) 2005, 2012, 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 * @bug 5086470 6358247 7193302 27 * @summary Test type conversion when invoking ThreadMXBean.dumpAllThreads 28 * through proxy. 29 * 30 * @author Mandy Chung 31 * 32 * @run main ThreadMXBeanProxy 33 */ 34 35 import static java.lang.management.ManagementFactory.*; 36 import java.lang.management.*; 37 import java.util.*; 38 import java.util.concurrent.locks.*; 39 import java.util.concurrent.TimeUnit; 40 import java.io.*; 41 import javax.management.*; 42 43 public class ThreadMXBeanProxy { 44 private static MBeanServer server = 45 ManagementFactory.getPlatformMBeanServer(); 46 private static ThreadMXBean mbean; 47 static Mutex mutex = new Mutex(); 48 static Object lock = new Object(); 49 static MyThread thread = new MyThread(); 50 public static void main(String[] argv) throws Exception { 51 mbean = newPlatformMXBeanProxy(server, 52 THREAD_MXBEAN_NAME, 53 ThreadMXBean.class); 54 55 if (!mbean.isSynchronizerUsageSupported()) { 56 System.out.println("Monitoring of synchronizer usage not supported"); 57 return; 58 } 59 60 thread.setDaemon(true); 61 thread.start(); 62 63 // wait until myThread acquires mutex and lock owner is set. 64 while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) { 65 try { 66 Thread.sleep(100); 67 } catch (InterruptedException e) { 68 throw new RuntimeException(e); 69 } 70 } 71 72 long[] ids = new long[] { thread.getId() }; 73 74 // validate the local access 75 ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true); 76 if (infos.length != 1) { 77 throw new RuntimeException("Returned ThreadInfo[] of length=" + 78 infos.length + ". Expected to be 1."); 79 } 80 thread.checkThreadInfo(infos[0]); 81 82 // validate the remote access 83 infos = mbean.getThreadInfo(ids, true, true); 84 if (infos.length != 1) { 85 throw new RuntimeException("Returned ThreadInfo[] of length=" + 86 infos.length + ". Expected to be 1."); 87 } 88 thread.checkThreadInfo(infos[0]); 89 90 boolean found = false; 91 infos = mbean.dumpAllThreads(true, true); 92 for (ThreadInfo ti : infos) { 93 if (ti.getThreadId() == thread.getId()) { 94 thread.checkThreadInfo(ti); 95 found = true; 96 } 97 } 98 99 if (!found) { 100 throw new RuntimeException("No ThreadInfo found for MyThread"); 101 } 102 103 System.out.println("Test passed"); 104 } 105 106 static class MyThread extends Thread { 107 public MyThread() { 108 super("MyThread"); 109 } 110 public void run() { 111 synchronized (lock) { 112 mutex.lock(); 113 Object o = new Object(); 114 synchronized(o) { 115 try { 116 o.wait(); 117 } catch (InterruptedException e) { 118 throw new RuntimeException(e); 119 } 120 } 121 } 122 } 123 124 int OWNED_MONITORS = 1; 125 int OWNED_SYNCS = 1; 126 void checkThreadInfo(ThreadInfo info) { 127 if (!getName().equals(info.getThreadName())) { 128 throw new RuntimeException("Name: " + info.getThreadName() + 129 " not matched. Expected: " + getName()); 130 } 131 132 MonitorInfo[] monitors = info.getLockedMonitors(); 133 if (monitors.length != OWNED_MONITORS) { 134 throw new RuntimeException("Number of locked monitors = " + 135 monitors.length + 136 " not matched. Expected: " + OWNED_MONITORS); 137 } 138 MonitorInfo m = monitors[0]; 139 StackTraceElement ste = m.getLockedStackFrame(); 140 int depth = m.getLockedStackDepth(); 141 StackTraceElement[] stacktrace = info.getStackTrace(); 142 if (!ste.equals(stacktrace[depth])) { 143 System.out.println("LockedStackFrame:- " + ste); 144 System.out.println("StackTrace at " + depth + " :-" + 145 stacktrace[depth]); 146 throw new RuntimeException("LockedStackFrame does not match " + 147 "stack frame in ThreadInfo.getStackTrace"); 148 } 149 150 String className = lock.getClass().getName(); 151 int hcode = System.identityHashCode(lock); 152 if (!className.equals(m.getClassName()) || 153 hcode != m.getIdentityHashCode() || 154 !m.getLockedStackFrame().getMethodName().equals("run")) { 155 System.out.println(info); 156 throw new RuntimeException("MonitorInfo " + m + 157 " doesn't match."); 158 } 159 160 LockInfo[] syncs = info.getLockedSynchronizers(); 161 if (syncs.length != OWNED_SYNCS) { 162 throw new RuntimeException("Number of locked syncs = " + 163 syncs.length + " not matched. Expected: " + OWNED_SYNCS); 164 } 165 AbstractOwnableSynchronizer s = mutex.getSync(); 166 String lockName = s.getClass().getName(); 167 hcode = System.identityHashCode(s); 168 if (!lockName.equals(syncs[0].getClassName())) { 169 throw new RuntimeException("LockInfo : " + syncs[0] + 170 " class name not matched. Expected: " + lockName); 171 } 172 if (hcode != syncs[0].getIdentityHashCode()) { 173 throw new RuntimeException("LockInfo: " + syncs[0] + 174 " IdentityHashCode not matched. Expected: " + hcode); 175 } 176 LockInfo li = info.getLockInfo(); 177 if (li == null) { 178 throw new RuntimeException("Expected non-null LockInfo"); 179 } 180 } 181 } 182 static class Mutex implements Lock, java.io.Serializable { 183 184 // Our internal helper class 185 class Sync extends AbstractQueuedSynchronizer { 186 // Report whether in locked state 187 protected boolean isHeldExclusively() { 188 return getState() == 1; 189 } 190 191 // Acquire the lock if state is zero 192 public boolean tryAcquire(int acquires) { 193 assert acquires == 1; // Otherwise unused 194 if (compareAndSetState(0, 1)) { 195 setExclusiveOwnerThread(Thread.currentThread()); 196 return true; 197 } 198 return false; 199 } 200 201 // Release the lock by setting state to zero 202 protected boolean tryRelease(int releases) { 203 assert releases == 1; // Otherwise unused 204 if (getState() == 0) throw new IllegalMonitorStateException(); 205 setExclusiveOwnerThread(null); 206 setState(0); 207 return true; 208 } 209 210 // Provide a Condition 211 Condition newCondition() { return new ConditionObject(); } 212 213 // Deserialize properly 214 private void readObject(ObjectInputStream s) 215 throws IOException, ClassNotFoundException { 216 s.defaultReadObject(); 217 setState(0); // reset to unlocked state 218 } 219 220 protected Thread getLockOwner() { 221 return getExclusiveOwnerThread(); 222 } 223 } 224 225 // The sync object does all the hard work. We just forward to it. 226 private final Sync sync = new Sync(); 227 228 public void lock() { sync.acquire(1); } 229 public boolean tryLock() { return sync.tryAcquire(1); } 230 public void unlock() { sync.release(1); } 231 public Condition newCondition() { return sync.newCondition(); } 232 public boolean isLocked() { return sync.isHeldExclusively(); } 233 public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } 234 public void lockInterruptibly() throws InterruptedException { 235 sync.acquireInterruptibly(1); 236 } 237 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { 238 return sync.tryAcquireNanos(1, unit.toNanos(timeout)); 239 } 240 241 public Thread getLockOwner() { return sync.getLockOwner(); } 242 243 public AbstractOwnableSynchronizer getSync() { return sync; } 244 } 245 }