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.2 by dl, Tue Oct 4 20:09:41 2005 UTC

# Line 13 | Line 13
13  
14   import java.util.*;
15   import java.util.concurrent.*;
16 + import java.util.concurrent.locks.*;
17   import java.util.concurrent.atomic.*;
18  
19   public class ConcurrentQueueLoops {
20      static final ExecutorService pool = Executors.newCachedThreadPool();
20    static AtomicInteger totalItems;
21      static boolean print = false;
22 +    static final Integer even = new Integer(42);
23 +    static final Integer odd = new Integer(17);
24 +    static int workMask;
25 +    static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
26  
27      public static void main(String[] args) throws Exception {
28 <        int maxStages = 8;
29 <        int items = 100000;
26 <
28 >        int maxStages = 48;
29 >        int work = 32;
30          Class klass = null;
31          if (args.length > 0) {
32              try {
# Line 32 | Line 35 | public class ConcurrentQueueLoops {
35                  throw new RuntimeException("Class " + args[0] + " not found.");
36              }
37          }
35        else
36            klass = java.util.concurrent.ConcurrentLinkedQueue.class;
38  
39          if (args.length > 1)
40              maxStages = Integer.parseInt(args[1]);
41  
42 +        if (args.length > 2)
43 +            work = Integer.parseInt(args[2]);
44 +
45 +        workMask = work - 1;
46          System.out.print("Class: " + klass.getName());
47 <        System.out.println(" stages: " + maxStages);
47 >        System.out.print(" stages: " + maxStages);
48 >        System.out.println(" work: " + work);
49  
50          print = false;
51          System.out.println("Warmup...");
52 <        oneRun(klass, 1, items);
52 >        oneRun(klass, 4);
53          Thread.sleep(100);
54 <        oneRun(klass, 1, items);
54 >        oneRun(klass, 1);
55          Thread.sleep(100);
56          print = true;
57  
58 <        for (int i = 1; i <= maxStages; i += (i+1) >>> 1) {
59 <            oneRun(klass, i, items);
58 >        int k = 1;
59 >        for (int i = 1; i <= maxStages;) {
60 >            oneRun(klass, i);
61 >            if (i == k) {
62 >                k = i << 1;
63 >                i = i + (i >>> 1);
64 >            }
65 >            else
66 >                i = k;
67          }
68          pool.shutdown();
69     }
70  
71 <    static class Stage implements Callable<Integer> {
71 >    static final class Stage implements Callable<Integer> {
72          final Queue<Integer> queue;
73          final CyclicBarrier barrier;
74 <        int items;
62 <        Stage (Queue<Integer> q, CyclicBarrier b, int items) {
74 >        Stage (Queue<Integer> q, CyclicBarrier b) {
75              queue = q;
76              barrier = b;
77 <            this.items = items;
77 >        }
78 >
79 >        static int compute127(int l) {
80 >            l = LoopHelpers.compute1(l);
81 >            l = LoopHelpers.compute2(l);
82 >            l = LoopHelpers.compute3(l);
83 >            l = LoopHelpers.compute4(l);
84 >            l = LoopHelpers.compute5(l);
85 >            l = LoopHelpers.compute6(l);
86 >            return l;
87 >        }
88 >
89 >        static int compute(int l) {
90 >            if (l == 0)
91 >                return (int)System.nanoTime();
92 >            int nn =  l & workMask;
93 >            while (nn-- > 0)
94 >                l = compute127(l);
95 >            return l;
96          }
97  
98          public Integer call() {
69            // Repeatedly take something from queue if possible,
70            // transform it, and put back in.
99              try {
100                  barrier.await();
101 <                int l = (int)System.nanoTime();
101 >                long now = System.nanoTime();
102 >                long stopTime = now + RUN_TIME_NANOS;
103 >                int l = (int)now;
104                  int takes = 0;
105 <                int seq = l;
105 >                int misses = 0;
106                  for (;;) {
107 +                    l = compute(l);
108                      Integer item = queue.poll();
109                      if (item != null) {
110 +                        l += item.intValue();
111                          ++takes;
112 <                        l = LoopHelpers.compute2(item.intValue());
113 <                    }
82 <                    else if (takes != 0) {
83 <                        totalItems.getAndAdd(-takes);
84 <                        takes = 0;
85 <                    }
86 <                    else if (totalItems.get() <= 0)
112 >                    } else if ((misses++ & 255) == 0 &&
113 >                               System.nanoTime() >= stopTime) {
114                          break;
115 <                    l = LoopHelpers.compute1(l);
116 <                    if (items > 0) {
117 <                        --items;
91 <                        while (!queue.offer(new Integer(l^seq++))) ;
115 >                    } else {
116 >                        Integer a = ((l++ & 4)== 0)? even : odd;
117 >                        queue.add(a);
118                      }
93                    else if ( (l & (3 << 5)) == 0) // spinwait
94                        Thread.sleep(1);
119                  }
120 <                return new Integer(l);
120 >                return new Integer(takes);
121              }
122              catch (Exception ie) {
123                  ie.printStackTrace();
# Line 102 | Line 126 | public class ConcurrentQueueLoops {
126          }
127      }
128  
129 <    static void oneRun(Class klass, int n, int items) throws Exception {
129 >    static void oneRun(Class klass, int n) throws Exception {
130          Queue<Integer> q = (Queue<Integer>)klass.newInstance();
131          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
132          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
109        totalItems = new AtomicInteger(n * items);
133          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
134          for (int i = 0; i < n; ++i)
135 <            results.add(pool.submit(new Stage(q, barrier, items)));
135 >            results.add(pool.submit(new Stage(q, barrier)));
136  
137          if (print)
138              System.out.print("Threads: " + n + "\t:");
# Line 122 | Line 145 | public class ConcurrentQueueLoops {
145          }
146          long endTime = System.nanoTime();
147          long time = endTime - timer.startTime;
148 +        long tpi = time / total;
149          if (print)
150 <            System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item");
127 <        if (total == 0) // avoid overoptimization
128 <            System.out.println("useless result: " + total);
150 >            System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
151          
152      }
153 +
154   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines