ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentQueueLoops.java
Revision: 1.15
Committed: Thu Dec 18 18:13:06 2014 UTC (9 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +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.5 * Expert Group and released to the public domain, as explained at
4 jsr166 1.12 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.*;
8     import java.util.concurrent.*;
9 dl 1.2 import java.util.concurrent.locks.*;
10 dl 1.1 import java.util.concurrent.atomic.*;
11    
12     public class ConcurrentQueueLoops {
13     static final ExecutorService pool = Executors.newCachedThreadPool();
14     static boolean print = false;
15 dl 1.3 static final Integer zero = new Integer(0);
16     static final Integer one = new Integer(1);
17 dl 1.2 static int workMask;
18     static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
19 dl 1.3 static final int BATCH_SIZE = 8;
20 dl 1.1
21     public static void main(String[] args) throws Exception {
22 dl 1.3 int maxStages = 100;
23     int work = 1024;
24 dl 1.1 Class klass = null;
25     if (args.length > 0) {
26     try {
27     klass = Class.forName(args[0]);
28 jsr166 1.8 } catch (ClassNotFoundException e) {
29 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
30     }
31     }
32    
33 jsr166 1.7 if (args.length > 1)
34 dl 1.1 maxStages = Integer.parseInt(args[1]);
35    
36 jsr166 1.7 if (args.length > 2)
37 dl 1.2 work = Integer.parseInt(args[2]);
38    
39     workMask = work - 1;
40 dl 1.1 System.out.print("Class: " + klass.getName());
41 dl 1.2 System.out.print(" stages: " + maxStages);
42     System.out.println(" work: " + work);
43 dl 1.1
44     print = false;
45     System.out.println("Warmup...");
46 dl 1.4 // oneRun(klass, 4);
47     //
48 dl 1.1 Thread.sleep(100);
49 dl 1.2 oneRun(klass, 1);
50 dl 1.1 Thread.sleep(100);
51     print = true;
52    
53 jsr166 1.15 for (int k = 1, i = 1; i <= maxStages;) {
54 dl 1.2 oneRun(klass, i);
55     if (i == k) {
56     k = i << 1;
57     i = i + (i >>> 1);
58 jsr166 1.7 }
59     else
60 dl 1.2 i = k;
61 dl 1.1 }
62     pool.shutdown();
63 jsr166 1.13 }
64 dl 1.1
65 dl 1.2 static final class Stage implements Callable<Integer> {
66 dl 1.1 final Queue<Integer> queue;
67     final CyclicBarrier barrier;
68 dl 1.3 final int nthreads;
69 jsr166 1.11 Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) {
70 jsr166 1.7 queue = q;
71 dl 1.1 barrier = b;
72 dl 1.3 this.nthreads = nthreads;
73 dl 1.2 }
74    
75     static int compute(int l) {
76 jsr166 1.7 if (l == 0)
77 jsr166 1.10 return (int) System.nanoTime();
78 jsr166 1.14 int nn = (l >>> 7) & workMask;
79 jsr166 1.7 while (nn-- > 0)
80 dl 1.3 l = LoopHelpers.compute6(l);
81 dl 1.2 return l;
82 dl 1.1 }
83    
84     public Integer call() {
85     try {
86     barrier.await();
87 dl 1.2 long now = System.nanoTime();
88     long stopTime = now + RUN_TIME_NANOS;
89 jsr166 1.10 int l = (int) now;
90 dl 1.1 int takes = 0;
91 dl 1.2 int misses = 0;
92 dl 1.3 int lmask = 1;
93 dl 1.1 for (;;) {
94 dl 1.2 l = compute(l);
95 dl 1.1 Integer item = queue.poll();
96     if (item != null) {
97     ++takes;
98 dl 1.3 if (item == one)
99     l = LoopHelpers.compute6(l);
100 dl 1.2 } else if ((misses++ & 255) == 0 &&
101     System.nanoTime() >= stopTime) {
102 dl 1.6 return Integer.valueOf(takes);
103 dl 1.2 } else {
104 dl 1.3 for (int i = 0; i < BATCH_SIZE; ++i) {
105 jsr166 1.8 queue.offer(((l & lmask)== 0) ? zero : one);
106 dl 1.3 if ((lmask <<= 1) == 0) lmask = 1;
107     if (i != 0) l = compute(l);
108     }
109 dl 1.1 }
110     }
111     }
112 jsr166 1.7 catch (Exception ie) {
113 dl 1.1 ie.printStackTrace();
114     throw new Error("Call loop failed");
115     }
116     }
117     }
118    
119 dl 1.2 static void oneRun(Class klass, int n) throws Exception {
120 jsr166 1.9 Queue<Integer> q = (Queue<Integer>) klass.newInstance();
121 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
122     CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
123     ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
124 jsr166 1.7 for (int i = 0; i < n; ++i)
125 dl 1.3 results.add(pool.submit(new Stage(q, barrier, n)));
126 dl 1.1
127     if (print)
128     System.out.print("Threads: " + n + "\t:");
129     barrier.await();
130     int total = 0;
131     for (int i = 0; i < n; ++i) {
132     Future<Integer> f = results.get(i);
133     Integer r = f.get();
134     total += r.intValue();
135     }
136     long endTime = System.nanoTime();
137     long time = endTime - timer.startTime;
138 dl 1.3 long ips = 1000000000L * total / time;
139 jsr166 1.7
140     if (print)
141 dl 1.3 System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
142 dl 1.1 if (print)
143 dl 1.3 System.out.println();
144 dl 1.1 }
145 dl 1.2
146 dl 1.1 }