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