author | xdono |
Thu, 02 Oct 2008 19:58:32 -0700 | |
changeset 1247 | b4c26443dee5 |
parent 1012 | c3c2f8023df1 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 |
||
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 |
||
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
63 |
// wait until myThread acquires mutex and lock owner is set. |
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
64 |
while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) { |
2 | 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); |
|
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
76 |
if (infos.length != 1) { |
2 | 77 |
throw new RuntimeException("Returned ThreadInfo[] of length=" + |
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
78 |
infos.length + ". Expected to be 1."); |
2 | 79 |
} |
80 |
thread.checkThreadInfo(infos[0]); |
|
81 |
||
82 |
// validate the remote access |
|
83 |
infos = mbean.getThreadInfo(ids, true, true); |
|
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
84 |
if (infos.length != 1) { |
2 | 85 |
throw new RuntimeException("Returned ThreadInfo[] of length=" + |
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
86 |
infos.length + ". Expected to be 1."); |
2 | 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 = " + |
|
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
163 |
syncs.length + " not matched. Expected: " + OWNED_SYNCS); |
2 | 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 |
} |
|
177 |
} |
|
178 |
static class Mutex implements Lock, java.io.Serializable { |
|
179 |
||
180 |
// Our internal helper class |
|
181 |
class Sync extends AbstractQueuedSynchronizer { |
|
182 |
// Report whether in locked state |
|
183 |
protected boolean isHeldExclusively() { |
|
184 |
return getState() == 1; |
|
185 |
} |
|
186 |
||
187 |
// Acquire the lock if state is zero |
|
188 |
public boolean tryAcquire(int acquires) { |
|
189 |
assert acquires == 1; // Otherwise unused |
|
190 |
if (compareAndSetState(0, 1)) { |
|
191 |
setExclusiveOwnerThread(Thread.currentThread()); |
|
192 |
return true; |
|
193 |
} |
|
194 |
return false; |
|
195 |
} |
|
196 |
||
197 |
// Release the lock by setting state to zero |
|
198 |
protected boolean tryRelease(int releases) { |
|
199 |
assert releases == 1; // Otherwise unused |
|
200 |
if (getState() == 0) throw new IllegalMonitorStateException(); |
|
201 |
setExclusiveOwnerThread(null); |
|
202 |
setState(0); |
|
203 |
return true; |
|
204 |
} |
|
205 |
||
206 |
// Provide a Condition |
|
207 |
Condition newCondition() { return new ConditionObject(); } |
|
208 |
||
209 |
// Deserialize properly |
|
210 |
private void readObject(ObjectInputStream s) |
|
211 |
throws IOException, ClassNotFoundException { |
|
212 |
s.defaultReadObject(); |
|
213 |
setState(0); // reset to unlocked state |
|
214 |
} |
|
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
215 |
|
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
216 |
protected Thread getLockOwner() { |
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
217 |
return getExclusiveOwnerThread(); |
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
218 |
} |
2 | 219 |
} |
220 |
||
221 |
// The sync object does all the hard work. We just forward to it. |
|
222 |
private final Sync sync = new Sync(); |
|
223 |
||
224 |
public void lock() { sync.acquire(1); } |
|
225 |
public boolean tryLock() { return sync.tryAcquire(1); } |
|
226 |
public void unlock() { sync.release(1); } |
|
227 |
public Condition newCondition() { return sync.newCondition(); } |
|
228 |
public boolean isLocked() { return sync.isHeldExclusively(); } |
|
229 |
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } |
|
230 |
public void lockInterruptibly() throws InterruptedException { |
|
231 |
sync.acquireInterruptibly(1); |
|
232 |
} |
|
233 |
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { |
|
234 |
return sync.tryAcquireNanos(1, unit.toNanos(timeout)); |
|
235 |
} |
|
236 |
||
1012
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
237 |
public Thread getLockOwner() { return sync.getLockOwner(); } |
c3c2f8023df1
6732441: TEST_BUG: ThreadMXBeanProxy test fails intermittently.
swamyv
parents:
2
diff
changeset
|
238 |
|
2 | 239 |
public AbstractOwnableSynchronizer getSync() { return sync; } |
240 |
} |
|
241 |
} |