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 |
} |
142 |
|
143 |
abstract static class LockLoop implements Runnable { |
144 |
int v; |
145 |
int iters; |
146 |
volatile int result; |
147 |
final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
148 |
CyclicBarrier barrier; |
149 |
|
150 |
final void test(int initialValue, int nthreads, int iters) throws Exception { |
151 |
v = initialValue; |
152 |
this.iters = iters; |
153 |
barrier = new CyclicBarrier(nthreads+1, timer); |
154 |
for (int i = 0; i < nthreads; ++i) |
155 |
pool.execute(this); |
156 |
barrier.await(); |
157 |
barrier.await(); |
158 |
long time = timer.getTime(); |
159 |
if (print) { |
160 |
long tpi = time / (iters * nthreads); |
161 |
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update"); |
162 |
// double secs = (double) time / 1000000000.0; |
163 |
// System.out.print("\t " + secs + "s run time"); |
164 |
System.out.println(); |
165 |
} |
166 |
|
167 |
if (result == 0) // avoid overoptimization |
168 |
System.out.println("useless result: " + result); |
169 |
} |
170 |
abstract int loop(int n); |
171 |
public final void run() { |
172 |
try { |
173 |
barrier.await(); |
174 |
result += loop(iters); |
175 |
barrier.await(); |
176 |
} |
177 |
catch (Exception ie) { |
178 |
return; |
179 |
} |
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
private static class BuiltinLockLoop extends LockLoop { |
185 |
final int loop(int n) { |
186 |
int sum = 0; |
187 |
while (n-- > 0) { |
188 |
synchronized (this) { |
189 |
v = LoopHelpers.compute1(v); |
190 |
} |
191 |
sum += LoopHelpers.compute2(v); |
192 |
} |
193 |
return sum; |
194 |
} |
195 |
} |
196 |
|
197 |
private static class NoLockLoop extends LockLoop { |
198 |
final int loop(int n) { |
199 |
int sum = 0; |
200 |
int y = v; |
201 |
while (n-- > 0) { |
202 |
y = LoopHelpers.compute1(y); |
203 |
sum += LoopHelpers.compute2(y); |
204 |
} |
205 |
return sum; |
206 |
} |
207 |
} |
208 |
|
209 |
private static class NoLockVolatileLoop extends LockLoop { |
210 |
private volatile int vv; |
211 |
final int loop(int n) { |
212 |
int sum = 0; |
213 |
while (n-- > 0) { |
214 |
int y = LoopHelpers.compute1(vv); |
215 |
vv = y; |
216 |
sum += LoopHelpers.compute2(y); |
217 |
} |
218 |
return sum; |
219 |
} |
220 |
} |
221 |
|
222 |
private static class ReentrantLockLoop extends LockLoop { |
223 |
private final ReentrantLock lock = new ReentrantLock(); |
224 |
final int loop(int n) { |
225 |
int sum = 0; |
226 |
while (n-- > 0) { |
227 |
lock.lock(); |
228 |
try { |
229 |
v = LoopHelpers.compute1(v); |
230 |
} |
231 |
finally { |
232 |
lock.unlock(); |
233 |
} |
234 |
sum += LoopHelpers.compute2(v); |
235 |
} |
236 |
return sum; |
237 |
} |
238 |
} |
239 |
|
240 |
private static class FairReentrantLockLoop extends LockLoop { |
241 |
private final ReentrantLock lock = new ReentrantLock(true); |
242 |
final int loop(int n) { |
243 |
int sum = 0; |
244 |
while (n-- > 0) { |
245 |
lock.lock(); |
246 |
try { |
247 |
v = LoopHelpers.compute1(v); |
248 |
} |
249 |
finally { |
250 |
lock.unlock(); |
251 |
} |
252 |
sum += LoopHelpers.compute2(v); |
253 |
} |
254 |
return sum; |
255 |
} |
256 |
} |
257 |
|
258 |
private static class ReentrantWriteLockLoop extends LockLoop { |
259 |
private final Lock lock = new ReentrantReadWriteLock().writeLock(); |
260 |
final int loop(int n) { |
261 |
int sum = 0; |
262 |
while (n-- > 0) { |
263 |
lock.lock(); |
264 |
try { |
265 |
v = LoopHelpers.compute1(v); |
266 |
} |
267 |
finally { |
268 |
lock.unlock(); |
269 |
} |
270 |
sum += LoopHelpers.compute2(v); |
271 |
} |
272 |
return sum; |
273 |
} |
274 |
} |
275 |
|
276 |
private static class ReentrantReadWriteLockLoop extends LockLoop { |
277 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
278 |
final int loop(int n) { |
279 |
int sum = 0; |
280 |
while (n-- > 0) { |
281 |
int x; |
282 |
lock.readLock().lock(); |
283 |
try { |
284 |
x = LoopHelpers.compute1(v); |
285 |
} |
286 |
finally { |
287 |
lock.readLock().unlock(); |
288 |
} |
289 |
lock.writeLock().lock(); |
290 |
try { |
291 |
v = x; |
292 |
} |
293 |
finally { |
294 |
lock.writeLock().unlock(); |
295 |
} |
296 |
sum += LoopHelpers.compute2(v); |
297 |
} |
298 |
return sum; |
299 |
} |
300 |
} |
301 |
|
302 |
private static class FairReentrantWriteLockLoop extends LockLoop { |
303 |
final Lock lock = new ReentrantReadWriteLock(true).writeLock(); |
304 |
final int loop(int n) { |
305 |
int sum = 0; |
306 |
while (n-- > 0) { |
307 |
lock.lock(); |
308 |
try { |
309 |
v = LoopHelpers.compute1(v); |
310 |
} |
311 |
finally { |
312 |
lock.unlock(); |
313 |
} |
314 |
sum += LoopHelpers.compute2(v); |
315 |
} |
316 |
return sum; |
317 |
} |
318 |
} |
319 |
|
320 |
private static class SemaphoreLoop extends LockLoop { |
321 |
private final Semaphore sem = new Semaphore(1, false); |
322 |
final int loop(int n) { |
323 |
int sum = 0; |
324 |
try { |
325 |
while (n-- > 0) { |
326 |
sem.acquire(); |
327 |
try { |
328 |
v = LoopHelpers.compute1(v); |
329 |
} |
330 |
finally { |
331 |
sem.release(); |
332 |
} |
333 |
sum += LoopHelpers.compute2(v); |
334 |
} |
335 |
} |
336 |
catch (InterruptedException ie) { |
337 |
return sum; |
338 |
} |
339 |
return sum; |
340 |
} |
341 |
} |
342 |
|
343 |
private static class FairSemaphoreLoop extends LockLoop { |
344 |
private final Semaphore sem = new Semaphore(1, true); |
345 |
final int loop(int n) { |
346 |
int sum = 0; |
347 |
try { |
348 |
while (n-- > 0) { |
349 |
sem.acquire(); |
350 |
try { |
351 |
v = LoopHelpers.compute1(v); |
352 |
} |
353 |
finally { |
354 |
sem.release(); |
355 |
} |
356 |
sum += LoopHelpers.compute2(v); |
357 |
} |
358 |
} |
359 |
catch (InterruptedException ie) { |
360 |
return sum; |
361 |
} |
362 |
return sum; |
363 |
} |
364 |
} |
365 |
|
366 |
private static class FairReentrantReadWriteLockLoop extends LockLoop { |
367 |
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
368 |
final int loop(int n) { |
369 |
int sum = 0; |
370 |
while (n-- > 0) { |
371 |
int x; |
372 |
lock.readLock().lock(); |
373 |
try { |
374 |
x = LoopHelpers.compute1(v); |
375 |
} |
376 |
finally { |
377 |
lock.readLock().unlock(); |
378 |
} |
379 |
lock.writeLock().lock(); |
380 |
try { |
381 |
v = x; |
382 |
} |
383 |
finally { |
384 |
lock.writeLock().unlock(); |
385 |
} |
386 |
sum += LoopHelpers.compute2(v); |
387 |
} |
388 |
return sum; |
389 |
} |
390 |
} |
391 |
|
392 |
private static class StampedLockWriteLoop extends LockLoop { |
393 |
private final StampedLock lock = new StampedLock(); |
394 |
final int loop(int n) { |
395 |
int sum = 0; |
396 |
while (n-- > 0) { |
397 |
long stamp = lock.writeLock(); |
398 |
try { |
399 |
v = LoopHelpers.compute1(v); |
400 |
} |
401 |
finally { |
402 |
lock.unlockWrite(stamp); |
403 |
} |
404 |
sum += LoopHelpers.compute2(v); |
405 |
} |
406 |
return sum; |
407 |
} |
408 |
} |
409 |
|
410 |
private static class StampedLockReadLoop extends LockLoop { |
411 |
private final StampedLock lock = new StampedLock(); |
412 |
final int loop(int n) { |
413 |
int sum = 0; |
414 |
while (n-- > 0) { |
415 |
long stamp = lock.readLock(); |
416 |
try { |
417 |
v = LoopHelpers.compute1(v); |
418 |
} |
419 |
finally { |
420 |
lock.unlockRead(stamp); |
421 |
} |
422 |
sum += LoopHelpers.compute2(v); |
423 |
} |
424 |
return sum; |
425 |
} |
426 |
} |
427 |
} |