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