ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ProducerConsumerLoops.java
Revision: 1.7
Committed: Tue Mar 15 19:47:05 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.6: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

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     int k = 1;
58     for (int i = 1; i <= maxPairs;) {
59     System.out.println("Pairs:" + i);
60 dl 1.3 oneTest(i, ITERS);
61 dl 1.1 if (i == k) {
62     k = i << 1;
63     i = i + (i >>> 1);
64 jsr166 1.4 }
65     else
66 dl 1.1 i = k;
67     }
68     pool.shutdown();
69 dl 1.3 }
70 dl 1.1
71 dl 1.3 static void warmup() throws Exception {
72     print = false;
73     System.out.print("Warmup ");
74     int it = 2000;
75     for (int j = 5; j > 0; --j) {
76 jsr166 1.4 oneTest(j, it);
77 dl 1.3 System.out.print(".");
78     it += 1000;
79     }
80     System.gc();
81     it = 20000;
82     for (int j = 5; j > 0; --j) {
83 jsr166 1.4 oneTest(j, it);
84 dl 1.3 System.out.print(".");
85     it += 10000;
86     }
87     System.gc();
88     System.out.println();
89     }
90    
91     static void oneTest(int n, int iters) throws Exception {
92     int fairIters = iters/16;
93    
94     Thread.sleep(100); // System.gc();
95 dl 1.1 if (print)
96 dl 1.3 System.out.print("LinkedTransferQueue ");
97     oneRun(new LinkedTransferQueue<Integer>(), n, iters);
98 dl 1.1
99 dl 1.3 Thread.sleep(100); // System.gc();
100 dl 1.1 if (print)
101     System.out.print("LinkedBlockingQueue ");
102 dl 1.3 oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
103    
104     Thread.sleep(100); // System.gc();
105     if (print)
106     System.out.print("LinkedBlockingQueue(cap)");
107     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
108 dl 1.1
109 dl 1.3 Thread.sleep(100); // System.gc();
110 dl 1.1 if (print)
111     System.out.print("LinkedBlockingDeque ");
112 dl 1.3 oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
113 jsr166 1.4
114 dl 1.3 Thread.sleep(100); // System.gc();
115     if (print)
116     System.out.print("ArrayBlockingQueue ");
117     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
118 dl 1.1
119 dl 1.3 Thread.sleep(100); // System.gc();
120 dl 1.1 if (print)
121     System.out.print("SynchronousQueue ");
122 dl 1.3 oneRun(new SynchronousQueue<Integer>(), n, iters);
123 dl 1.1
124 jsr166 1.4
125 dl 1.3 Thread.sleep(100); // System.gc();
126 dl 1.1 if (print)
127     System.out.print("SynchronousQueue(fair) ");
128 dl 1.3 oneRun(new SynchronousQueue<Integer>(true), n, iters);
129    
130     Thread.sleep(100); // System.gc();
131     if (print)
132     System.out.print("LinkedTransferQueue(xfer)");
133     oneRun(new LTQasSQ<Integer>(), n, iters);
134 dl 1.1
135 dl 1.3 Thread.sleep(100); // System.gc();
136     if (print)
137     System.out.print("LinkedTransferQueue(half)");
138     oneRun(new HalfSyncLTQ<Integer>(), n, iters);
139 jsr166 1.4
140 dl 1.3 Thread.sleep(100); // System.gc();
141 dl 1.1 if (print)
142     System.out.print("PriorityBlockingQueue ");
143 dl 1.3 oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters);
144 dl 1.1
145 dl 1.3 Thread.sleep(100); // System.gc();
146 dl 1.1 if (print)
147     System.out.print("ArrayBlockingQueue(fair)");
148 dl 1.3 oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
149 dl 1.1
150     }
151 jsr166 1.4
152 jsr166 1.6 abstract static class Stage implements Runnable {
153 dl 1.1 final int iters;
154     final BlockingQueue<Integer> queue;
155     final CyclicBarrier barrier;
156 dl 1.3 final Phaser lagPhaser;
157 jsr166 1.5 Stage(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
158 jsr166 1.4 queue = q;
159 dl 1.1 barrier = b;
160 dl 1.3 lagPhaser = s;
161 dl 1.1 this.iters = iters;
162     }
163     }
164    
165     static class Producer extends Stage {
166 dl 1.3 Producer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
167     int iters) {
168     super(q, b, s, iters);
169 dl 1.1 }
170    
171     public void run() {
172     try {
173     barrier.await();
174 dl 1.3 int ps = 0;
175     int r = hashCode();
176 dl 1.1 for (int i = 0; i < iters; ++i) {
177 dl 1.3 r = LoopHelpers.compute7(r);
178     Integer v = intPool[r & POOL_MASK];
179     int k = v.intValue();
180     queue.put(v);
181     ps += k;
182     if ((i & LAG_MASK) == LAG_MASK)
183     lagPhaser.arriveAndAwaitAdvance();
184 dl 1.1 }
185 dl 1.3 addProducerSum(ps);
186 dl 1.1 barrier.await();
187     }
188 jsr166 1.4 catch (Exception ie) {
189     ie.printStackTrace();
190     return;
191 dl 1.1 }
192     }
193     }
194    
195     static class Consumer extends Stage {
196 dl 1.3 Consumer(BlockingQueue<Integer> q, CyclicBarrier b, Phaser s,
197 jsr166 1.4 int iters) {
198 dl 1.3 super(q, b, s, iters);
199 dl 1.1 }
200    
201     public void run() {
202     try {
203     barrier.await();
204 dl 1.3 int cs = 0;
205 dl 1.1 for (int i = 0; i < iters; ++i) {
206 dl 1.3 Integer v = queue.take();
207     int k = v.intValue();
208     cs += k;
209     if ((i & LAG_MASK) == LAG_MASK)
210     lagPhaser.arriveAndAwaitAdvance();
211 dl 1.1 }
212 dl 1.3 addConsumerSum(cs);
213 dl 1.1 barrier.await();
214     }
215 jsr166 1.4 catch (Exception ie) {
216     ie.printStackTrace();
217     return;
218 dl 1.1 }
219     }
220    
221     }
222    
223 dl 1.3 static void oneRun(BlockingQueue<Integer> q, int n, int iters) throws Exception {
224 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
225 dl 1.3 CyclicBarrier barrier = new CyclicBarrier(n * 2 + 1, timer);
226     for (int i = 0; i < n; ++i) {
227     Phaser s = new Phaser(2);
228     pool.execute(new Producer(q, barrier, s, iters));
229     pool.execute(new Consumer(q, barrier, s, iters));
230 dl 1.1 }
231     barrier.await();
232     barrier.await();
233     long time = timer.getTime();
234     checkSum();
235     if (print)
236 dl 1.3 System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer");
237     }
238    
239     static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
240     LTQasSQ() { super(); }
241     public void put(T x) {
242 jsr166 1.4 try { super.transfer(x);
243 dl 1.3 } catch (InterruptedException ex) { throw new Error(); }
244     }
245 dl 1.1 }
246    
247 dl 1.3 static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
248     int calls;
249     HalfSyncLTQ() { super(); }
250     public void put(T x) {
251     if ((++calls & 1) == 0)
252     super.put(x);
253     else {
254 jsr166 1.4 try { super.transfer(x);
255     } catch (InterruptedException ex) {
256     throw new Error();
257 dl 1.3 }
258     }
259     }
260     }
261 dl 1.1 }