ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ProducerConsumerLoops.java
Revision: 1.8
Committed: Thu Dec 18 18:13:06 2014 UTC (9 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +1 -2 lines
Log Message:
move k = 1 into loop initializer

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7 dl 1.3 import java.util.*;
8 dl 1.1 import java.util.concurrent.*;
9 dl 1.3 //import jsr166y.*;
10 dl 1.1
11     public class ProducerConsumerLoops {
12 dl 1.3 static final int NCPUS = Runtime.getRuntime().availableProcessors();
13     static final Random rng = new Random();
14 dl 1.1 static final ExecutorService pool = Executors.newCachedThreadPool();
15     static boolean print = false;
16     static int producerSum;
17     static int consumerSum;
18     static synchronized void addProducerSum(int x) {
19     producerSum += x;
20     }
21    
22     static synchronized void addConsumerSum(int x) {
23     consumerSum += x;
24     }
25    
26     static synchronized void checkSum() {
27     if (producerSum != consumerSum)
28     throw new Error("CheckSum mismatch");
29     }
30    
31 dl 1.3 // Number of elements passed around -- must be power of two
32     // Elements are reused from pool to minimize alloc impact
33     static final int POOL_SIZE = 1 << 7;
34     static final int POOL_MASK = POOL_SIZE-1;
35     static final Integer[] intPool = new Integer[POOL_SIZE];
36     static {
37 jsr166 1.4 for (int i = 0; i < POOL_SIZE; ++i)
38 dl 1.3 intPool[i] = Integer.valueOf(i);
39     }
40    
41     // Number of puts by producers or takes by consumers
42     static final int ITERS = 1 << 20;
43    
44 jsr166 1.4 // max lag between a producer and consumer to avoid
45 dl 1.3 // this becoming a GC test rather than queue test.
46     // Used only per-pair to lessen impact on queue sync
47     static final int LAG_MASK = (1 << 12) - 1;
48    
49 dl 1.1 public static void main(String[] args) throws Exception {
50 dl 1.3 int maxPairs = NCPUS * 3 / 2;
51 dl 1.1
52 jsr166 1.4 if (args.length > 0)
53 dl 1.1 maxPairs = Integer.parseInt(args[0]);
54    
55 dl 1.3 warmup();
56 dl 1.1 print = true;
57 jsr166 1.8 for (int k = 1, i = 1; i <= maxPairs;) {
58 dl 1.1 System.out.println("Pairs:" + i);
59 dl 1.3 oneTest(i, ITERS);
60 dl 1.1 if (i == k) {
61     k = i << 1;
62     i = i + (i >>> 1);
63 jsr166 1.4 }
64     else
65 dl 1.1 i = k;
66     }
67     pool.shutdown();
68 dl 1.3 }
69 dl 1.1
70 dl 1.3 static void warmup() throws Exception {
71     print = false;
72     System.out.print("Warmup ");
73     int it = 2000;
74     for (int j = 5; j > 0; --j) {
75 jsr166 1.4 oneTest(j, it);
76 dl 1.3 System.out.print(".");
77     it += 1000;
78     }
79     System.gc();
80     it = 20000;
81     for (int j = 5; j > 0; --j) {
82 jsr166 1.4 oneTest(j, it);
83 dl 1.3 System.out.print(".");
84     it += 10000;
85     }
86     System.gc();
87     System.out.println();
88     }
89    
90     static void oneTest(int n, int iters) throws Exception {
91     int fairIters = iters/16;
92    
93     Thread.sleep(100); // System.gc();
94 dl 1.1 if (print)
95 dl 1.3 System.out.print("LinkedTransferQueue ");
96     oneRun(new LinkedTransferQueue<Integer>(), n, iters);
97 dl 1.1
98 dl 1.3 Thread.sleep(100); // System.gc();
99 dl 1.1 if (print)
100     System.out.print("LinkedBlockingQueue ");
101 dl 1.3 oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
102    
103     Thread.sleep(100); // System.gc();
104     if (print)
105     System.out.print("LinkedBlockingQueue(cap)");
106     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
107 dl 1.1
108 dl 1.3 Thread.sleep(100); // System.gc();
109 dl 1.1 if (print)
110     System.out.print("LinkedBlockingDeque ");
111 dl 1.3 oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
112 jsr166 1.4
113 dl 1.3 Thread.sleep(100); // System.gc();
114     if (print)
115     System.out.print("ArrayBlockingQueue ");
116     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
117 dl 1.1
118 dl 1.3 Thread.sleep(100); // System.gc();
119 dl 1.1 if (print)
120     System.out.print("SynchronousQueue ");
121 dl 1.3 oneRun(new SynchronousQueue<Integer>(), n, iters);
122 dl 1.1
123 jsr166 1.4
124 dl 1.3 Thread.sleep(100); // System.gc();
125 dl 1.1 if (print)
126     System.out.print("SynchronousQueue(fair) ");
127 dl 1.3 oneRun(new SynchronousQueue<Integer>(true), n, iters);
128    
129     Thread.sleep(100); // System.gc();
130     if (print)
131     System.out.print("LinkedTransferQueue(xfer)");
132     oneRun(new LTQasSQ<Integer>(), n, iters);
133 dl 1.1
134 dl 1.3 Thread.sleep(100); // System.gc();
135     if (print)
136     System.out.print("LinkedTransferQueue(half)");
137     oneRun(new HalfSyncLTQ<Integer>(), n, iters);
138 jsr166 1.4
139 dl 1.3 Thread.sleep(100); // System.gc();
140 dl 1.1 if (print)
141     System.out.print("PriorityBlockingQueue ");
142 dl 1.3 oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters);
143 dl 1.1
144 dl 1.3 Thread.sleep(100); // System.gc();
145 dl 1.1 if (print)
146     System.out.print("ArrayBlockingQueue(fair)");
147 dl 1.3 oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
148 dl 1.1
149     }
150 jsr166 1.4
151 jsr166 1.6 abstract static class Stage implements Runnable {
152 dl 1.1 final int iters;
153     final BlockingQueue<Integer> queue;
154     final CyclicBarrier barrier;
155 dl 1.3 final Phaser lagPhaser;
156 jsr166 1.5 Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
157 jsr166 1.4 queue = q;
158 dl 1.1 barrier = b;
159 dl 1.3 lagPhaser = s;
160 dl 1.1 this.iters = iters;
161     }
162     }
163    
164     static class Producer extends Stage {
165 dl 1.3 Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
166     int iters) {
167     super(q, b, s, iters);
168 dl 1.1 }
169    
170     public void run() {
171     try {
172     barrier.await();
173 dl 1.3 int ps = 0;
174     int r = hashCode();
175 dl 1.1 for (int i = 0; i < iters; ++i) {
176 dl 1.3 r = LoopHelpers.compute7(r);
177     Integer v = intPool[r & POOL_MASK];
178     int k = v.intValue();
179     queue.put(v);
180     ps += k;
181     if ((i & LAG_MASK) == LAG_MASK)
182     lagPhaser.arriveAndAwaitAdvance();
183 dl 1.1 }
184 dl 1.3 addProducerSum(ps);
185 dl 1.1 barrier.await();
186     }
187 jsr166 1.4 catch (Exception ie) {
188     ie.printStackTrace();
189     return;
190 dl 1.1 }
191     }
192     }
193    
194     static class Consumer extends Stage {
195 dl 1.3 Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
196 jsr166 1.4 int iters) {
197 dl 1.3 super(q, b, s, iters);
198 dl 1.1 }
199    
200     public void run() {
201     try {
202     barrier.await();
203 dl 1.3 int cs = 0;
204 dl 1.1 for (int i = 0; i < iters; ++i) {
205 dl 1.3 Integer v = queue.take();
206     int k = v.intValue();
207     cs += k;
208     if ((i & LAG_MASK) == LAG_MASK)
209     lagPhaser.arriveAndAwaitAdvance();
210 dl 1.1 }
211 dl 1.3 addConsumerSum(cs);
212 dl 1.1 barrier.await();
213     }
214 jsr166 1.4 catch (Exception ie) {
215     ie.printStackTrace();
216     return;
217 dl 1.1 }
218     }
219    
220     }
221    
222 dl 1.3 static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception {
223 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
224 dl 1.3 CyclicBarrier barrier = new CyclicBarrier(n * 2 + 1, timer);
225     for (int i = 0; i < n; ++i) {
226     Phaser s = new Phaser(2);
227     pool.execute(new Producer(q, barrier, s, iters));
228     pool.execute(new Consumer(q, barrier, s, iters));
229 dl 1.1 }
230     barrier.await();
231     barrier.await();
232     long time = timer.getTime();
233     checkSum();
234     if (print)
235 dl 1.3 System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer");
236     }
237    
238     static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
239     LTQasSQ() { super(); }
240     public void put(T x) {
241 jsr166 1.4 try { super.transfer(x);
242 dl 1.3 } catch (InterruptedException ex) { throw new Error(); }
243     }
244 dl 1.1 }
245    
246 dl 1.3 static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
247     int calls;
248     HalfSyncLTQ() { super(); }
249     public void put(T x) {
250     if ((++calls & 1) == 0)
251     super.put(x);
252     else {
253 jsr166 1.4 try { super.transfer(x);
254     } catch (InterruptedException ex) {
255     throw new Error();
256 dl 1.3 }
257     }
258     }
259     }
260 dl 1.1 }