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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import java.util.*; |
8 |
< |
import java.util.concurrent.*; |
9 |
< |
//import jsr166y.*; |
7 |
> |
import java.util.Queue; |
8 |
> |
import java.util.Random; |
9 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
10 |
> |
import java.util.concurrent.CyclicBarrier; |
11 |
> |
import java.util.concurrent.BlockingQueue; |
12 |
> |
import java.util.concurrent.ConcurrentLinkedDeque; |
13 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
14 |
> |
import java.util.concurrent.ExecutorService; |
15 |
> |
import java.util.concurrent.Executors; |
16 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
17 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
18 |
> |
import java.util.concurrent.LinkedTransferQueue; |
19 |
> |
import java.util.concurrent.Phaser; |
20 |
> |
import java.util.concurrent.PriorityBlockingQueue; |
21 |
|
|
22 |
|
public class OfferPollLoops { |
23 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
45 |
|
static final int POOL_MASK = POOL_SIZE-1; |
46 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
47 |
|
static { |
48 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
48 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
49 |
|
intPool[i] = Integer.valueOf(i); |
50 |
|
} |
51 |
|
|
52 |
|
// Number of puts by producers or takes by consumers |
53 |
|
static final int ITERS = 1 << 20; |
54 |
|
|
55 |
< |
// max lag between a producer and consumer to avoid |
55 |
> |
// max lag between a producer and consumer to avoid |
56 |
|
// this becoming a GC test rather than queue test. |
57 |
|
// Used only per-pair to lessen impact on queue sync |
58 |
|
static final int LAG_MASK = (1 << 12) - 1; |
60 |
|
public static void main(String[] args) throws Exception { |
61 |
|
int maxN = NCPUS * 3 / 2; |
62 |
|
|
63 |
< |
if (args.length > 0) |
63 |
> |
if (args.length > 0) |
64 |
|
maxN = Integer.parseInt(args[0]); |
65 |
|
|
66 |
|
warmup(); |
67 |
|
print = true; |
68 |
< |
int k = 1; |
58 |
< |
for (int i = 1; i <= maxN;) { |
68 |
> |
for (int k = 1, i = 1; i <= maxN;) { |
69 |
|
System.out.println("Pairs:" + i); |
70 |
|
oneTest(i, ITERS); |
71 |
|
if (i == k) { |
72 |
|
k = i << 1; |
73 |
|
i = i + (i >>> 1); |
74 |
< |
} |
75 |
< |
else |
74 |
> |
} |
75 |
> |
else |
76 |
|
i = k; |
77 |
|
} |
78 |
|
pool.shutdown(); |
83 |
|
System.out.print("Warmup "); |
84 |
|
int it = 2000; |
85 |
|
for (int j = 5; j > 0; --j) { |
86 |
< |
oneTest(j, it); |
86 |
> |
oneTest(j, it); |
87 |
|
System.out.print("."); |
88 |
|
it += 1000; |
89 |
|
} |
90 |
|
System.gc(); |
91 |
|
it = 20000; |
92 |
|
for (int j = 5; j > 0; --j) { |
93 |
< |
oneTest(j, it); |
93 |
> |
oneTest(j, it); |
94 |
|
System.out.print("."); |
95 |
|
it += 10000; |
96 |
|
} |
113 |
|
|
114 |
|
Thread.sleep(100); // System.gc(); |
115 |
|
if (print) |
116 |
+ |
System.out.print("ConcurrentLinkedDeque "); |
117 |
+ |
oneRun(new ConcurrentLinkedDeque<Integer>(), n, iters); |
118 |
+ |
|
119 |
+ |
Thread.sleep(100); // System.gc(); |
120 |
+ |
if (print) |
121 |
|
System.out.print("LinkedBlockingQueue "); |
122 |
|
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
123 |
|
|
130 |
|
if (print) |
131 |
|
System.out.print("LinkedBlockingDeque "); |
132 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
133 |
< |
|
133 |
> |
|
134 |
|
Thread.sleep(100); // System.gc(); |
135 |
|
if (print) |
136 |
|
System.out.print("ArrayBlockingQueue "); |
137 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
138 |
|
|
124 |
– |
|
139 |
|
Thread.sleep(100); // System.gc(); |
140 |
|
if (print) |
141 |
|
System.out.print("PriorityBlockingQueue "); |
145 |
|
if (print) |
146 |
|
System.out.print("ArrayBlockingQueue(fair)"); |
147 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
134 |
– |
|
148 |
|
} |
149 |
< |
|
150 |
< |
static abstract class Stage implements Runnable { |
149 |
> |
|
150 |
> |
abstract static class Stage implements Runnable { |
151 |
|
final int iters; |
152 |
|
final Queue<Integer> queue; |
153 |
|
final CyclicBarrier barrier; |
154 |
|
final Phaser lagPhaser; |
155 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, Phaser s, |
156 |
< |
int iters) { |
144 |
< |
queue = q; |
155 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
156 |
> |
queue = q; |
157 |
|
barrier = b; |
158 |
|
lagPhaser = s; |
159 |
|
this.iters = iters; |
183 |
|
break; |
184 |
|
if ((i & LAG_MASK) == LAG_MASK) |
185 |
|
lagPhaser.arriveAndAwaitAdvance(); |
186 |
< |
} |
186 |
> |
} |
187 |
|
} |
188 |
|
addProducerSum(ps); |
189 |
|
barrier.await(); |
190 |
|
} |
191 |
< |
catch (Exception ie) { |
192 |
< |
ie.printStackTrace(); |
193 |
< |
return; |
191 |
> |
catch (Exception ie) { |
192 |
> |
ie.printStackTrace(); |
193 |
> |
return; |
194 |
|
} |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
|
static class Consumer extends Stage { |
199 |
|
Consumer(Queue<Integer> q, CyclicBarrier b, Phaser s, |
200 |
< |
int iters) { |
200 |
> |
int iters) { |
201 |
|
super(q, b, s, iters); |
202 |
|
} |
203 |
|
|
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 |
|
|
245 |
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer"); |
246 |
|
} |
247 |
|
|
236 |
– |
|
248 |
|
} |
238 |
– |
|