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.concurrent.ArrayBlockingQueue; |
9 |
> |
import java.util.concurrent.CyclicBarrier; |
10 |
> |
import java.util.concurrent.ConcurrentLinkedDeque; |
11 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
12 |
> |
import java.util.concurrent.ExecutorService; |
13 |
> |
import java.util.concurrent.Executors; |
14 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
15 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
16 |
> |
import java.util.concurrent.LinkedTransferQueue; |
17 |
> |
import java.util.concurrent.Phaser; |
18 |
> |
import java.util.concurrent.PriorityBlockingQueue; |
19 |
|
|
20 |
|
public class OfferPollLoops { |
21 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
13 |
– |
static final Random rng = new Random(); |
22 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
23 |
|
static boolean print = false; |
24 |
|
static int producerSum; |
42 |
|
static final int POOL_MASK = POOL_SIZE-1; |
43 |
|
static final Integer[] intPool = new Integer[POOL_SIZE]; |
44 |
|
static { |
45 |
< |
for (int i = 0; i < POOL_SIZE; ++i) |
45 |
> |
for (int i = 0; i < POOL_SIZE; ++i) |
46 |
|
intPool[i] = Integer.valueOf(i); |
47 |
|
} |
48 |
|
|
49 |
|
// Number of puts by producers or takes by consumers |
50 |
|
static final int ITERS = 1 << 20; |
51 |
|
|
52 |
< |
// max lag between a producer and consumer to avoid |
52 |
> |
// max lag between a producer and consumer to avoid |
53 |
|
// this becoming a GC test rather than queue test. |
54 |
|
// Used only per-pair to lessen impact on queue sync |
55 |
|
static final int LAG_MASK = (1 << 12) - 1; |
57 |
|
public static void main(String[] args) throws Exception { |
58 |
|
int maxN = NCPUS * 3 / 2; |
59 |
|
|
60 |
< |
if (args.length > 0) |
60 |
> |
if (args.length > 0) |
61 |
|
maxN = Integer.parseInt(args[0]); |
62 |
|
|
63 |
|
warmup(); |
64 |
|
print = true; |
65 |
< |
int k = 1; |
58 |
< |
for (int i = 1; i <= maxN;) { |
65 |
> |
for (int k = 1, i = 1; i <= maxN;) { |
66 |
|
System.out.println("Pairs:" + i); |
67 |
|
oneTest(i, ITERS); |
68 |
|
if (i == k) { |
69 |
|
k = i << 1; |
70 |
|
i = i + (i >>> 1); |
71 |
< |
} |
72 |
< |
else |
71 |
> |
} |
72 |
> |
else |
73 |
|
i = k; |
74 |
|
} |
75 |
|
pool.shutdown(); |
80 |
|
System.out.print("Warmup "); |
81 |
|
int it = 2000; |
82 |
|
for (int j = 5; j > 0; --j) { |
83 |
< |
oneTest(j, it); |
83 |
> |
oneTest(j, it); |
84 |
|
System.out.print("."); |
85 |
|
it += 1000; |
86 |
|
} |
87 |
|
System.gc(); |
88 |
|
it = 20000; |
89 |
|
for (int j = 5; j > 0; --j) { |
90 |
< |
oneTest(j, it); |
90 |
> |
oneTest(j, it); |
91 |
|
System.out.print("."); |
92 |
|
it += 10000; |
93 |
|
} |
110 |
|
|
111 |
|
Thread.sleep(100); // System.gc(); |
112 |
|
if (print) |
113 |
+ |
System.out.print("ConcurrentLinkedDeque "); |
114 |
+ |
oneRun(new ConcurrentLinkedDeque<Integer>(), n, iters); |
115 |
+ |
|
116 |
+ |
Thread.sleep(100); // System.gc(); |
117 |
+ |
if (print) |
118 |
|
System.out.print("LinkedBlockingQueue "); |
119 |
|
oneRun(new LinkedBlockingQueue<Integer>(), n, iters); |
120 |
|
|
127 |
|
if (print) |
128 |
|
System.out.print("LinkedBlockingDeque "); |
129 |
|
oneRun(new LinkedBlockingDeque<Integer>(), n, iters); |
130 |
< |
|
130 |
> |
|
131 |
|
Thread.sleep(100); // System.gc(); |
132 |
|
if (print) |
133 |
|
System.out.print("ArrayBlockingQueue "); |
134 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters); |
135 |
|
|
124 |
– |
|
136 |
|
Thread.sleep(100); // System.gc(); |
137 |
|
if (print) |
138 |
|
System.out.print("PriorityBlockingQueue "); |
142 |
|
if (print) |
143 |
|
System.out.print("ArrayBlockingQueue(fair)"); |
144 |
|
oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters); |
134 |
– |
|
145 |
|
} |
146 |
< |
|
147 |
< |
static abstract class Stage implements Runnable { |
146 |
> |
|
147 |
> |
abstract static class Stage implements Runnable { |
148 |
|
final int iters; |
149 |
|
final Queue<Integer> queue; |
150 |
|
final CyclicBarrier barrier; |
151 |
|
final Phaser lagPhaser; |
152 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, Phaser s, |
153 |
< |
int iters) { |
144 |
< |
queue = q; |
152 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, Phaser s, int iters) { |
153 |
> |
queue = q; |
154 |
|
barrier = b; |
155 |
|
lagPhaser = s; |
156 |
|
this.iters = iters; |
180 |
|
break; |
181 |
|
if ((i & LAG_MASK) == LAG_MASK) |
182 |
|
lagPhaser.arriveAndAwaitAdvance(); |
183 |
< |
} |
183 |
> |
} |
184 |
|
} |
185 |
|
addProducerSum(ps); |
186 |
|
barrier.await(); |
187 |
|
} |
188 |
< |
catch (Exception ie) { |
189 |
< |
ie.printStackTrace(); |
190 |
< |
return; |
188 |
> |
catch (Exception ie) { |
189 |
> |
ie.printStackTrace(); |
190 |
> |
return; |
191 |
|
} |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
static class Consumer extends Stage { |
196 |
|
Consumer(Queue<Integer> q, CyclicBarrier b, Phaser s, |
197 |
< |
int iters) { |
197 |
> |
int iters) { |
198 |
|
super(q, b, s, iters); |
199 |
|
} |
200 |
|
|
218 |
|
addConsumerSum(cs); |
219 |
|
barrier.await(); |
220 |
|
} |
221 |
< |
catch (Exception ie) { |
222 |
< |
ie.printStackTrace(); |
223 |
< |
return; |
221 |
> |
catch (Exception ie) { |
222 |
> |
ie.printStackTrace(); |
223 |
> |
return; |
224 |
|
} |
225 |
|
} |
226 |
|
|
242 |
|
System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer"); |
243 |
|
} |
244 |
|
|
236 |
– |
|
245 |
|
} |
238 |
– |
|