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