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