ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CheckedLockLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
1 /*
2 * @test
3 * @summary basic safety and liveness of ReentrantLocks, and other locks based on them
4 */
5
6 import java.util.concurrent.*;
7 import java.util.concurrent.locks.*;
8 import java.util.*;
9
10 public final class CheckedLockLoops {
11 static final ExecutorService pool = Executors.newCachedThreadPool();
12 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
13 static boolean print = false;
14 static boolean doBuiltin = true;
15
16 public static void main(String[] args) throws Exception {
17 int maxThreads = 100;
18 int iters = 2000000;
19 if (args.length > 0)
20 maxThreads = Integer.parseInt(args[0]);
21 rng.setSeed(3122688L);
22 warmup(iters);
23 runTest(maxThreads, iters);
24 pool.shutdown();
25 }
26
27 static void runTest(int maxThreads, int iters) throws Exception {
28 print = true;
29 int k = 1;
30 for (int i = 1; i <= maxThreads;) {
31 System.out.println("Threads:" + i);
32 oneTest(i, iters / i);
33 if (i == k) {
34 k = i << 1;
35 i = i + (i >>> 1);
36 }
37 else
38 i = k;
39 }
40 }
41
42 static void warmup(int iters) throws Exception {
43 print = false;
44 System.out.println("Warmup...");
45 oneTest(1, iters);
46 oneTest(2, iters / 2);
47 }
48
49 static void oneTest(int nthreads, int iters) throws Exception {
50 int fairIters = (nthreads <= 1)? iters : iters/20;
51 int v = rng.next();
52
53 if (print)
54 System.out.print("NoLock (1 thread) ");
55 new NoLockLoop().test(v, 1, iters * nthreads);
56 Thread.sleep(10);
57
58 if (print)
59 System.out.print("ReentrantLock ");
60 new ReentrantLockLoop().test(v, nthreads, iters);
61 Thread.sleep(10);
62
63 if (print)
64 System.out.print("FairReentrantLock ");
65 new FairReentrantLockLoop().test(v, nthreads, fairIters);
66 Thread.sleep(10);
67
68 if (doBuiltin) {
69 if (print)
70 System.out.print("builtin lock ");
71 new BuiltinLockLoop().test(v, nthreads, fairIters);
72 Thread.sleep(10);
73 }
74
75 if (print)
76 System.out.print("Mutex ");
77 new MutexLoop().test(v, nthreads, iters);
78 Thread.sleep(10);
79
80 if (print)
81 System.out.print("LongMutex ");
82 new LongMutexLoop().test(v, nthreads, iters);
83 Thread.sleep(10);
84
85 if (print)
86 System.out.print("Semaphore ");
87 new SemaphoreLoop().test(v, nthreads, iters);
88 Thread.sleep(10);
89
90 if (print)
91 System.out.print("FairSemaphore ");
92 new FairSemaphoreLoop().test(v, nthreads, fairIters);
93 Thread.sleep(10);
94
95 if (print)
96 System.out.print("ReentrantWriteLock ");
97 new ReentrantWriteLockLoop().test(v, nthreads, iters);
98 Thread.sleep(10);
99
100 if (print)
101 System.out.print("FairRWriteLock ");
102 new FairReentrantWriteLockLoop().test(v, nthreads, fairIters);
103 Thread.sleep(10);
104
105 if (print)
106 System.out.print("ReentrantReadWriteLock");
107 new ReentrantReadWriteLockLoop().test(v, nthreads, iters);
108 Thread.sleep(10);
109
110 if (print)
111 System.out.print("FairRReadWriteLock ");
112 new FairReentrantReadWriteLockLoop().test(v, nthreads, fairIters);
113 Thread.sleep(10);
114 }
115
116 static abstract class LockLoop implements Runnable {
117 int value;
118 int checkValue;
119 int iters;
120 volatile int result;
121 volatile int failures;
122 final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
123 CyclicBarrier barrier;
124
125 final int setValue(int v) {
126 checkValue = v ^ 0x55555555;
127 value = v;
128 return v;
129 }
130
131 final int getValue() {
132 int v = value;
133 if (checkValue != ~(v ^ 0xAAAAAAAA))
134 ++failures;
135 return v;
136 }
137
138 final void test(int initialValue, int nthreads, int iters) throws Exception {
139 setValue(initialValue);
140 this.iters = iters;
141 barrier = new CyclicBarrier(nthreads+1, timer);
142 for (int i = 0; i < nthreads; ++i)
143 pool.execute(this);
144 barrier.await();
145 barrier.await();
146 long time = timer.getTime();
147 if (print) {
148 long tpi = time / (iters * nthreads);
149 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per update");
150 System.out.println();
151 }
152
153 if (result == 0) // avoid overoptimization
154 System.out.println("useless result: " + result);
155 if (failures != 0)
156 throw new Error("lock protection failure");
157 }
158
159 abstract int loop(int n);
160 public final void run() {
161 try {
162 barrier.await();
163 result += loop(iters);
164 barrier.await();
165 }
166 catch (Exception ie) {
167 return;
168 }
169 }
170
171 }
172
173 private static class NoLockLoop extends LockLoop {
174 private volatile int readBarrier;
175 final int loop(int n) {
176 int sum = 0;
177 int x = 0;;
178 while (n-- > 0) {
179 int r1 = readBarrier;
180 x = setValue(LoopHelpers.compute1(getValue()));
181 int r2 = readBarrier;
182 if (r1 == r2 && x == r1)
183 ++readBarrier;
184 sum += LoopHelpers.compute2(x);
185 }
186 return sum;
187 }
188 }
189
190 private static class BuiltinLockLoop extends LockLoop {
191 final int loop(int n) {
192 int sum = 0;
193 int x = 0;;
194 while (n-- > 0) {
195 synchronized(this) {
196 x = setValue(LoopHelpers.compute1(getValue()));
197 }
198 sum += LoopHelpers.compute2(x);
199 }
200 return sum;
201 }
202 }
203
204 private static class ReentrantLockLoop extends LockLoop {
205 final private ReentrantLock lock = new ReentrantLock();
206 final int loop(int n) {
207 final ReentrantLock lock = this.lock;
208 int sum = 0;
209 int x = 0;
210 while (n-- > 0) {
211 lock.lock();
212 try {
213 x = setValue(LoopHelpers.compute1(getValue()));
214 }
215 finally {
216 lock.unlock();
217 }
218 sum += LoopHelpers.compute2(x);
219 }
220 return sum;
221 }
222 }
223
224 private static class MutexLoop extends LockLoop {
225 final private Mutex lock = new Mutex();
226 final int loop(int n) {
227 final Mutex lock = this.lock;
228 int sum = 0;
229 int x = 0;
230 while (n-- > 0) {
231 lock.lock();
232 try {
233 x = setValue(LoopHelpers.compute1(getValue()));
234 }
235 finally {
236 lock.unlock();
237 }
238 sum += LoopHelpers.compute2(x);
239 }
240 return sum;
241 }
242 }
243
244 private static class LongMutexLoop extends LockLoop {
245 final private LongMutex lock = new LongMutex();
246 final int loop(int n) {
247 final LongMutex lock = this.lock;
248 int sum = 0;
249 int x = 0;
250 while (n-- > 0) {
251 lock.lock();
252 try {
253 x = setValue(LoopHelpers.compute1(getValue()));
254 }
255 finally {
256 lock.unlock();
257 }
258 sum += LoopHelpers.compute2(x);
259 }
260 return sum;
261 }
262 }
263
264 private static class FairReentrantLockLoop extends LockLoop {
265 final private ReentrantLock lock = new ReentrantLock(true);
266 final int loop(int n) {
267 final ReentrantLock lock = this.lock;
268 int sum = 0;
269 int x = 0;
270 while (n-- > 0) {
271 lock.lock();
272 try {
273 x = setValue(LoopHelpers.compute1(getValue()));
274 }
275 finally {
276 lock.unlock();
277 }
278 sum += LoopHelpers.compute2(x);
279 }
280 return sum;
281 }
282 }
283
284 private static class ReentrantWriteLockLoop extends LockLoop {
285 final private Lock lock = new ReentrantReadWriteLock().writeLock();
286 final int loop(int n) {
287 final Lock lock = this.lock;
288 int sum = 0;
289 int x = 0;
290 while (n-- > 0) {
291 lock.lock();
292 try {
293 x = setValue(LoopHelpers.compute1(getValue()));
294 }
295 finally {
296 lock.unlock();
297 }
298 sum += LoopHelpers.compute2(x);
299 }
300 return sum;
301 }
302 }
303
304 private static class FairReentrantWriteLockLoop extends LockLoop {
305 final Lock lock = new ReentrantReadWriteLock(true).writeLock();
306 final int loop(int n) {
307 final Lock lock = this.lock;
308 int sum = 0;
309 int x = 0;
310 while (n-- > 0) {
311 lock.lock();
312 try {
313 x = setValue(LoopHelpers.compute1(getValue()));
314 }
315 finally {
316 lock.unlock();
317 }
318 sum += LoopHelpers.compute2(x);
319 }
320 return sum;
321 }
322 }
323
324 private static class SemaphoreLoop extends LockLoop {
325 final private Semaphore sem = new Semaphore(1, false);
326 final int loop(int n) {
327 final Semaphore sem = this.sem;
328 int sum = 0;
329 int x = 0;
330 while (n-- > 0) {
331 sem.acquireUninterruptibly();
332 try {
333 x = setValue(LoopHelpers.compute1(getValue()));
334 }
335 finally {
336 sem.release();
337 }
338 sum += LoopHelpers.compute2(x);
339 }
340 return sum;
341 }
342 }
343 private static class FairSemaphoreLoop extends LockLoop {
344 final private Semaphore sem = new Semaphore(1, true);
345 final int loop(int n) {
346 final Semaphore sem = this.sem;
347 int sum = 0;
348 int x = 0;
349 while (n-- > 0) {
350 sem.acquireUninterruptibly();
351 try {
352 x = setValue(LoopHelpers.compute1(getValue()));
353 }
354 finally {
355 sem.release();
356 }
357 sum += LoopHelpers.compute2(x);
358 }
359 return sum;
360 }
361 }
362
363 private static class ReentrantReadWriteLockLoop extends LockLoop {
364 final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
365 final int loop(int n) {
366 final Lock rlock = lock.readLock();
367 final Lock wlock = lock.writeLock();
368 int sum = 0;
369 int x = 0;
370 while (n-- > 0) {
371 if ((n & 16) != 0) {
372 rlock.lock();
373 try {
374 x = LoopHelpers.compute1(getValue());
375 x = LoopHelpers.compute2(x);
376 }
377 finally {
378 rlock.unlock();
379 }
380 }
381 else {
382 wlock.lock();
383 try {
384 setValue(x);
385 }
386 finally {
387 wlock.unlock();
388 }
389 sum += LoopHelpers.compute2(x);
390 }
391 }
392 return sum;
393 }
394
395 }
396
397
398 private static class FairReentrantReadWriteLockLoop extends LockLoop {
399 final private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
400 final int loop(int n) {
401 final Lock rlock = lock.readLock();
402 final Lock wlock = lock.writeLock();
403 int sum = 0;
404 int x = 0;
405 while (n-- > 0) {
406 if ((n & 16) != 0) {
407 rlock.lock();
408 try {
409 x = LoopHelpers.compute1(getValue());
410 x = LoopHelpers.compute2(x);
411 }
412 finally {
413 rlock.unlock();
414 }
415 }
416 else {
417 wlock.lock();
418 try {
419 setValue(x);
420 }
421 finally {
422 wlock.unlock();
423 }
424 sum += LoopHelpers.compute2(x);
425 }
426 }
427 return sum;
428 }
429
430 }
431 }