9ce305fdb8d2d87bedfe01d3ded7a1cfa011b87e
[cacao.git] / src / vm / statistics.c
1 /* statistics.c - global varables for statistics
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    Institut f. Computersprachen, TU Wien
5    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
6    S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
7    J. Wenninger
8
9    This file is part of CACAO.
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2, or (at
14    your option) any later version.
15
16    This program is distributed in the hope that it will be useful, but
17    WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.
25
26    Contact: cacao@complang.tuwien.ac.at
27
28    Authors: Christian Thalinger
29
30    $Id: statistics.c 1438 2004-11-05 09:52:49Z twisti $
31
32 */
33
34
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include "global.h"
38 #include "statistics.h"
39 #include "toolbox/logging.h"
40 #include "options.h"
41
42
43 /* global variables */
44
45 static s8 loadingtime = 0;              /* accumulated loading time           */
46 static s8 loadingstarttime = 0;
47 static s8 loadingstoptime = 0;
48 static s4 loadingtime_recursion = 0;
49
50 static s8 compilingtime = 0;            /* accumulated compile time           */
51 static s8 compilingstarttime = 0;
52 static s8 compilingstoptime = 0;
53 static s4 compilingtime_recursion = 0;
54
55 s4 memoryusage = 0;
56 s4 maxmemusage = 0;
57 s4 maxdumpsize = 0;
58
59 s4 globalallocateddumpsize = 0;
60 s4 globaluseddumpsize = 0;
61
62 int count_class_infos = 0;              /* variables for measurements         */
63 int count_const_pool_len = 0;
64 int count_vftbl_len = 0;
65 int count_all_methods = 0;
66 int count_methods_marked_used = 0;  /* RTA */
67
68 int count_vmcode_len = 0;
69 int count_extable_len = 0;
70 int count_class_loads = 0;
71 int count_class_inits = 0;
72
73 int count_utf_len = 0;                  /* size of utf hash                   */
74 int count_utf_new = 0;                  /* calls of utf_new                   */
75 int count_utf_new_found  = 0;           /* calls of utf_new with fast return  */
76
77 int count_jit_calls = 0;
78 int count_methods = 0;
79 int count_spills = 0;
80 int count_pcmd_activ = 0;
81 int count_pcmd_drop = 0;
82 int count_pcmd_zero = 0;
83 int count_pcmd_const_store = 0;
84 int count_pcmd_const_alu = 0;
85 int count_pcmd_const_bra = 0;
86 int count_pcmd_load = 0;
87 int count_pcmd_move = 0;
88 int count_load_instruction = 0;
89 int count_pcmd_store = 0;
90 int count_pcmd_store_comb = 0;
91 int count_dup_instruction = 0;
92 int count_pcmd_op = 0;
93 int count_pcmd_mem = 0;
94 int count_pcmd_met = 0;
95 int count_pcmd_bra = 0;
96 int count_pcmd_table = 0;
97 int count_pcmd_return = 0;
98 int count_pcmd_returnx = 0;
99 int count_check_null = 0;
100 int count_check_bound = 0;
101 int count_max_basic_blocks = 0;
102 int count_basic_blocks = 0;
103 int count_javainstr = 0;
104 int count_max_javainstr = 0;
105 int count_javacodesize = 0;
106 int count_javaexcsize = 0;
107 int count_calls = 0;
108 int count_tryblocks = 0;
109 int count_code_len = 0;
110 int count_data_len = 0;
111 int count_cstub_len = 0;
112 int count_nstub_len = 0;
113 int count_max_new_stack = 0;
114 int count_upper_bound_new_stack = 0;
115 static int count_block_stack_init[11] = {
116         0, 0, 0, 0, 0, 
117         0, 0, 0, 0, 0, 
118         0
119 };
120 int *count_block_stack = count_block_stack_init;
121 static int count_analyse_iterations_init[5] = {
122         0, 0, 0, 0, 0
123 };
124 int *count_analyse_iterations = count_analyse_iterations_init;
125 static int count_method_bb_distribution_init[9] = {
126         0, 0, 0, 0, 0,
127         0, 0, 0, 0
128 };
129 int *count_method_bb_distribution = count_method_bb_distribution_init;
130 static int count_block_size_distribution_init[18] = {
131         0, 0, 0, 0, 0,
132         0, 0, 0, 0, 0,
133         0, 0, 0, 0, 0,
134         0, 0, 0
135 };
136 int *count_block_size_distribution = count_block_size_distribution_init;
137 static int count_store_length_init[21] = {
138         0, 0, 0, 0, 0,
139         0, 0, 0, 0, 0,
140         0, 0, 0, 0, 0,
141         0, 0, 0, 0, 0,
142         0
143 };
144 int *count_store_length = count_store_length_init;
145 static int count_store_depth_init[11] = {
146         0, 0, 0, 0, 0,
147         0, 0, 0, 0, 0,
148         0
149 };
150 int *count_store_depth = count_store_depth_init;
151
152
153 /* getcputime *********************************** ******************************
154
155    Returns the used CPU time in microseconds
156         
157 *******************************************************************************/
158
159 s8 getcputime()
160 {
161         struct rusage ru;
162         int sec, usec;
163
164         getrusage(RUSAGE_SELF, &ru);
165         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
166         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
167
168         return sec * 1000000 + usec;
169 }
170
171
172 /* loadingtime_stop ************************************************************
173
174    XXX
175
176 *******************************************************************************/
177
178 void loadingtime_start()
179 {
180         loadingtime_recursion++;
181
182         if (loadingtime_recursion == 1)
183                 loadingstarttime = getcputime();
184 }
185
186
187 /* loadingtime_stop ************************************************************
188
189    XXX
190
191 *******************************************************************************/
192
193 void loadingtime_stop()
194 {
195         if (loadingtime_recursion == 1) {
196                 loadingstoptime = getcputime();
197                 loadingtime += (loadingstoptime - loadingstarttime);
198         }
199
200         loadingtime_recursion--;
201 }
202
203
204 /* compilingtime_stop **********************************************************
205
206    XXX
207
208 *******************************************************************************/
209
210 void compilingtime_start()
211 {
212         compilingtime_recursion++;
213
214         if (compilingtime_recursion == 1)
215                 compilingstarttime = getcputime();
216 }
217
218
219 /* compilingtime_stop **********************************************************
220
221    XXX
222
223 *******************************************************************************/
224
225 void compilingtime_stop()
226 {
227         if (compilingtime_recursion == 1) {
228                 compilingstoptime = getcputime();
229                 compilingtime += (compilingstoptime - compilingstarttime);
230         }
231
232         compilingtime_recursion--;
233 }
234
235
236 /* print_times *****************************************************************
237
238    Prints a summary of CPU time usage.
239
240 *******************************************************************************/
241
242 void print_times()
243 {
244         s8 totaltime = getcputime();
245         s8 runtime = totaltime - loadingtime - compilingtime;
246         char logtext[MAXLOGTEXT];
247
248 #if defined(__I386__) || defined(__POWERPC__)
249         sprintf(logtext, "Time for loading classes: %lld secs, %lld millis",
250 #else
251         sprintf(logtext, "Time for loading classes: %ld secs, %ld millis",
252 #endif
253                         loadingtime / 1000000, (loadingtime % 1000000) / 1000);
254         log_text(logtext);
255
256 #if defined(__I386__) || defined(__POWERPC__) 
257         sprintf(logtext, "Time for compiling code:  %lld secs, %lld millis",
258 #else
259         sprintf(logtext, "Time for compiling code:  %ld secs, %ld millis",
260 #endif
261                         compilingtime / 1000000, (compilingtime % 1000000) / 1000);
262         log_text(logtext);
263
264 #if defined(__I386__) || defined(__POWERPC__) 
265         sprintf(logtext, "Time for running program: %lld secs, %lld millis",
266 #else
267         sprintf(logtext, "Time for running program: %ld secs, %ld millis",
268 #endif
269                         runtime / 1000000, (runtime % 1000000) / 1000);
270         log_text(logtext);
271
272 #if defined(__I386__) || defined(__POWERPC__) 
273         sprintf(logtext, "Total time: %lld secs, %lld millis",
274 #else
275         sprintf(logtext, "Total time: %ld secs, %ld millis",
276 #endif
277                         totaltime / 1000000, (totaltime % 1000000) / 1000);
278         log_text(logtext);
279 }
280
281
282 /* print_stats *****************************************************************
283
284    outputs detailed compiler statistics
285
286 *******************************************************************************/
287
288 void print_stats()
289 {
290         char logtext[MAXLOGTEXT];
291
292         sprintf(logtext, "Number of JitCompiler Calls: %d", count_jit_calls);
293         log_text(logtext);
294         sprintf(logtext, "Number of compiled Methods: %d", count_methods);
295         log_text(logtext);
296         if (opt_rt) {
297           sprintf(logtext, "Number of Methods marked Used: %d", count_methods_marked_used);
298           log_text(logtext);
299           }
300         sprintf(logtext, "Number of max basic blocks per method: %d", count_max_basic_blocks);
301         log_text(logtext);
302         sprintf(logtext, "Number of compiled basic blocks: %d", count_basic_blocks);
303         log_text(logtext);
304         sprintf(logtext, "Number of max JavaVM-Instructions per method: %d", count_max_javainstr);
305         log_text(logtext);
306         sprintf(logtext, "Number of compiled JavaVM-Instructions: %d", count_javainstr);
307         log_text(logtext);
308         sprintf(logtext, "Size of compiled JavaVM-Instructions:   %d(%d)", count_javacodesize,
309                         count_javacodesize - count_methods * 18);
310         log_text(logtext);
311         sprintf(logtext, "Size of compiled Exception Tables:      %d", count_javaexcsize);
312         log_text(logtext);
313         sprintf(logtext, "Number of Machine-Instructions: %d", count_code_len >> 2);
314         log_text(logtext);
315         sprintf(logtext, "Number of Spills: %d", count_spills);
316         log_text(logtext);
317         sprintf(logtext, "Number of Activ    Pseudocommands: %5d", count_pcmd_activ);
318         log_text(logtext);
319         sprintf(logtext, "Number of Drop     Pseudocommands: %5d", count_pcmd_drop);
320         log_text(logtext);
321         sprintf(logtext, "Number of Const    Pseudocommands: %5d (zero:%5d)", count_pcmd_load, count_pcmd_zero);
322         log_text(logtext);
323         sprintf(logtext, "Number of ConstAlu Pseudocommands: %5d (cmp: %5d, store:%5d)", count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
324         log_text(logtext);
325         sprintf(logtext, "Number of Move     Pseudocommands: %5d", count_pcmd_move);
326         log_text(logtext);
327         sprintf(logtext, "Number of Load     Pseudocommands: %5d", count_load_instruction);
328         log_text(logtext);
329         sprintf(logtext, "Number of Store    Pseudocommands: %5d (combined: %5d)", count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
330         log_text(logtext);
331         sprintf(logtext, "Number of OP       Pseudocommands: %5d", count_pcmd_op);
332         log_text(logtext);
333         sprintf(logtext, "Number of DUP      Pseudocommands: %5d", count_dup_instruction);
334         log_text(logtext);
335         sprintf(logtext, "Number of Mem      Pseudocommands: %5d", count_pcmd_mem);
336         log_text(logtext);
337         sprintf(logtext, "Number of Method   Pseudocommands: %5d", count_pcmd_met);
338         log_text(logtext);
339         sprintf(logtext, "Number of Branch   Pseudocommands: %5d (rets:%5d, Xrets: %5d)",
340                         count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
341         log_text(logtext);
342         sprintf(logtext, "Number of Table    Pseudocommands: %5d", count_pcmd_table);
343         log_text(logtext);
344         sprintf(logtext, "Number of Useful   Pseudocommands: %5d", count_pcmd_table +
345                         count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
346         log_text(logtext);
347         sprintf(logtext, "Number of Null Pointer Checks:     %5d", count_check_null);
348         log_text(logtext);
349         sprintf(logtext, "Number of Array Bound Checks:      %5d", count_check_bound);
350         log_text(logtext);
351         sprintf(logtext, "Number of Try-Blocks: %d", count_tryblocks);
352         log_text(logtext);
353         sprintf(logtext, "Maximal count of stack elements:   %d", count_max_new_stack);
354         log_text(logtext);
355         sprintf(logtext, "Upper bound of max stack elements: %d", count_upper_bound_new_stack);
356         log_text(logtext);
357         sprintf(logtext, "Distribution of stack sizes at block boundary");
358         log_text(logtext);
359         sprintf(logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
360         log_text(logtext);
361         sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_block_stack[0],
362                         count_block_stack[1], count_block_stack[2], count_block_stack[3], count_block_stack[4],
363                         count_block_stack[5], count_block_stack[6], count_block_stack[7], count_block_stack[8],
364                         count_block_stack[9], count_block_stack[10]);
365         log_text(logtext);
366         sprintf(logtext, "Distribution of store stack depth");
367         log_text(logtext);
368         sprintf(logtext, "    0    1    2    3    4    5    6    7    8    9    >=10");
369         log_text(logtext);
370         sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_depth[0],
371                         count_store_depth[1], count_store_depth[2], count_store_depth[3], count_store_depth[4],
372                         count_store_depth[5], count_store_depth[6], count_store_depth[7], count_store_depth[8],
373                         count_store_depth[9], count_store_depth[10]);
374         log_text(logtext);
375         sprintf(logtext, "Distribution of store creator chains first part");
376         log_text(logtext);
377         sprintf(logtext, "    0    1    2    3    4    5    6    7    8    9  ");
378         log_text(logtext);
379         sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[0],
380                         count_store_length[1], count_store_length[2], count_store_length[3], count_store_length[4],
381                         count_store_length[5], count_store_length[6], count_store_length[7], count_store_length[8],
382                         count_store_length[9]);
383         log_text(logtext);
384         sprintf(logtext, "Distribution of store creator chains second part");
385         log_text(logtext);
386         sprintf(logtext, "   10   11   12   13   14   15   16   17   18   19  >=20");
387         log_text(logtext);
388         sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_store_length[10],
389                         count_store_length[11], count_store_length[12], count_store_length[13], count_store_length[14],
390                         count_store_length[15], count_store_length[16], count_store_length[17], count_store_length[18],
391                         count_store_length[19], count_store_length[20]);
392         log_text(logtext);
393         sprintf(logtext, "Distribution of analysis iterations");
394         log_text(logtext);
395         sprintf(logtext, "    1    2    3    4    >=5");
396         log_text(logtext);
397         sprintf(logtext, "%5d%5d%5d%5d%5d", count_analyse_iterations[0], count_analyse_iterations[1],
398                         count_analyse_iterations[2], count_analyse_iterations[3], count_analyse_iterations[4]);
399         log_text(logtext);
400         sprintf(logtext, "Distribution of basic blocks per method");
401         log_text(logtext);
402         sprintf(logtext, " <= 5 <=10 <=15 <=20 <=30 <=40 <=50 <=75  >75");
403         log_text(logtext);
404         sprintf(logtext, "%5d%5d%5d%5d%5d%5d%5d%5d%5d", count_method_bb_distribution[0],
405                         count_method_bb_distribution[1], count_method_bb_distribution[2], count_method_bb_distribution[3],
406                         count_method_bb_distribution[4], count_method_bb_distribution[5], count_method_bb_distribution[6],
407                         count_method_bb_distribution[7], count_method_bb_distribution[8]);
408         log_text(logtext);
409         sprintf(logtext, "Distribution of basic block sizes");
410         log_text(logtext);
411         sprintf(logtext,
412                          "  0    1    2    3    4   5   6   7   8   9 <13 <15 <17 <19 <21 <26 <31 >30");
413         log_text(logtext);
414         sprintf(logtext, "%3d%5d%5d%5d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d",
415                         count_block_size_distribution[0], count_block_size_distribution[1], count_block_size_distribution[2],
416                         count_block_size_distribution[3], count_block_size_distribution[4], count_block_size_distribution[5],
417                         count_block_size_distribution[6], count_block_size_distribution[7], count_block_size_distribution[8],
418                         count_block_size_distribution[9], count_block_size_distribution[10], count_block_size_distribution[11],
419                         count_block_size_distribution[12], count_block_size_distribution[13], count_block_size_distribution[14],
420                         count_block_size_distribution[15], count_block_size_distribution[16], count_block_size_distribution[17]);
421         log_text(logtext);
422         sprintf(logtext, "Size of Code Area (Kb):  %10.3f", (float) count_code_len / 1024);
423         log_text(logtext);
424         sprintf(logtext, "Size of data Area (Kb):  %10.3f", (float) count_data_len / 1024);
425         log_text(logtext);
426         sprintf(logtext, "Size of Class Infos (Kb):%10.3f", (float) (count_class_infos) / 1024);
427         log_text(logtext);
428         sprintf(logtext, "Size of Const Pool (Kb): %10.3f", (float) (count_const_pool_len + count_utf_len) / 1024);
429         log_text(logtext);
430         sprintf(logtext, "Size of Vftbl (Kb):      %10.3f", (float) count_vftbl_len / 1024);
431         log_text(logtext);
432         sprintf(logtext, "Size of comp stub (Kb):  %10.3f", (float) count_cstub_len / 1024);
433         log_text(logtext);
434         sprintf(logtext, "Size of native stub (Kb):%10.3f", (float) count_nstub_len / 1024);
435         log_text(logtext);
436         sprintf(logtext, "Size of Utf (Kb):        %10.3f", (float) count_utf_len / 1024);
437         log_text(logtext);
438         sprintf(logtext, "Size of VMCode (Kb):     %10.3f(%d)", (float) count_vmcode_len / 1024,
439                         count_vmcode_len - 18 * count_all_methods);
440         log_text(logtext);
441         sprintf(logtext, "Size of ExTable (Kb):    %10.3f", (float) count_extable_len / 1024);
442         log_text(logtext);
443         sprintf(logtext, "Number of class loads:   %d", count_class_loads);
444         log_text(logtext);
445         sprintf(logtext, "Number of class inits:   %d", count_class_inits);
446         log_text(logtext);
447         sprintf(logtext, "Number of loaded Methods: %d\n\n", count_all_methods);
448         log_text(logtext);
449
450         sprintf(logtext, "Calls of utf_new: %22d", count_utf_new);
451         log_text(logtext);
452         sprintf(logtext, "Calls of utf_new (element found): %6d\n\n", count_utf_new_found);
453         log_text(logtext);
454 }
455
456
457 /* mem_usagelog ****************************************************************
458
459    prints some memory related infos
460
461 *******************************************************************************/
462
463 void mem_usagelog(bool givewarnings)
464 {
465         if ((memoryusage != 0) && givewarnings) {
466                 dolog("Allocated memory not returned: %d", (s4) memoryusage);
467         }
468
469         if ((globalallocateddumpsize != 0) && givewarnings) {
470                 dolog("Dump memory not returned: %d", (s4) globalallocateddumpsize);
471         }
472
473         dolog("Random/Dump - max. memory usage: %dkB/%dkB", 
474                   (s4) ((maxmemusage + 1023) / 1024),
475                   (s4) ((maxdumpsize + 1023) / 1024));
476 }
477
478
479 /*
480  * These are local overrides for various environment variables in Emacs.
481  * Please do not remove this and leave it at the end of the file, where
482  * Emacs will automagically detect them.
483  * ---------------------------------------------------------------------
484  * Local variables:
485  * mode: c
486  * indent-tabs-mode: t
487  * c-basic-offset: 4
488  * tab-width: 4
489  * End:
490  */