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 |
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 |
jsr166 |
1.5 |
for (int i = 0; i < POOL_SIZE; ++i) |
38 |
dl |
1.4 |
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 |
jsr166 |
1.5 |
// max lag between a producer and consumer to avoid |
45 |
dl |
1.4 |
// 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 |
jsr166 |
1.5 |
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 |
jsr166 |
1.9 |
for (int k = 1, i = 1; i <= maxn;) { |
58 |
dl |
1.1 |
System.out.println("Producers:" + i); |
59 |
dl |
1.4 |
oneTest(i, ITERS); |
60 |
|
|
if (i == k) { |
61 |
|
|
k = i << 1; |
62 |
|
|
i = i + (i >>> 1); |
63 |
jsr166 |
1.5 |
} |
64 |
|
|
else |
65 |
dl |
1.4 |
i = k; |
66 |
dl |
1.1 |
} |
67 |
|
|
pool.shutdown(); |
68 |
dl |
1.4 |
} |
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 |
jsr166 |
1.5 |
oneTest(j, it); |
76 |
dl |
1.4 |
System.out.print("."); |
77 |
|
|
it += 1000; |
78 |
|
|
} |
79 |
|
|
System.gc(); |
80 |
|
|
it = 20000; |
81 |
|
|
for (int j = 5; j > 0; --j) { |
82 |
jsr166 |
1.5 |
oneTest(j, it); |
83 |
dl |
1.4 |
System.out.print("."); |
84 |
|
|
it += 10000; |
85 |
|
|
} |
86 |
|
|
System.gc(); |
87 |
|
|
System.out.println(); |
88 |
|
|
} |
89 |
dl |
1.1 |
|
90 |
dl |
1.4 |
static void oneTest(int n, int iters) throws Exception { |
91 |
|
|
int fairIters = iters/16; |
92 |
|
|
|
93 |
|
|
Thread.sleep(100); // System.gc(); |
94 |
dl |
1.1 |
if (print) |
95 |
dl |
1.4 |
System.out.print("LinkedTransferQueue "); |
96 |
|
|
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
97 |
dl |
1.1 |
|
98 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
99 |
dl |
1.1 |
if (print) |
100 |
|
|
System.out.print("LinkedBlockingQueue "); |
101 |
dl |
1.4 |
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
102 |
dl |
1.1 |
|
103 |
dl |
1.4 |
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 |
jsr166 |
1.5 |
|
113 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
114 |
|
|
if (print) |
115 |
|
|
System.out.print("ArrayBlockingQueue "); |
116 |
|
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
117 |
dl |
1.1 |
|
118 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
119 |
dl |
1.1 |
if (print) |
120 |
|
|
System.out.print("SynchronousQueue "); |
121 |
dl |
1.4 |
oneRun(new SynchronousQueue<Integer>(), n, iters); |
122 |
dl |
1.1 |
|
123 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
124 |
dl |
1.1 |
if (print) |
125 |
dl |
1.2 |
System.out.print("SynchronousQueue(fair) "); |
126 |
dl |
1.4 |
oneRun(new SynchronousQueue<Integer>(true), n, iters); |
127 |
|
|
|
128 |
|
|
Thread.sleep(100); // System.gc(); |
129 |
|
|
if (print) |
130 |
|
|
System.out.print("LinkedTransferQueue(xfer)"); |
131 |
|
|
oneRun(new LTQasSQ<Integer>(), n, iters); |
132 |
|
|
|
133 |
|
|
Thread.sleep(100); // System.gc(); |
134 |
|
|
if (print) |
135 |
|
|
System.out.print("LinkedTransferQueue(half)"); |
136 |
|
|
oneRun(new HalfSyncLTQ<Integer>(), n, iters); |
137 |
jsr166 |
1.5 |
|
138 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
139 |
|
|
if (print) |
140 |
|
|
System.out.print("PriorityBlockingQueue "); |
141 |
|
|
oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters); |
142 |
dl |
1.2 |
|
143 |
dl |
1.4 |
Thread.sleep(100); // System.gc(); |
144 |
dl |
1.2 |
if (print) |
145 |
dl |
1.1 |
System.out.print("ArrayBlockingQueue(fair)"); |
146 |
dl |
1.4 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
147 |
|
|
|
148 |
dl |
1.1 |
} |
149 |
jsr166 |
1.5 |
|
150 |
jsr166 |
1.7 |
abstract static class Stage implements Runnable { |
151 |
dl |
1.1 |
final int iters; |
152 |
|
|
final BlockingQueue<Integer> queue; |
153 |
|
|
final CyclicBarrier barrier; |
154 |
dl |
1.4 |
final Phaser lagPhaser; |
155 |
|
|
final int lag; |
156 |
jsr166 |
1.6 |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
157 |
|
|
int iters, int lag) { |
158 |
jsr166 |
1.5 |
queue = q; |
159 |
dl |
1.1 |
barrier = b; |
160 |
dl |
1.4 |
lagPhaser = s; |
161 |
dl |
1.1 |
this.iters = iters; |
162 |
dl |
1.4 |
this.lag = lag; |
163 |
dl |
1.1 |
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
static class Producer extends Stage { |
167 |
dl |
1.4 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
168 |
|
|
int iters, int lag) { |
169 |
|
|
super(q, b, s, iters, lag); |
170 |
dl |
1.1 |
} |
171 |
|
|
|
172 |
|
|
public void run() { |
173 |
|
|
try { |
174 |
|
|
barrier.await(); |
175 |
dl |
1.4 |
int ps = 0; |
176 |
|
|
int r = hashCode(); |
177 |
|
|
int j = 0; |
178 |
dl |
1.1 |
for (int i = 0; i < iters; ++i) { |
179 |
dl |
1.4 |
r = LoopHelpers.compute7(r); |
180 |
|
|
Integer v = intPool[r & POOL_MASK]; |
181 |
|
|
int k = v.intValue(); |
182 |
|
|
queue.put(v); |
183 |
|
|
ps += k; |
184 |
|
|
if (++j == lag) { |
185 |
|
|
j = 0; |
186 |
|
|
lagPhaser.arriveAndAwaitAdvance(); |
187 |
|
|
} |
188 |
dl |
1.1 |
} |
189 |
dl |
1.4 |
addProducerSum(ps); |
190 |
dl |
1.1 |
barrier.await(); |
191 |
|
|
} |
192 |
jsr166 |
1.5 |
catch (Exception ie) { |
193 |
|
|
ie.printStackTrace(); |
194 |
|
|
return; |
195 |
dl |
1.1 |
} |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
static class Consumer extends Stage { |
200 |
dl |
1.4 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, |
201 |
jsr166 |
1.5 |
int iters, int lag) { |
202 |
dl |
1.4 |
super(q, b, s, iters, lag); |
203 |
dl |
1.1 |
} |
204 |
|
|
|
205 |
|
|
public void run() { |
206 |
|
|
try { |
207 |
|
|
barrier.await(); |
208 |
dl |
1.4 |
int cs = 0; |
209 |
|
|
int j = 0; |
210 |
dl |
1.1 |
for (int i = 0; i < iters; ++i) { |
211 |
dl |
1.4 |
Integer v = queue.take(); |
212 |
|
|
int k = v.intValue(); |
213 |
|
|
cs += k; |
214 |
|
|
if (++j == lag) { |
215 |
|
|
j = 0; |
216 |
|
|
lagPhaser.arriveAndAwaitAdvance(); |
217 |
|
|
} |
218 |
dl |
1.1 |
} |
219 |
dl |
1.4 |
addConsumerSum(cs); |
220 |
dl |
1.1 |
barrier.await(); |
221 |
|
|
} |
222 |
jsr166 |
1.5 |
catch (Exception ie) { |
223 |
|
|
ie.printStackTrace(); |
224 |
|
|
return; |
225 |
dl |
1.1 |
} |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
} |
229 |
|
|
|
230 |
dl |
1.4 |
static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception { |
231 |
|
|
|
232 |
dl |
1.1 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
233 |
dl |
1.4 |
CyclicBarrier barrier = new CyclicBarrier(n + 2, timer); |
234 |
|
|
Phaser s = new Phaser(n + 1); |
235 |
|
|
for (int i = 0; i < n; ++i) { |
236 |
|
|
pool.execute(new Producer(q, barrier, s, iters, LAG)); |
237 |
dl |
1.1 |
} |
238 |
dl |
1.4 |
pool.execute(new Consumer(q, barrier, s, iters * n, LAG * n)); |
239 |
dl |
1.1 |
barrier.await(); |
240 |
|
|
barrier.await(); |
241 |
|
|
long time = timer.getTime(); |
242 |
|
|
checkSum(); |
243 |
|
|
if (print) |
244 |
dl |
1.4 |
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * (n + 1))) + " ns per transfer"); |
245 |
|
|
} |
246 |
jsr166 |
1.5 |
|
247 |
dl |
1.4 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
248 |
|
|
LTQasSQ() { super(); } |
249 |
|
|
public void put(T x) { |
250 |
jsr166 |
1.5 |
try { super.transfer(x); |
251 |
dl |
1.4 |
} catch (InterruptedException ex) { throw new Error(); } |
252 |
|
|
} |
253 |
dl |
1.1 |
} |
254 |
|
|
|
255 |
dl |
1.4 |
static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> { |
256 |
|
|
int calls; |
257 |
|
|
HalfSyncLTQ() { super(); } |
258 |
|
|
public void put(T x) { |
259 |
|
|
if ((++calls & 1) == 0) |
260 |
|
|
super.put(x); |
261 |
|
|
else { |
262 |
jsr166 |
1.5 |
try { super.transfer(x); |
263 |
|
|
} catch (InterruptedException ex) { |
264 |
|
|
throw new Error(); |
265 |
dl |
1.4 |
} |
266 |
|
|
} |
267 |
|
|
} |
268 |
|
|
} |
269 |
dl |
1.1 |
} |