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