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.*; |
8 |
|
9 |
|
10 |
public class TimeoutProducerConsumerLoops { |
11 |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
12 |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
13 |
|
14 |
// Number of elements passed around -- must be power of two |
15 |
// Elements are reused from pool to minimize alloc impact |
16 |
static final int POOL_SIZE = 1 << 8; |
17 |
static final int POOL_MASK = POOL_SIZE-1; |
18 |
static final Integer[] intPool = new Integer[POOL_SIZE]; |
19 |
static { |
20 |
for (int i = 0; i < POOL_SIZE; ++i) |
21 |
intPool[i] = Integer.valueOf(i); |
22 |
} |
23 |
|
24 |
// max lag between a producer and consumer to avoid |
25 |
// this becoming a GC test rather than queue test. |
26 |
// Used only per-pair to lessen impact on queue sync |
27 |
static final int LAG_MASK = (1 << 12) - 1; |
28 |
|
29 |
static boolean print = false; |
30 |
static int producerSum; |
31 |
static int consumerSum; |
32 |
static synchronized void addProducerSum(int x) { |
33 |
producerSum += x; |
34 |
} |
35 |
|
36 |
static synchronized void addConsumerSum(int x) { |
37 |
consumerSum += x; |
38 |
} |
39 |
|
40 |
static synchronized void checkSum() { |
41 |
if (producerSum != consumerSum) { |
42 |
throw new Error("CheckSum mismatch"); |
43 |
} |
44 |
} |
45 |
|
46 |
public static void main(String[] args) throws Exception { |
47 |
int maxPairs = NCPUS * 3 / 2; |
48 |
int iters = 1000000; |
49 |
|
50 |
if (args.length > 0) |
51 |
maxPairs = Integer.parseInt(args[0]); |
52 |
|
53 |
print = true; |
54 |
for (int k = 1, i = 1; i <= maxPairs;) { |
55 |
System.out.println("Pairs:" + i); |
56 |
oneTest(i, iters); |
57 |
Thread.sleep(100); |
58 |
if (i == k) { |
59 |
k = i << 1; |
60 |
i = i + (i >>> 1); |
61 |
} |
62 |
else |
63 |
i = k; |
64 |
} |
65 |
pool.shutdown(); |
66 |
} |
67 |
|
68 |
static void oneTest(int n, int iters) throws Exception { |
69 |
if (print) |
70 |
System.out.print("LinkedTransferQueue "); |
71 |
oneRun(new LinkedTransferQueue<Integer>(), n, iters); |
72 |
|
73 |
if (print) |
74 |
System.out.print("LinkedTransferQueue(xfer)"); |
75 |
oneRun(new LTQasSQ<Integer>(), n, iters); |
76 |
|
77 |
if (print) |
78 |
System.out.print("LinkedBlockingQueue "); |
79 |
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
80 |
|
81 |
if (print) |
82 |
System.out.print("LinkedBlockingQueue(cap) "); |
83 |
oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters); |
84 |
|
85 |
if (print) |
86 |
System.out.print("ArrayBlockingQueue(cap) "); |
87 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
88 |
|
89 |
if (print) |
90 |
System.out.print("LinkedBlockingDeque "); |
91 |
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
92 |
|
93 |
if (print) |
94 |
System.out.print("SynchronousQueue "); |
95 |
oneRun(new SynchronousQueue<Integer>(), n, iters); |
96 |
|
97 |
if (print) |
98 |
System.out.print("SynchronousQueue(fair) "); |
99 |
oneRun(new SynchronousQueue<Integer>(true), n, iters); |
100 |
|
101 |
if (print) |
102 |
System.out.print("PriorityBlockingQueue "); |
103 |
oneRun(new PriorityBlockingQueue<Integer>(), n, iters / 16); |
104 |
|
105 |
if (print) |
106 |
System.out.print("ArrayBlockingQueue(fair) "); |
107 |
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, iters/16); |
108 |
|
109 |
} |
110 |
|
111 |
abstract static class Stage implements Runnable { |
112 |
final int iters; |
113 |
final BlockingQueue<Integer> queue; |
114 |
final CyclicBarrier barrier; |
115 |
final Phaser lagPhaser; |
116 |
Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
117 |
queue = q; |
118 |
barrier = b; |
119 |
lagPhaser = s; |
120 |
this.iters = iters; |
121 |
} |
122 |
} |
123 |
|
124 |
static class Producer extends Stage { |
125 |
Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
126 |
super(q, b, s, iters); |
127 |
} |
128 |
|
129 |
public void run() { |
130 |
try { |
131 |
barrier.await(); |
132 |
int s = 0; |
133 |
int l = hashCode(); |
134 |
int i = 0; |
135 |
long timeout = 1000; |
136 |
while (i < iters) { |
137 |
l = LoopHelpers.compute4(l); |
138 |
Integer v = intPool[l & POOL_MASK]; |
139 |
if (queue.offer(v, timeout, TimeUnit.NANOSECONDS)) { |
140 |
s += LoopHelpers.compute4(v.intValue()); |
141 |
++i; |
142 |
if (timeout > 1) |
143 |
timeout--; |
144 |
if ((i & LAG_MASK) == LAG_MASK) |
145 |
lagPhaser.arriveAndAwaitAdvance(); |
146 |
} |
147 |
else |
148 |
timeout++; |
149 |
} |
150 |
addProducerSum(s); |
151 |
barrier.await(); |
152 |
} |
153 |
catch (Exception ie) { |
154 |
ie.printStackTrace(); |
155 |
return; |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
static class Consumer extends Stage { |
161 |
Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
162 |
super(q, b, s, iters); |
163 |
} |
164 |
|
165 |
public void run() { |
166 |
try { |
167 |
barrier.await(); |
168 |
int l = 0; |
169 |
int s = 0; |
170 |
int i = 0; |
171 |
long timeout = 1000; |
172 |
while (i < iters) { |
173 |
Integer e = queue.poll(timeout, |
174 |
TimeUnit.NANOSECONDS); |
175 |
if (e != null) { |
176 |
l = LoopHelpers.compute4(e.intValue()); |
177 |
s += l; |
178 |
++i; |
179 |
if (timeout > 1) |
180 |
--timeout; |
181 |
if ((i & LAG_MASK) == LAG_MASK) |
182 |
lagPhaser.arriveAndAwaitAdvance(); |
183 |
} |
184 |
else |
185 |
++timeout; |
186 |
} |
187 |
addConsumerSum(s); |
188 |
barrier.await(); |
189 |
} |
190 |
catch (Exception ie) { |
191 |
ie.printStackTrace(); |
192 |
return; |
193 |
} |
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
static void oneRun(BlockingQueue<Integer> q, int npairs, int iters) throws Exception { |
199 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
200 |
CyclicBarrier barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
201 |
for (int i = 0; i < npairs; ++i) { |
202 |
Phaser s = new Phaser(2); |
203 |
pool.execute(new Producer(q, barrier, s, iters)); |
204 |
pool.execute(new Consumer(q, barrier, s, iters)); |
205 |
} |
206 |
barrier.await(); |
207 |
barrier.await(); |
208 |
long time = timer.getTime(); |
209 |
checkSum(); |
210 |
q.clear(); |
211 |
if (print) |
212 |
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * npairs)) + " ns per transfer"); |
213 |
} |
214 |
|
215 |
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
216 |
LTQasSQ() { super(); } |
217 |
public void put(T x) { |
218 |
try { super.transfer(x); |
219 |
} catch (InterruptedException ex) { throw new Error(); } |
220 |
} |
221 |
|
222 |
public boolean offer(T x, long timeout, TimeUnit unit) { |
223 |
return super.offer(x, timeout, unit); |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
} |