ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/OfferPollLoops.java
Revision: 1.7
Committed: Thu Dec 18 18:13:06 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +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     * Expert Group and released to the public domain, as explained at
4 jsr166 1.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.*;
8     import java.util.concurrent.*;
9     //import jsr166y.*;
10    
11     public class OfferPollLoops {
12     static final int NCPUS = Runtime.getRuntime().availableProcessors();
13     static final Random rng = new Random();
14     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     // 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 << 8;
34     static final int POOL_MASK = POOL_SIZE-1;
35     static final Integer[] intPool = new Integer[POOL_SIZE];
36     static {
37 jsr166 1.2 for (int i = 0; i < POOL_SIZE; ++i)
38 dl 1.1 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.2 // max lag between a producer and consumer to avoid
45 dl 1.1 // 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     public static void main(String[] args) throws Exception {
50     int maxN = NCPUS * 3 / 2;
51    
52 jsr166 1.2 if (args.length > 0)
53 dl 1.1 maxN = Integer.parseInt(args[0]);
54    
55     warmup();
56     print = true;
57 jsr166 1.7 for (int k = 1, i = 1; i <= maxN;) {
58 dl 1.1 System.out.println("Pairs:" + i);
59     oneTest(i, ITERS);
60     if (i == k) {
61     k = i << 1;
62     i = i + (i >>> 1);
63 jsr166 1.2 }
64     else
65 dl 1.1 i = k;
66     }
67     pool.shutdown();
68     }
69    
70     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.2 oneTest(j, it);
76 dl 1.1 System.out.print(".");
77     it += 1000;
78     }
79     System.gc();
80     it = 20000;
81     for (int j = 5; j > 0; --j) {
82 jsr166 1.2 oneTest(j, it);
83 dl 1.1 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     if (print)
95     System.out.print("LinkedTransferQueue ");
96     oneRun(new LinkedTransferQueue<Integer>(), n, iters);
97    
98     Thread.sleep(100); // System.gc();
99     if (print)
100     System.out.print("ConcurrentLinkedQueue ");
101     oneRun(new ConcurrentLinkedQueue<Integer>(), n, iters);
102    
103     Thread.sleep(100); // System.gc();
104     if (print)
105 dl 1.4 System.out.print("ConcurrentLinkedDeque ");
106     oneRun(new ConcurrentLinkedDeque<Integer>(), n, iters);
107    
108     Thread.sleep(100); // System.gc();
109     if (print)
110 dl 1.1 System.out.print("LinkedBlockingQueue ");
111     oneRun(new LinkedBlockingQueue<Integer>(), n, iters);
112    
113     Thread.sleep(100); // System.gc();
114     if (print)
115     System.out.print("LinkedBlockingQueue(cap)");
116     oneRun(new LinkedBlockingQueue<Integer>(POOL_SIZE), n, iters);
117    
118     Thread.sleep(100); // System.gc();
119     if (print)
120     System.out.print("LinkedBlockingDeque ");
121     oneRun(new LinkedBlockingDeque<Integer>(), n, iters);
122 jsr166 1.2
123 dl 1.1 Thread.sleep(100); // System.gc();
124     if (print)
125     System.out.print("ArrayBlockingQueue ");
126     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE), n, iters);
127    
128    
129     Thread.sleep(100); // System.gc();
130     if (print)
131     System.out.print("PriorityBlockingQueue ");
132     oneRun(new PriorityBlockingQueue<Integer>(), n, fairIters);
133    
134     Thread.sleep(100); // System.gc();
135     if (print)
136     System.out.print("ArrayBlockingQueue(fair)");
137     oneRun(new ArrayBlockingQueue<Integer>(POOL_SIZE, true), n, fairIters);
138    
139     }
140 jsr166 1.2
141 jsr166 1.5 abstract static class Stage implements Runnable {
142 dl 1.1 final int iters;
143     final Queue<Integer> queue;
144     final CyclicBarrier barrier;
145     final Phaser lagPhaser;
146 jsr166 1.3 Stage(Queue<Integer> q, CyclicBarrier b, Phaser s, int iters) {
147 jsr166 1.2 queue = q;
148 dl 1.1 barrier = b;
149     lagPhaser = s;
150     this.iters = iters;
151     }
152     }
153    
154     static class Producer extends Stage {
155     Producer(Queue<Integer> q, CyclicBarrier b, Phaser s,
156     int iters) {
157     super(q, b, s, iters);
158     }
159    
160     public void run() {
161     try {
162     barrier.await();
163     int ps = 0;
164     int r = hashCode();
165     int i = 0;
166     for (;;) {
167     r = LoopHelpers.compute7(r);
168     Integer v = intPool[r & POOL_MASK];
169     int k = v.intValue();
170     if (queue.offer(v)) {
171     ps += k;
172     ++i;
173     if (i >= iters)
174     break;
175     if ((i & LAG_MASK) == LAG_MASK)
176     lagPhaser.arriveAndAwaitAdvance();
177 jsr166 1.2 }
178 dl 1.1 }
179     addProducerSum(ps);
180     barrier.await();
181     }
182 jsr166 1.2 catch (Exception ie) {
183     ie.printStackTrace();
184     return;
185 dl 1.1 }
186     }
187     }
188    
189     static class Consumer extends Stage {
190     Consumer(Queue<Integer> q, CyclicBarrier b, Phaser s,
191 jsr166 1.2 int iters) {
192 dl 1.1 super(q, b, s, iters);
193     }
194    
195     public void run() {
196     try {
197     barrier.await();
198     int cs = 0;
199     int i = 0;
200     for (;;) {
201     Integer v = queue.poll();
202     if (v != null) {
203     int k = v.intValue();
204     cs += k;
205     ++i;
206     if (i >= iters)
207     break;
208     if ((i & LAG_MASK) == LAG_MASK)
209     lagPhaser.arriveAndAwaitAdvance();
210     }
211     }
212     addConsumerSum(cs);
213     barrier.await();
214     }
215 jsr166 1.2 catch (Exception ie) {
216     ie.printStackTrace();
217     return;
218 dl 1.1 }
219     }
220    
221     }
222    
223     static void oneRun(Queue<Integer> q, int n, int iters) throws Exception {
224     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
225     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     }
231     barrier.await();
232     barrier.await();
233     long time = timer.getTime();
234     checkSum();
235     if (print)
236     System.out.println("\t: " + LoopHelpers.rightJustify(time / (iters * n)) + " ns per transfer");
237     }
238    
239    
240     }