* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / src / vm / rt-timing.c
1 /* src/vm/rt-timing.c - POSIX real-time timing utilities
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <time.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.hpp"
36
37 #include "vm/global.h"
38 #include "vm/rt-timing.h"
39
40
41 struct rt_timing_stat {
42         int index;
43         int totalindex;
44         const char *name;
45 };
46
47 static struct rt_timing_stat rt_timing_stat_defs[] = {
48     { RT_TIMING_JIT_CHECKS      ,RT_TIMING_JIT_TOTAL , "checks at beginning" },
49     { RT_TIMING_JIT_PARSE       ,RT_TIMING_JIT_TOTAL , "parse" },
50     { RT_TIMING_JIT_STACK       ,RT_TIMING_JIT_TOTAL , "analyse_stack" },
51     { RT_TIMING_JIT_TYPECHECK   ,RT_TIMING_JIT_TOTAL , "typecheck" },
52     { RT_TIMING_JIT_LOOP        ,RT_TIMING_JIT_TOTAL , "loop" },
53     { RT_TIMING_JIT_IFCONV      ,RT_TIMING_JIT_TOTAL , "if conversion" },
54     { RT_TIMING_JIT_ALLOC       ,RT_TIMING_JIT_TOTAL , "register allocation" },
55     { RT_TIMING_JIT_RPLPOINTS   ,RT_TIMING_JIT_TOTAL , "replacement point generation" },
56     { RT_TIMING_JIT_CODEGEN     ,RT_TIMING_JIT_TOTAL , "codegen" },
57     { RT_TIMING_JIT_TOTAL       ,-1                  , "total compile time" },
58     { -1                        ,-1                  , "" },
59
60     { RT_TIMING_LINK_RESOLVE    ,RT_TIMING_LINK_TOTAL, "link: resolve superclass/superinterfaces"},
61     { RT_TIMING_LINK_C_VFTBL    ,RT_TIMING_LINK_TOTAL, "link: compute vftbl length"},
62     { RT_TIMING_LINK_ABSTRACT   ,RT_TIMING_LINK_TOTAL, "link: handle abstract methods"},
63     { RT_TIMING_LINK_C_IFTBL    ,RT_TIMING_LINK_TOTAL, "link: compute interface table"},
64     { RT_TIMING_LINK_F_VFTBL    ,RT_TIMING_LINK_TOTAL, "link: fill vftbl"},
65     { RT_TIMING_LINK_OFFSETS    ,RT_TIMING_LINK_TOTAL, "link: set offsets"},
66     { RT_TIMING_LINK_F_IFTBL    ,RT_TIMING_LINK_TOTAL, "link: fill interface table"},
67     { RT_TIMING_LINK_FINALIZER  ,RT_TIMING_LINK_TOTAL, "link: set finalizer"},
68     { RT_TIMING_LINK_EXCEPTS    ,RT_TIMING_LINK_TOTAL, "link: resolve exception classes"},
69     { RT_TIMING_LINK_SUBCLASS   ,RT_TIMING_LINK_TOTAL, "link: re-calculate subclass indices"},
70     { RT_TIMING_LINK_TOTAL      ,-1                  , "total link time" },
71     { -1                        ,-1                  , "" },
72         
73         { RT_TIMING_LOAD_CHECKS     ,RT_TIMING_LOAD_TOTAL, "load: initial checks"},
74         { RT_TIMING_LOAD_NDPOOL     ,RT_TIMING_LOAD_TOTAL, "load: new descriptor pool"},
75         { RT_TIMING_LOAD_CPOOL      ,RT_TIMING_LOAD_TOTAL, "load: load constant pool"},
76         { RT_TIMING_LOAD_SETUP      ,RT_TIMING_LOAD_TOTAL, "load: class setup"},
77         { RT_TIMING_LOAD_FIELDS     ,RT_TIMING_LOAD_TOTAL, "load: load fields"},
78         { RT_TIMING_LOAD_METHODS    ,RT_TIMING_LOAD_TOTAL, "load: load methods"},
79         { RT_TIMING_LOAD_CLASSREFS  ,RT_TIMING_LOAD_TOTAL, "load: create classrefs"},
80         { RT_TIMING_LOAD_DESCS      ,RT_TIMING_LOAD_TOTAL, "load: allocate descriptors"},
81         { RT_TIMING_LOAD_SETREFS    ,RT_TIMING_LOAD_TOTAL, "load: set classrefs"},
82         { RT_TIMING_LOAD_PARSEFDS   ,RT_TIMING_LOAD_TOTAL, "load: parse field descriptors"},
83         { RT_TIMING_LOAD_PARSEMDS   ,RT_TIMING_LOAD_TOTAL, "load: parse method descriptors"},
84         { RT_TIMING_LOAD_PARSECP    ,RT_TIMING_LOAD_TOTAL, "load: parse descriptors in constant pool"},
85         { RT_TIMING_LOAD_VERIFY     ,RT_TIMING_LOAD_TOTAL, "load: verifier checks"},
86         { RT_TIMING_LOAD_ATTRS      ,RT_TIMING_LOAD_TOTAL, "load: load attributes"},
87         { RT_TIMING_LOAD_TOTAL      ,-1                  , "total load time (from classbuffer)"},
88     { -1                        ,-1                  , "" },
89
90         { RT_TIMING_LOAD_BOOT_LOOKUP,-1                       , "boot: lookup in classcache"},
91         { RT_TIMING_LOAD_BOOT_ARRAY ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: load array classes"},
92         { RT_TIMING_LOAD_BOOT_SUCK  ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: suck class files"},
93         { RT_TIMING_LOAD_BOOT_LOAD  ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: load from class buffer"},
94         { RT_TIMING_LOAD_BOOT_CACHE ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: store in classcache"},
95         { RT_TIMING_LOAD_BOOT_TOTAL ,-1                       , "total bootstrap loader time"},
96     { -1                        ,-1                       , "" },
97
98         { RT_TIMING_LOAD_CL_LOOKUP  ,-1                       , "classloader: lookup in classcache" },
99         { RT_TIMING_LOAD_CL_PREPARE ,-1                       , "classloader: prepare loader call" },
100         { RT_TIMING_LOAD_CL_JAVA    ,-1                       , "classloader: loader Java code" },
101         { RT_TIMING_LOAD_CL_CACHE   ,-1                       , "classloader: store in classcache" },
102     { -1                        ,-1                       , "" },
103
104         { RT_TIMING_NEW_OBJECT      ,-1                       , "builtin_new time" },
105         { RT_TIMING_NEW_ARRAY       ,-1                       , "builtin_newarray time" },
106     { -1                        ,-1                       , "" },
107
108         { RT_TIMING_GC_ALLOC        ,-1                       , "heap allocation time" },
109 #if defined(ENABLE_GC_CACAO)
110         { RT_TIMING_GC_SUSPEND      ,RT_TIMING_GC_TOTAL       , "gc: suspending threads" },
111         { RT_TIMING_GC_ROOTSET1     ,RT_TIMING_GC_TOTAL       , "gc: rootset finding" },
112         { RT_TIMING_GC_MARK         ,RT_TIMING_GC_TOTAL       , "gc: marking phase" },
113         { RT_TIMING_GC_COMPACT      ,RT_TIMING_GC_TOTAL       , "gc: compaction phase" },
114         { RT_TIMING_GC_ROOTSET2     ,RT_TIMING_GC_TOTAL       , "gc: rootset writeback" },
115         { RT_TIMING_GC_TOTAL        ,-1                       , "total garbage collection time" },
116 #endif
117         { -1                        ,-1                       , "" },
118
119 #if defined(ENABLE_REPLACEMENT)
120         { RT_TIMING_REPLACE         ,-1                       , "replacement" },
121         { -1                        ,-1                       , "" },
122 #endif
123
124         { RT_TIMING_1               ,-1                       , "temporary timer 1" },
125         { RT_TIMING_2               ,-1                       , "temporary timer 2" },
126         { RT_TIMING_3               ,-1                       , "temporary timer 3" },
127         { RT_TIMING_4               ,-1                       , "temporary timer 4" },
128         { -1                        ,-1                       , "" },
129
130     { 0                         ,-1                       , NULL }
131 };
132
133 static long long rt_timing_sum[RT_TIMING_N] = { 0 };
134
135 void rt_timing_gettime(struct timespec *ts)
136 {
137         if (clock_gettime(CLOCK_THREAD_CPUTIME_ID,ts) != 0) {
138                 fprintf(stderr,"could not get time by clock_gettime: %s\n",strerror(errno));
139                 abort();
140         }
141 }
142
143 long rt_timing_diff_usec(struct timespec *a,struct timespec *b)
144 {
145         long diff;
146         time_t atime;
147
148         diff = (b->tv_nsec - a->tv_nsec) / 1000;
149         atime = a->tv_sec;
150         while (atime < b->tv_sec) {
151                 atime++;
152                 diff += 1000000;
153         }
154         return diff;
155 }
156
157 void rt_timing_time_diff(struct timespec *a,struct timespec *b,int index)
158 {
159         long diff;
160
161         diff = rt_timing_diff_usec(a,b);
162         rt_timing_sum[index] += diff;
163 }
164
165 void rt_timing_print_time_stats(FILE *file)
166 {
167         struct rt_timing_stat *stats;
168         double total;
169
170         for (stats = rt_timing_stat_defs; stats->name; ++stats) {
171                 if (stats->index < 0) {
172                         fprintf(file,"%s\n",stats->name);
173                         continue;
174                 }
175                 
176                 if (stats->totalindex >= 0) {
177                         total = rt_timing_sum[stats->totalindex];
178                         fprintf(file,"%12lld usec %3.0f%% %s\n",
179                                         rt_timing_sum[stats->index],
180                                         (total != 0.0) ? rt_timing_sum[stats->index] / total * 100.0 : 0.0,
181                                         stats->name);
182                 }
183                 else {
184                         fprintf(file,"%12lld usec      %s\n",
185                                         rt_timing_sum[stats->index],
186                                         stats->name);
187                 }
188         }
189 }
190
191 /*
192  * These are local overrides for various environment variables in Emacs.
193  * Please do not remove this and leave it at the end of the file, where
194  * Emacs will automagically detect them.
195  * ---------------------------------------------------------------------
196  * Local variables:
197  * mode: c
198  * indent-tabs-mode: t
199  * c-basic-offset: 4
200  * tab-width: 4
201  * End:
202  * vim:noexpandtab:sw=4:ts=4:
203  */