1 |
dl |
1.1 |
/* |
2 |
dl |
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 |
jsr166 |
1.7 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
|
|
/* |
7 |
dl |
1.1 |
* A simple test program. Feel free to play. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
jsr166 |
1.9 |
import java.util.*; |
11 |
dl |
1.1 |
import java.util.concurrent.*; |
12 |
|
|
import java.util.concurrent.locks.*; |
13 |
|
|
|
14 |
|
|
public final class LockLoops { |
15 |
|
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
16 |
|
|
static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); |
17 |
|
|
static boolean print = false; |
18 |
|
|
static boolean doBuiltin = true; |
19 |
|
|
static boolean doReadWrite = true; |
20 |
|
|
static boolean doSemaphore = true; |
21 |
jsr166 |
1.8 |
static boolean doFair = true; |
22 |
dl |
1.1 |
|
23 |
|
|
public static void main(String[] args) throws Exception { |
24 |
|
|
int maxThreads = 100; |
25 |
|
|
int iters = 1000000; |
26 |
|
|
int replications = 1; |
27 |
|
|
|
28 |
jsr166 |
1.3 |
if (args.length > 0) |
29 |
dl |
1.1 |
maxThreads = Integer.parseInt(args[0]); |
30 |
|
|
|
31 |
jsr166 |
1.3 |
if (args.length > 1) |
32 |
dl |
1.1 |
iters = Integer.parseInt(args[1]); |
33 |
|
|
|
34 |
jsr166 |
1.3 |
if (args.length > 2) |
35 |
dl |
1.1 |
replications = Integer.parseInt(args[2]); |
36 |
|
|
|
37 |
|
|
rng.setSeed(3122688L); |
38 |
|
|
|
39 |
|
|
print = false; |
40 |
|
|
System.out.println("Warmup..."); |
41 |
|
|
oneTest(3, 10000); |
42 |
|
|
Thread.sleep(1000); |
43 |
|
|
oneTest(2, 10000); |
44 |
|
|
Thread.sleep(100); |
45 |
|
|
oneTest(1, 100000); |
46 |
|
|
Thread.sleep(100); |
47 |
|
|
oneTest(1, 100000); |
48 |
|
|
Thread.sleep(1000); |
49 |
|
|
print = true; |
50 |
|
|
|
51 |
|
|
for (int i = 1; i <= maxThreads; ++i) { |
52 |
|
|
for (int j = 0; j < replications; ++j) { |
53 |
|
|
System.out.println("Threads:" + i); |
54 |
|
|
oneTest(i, iters / i); |
55 |
|
|
Thread.sleep(100); |
56 |
|
|
} |
57 |
|
|
} |
58 |
|
|
pool.shutdown(); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
static void oneTest(int nthreads, int iters) throws Exception { |
62 |
|
|
int v = rng.next(); |
63 |
|
|
|
64 |
|
|
if (print) |
65 |
|
|
System.out.print("No shared vars "); |
66 |
|
|
new NoLockLoop().test(v, nthreads, iters * 10); |
67 |
|
|
Thread.sleep(10); |
68 |
|
|
|
69 |
|
|
if (print) |
70 |
|
|
System.out.print("No Lock + volatile "); |
71 |
|
|
new NoLockVolatileLoop().test(v, nthreads, iters); |
72 |
|
|
Thread.sleep(10); |
73 |
|
|
|
74 |
|
|
if (doBuiltin) { |
75 |
|
|
if (print) |
76 |
|
|
System.out.print("builtin lock "); |
77 |
|
|
new BuiltinLockLoop().test(v, nthreads, iters); |
78 |
|
|
Thread.sleep(10); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
if (print) |
82 |
|
|
System.out.print("ReentrantLock "); |
83 |
|
|
new ReentrantLockLoop().test(v, nthreads, iters); |
84 |
|
|
Thread.sleep(10); |
85 |
|
|
|
86 |
|
|
if (doReadWrite) { |
87 |
|
|
if (print) |
88 |
|
|
System.out.print("ReentrantWriteLock "); |
89 |
|
|
new ReentrantWriteLockLoop().test(v, nthreads, iters); |
90 |
|
|
Thread.sleep(10); |
91 |
|
|
|
92 |
|
|
if (print) |
93 |
|
|
System.out.print("ReentrantReadWriteLock"); |
94 |
|
|
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
95 |
|
|
Thread.sleep(10); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
if (doSemaphore) { |
99 |
|
|
if (print) |
100 |
|
|
System.out.print("Semaphore "); |
101 |
|
|
new SemaphoreLoop().test(v, nthreads, iters); |
102 |
|
|
Thread.sleep(10); |
103 |
|
|
|
104 |
|
|
if (print) |
105 |
|
|
System.out.print("FairSemaphore "); |
106 |
|
|
new FairSemaphoreLoop().test(v, nthreads, iters); |
107 |
|
|
Thread.sleep(10); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
if (doFair) { |
111 |
|
|
if (print) |
112 |
|
|
System.out.print("FairReentrantLock "); |
113 |
|
|
new FairReentrantLockLoop().test(v, nthreads, iters); |
114 |
|
|
Thread.sleep(10); |
115 |
|
|
|
116 |
|
|
if (doReadWrite) { |
117 |
|
|
if (print) |
118 |
|
|
System.out.print("FairRWriteLock "); |
119 |
|
|
new FairReentrantWriteLockLoop().test(v, nthreads, iters); |
120 |
|
|
Thread.sleep(10); |
121 |
|
|
|
122 |
|
|
if (print) |
123 |
|
|
System.out.print("FairRReadWriteLock "); |
124 |
|
|
new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); |
125 |
|
|
Thread.sleep(10); |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
jsr166 |
1.6 |
abstract static class LockLoop implements Runnable { |
131 |
dl |
1.1 |
int v; |
132 |
|
|
int iters; |
133 |
|
|
volatile int result; |
134 |
|
|
final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
135 |
|
|
CyclicBarrier barrier; |
136 |
|
|
|
137 |
|
|
final void test(int initialValue, int nthreads, int iters) throws Exception { |
138 |
|
|
v = initialValue; |
139 |
|
|
this.iters = iters; |
140 |
|
|
barrier = new CyclicBarrier(nthreads+1, timer); |
141 |
jsr166 |
1.3 |
for (int i = 0; i < nthreads; ++i) |
142 |
dl |
1.1 |
pool.execute(this); |
143 |
|
|
barrier.await(); |
144 |
|
|
barrier.await(); |
145 |
|
|
long time = timer.getTime(); |
146 |
|
|
if (print) { |
147 |
|
|
long tpi = time / (iters * nthreads); |
148 |
|
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
149 |
jsr166 |
1.4 |
// double secs = (double) time / 1000000000.0; |
150 |
dl |
1.1 |
// System.out.print("\t " + secs + "s run time"); |
151 |
|
|
System.out.println(); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
if (result == 0) // avoid overoptimization |
155 |
|
|
System.out.println("useless result: " + result); |
156 |
|
|
} |
157 |
|
|
abstract int loop(int n); |
158 |
|
|
public final void run() { |
159 |
|
|
try { |
160 |
jsr166 |
1.3 |
barrier.await(); |
161 |
dl |
1.1 |
result += loop(iters); |
162 |
|
|
barrier.await(); |
163 |
|
|
} |
164 |
jsr166 |
1.3 |
catch (Exception ie) { |
165 |
|
|
return; |
166 |
dl |
1.1 |
} |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
private static class BuiltinLockLoop extends LockLoop { |
172 |
|
|
final int loop(int n) { |
173 |
|
|
int sum = 0; |
174 |
|
|
while (n-- > 0) { |
175 |
jsr166 |
1.5 |
synchronized (this) { |
176 |
dl |
1.1 |
v = LoopHelpers.compute1(v); |
177 |
|
|
} |
178 |
|
|
sum += LoopHelpers.compute2(v); |
179 |
|
|
} |
180 |
|
|
return sum; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
private static class NoLockLoop extends LockLoop { |
185 |
|
|
final int loop(int n) { |
186 |
|
|
int sum = 0; |
187 |
|
|
int y = v; |
188 |
|
|
while (n-- > 0) { |
189 |
|
|
y = LoopHelpers.compute1(y); |
190 |
|
|
sum += LoopHelpers.compute2(y); |
191 |
|
|
} |
192 |
|
|
return sum; |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
private static class NoLockVolatileLoop extends LockLoop { |
197 |
jsr166 |
1.6 |
private volatile int vv; |
198 |
dl |
1.1 |
final int loop(int n) { |
199 |
|
|
int sum = 0; |
200 |
|
|
while (n-- > 0) { |
201 |
|
|
int y = LoopHelpers.compute1(vv); |
202 |
|
|
vv = y; |
203 |
|
|
sum += LoopHelpers.compute2(y); |
204 |
|
|
} |
205 |
|
|
return sum; |
206 |
|
|
} |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
private static class ReentrantLockLoop extends LockLoop { |
210 |
jsr166 |
1.6 |
private final ReentrantLock lock = new ReentrantLock(); |
211 |
dl |
1.1 |
final int loop(int n) { |
212 |
|
|
int sum = 0; |
213 |
|
|
while (n-- > 0) { |
214 |
|
|
lock.lock(); |
215 |
|
|
try { |
216 |
|
|
v = LoopHelpers.compute1(v); |
217 |
|
|
} |
218 |
|
|
finally { |
219 |
|
|
lock.unlock(); |
220 |
|
|
} |
221 |
|
|
sum += LoopHelpers.compute2(v); |
222 |
|
|
} |
223 |
|
|
return sum; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
private static class FairReentrantLockLoop extends LockLoop { |
228 |
jsr166 |
1.6 |
private final ReentrantLock lock = new ReentrantLock(true); |
229 |
dl |
1.1 |
final int loop(int n) { |
230 |
|
|
int sum = 0; |
231 |
|
|
while (n-- > 0) { |
232 |
|
|
lock.lock(); |
233 |
|
|
try { |
234 |
|
|
v = LoopHelpers.compute1(v); |
235 |
|
|
} |
236 |
|
|
finally { |
237 |
|
|
lock.unlock(); |
238 |
|
|
} |
239 |
|
|
sum += LoopHelpers.compute2(v); |
240 |
|
|
} |
241 |
|
|
return sum; |
242 |
|
|
} |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
private static class ReentrantWriteLockLoop extends LockLoop { |
246 |
jsr166 |
1.6 |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
247 |
dl |
1.1 |
final int loop(int n) { |
248 |
|
|
int sum = 0; |
249 |
|
|
while (n-- > 0) { |
250 |
|
|
lock.lock(); |
251 |
|
|
try { |
252 |
|
|
v = LoopHelpers.compute1(v); |
253 |
|
|
} |
254 |
|
|
finally { |
255 |
|
|
lock.unlock(); |
256 |
|
|
} |
257 |
|
|
sum += LoopHelpers.compute2(v); |
258 |
|
|
} |
259 |
|
|
return sum; |
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
private static class ReentrantReadWriteLockLoop extends LockLoop { |
264 |
jsr166 |
1.6 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
265 |
dl |
1.1 |
final int loop(int n) { |
266 |
|
|
int sum = 0; |
267 |
|
|
while (n-- > 0) { |
268 |
|
|
int x; |
269 |
|
|
lock.readLock().lock(); |
270 |
|
|
try { |
271 |
|
|
x = LoopHelpers.compute1(v); |
272 |
|
|
} |
273 |
|
|
finally { |
274 |
|
|
lock.readLock().unlock(); |
275 |
|
|
} |
276 |
|
|
lock.writeLock().lock(); |
277 |
|
|
try { |
278 |
|
|
v = x; |
279 |
|
|
} |
280 |
|
|
finally { |
281 |
|
|
lock.writeLock().unlock(); |
282 |
|
|
} |
283 |
|
|
sum += LoopHelpers.compute2(v); |
284 |
|
|
} |
285 |
|
|
return sum; |
286 |
|
|
} |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
private static class FairReentrantWriteLockLoop extends LockLoop { |
290 |
|
|
final Lock lock = new ReentrantReadWriteLock(true).writeLock(); |
291 |
|
|
final int loop(int n) { |
292 |
|
|
int sum = 0; |
293 |
|
|
while (n-- > 0) { |
294 |
|
|
lock.lock(); |
295 |
|
|
try { |
296 |
|
|
v = LoopHelpers.compute1(v); |
297 |
|
|
} |
298 |
|
|
finally { |
299 |
|
|
lock.unlock(); |
300 |
|
|
} |
301 |
|
|
sum += LoopHelpers.compute2(v); |
302 |
|
|
} |
303 |
|
|
return sum; |
304 |
|
|
} |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
private static class SemaphoreLoop extends LockLoop { |
308 |
jsr166 |
1.6 |
private final Semaphore sem = new Semaphore(1, false); |
309 |
dl |
1.1 |
final int loop(int n) { |
310 |
|
|
int sum = 0; |
311 |
|
|
try { |
312 |
|
|
while (n-- > 0) { |
313 |
|
|
sem.acquire(); |
314 |
|
|
try { |
315 |
|
|
v = LoopHelpers.compute1(v); |
316 |
|
|
} |
317 |
|
|
finally { |
318 |
|
|
sem.release(); |
319 |
|
|
} |
320 |
|
|
sum += LoopHelpers.compute2(v); |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
catch (InterruptedException ie) { |
324 |
|
|
return sum; |
325 |
|
|
} |
326 |
|
|
return sum; |
327 |
|
|
} |
328 |
|
|
} |
329 |
|
|
private static class FairSemaphoreLoop extends LockLoop { |
330 |
jsr166 |
1.6 |
private final Semaphore sem = new Semaphore(1, true); |
331 |
dl |
1.1 |
final int loop(int n) { |
332 |
|
|
int sum = 0; |
333 |
|
|
try { |
334 |
|
|
while (n-- > 0) { |
335 |
|
|
sem.acquire(); |
336 |
|
|
try { |
337 |
|
|
v = LoopHelpers.compute1(v); |
338 |
|
|
} |
339 |
|
|
finally { |
340 |
|
|
sem.release(); |
341 |
|
|
} |
342 |
|
|
sum += LoopHelpers.compute2(v); |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
catch (InterruptedException ie) { |
346 |
|
|
return sum; |
347 |
|
|
} |
348 |
|
|
return sum; |
349 |
|
|
} |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
353 |
jsr166 |
1.6 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
354 |
dl |
1.1 |
final int loop(int n) { |
355 |
|
|
int sum = 0; |
356 |
|
|
while (n-- > 0) { |
357 |
|
|
int x; |
358 |
|
|
lock.readLock().lock(); |
359 |
|
|
try { |
360 |
|
|
x = LoopHelpers.compute1(v); |
361 |
|
|
} |
362 |
|
|
finally { |
363 |
|
|
lock.readLock().unlock(); |
364 |
|
|
} |
365 |
|
|
lock.writeLock().lock(); |
366 |
|
|
try { |
367 |
|
|
v = x; |
368 |
|
|
} |
369 |
|
|
finally { |
370 |
|
|
lock.writeLock().unlock(); |
371 |
|
|
} |
372 |
|
|
sum += LoopHelpers.compute2(v); |
373 |
|
|
} |
374 |
|
|
return sum; |
375 |
|
|
} |
376 |
|
|
} |
377 |
|
|
} |