ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentQueueLoops.java
Revision: 1.19
Committed: Sat Dec 31 19:02:43 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.18: +7 -4 lines
Log Message:
organize imports

File Contents

# Content
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 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
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();
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 = 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) {
32 throw new RuntimeException("Class " + args[0] + " not found.");
33 }
34 }
35
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.print(" stages: " + maxStages);
45 System.out.println(" work: " + work);
46
47 print = false;
48 System.out.println("Warmup...");
49 // oneRun(klass, 4);
50 //
51 Thread.sleep(100);
52 oneRun(klass, 1);
53 Thread.sleep(100);
54 print = true;
55
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 }
67
68 static final class Stage implements Callable<Integer> {
69 final Queue<Integer> queue;
70 final CyclicBarrier barrier;
71 final int nthreads;
72 Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) {
73 queue = q;
74 barrier = b;
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() {
88 try {
89 barrier.await();
90 long now = System.nanoTime();
91 long stopTime = now + RUN_TIME_NANOS;
92 int l = (int) now;
93 int takes = 0;
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 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 }
113 }
114 }
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) 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);
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, n)));
130
131 if (print)
132 System.out.print("Threads: " + n + "\t:");
133 barrier.await();
134 int total = 0;
135 for (int i = 0; i < n; ++i) {
136 Future<Integer> f = results.get(i);
137 Integer r = f.get();
138 total += r.intValue();
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();
148 }
149
150 }