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