1 |
// Yet another contended object monitor throughput test |
2 |
// adapted from bug reports |
3 |
|
4 |
import java.util.*; |
5 |
import java.lang.*; |
6 |
import java.util.concurrent.*; |
7 |
import java.util.concurrent.locks.*; |
8 |
|
9 |
class Producer extends Thread |
10 |
{ |
11 |
// private static Hashtable buddiesOnline = new Hashtable(); |
12 |
private static Map buddiesOnline = new ConcurrentHashMap(); |
13 |
public Producer (String name) { super(name); } |
14 |
|
15 |
public void run() |
16 |
{ |
17 |
Object key = null ; |
18 |
final ReentrantLock dr = RLJBar.DeathRow; |
19 |
final ReentrantLock bar = RLJBar.bar; |
20 |
final ReentrantLock end = RLJBar.End; |
21 |
final Condition endCondition = RLJBar.EndCondition; |
22 |
if (RLJBar.OneKey) key = new Integer(0) ; // per-thread v. per iteration |
23 |
|
24 |
// The barrier has a number of interesting effects: |
25 |
// 1. It enforces full LWP provisioning on T1. |
26 |
// (nearly all workers park concurrently). |
27 |
// 2. It gives the C2 compiler thread(s) a chance to run. |
28 |
// By transiently quiescing the workings the C2 threads |
29 |
// might avoid starvation. |
30 |
// |
31 |
|
32 |
try { |
33 |
bar.lock(); |
34 |
try { |
35 |
++RLJBar.nUp ; |
36 |
if (RLJBar.nUp == RLJBar.nThreads) { |
37 |
if (RLJBar.quiesce != 0) { |
38 |
RLJBar.barCondition.awaitNanos(RLJBar.quiesce * 1000000) ; |
39 |
} |
40 |
RLJBar.epoch = System.currentTimeMillis () ; |
41 |
RLJBar.barCondition.signalAll () ; |
42 |
// System.out.print ("Consensus ") ; |
43 |
} |
44 |
if (RLJBar.UseBar) { |
45 |
while (RLJBar.nUp != RLJBar.nThreads) { |
46 |
RLJBar.barCondition.await () ; |
47 |
} |
48 |
} |
49 |
} |
50 |
finally { |
51 |
bar.unlock(); |
52 |
} |
53 |
} catch (Exception ex) { |
54 |
System.out.println ("Exception in barrier: " + ex) ; |
55 |
} |
56 |
|
57 |
// Main execution time ... the code being timed ... |
58 |
// HashTable.get() is highly contended (serial). |
59 |
for (int loop = 1; loop < 100000 ;loop++) { |
60 |
if (!RLJBar.OneKey) key = new Integer(0) ; |
61 |
buddiesOnline.get(key); |
62 |
} |
63 |
|
64 |
// Mutator epilog: |
65 |
// The following code determines if the test will/wont include (measure) |
66 |
// thread death time. |
67 |
|
68 |
end.lock(); |
69 |
try { |
70 |
++RLJBar.nDead ; |
71 |
if (RLJBar.nDead == RLJBar.nUp) { |
72 |
// System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms") ; |
73 |
endCondition.signalAll() ; |
74 |
} |
75 |
} |
76 |
finally { |
77 |
end.unlock(); |
78 |
} |
79 |
dr.lock(); |
80 |
dr.unlock(); |
81 |
} |
82 |
} |
83 |
|
84 |
|
85 |
public class RLJBar // ProdConsTest |
86 |
{ |
87 |
|
88 |
public static final int ITERS = 10; |
89 |
public static boolean OneKey = false ; // alloc once or once per iteration |
90 |
|
91 |
public static boolean UseBar = false ; |
92 |
public static int nThreads = 100 ; |
93 |
public static int nUp = 0 ; |
94 |
public static int nDead = 0 ; |
95 |
public static ReentrantLock bar = new ReentrantLock() ; |
96 |
public static Condition barCondition = bar.newCondition() ; |
97 |
public static long epoch ; |
98 |
public static ReentrantLock DeathRow = new ReentrantLock () ; |
99 |
public static ReentrantLock End = new ReentrantLock () ; |
100 |
public static int quiesce = 0 ; |
101 |
public static Condition EndCondition = End.newCondition(); |
102 |
|
103 |
public static void main (String[] args) { |
104 |
int argix = 0 ; |
105 |
if (argix < args.length && args[argix].equals("-o")) { |
106 |
++argix ; |
107 |
OneKey = true ; |
108 |
System.out.println ("OneKey") ; |
109 |
} |
110 |
if (argix < args.length && args[argix].equals ("-b")) { |
111 |
++argix ; |
112 |
UseBar = true ; |
113 |
System.out.println ("UseBar") ; |
114 |
} |
115 |
if (argix < args.length && args[argix].equals ("-q")) { |
116 |
++argix ; |
117 |
if (argix < args.length) { |
118 |
quiesce = Integer.parseInt (args[argix++]) ; |
119 |
System.out.println ("Quiesce " + quiesce + " msecs") ; |
120 |
} |
121 |
} |
122 |
for (int k = 0; k < ITERS; ++k) |
123 |
oneRun(); |
124 |
} |
125 |
|
126 |
public static void oneRun() { |
127 |
DeathRow = new ReentrantLock () ; |
128 |
End = new ReentrantLock () ; |
129 |
EndCondition = End.newCondition(); |
130 |
|
131 |
nDead = nUp = 0 ; |
132 |
long cyBase = System.currentTimeMillis () ; |
133 |
DeathRow.lock(); |
134 |
try { |
135 |
for (int i = 1; i <= nThreads ; i++) { |
136 |
new Producer("Producer" + i).start(); |
137 |
} |
138 |
try { |
139 |
End.lock(); |
140 |
try { |
141 |
while (nDead != nThreads) |
142 |
EndCondition.await() ; |
143 |
} |
144 |
finally { |
145 |
End.unlock(); |
146 |
} |
147 |
} catch (Exception ex) { |
148 |
System.out.println ("Exception in End: " + ex) ; |
149 |
} |
150 |
} |
151 |
finally { |
152 |
DeathRow.unlock(); |
153 |
} |
154 |
System.out.println ("Outer time: " + (System.currentTimeMillis()-cyBase)) ; |
155 |
|
156 |
// Let workers quiesce/exit. |
157 |
try { Thread.sleep (1000) ; } catch (Exception ex) {} ; |
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
|