* src/vm/string.c, src/vm/stringlocal.h (java_string_new_char):
[cacao.git] / src / vm / jit / profile / profile.c
1 /* src/vm/jit/profile.c - runtime profiling
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, 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: cacao.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39
40 #include "vm/types.h"
41
42 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/include/java_lang_Thread.h"
45 #include "native/include/java_lang_VMThread.h"
46
47 #if defined(USE_THREADS)
48 # if defined(NATIVE_THREADS)
49 #  include "threads/native/threads.h"
50 # else
51 #  include "threads/green/threads.h"
52 # endif
53 #endif
54
55 #include "vm/builtin.h"
56 #include "vm/class.h"
57 #include "vm/classcache.h"
58 #include "vm/method.h"
59 #include "vm/options.h"
60 #include "vm/stringlocal.h"
61
62
63 /* list_method_entry **********************************************************/
64
65 typedef struct list_method_entry list_method_entry;
66
67 struct list_method_entry {
68         methodinfo *m;
69         listnode    linkage;
70 };
71
72
73 /* global variables ***********************************************************/
74
75 #if defined(USE_THREADS)
76 static java_lang_VMThread *profile_vmthread;
77 #endif
78
79
80 /* profile_init ****************************************************************
81
82    Initializes the profile global lock.
83
84 *******************************************************************************/
85
86 bool profile_init(void)
87 {
88         /* everything's ok */
89
90         return true;
91 }
92
93
94 /* profile_thread **************************************************************
95
96    XXX
97
98 *******************************************************************************/
99
100 #if defined(USE_THREADS)
101 static void profile_thread(void)
102 {
103 /*      s4 i = 0; */
104
105         while (true) {
106                 /* sleep thread for 100 nanos */
107
108                 thread_sleep(0, 100);
109
110 #if 0
111                 /* get the lock on the finalizer lock object, so we can call wait */
112
113                 builtin_monitorenter(lock_profile_thread);
114
115                 /* wait 1 ms */
116         
117                 wait_cond_for_object(lock_profile_thread, 0, 100);
118
119                 /* leave the lock */
120
121                 builtin_monitorexit(lock_profile_thread);
122 #endif
123
124 /*              if ((i++ % 1000) == 0) */
125 /*                      printf("profile_thread: %d\n", i); */
126         }
127 }
128 #endif
129
130
131 /* profile_start_thread ********************************************************
132
133    Starts the profile sampling thread.
134
135 *******************************************************************************/
136
137 #if defined(USE_THREADS)
138 bool profile_start_thread(void)
139 {
140         java_lang_Thread *t;
141
142         /* create the profile object */
143
144         profile_vmthread =
145                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
146
147         if (!profile_vmthread)
148                 return false;
149
150         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
151
152         t->vmThread = profile_vmthread;
153         t->name     = javastring_new_from_ascii("Profiling Sampler");
154         t->daemon   = true;
155         t->priority = 5;
156
157         profile_vmthread->thread = t;
158
159         /* actually start the profile sampling thread */
160
161         threads_start_thread(t, profile_thread);
162
163         /* everything's ok */
164
165         return true;
166 }
167 #endif
168
169
170 /* profile_printstats **********************************************************
171
172    Prints profiling statistics gathered during runtime.
173
174 *******************************************************************************/
175
176 #if !defined(NDEBUG)
177 void profile_printstats(void)
178 {
179         list                   *l;
180         list_method_entry      *lme;
181         list_method_entry      *tlme;
182         classinfo              *c;
183         methodinfo             *m;
184         u4                      slot;
185         classcache_name_entry  *nmen;
186         classcache_class_entry *clsen;
187         s4                      i;
188         s4                      j;
189         u4                      frequency;
190         s8                      cycles;
191
192         frequency = 0;
193         cycles    = 0;
194
195         /* create new method list */
196
197         l = NEW(list);
198
199         list_init(l, OFFSET(list_method_entry, linkage));
200
201         /* iterate through all classes and methods */
202
203         for (slot = 0; slot < hashtable_classcache.size; slot++) {
204                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
205
206                 for (; nmen; nmen = nmen->hashlink) {
207                         /* iterate over all class entries */
208
209                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
210                                 c = clsen->classobj;
211
212                                 if (!c)
213                                         continue;
214
215                                 /* compile all class methods */
216
217                                 for (i = 0; i < c->methodscount; i++) {
218                                         m = &(c->methods[i]);
219
220                                         /* was this method actually called? */
221
222                                         if (m->frequency > 0) {
223                                                 /* add to overall stats */
224
225                                                 frequency += m->frequency;
226                                                 cycles    += m->cycles;
227
228                                                 /* create new list entry */
229
230                                                 lme = NEW(list_method_entry);
231                                                 lme->m = m;
232
233                                                 /* sort the new entry into the list */
234                                                 
235                                                 if ((tlme = list_first(l)) == NULL) {
236                                                         list_addfirst(l, lme);
237
238                                                 } else {
239                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
240                                                                 /* check the frequency */
241
242                                                                 if (m->frequency > tlme->m->frequency) {
243                                                                         list_add_before(l, tlme, lme);
244                                                                         break;
245                                                                 }
246                                                         }
247
248                                                         /* if we are at the end of the list, add
249                                                            it at last */
250
251                                                         if (tlme == NULL)
252                                                                 list_addlast(l, lme);
253                                                 }
254                                         }
255                                 }
256                         }
257                 }
258         }
259
260         /* print all methods sorted */
261
262         printf(" frequency     ratio         cycles     ratio   method name\n");
263         printf("----------- --------- -------------- --------- -------------\n");
264
265         /* now iterate through the list and print it */
266
267         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
268                 /* get method of the list element */
269
270                 m = lme->m;
271
272                 printf("%10d   %.5f   %12ld   %.5f   ",
273                            m->frequency,
274                            (double) m->frequency / (double) frequency,
275                            (long)m->cycles,
276                            (double) m->cycles / (double) cycles);
277
278                 method_println(m);
279
280                 /* print basic block frequencies */
281
282                 if (opt_prof_bb) {
283                         for (j = 0; j < m->basicblockcount; j++)
284                                 printf("                                                    L%03d: %10d\n",
285                                            j, m->bbfrequency[j]);
286                 }
287         }
288
289         printf("-----------           -------------- \n");
290         printf("%10d             %12ld\n", frequency, (long)cycles);
291 }
292 #endif /* !defined(NDEBUG) */
293
294
295 /*
296  * These are local overrides for various environment variables in Emacs.
297  * Please do not remove this and leave it at the end of the file, where
298  * Emacs will automagically detect them.
299  * ---------------------------------------------------------------------
300  * Local variables:
301  * mode: c
302  * indent-tabs-mode: t
303  * c-basic-offset: 4
304  * tab-width: 4
305  * End:
306  */