ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RLJBar.java
Revision: 1.2
Committed: Mon Feb 19 00:46:06 2007 UTC (17 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.1: +6 -0 lines
Log Message:
Uniform headers

File Contents

# User Rev Content
1 dl 1.2 /*
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
5     */
6    
7 dl 1.1 // Yet another contended object monitor throughput test
8     // adapted from bug reports
9    
10     import java.util.*;
11     import java.lang.*;
12     import java.util.concurrent.*;
13     import java.util.concurrent.locks.*;
14    
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); }
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();
87     }
88     }
89    
90    
91     public class RLJBar // ProdConsTest
92     {
93    
94     public static final int ITERS = 10;
95     public static boolean OneKey = false ; // alloc once or once per iteration
96    
97     public static boolean UseBar = false ;
98     public static int nThreads = 100 ;
99     public static int nUp = 0 ;
100     public static int nDead = 0 ;
101     public static ReentrantLock bar = new ReentrantLock() ;
102     public static Condition barCondition = bar.newCondition() ;
103     public static long epoch ;
104     public static ReentrantLock DeathRow = new ReentrantLock () ;
105     public static ReentrantLock End = new ReentrantLock () ;
106     public static int quiesce = 0 ;
107     public static Condition EndCondition = End.newCondition();
108    
109     public static void main (String[] args) {
110     int argix = 0 ;
111     if (argix < args.length && args[argix].equals("-o")) {
112     ++argix ;
113     OneKey = true ;
114     System.out.println ("OneKey") ;
115     }
116     if (argix < args.length && args[argix].equals ("-b")) {
117     ++argix ;
118     UseBar = true ;
119     System.out.println ("UseBar") ;
120     }
121     if (argix < args.length && args[argix].equals ("-q")) {
122     ++argix ;
123     if (argix < args.length) {
124     quiesce = Integer.parseInt (args[argix++]) ;
125     System.out.println ("Quiesce " + quiesce + " msecs") ;
126     }
127     }
128     for (int k = 0; k < ITERS; ++k)
129     oneRun();
130     }
131    
132     public static void oneRun() {
133     DeathRow = new ReentrantLock () ;
134     End = new ReentrantLock () ;
135     EndCondition = End.newCondition();
136    
137     nDead = nUp = 0 ;
138     long cyBase = System.currentTimeMillis () ;
139     DeathRow.lock();
140     try {
141     for (int i = 1; i <= nThreads ; i++) {
142     new Producer("Producer" + i).start();
143     }
144     try {
145     End.lock();
146     try {
147     while (nDead != nThreads)
148     EndCondition.await() ;
149     }
150     finally {
151     End.unlock();
152     }
153     } catch (Exception ex) {
154     System.out.println ("Exception in End: " + ex) ;
155     }
156     }
157     finally {
158     DeathRow.unlock();
159     }
160     System.out.println ("Outer time: " + (System.currentTimeMillis()-cyBase)) ;
161    
162     // Let workers quiesce/exit.
163     try { Thread.sleep (1000) ; } catch (Exception ex) {} ;
164     }
165     }
166    
167    
168