1 |
|
/* |
2 |
|
* Written by Bill Scherer and Doug Lea with assistance from members |
3 |
< |
* of JCP JSR-166 Expert Group and released to the public domain. Use, |
4 |
< |
* modify, and redistribute this code in any way without |
5 |
< |
* acknowledgement. |
3 |
> |
* of JCP JSR-166 Expert Group and released to the public domain, as |
4 |
> |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
9 |
|
import java.util.concurrent.locks.*; |
10 |
|
|
11 |
|
public class TimeoutExchangerLoops { |
12 |
< |
static final int DEFAULT_THREADS = 32; |
13 |
< |
static final long DEFAULT_TRIAL_MILLIS = 5000; |
12 |
> |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
13 |
> |
|
14 |
> |
static final int DEFAULT_THREADS = NCPUS + 2; |
15 |
|
static final long DEFAULT_PATIENCE_NANOS = 500000; |
16 |
+ |
static final long DEFAULT_TRIAL_MILLIS = 10000; |
17 |
|
|
17 |
– |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
18 |
– |
|
18 |
|
public static void main(String[] args) throws Exception { |
19 |
|
int maxThreads = DEFAULT_THREADS; |
20 |
|
long trialMillis = DEFAULT_TRIAL_MILLIS; |
21 |
|
long patienceNanos = DEFAULT_PATIENCE_NANOS; |
22 |
+ |
int nReps = 3; |
23 |
|
|
24 |
|
// Parse and check args |
25 |
|
int argc = 0; |
26 |
< |
try { |
27 |
< |
while (argc < args.length) { |
28 |
< |
String option = args[argc++]; |
29 |
< |
if (option.equals("-t")) |
30 |
< |
trialMillis = Integer.parseInt(args[argc]); |
31 |
< |
else if (option.equals("-p")) |
32 |
< |
patienceNanos = Long.parseLong(args[argc]); |
33 |
< |
else |
34 |
< |
maxThreads = Integer.parseInt(option); |
35 |
< |
argc++; |
26 |
> |
while (argc < args.length) { |
27 |
> |
String option = args[argc++]; |
28 |
> |
if (option.equals("-t")) |
29 |
> |
trialMillis = Integer.parseInt(args[argc]); |
30 |
> |
else if (option.equals("-p")) |
31 |
> |
patienceNanos = Long.parseLong(args[argc]); |
32 |
> |
else if (option.equals("-r")) |
33 |
> |
nReps = Integer.parseInt(args[argc]); |
34 |
> |
else |
35 |
> |
maxThreads = Integer.parseInt(option); |
36 |
> |
argc++; |
37 |
> |
} |
38 |
> |
|
39 |
> |
// Display runtime parameters |
40 |
> |
System.out.print("TimeoutExchangerTest"); |
41 |
> |
System.out.print(" -t " + trialMillis); |
42 |
> |
System.out.print(" -p " + patienceNanos); |
43 |
> |
System.out.print(" -r " + nReps); |
44 |
> |
System.out.print(" max threads " + maxThreads); |
45 |
> |
System.out.println(); |
46 |
> |
|
47 |
> |
System.out.println("Warmups.."); |
48 |
> |
long warmupTime = 1000; |
49 |
> |
long sleepTime = 500; |
50 |
> |
if (false) { |
51 |
> |
for (int k = 0; k < 10; ++k) { |
52 |
> |
for (int j = 0; j < 10; ++j) { |
53 |
> |
oneRun(2, (j + 1) * 1000, patienceNanos); |
54 |
> |
Thread.sleep(sleepTime); |
55 |
> |
} |
56 |
|
} |
57 |
|
} |
58 |
< |
catch (Exception e) { |
59 |
< |
e.printStackTrace(); |
60 |
< |
System.exit(0); |
61 |
< |
} |
62 |
< |
|
63 |
< |
// Display runtime parameters |
64 |
< |
System.out.print("TimeoutExchangerTest"); |
65 |
< |
System.out.print(" -t " + trialMillis); |
66 |
< |
System.out.print(" -p " + patienceNanos); |
67 |
< |
System.out.print(" max threads " + maxThreads); |
68 |
< |
System.out.println(); |
69 |
< |
|
70 |
< |
// warmup |
71 |
< |
System.out.print("Threads: " + 2 + "\t"); |
72 |
< |
oneRun(2, trialMillis, patienceNanos); |
53 |
< |
Thread.sleep(100); |
54 |
< |
|
55 |
< |
int k = 4; |
56 |
< |
for (int i = 2; i <= maxThreads;) { |
57 |
< |
System.out.print("Threads: " + i + "\t"); |
58 |
< |
oneRun(i, trialMillis, patienceNanos); |
59 |
< |
Thread.sleep(100); |
60 |
< |
if (i == k) { |
61 |
< |
k = i << 1; |
62 |
< |
i = i + (i >>> 1); |
63 |
< |
} |
64 |
< |
else |
65 |
< |
i = k; |
58 |
> |
|
59 |
> |
oneRun(3, warmupTime, patienceNanos); |
60 |
> |
Thread.sleep(sleepTime); |
61 |
> |
|
62 |
> |
for (int i = maxThreads; i >= 2; i -= 1) { |
63 |
> |
oneRun(i, warmupTime, patienceNanos); |
64 |
> |
Thread.sleep(sleepTime); |
65 |
> |
} |
66 |
> |
|
67 |
> |
for (int j = 0; j < nReps; ++j) { |
68 |
> |
System.out.println("Replication " + j); |
69 |
> |
for (int i = 2; i <= maxThreads; i += 2) { |
70 |
> |
oneRun(i, trialMillis, patienceNanos); |
71 |
> |
Thread.sleep(sleepTime); |
72 |
> |
} |
73 |
|
} |
67 |
– |
pool.shutdown(); |
74 |
|
} |
75 |
|
|
76 |
< |
static void oneRun(int nThreads, long trialMillis, long patienceNanos) |
76 |
> |
static void oneRun(int nThreads, long trialMillis, long patienceNanos) |
77 |
|
throws Exception { |
78 |
< |
CyclicBarrier barrier = new CyclicBarrier(nThreads+1); |
79 |
< |
long stopTime = System.currentTimeMillis() + trialMillis; |
80 |
< |
Exchanger<MutableInt> x = new Exchanger<MutableInt>(); |
78 |
> |
System.out.printf("%4d threads", nThreads); |
79 |
> |
System.out.printf("%9dms", trialMillis); |
80 |
> |
final CountDownLatch start = new CountDownLatch(1); |
81 |
> |
Exchanger x = new Exchanger(); |
82 |
|
Runner[] runners = new Runner[nThreads]; |
83 |
< |
for (int i = 0; i < nThreads; ++i) |
84 |
< |
runners[i] = new Runner(x, stopTime, patienceNanos, barrier); |
85 |
< |
for (int i = 0; i < nThreads; ++i) |
86 |
< |
pool.execute(runners[i]); |
87 |
< |
barrier.await(); |
88 |
< |
barrier.await(); |
89 |
< |
long iters = 0; |
83 |
> |
Thread[] threads = new Thread[nThreads]; |
84 |
> |
for (int i = 0; i < nThreads; ++i) { |
85 |
> |
runners[i] = new Runner(x, patienceNanos, start); |
86 |
> |
threads[i] = new Thread(runners[i]); |
87 |
> |
threads[i].start(); |
88 |
> |
} |
89 |
> |
long startTime = System.nanoTime(); |
90 |
> |
start.countDown(); |
91 |
> |
Thread.sleep(trialMillis); |
92 |
> |
for (int i = 0; i < nThreads; ++i) |
93 |
> |
threads[i].interrupt(); |
94 |
> |
long elapsed = System.nanoTime() - startTime; |
95 |
> |
for (int i = 0; i < nThreads; ++i) |
96 |
> |
threads[i].join(); |
97 |
> |
int iters = 0; |
98 |
|
long fails = 0; |
84 |
– |
long check = 0; |
99 |
|
for (int i = 0; i < nThreads; ++i) { |
100 |
< |
iters += runners[i].iterations; |
100 |
> |
iters += runners[i].iters; |
101 |
|
fails += runners[i].failures; |
88 |
– |
check += runners[i].mine.value; |
102 |
|
} |
103 |
< |
if (check != iters) |
104 |
< |
throw new Error("bad checksum " + iters + "/" + check); |
105 |
< |
long rate = (iters * 1000) / trialMillis; |
106 |
< |
double failRate = (fails * 100.0) / (double)iters; |
107 |
< |
System.out.print(LoopHelpers.rightJustify(rate) + " iterations/s "); |
108 |
< |
System.out.printf("%9.5f", failRate); |
109 |
< |
System.out.print("% timeouts"); |
103 |
> |
if (iters <= 0) iters = 1; |
104 |
> |
long rate = iters * 1000L * 1000L * 1000L / elapsed; |
105 |
> |
long npt = elapsed / iters; |
106 |
> |
double failRate = (fails * 100.0) / (double) iters; |
107 |
> |
System.out.printf("%9d it/s ", rate); |
108 |
> |
System.out.printf("%9d ns/it", npt); |
109 |
> |
System.out.printf("%9.5f%% fails", failRate); |
110 |
|
System.out.println(); |
111 |
+ |
// x.printStats(); |
112 |
|
} |
113 |
|
|
114 |
< |
static final class MutableInt { |
101 |
< |
int value; |
102 |
< |
} |
103 |
< |
|
114 |
> |
|
115 |
|
static final class Runner implements Runnable { |
116 |
< |
final Exchanger<MutableInt> x; |
117 |
< |
volatile long iterations; |
107 |
< |
volatile long failures; |
108 |
< |
volatile MutableInt mine; |
109 |
< |
final long stopTime; |
116 |
> |
final Exchanger exchanger; |
117 |
> |
final CountDownLatch start; |
118 |
|
final long patience; |
119 |
< |
final CyclicBarrier barrier; |
120 |
< |
Runner(Exchanger<MutableInt> x, long stopTime, |
121 |
< |
long patience, CyclicBarrier b) { |
122 |
< |
this.x = x; |
115 |
< |
this.stopTime = stopTime; |
119 |
> |
volatile int iters; |
120 |
> |
volatile int failures; |
121 |
> |
Runner(Exchanger x, long patience, CountDownLatch start) { |
122 |
> |
this.exchanger = x; |
123 |
|
this.patience = patience; |
124 |
< |
this.barrier = b; |
118 |
< |
mine = new MutableInt(); |
124 |
> |
this.start = start; |
125 |
|
} |
126 |
|
|
127 |
|
public void run() { |
128 |
+ |
int i = 0; |
129 |
|
try { |
130 |
< |
barrier.await(); |
131 |
< |
MutableInt m = mine; |
132 |
< |
int i = 0; |
133 |
< |
int fails = 0; |
134 |
< |
do { |
130 |
> |
Exchanger x = exchanger; |
131 |
> |
Object m = new Integer(17); |
132 |
> |
long p = patience; |
133 |
> |
start.await(); |
134 |
> |
for (;;) { |
135 |
|
try { |
136 |
+ |
Object e = x.exchange(m, p, TimeUnit.NANOSECONDS); |
137 |
+ |
if (e == null || e == m) |
138 |
+ |
throw new Error(); |
139 |
+ |
m = e; |
140 |
|
++i; |
130 |
– |
m.value++; |
131 |
– |
m = x.exchange(m, patience, TimeUnit.NANOSECONDS); |
141 |
|
} catch (TimeoutException to) { |
142 |
< |
if (System.currentTimeMillis() >= stopTime) |
143 |
< |
break; |
144 |
< |
else |
145 |
< |
++fails; |
142 |
> |
if (Thread.interrupted()) { |
143 |
> |
iters = i; |
144 |
> |
return; |
145 |
> |
} |
146 |
> |
++i; |
147 |
> |
++failures; |
148 |
|
} |
149 |
< |
} while ((i & 127) != 0 || // only check time periodically |
150 |
< |
System.currentTimeMillis() < stopTime); |
151 |
< |
|
141 |
< |
mine = m; |
142 |
< |
iterations = i; |
143 |
< |
failures = fails; |
144 |
< |
barrier.await(); |
145 |
< |
} catch(Exception e) { |
146 |
< |
e.printStackTrace(); |
147 |
< |
return; |
149 |
> |
} |
150 |
> |
} catch (InterruptedException ie) { |
151 |
> |
iters = i; |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
|
} |
152 |
– |
|