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

Comparing jsr166/src/test/loops/CASLoops.java (file contents):
Revision 1.2 by dl, Sun Aug 7 19:25:55 2005 UTC vs.
Revision 1.10 by jsr166, Thu Jan 15 18:34:18 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea and released to the public domain, as explained at
3 < * http://creativecommons.org/licenses/publicdomain
3 > * http://creativecommons.org/publicdomain/zero/1.0/
4   */
5  
6   /*
# Line 26 | Line 26
26   * if this program is called with (any) second argument
27   */
28  
29 <
29 > import java.util.concurrent.*;
30   import java.util.concurrent.atomic.AtomicInteger;
31 import java.util.concurrent.atomic.AtomicLong;
31   import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
32 < import java.util.concurrent.*;
32 > import java.util.concurrent.atomic.AtomicLong;
33   import java.util.concurrent.locks.*;
34  
35   public class CASLoops {
# Line 43 | Line 42 | public class CASLoops {
42      static boolean includeLocks = false;
43  
44      public static void main(String[] args) throws Exception {
45 <        if (args.length > 0)
45 >        if (args.length > 0)
46              maxThreads = Integer.parseInt(args[0]);
47  
48 +        loopIters = new long[maxThreads+1];
49 +
50          if (args.length > 1)
51              includeLocks = true;
52  
# Line 56 | Line 57 | public class CASLoops {
57              System.out.print(".");
58          }
59  
60 <        for (int i = 1; i <= maxThreads; ++i)
60 >        for (int i = 1; i <= maxThreads; ++i)
61              loopIters[i] = 0;
62  
63          for (int j = 0; j < 2; ++j) {
# Line 67 | Line 68 | public class CASLoops {
68              }
69          }
70  
71 <        for (int i = 1; i <= maxThreads; ++i)
71 >        for (int i = 1; i <= maxThreads; ++i)
72              loopIters[i] = 0;
73  
74          for (int j = 0; j < TRIALS; ++j) {
# Line 85 | Line 86 | public class CASLoops {
86  
87      static final LoopHelpers.MarsagliaRandom rng = new LoopHelpers.MarsagliaRandom();
88  
89 <    static final long[] loopIters = new long[maxThreads+1];
89 >    static long[] loopIters;
90  
91      static final class NonAtomicInteger {
92          volatile int readBarrier;
# Line 109 | Line 110 | public class CASLoops {
110      static final class UpdaterAtomicInteger {
111          volatile int value;
112  
113 <        static final AtomicIntegerFieldUpdater<UpdaterAtomicInteger>
113 >        static final AtomicIntegerFieldUpdater<UpdaterAtomicInteger>
114                  valueUpdater = AtomicIntegerFieldUpdater.newUpdater
115                  (UpdaterAtomicInteger.class, "value");
116  
116
117          UpdaterAtomicInteger() {}
118          int get() {
119              return value;
# Line 159 | Line 159 | public class CASLoops {
159          synchronized void set(int val) { value = val; }
160      }
161  
162
162      static final class LockedInteger extends AbstractQueuedSynchronizer {
163          int value;
164          LockedInteger() {}
# Line 189 | Line 188 | public class CASLoops {
188                  unlock();
189              }
190          }
191 <        void set(int val) {
192 <            lock();
191 >        void set(int val) {
192 >            lock();
193              try {
194 <                value = val;
194 >                value = val;
195              } finally {
196                  unlock();
197              }
198          }
199      }
200  
201 <    // All these versions are copy-paste-hacked to avoid
201 >    // All these versions are copy-paste-hacked to avoid
202      // contamination with virtual call resolution etc.
203  
204      // Use fixed-length unrollable inner loops to reduce safepoint checks
# Line 218 | Line 217 | public class CASLoops {
217  
218          public void run() {
219              try {
220 <                barrier.await();
220 >                barrier.await();
221                  long i = iters;
222                  int y = 0;
223                  int succ = 0;
# Line 236 | Line 235 | public class CASLoops {
235                  successes.getAndAdd(succ);
236                  barrier.await();
237              }
238 <            catch (Exception ie) {
239 <                return;
238 >            catch (Exception ie) {
239 >                return;
240              }
241          }
242      }
# Line 255 | Line 254 | public class CASLoops {
254  
255          public void run() {
256              try {
257 <                barrier.await();
257 >                barrier.await();
258                  long i = iters;
259                  int y = 0;
260                  int succ = 0;
# Line 273 | Line 272 | public class CASLoops {
272                  successes.getAndAdd(succ);
273                  barrier.await();
274              }
275 <            catch (Exception ie) {
276 <                return;
275 >            catch (Exception ie) {
276 >                return;
277              }
278          }
279      }
# Line 292 | Line 291 | public class CASLoops {
291  
292          public void run() {
293              try {
294 <                barrier.await();
294 >                barrier.await();
295                  long i = iters;
296                  int y = 0;
297                  int succ = 0;
# Line 310 | Line 309 | public class CASLoops {
309                  successes.getAndAdd(succ);
310                  barrier.await();
311              }
312 <            catch (Exception ie) {
313 <                return;
312 >            catch (Exception ie) {
313 >                return;
314              }
315          }
316      }
# Line 329 | Line 328 | public class CASLoops {
328  
329          public void run() {
330              try {
331 <                barrier.await();
331 >                barrier.await();
332                  long i = iters;
333                  int y = 0;
334                  int succ = 0;
# Line 347 | Line 346 | public class CASLoops {
346                  successes.getAndAdd(succ);
347                  barrier.await();
348              }
349 <            catch (Exception ie) {
350 <                return;
349 >            catch (Exception ie) {
350 >                return;
351              }
352          }
353      }
# Line 366 | Line 365 | public class CASLoops {
365  
366          public void run() {
367              try {
368 <                barrier.await();
368 >                barrier.await();
369                  long i = iters;
370                  int y = 0;
371                  int succ = 0;
# Line 384 | Line 383 | public class CASLoops {
383                  successes.getAndAdd(succ);
384                  barrier.await();
385              }
386 <            catch (Exception ie) {
387 <                return;
386 >            catch (Exception ie) {
387 >                return;
388              }
389          }
390      }
# Line 403 | Line 402 | public class CASLoops {
402  
403          public void run() {
404              try {
405 <                barrier.await();
405 >                barrier.await();
406                  long i = iters;
407                  int y = 0;
408                  int succ = 0;
# Line 421 | Line 420 | public class CASLoops {
420                  successes.getAndAdd(succ);
421                  barrier.await();
422              }
423 <            catch (Exception ie) {
424 <                return;
423 >            catch (Exception ie) {
424 >                return;
425              }
426          }
427      }
# Line 442 | Line 441 | public class CASLoops {
441  
442          public void run() {
443              try {
444 <                barrier.await();
444 >                barrier.await();
445                  long iters = 0;
446                  int y = 0;
447                  int succ = 0;
# Line 465 | Line 464 | public class CASLoops {
464                  successes.getAndAdd(succ);
465                  barrier.await();
466              }
467 <            catch (Exception ie) {
468 <                return;
467 >            catch (Exception ie) {
468 >                return;
469              }
470          }
471      }
# Line 477 | Line 476 | public class CASLoops {
476          CyclicBarrier b = new CyclicBarrier(n+1);
477          totalIters.set(0);
478          NonAtomicInteger a = new NonAtomicInteger();
479 <        for (int j = 0; j < n; ++j)
479 >        for (int j = 0; j < n; ++j)
480              new Thread(new NACalibrationLoop(endTime, a, b)).start();
481          b.await();
482          b.await();
# Line 491 | Line 490 | public class CASLoops {
490          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
491          CyclicBarrier b = new CyclicBarrier(n+1, timer);
492          NonAtomicInteger a = new NonAtomicInteger();
493 <        for (int j = 0; j < n; ++j)
493 >        for (int j = 0; j < n; ++j)
494              new Thread(new NonAtomicLoop(iters, a, b)).start();
495          b.await();
496          b.await();
# Line 503 | Line 502 | public class CASLoops {
502          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
503          CyclicBarrier b = new CyclicBarrier(n+1, timer);
504          UpdaterAtomicInteger a = new UpdaterAtomicInteger();
505 <        for (int j = 0; j < n; ++j)
505 >        for (int j = 0; j < n; ++j)
506              new Thread(new UpdaterAtomicLoop(iters, a, b)).start();
507          b.await();
508          b.await();
# Line 515 | Line 514 | public class CASLoops {
514          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
515          CyclicBarrier b = new CyclicBarrier(n+1, timer);
516          AtomicInteger a = new AtomicInteger();
517 <        for (int j = 0; j < n; ++j)
517 >        for (int j = 0; j < n; ++j)
518              new Thread(new AtomicLoop(iters, a, b)).start();
519          b.await();
520          b.await();
# Line 527 | Line 526 | public class CASLoops {
526          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
527          CyclicBarrier b = new CyclicBarrier(n+1, timer);
528          VolatileInteger a = new VolatileInteger();
529 <        for (int j = 0; j < n; ++j)
529 >        for (int j = 0; j < n; ++j)
530              new Thread(new VolatileLoop(iters, a, b)).start();
531          b.await();
532          b.await();
# Line 535 | Line 534 | public class CASLoops {
534          return timer.getTime();
535      }
536  
538
537      static long runSynched(int n, long iters) throws Exception {
538          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
539          CyclicBarrier b = new CyclicBarrier(n+1, timer);
540          SynchedInteger a = new SynchedInteger();
541 <        for (int j = 0; j < n; ++j)
541 >        for (int j = 0; j < n; ++j)
542              new Thread(new SynchedLoop(iters, a, b)).start();
543          b.await();
544          b.await();
# Line 552 | Line 550 | public class CASLoops {
550          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
551          CyclicBarrier b = new CyclicBarrier(n+1, timer);
552          LockedInteger a = new LockedInteger();
553 <        for (int j = 0; j < n; ++j)
553 >        for (int j = 0; j < n; ++j)
554              new Thread(new LockedLoop(iters, a, b)).start();
555          b.await();
556          b.await();
# Line 560 | Line 558 | public class CASLoops {
558          return timer.getTime();
559      }
560  
561 <    static void report(String tag, long runtime, long basetime,
561 >    static void report(String tag, long runtime, long basetime,
562                         int nthreads, long iters) {
563          System.out.print(tag);
564          long t = (runtime - basetime) / iters;
565          if (nthreads > NCPUS)
566              t = t * NCPUS / nthreads;
567          System.out.print(LoopHelpers.rightJustify(t));
568 <        double secs = (double)(runtime) / 1000000000.0;
568 >        double secs = (double) runtime / 1000000000.0;
569          System.out.println("\t " + secs + "s run time");
570      }
573        
571  
572      static void oneRun(int i, long iters, boolean print) throws Exception {
573 <        if (print)
574 <            System.out.println("threads : " + i +
575 <                               " base iters per thread per run : " +
573 >        if (print)
574 >            System.out.println("threads : " + i +
575 >                               " base iters per thread per run : " +
576                                 LoopHelpers.rightJustify(loopIters[i]));
577 <        long ntime = runNonAtomic(i,  iters);
577 >        long ntime = runNonAtomic(i, iters);
578          if (print)
579              report("Base        : ", ntime, ntime, i, iters);
580          Thread.sleep(100L);
# Line 600 | Line 597 | public class CASLoops {
597              report("Mutex       : ", mtime, ntime, i, iters);
598          Thread.sleep(100L);
599          long stime = runSynched(i, iters);
600 <        if (print)
600 >        if (print)
601              report("Synchronized: ", stime, ntime, i, iters);
602          Thread.sleep(100L);
603      }
607
608
604   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines