1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.agent; |
23 |
|
|
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import org.apache.log4j.Logger; |
31 |
|
|
32 |
|
import net.sf.infrared.agent.util.MutableInteger; |
33 |
|
import net.sf.infrared.base.model.ExecutionTimer; |
34 |
|
import net.sf.infrared.base.model.LayerTime; |
35 |
|
import net.sf.infrared.base.util.LoggingFactory; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
3 |
public class LayerTimeTracker { |
43 |
2 |
private static final Logger log = LoggingFactory.getLogger(LayerTimeTracker.class); |
44 |
|
|
45 |
|
private static final String LAYER_SEPARATOR = "."; |
46 |
|
|
47 |
|
private static final int LAYER_PATH_INITIAL_SIZE = 256; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
2 |
private Map layerTimings = new HashMap(); |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
2 |
private Map layerCount = new HashMap(); |
58 |
|
|
59 |
2 |
private StringBuffer layerPath = new StringBuffer(LAYER_PATH_INITIAL_SIZE); |
60 |
|
|
61 |
2 |
private long pruneThreshold = -1; |
62 |
|
|
63 |
|
public void enterLayer(ExecutionTimer et) { |
64 |
0 |
String layer = et.getContext().getLayer(); |
65 |
0 |
enterLayer(layer); |
66 |
0 |
} |
67 |
|
|
68 |
|
public boolean leaveLayer(ExecutionTimer et) { |
69 |
0 |
String layer = et.getContext().getLayer(); |
70 |
0 |
return leaveLayer(layer, et.getInclusiveTime()); |
71 |
|
} |
72 |
|
|
73 |
|
public Map reset(boolean isFaulty) { |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
0 |
Map oldLayerTimings = layerTimings; |
78 |
0 |
layerTimings = new HashMap(); |
79 |
0 |
if (isFaulty) { |
80 |
0 |
layerCount.clear(); |
81 |
0 |
layerPath = new StringBuffer(LAYER_PATH_INITIAL_SIZE); |
82 |
|
} |
83 |
|
|
84 |
0 |
return oldLayerTimings; |
85 |
|
} |
86 |
|
|
87 |
|
public Map getLayerTimings() { |
88 |
2 |
return Collections.unmodifiableMap(layerTimings); |
89 |
|
} |
90 |
|
|
91 |
|
public String toString() { |
92 |
0 |
return "LayerTimeTracker for thread " + Thread.currentThread(); |
93 |
|
} |
94 |
|
|
95 |
|
public String getCurrentLayer() { |
96 |
8 |
return layerPath.toString().intern(); |
97 |
|
} |
98 |
|
|
99 |
|
void enterLayer(String layer) { |
100 |
9 |
MutableInteger currentCount = getLayerCount(layer); |
101 |
|
|
102 |
9 |
if (currentCount.isZero()) { |
103 |
8 |
addLayerToPath(layer); |
104 |
|
} |
105 |
|
|
106 |
9 |
currentCount.increment(); |
107 |
|
|
108 |
9 |
if (log.isDebugEnabled()) { |
109 |
0 |
log.debug(this + " - Entered layer " + layer + " for the " + currentCount + "-th time"); |
110 |
|
} |
111 |
9 |
} |
112 |
|
|
113 |
|
boolean leaveLayer(String layer, long time) { |
114 |
9 |
MutableInteger currentCount = getLayerCount(layer); |
115 |
|
assert currentCount.isPositive(): |
116 |
9 |
this + " - Mistmatch in Enter/Leave layer. Count should be +ve, was " + currentCount; |
117 |
|
|
118 |
|
|
119 |
9 |
currentCount.decrement(); |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
9 |
if (currentCount.isZero()) { |
127 |
|
|
128 |
|
|
129 |
8 |
addTimeToLayer(getCurrentLayer(), time); |
130 |
8 |
removeLayerFromPath(); |
131 |
|
} |
132 |
|
|
133 |
9 |
return true; |
134 |
|
} |
135 |
|
|
136 |
|
long getPruneBelowTime() { |
137 |
8 |
return pruneThreshold; |
138 |
|
} |
139 |
|
|
140 |
|
void setPruneBelowTime(long time) { |
141 |
0 |
pruneThreshold = time; |
142 |
0 |
} |
143 |
|
|
144 |
|
private MutableInteger getLayerCount(String layer) { |
145 |
18 |
MutableInteger count = (MutableInteger) layerCount.get(layer); |
146 |
|
|
147 |
18 |
if (count == null) { |
148 |
5 |
count = new MutableInteger(0); |
149 |
5 |
layerCount.put(layer, count); |
150 |
|
} |
151 |
|
|
152 |
18 |
return count; |
153 |
|
} |
154 |
|
|
155 |
|
private void addLayerToPath(String layer) { |
156 |
8 |
if (layerPath.length() > 0) { |
157 |
4 |
layerPath.append(LAYER_SEPARATOR); |
158 |
|
} |
159 |
|
|
160 |
8 |
layerPath.append(layer); |
161 |
8 |
} |
162 |
|
|
163 |
|
private void addTimeToLayer(String layer, long executionTime) { |
164 |
8 |
if (executionTime <= getPruneBelowTime()) { |
165 |
0 |
if (log.isDebugEnabled()) { |
166 |
0 |
log.debug("Discarded tracking of layer " + layer + |
167 |
|
" because the time (" + executionTime + |
168 |
|
") <= prune threshold (" + getPruneBelowTime() + ")"); |
169 |
|
} |
170 |
0 |
return; |
171 |
|
} |
172 |
8 |
LayerTime layerTime = (LayerTime) layerTimings.get(layer); |
173 |
|
|
174 |
8 |
if (layerTime == null) { |
175 |
7 |
layerTime = new LayerTime(layer); |
176 |
7 |
layerTimings.put(layer, layerTime); |
177 |
|
} |
178 |
|
|
179 |
8 |
layerTime.addToTime(executionTime); |
180 |
|
|
181 |
8 |
if (log.isDebugEnabled()) { |
182 |
0 |
log.debug(this + " - Adding " + executionTime + " to layer " + layer); |
183 |
|
} |
184 |
8 |
} |
185 |
|
|
186 |
|
private void removeLayerFromPath() { |
187 |
8 |
if (layerPath.lastIndexOf(LAYER_SEPARATOR) > -1) { |
188 |
|
|
189 |
4 |
layerPath.delete(layerPath.lastIndexOf(LAYER_SEPARATOR), layerPath.length()); |
190 |
|
} else { |
191 |
4 |
layerPath.delete(0, layerPath.length()); |
192 |
|
} |
193 |
8 |
} |
194 |
|
} |