Fixing a bug in checkForConflict method in Conflict Tracker analysis!
[jpf-core.git] / src / main / gov / nasa / jpf / listener / InsnCounter.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.listener;
19
20 import gov.nasa.jpf.ListenerAdapter;
21 import gov.nasa.jpf.search.Search;
22 import gov.nasa.jpf.vm.Instruction;
23 import gov.nasa.jpf.vm.ThreadInfo;
24 import gov.nasa.jpf.vm.VM;
25
26 /**
27  * simple tools to gather statistics about instructions executed by JPF.
28  * InsnCounter is mostly a VMListener that observes 'instructionExecuted'
29  */
30 public class InsnCounter extends ListenerAdapter {
31
32   String[] opCodes = new String[500];
33   int[] counts = new int[500];
34   int   total;
35   
36   //----------------------------------------- SearchKistener interface
37   @Override
38   public void searchFinished(Search search) {
39     reportStatistics();
40   }
41     
42   //----------------------------------------------------- VMListener interface
43   @Override
44   public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
45     int bc = executedInsn.getByteCode();
46     
47     if (opCodes[bc] == null) {
48       opCodes[bc] = executedInsn.getMnemonic();
49     }
50     counts[bc]++;
51     total++;
52   }
53
54   
55   //----------------------------------------------------- internal stuff
56   void reportStatistics () {
57     int[] sorted = getSortedCounts();
58     int i;
59     
60     int total = 0;
61     
62     for (i=0; i<sorted.length; i++) {
63       int idx = sorted[i];
64       String opc = opCodes[idx];
65             
66       if (counts[idx] > 0) {
67         System.out.print( i);
68         System.out.print( "  ");
69         System.out.print( opc);
70         System.out.print( " : ");
71         System.out.println( counts[idx]);
72         
73         total += counts[idx];
74       } else {
75         break;
76       }
77     }
78     
79     System.out.println();
80     System.out.println("total number of executed instructions: " + total);
81   }
82   
83   int[] getSortedCounts () {
84     int[] sorted = new int[256];
85     int last = -1;
86     int i, j;
87     
88     for (i=0; i<256; i++) {
89       int c = counts[i];
90       if (c > 0) {
91         for (j=0; j<last; j++) {
92           if (counts[sorted[j]] < c) {
93             System.arraycopy(sorted, j, sorted, j+1, (last-j));
94             break;
95           }
96         }
97         sorted[j] = i;
98         last++;
99       }
100     }
101     
102     return sorted;
103   }
104   
105   void filterArgs (String[] args) {
106     // we don't have any yet
107   }
108 }
109