1 cananian 1.1.2.1 // CounterSupport.java, created Tue Nov  7 14:12:49 2000 by root
  2 cananian 1.1.2.9 // Copyright (C) 2000 Brian Demsky <bdemsky@mit.edu>
  3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.2.1 package harpoon.Runtime;
  5 cananian 1.1.2.1 import java.io.ObjectOutputStream;
  6 cananian 1.1.2.1 import java.io.FileOutputStream;
  7 cananian 1.1.2.1 import java.io.PrintStream;
  8 cananian 1.1.2.1 /**
  9 cananian 1.1.2.1  * <code>CounterSupport</code> provides support for simple instrumentation.
 10 cananian 1.1.2.1  * using counters identified by integers.
 11 cananian 1.1.2.1  * 
 12 cananian 1.1.2.9  * @author  Brian Demsky <bdemsky@mit.edu>
 13 salcianu 1.6      * @version $Id: CounterSupport.java,v 1.6 2003/03/18 03:54:34 salcianu Exp $ */
 14 cananian 1.1.2.1 public class CounterSupport {
 15 salcianu 1.4         static int sizesync;
 16 bdemsky  1.1.2.2     static int numbins, bincap;
 17 salcianu 1.4         static long[] obj_count;
 18 salcianu 1.4         static int obj_count_length;
 19 salcianu 1.4         static long[] mem_amount;
 20 salcianu 1.4         static int mem_amount_length;
 21 bdemsky  1.1.2.2     static long[] arraysync;
 22 bdemsky  1.1.2.3     static Object[][] boundedkey;
 23 bdemsky  1.1.2.2     static int[][] boundedvalue;
 24 cananian 1.1.2.1     static Object lock=new Object();
 25 bdemsky  1.1.2.6     static boolean counton,setup;
 26 bdemsky  1.1.2.2     static int error;
 27 bdemsky  1.1.2.2     static int overflow;
 28 bdemsky  1.1.2.7     static int[][] callchain;
 29 bdemsky  1.1.2.7     static int csize,tsize;
 30 bdemsky  1.1.2.7     static int[] depth;
 31 bdemsky  1.1.2.4     static int[][] alloccall;
 32 bdemsky  1.1.2.4     static int size1,size2;
 33 bdemsky  1.1.2.7     static Thread[] threadarray;
 34 bdemsky  1.1.2.8     static int depthoverflow, threadoverflow;
 35 bdemsky  1.1.2.4 
 36 cananian 1.1.2.1     static {
 37 bdemsky  1.1.2.2         //redefine these
 38 bdemsky  1.1.2.2         numbins=211;
 39 bdemsky  1.1.2.3         bincap=30;
 40 bdemsky  1.1.2.4         csize=100;
 41 bdemsky  1.1.2.7         tsize=100;
 42 bdemsky  1.1.2.3         boundedkey=new Object[numbins][bincap];
 43 bdemsky  1.1.2.2         boundedvalue=new int[numbins][bincap];
 44 bdemsky  1.1.2.7         callchain=new int[csize][tsize];
 45 bdemsky  1.1.2.7         depth=new int[tsize];
 46 bdemsky  1.1.2.7         threadarray=new Thread[tsize];
 47 bdemsky  1.1.2.2 
 48 bdemsky  1.1.2.8         depthoverflow=0;
 49 bdemsky  1.1.2.8         threadoverflow=0;
 50 bdemsky  1.1.2.2         overflow=0;
 51 bdemsky  1.1.2.2         error=0;
 52 salcianu 1.4     
 53 salcianu 1.4             obj_count_length = 10;
 54 salcianu 1.4             obj_count = new long[obj_count_length];
 55 salcianu 1.4             mem_amount_length = obj_count_length;
 56 salcianu 1.4             mem_amount = new long[mem_amount_length];
 57 salcianu 1.4     
 58 bdemsky  1.1.2.2         sizesync=10;
 59 bdemsky  1.1.2.2         arraysync=new long[sizesync];
 60 bdemsky  1.1.2.4         size1=10;
 61 bdemsky  1.1.2.4         size2=10;
 62 bdemsky  1.1.2.4         alloccall=new int[size1][size2];
 63 cananian 1.1.2.1         counton=true;
 64 bdemsky  1.1.2.6         setup=true;
 65 cananian 1.1.2.1     }
 66 cananian 1.1.2.1     
 67 salcianu 1.4     
 68 salcianu 1.4         static void count(int allocID) {
 69 salcianu 1.4             if (setup && counton)
 70 salcianu 1.4                 synchronized(lock) {
 71 salcianu 1.4                     count_aux(allocID);
 72 salcianu 1.4                 }
 73 salcianu 1.4         }
 74 salcianu 1.4     
 75 salcianu 1.4     
 76 salcianu 1.4         static void count2(int allocID, int length) {
 77 salcianu 1.4             if (setup && counton)
 78 salcianu 1.4                 synchronized(lock) {
 79 salcianu 1.4                     count_aux(allocID);
 80 salcianu 1.4                     update_mem_amount(allocID, length);
 81 salcianu 1.4                 }
 82 salcianu 1.4         }
 83 salcianu 1.4     
 84 salcianu 1.4         
 85 salcianu 1.4         static void update_mem_amount(int allocID, int length) {
 86 salcianu 1.4             // allocID == -1 stands for allocation sites that haven't
 87 salcianu 1.4             // received an unique integer ID 
 88 salcianu 1.4             if (allocID == -1) return;
 89 salcianu 1.6     
 90 salcianu 1.6             if((length % 4) != 0)
 91 salcianu 1.6                 length += 4 - (length % 4);
 92 salcianu 1.6     
 93 salcianu 1.4             if (allocID >= mem_amount_length) {
 94 salcianu 1.4                 long[] new_mem_amount = new long[allocID * 2];
 95 salcianu 1.4                 System.arraycopy(mem_amount, 0,
 96 salcianu 1.4                                  new_mem_amount, 0,
 97 salcianu 1.4                                  mem_amount_length);
 98 salcianu 1.4                 mem_amount = new_mem_amount;
 99 salcianu 1.4                 mem_amount_length = allocID * 2;
100 salcianu 1.4             }
101 salcianu 1.5             mem_amount[allocID] += length;
102 salcianu 1.4         }
103 salcianu 1.4         
104 salcianu 1.4     
105 salcianu 1.4         static void count_aux(int value) {
106 salcianu 1.4             if (counton) {
107 salcianu 1.4                 boolean resize=false;
108 salcianu 1.4                 int nsize1=size1,nsize2=size2;
109 salcianu 1.4                 if (value>=size1) {
110 salcianu 1.4                     nsize1=value*2;
111 salcianu 1.4                     resize=true;
112 salcianu 1.4                 }
113 salcianu 1.4                 int thisthread=-1;
114 salcianu 1.4                 Thread t=Thread.currentThread();
115 salcianu 1.4                 for(int j=0;j<tsize;j++) {
116 salcianu 1.4                     if (t==threadarray[j]) {
117 salcianu 1.4                         thisthread=j;
118 salcianu 1.4                         break;
119 bdemsky  1.1.2.4                 }
120 salcianu 1.4                 }
121 salcianu 1.4                 if (thisthread!=-1)
122 salcianu 1.4                     if ((depth[thisthread]!=0)&&((depth[thisthread]-1)<csize))
123 salcianu 1.4                         if (callchain[depth[thisthread]-1][thisthread]>=size2) {
124 salcianu 1.4                             nsize2=callchain[depth[thisthread]-1][thisthread]*2;
125 salcianu 1.4                             resize=true;
126 salcianu 1.4                         }
127 salcianu 1.4                 if (resize) {
128 salcianu 1.4                     int[][] newarray=new int[nsize1][nsize2];
129 salcianu 1.4                     for(int i=0;i<size1;i++) {
130 salcianu 1.4                         System.arraycopy(alloccall[i],0,newarray[i],0,size2);
131 bdemsky  1.1.2.7                 }
132 salcianu 1.4                     size1=nsize1;
133 salcianu 1.4                     size2=nsize2;
134 salcianu 1.4                     alloccall=newarray;
135 salcianu 1.4                 }
136 salcianu 1.4                 if (thisthread!=-1) {
137 salcianu 1.4                     if ((depth[thisthread]!=0)&&((depth[thisthread]-1)<csize))
138 salcianu 1.4                         alloccall[value][callchain[depth[thisthread]-1][thisthread]]++;
139 salcianu 1.4                 } else alloccall[value][0]++;
140 salcianu 1.4                 
141 salcianu 1.4                 if (value >= obj_count_length) {
142 salcianu 1.4                     long[] new_obj_count = new long[value*2];
143 salcianu 1.4                     System.arraycopy(obj_count, 0,
144 salcianu 1.4                                      new_obj_count, 0,
145 salcianu 1.4                                      obj_count_length);
146 salcianu 1.4                     obj_count = new_obj_count;
147 salcianu 1.4                     obj_count_length = value*2;
148 cananian 1.1.2.1             }
149 salcianu 1.4                 obj_count[value]++;
150 cananian 1.1.2.1         }
151 cananian 1.1.2.1     }
152 bdemsky  1.1.2.2 
153 bdemsky  1.1.2.2     //Sync code only
154 bdemsky  1.1.2.2     static void countm(Object obj) {
155 bdemsky  1.1.2.6         if (setup)
156 bdemsky  1.1.2.2         synchronized(lock) {
157 bdemsky  1.1.2.2             if (counton) {
158 bdemsky  1.1.2.5                 counton=false;
159 bdemsky  1.1.2.8                 int hash=java.lang.System.identityHashCode(obj);
160 bdemsky  1.1.2.5                 counton=true;
161 bdemsky  1.1.2.2                 int hashmod=hash % numbins;
162 bdemsky  1.1.2.2                 int bin=-1;
163 bdemsky  1.1.2.2                 for(int i=0;i<bincap;i++)
164 bdemsky  1.1.2.3                     if (boundedkey[hashmod][i]==obj) {
165 bdemsky  1.1.2.2                         bin=i;
166 bdemsky  1.1.2.2                         break;
167 bdemsky  1.1.2.2                     }
168 bdemsky  1.1.2.2                 if(bin==-1)
169 bdemsky  1.1.2.2                     error++;
170 bdemsky  1.1.2.2                 else {
171 bdemsky  1.1.2.2                     int value=boundedvalue[hashmod][bin];
172 bdemsky  1.1.2.2                     for(int i=bin;i>0;i--) {
173 bdemsky  1.1.2.2                         boundedkey[hashmod][i]=boundedkey[hashmod][i-1];
174 bdemsky  1.1.2.2                         boundedvalue[hashmod][i]=boundedvalue[hashmod][i-1];
175 bdemsky  1.1.2.2                     }
176 bdemsky  1.1.2.3                     boundedkey[hashmod][0]=obj;
177 bdemsky  1.1.2.2                     boundedvalue[hashmod][0]=value;
178 bdemsky  1.1.2.4                     if (value>=sizesync) {
179 bdemsky  1.1.2.2                         long[] newarray=new long[value*2];
180 bdemsky  1.1.2.2                         System.arraycopy(arraysync,0,newarray,0,sizesync);
181 bdemsky  1.1.2.2                         arraysync=newarray;
182 bdemsky  1.1.2.2                         sizesync=value*2;
183 bdemsky  1.1.2.2                     }
184 bdemsky  1.1.2.2                     arraysync[value]++;
185 bdemsky  1.1.2.2                 }
186 bdemsky  1.1.2.2             }
187 bdemsky  1.1.2.2         }
188 bdemsky  1.1.2.2     }
189 bdemsky  1.1.2.2 
190 bdemsky  1.1.2.2     static void label(Object obj, int value) {
191 bdemsky  1.1.2.6         if (setup)
192 bdemsky  1.1.2.3         synchronized(lock) {
193 bdemsky  1.1.2.5             if (counton) {
194 bdemsky  1.1.2.5                 counton=false;
195 bdemsky  1.1.2.8                 int hash=java.lang.System.identityHashCode(obj);
196 bdemsky  1.1.2.5                 counton=true;
197 bdemsky  1.1.2.5                 int hashmod=hash % numbins;
198 bdemsky  1.1.2.5                 if (boundedkey[hashmod][bincap-1]!=null)
199 bdemsky  1.1.2.5                     overflow++;
200 bdemsky  1.1.2.5                 //          for (int i=bincap-1;i>0;i--) {
201 bdemsky  1.1.2.5                 //      boundedkey[hashmod][i]=boundedkey[hashmod][i-1];
202 bdemsky  1.1.2.5                 //}
203 bdemsky  1.1.2.5                 System.arraycopy(boundedkey[hashmod],0,boundedkey[hashmod],1,bincap-1);
204 bdemsky  1.1.2.5                 System.arraycopy(boundedvalue[hashmod],0,boundedvalue[hashmod],1,bincap-1);
205 bdemsky  1.1.2.5                 boundedkey[hashmod][0]=obj;
206 bdemsky  1.1.2.5                 boundedvalue[hashmod][0]=value;
207 bdemsky  1.1.2.5             }
208 bdemsky  1.1.2.2         }
209 bdemsky  1.1.2.2     }
210 bdemsky  1.1.2.2 
211 bdemsky  1.1.2.4     static void callenter(int callsite) {
212 bdemsky  1.1.2.6         if (setup)
213 bdemsky  1.1.2.6             synchronized(lock) {
214 bdemsky  1.1.2.6                 if (counton) {
215 bdemsky  1.1.2.7                     int firstnull=-1,thisthread=-1;
216 bdemsky  1.1.2.7                     Thread t=Thread.currentThread();
217 bdemsky  1.1.2.7                     for(int j=0;j<tsize;j++) {
218 bdemsky  1.1.2.7                         if (t==threadarray[j]) {
219 bdemsky  1.1.2.7                             thisthread=j;
220 bdemsky  1.1.2.7                             break;
221 bdemsky  1.1.2.7                         }
222 bdemsky  1.1.2.7                         if ((threadarray[j]==null)&&(firstnull==-1))
223 bdemsky  1.1.2.7                             firstnull=j;
224 bdemsky  1.1.2.7                     }
225 bdemsky  1.1.2.7                     if (thisthread==-1) {
226 bdemsky  1.1.2.7                         if (firstnull!=-1) {
227 bdemsky  1.1.2.7                             threadarray[firstnull]=t;
228 bdemsky  1.1.2.7                             depth[firstnull]=0;
229 bdemsky  1.1.2.7                             thisthread=firstnull;
230 bdemsky  1.1.2.8                         } else
231 bdemsky  1.1.2.8                             threadoverflow++;
232 bdemsky  1.1.2.7                     }
233 bdemsky  1.1.2.7                     if (thisthread!=-1) {
234 bdemsky  1.1.2.7                         if (depth[thisthread]<csize)
235 bdemsky  1.1.2.7                             callchain[depth[thisthread]][thisthread]=callsite+1;
236 bdemsky  1.1.2.8                         else
237 bdemsky  1.1.2.8                             depthoverflow++;
238 bdemsky  1.1.2.7                         depth[thisthread]++;
239 bdemsky  1.1.2.7                     }
240 bdemsky  1.1.2.6                 }
241 bdemsky  1.1.2.4             }
242 bdemsky  1.1.2.4     }
243 bdemsky  1.1.2.4 
244 bdemsky  1.1.2.4     static void callexit() {
245 bdemsky  1.1.2.6         if (setup)
246 bdemsky  1.1.2.6             synchronized(lock) {
247 bdemsky  1.1.2.7                 if (counton) {
248 bdemsky  1.1.2.7                     int thisthread=-1;
249 bdemsky  1.1.2.7                     Thread t=Thread.currentThread();
250 bdemsky  1.1.2.7                     for(int j=0;j<tsize;j++) {
251 bdemsky  1.1.2.7                         if (t==threadarray[j]) {
252 bdemsky  1.1.2.7                             thisthread=j;
253 bdemsky  1.1.2.7                             break;
254 bdemsky  1.1.2.7                         }
255 bdemsky  1.1.2.7                     }
256 bdemsky  1.1.2.7                     if (thisthread!=-1) {
257 bdemsky  1.1.2.7                         depth[thisthread]--;
258 bdemsky  1.1.2.7                         if (depth[thisthread]==0)
259 bdemsky  1.1.2.7                             threadarray[thisthread]=null;
260 bdemsky  1.1.2.7                     }
261 bdemsky  1.1.2.7                 }
262 bdemsky  1.1.2.6             }
263 bdemsky  1.1.2.4     }
264 bdemsky  1.1.2.4 
265 cananian 1.1.2.1     static void exit() {
266 bdemsky  1.1.2.2         counton=false;
267 cananian 1.1.2.1         //Show to the screen for the curious
268 bdemsky  1.1.2.2         System.out.println("Error count[no mapping]="+error);
269 bdemsky  1.1.2.2         System.out.println("# overflowed="+overflow);
270 bdemsky  1.1.2.8         System.out.println("Call stack depth overflow="+depthoverflow);
271 bdemsky  1.1.2.8         System.out.println("Call stack thread overflow="+threadoverflow);
272 bdemsky  1.1.2.4 
273 salcianu 1.3             System.out.println("Allocation array BEGIN");
274 salcianu 1.4             for(int i = 0; i < obj_count_length; i++)
275 salcianu 1.4                 if (obj_count[i] != 0)
276 salcianu 1.4                     System.out.println
277 salcianu 1.5                         (i + "  " + obj_count[i] + " " + 
278 salcianu 1.4                          ((i < mem_amount_length) ? mem_amount[i] : 0));
279 salcianu 1.3             System.out.println("Allocation array END");
280 salcianu 1.3     
281 bdemsky  1.1.2.4 
282 salcianu 1.3             System.out.println("Sync array BEGIN");
283 bdemsky  1.1.2.2         for(int i=0;i<sizesync;i++)
284 bdemsky  1.1.2.4             if (arraysync[i]!=0)
285 bdemsky  1.1.2.4                 System.out.println(i+"  "+arraysync[i]);
286 salcianu 1.3             System.out.println("Sync array END");
287 bdemsky  1.1.2.4 
288 bdemsky  1.1.2.4         int tripletcount=0;
289 bdemsky  1.1.2.4         System.out.println("Call Chain Allocation array");
290 bdemsky  1.1.2.4         for(int i=0;i<size1;i++) {
291 bdemsky  1.1.2.4             if (alloccall[i][0]!=0) {
292 bdemsky  1.1.2.4                 System.out.println(i+" Unknown or main caller callsite = "+alloccall[i][0]);
293 bdemsky  1.1.2.4                 tripletcount++;
294 bdemsky  1.1.2.4             }
295 bdemsky  1.1.2.4             for(int j=1;j<size2;j++)
296 bdemsky  1.1.2.4                 if (alloccall[i][j]!=0) {
297 bdemsky  1.1.2.4                     System.out.println(i+"  "+" "+(j-1)+" = "+alloccall[i][j]);
298 bdemsky  1.1.2.4                     tripletcount++;
299 bdemsky  1.1.2.4                 }
300 bdemsky  1.1.2.4         }
301 bdemsky  1.1.2.2 
302 cananian 1.1.2.1         try {
303 salcianu 1.3                 PrintStream fos
304 salcianu 1.3                     = new java.io.PrintStream(new FileOutputStream("profile"));
305 salcianu 1.3     
306 salcianu 1.4                 fos.println(obj_count_length);
307 salcianu 1.4                 for(int i = 0; i < obj_count_length; i++) {
308 salcianu 1.4                     fos.println(obj_count[i]);
309 salcianu 1.4                     fos.println((i < mem_amount_length) ? mem_amount[i] : 0);
310 salcianu 1.4                 }
311 salcianu 1.3     
312 bdemsky  1.1.2.2             fos.println(sizesync);
313 bdemsky  1.1.2.2             for(int i=0;i<sizesync;i++)
314 bdemsky  1.1.2.2                 fos.println(arraysync[i]);
315 salcianu 1.3     
316 bdemsky  1.1.2.4             fos.println(tripletcount);
317 bdemsky  1.1.2.4             for(int i=0;i<size1;i++)
318 bdemsky  1.1.2.4                 for(int j=0;j<size2;j++)
319 bdemsky  1.1.2.4                     if (alloccall[i][j]!=0) {
320 bdemsky  1.1.2.4                         fos.println(i);
321 bdemsky  1.1.2.4                         fos.println(j);
322 bdemsky  1.1.2.4                         fos.println(alloccall[i][j]);
323 bdemsky  1.1.2.4                     }
324 cananian 1.1.2.1             fos.close();
325 cananian 1.1.2.1         } catch (Exception e) {
326 cananian 1.1.2.1             e.printStackTrace();
327 cananian 1.1.2.1         }
328 cananian 1.1.2.1     }
329 cananian 1.1.2.1 }