1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.2 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import java.util.concurrent.*; |
8 |
dl |
1.5 |
|
9 |
dl |
1.1 |
|
10 |
|
|
public class TimeoutProducerConsumerLoops { |
11 |
dl |
1.3 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
12 |
dl |
1.1 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
13 |
dl |
1.3 |
|
14 |
|
|
// Number of elements passed around -- must be power of two |
15 |
|
|
// Elements are reused from pool to minimize alloc impact |
16 |
|
|
static final int POOL_SIZE = 1 << 8; |
17 |
|
|
static final int POOL_MASK = POOL_SIZE-1; |
18 |
|
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
19 |
|
|
static { |
20 |
jsr166 |
1.6 |
for (int i = 0; i < POOL_SIZE; ++i) |
21 |
dl |
1.3 |
intPool[i] = Integer.valueOf(i); |
22 |
|
|
} |
23 |
|
|
|
24 |
jsr166 |
1.6 |
// max lag between a producer and consumer to avoid |
25 |
dl |
1.5 |
// this becoming a GC test rather than queue test. |
26 |
|
|
// Used only per-pair to lessen impact on queue sync |
27 |
|
|
static final int LAG_MASK = (1 << 12) - 1; |
28 |
dl |
1.3 |
|
29 |
dl |
1.1 |
static boolean print = false; |
30 |
|
|
static int producerSum; |
31 |
|
|
static int consumerSum; |
32 |
|
|
static synchronized void addProducerSum(int x) { |
33 |
|
|
producerSum += x; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
static synchronized void addConsumerSum(int x) { |
37 |
|
|
consumerSum += x; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
static synchronized void checkSum() { |
41 |
|
|
if (producerSum != consumerSum) { |
42 |
|
|
throw new Error("CheckSum mismatch"); |
43 |
|
|
} |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
public static void main(String[] args) throws Exception { |
47 |
dl |
1.3 |
int maxPairs = NCPUS * 3 / 2; |
48 |
|
|
int iters = 1000000; |
49 |
dl |
1.1 |
|
50 |
jsr166 |
1.6 |
if (args.length > 0) |
51 |
dl |
1.1 |
maxPairs = Integer.parseInt(args[0]); |
52 |
|
|
|
53 |
|
|
print = true; |
54 |
|
|
int k = 1; |
55 |
|
|
for (int i = 1; i <= maxPairs;) { |
56 |
|
|
System.out.println("Pairs:" + i); |
57 |
|
|
oneTest(i, iters); |
58 |
|
|
Thread.sleep(100); |
59 |
|
|
if (i == k) { |
60 |
|
|
k = i << 1; |
61 |
|
|
i = i + (i >>> 1); |
62 |
jsr166 |
1.6 |
} |
63 |
|
|
else |
64 |
dl |
1.1 |
i = k; |
65 |
|
|
} |
66 |
|
|
pool.shutdown(); |
67 |
|
|
} |
68 |
|
|
|
69 |
dl |
1.3 |
static void oneTest(int n, int iters) throws Exception { |
70 |
|
|
if (print) |
71 |
|
|
System.out.print("LinkedTransferQueue "); |
72 |
|
|
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
73 |
|
|
|
74 |
|
|
if (print) |
75 |
|
|
System.out.print("LinkedTransferQueue(xfer)"); |
76 |
|
|
oneRun(new LTQasSQ<Integer>(), n, iters); |
77 |
|
|
|
78 |
dl |
1.1 |
if (print) |
79 |
dl |
1.3 |
System.out.print("LinkedBlockingQueue "); |
80 |
|
|
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
81 |
dl |
1.1 |
|
82 |
|
|
if (print) |
83 |
dl |
1.3 |
System.out.print("LinkedBlockingQueue(cap) "); |
84 |
|
|
oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters); |
85 |
dl |
1.1 |
|
86 |
|
|
if (print) |
87 |
dl |
1.3 |
System.out.print("ArrayBlockingQueue(cap) "); |
88 |
|
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
89 |
dl |
1.1 |
|
90 |
|
|
if (print) |
91 |
dl |
1.3 |
System.out.print("LinkedBlockingDeque "); |
92 |
|
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
93 |
dl |
1.1 |
|
94 |
|
|
if (print) |
95 |
dl |
1.3 |
System.out.print("SynchronousQueue "); |
96 |
|
|
oneRun(new SynchronousQueue<Integer>(), n, iters); |
97 |
dl |
1.1 |
|
98 |
|
|
if (print) |
99 |
dl |
1.3 |
System.out.print("SynchronousQueue(fair) "); |
100 |
|
|
oneRun(new SynchronousQueue<Integer>(true), n, iters); |
101 |
dl |
1.1 |
|
102 |
|
|
if (print) |
103 |
dl |
1.3 |
System.out.print("PriorityBlockingQueue "); |
104 |
|
|
oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16); |
105 |
|
|
|
106 |
|
|
if (print) |
107 |
|
|
System.out.print("ArrayBlockingQueue(fair) "); |
108 |
|
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16); |
109 |
dl |
1.1 |
|
110 |
|
|
} |
111 |
jsr166 |
1.6 |
|
112 |
dl |
1.1 |
static abstract class Stage implements Runnable { |
113 |
|
|
final int iters; |
114 |
|
|
final BlockingQueue<Integer> queue; |
115 |
|
|
final CyclicBarrier barrier; |
116 |
dl |
1.5 |
final Phaser lagPhaser; |
117 |
jsr166 |
1.7 |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
118 |
jsr166 |
1.6 |
queue = q; |
119 |
dl |
1.1 |
barrier = b; |
120 |
dl |
1.5 |
lagPhaser = s; |
121 |
dl |
1.1 |
this.iters = iters; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
static class Producer extends Stage { |
126 |
dl |
1.5 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
127 |
|
|
super(q, b, s, iters); |
128 |
dl |
1.1 |
} |
129 |
|
|
|
130 |
|
|
public void run() { |
131 |
|
|
try { |
132 |
|
|
barrier.await(); |
133 |
|
|
int s = 0; |
134 |
|
|
int l = hashCode(); |
135 |
|
|
int i = 0; |
136 |
dl |
1.3 |
long timeout = 1000; |
137 |
dl |
1.1 |
while (i < iters) { |
138 |
|
|
l = LoopHelpers.compute4(l); |
139 |
dl |
1.3 |
Integer v = intPool[l & POOL_MASK]; |
140 |
|
|
if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) { |
141 |
|
|
s += LoopHelpers.compute4(v.intValue()); |
142 |
dl |
1.1 |
++i; |
143 |
|
|
if (timeout > 1) |
144 |
|
|
timeout--; |
145 |
dl |
1.5 |
if ((i & LAG_MASK) == LAG_MASK) |
146 |
|
|
lagPhaser.arriveAndAwaitAdvance(); |
147 |
dl |
1.1 |
} |
148 |
|
|
else |
149 |
|
|
timeout++; |
150 |
|
|
} |
151 |
|
|
addProducerSum(s); |
152 |
|
|
barrier.await(); |
153 |
|
|
} |
154 |
jsr166 |
1.6 |
catch (Exception ie) { |
155 |
|
|
ie.printStackTrace(); |
156 |
|
|
return; |
157 |
dl |
1.1 |
} |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
static class Consumer extends Stage { |
162 |
jsr166 |
1.6 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
163 |
dl |
1.5 |
super(q, b, s, iters); |
164 |
dl |
1.1 |
} |
165 |
|
|
|
166 |
|
|
public void run() { |
167 |
|
|
try { |
168 |
|
|
barrier.await(); |
169 |
|
|
int l = 0; |
170 |
|
|
int s = 0; |
171 |
|
|
int i = 0; |
172 |
dl |
1.3 |
long timeout = 1000; |
173 |
dl |
1.1 |
while (i < iters) { |
174 |
jsr166 |
1.6 |
Integer e = queue.poll(timeout, |
175 |
dl |
1.1 |
TimeUnit.NANOSECONDS); |
176 |
|
|
if (e != null) { |
177 |
|
|
l = LoopHelpers.compute4(e.intValue()); |
178 |
|
|
s += l; |
179 |
|
|
++i; |
180 |
|
|
if (timeout > 1) |
181 |
|
|
--timeout; |
182 |
dl |
1.5 |
if ((i & LAG_MASK) == LAG_MASK) |
183 |
|
|
lagPhaser.arriveAndAwaitAdvance(); |
184 |
dl |
1.1 |
} |
185 |
|
|
else |
186 |
|
|
++timeout; |
187 |
|
|
} |
188 |
|
|
addConsumerSum(s); |
189 |
|
|
barrier.await(); |
190 |
|
|
} |
191 |
jsr166 |
1.6 |
catch (Exception ie) { |
192 |
|
|
ie.printStackTrace(); |
193 |
|
|
return; |
194 |
dl |
1.1 |
} |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception { |
200 |
|
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
201 |
|
|
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
202 |
|
|
for (int i = 0; i < npairs; ++i) { |
203 |
dl |
1.5 |
Phaser s = new Phaser(2); |
204 |
|
|
pool.execute(new Producer(q, barrier, s, iters)); |
205 |
|
|
pool.execute(new Consumer(q, barrier, s, iters)); |
206 |
dl |
1.1 |
} |
207 |
|
|
barrier.await(); |
208 |
|
|
barrier.await(); |
209 |
|
|
long time = timer.getTime(); |
210 |
|
|
checkSum(); |
211 |
dl |
1.3 |
q.clear(); |
212 |
dl |
1.1 |
if (print) |
213 |
|
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer"); |
214 |
|
|
} |
215 |
|
|
|
216 |
dl |
1.3 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
217 |
|
|
LTQasSQ() { super(); } |
218 |
|
|
public void put(T x) { |
219 |
jsr166 |
1.6 |
try { super.transfer(x); |
220 |
dl |
1.3 |
} catch (InterruptedException ex) { throw new Error(); } |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
public boolean offer(T x, long timeout, TimeUnit unit) { |
224 |
jsr166 |
1.6 |
return super.offer(x, timeout, unit); |
225 |
dl |
1.3 |
} |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
|
229 |
dl |
1.1 |
} |