blob: f76794f407c42318b4feb5b02457178cef13aeac [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 5086470 6358247
27 * @summary Test type conversion when invoking ThreadMXBean.dumpAllThreads
28 * through proxy.
29 *
30 * @author Mandy Chung
31 *
32 * @run main ThreadMXBeanProxy
33 */
34
35import static java.lang.management.ManagementFactory.*;
36import java.lang.management.*;
37import java.util.*;
38import java.util.concurrent.locks.*;
39import java.util.concurrent.TimeUnit;
40import java.io.*;
41import javax.management.*;
42
43public 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
64 while (!mutex.isLocked()) {
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 (ids.length != 1) {
77 throw new RuntimeException("Returned ThreadInfo[] of length=" +
78 ids.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 (ids.length != 1) {
85 throw new RuntimeException("Returned ThreadInfo[] of length=" +
86 ids.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 +
164 " not matched. Expected: " + OWNED_SYNCS);
165 }
166 AbstractOwnableSynchronizer s = mutex.getSync();
167 String lockName = s.getClass().getName();
168 hcode = System.identityHashCode(s);
169 if (!lockName.equals(syncs[0].getClassName())) {
170 throw new RuntimeException("LockInfo : " + syncs[0] +
171 " class name not matched. Expected: " + lockName);
172 }
173 if (hcode != syncs[0].getIdentityHashCode()) {
174 throw new RuntimeException("LockInfo: " + syncs[0] +
175 " IdentityHashCode not matched. Expected: " + hcode);
176 }
177
178 }
179 }
180 static class Mutex implements Lock, java.io.Serializable {
181
182 // Our internal helper class
183 class Sync extends AbstractQueuedSynchronizer {
184 // Report whether in locked state
185 protected boolean isHeldExclusively() {
186 return getState() == 1;
187 }
188
189 // Acquire the lock if state is zero
190 public boolean tryAcquire(int acquires) {
191 assert acquires == 1; // Otherwise unused
192 if (compareAndSetState(0, 1)) {
193 setExclusiveOwnerThread(Thread.currentThread());
194 return true;
195 }
196 return false;
197 }
198
199 // Release the lock by setting state to zero
200 protected boolean tryRelease(int releases) {
201 assert releases == 1; // Otherwise unused
202 if (getState() == 0) throw new IllegalMonitorStateException();
203 setExclusiveOwnerThread(null);
204 setState(0);
205 return true;
206 }
207
208 // Provide a Condition
209 Condition newCondition() { return new ConditionObject(); }
210
211 // Deserialize properly
212 private void readObject(ObjectInputStream s)
213 throws IOException, ClassNotFoundException {
214 s.defaultReadObject();
215 setState(0); // reset to unlocked state
216 }
217 }
218
219 // The sync object does all the hard work. We just forward to it.
220 private final Sync sync = new Sync();
221
222 public void lock() { sync.acquire(1); }
223 public boolean tryLock() { return sync.tryAcquire(1); }
224 public void unlock() { sync.release(1); }
225 public Condition newCondition() { return sync.newCondition(); }
226 public boolean isLocked() { return sync.isHeldExclusively(); }
227 public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
228 public void lockInterruptibly() throws InterruptedException {
229 sync.acquireInterruptibly(1);
230 }
231 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
232 return sync.tryAcquireNanos(1, unit.toNanos(timeout));
233 }
234
235 public AbstractOwnableSynchronizer getSync() { return sync; }
236 }
237}