KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > tools > InsnCounter


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.tools;
20
21 import gov.nasa.jpf.VMListener;
22 import gov.nasa.jpf.VM;
23 import gov.nasa.jpf.SearchListener;
24 import gov.nasa.jpf.jvm.JVM;
25 import gov.nasa.jpf.jvm.bytecode.Instruction;
26 import gov.nasa.jpf.Search;
27 import gov.nasa.jpf.Config;
28 import gov.nasa.jpf.JPF;
29
30 /**
31  * simple tools to gather statistics about instructions executed by JPF.
32  * InsnCounter is mostly a VMListener that observes 'instructionExecuted'
33  */

34 public class InsnCounter implements VMListener, SearchListener {
35
36   String JavaDoc[] opCodes = new String JavaDoc[256];
37   int[] counts = new int[256];
38   int total;
39   
40   //----------------------------------------- SearchKistener interface
41
public void searchFinished(Search search) {
42     reportStatistics();
43   }
44   
45   public void stateBacktracked(Search search) {
46     // not interested
47
}
48   public void propertyViolated(Search search) {
49     // not interested
50
}
51   public void searchConstraintHit(Search search) {
52     // not interested
53
}
54   public void stateAdvanced(Search search) {
55     // not interested
56
}
57   public void stateProcessed(Search search) {
58     // not interested
59
}
60   public void searchStarted(Search search) {
61     // not interested
62
}
63   public void stateRestored(Search search) {
64     // not interested
65
}
66   
67   
68   //----------------------------------------------------- VMListener interface
69
public void instructionExecuted(VM vm) {
70     JVM jvm = (JVM)vm;
71     Instruction insn = jvm.getLastInstruction();
72     int bc = insn.getByteCode();
73     
74     if (opCodes[bc] == null) {
75       opCodes[bc] = insn.getMnemonic();
76     }
77     counts[bc]++;
78     total++;
79   }
80   
81   public void threadStarted(VM vm) {
82     // not interested
83
}
84   public void threadTerminated(VM vm) {
85     // not interested
86
}
87   public void objectCreated(VM vm) {
88     // not interested
89
}
90   public void objectReleased(VM vm) {
91     // not interested
92
}
93   public void gcBegin (VM vm) {
94     // not interested
95
}
96   public void gcEnd (VM vm) {
97     // not interested
98
}
99   public void classLoaded(VM vm) {
100     // not interested
101
}
102   public void exceptionThrown(VM vm) {
103     // not interested
104
}
105   
106   
107   //----------------------------------------------------- internal stuff
108
void reportStatistics () {
109     int[] sorted = getSortedCounts();
110     int i;
111     
112     for (i=0; i<sorted.length; i++) {
113       int idx = sorted[i];
114       String JavaDoc opc = opCodes[idx];
115             
116       if (counts[idx] > 0) {
117         System.out.print( i);
118         System.out.print( " ");
119         System.out.print( opc);
120         System.out.print( " : ");
121         System.out.println( counts[idx]);
122       } else {
123         break;
124       }
125     }
126   }
127   
128   int[] getSortedCounts () {
129     int[] sorted = new int[256];
130     int last = -1;
131     int i, j;
132     
133     for (i=0; i<256; i++) {
134       int c = counts[i];
135       if (c > 0) {
136         for (j=0; j<last; j++) {
137           if (counts[sorted[j]] < c) {
138             System.arraycopy(sorted, j, sorted, j+1, (last-j));
139             break;
140           }
141         }
142         sorted[j] = i;
143         last++;
144       }
145     }
146     
147     return sorted;
148   }
149   
150   void filterArgs (String JavaDoc[] args) {
151     // we don't have any yet
152
}
153   
154   public static void main (String JavaDoc[] args) {
155     InsnCounter listener = new InsnCounter();
156     listener.filterArgs(args);
157     
158     Config conf = JPF.createConfig(args);
159     // do own settings here
160

161     JPF jpf = new JPF(conf);
162     jpf.addSearchListener(listener);
163     jpf.addVMListener(listener);
164
165     jpf.run();
166   }
167 }
168
169
Popular Tags