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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
/* |
7 |
* A simple test program. Feel free to play. |
8 |
*/ |
9 |
|
10 |
import java.util.*; |
11 |
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 |
static boolean doStampedLock = true; |
22 |
static boolean doFair = true; |
23 |
|
24 |
public static void main(String[] args) throws Exception { |
25 |
int maxThreads = 100; |
26 |
int iters = 1000000; |
27 |
int replications = 1; |
28 |
|
29 |
if (args.length > 0) |
30 |
maxThreads = Integer.parseInt(args[0]); |
31 |
|
32 |
if (args.length > 1) |
33 |
iters = Integer.parseInt(args[1]); |
34 |
|
35 |
if (args.length > 2) |
36 |
replications = Integer.parseInt(args[2]); |
37 |
|
38 |
rng.setSeed(3122688L); |
39 |
|
40 |
print = false; |
41 |
System.out.println("Warmup..."); |
42 |
oneTest(3, 10000); |
43 |
Thread.sleep(1000); |
44 |
oneTest(2, 10000); |
45 |
Thread.sleep(100); |
46 |
oneTest(1, 100000); |
47 |
Thread.sleep(100); |
48 |
oneTest(1, 100000); |
49 |
Thread.sleep(1000); |
50 |
print = true; |
51 |
|
52 |
for (int i = 1; i <= maxThreads; ++i) { |
53 |
for (int j = 0; j < replications; ++j) { |
54 |
System.out.println("Threads:" + i); |
55 |
oneTest(i, iters / i); |
56 |
Thread.sleep(100); |
57 |
} |
58 |
} |
59 |
pool.shutdown(); |
60 |
} |
61 |
|
62 |
static void oneTest(int nthreads, int iters) throws Exception { |
63 |
int v = rng.next(); |
64 |
|
65 |
if (print) |
66 |
System.out.print("No shared vars "); |
67 |
new NoLockLoop().test(v, nthreads, iters * 10); |
68 |
Thread.sleep(10); |
69 |
|
70 |
if (print) |
71 |
System.out.print("No Lock + volatile "); |
72 |
new NoLockVolatileLoop().test(v, nthreads, iters); |
73 |
Thread.sleep(10); |
74 |
|
75 |
if (doBuiltin) { |
76 |
if (print) |
77 |
System.out.print("builtin lock "); |
78 |
new BuiltinLockLoop().test(v, nthreads, iters); |
79 |
Thread.sleep(10); |
80 |
} |
81 |
|
82 |
if (print) |
83 |
System.out.print("ReentrantLock "); |
84 |
new ReentrantLockLoop().test(v, nthreads, iters); |
85 |
Thread.sleep(10); |
86 |
|
87 |
if (doReadWrite) { |
88 |
if (print) |
89 |
System.out.print("ReentrantWriteLock "); |
90 |
new ReentrantWriteLockLoop().test(v, nthreads, iters); |
91 |
Thread.sleep(10); |
92 |
|
93 |
if (print) |
94 |
System.out.print("ReentrantReadWriteLock"); |
95 |
new ReentrantReadWriteLockLoop().test(v, nthreads, iters); |
96 |
Thread.sleep(10); |
97 |
} |
98 |
|
99 |
if (doSemaphore) { |
100 |
if (print) |
101 |
System.out.print("Semaphore "); |
102 |
new SemaphoreLoop().test(v, nthreads, iters); |
103 |
Thread.sleep(10); |
104 |
|
105 |
if (print) |
106 |
System.out.print("FairSemaphore "); |
107 |
new FairSemaphoreLoop().test(v, nthreads, iters); |
108 |
Thread.sleep(10); |
109 |
} |
110 |
|
111 |
if (doFair) { |
112 |
if (print) |
113 |
System.out.print("FairReentrantLock "); |
114 |
new FairReentrantLockLoop().test(v, nthreads, iters); |
115 |
Thread.sleep(10); |
116 |
|
117 |
if (doReadWrite) { |
118 |
if (print) |
119 |
System.out.print("FairRWriteLock "); |
120 |
new FairReentrantWriteLockLoop().test(v, nthreads, iters); |
121 |
Thread.sleep(10); |
122 |
|
123 |
if (print) |
124 |
System.out.print("FairRReadWriteLock "); |
125 |
new FairReentrantReadWriteLockLoop().test(v, nthreads, iters); |
126 |
Thread.sleep(10); |
127 |
} |
128 |
} |
129 |
|
130 |
if (doStampedLock) { |
131 |
if (print) |
132 |
System.out.print("StampedLockWrite "); |
133 |
new StampedLockWriteLoop().test(v, nthreads, iters); |
134 |
Thread.sleep(10); |
135 |
|
136 |
if (print) |
137 |
System.out.print("StampedLockRead "); |
138 |
new StampedLockReadLoop().test(v, nthreads, iters); |
139 |
Thread.sleep(10); |
140 |
|
141 |
if (print) |
142 |
System.out.print("StampedLockReadWrite"); |
143 |
new StampedLockReadLoop().test(v, nthreads, iters); |
144 |
Thread.sleep(10); |
145 |
} |
146 |
} |
147 |
|
148 |
abstract static class LockLoop implements Runnable { |
149 |
int v; |
150 |
int iters; |
151 |
volatile int result; |
152 |
final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
153 |
CyclicBarrier barrier; |
154 |
|
155 |
final void test(int initialValue, int nthreads, int iters) throws Exception { |
156 |
v = initialValue; |
157 |
this.iters = iters; |
158 |
barrier = new CyclicBarrier(nthreads+1, timer); |
159 |
for (int i = 0; i < nthreads; ++i) |
160 |
pool.execute(this); |
161 |
barrier.await(); |
162 |
barrier.await(); |
163 |
long time = timer.getTime(); |
164 |
if (print) { |
165 |
long tpi = time / (iters * nthreads); |
166 |
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
167 |
// double secs = (double) time / 1000000000.0; |
168 |
// System.out.print("\t " + secs + "s run time"); |
169 |
System.out.println(); |
170 |
} |
171 |
|
172 |
if (result == 0) // avoid overoptimization |
173 |
System.out.println("useless result: " + result); |
174 |
} |
175 |
abstract int loop(int n); |
176 |
public final void run() { |
177 |
try { |
178 |
barrier.await(); |
179 |
result += loop(iters); |
180 |
barrier.await(); |
181 |
} |
182 |
catch (Exception ie) { |
183 |
return; |
184 |
} |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
private static class BuiltinLockLoop extends LockLoop { |
190 |
final int loop(int n) { |
191 |
int sum = 0; |
192 |
while (n-- > 0) { |
193 |
synchronized (this) { |
194 |
v = LoopHelpers.compute1(v); |
195 |
} |
196 |
sum += LoopHelpers.compute2(v); |
197 |
} |
198 |
return sum; |
199 |
} |
200 |
} |
201 |
|
202 |
private static class NoLockLoop extends LockLoop { |
203 |
final int loop(int n) { |
204 |
int sum = 0; |
205 |
int y = v; |
206 |
while (n-- > 0) { |
207 |
y = LoopHelpers.compute1(y); |
208 |
sum += LoopHelpers.compute2(y); |
209 |
} |
210 |
return sum; |
211 |
} |
212 |
} |
213 |
|
214 |
private static class NoLockVolatileLoop extends LockLoop { |
215 |
private volatile int vv; |
216 |
final int loop(int n) { |
217 |
int sum = 0; |
218 |
while (n-- > 0) { |
219 |
int y = LoopHelpers.compute1(vv); |
220 |
vv = y; |
221 |
sum += LoopHelpers.compute2(y); |
222 |
} |
223 |
return sum; |
224 |
} |
225 |
} |
226 |
|
227 |
private static class ReentrantLockLoop extends LockLoop { |
228 |
private final ReentrantLock lock = new ReentrantLock(); |
229 |
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 FairReentrantLockLoop extends LockLoop { |
246 |
private final ReentrantLock lock = new ReentrantLock(true); |
247 |
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 ReentrantWriteLockLoop extends LockLoop { |
264 |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
265 |
final int loop(int n) { |
266 |
int sum = 0; |
267 |
while (n-- > 0) { |
268 |
lock.lock(); |
269 |
try { |
270 |
v = LoopHelpers.compute1(v); |
271 |
} |
272 |
finally { |
273 |
lock.unlock(); |
274 |
} |
275 |
sum += LoopHelpers.compute2(v); |
276 |
} |
277 |
return sum; |
278 |
} |
279 |
} |
280 |
|
281 |
private static class ReentrantReadWriteLockLoop extends LockLoop { |
282 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
283 |
final int loop(int n) { |
284 |
int sum = 0; |
285 |
while (n-- > 0) { |
286 |
int x; |
287 |
lock.readLock().lock(); |
288 |
try { |
289 |
x = LoopHelpers.compute1(v); |
290 |
} |
291 |
finally { |
292 |
lock.readLock().unlock(); |
293 |
} |
294 |
lock.writeLock().lock(); |
295 |
try { |
296 |
v = x; |
297 |
} |
298 |
finally { |
299 |
lock.writeLock().unlock(); |
300 |
} |
301 |
sum += LoopHelpers.compute2(v); |
302 |
} |
303 |
return sum; |
304 |
} |
305 |
} |
306 |
|
307 |
private static class FairReentrantWriteLockLoop extends LockLoop { |
308 |
final Lock lock = new ReentrantReadWriteLock(true).writeLock(); |
309 |
final int loop(int n) { |
310 |
int sum = 0; |
311 |
while (n-- > 0) { |
312 |
lock.lock(); |
313 |
try { |
314 |
v = LoopHelpers.compute1(v); |
315 |
} |
316 |
finally { |
317 |
lock.unlock(); |
318 |
} |
319 |
sum += LoopHelpers.compute2(v); |
320 |
} |
321 |
return sum; |
322 |
} |
323 |
} |
324 |
|
325 |
private static class SemaphoreLoop extends LockLoop { |
326 |
private final Semaphore sem = new Semaphore(1, false); |
327 |
final int loop(int n) { |
328 |
int sum = 0; |
329 |
try { |
330 |
while (n-- > 0) { |
331 |
sem.acquire(); |
332 |
try { |
333 |
v = LoopHelpers.compute1(v); |
334 |
} |
335 |
finally { |
336 |
sem.release(); |
337 |
} |
338 |
sum += LoopHelpers.compute2(v); |
339 |
} |
340 |
} |
341 |
catch (InterruptedException ie) { |
342 |
return sum; |
343 |
} |
344 |
return sum; |
345 |
} |
346 |
} |
347 |
|
348 |
private static class FairSemaphoreLoop extends LockLoop { |
349 |
private final Semaphore sem = new Semaphore(1, true); |
350 |
final int loop(int n) { |
351 |
int sum = 0; |
352 |
try { |
353 |
while (n-- > 0) { |
354 |
sem.acquire(); |
355 |
try { |
356 |
v = LoopHelpers.compute1(v); |
357 |
} |
358 |
finally { |
359 |
sem.release(); |
360 |
} |
361 |
sum += LoopHelpers.compute2(v); |
362 |
} |
363 |
} |
364 |
catch (InterruptedException ie) { |
365 |
return sum; |
366 |
} |
367 |
return sum; |
368 |
} |
369 |
} |
370 |
|
371 |
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
372 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
373 |
final int loop(int n) { |
374 |
int sum = 0; |
375 |
while (n-- > 0) { |
376 |
int x; |
377 |
lock.readLock().lock(); |
378 |
try { |
379 |
x = LoopHelpers.compute1(v); |
380 |
} |
381 |
finally { |
382 |
lock.readLock().unlock(); |
383 |
} |
384 |
lock.writeLock().lock(); |
385 |
try { |
386 |
v = x; |
387 |
} |
388 |
finally { |
389 |
lock.writeLock().unlock(); |
390 |
} |
391 |
sum += LoopHelpers.compute2(v); |
392 |
} |
393 |
return sum; |
394 |
} |
395 |
} |
396 |
|
397 |
private static class StampedLockWriteLoop extends LockLoop { |
398 |
private final StampedLock lock = new StampedLock(); |
399 |
final int loop(int n) { |
400 |
int sum = 0; |
401 |
while (n-- > 0) { |
402 |
long stamp = lock.writeLock(); |
403 |
try { |
404 |
v = LoopHelpers.compute1(v); |
405 |
} |
406 |
finally { |
407 |
lock.unlockWrite(stamp); |
408 |
} |
409 |
sum += LoopHelpers.compute2(v); |
410 |
} |
411 |
return sum; |
412 |
} |
413 |
} |
414 |
|
415 |
private static class StampedLockReadLoop extends LockLoop { |
416 |
private final StampedLock lock = new StampedLock(); |
417 |
final int loop(int n) { |
418 |
int sum = 0; |
419 |
while (n-- > 0) { |
420 |
long stamp = lock.readLock(); |
421 |
try { |
422 |
v = LoopHelpers.compute1(v); |
423 |
} |
424 |
finally { |
425 |
lock.unlockRead(stamp); |
426 |
} |
427 |
sum += LoopHelpers.compute2(v); |
428 |
} |
429 |
return sum; |
430 |
} |
431 |
} |
432 |
|
433 |
private static class StampedLockReadWriteLoop extends LockLoop { |
434 |
private final StampedLock lock = new StampedLock(); |
435 |
final int loop(int n) { |
436 |
int sum = 0; |
437 |
while (n-- > 0) { |
438 |
int x; |
439 |
long stamp = lock.readLock(); |
440 |
try { |
441 |
x = LoopHelpers.compute1(v); |
442 |
} |
443 |
finally { |
444 |
lock.unlockRead(stamp); |
445 |
} |
446 |
stamp = lock.writeLock(); |
447 |
try { |
448 |
v = x; |
449 |
} finally { |
450 |
lock.unlockWrite(stamp); |
451 |
} |
452 |
sum += LoopHelpers.compute2(v); |
453 |
} |
454 |
return sum; |
455 |
} |
456 |
} |
457 |
} |