* src/vm/jit/stack.c (new_stack_analyse): Add a third move at the
[cacao.git] / src / vm / statistics.c
1 /* src/vm/statistics.c - global varables for statistics
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: statistics.c 5093 2006-07-10 13:36:47Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <string.h> 
39 #include <sys/time.h>
40 #include <sys/resource.h>
41
42 #include "vm/types.h"
43
44 #include "toolbox/logging.h"
45 #include "vm/global.h"
46 #include "vm/options.h"
47 #include "vm/statistics.h"
48
49
50 /* global variables ***********************************************************/
51
52 static s8 loadingtime = 0;              /* accumulated loading time           */
53 static s8 loadingstarttime = 0;
54 static s8 loadingstoptime = 0;
55 static s4 loadingtime_recursion = 0;
56
57 static s8 compilingtime = 0;            /* accumulated compile time           */
58 static s8 compilingstarttime = 0;
59 static s8 compilingstoptime = 0;
60 static s4 compilingtime_recursion = 0;
61
62 s4 codememusage = 0;
63 s4 maxcodememusage = 0;
64
65 s4 memoryusage = 0;
66 s4 maxmemusage = 0;
67
68 s4 maxdumpsize = 0;
69
70 s4 globalallocateddumpsize = 0;
71 s4 globaluseddumpsize = 0;
72
73
74 /* variables for measurements *************************************************/
75
76 s4 size_classinfo  = 0;
77 s4 size_fieldinfo  = 0;
78 s4 size_methodinfo = 0;
79 s4 size_codeinfo   = 0;
80
81 int count_const_pool_len = 0;
82 int count_classref_len = 0;
83 int count_parsed_desc_len = 0;
84 int count_vftbl_len = 0;
85 int count_all_methods = 0;
86 int count_methods_marked_used = 0;  /* RTA */
87
88 int count_vmcode_len = 0;
89 int count_extable_len = 0;
90 int count_class_loads = 0;
91 int count_class_inits = 0;
92
93 int count_utf_len = 0;                  /* size of utf hash                   */
94 int count_utf_new = 0;                  /* calls of utf_new                   */
95 int count_utf_new_found  = 0;           /* calls of utf_new with fast return  */
96
97 int count_locals_conflicts = 0;         /* register allocator statistics */
98 int count_locals_spilled = 0;
99 int count_locals_register = 0;
100 int count_ss_spilled = 0;
101 int count_ss_register = 0;
102 int count_methods_allocated_by_lsra = 0;
103 int count_mem_move_bb = 0;
104 int count_interface_size = 0;
105 int count_argument_mem_ss = 0;
106 int count_argument_reg_ss = 0;
107 int count_method_in_register = 0;
108 int count_mov_reg_reg = 0;
109 int count_mov_mem_reg = 0;
110 int count_mov_reg_mem = 0;
111 int count_mov_mem_mem = 0;
112
113 int count_jit_calls = 0;
114 int count_methods = 0;
115 int count_spills = 0;
116 int count_spills_read = 0;
117 int count_pcmd_activ = 0;
118 int count_pcmd_drop = 0;
119 int count_pcmd_zero = 0;
120 int count_pcmd_const_store = 0;
121 int count_pcmd_const_alu = 0;
122 int count_pcmd_const_bra = 0;
123 int count_pcmd_load = 0;
124 int count_pcmd_move = 0;
125 int count_load_instruction = 0;
126 int count_pcmd_store = 0;
127 int count_pcmd_store_comb = 0;
128 int count_dup_instruction = 0;
129 int count_pcmd_op = 0;
130 int count_pcmd_mem = 0;
131 int count_pcmd_met = 0;
132 int count_pcmd_bra = 0;
133 int count_pcmd_table = 0;
134 int count_pcmd_return = 0;
135 int count_pcmd_returnx = 0;
136 int count_check_null = 0;
137 int count_check_bound = 0;
138 int count_max_basic_blocks = 0;
139 int count_basic_blocks = 0;
140 int count_javainstr = 0;
141 int count_max_javainstr = 0;
142 int count_javacodesize = 0;
143 int count_javaexcsize = 0;
144 int count_calls = 0;
145 int count_tryblocks = 0;
146 int count_code_len = 0;
147 int count_data_len = 0;
148 int count_cstub_len = 0;
149 int count_nstub_len = 0;
150 int count_max_new_stack = 0;
151 int count_upper_bound_new_stack = 0;
152
153 u8 count_native_function_calls=0;
154 u8 count_jni_callXmethod_calls=0;
155 u8 count_jni_calls=0;
156
157
158 static int count_block_stack_init[11] = {
159         0, 0, 0, 0, 0, 
160         0, 0, 0, 0, 0, 
161         0
162 };
163 int *count_block_stack = count_block_stack_init;
164 static int count_analyse_iterations_init[5] = {
165         0, 0, 0, 0, 0
166 };
167 int *count_analyse_iterations = count_analyse_iterations_init;
168 static int count_method_bb_distribution_init[9] = {
169         0, 0, 0, 0, 0,
170         0, 0, 0, 0
171 };
172 int *count_method_bb_distribution = count_method_bb_distribution_init;
173 static int count_block_size_distribution_init[18] = {
174         0, 0, 0, 0, 0,
175         0, 0, 0, 0, 0,
176         0, 0, 0, 0, 0,
177         0, 0, 0
178 };
179 int *count_block_size_distribution = count_block_size_distribution_init;
180 static int count_store_length_init[21] = {
181         0, 0, 0, 0, 0,
182         0, 0, 0, 0, 0,
183         0, 0, 0, 0, 0,
184         0, 0, 0, 0, 0,
185         0
186 };
187 int *count_store_length = count_store_length_init;
188 static int count_store_depth_init[11] = {
189         0, 0, 0, 0, 0,
190         0, 0, 0, 0, 0,
191         0
192 };
193 int *count_store_depth = count_store_depth_init;
194
195
196 /* instruction scheduler statistics *******************************************/
197
198 s4 count_schedule_basic_blocks = 0;
199 s4 count_schedule_nodes = 0;
200 s4 count_schedule_leaders = 0;
201 s4 count_schedule_max_leaders = 0;
202 s4 count_schedule_critical_path = 0;
203
204
205 /* nativeinvokation ***********************************************************
206
207    increments the native invokation count by one
208         
209 *******************************************************************************/
210
211 void nativeinvokation(void)
212 {
213         /* XXX do locking here */
214         count_native_function_calls++;
215 }
216
217
218 /* jnicallXmethodinvokation ***************************************************
219
220    increments the jni CallXMethod invokation count by one
221         
222 *******************************************************************************/
223
224 void jnicallXmethodnvokation(void)
225 {
226         /* XXX do locking here */
227         count_jni_callXmethod_calls++;
228 }
229
230
231 /* jniinvokation *************************************************************
232
233    increments the jni overall  invokation count by one
234         
235 *******************************************************************************/
236
237 void jniinvokation(void)
238 {
239         /* XXX do locking here */
240         count_jni_calls++;
241 }
242
243
244 /* getcputime *********************************** ******************************
245
246    Returns the used CPU time in microseconds
247         
248 *******************************************************************************/
249
250 s8 getcputime(void)
251 {
252         struct rusage ru;
253         int sec, usec;
254
255         getrusage(RUSAGE_SELF, &ru);
256         sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
257         usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
258
259         return sec * 1000000 + usec;
260 }
261
262
263 /* loadingtime_stop ************************************************************
264
265    XXX
266
267 *******************************************************************************/
268
269 void loadingtime_start(void)
270 {
271         loadingtime_recursion++;
272
273         if (loadingtime_recursion == 1)
274                 loadingstarttime = getcputime();
275 }
276
277
278 /* loadingtime_stop ************************************************************
279
280    XXX
281
282 *******************************************************************************/
283
284 void loadingtime_stop(void)
285 {
286         if (loadingtime_recursion == 1) {
287                 loadingstoptime = getcputime();
288                 loadingtime += (loadingstoptime - loadingstarttime);
289         }
290
291         loadingtime_recursion--;
292 }
293
294
295 /* compilingtime_stop **********************************************************
296
297    XXX
298
299 *******************************************************************************/
300
301 void compilingtime_start(void)
302 {
303         compilingtime_recursion++;
304
305         if (compilingtime_recursion == 1)
306                 compilingstarttime = getcputime();
307 }
308
309
310 /* compilingtime_stop **********************************************************
311
312    XXX
313
314 *******************************************************************************/
315
316 void compilingtime_stop(void)
317 {
318         if (compilingtime_recursion == 1) {
319                 compilingstoptime = getcputime();
320                 compilingtime += (compilingstoptime - compilingstarttime);
321         }
322
323         compilingtime_recursion--;
324 }
325
326
327 /* print_times *****************************************************************
328
329    Prints a summary of CPU time usage.
330
331 *******************************************************************************/
332
333 void print_times(void)
334 {
335         s8 totaltime;
336         s8 runtime;
337
338         totaltime = getcputime();
339         runtime = totaltime - loadingtime - compilingtime;
340
341 #if SIZEOF_VOID_P == 8
342         dolog("Time for loading classes: %6ld ms", loadingtime / 1000);
343         dolog("Time for compiling code:  %6ld ms", compilingtime / 1000);
344         dolog("Time for running program: %6ld ms", runtime / 1000);
345         dolog("Total time:               %6ld ms", totaltime / 1000);
346 #else
347         dolog("Time for loading classes: %6lld ms", loadingtime / 1000);
348         dolog("Time for compiling code:  %6lld ms", compilingtime / 1000);
349         dolog("Time for running program: %6lld ms", runtime / 1000);
350         dolog("Total time:               %6lld ms", totaltime / 1000);
351 #endif
352 }
353
354
355 /* print_stats *****************************************************************
356
357    outputs detailed compiler statistics
358
359 *******************************************************************************/
360
361 void print_stats(void)
362 {
363         s4    i;
364         float f;
365         s4    sum;
366
367
368         dolog("Number of JIT compiler calls: %6d", count_jit_calls);
369         dolog("Number of compiled methods:   %6d", count_methods);
370
371         dolog("Number of compiled basic blocks:               %6d",
372                   count_basic_blocks);
373         dolog("Number of max. basic blocks per method:        %6d",
374                   count_max_basic_blocks);
375
376         dolog("Number of compiled JavaVM instructions:        %6d",
377                   count_javainstr);
378         dolog("Number of max. JavaVM instructions per method: %6d",
379                   count_max_javainstr);
380         dolog("Size of compiled JavaVM instructions:          %6d(%d)",
381                   count_javacodesize, count_javacodesize - count_methods * 18);
382
383         dolog("Size of compiled Exception Tables:      %d", count_javaexcsize);
384         dolog("Number of Machine-Instructions: %d", count_code_len >> 2);
385         dolog("Number of Spills (write to memory): %d", count_spills);
386         dolog("Number of Spills (read from memory): %d", count_spills_read);
387         dolog("Number of Activ    Pseudocommands: %6d", count_pcmd_activ);
388         dolog("Number of Drop     Pseudocommands: %6d", count_pcmd_drop);
389         dolog("Number of Const    Pseudocommands: %6d (zero:%5d)",
390                   count_pcmd_load, count_pcmd_zero);
391         dolog("Number of ConstAlu Pseudocommands: %6d (cmp: %5d, store:%5d)",
392                   count_pcmd_const_alu, count_pcmd_const_bra, count_pcmd_const_store);
393         dolog("Number of Move     Pseudocommands: %6d", count_pcmd_move);
394         dolog("Number of Load     Pseudocommands: %6d", count_load_instruction);
395         dolog("Number of Store    Pseudocommands: %6d (combined: %5d)",
396                   count_pcmd_store, count_pcmd_store - count_pcmd_store_comb);
397         dolog("Number of OP       Pseudocommands: %6d", count_pcmd_op);
398         dolog("Number of DUP      Pseudocommands: %6d", count_dup_instruction);
399         dolog("Number of Mem      Pseudocommands: %6d", count_pcmd_mem);
400         dolog("Number of Method   Pseudocommands: %6d", count_pcmd_met);
401         dolog("Number of Branch   Pseudocommands: %6d (rets:%5d, Xrets: %5d)",
402                   count_pcmd_bra, count_pcmd_return, count_pcmd_returnx);
403         dolog("Number of Table    Pseudocommands: %6d", count_pcmd_table);
404         dolog("Number of Useful   Pseudocommands: %6d", count_pcmd_table +
405                   count_pcmd_bra + count_pcmd_load + count_pcmd_mem + count_pcmd_op);
406         dolog("Number of Null Pointer Checks:     %6d", count_check_null);
407         dolog("Number of Array Bound Checks:      %6d", count_check_bound);
408         dolog("Number of Try-Blocks: %d", count_tryblocks);
409         dolog("Maximal count of stack elements:   %d", count_max_new_stack);
410         dolog("Upper bound of max stack elements: %d", count_upper_bound_new_stack);
411         dolog("Distribution of stack sizes at block boundary");
412         dolog("     0     1     2     3     4     5     6     7     8     9  >=10");
413         dolog("%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d",
414                   count_block_stack[0], count_block_stack[1], count_block_stack[2],
415                   count_block_stack[3], count_block_stack[4], count_block_stack[5],
416                   count_block_stack[6], count_block_stack[7], count_block_stack[8],
417                   count_block_stack[9], count_block_stack[10]);
418         dolog("Distribution of store stack depth");
419         dolog("     0     1     2     3     4     5     6     7     8     9  >=10");
420         dolog("%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d",
421                   count_store_depth[0], count_store_depth[1], count_store_depth[2],
422                   count_store_depth[3], count_store_depth[4], count_store_depth[5],
423                   count_store_depth[6], count_store_depth[7], count_store_depth[8],
424                   count_store_depth[9], count_store_depth[10]);
425         dolog("Distribution of store creator chains first part");
426         dolog("     0     1     2     3     4     5     6     7     8     9");
427         dolog("%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d",
428                   count_store_length[0], count_store_length[1], count_store_length[2],
429                   count_store_length[3], count_store_length[4], count_store_length[5],
430                   count_store_length[6], count_store_length[7], count_store_length[8],
431                   count_store_length[9]);
432         dolog("Distribution of store creator chains second part");
433         dolog("    10    11    12    13    14    15    16    17    18    19  >=20");
434         dolog("%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d",
435                   count_store_length[10], count_store_length[11],
436                   count_store_length[12], count_store_length[13],
437                   count_store_length[14], count_store_length[15],
438                   count_store_length[16], count_store_length[17],
439                   count_store_length[18], count_store_length[19],
440                   count_store_length[20]);
441         dolog("Distribution of analysis iterations");
442         dolog("     1     2     3     4   >=5");
443         dolog("%6d%6d%6d%6d%6d",
444                   count_analyse_iterations[0], count_analyse_iterations[1],
445                   count_analyse_iterations[2], count_analyse_iterations[3],
446                   count_analyse_iterations[4]);
447
448
449         /* Distribution of basic blocks per method ********************************/
450
451         log_println("Distribution of basic blocks per method:");
452         log_println("   <=5  <=10  <=15  <=20  <=30  <=40  <=50  <=75   >75");
453
454         log_start();
455         for (i = 0; i <= 8; i++)
456                 log_print("%6d", count_method_bb_distribution[i]);
457         log_finish();
458
459         /* print ratio */
460
461         f = (float) count_methods;
462
463         log_start();
464         for (i = 0; i <= 8; i++)
465                 log_print("%6.2f", (float) count_method_bb_distribution[i] / f);
466         log_finish();
467
468         /* print cumulated ratio */
469
470         log_start();
471         for (i = 0, sum = 0; i <= 8; i++) {
472                 sum += count_method_bb_distribution[i];
473                 log_print("%6.2f", (float) sum / f);
474         }
475         log_finish();
476
477
478         /* Distribution of basic block sizes **************************************/
479
480         log_println("Distribution of basic block sizes:");
481         log_println("     0     1     2     3     4     5     6     7     8     9   <13   <15   <17   <19   <21   <26   <31   >30");
482
483         /* print block sizes */
484
485         log_start();
486         for (i = 0; i <= 17; i++)
487                 log_print("%6d", count_block_size_distribution[i]);
488         log_finish();
489
490         /* print ratio */
491
492         f = (float) count_basic_blocks;
493
494         log_start();
495         for (i = 0; i <= 17; i++)
496                 log_print("%6.2f", (float) count_block_size_distribution[i] / f);
497         log_finish();
498
499         /* print cumulated ratio */
500
501         log_start();
502         for (i = 0, sum = 0; i <= 17; i++) {
503                 sum += count_block_size_distribution[i];
504                 log_print("%6.2f", (float) sum / f);
505         }
506         log_finish();
507
508         log_println("Size of Code Area:          %10.3f kB", (float) count_code_len / 1024);
509         log_println("Size of Data Area:          %10.3f kB", (float) count_data_len / 1024);
510
511         log_println("Size of classinfo  (%3d B): %10.3f kB", sizeof(classinfo), (float) size_classinfo / 1024);
512         log_println("Size of fieldinfo  (%3d B): %10.3f kB", sizeof(fieldinfo), (float) size_fieldinfo / 1024);
513         log_println("Size of methodinfo (%3d B): %10.3f kB", sizeof(methodinfo), (float) size_methodinfo / 1024);
514         log_println("Size of codeinfo   (%3d B): %10.3f kB", sizeof(codeinfo), (float) size_codeinfo / 1024);
515
516         log_println("Size of Const Pool:         %10.3f kB", (float) (count_const_pool_len + count_utf_len) / 1024);
517         log_println("Size of Class refs:         %10.3f kB", (float) count_classref_len / 1024);
518         log_println("Size of descriptors:        %10.3f kB", (float) count_parsed_desc_len / 1024);
519         log_println("Size of vftbl:              %10.3f kB", (float) count_vftbl_len / 1024);
520         log_println("Size of compiler stubs:     %10.3f kB", (float) count_cstub_len / 1024);
521         log_println("Size of native stubs:       %10.3f kB", (float) count_nstub_len / 1024);
522         log_println("Size of utf:                %10.3f kB", (float) count_utf_len / 1024);
523         log_println("Size of VMCode:             %10.3f kB", (float) count_vmcode_len / 1024);
524         log_println("Size of exception tables:   %10.3f kB\n", (float) count_extable_len / 1024);
525
526         dolog("Number of class loads:    %6d", count_class_loads);
527         dolog("Number of class inits:    %6d", count_class_inits);
528         dolog("Number of loaded Methods: %6d\n", count_all_methods);
529
530         dolog("Calls of utf_new:                 %6d", count_utf_new);
531         dolog("Calls of utf_new (element found): %6d\n", count_utf_new_found);
532
533
534         /* LSRA statistics ********************************************************/
535
536         dolog("Moves reg -> reg:     %6d", count_mov_reg_reg);
537         dolog("Moves mem -> reg:     %6d", count_mov_mem_reg);
538         dolog("Moves reg -> mem:     %6d", count_mov_reg_mem);
539         dolog("Moves mem -> mem:     %6d", count_mov_mem_mem);
540
541         dolog("Methods allocated by LSRA:         %6d",
542                   count_methods_allocated_by_lsra);
543         dolog("Conflicts between local Variables: %6d", count_locals_conflicts);
544         dolog("Local Variables held in Memory:    %6d", count_locals_spilled);
545         dolog("Local Variables held in Registers: %6d", count_locals_register);
546         dolog("Stackslots held in Memory:         %6d", count_ss_spilled);
547         dolog("Stackslots held in Registers:      %6d", count_ss_register);
548         dolog("Memory moves at BB Boundaries:     %6d", count_mem_move_bb);
549         dolog("Number of interface slots:         %6d\n", count_interface_size);
550         dolog("Number of Argument stack slots in register:  %6d",
551                   count_argument_reg_ss);
552         dolog("Number of Argument stack slots in memory:    %6d\n",
553                   count_argument_mem_ss);
554         dolog("Number of Methods kept in registers:         %6d\n",
555                   count_method_in_register);
556
557
558         /* instruction scheduler statistics ***************************************/
559
560 #if defined(USE_SCHEDULER)
561         dolog("Instruction scheduler statistics:");
562         dolog("Number of basic blocks:       %7d", count_schedule_basic_blocks);
563         dolog("Number of nodes:              %7d", count_schedule_nodes);
564         dolog("Number of leaders nodes:      %7d", count_schedule_leaders);
565         dolog("Number of max. leaders nodes: %7d", count_schedule_max_leaders);
566         dolog("Length of critical path:      %7d\n", count_schedule_critical_path);
567 #endif
568
569
570         /* call statistics ********************************************************/
571
572         dolog("Function call statistics:");
573         dolog("Number of native function invokations:           %ld",
574                   count_native_function_calls);
575         dolog("Number of jni->CallXMethod function invokations: %ld",
576                   count_jni_callXmethod_calls);
577         dolog("Overall number of jni invokations:               %ld",
578                   count_jni_calls);
579
580
581         /* now print other statistics ********************************************/
582
583 #if defined(ENABLE_INTRP)
584         print_dynamic_super_statistics();
585 #endif
586 }
587
588
589 /* mem_usagelog ****************************************************************
590
591    prints some memory related infos
592
593 *******************************************************************************/
594
595 void mem_usagelog(bool givewarnings)
596 {
597         if ((memoryusage != 0) && givewarnings) {
598                 dolog("Allocated memory not returned: %9d", (s4) memoryusage);
599         }
600
601         if ((globalallocateddumpsize != 0) && givewarnings) {
602                 dolog("Dump memory not returned:      %9d",
603                           (s4) globalallocateddumpsize);
604         }
605
606         dolog("Code - max. memory usage:      %9d kB",
607                   (s4) ((maxcodememusage + 1023) / 1024));
608         dolog("Random - max. memory usage:    %9d kB",
609                   (s4) ((maxmemusage + 1023) / 1024));
610         dolog("Dump - max. memory usage:      %9d kB",
611                   (s4) ((maxdumpsize + 1023) / 1024));
612 }
613
614
615 /*
616  * These are local overrides for various environment variables in Emacs.
617  * Please do not remove this and leave it at the end of the file, where
618  * Emacs will automagically detect them.
619  * ---------------------------------------------------------------------
620  * Local variables:
621  * mode: c
622  * indent-tabs-mode: t
623  * c-basic-offset: 4
624  * tab-width: 4
625  * End:
626  */