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