1 cananian 1.1.2.3 // CollectSyncStats.java, created Thu Jul 13  2:18:28 2000 by jwhaley
  2 jwhaley  1.1.2.1 // Copyright (C) 2000 John Whaley <jwhaley@alum.mit.edu>
  3 jwhaley  1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 jwhaley  1.1.2.1 
  5 jwhaley  1.1.2.1 package harpoon.Analysis.Quads;
  6 jwhaley  1.1.2.1 
  7 jwhaley  1.1.2.1 import java.util.Set;
  8 jwhaley  1.1.2.1 import java.util.AbstractSet;
  9 jwhaley  1.1.2.1 import java.util.List;
 10 jwhaley  1.1.2.1 import java.util.ArrayList;
 11 jwhaley  1.1.2.1 import java.util.LinkedList;
 12 jwhaley  1.1.2.1 import java.util.Iterator;
 13 jwhaley  1.1.2.1 import java.util.Map;
 14 jwhaley  1.1.2.1 import java.util.AbstractMap;
 15 jwhaley  1.1.2.1 import java.util.HashMap;
 16 jwhaley  1.1.2.1 import java.util.NoSuchElementException;
 17 jwhaley  1.1.2.1 
 18 jwhaley  1.1.2.1 import java.lang.ref.WeakReference;
 19 jwhaley  1.1.2.1 import java.lang.ref.ReferenceQueue;
 20 jwhaley  1.1.2.1 
 21 jwhaley  1.1.2.2 import java.io.FileDescriptor;
 22 jwhaley  1.1.2.2 import java.util.Hashtable;
 23 jwhaley  1.1.2.2 import java.net.InetAddress;
 24 jwhaley  1.1.2.2 import java.lang.reflect.Member;
 25 jwhaley  1.1.2.2 
 26 jwhaley  1.1.2.1 /**
 27 jwhaley  1.1.2.1  * <code>CollectSyncStats</code> is used at run time to collect information
 28 jwhaley  1.1.2.1  * about synchronization operations.  (<code>InstrumentSyncOps</code>
 29 jwhaley  1.1.2.1  * instruments code to add calls to methods in this class.)
 30 jwhaley  1.1.2.1  * 
 31 jwhaley  1.1.2.1  * @author  John Whaley <jwhaley@alum.mit.edu>
 32 cananian 1.3      * @version $Id: CollectSyncStats.java,v 1.3 2004/02/08 03:20:10 cananian Exp $
 33 jwhaley  1.1.2.1  */
 34 jwhaley  1.1.2.1 
 35 jwhaley  1.1.2.1 public abstract class CollectSyncStats {
 36 jwhaley  1.1.2.1 
 37 jwhaley  1.1.2.1     static boolean hasdumped = false;
 38 jwhaley  1.1.2.1     
 39 jwhaley  1.1.2.1     public static void main(String[] args) throws Throwable {
 40 jwhaley  1.1.2.1         String classname = args[0];
 41 jwhaley  1.1.2.1         Class c = Class.forName(classname);
 42 jwhaley  1.1.2.1         if (c == null) {
 43 jwhaley  1.1.2.1             System.err.println("Error loading class "+classname);
 44 jwhaley  1.1.2.1             return;
 45 jwhaley  1.1.2.1         }
 46 jwhaley  1.1.2.1         //Class[] argdesc = new Class[] { java.lang.reflect.Array.newInstance(String.class, 0).getClass() };
 47 jwhaley  1.1.2.1         Class[] argdesc = new Class[] { Class.forName("[Ljava.lang.String;") };
 48 jwhaley  1.1.2.1         java.lang.reflect.Method m = c.getMethod("main", argdesc);
 49 jwhaley  1.1.2.1         if (m == null) {
 50 jwhaley  1.1.2.1             System.err.println("No main method in class "+c);
 51 jwhaley  1.1.2.1             return;
 52 jwhaley  1.1.2.1         }
 53 jwhaley  1.1.2.1         String[] arg = new String[args.length-1];
 54 jwhaley  1.1.2.1         System.arraycopy(args, 1, arg, 0, arg.length);
 55 jwhaley  1.1.2.1         try {
 56 jwhaley  1.1.2.1             System.runFinalizersOnExit(true);
 57 jwhaley  1.1.2.1             CollectSyncStats.init(2, 2);
 58 jwhaley  1.1.2.1             System.setSecurityManager(new SecurityManager() {
 59 jwhaley  1.1.2.1                 public void checkExit(int status) { throw new SecurityException(); }
 60 jwhaley  1.1.2.2 
 61 jwhaley  1.1.2.2                 public void checkCreateClassLoader() { } 
 62 jwhaley  1.1.2.2                 public void checkAccess(Thread g) { }
 63 jwhaley  1.1.2.2                 public void checkAccess(ThreadGroup g) { }
 64 jwhaley  1.1.2.2                 public void checkExec(String cmd) { }
 65 jwhaley  1.1.2.2                 public void checkLink(String lib) { }
 66 jwhaley  1.1.2.2                 public void checkRead(FileDescriptor fd) { }
 67 jwhaley  1.1.2.2                 public void checkRead(String file) { }
 68 jwhaley  1.1.2.2                 public void checkRead(String file, Object context) { }
 69 jwhaley  1.1.2.2                 public void checkWrite(FileDescriptor fd) { }
 70 jwhaley  1.1.2.2                 public void checkWrite(String file) { }
 71 jwhaley  1.1.2.2                 public void checkDelete(String file) { }
 72 jwhaley  1.1.2.2                 public void checkConnect(String host, int port) { }
 73 jwhaley  1.1.2.2                 public void checkConnect(String host, int port, Object context) { }
 74 jwhaley  1.1.2.2                 public void checkListen(int port) { }
 75 jwhaley  1.1.2.2                 public void checkAccept(String host, int port) { }
 76 jwhaley  1.1.2.2                 public void checkMulticast(InetAddress maddr) { }
 77 jwhaley  1.1.2.2                 public void checkMulticast(InetAddress maddr, byte ttl) { }
 78 jwhaley  1.1.2.2                 public void checkPropertiesAccess() { }
 79 jwhaley  1.1.2.2                 public void checkPropertyAccess(String key) { }
 80 jwhaley  1.1.2.2                 public void checkPropertyAccess(String key, String def) { }
 81 jwhaley  1.1.2.2                 public boolean checkTopLevelWindow(Object window) { return true; }
 82 jwhaley  1.1.2.2                 public void checkPrintJobAccess() { }
 83 jwhaley  1.1.2.2                 public void checkSystemClipboardAccess() { }
 84 jwhaley  1.1.2.2                 public void checkAwtEventQueueAccess() { }
 85 jwhaley  1.1.2.2                 public void checkPackageAccess(String pkg) { }
 86 jwhaley  1.1.2.2                 public void checkPackageDefinition(String pkg) { }
 87 jwhaley  1.1.2.2                 public void checkSetFactory() { }
 88 jwhaley  1.1.2.2                 public void checkMemberAccess(Class clazz, int which) { }
 89 jwhaley  1.1.2.2                 public void checkSecurityAccess(String provider) { }
 90 jwhaley  1.1.2.2                 });
 91 jwhaley  1.1.2.1             enabled = true;
 92 jwhaley  1.1.2.1             m.invoke(null, new Object[] { arg });
 93 jwhaley  1.1.2.1             enabled = false;
 94 jwhaley  1.1.2.1         } catch (java.lang.reflect.InvocationTargetException x) {
 95 jwhaley  1.1.2.1             throw x.getTargetException();
 96 jwhaley  1.1.2.1         } finally {
 97 jwhaley  1.1.2.1             dump();
 98 jwhaley  1.1.2.1         }
 99 jwhaley  1.1.2.1     }
100 jwhaley  1.1.2.1     
101 jwhaley  1.1.2.1     /** The <code>WeakIdentityHashMap</code> is used to keep track of objects
102 jwhaley  1.1.2.1      * that have been synchronized on. It uses weak keys and the
103 jwhaley  1.1.2.1      * identityHashCode function.
104 jwhaley  1.1.2.1      */
105 jwhaley  1.1.2.1     static class WeakIdentityHashMap extends AbstractMap implements Map {
106 jwhaley  1.1.2.1 
107 jwhaley  1.1.2.1         static private class WeakKey extends WeakReference {
108 jwhaley  1.1.2.1             private int hash;
109 jwhaley  1.1.2.1             private WeakKey(Object k) {
110 jwhaley  1.1.2.1                 super(k);
111 jwhaley  1.1.2.1                 hash = System.identityHashCode(k);
112 jwhaley  1.1.2.1             }
113 jwhaley  1.1.2.1             private static WeakKey create(Object k) {
114 jwhaley  1.1.2.1                 if (k == null) return null;
115 jwhaley  1.1.2.1                 else return new WeakKey(k);
116 jwhaley  1.1.2.1             }
117 jwhaley  1.1.2.1             private WeakKey(Object k, ReferenceQueue q) {
118 jwhaley  1.1.2.1                 super(k, q);
119 jwhaley  1.1.2.1                 hash = System.identityHashCode(k);
120 jwhaley  1.1.2.1             }
121 jwhaley  1.1.2.1             private static WeakKey create(Object k, ReferenceQueue q) {
122 jwhaley  1.1.2.1                 if (k == null) return null;
123 jwhaley  1.1.2.1                 else return new WeakKey(k, q);
124 jwhaley  1.1.2.1             }
125 jwhaley  1.1.2.1             public boolean equals(Object o) {
126 jwhaley  1.1.2.1                 if (this == o) return true;
127 jwhaley  1.1.2.1                 if (!(o instanceof WeakKey)) return false;
128 jwhaley  1.1.2.1                 Object t = this.get();
129 jwhaley  1.1.2.1                 Object u = ((WeakKey)o).get();
130 jwhaley  1.1.2.1                 if ((t == null) || (u == null)) return false;
131 jwhaley  1.1.2.1                 return (t == u);
132 jwhaley  1.1.2.1             }
133 jwhaley  1.1.2.1             public int hashCode() {
134 jwhaley  1.1.2.1                 return hash;
135 jwhaley  1.1.2.1             }
136 jwhaley  1.1.2.1         }
137 jwhaley  1.1.2.1         private Map hash;
138 jwhaley  1.1.2.1         private ReferenceQueue queue = new ReferenceQueue();
139 jwhaley  1.1.2.1         private void processQueue() {
140 jwhaley  1.1.2.1             WeakKey wk;
141 jwhaley  1.1.2.1             while ((wk = (WeakKey)queue.poll()) != null) {
142 jwhaley  1.1.2.1                 hash.remove(wk);
143 jwhaley  1.1.2.1             }
144 jwhaley  1.1.2.1         }
145 jwhaley  1.1.2.1         public WeakIdentityHashMap(int initialCapacity, float loadFactor) {
146 jwhaley  1.1.2.1             hash = new HashMap(initialCapacity, loadFactor);
147 jwhaley  1.1.2.1         }
148 jwhaley  1.1.2.1         public WeakIdentityHashMap(int initialCapacity) {
149 jwhaley  1.1.2.1             hash = new HashMap(initialCapacity);
150 jwhaley  1.1.2.1         }
151 jwhaley  1.1.2.1         public WeakIdentityHashMap() {
152 jwhaley  1.1.2.1             hash = new HashMap();
153 jwhaley  1.1.2.1         }
154 jwhaley  1.1.2.1         public int size() {
155 jwhaley  1.1.2.1             return entrySet().size();
156 jwhaley  1.1.2.1         }
157 jwhaley  1.1.2.1         public boolean isEmpty() {
158 jwhaley  1.1.2.1             return entrySet().isEmpty();
159 jwhaley  1.1.2.1         }
160 jwhaley  1.1.2.1         public boolean containsKey(Object key) {
161 jwhaley  1.1.2.1             return hash.containsKey(WeakKey.create(key));
162 jwhaley  1.1.2.1         }
163 jwhaley  1.1.2.1         public Object get(Object key) {
164 jwhaley  1.1.2.1             return hash.get(WeakKey.create(key));
165 jwhaley  1.1.2.1         }
166 jwhaley  1.1.2.1         public Object put(Object key, Object value) {
167 jwhaley  1.1.2.1             processQueue();
168 jwhaley  1.1.2.1             return hash.put(WeakKey.create(key, queue), value);
169 jwhaley  1.1.2.1         }
170 jwhaley  1.1.2.1         public Object remove(Object key) {
171 jwhaley  1.1.2.1             processQueue();
172 jwhaley  1.1.2.1             return hash.remove(WeakKey.create(key));
173 jwhaley  1.1.2.1         }
174 jwhaley  1.1.2.1         public void clear() {
175 jwhaley  1.1.2.1             processQueue();
176 jwhaley  1.1.2.1             hash.clear();
177 jwhaley  1.1.2.1         }
178 jwhaley  1.1.2.1         static private class Entry implements Map.Entry {
179 jwhaley  1.1.2.1             private Map.Entry ent;
180 jwhaley  1.1.2.1             private Object key;
181 jwhaley  1.1.2.1             Entry(Map.Entry ent, Object key) {
182 jwhaley  1.1.2.1                 this.ent = ent;
183 jwhaley  1.1.2.1                 this.key = key;
184 jwhaley  1.1.2.1             }
185 jwhaley  1.1.2.1             public Object getKey() {
186 jwhaley  1.1.2.1                 return key;
187 jwhaley  1.1.2.1             }
188 jwhaley  1.1.2.1             public Object getValue() {
189 jwhaley  1.1.2.1                 return ent.getValue();
190 jwhaley  1.1.2.1             }
191 jwhaley  1.1.2.1             public Object setValue(Object value) {
192 jwhaley  1.1.2.1                 return ent.setValue(value);
193 jwhaley  1.1.2.1             }
194 jwhaley  1.1.2.1             private static boolean valEquals(Object o1, Object o2) {
195 jwhaley  1.1.2.1                 return o1 == o2;
196 jwhaley  1.1.2.1             }
197 jwhaley  1.1.2.1             public boolean equals(Object o) {
198 jwhaley  1.1.2.1                 if (! (o instanceof Map.Entry)) return false;
199 jwhaley  1.1.2.1                 Map.Entry e = (Map.Entry)o;
200 jwhaley  1.1.2.1                 return (valEquals(key, e.getKey())
201 jwhaley  1.1.2.1                     && valEquals(getValue(), e.getValue()));
202 jwhaley  1.1.2.1             }
203 jwhaley  1.1.2.1             public int hashCode() {
204 jwhaley  1.1.2.1                 Object v;
205 jwhaley  1.1.2.1                 return (((key == null) ? 0 : System.identityHashCode(key))
206 jwhaley  1.1.2.1                     ^ (((v = getValue()) == null) ? 0 : v.hashCode()));
207 jwhaley  1.1.2.1             }
208 jwhaley  1.1.2.1         }
209 jwhaley  1.1.2.1         private class EntrySet extends AbstractSet {
210 jwhaley  1.1.2.1             Set hashEntrySet = hash.entrySet();
211 jwhaley  1.1.2.1             public Iterator iterator() {
212 jwhaley  1.1.2.1                 return new Iterator() {
213 jwhaley  1.1.2.1                     Iterator hashIterator = hashEntrySet.iterator();
214 jwhaley  1.1.2.1                     Entry next = null;
215 jwhaley  1.1.2.1                     public boolean hasNext() {
216 jwhaley  1.1.2.1                         while (hashIterator.hasNext()) {
217 jwhaley  1.1.2.1                             Map.Entry ent = (Map.Entry)hashIterator.next();
218 jwhaley  1.1.2.1                             WeakKey wk = (WeakKey)ent.getKey();
219 jwhaley  1.1.2.1                             Object k = null;
220 jwhaley  1.1.2.1                             if ((wk != null) && ((k = wk.get()) == null)) {
221 jwhaley  1.1.2.1                                 /* Weak key has been cleared by GC */
222 jwhaley  1.1.2.1                                 continue;
223 jwhaley  1.1.2.1                             }
224 jwhaley  1.1.2.1                             next = new Entry(ent, k);
225 jwhaley  1.1.2.1                             return true;
226 jwhaley  1.1.2.1                         }
227 jwhaley  1.1.2.1                         return false;
228 jwhaley  1.1.2.1                     }
229 jwhaley  1.1.2.1                     public Object next() {
230 jwhaley  1.1.2.1                         if ((next == null) && !hasNext())
231 jwhaley  1.1.2.1                             throw new NoSuchElementException();
232 jwhaley  1.1.2.1                         Entry e = next;
233 jwhaley  1.1.2.1                         next = null;
234 jwhaley  1.1.2.1                         return e;
235 jwhaley  1.1.2.1                     }
236 jwhaley  1.1.2.1                     public void remove() {
237 jwhaley  1.1.2.1                         hashIterator.remove();
238 jwhaley  1.1.2.1                     }
239 jwhaley  1.1.2.1                 };
240 jwhaley  1.1.2.1             }
241 jwhaley  1.1.2.1             public boolean isEmpty() {
242 jwhaley  1.1.2.1                 return !(iterator().hasNext());
243 jwhaley  1.1.2.1             }
244 jwhaley  1.1.2.1             public int size() {
245 jwhaley  1.1.2.1                 int j = 0;
246 jwhaley  1.1.2.1                 for (Iterator i = iterator(); i.hasNext(); i.next()) j++;
247 jwhaley  1.1.2.1                 return j;
248 jwhaley  1.1.2.1             }
249 jwhaley  1.1.2.1             public boolean remove(Object o) {
250 jwhaley  1.1.2.1                 processQueue();
251 jwhaley  1.1.2.1                 if (!(o instanceof Map.Entry)) return false;
252 jwhaley  1.1.2.1                 Map.Entry e = (Map.Entry)o;
253 jwhaley  1.1.2.1                 Object ev = e.getValue();
254 jwhaley  1.1.2.1                 WeakKey wk = WeakKey.create(e.getKey());
255 jwhaley  1.1.2.1                 Object hv = hash.get(wk);
256 jwhaley  1.1.2.1                 if ((hv == null)
257 jwhaley  1.1.2.1                     ? ((ev == null) && hash.containsKey(wk)) : (hv == ev)) {
258 jwhaley  1.1.2.1                     hash.remove(wk);
259 jwhaley  1.1.2.1                     return true;
260 jwhaley  1.1.2.1                 }
261 jwhaley  1.1.2.1                 return false;
262 jwhaley  1.1.2.1             }
263 jwhaley  1.1.2.1             public int hashCode() {
264 jwhaley  1.1.2.1                 int h = 0;
265 cananian 1.3                     for (Object entO : hashEntrySet) {
266 cananian 1.3                         Map.Entry ent = (Map.Entry) entO;
267 jwhaley  1.1.2.1                     WeakKey wk = (WeakKey)ent.getKey();
268 jwhaley  1.1.2.1                     Object v;
269 jwhaley  1.1.2.1                     if (wk == null) continue;
270 jwhaley  1.1.2.1                     h += (wk.hashCode()
271 jwhaley  1.1.2.1                           ^ (((v = ent.getValue()) == null) ? 0 : System.identityHashCode(v)));
272 jwhaley  1.1.2.1                 }
273 jwhaley  1.1.2.1                 return h;
274 jwhaley  1.1.2.1             }
275 jwhaley  1.1.2.1         }
276 jwhaley  1.1.2.1         private Set entrySet = null;
277 jwhaley  1.1.2.1         public Set entrySet() {
278 jwhaley  1.1.2.1             if (entrySet == null) entrySet = new EntrySet();
279 jwhaley  1.1.2.1             return entrySet;
280 jwhaley  1.1.2.1         }
281 jwhaley  1.1.2.1     }
282 jwhaley  1.1.2.1     
283 jwhaley  1.1.2.1     /** Dummy object that we use to insure atomic updates to the counters.
284 jwhaley  1.1.2.1      */
285 jwhaley  1.1.2.1     static Object locking = new Object();
286 jwhaley  1.1.2.1 
287 jwhaley  1.1.2.1 
288 jwhaley  1.1.2.1     /** All created objects are registered here, associated with their creation site.
289 jwhaley  1.1.2.1      */
290 jwhaley  1.1.2.1     static WeakIdentityHashMap objmap;
291 jwhaley  1.1.2.1     
292 jwhaley  1.1.2.1     /** MUST call this function before enabling stats collection.
293 jwhaley  1.1.2.1      */
294 jwhaley  1.1.2.1     public static void init(int nNewTypes, int nLockTypes) {
295 jwhaley  1.1.2.1         objmap = new WeakIdentityHashMap();
296 jwhaley  1.1.2.1         monitorEnterMap = new HashMap();
297 jwhaley  1.1.2.1         monitorExitMap = new HashMap();
298 jwhaley  1.1.2.1         newcount = new int[nNewTypes];
299 jwhaley  1.1.2.1         totalnewcount = 0;
300 jwhaley  1.1.2.1         lockcount = new int[nLockTypes];
301 jwhaley  1.1.2.1     }
302 jwhaley  1.1.2.1     
303 jwhaley  1.1.2.1     /** Whether stats collection is currently enabled.
304 jwhaley  1.1.2.1      */
305 jwhaley  1.1.2.1     public static volatile boolean enabled;
306 jwhaley  1.1.2.1     
307 jwhaley  1.1.2.1     public static int[] newcount;
308 jwhaley  1.1.2.1     public static int totalnewcount;
309 jwhaley  1.1.2.1     public static void onNew(int index, Object obj, int b) {
310 jwhaley  1.1.2.1         synchronized (locking) {
311 jwhaley  1.1.2.1             if (!enabled) return;
312 jwhaley  1.1.2.1             enabled = false;
313 jwhaley  1.1.2.1             //System.err.println("New "+index+" object "+System.identityHashCode(obj));
314 jwhaley  1.1.2.1             // Register the created object with the given index.
315 jwhaley  1.1.2.1             Integer i = new Integer(index);
316 jwhaley  1.1.2.1             objmap.put(obj, i);
317 jwhaley  1.1.2.1             for (int j=0; j<newcount.length; ++j) {
318 jwhaley  1.1.2.1                 if ((b&1) != 0) ++newcount[j];
319 jwhaley  1.1.2.1                 b >>= 1;
320 jwhaley  1.1.2.1             }
321 jwhaley  1.1.2.1             ++totalnewcount;
322 jwhaley  1.1.2.1             enabled = true;
323 jwhaley  1.1.2.1         }
324 jwhaley  1.1.2.1     }
325 jwhaley  1.1.2.1 
326 jwhaley  1.1.2.1     public static int[] lockcount;
327 jwhaley  1.1.2.1     public static int totallockcount;
328 jwhaley  1.1.2.1     private static void onMonitorHelper(HashMap map, int index, Object obj, int b) {
329 jwhaley  1.1.2.1         synchronized (locking) {
330 jwhaley  1.1.2.1             if (!enabled) return;
331 jwhaley  1.1.2.1             enabled = false;
332 jwhaley  1.1.2.1             //System.err.println("Monitor"+((map == monitorEnterMap)?"enter":"exit")+" id "+index+" on object "+System.identityHashCode(obj));
333 jwhaley  1.1.2.1             // Look up the object id.
334 jwhaley  1.1.2.1             Integer i = (Integer)objmap.get(obj);
335 jwhaley  1.1.2.1             int in;
336 jwhaley  1.1.2.1             if (i == null) in = -1;
337 jwhaley  1.1.2.1             else in = i.intValue();
338 jwhaley  1.1.2.1 
339 jwhaley  1.1.2.1             // Bump counter
340 jwhaley  1.1.2.1             Integer j = new Integer(index);
341 jwhaley  1.1.2.1             LinkedList ll = (LinkedList)map.get(j);
342 jwhaley  1.1.2.1             if (ll == null) {
343 jwhaley  1.1.2.1                 map.put(j, ll = new LinkedList());
344 jwhaley  1.1.2.1             }
345 jwhaley  1.1.2.1             Counter c = new Counter(in);
346 jwhaley  1.1.2.1             int ind = ll.indexOf(c);
347 jwhaley  1.1.2.1             if (ind == -1) {
348 jwhaley  1.1.2.1                 ll.addFirst(c);
349 jwhaley  1.1.2.1             } else {
350 jwhaley  1.1.2.1                 c = (Counter)ll.get(ind);
351 jwhaley  1.1.2.1             }
352 jwhaley  1.1.2.1             c.inc();
353 jwhaley  1.1.2.1             for (int k=0; k<lockcount.length; ++k) {
354 jwhaley  1.1.2.1                 if ((b&1) != 0) ++lockcount[k];
355 jwhaley  1.1.2.1                 b >>= 1;
356 jwhaley  1.1.2.1             }
357 jwhaley  1.1.2.1             ++totallockcount;
358 jwhaley  1.1.2.1             enabled = true;
359 jwhaley  1.1.2.1         }
360 jwhaley  1.1.2.1     }
361 jwhaley  1.1.2.1 
362 jwhaley  1.1.2.1     static HashMap monitorEnterMap;
363 jwhaley  1.1.2.1     static HashMap monitorExitMap;
364 jwhaley  1.1.2.1     
365 jwhaley  1.1.2.1     public static void onMonitorEnter(int index, Object obj, int b) {
366 jwhaley  1.1.2.1         onMonitorHelper(monitorEnterMap, index, obj, b);
367 jwhaley  1.1.2.1     }
368 jwhaley  1.1.2.1 
369 jwhaley  1.1.2.1     public static void onMonitorExit(int index, Object obj, int b) {
370 jwhaley  1.1.2.1         onMonitorHelper(monitorExitMap, index, obj, b);
371 jwhaley  1.1.2.1     }
372 jwhaley  1.1.2.1 
373 jwhaley  1.1.2.1     private static int dumpMap(String name, HashMap map) {
374 jwhaley  1.1.2.1         int badsyncs = 0;
375 cananian 1.3             for (Object jO : map.keySet()) {
376 cananian 1.3                 Integer j = (Integer) jO;
377 jwhaley  1.1.2.1             System.out.println(name+" id "+j+":");
378 jwhaley  1.1.2.1             LinkedList ll = (LinkedList)map.get(j);
379 jwhaley  1.1.2.1             java.util.Collections.sort(ll);
380 jwhaley  1.1.2.1             Iterator k = ll.iterator();
381 jwhaley  1.1.2.1             int total = 0;
382 jwhaley  1.1.2.1             while (k.hasNext()) {
383 jwhaley  1.1.2.1                 Counter l = (Counter)k.next();
384 jwhaley  1.1.2.1                 System.out.println("\tObject id "+l.getId()+"\tnum: "+l.getCount());
385 jwhaley  1.1.2.1                 total += l.getCount();
386 jwhaley  1.1.2.1                 if (l.getId() == -1) badsyncs += l.getCount();
387 jwhaley  1.1.2.1             }
388 jwhaley  1.1.2.1             System.out.println("\t\t\ttot: "+total);
389 jwhaley  1.1.2.1         }
390 jwhaley  1.1.2.1         return badsyncs;
391 jwhaley  1.1.2.1     }
392 jwhaley  1.1.2.1     
393 jwhaley  1.1.2.1     public static void dump() {
394 jwhaley  1.1.2.1         int badsyncs;
395 jwhaley  1.1.2.1         enabled = false;
396 jwhaley  1.1.2.1         System.out.println("MONITORENTER stats:");
397 jwhaley  1.1.2.1         badsyncs = dumpMap("MONITORENTER", monitorEnterMap);
398 jwhaley  1.1.2.1         System.out.println("MONITOREXIT stats:");
399 jwhaley  1.1.2.1         badsyncs += dumpMap("MONITOREXIT", monitorExitMap);
400 jwhaley  1.1.2.1         System.out.println("#objs (total): "+totalnewcount);
401 jwhaley  1.1.2.1         for (int j=0; j<newcount.length; ++j) {
402 jwhaley  1.1.2.1             System.out.println("#objs"+j+": "+newcount[j]);
403 jwhaley  1.1.2.1         }
404 jwhaley  1.1.2.1         System.out.println("#locks (total): "+totallockcount);
405 jwhaley  1.1.2.1         for (int j=0; j<lockcount.length; ++j) {
406 jwhaley  1.1.2.1             System.out.println("#locks"+j+": "+lockcount[j]);
407 jwhaley  1.1.2.1         }
408 jwhaley  1.1.2.1         System.out.println("#locks on -1: "+badsyncs);
409 jwhaley  1.1.2.1         hasdumped = true;
410 jwhaley  1.1.2.1         enabled = true;
411 jwhaley  1.1.2.1     }
412 jwhaley  1.1.2.1     
413 jwhaley  1.1.2.1     private static class Counter implements Comparable {
414 jwhaley  1.1.2.1         final int id;
415 jwhaley  1.1.2.1         int count;
416 jwhaley  1.1.2.1         Counter(int id) { this.id = id; }
417 jwhaley  1.1.2.1         public boolean equals(Object o) { if (o instanceof Counter) return equals((Counter)o); return false; }
418 jwhaley  1.1.2.1         public boolean equals(Counter that) { return this.id == that.id; }
419 jwhaley  1.1.2.1         public int hashCode() { return id; }
420 jwhaley  1.1.2.1         public int inc() { return ++count; }
421 jwhaley  1.1.2.1         public int getId() { return id; }
422 jwhaley  1.1.2.1         public int getCount() { return count; }
423 jwhaley  1.1.2.1         public int compareTo(Counter that) {
424 jwhaley  1.1.2.1             if (this.id == that.id) return 0;
425 jwhaley  1.1.2.1             else if (this.id < that.id) return -1;
426 jwhaley  1.1.2.1             else return 1;
427 jwhaley  1.1.2.1         }
428 jwhaley  1.1.2.1         public int compareTo(Object o) { return compareTo((Counter)o); }
429 jwhaley  1.1.2.1     }
430 jwhaley  1.1.2.1     
431 cananian 1.2     }