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