1 |
|
/* |
2 |
– |
* @test %I% %E% |
3 |
– |
* @bug 4486658 |
4 |
– |
* @compile -source 1.5 ConcurrentQueueLoops.java |
5 |
– |
* @run main/timeout=230 ConcurrentQueueLoops |
6 |
– |
* @summary Checks that a set of threads can repeatedly get and modify items |
7 |
– |
*/ |
8 |
– |
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
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.*; |
8 |
< |
import java.util.concurrent.*; |
9 |
< |
import java.util.concurrent.atomic.*; |
7 |
> |
import java.util.ArrayList; |
8 |
> |
import java.util.Queue; |
9 |
> |
import java.util.concurrent.Callable; |
10 |
> |
import java.util.concurrent.CyclicBarrier; |
11 |
> |
import java.util.concurrent.ExecutorService; |
12 |
> |
import java.util.concurrent.Executors; |
13 |
> |
import java.util.concurrent.Future; |
14 |
|
|
15 |
|
public class ConcurrentQueueLoops { |
16 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
20 |
– |
static AtomicInteger totalItems; |
17 |
|
static boolean print = false; |
18 |
+ |
static final Integer zero = new Integer(0); |
19 |
+ |
static final Integer one = new Integer(1); |
20 |
+ |
static int workMask; |
21 |
+ |
static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L; |
22 |
+ |
static final int BATCH_SIZE = 8; |
23 |
|
|
24 |
|
public static void main(String[] args) throws Exception { |
25 |
< |
int maxStages = 8; |
26 |
< |
int items = 100000; |
27 |
< |
|
27 |
< |
Class klass = null; |
25 |
> |
int maxStages = 100; |
26 |
> |
int work = 1024; |
27 |
> |
Class<?> klass = null; |
28 |
|
if (args.length > 0) { |
29 |
|
try { |
30 |
|
klass = Class.forName(args[0]); |
31 |
< |
} catch(ClassNotFoundException e) { |
31 |
> |
} catch (ClassNotFoundException e) { |
32 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
33 |
|
} |
34 |
|
} |
35 |
– |
else |
36 |
– |
klass = java.util.concurrent.ConcurrentLinkedQueue.class; |
35 |
|
|
36 |
< |
if (args.length > 1) |
36 |
> |
if (args.length > 1) |
37 |
|
maxStages = Integer.parseInt(args[1]); |
38 |
|
|
39 |
+ |
if (args.length > 2) |
40 |
+ |
work = Integer.parseInt(args[2]); |
41 |
+ |
|
42 |
+ |
workMask = work - 1; |
43 |
|
System.out.print("Class: " + klass.getName()); |
44 |
< |
System.out.println(" stages: " + maxStages); |
44 |
> |
System.out.print(" stages: " + maxStages); |
45 |
> |
System.out.println(" work: " + work); |
46 |
|
|
47 |
|
print = false; |
48 |
|
System.out.println("Warmup..."); |
49 |
< |
oneRun(klass, 1, items); |
49 |
> |
// oneRun(klass, 4); |
50 |
> |
// |
51 |
|
Thread.sleep(100); |
52 |
< |
oneRun(klass, 1, items); |
52 |
> |
oneRun(klass, 1); |
53 |
|
Thread.sleep(100); |
54 |
|
print = true; |
55 |
|
|
56 |
< |
for (int i = 1; i <= maxStages; i += (i+1) >>> 1) { |
57 |
< |
oneRun(klass, i, items); |
56 |
> |
for (int k = 1, i = 1; i <= maxStages;) { |
57 |
> |
oneRun(klass, i); |
58 |
> |
if (i == k) { |
59 |
> |
k = i << 1; |
60 |
> |
i = i + (i >>> 1); |
61 |
> |
} |
62 |
> |
else |
63 |
> |
i = k; |
64 |
|
} |
65 |
|
pool.shutdown(); |
66 |
< |
} |
66 |
> |
} |
67 |
|
|
68 |
< |
static class Stage implements Callable<Integer> { |
68 |
> |
static final class Stage implements Callable<Integer> { |
69 |
|
final Queue<Integer> queue; |
70 |
|
final CyclicBarrier barrier; |
71 |
< |
int items; |
72 |
< |
Stage (Queue<Integer> q, CyclicBarrier b, int items) { |
73 |
< |
queue = q; |
71 |
> |
final int nthreads; |
72 |
> |
Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) { |
73 |
> |
queue = q; |
74 |
|
barrier = b; |
75 |
< |
this.items = items; |
75 |
> |
this.nthreads = nthreads; |
76 |
> |
} |
77 |
> |
|
78 |
> |
static int compute(int l) { |
79 |
> |
if (l == 0) |
80 |
> |
return (int) System.nanoTime(); |
81 |
> |
int nn = (l >>> 7) & workMask; |
82 |
> |
while (nn-- > 0) |
83 |
> |
l = LoopHelpers.compute6(l); |
84 |
> |
return l; |
85 |
|
} |
86 |
|
|
87 |
|
public Integer call() { |
69 |
– |
// Repeatedly take something from queue if possible, |
70 |
– |
// transform it, and put back in. |
88 |
|
try { |
89 |
|
barrier.await(); |
90 |
< |
int l = (int)System.nanoTime(); |
90 |
> |
long now = System.nanoTime(); |
91 |
> |
long stopTime = now + RUN_TIME_NANOS; |
92 |
> |
int l = (int) now; |
93 |
|
int takes = 0; |
94 |
< |
int seq = l; |
94 |
> |
int misses = 0; |
95 |
> |
int lmask = 1; |
96 |
|
for (;;) { |
97 |
+ |
l = compute(l); |
98 |
|
Integer item = queue.poll(); |
99 |
|
if (item != null) { |
100 |
|
++takes; |
101 |
< |
l = LoopHelpers.compute2(item.intValue()); |
102 |
< |
} |
103 |
< |
else if (takes != 0) { |
104 |
< |
totalItems.getAndAdd(-takes); |
105 |
< |
takes = 0; |
106 |
< |
} |
107 |
< |
else if (totalItems.get() <= 0) |
108 |
< |
break; |
109 |
< |
l = LoopHelpers.compute1(l); |
110 |
< |
if (items > 0) { |
111 |
< |
--items; |
91 |
< |
while (!queue.offer(new Integer(l^seq++))) ; |
101 |
> |
if (item == one) |
102 |
> |
l = LoopHelpers.compute6(l); |
103 |
> |
} else if ((misses++ & 255) == 0 && |
104 |
> |
System.nanoTime() >= stopTime) { |
105 |
> |
return Integer.valueOf(takes); |
106 |
> |
} else { |
107 |
> |
for (int i = 0; i < BATCH_SIZE; ++i) { |
108 |
> |
queue.offer(((l & lmask)== 0) ? zero : one); |
109 |
> |
if ((lmask <<= 1) == 0) lmask = 1; |
110 |
> |
if (i != 0) l = compute(l); |
111 |
> |
} |
112 |
|
} |
93 |
– |
else if ( (l & (3 << 5)) == 0) // spinwait |
94 |
– |
Thread.sleep(1); |
113 |
|
} |
96 |
– |
return new Integer(l); |
114 |
|
} |
115 |
< |
catch (Exception ie) { |
115 |
> |
catch (Exception ie) { |
116 |
|
ie.printStackTrace(); |
117 |
|
throw new Error("Call loop failed"); |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
< |
static void oneRun(Class klass, int n, int items) throws Exception { |
123 |
< |
Queue<Integer> q = (Queue<Integer>)klass.newInstance(); |
122 |
> |
static void oneRun(Class<?> klass, int n) throws Exception { |
123 |
> |
Queue<Integer> q = |
124 |
> |
(Queue<Integer>) klass.getConstructor().newInstance(); |
125 |
|
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
126 |
|
CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); |
109 |
– |
totalItems = new AtomicInteger(n * items); |
127 |
|
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n); |
128 |
< |
for (int i = 0; i < n; ++i) |
129 |
< |
results.add(pool.submit(new Stage(q, barrier, items))); |
128 |
> |
for (int i = 0; i < n; ++i) |
129 |
> |
results.add(pool.submit(new Stage(q, barrier, n))); |
130 |
|
|
131 |
|
if (print) |
132 |
|
System.out.print("Threads: " + n + "\t:"); |
139 |
|
} |
140 |
|
long endTime = System.nanoTime(); |
141 |
|
long time = endTime - timer.startTime; |
142 |
+ |
long ips = 1000000000L * total / time; |
143 |
+ |
|
144 |
+ |
if (print) |
145 |
+ |
System.out.print(LoopHelpers.rightJustify(ips) + " items per sec"); |
146 |
|
if (print) |
147 |
< |
System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item"); |
127 |
< |
if (total == 0) // avoid overoptimization |
128 |
< |
System.out.println("useless result: " + total); |
129 |
< |
|
147 |
> |
System.out.println(); |
148 |
|
} |
149 |
+ |
|
150 |
|
} |