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

Comparing jsr166/src/test/loops/RLJBar.java (file contents):
Revision 1.4 by jsr166, Wed Sep 1 07:20:36 2010 UTC vs.
Revision 1.8 by jsr166, Mon Dec 19 19:18:35 2011 UTC

# Line 1 | Line 1
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/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   // Yet another contended object monitor throughput test
# Line 15 | Line 15 | import java.util.concurrent.locks.*;
15   class Producer extends Thread
16   {
17      //  private static Hashtable buddiesOnline = new Hashtable();
18 <  private static Map buddiesOnline = new ConcurrentHashMap();
19 <  public Producer (String name) { super(name); }
18 >    private static Map buddiesOnline = new ConcurrentHashMap();
19 >    public Producer(String name) { super(name); }
20  
21 <  public void run()
22 <    {
23 <      Object key = null ;
24 <      final ReentrantLock dr = RLJBar.DeathRow;
25 <      final ReentrantLock bar = RLJBar.bar;
26 <      final ReentrantLock end = RLJBar.End;
27 <      final Condition endCondition = RLJBar.EndCondition;
28 <      if (RLJBar.OneKey) key = new Integer(0) ;         // per-thread v. per iteration
29 <
30 <      // The barrier has a number of interesting effects:
31 <      // 1.     It enforces full LWP provisioning on T1.
32 <      //                (nearly all workers park concurrently).
33 <      // 2.     It gives the C2 compiler thread(s) a chance to run.
34 <      //                By transiently quiescing the workings the C2 threads
35 <      //                might avoid starvation.
36 <      //
37 <
38 <      try {
39 <          bar.lock();
40 <          try {
41 <              ++RLJBar.nUp ;
42 <              if (RLJBar.nUp == RLJBar.nThreads) {
43 <                  if (RLJBar.quiesce != 0) {
44 <                      RLJBar.barCondition.awaitNanos(RLJBar.quiesce * 1000000) ;
45 <                  }
46 <                  RLJBar.epoch = System.currentTimeMillis () ;
47 <                  RLJBar.barCondition.signalAll () ;
48 <                  //                  System.out.print ("Consensus ") ;
49 <              }
50 <              if (RLJBar.UseBar) {
51 <                  while (RLJBar.nUp != RLJBar.nThreads) {
52 <                      RLJBar.barCondition.await () ;
53 <                  }
54 <              }
55 <          }
56 <          finally {
57 <              bar.unlock();
58 <          }
59 <      } catch (Exception ex) {
60 <        System.out.println ("Exception in barrier: " + ex) ;
61 <      }
62 <
63 <      // Main execution time ... the code being timed ...
64 <      // HashTable.get() is highly contended (serial).
65 <      for (int loop = 1; loop < 100000 ;loop++) {
66 <        if (!RLJBar.OneKey) key = new Integer(0) ;
67 <        buddiesOnline.get(key);
68 <      }
69 <
70 <      // Mutator epilog:
71 <      // The following code determines if the test will/wont include (measure)
72 <      // thread death time.
73 <
74 <      end.lock();
75 <      try {
76 <        ++RLJBar.nDead ;
77 <        if (RLJBar.nDead == RLJBar.nUp) {
78 <            //          System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms") ;
79 <          endCondition.signalAll() ;
80 <        }
81 <      }
82 <      finally {
83 <          end.unlock();
84 <      }
85 <      dr.lock();
86 <      dr.unlock();
21 >    public void run() {
22 >        Object key = null;
23 >        final ReentrantLock dr = RLJBar.DeathRow;
24 >        final ReentrantLock bar = RLJBar.bar;
25 >        final ReentrantLock end = RLJBar.End;
26 >        final Condition endCondition = RLJBar.EndCondition;
27 >        if (RLJBar.OneKey) key = new Integer(0);         // per-thread v. per iteration
28 >
29 >        // The barrier has a number of interesting effects:
30 >        // 1.     It enforces full LWP provisioning on T1.
31 >        //                (nearly all workers park concurrently).
32 >        // 2.     It gives the C2 compiler thread(s) a chance to run.
33 >        //                By transiently quiescing the workings the C2 threads
34 >        //                might avoid starvation.
35 >        //
36 >
37 >        try {
38 >            bar.lock();
39 >            try {
40 >                ++RLJBar.nUp;
41 >                if (RLJBar.nUp == RLJBar.nThreads) {
42 >                    if (RLJBar.quiesce != 0) {
43 >                        RLJBar.barCondition.awaitNanos(RLJBar.quiesce * 1000000);
44 >                    }
45 >                    RLJBar.epoch = System.currentTimeMillis();
46 >                    RLJBar.barCondition.signalAll();
47 >                    //                  System.out.print ("Consensus ");
48 >                }
49 >                if (RLJBar.UseBar) {
50 >                    while (RLJBar.nUp != RLJBar.nThreads) {
51 >                        RLJBar.barCondition.await();
52 >                    }
53 >                }
54 >            }
55 >            finally {
56 >                bar.unlock();
57 >            }
58 >        } catch (Exception ex) {
59 >            System.out.println("Exception in barrier: " + ex);
60 >        }
61 >
62 >        // Main execution time ... the code being timed ...
63 >        // HashTable.get() is highly contended (serial).
64 >        for (int loop = 1; loop < 100000 ;loop++) {
65 >            if (!RLJBar.OneKey) key = new Integer(0);
66 >            buddiesOnline.get(key);
67 >        }
68 >
69 >        // Mutator epilog:
70 >        // The following code determines if the test will/wont include (measure)
71 >        // thread death time.
72 >
73 >        end.lock();
74 >        try {
75 >            ++RLJBar.nDead;
76 >            if (RLJBar.nDead == RLJBar.nUp) {
77 >                //          System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms");
78 >                endCondition.signalAll();
79 >            }
80 >        }
81 >        finally {
82 >            end.unlock();
83 >        }
84 >        dr.lock();
85 >        dr.unlock();
86      }
87   }
88  
# Line 92 | Line 91 | public class RLJBar
91   {
92  
93      public static final int ITERS = 10;
94 <  public static boolean OneKey = false ;                        // alloc once or once per iteration
94 >    public static boolean OneKey = false;                        // alloc once or once per iteration
95  
96 <  public static boolean UseBar = false ;
97 <  public static int nThreads = 100 ;
98 <  public static int nUp = 0 ;
99 <  public static int nDead = 0 ;
100 <  public static ReentrantLock bar = new ReentrantLock() ;
101 <  public static Condition barCondition = bar.newCondition() ;
102 <  public static long epoch ;
103 <  public static ReentrantLock DeathRow = new ReentrantLock () ;
104 <  public static ReentrantLock End = new ReentrantLock () ;
105 <  public static int quiesce = 0 ;
106 <  public static Condition EndCondition = End.newCondition();
107 <
108 <  public static void main (String[] args)    {
109 <      int argix = 0 ;
110 <      if (argix < args.length && args[argix].equals("-o")) {
111 <          ++argix ;
112 <          OneKey = true ;
113 <          System.out.println ("OneKey") ;
114 <      }
115 <      if (argix < args.length && args[argix].equals ("-b")) {
116 <          ++argix ;
117 <          UseBar = true ;
118 <          System.out.println ("UseBar") ;
119 <      }
120 <      if (argix < args.length && args[argix].equals ("-q")) {
121 <          ++argix ;
122 <          if (argix < args.length) {
123 <              quiesce = Integer.parseInt (args[argix++]) ;
124 <              System.out.println ("Quiesce " + quiesce + " msecs") ;
125 <          }
126 <      }
127 <      for (int k = 0; k < ITERS; ++k)
128 <          oneRun();
129 <  }
96 >    public static boolean UseBar = false;
97 >    public static int nThreads = 100;
98 >    public static int nUp = 0;
99 >    public static int nDead = 0;
100 >    public static ReentrantLock bar = new ReentrantLock();
101 >    public static Condition barCondition = bar.newCondition();
102 >    public static long epoch;
103 >    public static ReentrantLock DeathRow = new ReentrantLock();
104 >    public static ReentrantLock End = new ReentrantLock();
105 >    public static int quiesce = 0;
106 >    public static Condition EndCondition = End.newCondition();
107 >
108 >    public static void main(String[] args) {
109 >        int argix = 0;
110 >        if (argix < args.length && args[argix].equals("-o")) {
111 >            ++argix;
112 >            OneKey = true;
113 >            System.out.println("OneKey");
114 >        }
115 >        if (argix < args.length && args[argix].equals ("-b")) {
116 >            ++argix;
117 >            UseBar = true;
118 >            System.out.println("UseBar");
119 >        }
120 >        if (argix < args.length && args[argix].equals ("-q")) {
121 >            ++argix;
122 >            if (argix < args.length) {
123 >                quiesce = Integer.parseInt(args[argix++]);
124 >                System.out.println("Quiesce " + quiesce + " msecs");
125 >            }
126 >        }
127 >        for (int k = 0; k < ITERS; ++k)
128 >            oneRun();
129 >    }
130  
131      public static void oneRun() {
132 <        DeathRow = new ReentrantLock () ;
133 <        End = new ReentrantLock () ;
132 >        DeathRow = new ReentrantLock();
133 >        End = new ReentrantLock();
134          EndCondition = End.newCondition();
135  
136 <        nDead = nUp = 0 ;
137 <        long cyBase = System.currentTimeMillis () ;
136 >        nDead = nUp = 0;
137 >        long cyBase = System.currentTimeMillis();
138          DeathRow.lock();
139          try {
140 <            for (int i = 1; i <= nThreads ; i++) {
140 >            for (int i = 1; i <= nThreads; i++) {
141                  new Producer("Producer" + i).start();
142              }
143              try {
144                  End.lock();
145                  try {
146                      while (nDead != nThreads)
147 <                        EndCondition.await() ;
147 >                        EndCondition.await();
148                  }
149                  finally {
150                      End.unlock();
151                  }
152              } catch (Exception ex) {
153 <                System.out.println ("Exception in End: " + ex) ;
153 >                System.out.println("Exception in End: " + ex);
154              }
155          }
156          finally {
157              DeathRow.unlock();
158          }
159 <        System.out.println ("Outer time: " + (System.currentTimeMillis()-cyBase)) ;
159 >        System.out.println("Outer time: " + (System.currentTimeMillis()-cyBase));
160  
161          // Let workers quiesce/exit.
162 <        try { Thread.sleep (1000) ; } catch (Exception ex) {} ;
162 >        try { Thread.sleep (1000); } catch (Exception ex) {};
163      }
164   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines