Making the implementation of updateBacktrackSetDFS closer to the paper in DPORStateRe...
[jpf-core.git] / src / main / gov / nasa / jpf / report / Statistics.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
19 package gov.nasa.jpf.report;
20
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.jvm.bytecode.EXECUTENATIVE;
23 import gov.nasa.jpf.jvm.bytecode.JVMFieldInstruction;
24 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction;
25 import gov.nasa.jpf.jvm.bytecode.LockInstruction;
26 import gov.nasa.jpf.search.Search;
27 import gov.nasa.jpf.vm.ChoiceGenerator;
28 import gov.nasa.jpf.vm.ClassInfo;
29 import gov.nasa.jpf.vm.ElementInfo;
30 import gov.nasa.jpf.vm.Instruction;
31 import gov.nasa.jpf.vm.ThreadInfo;
32 import gov.nasa.jpf.vm.VM;
33 import gov.nasa.jpf.vm.MethodInfo;
34 import gov.nasa.jpf.vm.ThreadChoiceGenerator;
35
36 /**
37  * simple structure to hold statistics info created by Reporters/Publishers
38  * this is kind of a second tier SearchListener, which does not
39  * explicitly have to be registered
40  * 
41  * <2do> this should get generic and accessible enough to replace all the
42  * other statistics collectors, otherwise there is too much redundancy.
43  * If users have special requirements, they should subclass Statistics
44  * and set jpf.report.statistics.class accordingly
45  * 
46  * Note that Statistics might be accessed by a background thread
47  * reporting JPF progress, hence we have to synchronize
48  */
49 public class Statistics extends ListenerAdapter implements Cloneable {
50     
51   // we make these public since we don't want to add a gazillion of
52   // getters for these purely informal numbers
53   
54   public long maxUsed = 0;
55   public long newStates = 0;
56   public long backtracked = 0;
57   public long restored = 0;
58   public int processed = 0;
59   public int constraints = 0;
60   public long visitedStates = 0;
61   public long endStates = 0;
62   public int maxDepth = 0;
63   
64   public int gcCycles = 0;
65   public long insns = 0;
66   public int threadCGs = 0;
67   public int sharedAccessCGs = 0;
68   public int monitorCGs = 0;
69   public int signalCGs = 0;
70   public int threadApiCGs = 0;
71   public int breakTransitionCGs = 0;
72   public int dataCGs = 0;
73   public long nNewObjects = 0;
74   public long nReleasedObjects = 0;
75   public int maxLiveObjects = 0;
76
77   @Override
78   public Statistics clone() {
79     try {
80       return (Statistics)super.clone();
81     } catch (CloneNotSupportedException e) {
82       return null; // can't happen
83     }
84   }
85   
86   @Override
87   public void gcBegin (VM vm) {
88     int heapSize = vm.getHeap().size();
89     if (heapSize > maxLiveObjects){
90       maxLiveObjects = heapSize;
91     }
92
93     gcCycles++;
94   }
95   
96   @Override
97   public void instructionExecuted (VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn){
98     insns++;
99   }
100
101   @Override
102   public void choiceGeneratorSet (VM vm, ChoiceGenerator<?> newCG){
103     ChoiceGenerator<?> cg = VM.getVM().getChoiceGenerator();
104     if (cg instanceof ThreadChoiceGenerator){
105       threadCGs++;
106
107       Instruction insn = cg.getInsn();
108       if (insn instanceof JVMFieldInstruction) {
109         sharedAccessCGs++;
110       } else if (insn instanceof LockInstruction || insn instanceof JVMInvokeInstruction) {
111         monitorCGs++;
112       } else if (insn instanceof EXECUTENATIVE) {
113         MethodInfo mi = insn.getMethodInfo();
114         if (mi != null) {
115           ClassInfo ci = mi.getClassInfo();
116           if (ci != null) {
117             if (ci.isObjectClassInfo()) {
118               // its got to be either a wait or a notify since we know the java.lang.Object methods
119               signalCGs++;
120             } else if (ci.isThreadClassInfo()) {
121               threadApiCGs++;
122             }
123           } else {
124             // Hmm - a CG from a synthetic method?
125           }
126         } else {
127           // even more Hmmm - a GC from a synthesized instruction 
128         }
129       } else {
130         breakTransitionCGs++; // e.g. max_transition_length or idleLoop breakers
131       }
132     } else {
133       dataCGs++;
134     }
135   }
136   
137   @Override
138   public void objectCreated (VM vm, ThreadInfo ti, ElementInfo ei){
139     nNewObjects++;
140   }
141   
142   @Override
143   public void objectReleased (VM vm, ThreadInfo ti, ElementInfo ei){
144     nReleasedObjects++;
145   }
146   
147   @Override
148   public void stateAdvanced (Search search){
149     long m = Runtime.getRuntime().totalMemory();
150     if (m > maxUsed) {
151       maxUsed = m;
152     }
153
154     if (search.isNewState()){
155       newStates++;
156       int depth = search.getDepth();
157       if (depth > maxDepth){
158         maxDepth = depth;
159       }
160     } else {
161       visitedStates++;
162     }
163     if (search.isEndState()){
164       endStates++;
165     }
166   }
167   
168   @Override
169   public void stateBacktracked (Search search){
170     backtracked++;
171   }
172   
173   @Override
174   public void stateProcessed (Search search){
175     processed++;
176   }
177
178   @Override
179   public void stateRestored (Search search){
180     restored++;
181   }
182   
183   @Override
184   public void searchConstraintHit (Search search){
185     constraints++;
186   }
187
188 }