1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
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.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 |
|
19 |
public class ProducerConsumerLoops { |
20 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
21 |
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 |
// 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 << 7; |
41 |
static final int POOL_MASK = POOL_SIZE-1; |
42 |
static final Integer[] intPool = new Integer[POOL_SIZE]; |
43 |
static { |
44 |
for (int i = 0; i < POOL_SIZE; ++i) |
45 |
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 |
// max lag between a producer and consumer to avoid |
52 |
// this becoming a GC test rather than queue test. |
53 |
// Used only per-pair to lessen impact on queue sync |
54 |
static final int LAG_MASK = (1 << 12) - 1; |
55 |
|
56 |
public static void main(String[] args) throws Exception { |
57 |
int maxPairs = NCPUS * 3 / 2; |
58 |
|
59 |
if (args.length > 0) |
60 |
maxPairs = Integer.parseInt(args[0]); |
61 |
|
62 |
warmup(); |
63 |
print = true; |
64 |
for (int k = 1, i = 1; i <= maxPairs;) { |
65 |
System.out.println("Pairs:" + i); |
66 |
oneTest(i, ITERS); |
67 |
if (i == k) { |
68 |
k = i << 1; |
69 |
i = i + (i >>> 1); |
70 |
} |
71 |
else |
72 |
i = k; |
73 |
} |
74 |
pool.shutdown(); |
75 |
} |
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 |
oneTest(j, it); |
83 |
System.out.print("."); |
84 |
it += 1000; |
85 |
} |
86 |
System.gc(); |
87 |
it = 20000; |
88 |
for (int j = 5; j > 0; --j) { |
89 |
oneTest(j, it); |
90 |
System.out.print("."); |
91 |
it += 10000; |
92 |
} |
93 |
System.gc(); |
94 |
System.out.println(); |
95 |
} |
96 |
|
97 |
static void oneTest(int n, int iters) throws Exception { |
98 |
int fairIters = iters/16; |
99 |
|
100 |
Thread.sleep(100); // System.gc(); |
101 |
if (print) |
102 |
System.out.print("LinkedTransferQueue "); |
103 |
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
104 |
|
105 |
Thread.sleep(100); // System.gc(); |
106 |
if (print) |
107 |
System.out.print("LinkedBlockingQueue "); |
108 |
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
109 |
|
110 |
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 |
|
120 |
Thread.sleep(100); // System.gc(); |
121 |
if (print) |
122 |
System.out.print("ArrayBlockingQueue "); |
123 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
124 |
|
125 |
Thread.sleep(100); // System.gc(); |
126 |
if (print) |
127 |
System.out.print("SynchronousQueue "); |
128 |
oneRun(new SynchronousQueue<Integer>(), n, iters); |
129 |
|
130 |
Thread.sleep(100); // System.gc(); |
131 |
if (print) |
132 |
System.out.print("SynchronousQueue(fair) "); |
133 |
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 |
|
145 |
Thread.sleep(100); // System.gc(); |
146 |
if (print) |
147 |
System.out.print("PriorityBlockingQueue "); |
148 |
oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters); |
149 |
|
150 |
Thread.sleep(100); // System.gc(); |
151 |
if (print) |
152 |
System.out.print("ArrayBlockingQueue(fair)"); |
153 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
154 |
} |
155 |
|
156 |
abstract static class Stage implements Runnable { |
157 |
final int iters; |
158 |
final BlockingQueue<Integer> queue; |
159 |
final CyclicBarrier barrier; |
160 |
final Phaser lagPhaser; |
161 |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
162 |
queue = q; |
163 |
barrier = b; |
164 |
lagPhaser = s; |
165 |
this.iters = iters; |
166 |
} |
167 |
} |
168 |
|
169 |
static class Producer extends Stage { |
170 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
171 |
int iters) { |
172 |
super(q, b, s, iters); |
173 |
} |
174 |
|
175 |
public void run() { |
176 |
try { |
177 |
barrier.await(); |
178 |
int ps = 0; |
179 |
int r = hashCode(); |
180 |
for (int i = 0; i < iters; ++i) { |
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 ((i & LAG_MASK) == LAG_MASK) |
187 |
lagPhaser.arriveAndAwaitAdvance(); |
188 |
} |
189 |
addProducerSum(ps); |
190 |
barrier.await(); |
191 |
} |
192 |
catch (Exception ie) { |
193 |
ie.printStackTrace(); |
194 |
return; |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
static class Consumer extends Stage { |
200 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
201 |
int iters) { |
202 |
super(q, b, s, iters); |
203 |
} |
204 |
|
205 |
public void run() { |
206 |
try { |
207 |
barrier.await(); |
208 |
int cs = 0; |
209 |
for (int i = 0; i < iters; ++i) { |
210 |
Integer v = queue.take(); |
211 |
int k = v.intValue(); |
212 |
cs += k; |
213 |
if ((i & LAG_MASK) == LAG_MASK) |
214 |
lagPhaser.arriveAndAwaitAdvance(); |
215 |
} |
216 |
addConsumerSum(cs); |
217 |
barrier.await(); |
218 |
} |
219 |
catch (Exception ie) { |
220 |
ie.printStackTrace(); |
221 |
return; |
222 |
} |
223 |
} |
224 |
|
225 |
} |
226 |
|
227 |
static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception { |
228 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
229 |
CyclicBarrier barrier = new CyclicBarrier(n * 2 + 1, timer); |
230 |
for (int i = 0; i < n; ++i) { |
231 |
Phaser s = new Phaser(2); |
232 |
pool.execute(new Producer(q, barrier, s, iters)); |
233 |
pool.execute(new Consumer(q, barrier, s, iters)); |
234 |
} |
235 |
barrier.await(); |
236 |
barrier.await(); |
237 |
long time = timer.getTime(); |
238 |
checkSum(); |
239 |
if (print) |
240 |
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer"); |
241 |
} |
242 |
|
243 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
244 |
LTQasSQ() { super(); } |
245 |
public void put(T x) { |
246 |
try { super.transfer(x); } |
247 |
catch (InterruptedException ex) { throw new Error(ex); } |
248 |
} |
249 |
} |
250 |
|
251 |
static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> { |
252 |
int calls; |
253 |
HalfSyncLTQ() { super(); } |
254 |
public void put(T x) { |
255 |
if ((++calls & 1) == 0) |
256 |
super.put(x); |
257 |
else { |
258 |
try { super.transfer(x); } |
259 |
catch (InterruptedException ex) { throw new Error(ex); } |
260 |
} |
261 |
} |
262 |
} |
263 |
} |