* src/vm/jit/alpha/emit.c [ENABLE_THREADS] (threads/native/lock.h):
[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 #include <stdlib.h>
40
41 #include "vm/types.h"
42
43 #include "mm/memory.h"
44 #include "native/jni.h"
45 #include "native/include/java_lang_Thread.h"
46 #include "native/include/java_lang_VMThread.h"
47
48 #if defined(ENABLE_THREADS)
49 # include "threads/native/threads.h"
50 #endif
51
52 #include "vm/builtin.h"
53 #include "vm/class.h"
54 #include "vm/classcache.h"
55 #include "vm/method.h"
56 #include "vm/options.h"
57 #include "vm/stringlocal.h"
58 #include "vm/jit/jit.h"
59 #include "vm/jit/methodheader.h"
60 #include "vm/jit/recompile.h"
61
62
63 /* global variables ***********************************************************/
64
65 #if defined(ENABLE_THREADS)
66 static java_lang_VMThread *profile_vmthread;
67 #endif
68
69
70 /* profile_init ****************************************************************
71
72    Initializes the profile global lock.
73
74 *******************************************************************************/
75
76 bool profile_init(void)
77 {
78         /* everything's ok */
79
80         return true;
81 }
82
83
84 /* profile_thread **************************************************************
85
86    XXX
87
88 *******************************************************************************/
89
90 static s4 runs = 0;
91 static s4 hits = 0;
92 static s4 misses = 0;
93
94 #if defined(ENABLE_THREADS)
95 static void profile_thread(void)
96 {
97         threadobject *t;
98         s4            nanos;
99         u1           *pc;
100         u1           *pv;
101         methodinfo   *m;
102         codeinfo     *code;
103
104         while (true) {
105                 /* sleep thread for 0.5-1.0 ms */
106
107                 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
108 /*              fprintf(stderr, "%d\n", nanos); */
109
110                 threads_sleep(0, nanos);
111                 runs++;
112
113                 /* iterate over all started threads */
114
115                 t = mainthreadobj;
116
117                 do {
118                         /* is this a Java thread? */
119
120                         if (t->flags & THREAD_FLAG_JAVA) {
121                                 /* send SIGUSR2 to thread to get the current PC */
122
123                                 pthread_kill(t->tid, SIGUSR2);
124
125                                 /* the thread object now contains the current thread PC */
126
127                                 pc = t->pc;
128
129                                 /* get the PV for the current PC */
130
131                                 pv = codegen_get_pv_from_pc_nocheck(pc);
132
133                                 /* get methodinfo pointer from data segment */
134
135                                 if (pv == NULL) {
136                                         misses++;
137                                 }
138                                 else {
139                                         code = *((codeinfo **) (pv + CodeinfoPointer));
140
141                                         /* For asm_vm_call_method the codeinfo pointer is
142                                            NULL (which is also in the method tree). */
143
144                                         if (code != NULL) {
145                                                 m = code->m;
146
147                                                 /* native methods are never recompiled */
148
149                                                 if (!(m->flags & ACC_NATIVE)) {
150                                                         /* increase the method incovation counter */
151
152                                                         code->frequency++;
153                                                         hits++;
154
155                                                         if (code->frequency > 500) {
156                                                                 /* clear frequency count before
157                                                                    recompilation */
158
159                                                                 code->frequency = 0;
160
161                                                                 /* add this method to the method list
162                                                                    and start recompilation */
163
164                                                                 recompile_queue_method(m);
165                                                         }
166                                                 }
167                                         }
168                                 }
169                         }
170
171                         t = t->next;
172                 } while ((t != NULL) && (t != mainthreadobj));
173         }
174 }
175 #endif
176
177
178 /* profile_start_thread ********************************************************
179
180    Starts the profile sampling thread.
181
182 *******************************************************************************/
183
184 #if defined(ENABLE_THREADS)
185 bool profile_start_thread(void)
186 {
187         java_lang_Thread *t;
188
189         /* create the profile object */
190
191         profile_vmthread =
192                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
193
194         if (profile_vmthread == NULL)
195                 return false;
196
197         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
198
199         t->vmThread = profile_vmthread;
200         t->name     = javastring_new_from_ascii("Profiling Sampler");
201         t->daemon   = true;
202         t->priority = 5;
203
204         profile_vmthread->thread = t;
205
206         /* actually start the profile sampling thread */
207
208         threads_start_thread(t, profile_thread);
209
210         /* everything's ok */
211
212         return true;
213 }
214 #endif
215
216
217 /* profile_printstats **********************************************************
218
219    Prints profiling statistics gathered during runtime.
220
221 *******************************************************************************/
222
223 #if !defined(NDEBUG)
224 void profile_printstats(void)
225 {
226         list                   *l;
227         list_method_entry      *lme;
228         list_method_entry      *tlme;
229         classinfo              *c;
230         methodinfo             *m;
231         codeinfo               *code;
232         u4                      slot;
233         classcache_name_entry  *nmen;
234         classcache_class_entry *clsen;
235         s4                      i;
236         s4                      j;
237         u4                      frequency;
238         s8                      cycles;
239
240         frequency = 0;
241         cycles    = 0;
242
243         /* create new method list */
244
245         l = list_create(OFFSET(list_method_entry, linkage));
246
247         /* iterate through all classes and methods */
248
249         for (slot = 0; slot < hashtable_classcache.size; slot++) {
250                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
251
252                 for (; nmen; nmen = nmen->hashlink) {
253                         /* iterate over all class entries */
254
255                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
256                                 c = clsen->classobj;
257
258                                 if (c == NULL)
259                                         continue;
260
261                                 /* interate over all class methods */
262
263                                 for (i = 0; i < c->methodscount; i++) {
264                                         m = &(c->methods[i]);
265
266                                         code = m->code;
267
268                                         /* was this method actually called? */
269
270                                         if ((code != NULL) && (code->frequency > 0)) {
271                                                 /* add to overall stats */
272
273                                                 frequency += code->frequency;
274                                                 cycles    += code->cycles;
275
276                                                 /* create new list entry */
277
278                                                 lme = NEW(list_method_entry);
279                                                 lme->m = m;
280
281                                                 /* sort the new entry into the list */
282                                                 
283                                                 if ((tlme = list_first(l)) == NULL) {
284                                                         list_add_first(l, lme);
285                                                 }
286                                                 else {
287                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
288                                                                 /* check the frequency */
289
290                                                                 if (code->frequency > tlme->m->code->frequency) {
291                                                                         list_add_before(l, tlme, lme);
292                                                                         break;
293                                                                 }
294                                                         }
295
296                                                         /* if we are at the end of the list, add
297                                                            it as last entry */
298
299                                                         if (tlme == NULL)
300                                                                 list_add_last(l, lme);
301                                                 }
302                                         }
303                                 }
304                         }
305                 }
306         }
307
308         /* print all methods sorted */
309
310         printf(" frequency     ratio         cycles     ratio   method name\n");
311         printf("----------- --------- -------------- --------- -------------\n");
312
313         /* now iterate through the list and print it */
314
315         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
316                 /* get method of the list element */
317
318                 m = lme->m;
319
320                 code = m->code;
321
322                 printf("%10d   %.5f   %12ld   %.5f   ",
323                            code->frequency,
324                            (double) code->frequency / (double) frequency,
325                            (long) code->cycles,
326                            (double) code->cycles / (double) cycles);
327
328                 method_println(m);
329
330                 /* print basic block frequencies */
331
332                 if (opt_prof_bb) {
333                         for (j = 0; j < code->basicblockcount; j++)
334                                 printf("                                                    L%03d: %10d\n",
335                                            j, code->bbfrequency[j]);
336                 }
337         }
338
339         printf("-----------           -------------- \n");
340         printf("%10d             %12ld\n", frequency, (long) cycles);
341
342         printf("\nruns  : %10d\n", runs);
343         printf("hits  : %10d\n", hits);
344         printf("misses: %10d\n", misses);
345 }
346 #endif /* !defined(NDEBUG) */
347
348
349 /*
350  * These are local overrides for various environment variables in Emacs.
351  * Please do not remove this and leave it at the end of the file, where
352  * Emacs will automagically detect them.
353  * ---------------------------------------------------------------------
354  * Local variables:
355  * mode: c
356  * indent-tabs-mode: t
357  * c-basic-offset: 4
358  * tab-width: 4
359  * End:
360  */