* Removed all Id tags.
[cacao.git] / src / vmcore / rt-timing.c
1 /* src/vmcore/rt-timing.c - POSIX real-time timing utilities
2
3    Copyright (C) 1996-2005, 2006, 2007 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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <time.h>
34
35 #include "vm/types.h"
36
37 #include "mm/memory.h"
38
39 #include "vm/global.h"
40
41 #include "vmcore/rt-timing.h"
42
43
44 struct rt_timing_stat {
45         int index;
46         int totalindex;
47         const char *name;
48 };
49
50 static struct rt_timing_stat rt_timing_stat_defs[] = {
51     { RT_TIMING_JIT_CHECKS      ,RT_TIMING_JIT_TOTAL , "checks at beginning" },
52     { RT_TIMING_JIT_PARSE       ,RT_TIMING_JIT_TOTAL , "parse" },
53     { RT_TIMING_JIT_STACK       ,RT_TIMING_JIT_TOTAL , "analyse_stack" },
54     { RT_TIMING_JIT_TYPECHECK   ,RT_TIMING_JIT_TOTAL , "typecheck" },
55     { RT_TIMING_JIT_LOOP        ,RT_TIMING_JIT_TOTAL , "loop" },
56     { RT_TIMING_JIT_IFCONV      ,RT_TIMING_JIT_TOTAL , "if conversion" },
57     { RT_TIMING_JIT_ALLOC       ,RT_TIMING_JIT_TOTAL , "register allocation" },
58     { RT_TIMING_JIT_RPLPOINTS   ,RT_TIMING_JIT_TOTAL , "replacement point generation" },
59     { RT_TIMING_JIT_CODEGEN     ,RT_TIMING_JIT_TOTAL , "codegen" },
60     { RT_TIMING_JIT_TOTAL       ,-1                  , "total compile time" },
61     { -1                        ,-1                  , "" },
62
63     { RT_TIMING_LINK_RESOLVE    ,RT_TIMING_LINK_TOTAL, "link: resolve superclass/superinterfaces"},
64     { RT_TIMING_LINK_C_VFTBL    ,RT_TIMING_LINK_TOTAL, "link: compute vftbl length"},
65     { RT_TIMING_LINK_ABSTRACT   ,RT_TIMING_LINK_TOTAL, "link: handle abstract methods"},
66     { RT_TIMING_LINK_C_IFTBL    ,RT_TIMING_LINK_TOTAL, "link: compute interface table"},
67     { RT_TIMING_LINK_F_VFTBL    ,RT_TIMING_LINK_TOTAL, "link: fill vftbl"},
68     { RT_TIMING_LINK_OFFSETS    ,RT_TIMING_LINK_TOTAL, "link: set offsets"},
69     { RT_TIMING_LINK_F_IFTBL    ,RT_TIMING_LINK_TOTAL, "link: fill interface table"},
70     { RT_TIMING_LINK_FINALIZER  ,RT_TIMING_LINK_TOTAL, "link: set finalizer"},
71     { RT_TIMING_LINK_EXCEPTS    ,RT_TIMING_LINK_TOTAL, "link: resolve exception classes"},
72     { RT_TIMING_LINK_SUBCLASS   ,RT_TIMING_LINK_TOTAL, "link: re-calculate subclass indices"},
73     { RT_TIMING_LINK_TOTAL      ,-1                  , "total link time" },
74     { -1                        ,-1                  , "" },
75         
76         { RT_TIMING_LOAD_CHECKS     ,RT_TIMING_LOAD_TOTAL, "load: initial checks"},
77         { RT_TIMING_LOAD_NDPOOL     ,RT_TIMING_LOAD_TOTAL, "load: new descriptor pool"},
78         { RT_TIMING_LOAD_CPOOL      ,RT_TIMING_LOAD_TOTAL, "load: load constant pool"},
79         { RT_TIMING_LOAD_SETUP      ,RT_TIMING_LOAD_TOTAL, "load: class setup"},
80         { RT_TIMING_LOAD_FIELDS     ,RT_TIMING_LOAD_TOTAL, "load: load fields"},
81         { RT_TIMING_LOAD_METHODS    ,RT_TIMING_LOAD_TOTAL, "load: load methods"},
82         { RT_TIMING_LOAD_CLASSREFS  ,RT_TIMING_LOAD_TOTAL, "load: create classrefs"},
83         { RT_TIMING_LOAD_DESCS      ,RT_TIMING_LOAD_TOTAL, "load: allocate descriptors"},
84         { RT_TIMING_LOAD_SETREFS    ,RT_TIMING_LOAD_TOTAL, "load: set classrefs"},
85         { RT_TIMING_LOAD_PARSEFDS   ,RT_TIMING_LOAD_TOTAL, "load: parse field descriptors"},
86         { RT_TIMING_LOAD_PARSEMDS   ,RT_TIMING_LOAD_TOTAL, "load: parse method descriptors"},
87         { RT_TIMING_LOAD_PARSECP    ,RT_TIMING_LOAD_TOTAL, "load: parse descriptors in constant pool"},
88         { RT_TIMING_LOAD_VERIFY     ,RT_TIMING_LOAD_TOTAL, "load: verifier checks"},
89         { RT_TIMING_LOAD_ATTRS      ,RT_TIMING_LOAD_TOTAL, "load: load attributes"},
90         { RT_TIMING_LOAD_TOTAL      ,-1                  , "total load time (from classbuffer)"},
91     { -1                        ,-1                  , "" },
92
93         { RT_TIMING_LOAD_BOOT_LOOKUP,-1                       , "boot: lookup in classcache"},
94         { RT_TIMING_LOAD_BOOT_ARRAY ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: load array classes"},
95         { RT_TIMING_LOAD_BOOT_SUCK  ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: suck class files"},
96         { RT_TIMING_LOAD_BOOT_LOAD  ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: load from class buffer"},
97         { RT_TIMING_LOAD_BOOT_CACHE ,RT_TIMING_LOAD_BOOT_TOTAL, "boot: store in classcache"},
98         { RT_TIMING_LOAD_BOOT_TOTAL ,-1                       , "total bootstrap loader time"},
99     { -1                        ,-1                       , "" },
100
101         { RT_TIMING_LOAD_CL_LOOKUP  ,-1                       , "classloader: lookup in classcache" },
102         { RT_TIMING_LOAD_CL_PREPARE ,-1                       , "classloader: prepare loader call" },
103         { RT_TIMING_LOAD_CL_JAVA    ,-1                       , "classloader: loader Java code" },
104         { RT_TIMING_LOAD_CL_CACHE   ,-1                       , "classloader: store in classcache" },
105     { -1                        ,-1                       , "" },
106
107         { RT_TIMING_NEW_OBJECT      ,-1                       , "builtin_new time" },
108         { RT_TIMING_NEW_ARRAY       ,-1                       , "builtin_newarray time" },
109     { -1                        ,-1                       , "" },
110
111     { 0                         ,-1                       , NULL }
112 };
113
114 static long long rt_timing_sum[RT_TIMING_N] = { 0 };
115
116 void rt_timing_gettime(struct timespec *ts)
117 {
118         if (clock_gettime(CLOCK_THREAD_CPUTIME_ID,ts) != 0) {
119                 fprintf(stderr,"could not get time by clock_gettime: %s\n",strerror(errno));
120                 abort();
121         }
122 }
123
124 long rt_timing_diff_usec(struct timespec *a,struct timespec *b)
125 {
126         long diff;
127         time_t atime;
128
129         diff = (b->tv_nsec - a->tv_nsec) / 1000;
130         atime = a->tv_sec;
131         while (atime < b->tv_sec) {
132                 atime++;
133                 diff += 1000000;
134         }
135         return diff;
136 }
137
138 void rt_timing_time_diff(struct timespec *a,struct timespec *b,int index)
139 {
140         long diff;
141
142         diff = rt_timing_diff_usec(a,b);
143         rt_timing_sum[index] += diff;
144 }
145
146 void rt_timing_print_time_stats(FILE *file)
147 {
148         struct rt_timing_stat *stats;
149         double total;
150
151         for (stats = rt_timing_stat_defs; stats->name; ++stats) {
152                 if (stats->index < 0) {
153                         fprintf(file,"%s\n",stats->name);
154                         continue;
155                 }
156                 
157                 if (stats->totalindex >= 0) {
158                         total = rt_timing_sum[stats->totalindex];
159                         fprintf(file,"%12lld usec %3.0f%% %s\n",
160                                         rt_timing_sum[stats->index],
161                                         (total != 0.0) ? rt_timing_sum[stats->index] / total * 100.0 : 0.0,
162                                         stats->name);
163                 }
164                 else {
165                         fprintf(file,"%12lld usec      %s\n",
166                                         rt_timing_sum[stats->index],
167                                         stats->name);
168                 }
169         }
170 }
171
172 /*
173  * These are local overrides for various environment variables in Emacs.
174  * Please do not remove this and leave it at the end of the file, where
175  * Emacs will automagically detect them.
176  * ---------------------------------------------------------------------
177  * Local variables:
178  * mode: c
179  * indent-tabs-mode: t
180  * c-basic-offset: 4
181  * tab-width: 4
182  * End:
183  * vim:noexpandtab:sw=4:ts=4:
184  */