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