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