11 |
|
* just compare and conditionally store (int) values, so are |
12 |
|
* not intended to measure the "raw" cost of a CAS. |
13 |
|
* |
14 |
< |
* Outputs, for runs using 1 ... #cpus threads are, in nanoseconds: |
14 |
> |
* Outputs, in nanoseconds: |
15 |
|
* "Atomic CAS" AtomicInteger.compareAndSet |
16 |
+ |
* "Updater CAS" CAS first comparing args |
17 |
|
* "Volatile" pseudo-CAS using volatile store if comparison succeeds |
18 |
|
* "Mutex" emulated compare and set done under AQS-based mutex lock |
19 |
|
* "Synchronized" emulated compare and set done under a synchronized block. |
20 |
|
* |
21 |
< |
* The last two are done only if this program is called with (any) argument |
21 |
> |
* By default, these are printed for 1..#cpus threads, but you can |
22 |
> |
* change the upper bound number of threads by providing the |
23 |
> |
* first argument to this program. |
24 |
> |
* |
25 |
> |
* The last two kinds of runs (mutex and synchronized) are done only |
26 |
> |
* if this program is called with (any) second argument |
27 |
|
*/ |
28 |
|
|
29 |
|
|
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
|
import java.util.concurrent.atomic.AtomicLong; |
32 |
+ |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
33 |
|
import java.util.concurrent.*; |
34 |
|
import java.util.concurrent.locks.*; |
35 |
|
|
38 |
|
static final int TRIALS = 2; |
39 |
|
static final long BASE_SECS_PER_RUN = 4; |
40 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
41 |
+ |
static int maxThreads = NCPUS; |
42 |
|
|
43 |
|
static boolean includeLocks = false; |
44 |
|
|
45 |
|
public static void main(String[] args) throws Exception { |
46 |
< |
if (args.length > 0) |
46 |
> |
if (args.length > 0) |
47 |
> |
maxThreads = Integer.parseInt(args[0]); |
48 |
> |
|
49 |
> |
loopIters = new long[maxThreads+1]; |
50 |
> |
|
51 |
> |
if (args.length > 1) |
52 |
|
includeLocks = true; |
53 |
|
|
54 |
|
System.out.println("Warmup..."); |
55 |
+ |
for (int i = maxThreads; i > 0; --i) { |
56 |
+ |
runCalibration(i, 10); |
57 |
+ |
oneRun(i, loopIters[i] / 4, false); |
58 |
+ |
System.out.print("."); |
59 |
+ |
} |
60 |
+ |
|
61 |
+ |
for (int i = 1; i <= maxThreads; ++i) |
62 |
+ |
loopIters[i] = 0; |
63 |
+ |
|
64 |
|
for (int j = 0; j < 2; ++j) { |
65 |
< |
for (int i = 1; i <= NCPUS; ++i) { |
65 |
> |
for (int i = 1; i <= maxThreads; ++i) { |
66 |
|
runCalibration(i, 1000); |
67 |
|
oneRun(i, loopIters[i] / 8, false); |
68 |
+ |
System.out.print("."); |
69 |
|
} |
70 |
|
} |
71 |
|
|
72 |
< |
for (int i = 1; i <= NCPUS; ++i) |
72 |
> |
for (int i = 1; i <= maxThreads; ++i) |
73 |
|
loopIters[i] = 0; |
74 |
|
|
75 |
|
for (int j = 0; j < TRIALS; ++j) { |
76 |
|
System.out.println("Trial " + j); |
77 |
< |
for (int i = 1; i <= NCPUS; ++i) { |
77 |
> |
for (int i = 1; i <= maxThreads; ++i) { |
78 |
|
runCalibration(i, BASE_SECS_PER_RUN * 1000L); |
79 |
|
oneRun(i, loopIters[i], true); |
80 |
|
} |
87 |
|
|
88 |
|
static final LoopHelpers.MarsagliaRandom rng = new LoopHelpers.MarsagliaRandom(); |
89 |
|
|
90 |
< |
static final long[] loopIters = new long[NCPUS+1]; |
90 |
> |
static long[] loopIters; |
91 |
|
|
92 |
|
static final class NonAtomicInteger { |
93 |
|
volatile int readBarrier; |
108 |
|
void set(int val) { value = val; } |
109 |
|
} |
110 |
|
|
111 |
+ |
static final class UpdaterAtomicInteger { |
112 |
+ |
volatile int value; |
113 |
+ |
|
114 |
+ |
static final AtomicIntegerFieldUpdater<UpdaterAtomicInteger> |
115 |
+ |
valueUpdater = AtomicIntegerFieldUpdater.newUpdater |
116 |
+ |
(UpdaterAtomicInteger.class, "value"); |
117 |
+ |
|
118 |
+ |
|
119 |
+ |
UpdaterAtomicInteger() {} |
120 |
+ |
int get() { |
121 |
+ |
return value; |
122 |
+ |
} |
123 |
+ |
boolean compareAndSet(int cmp, int val) { |
124 |
+ |
return valueUpdater.compareAndSet(this, cmp, val); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
void set(int val) { value = val; } |
128 |
+ |
} |
129 |
+ |
|
130 |
|
static final class VolatileInteger { |
131 |
|
volatile int value; |
132 |
|
|
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
+ |
static final class UpdaterAtomicLoop implements Runnable { |
285 |
+ |
final long iters; |
286 |
+ |
final UpdaterAtomicInteger obj; |
287 |
+ |
final CyclicBarrier barrier; |
288 |
+ |
UpdaterAtomicLoop(long iters, UpdaterAtomicInteger obj, CyclicBarrier b) { |
289 |
+ |
this.iters = iters; |
290 |
+ |
this.obj = obj; |
291 |
+ |
this.barrier = b; |
292 |
+ |
obj.set(rng.next()); |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
public void run() { |
296 |
+ |
try { |
297 |
+ |
barrier.await(); |
298 |
+ |
long i = iters; |
299 |
+ |
int y = 0; |
300 |
+ |
int succ = 0; |
301 |
+ |
while (i > 0) { |
302 |
+ |
for (int k = 0; k < innerPerOuter; ++k) { |
303 |
+ |
int x = obj.get(); |
304 |
+ |
int z = y + LoopHelpers.compute6(x); |
305 |
+ |
if (obj.compareAndSet(x, z)) |
306 |
+ |
++succ; |
307 |
+ |
y = LoopHelpers.compute7(z); |
308 |
+ |
} |
309 |
+ |
i -= innerPerOuter; |
310 |
+ |
} |
311 |
+ |
sum.getAndAdd(obj.get()); |
312 |
+ |
successes.getAndAdd(succ); |
313 |
+ |
barrier.await(); |
314 |
+ |
} |
315 |
+ |
catch (Exception ie) { |
316 |
+ |
return; |
317 |
+ |
} |
318 |
+ |
} |
319 |
+ |
} |
320 |
+ |
|
321 |
|
static final class VolatileLoop implements Runnable { |
322 |
|
final long iters; |
323 |
|
final VolatileInteger obj; |
501 |
|
return timer.getTime(); |
502 |
|
} |
503 |
|
|
504 |
+ |
static long runUpdaterAtomic(int n, long iters) throws Exception { |
505 |
+ |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
506 |
+ |
CyclicBarrier b = new CyclicBarrier(n+1, timer); |
507 |
+ |
UpdaterAtomicInteger a = new UpdaterAtomicInteger(); |
508 |
+ |
for (int j = 0; j < n; ++j) |
509 |
+ |
new Thread(new UpdaterAtomicLoop(iters, a, b)).start(); |
510 |
+ |
b.await(); |
511 |
+ |
b.await(); |
512 |
+ |
if (sum.get() == 0) System.out.print(" "); |
513 |
+ |
return timer.getTime(); |
514 |
+ |
} |
515 |
+ |
|
516 |
|
static long runAtomic(int n, long iters) throws Exception { |
517 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
518 |
|
CyclicBarrier b = new CyclicBarrier(n+1, timer); |
562 |
|
return timer.getTime(); |
563 |
|
} |
564 |
|
|
565 |
< |
static void report(String tag, long runtime, long basetime, long iters) { |
565 |
> |
static void report(String tag, long runtime, long basetime, |
566 |
> |
int nthreads, long iters) { |
567 |
|
System.out.print(tag); |
568 |
< |
System.out.print(LoopHelpers.rightJustify((runtime - basetime) / iters) + " ns"); |
568 |
> |
long t = (runtime - basetime) / iters; |
569 |
> |
if (nthreads > NCPUS) |
570 |
> |
t = t * NCPUS / nthreads; |
571 |
> |
System.out.print(LoopHelpers.rightJustify(t)); |
572 |
|
double secs = (double)(runtime) / 1000000000.0; |
573 |
|
System.out.println("\t " + secs + "s run time"); |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
|
static void oneRun(int i, long iters, boolean print) throws Exception { |
578 |
< |
System.out.println("threads : " + i + |
579 |
< |
" base iters per thread per run : " + |
580 |
< |
LoopHelpers.rightJustify(loopIters[i])); |
578 |
> |
if (print) |
579 |
> |
System.out.println("threads : " + i + |
580 |
> |
" base iters per thread per run : " + |
581 |
> |
LoopHelpers.rightJustify(loopIters[i])); |
582 |
|
long ntime = runNonAtomic(i, iters); |
583 |
|
if (print) |
584 |
< |
report("Base : ", ntime, ntime, iters); |
584 |
> |
report("Base : ", ntime, ntime, i, iters); |
585 |
|
Thread.sleep(100L); |
586 |
|
long atime = runAtomic(i, iters); |
587 |
|
if (print) |
588 |
< |
report("Atomic CAS : ", atime, ntime, iters); |
588 |
> |
report("Atomic CAS : ", atime, ntime, i, iters); |
589 |
> |
Thread.sleep(100L); |
590 |
> |
long gtime = runUpdaterAtomic(i, iters); |
591 |
> |
if (print) |
592 |
> |
report("Updater CAS : ", gtime, ntime, i, iters); |
593 |
|
Thread.sleep(100L); |
594 |
|
long vtime = runVolatile(i, iters); |
595 |
|
if (print) |
596 |
< |
report("Volatile : ", vtime, ntime, iters); |
596 |
> |
report("Volatile : ", vtime, ntime, i, iters); |
597 |
|
|
598 |
|
Thread.sleep(100L); |
599 |
|
if (!includeLocks) return; |
600 |
|
long mtime = runLocked(i, iters); |
601 |
|
if (print) |
602 |
< |
report("Mutex : ", mtime, ntime, iters); |
602 |
> |
report("Mutex : ", mtime, ntime, i, iters); |
603 |
|
Thread.sleep(100L); |
604 |
|
long stime = runSynched(i, iters); |
605 |
|
if (print) |
606 |
< |
report("Synchronized: ", stime, ntime, iters); |
606 |
> |
report("Synchronized: ", stime, ntime, i, iters); |
607 |
|
Thread.sleep(100L); |
608 |
|
} |
609 |
|
|