ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentQueueLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/ConcurrentQueueLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.15 by jsr166, Thu Dec 18 18:13:06 2014 UTC

# Line 1 | Line 1
1   /*
2 * @test %I% %E%
3 * @bug 4486658
4 * @compile -source 1.5 ConcurrentQueueLoops.java
5 * @run main/timeout=230 ConcurrentQueueLoops
6 * @summary Checks that a set of threads can repeatedly get and modify items
7 */
8 /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
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.*;
8   import java.util.concurrent.*;
9 + import java.util.concurrent.locks.*;
10   import java.util.concurrent.atomic.*;
11  
12   public class ConcurrentQueueLoops {
13      static final ExecutorService pool = Executors.newCachedThreadPool();
20    static AtomicInteger totalItems;
14      static boolean print = false;
15 +    static final Integer zero = new Integer(0);
16 +    static final Integer one = new Integer(1);
17 +    static int workMask;
18 +    static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
19 +    static final int BATCH_SIZE = 8;
20  
21      public static void main(String[] args) throws Exception {
22 <        int maxStages = 8;
23 <        int items = 100000;
26 <
22 >        int maxStages = 100;
23 >        int work = 1024;
24          Class klass = null;
25          if (args.length > 0) {
26              try {
27                  klass = Class.forName(args[0]);
28 <            } catch(ClassNotFoundException e) {
28 >            } catch (ClassNotFoundException e) {
29                  throw new RuntimeException("Class " + args[0] + " not found.");
30              }
31          }
35        else
36            klass = java.util.concurrent.ConcurrentLinkedQueue.class;
32  
33 <        if (args.length > 1)
33 >        if (args.length > 1)
34              maxStages = Integer.parseInt(args[1]);
35  
36 +        if (args.length > 2)
37 +            work = Integer.parseInt(args[2]);
38 +
39 +        workMask = work - 1;
40          System.out.print("Class: " + klass.getName());
41 <        System.out.println(" stages: " + maxStages);
41 >        System.out.print(" stages: " + maxStages);
42 >        System.out.println(" work: " + work);
43  
44          print = false;
45          System.out.println("Warmup...");
46 <        oneRun(klass, 1, items);
46 >        //        oneRun(klass, 4);
47 >        //
48          Thread.sleep(100);
49 <        oneRun(klass, 1, items);
49 >        oneRun(klass, 1);
50          Thread.sleep(100);
51          print = true;
52  
53 <        for (int i = 1; i <= maxStages; i += (i+1) >>> 1) {
54 <            oneRun(klass, i, items);
53 >        for (int k = 1, i = 1; i <= maxStages;) {
54 >            oneRun(klass, i);
55 >            if (i == k) {
56 >                k = i << 1;
57 >                i = i + (i >>> 1);
58 >            }
59 >            else
60 >                i = k;
61          }
62          pool.shutdown();
63 <   }
63 >    }
64  
65 <    static class Stage implements Callable<Integer> {
65 >    static final class Stage implements Callable<Integer> {
66          final Queue<Integer> queue;
67          final CyclicBarrier barrier;
68 <        int items;
69 <        Stage (Queue<Integer> q, CyclicBarrier b, int items) {
70 <            queue = q;
68 >        final int nthreads;
69 >        Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) {
70 >            queue = q;
71              barrier = b;
72 <            this.items = items;
72 >            this.nthreads = nthreads;
73 >        }
74 >
75 >        static int compute(int l) {
76 >            if (l == 0)
77 >                return (int) System.nanoTime();
78 >            int nn = (l >>> 7) & workMask;
79 >            while (nn-- > 0)
80 >                l = LoopHelpers.compute6(l);
81 >            return l;
82          }
83  
84          public Integer call() {
69            // Repeatedly take something from queue if possible,
70            // transform it, and put back in.
85              try {
86                  barrier.await();
87 <                int l = (int)System.nanoTime();
87 >                long now = System.nanoTime();
88 >                long stopTime = now + RUN_TIME_NANOS;
89 >                int l = (int) now;
90                  int takes = 0;
91 <                int seq = l;
91 >                int misses = 0;
92 >                int lmask = 1;
93                  for (;;) {
94 +                    l = compute(l);
95                      Integer item = queue.poll();
96                      if (item != null) {
97                          ++takes;
98 <                        l = LoopHelpers.compute2(item.intValue());
99 <                    }
100 <                    else if (takes != 0) {
101 <                        totalItems.getAndAdd(-takes);
102 <                        takes = 0;
103 <                    }
104 <                    else if (totalItems.get() <= 0)
105 <                        break;
106 <                    l = LoopHelpers.compute1(l);
107 <                    if (items > 0) {
108 <                        --items;
91 <                        while (!queue.offer(new Integer(l^seq++))) ;
98 >                        if (item == one)
99 >                            l = LoopHelpers.compute6(l);
100 >                    } else if ((misses++ & 255) == 0 &&
101 >                               System.nanoTime() >= stopTime) {
102 >                        return Integer.valueOf(takes);
103 >                    } else {
104 >                        for (int i = 0; i < BATCH_SIZE; ++i) {
105 >                            queue.offer(((l & lmask)== 0) ? zero : one);
106 >                            if ((lmask <<= 1) == 0) lmask = 1;
107 >                            if (i != 0) l = compute(l);
108 >                        }
109                      }
93                    else if ( (l & (3 << 5)) == 0) // spinwait
94                        Thread.sleep(1);
110                  }
96                return new Integer(l);
111              }
112 <            catch (Exception ie) {
112 >            catch (Exception ie) {
113                  ie.printStackTrace();
114                  throw new Error("Call loop failed");
115              }
116          }
117      }
118  
119 <    static void oneRun(Class klass, int n, int items) throws Exception {
120 <        Queue<Integer> q = (Queue<Integer>)klass.newInstance();
119 >    static void oneRun(Class klass, int n) throws Exception {
120 >        Queue<Integer> q = (Queue<Integer>) klass.newInstance();
121          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
122          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
109        totalItems = new AtomicInteger(n * items);
123          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
124 <        for (int i = 0; i < n; ++i)
125 <            results.add(pool.submit(new Stage(q, barrier, items)));
124 >        for (int i = 0; i < n; ++i)
125 >            results.add(pool.submit(new Stage(q, barrier, n)));
126  
127          if (print)
128              System.out.print("Threads: " + n + "\t:");
# Line 122 | Line 135 | public class ConcurrentQueueLoops {
135          }
136          long endTime = System.nanoTime();
137          long time = endTime - timer.startTime;
138 +        long ips = 1000000000L * total / time;
139 +
140 +        if (print)
141 +            System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
142          if (print)
143 <            System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item");
127 <        if (total == 0) // avoid overoptimization
128 <            System.out.println("useless result: " + total);
129 <        
143 >            System.out.println();
144      }
145 +
146   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines