1
15
16
17 package gate.util.profile;
18
19
44
45 import java.io.PrintStream;
46 import java.util.Enumeration;
47 import java.util.Hashtable;
48
49 public class Profiler {
50
51 private PrintStream m_out;
52 private boolean m_enabled = true;
53 private boolean m_garbageCollection = true;
54
56 private boolean m_doPrintToStdOut = true;
57
58 private Hashtable m_categorySums;
62
63 private Hashtable m_categoryLasts;
67
68 private Runtime m_rt;
69
70 private long m_startTime;
73 private long m_lastCheckTime, m_profilerTime, m_lastDuration;
74
75 private long m_maxMemory, m_currMemory, m_diffMemory;
76
77 public Profiler() {
78 m_rt = Runtime.getRuntime();
79 m_out = System.out;
80 }
81
82
85 public void enable(boolean isEnabled) {
86 m_enabled = isEnabled;
87 }
89
93 public boolean isEnabled() {
94 return m_enabled;
95 }
96
97
107 public void enableGCCalling(boolean collect) {
108 m_garbageCollection = collect;
109 }
110
111
114 public boolean isGCCallingEnabled() {
115 return m_garbageCollection;
116 }
117
118
119
124 public long getProfilerTime() {
125 return m_profilerTime;
126 }
127
128
133 public long getNetRunTime() {
134 return m_lastCheckTime - m_profilerTime;
135 };
136
137
142 public long getRunDuration() {
143 return m_lastCheckTime;
144 } ;
147
148
149
152 public String initRun(String runDescription) {
153 StringBuffer buf = new StringBuffer();
154 buf.append("-----------------------------------------------\n");
155 buf.append("New profiler run: " + runDescription);
156 buf.append("\n-----------------------------------------------\n");
157
158 m_maxMemory=0;
159 m_currMemory=0;
160 m_diffMemory=0;
161 m_profilerTime=0;
162 m_startTime = System.currentTimeMillis();
163 m_lastCheckTime=0;
164 m_lastDuration=0;
165
166 m_categorySums = new Hashtable();
167 m_categoryLasts = new Hashtable();
168 if ( m_doPrintToStdOut ) {
169 m_out.print(buf.toString());
170 }
171 return buf.toString();
172 }
174
179 public String checkPoint(String execPointDescr) {
180 return checkPoint(execPointDescr, new String[0], true, true, true);
181 }
182
183
190 public String checkPoint(String execPointDescr, String categories[],
191 boolean showDescr, boolean showStats, boolean memoryCheck)
192 {
193 if (!m_enabled)
194 return "";
195
196 long currTime = System.currentTimeMillis() - m_startTime;
197 m_lastDuration = currTime - m_lastCheckTime;
198
199 if (memoryCheck) {
200 long oldMemory = m_currMemory;
201
202 if (m_garbageCollection) {
203 do {
204 m_currMemory = m_rt.totalMemory() - m_rt.freeMemory();
205 m_rt.gc();
206 try {wait(300);} catch (Exception e) {}
207 m_rt.gc();
208 } while (m_currMemory > m_rt.totalMemory() - m_rt.freeMemory());
209 }
210 else {
211 m_currMemory = m_rt.totalMemory() - m_rt.freeMemory();
212 }
213
214 m_currMemory /= 1000;
215 m_maxMemory = Math.max(m_maxMemory, m_currMemory);
216 m_diffMemory = m_currMemory - oldMemory;
217 }
219 m_lastCheckTime = System.currentTimeMillis() - m_startTime;
220 m_profilerTime += (m_lastCheckTime - currTime);
221
222 checkCategories(categories);
223 return showResults(execPointDescr, showDescr, showStats);
224 }
226 private void checkCategories(String categs[]) {
227 int size = categs.length;
228 String categ;
229 long last, sum;
230 Long l;
231 for (int i=0; i<size; i++) {
232 categ = categs[i].toUpperCase();
233 l = (Long)m_categorySums.get(categ);
234 sum = (l==null) ? 0 : l.longValue();
235 sum += m_lastDuration;
236 m_categorySums.put(categ, new Long(sum));
237 m_categoryLasts.put(categ, new Long(m_lastDuration));
238 } }
241 private String showResults(String execPointDescr, boolean showDescr,
242 boolean showStats)
243 {
244 StringBuffer buff = new StringBuffer(500);
245 if (showDescr) {
246 buff.append("---------LOG: ");
247 buff.append(execPointDescr);
248 buff.append("---------");
249 }
250
251 if (showStats) {
252 buff.append("\nMemory: ");
253 buff.append(m_currMemory);
254 buff.append("k; change: ");
255 buff.append(m_diffMemory);
256 buff.append("k; max: ");
257 buff.append(m_maxMemory);
258 buff.append("k; Net time: ");
259 buff.append(printTime(getNetRunTime()));
260 buff.append("; since prev.: ");
261 buff.append(printTime(m_lastDuration));
262 }
267
268 if (buff.length() > 0 && m_doPrintToStdOut) {
269 m_out.println(buff.toString());
270 }
271 return buff.toString();
272 }
274
277 public long getCategoryTimeSum(String category) {
278 Long sum = (Long)m_categorySums.get(category.toUpperCase());
279 return (sum == null) ? 0 : sum.longValue();
280 }
282
285 public long getCategoryTimeLast(String category) {
286 Long sum = (Long)m_categoryLasts.get(category.toUpperCase());
287 return (sum == null) ? 0 : sum.longValue();
288 }
290
293 public void showCategoryTimes() {
294 m_out.println("Time spent by categories:");
295 Enumeration categNames = m_categorySums.keys();
296 String categ;
297 while (categNames.hasMoreElements()) {
298 categ = (String)categNames.nextElement();
299 showCategoryTime(categ);
300 } }
303
306 public void showCategoryTime(String categ) {
307 m_out.println(categ + ", sum=" +
308 printTime(getCategoryTimeSum(categ)) +
309 ", last=" + printTime(getCategoryTimeLast(categ)));
310 }
312
315 public String printTime(long timeMillis) {
316 long round = timeMillis/1000;
317 long remaind = (timeMillis % 1000)/10;
318 StringBuffer buff = new StringBuffer(10);
319 buff.append(round);
320 buff.append(".");
321 buff.append(remaind);
322 buff.append("s");
323 return buff.toString();
324 }
326
329 public String printSpeed(long timeMillis,
330 double whatever, String whateverMeasure)
331 {
332 double speed1000 = (double) whatever/ timeMillis;
333 long round = (long)((double)speed1000*1000);
334 long remaind = (long)(((double)speed1000*100000) - 100 * round);
335 StringBuffer buff = new StringBuffer(10);
336 buff.append(round);
337 buff.append(".");
338 buff.append(remaind);
339 buff.append(whateverMeasure);
340 buff.append("/s");
341 return buff.toString();
342 }
344
348 public void printCategAvg(String categ, long items,
349 double volume, String whateverMeasure)
350 {
351 long time = getCategoryTimeSum(categ);
352 if (time==0) {
353 m_out.println("Category \"" + categ + "\" not found");
354 }
355
356 m_out.println("Category \"" + categ + "\", Time= " +
357 printTime(time) + "; avg. time= " +
358 printTime(time/items) + "; speed= " +
359 printSpeed(time, volume, whateverMeasure));
360 }
362
367 public void printToSystemOut(boolean doPrint){
368 m_doPrintToStdOut = doPrint;
369 } }