* src/vm/jit/codegen-common.cpp (codegen_emit): New generic version of the
[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, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include "vm/types.h"
32
33 #include "mm/memory.hpp"
34
35 #include "threads/threadlist.hpp"
36 #include "threads/thread.hpp"
37
38 #include "vm/jit/builtin.hpp"
39 #include "vm/class.hpp"
40 #include "vm/classcache.hpp"
41 #include "vm/method.hpp"
42 #include "vm/options.h"
43 #include "vm/string.hpp"
44
45 #include "vm/jit/jit.hpp"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/methodtree.h"
48
49 #include "vm/jit/optimizing/recompiler.hpp"
50
51
52 /* profile_init ****************************************************************
53
54    Initializes the profile global lock.
55
56 *******************************************************************************/
57
58 bool profile_init(void)
59 {
60         /* everything's ok */
61
62         return true;
63 }
64
65
66 /* profile_thread **************************************************************
67
68    XXX
69
70 *******************************************************************************/
71
72 static s4 runs = 0;
73 static s4 hits = 0;
74 static s4 misses = 0;
75
76 #if defined(ENABLE_THREADS)
77 static void profile_thread(void)
78 {
79         threadobject *t;
80         s4            nanos;
81         u1           *pc;
82         u1           *pv;
83         methodinfo   *m;
84         codeinfo     *code;
85
86         while (true) {
87                 /* sleep thread for 0.5-1.0 ms */
88
89                 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
90 /*              fprintf(stderr, "%d\n", nanos); */
91
92                 threads_sleep(0, nanos);
93                 runs++;
94
95                 // Lock the thread lists.
96                 ThreadList_lock();
97
98 #if 0
99                 /* iterate over all started threads */
100
101                 for (t = ThreadList_first(); t != NULL; t = ThreadList_next(t)) {
102                         /* is this a Java thread? */
103
104                         if (!(t->flags & THREAD_FLAG_JAVA))
105                                 continue;
106
107                         /* send SIGUSR2 to thread to get the current PC */
108                         /* XXX write a threads-function for that */
109
110                         pthread_kill(t->tid, SIGUSR2);
111
112                         /* the thread object now contains the current thread PC */
113
114                         pc = t->pc;
115
116                         /* Get the PV for the current PC. */
117
118                         pv = methodtree_find_nocheck(pc);
119
120                         /* get methodinfo pointer from data segment */
121
122                         if (pv == NULL) {
123                                 misses++;
124                         }
125                         else {
126                                 code = *((codeinfo **) (pv + CodeinfoPointer));
127
128                                 /* For asm_vm_call_method the codeinfo pointer is NULL
129                                    (which is also in the method tree). */
130
131                                 if (code != NULL) {
132                                         m = code->m;
133
134                                         /* native methods are never recompiled */
135
136                                         if (!(m->flags & ACC_NATIVE)) {
137                                                 /* increase the method incovation counter */
138
139                                                 code->frequency++;
140                                                 hits++;
141
142                                                 if (code->frequency > 500) {
143                                                         /* clear frequency count before
144                                                            recompilation */
145
146                                                         code->frequency = 0;
147
148                                                         /* add this method to the method list and
149                                                            start recompilation */
150
151                                                         Recompiler_queue_method(m);
152                                                 }
153                                         }
154                                 }
155                         }
156                 }
157 #endif
158
159                 // Unlock the thread lists.
160                 ThreadList_unlock();
161         }
162 }
163 #endif
164
165
166 /* profile_start_thread ********************************************************
167
168    Starts the profile sampling thread.
169
170 *******************************************************************************/
171
172 #if defined(ENABLE_THREADS)
173 bool profile_start_thread(void)
174 {
175         utf *name;
176
177         name = utf_new_char("Profiling Sampler");
178
179         if (!threads_thread_start_internal(name, profile_thread))
180                 return false;
181
182         /* everything's ok */
183
184         return true;
185 }
186 #endif
187
188
189 /* profile_printstats **********************************************************
190
191    Prints profiling statistics gathered during runtime.
192
193 *******************************************************************************/
194
195 #if !defined(NDEBUG)
196 void profile_printstats(void)
197 {
198         classinfo              *c;
199         methodinfo             *m;
200         codeinfo               *code;
201         u4                      slot;
202         classcache_name_entry  *nmen;
203         classcache_class_entry *clsen;
204         s4                      i;
205         s4                      j;
206         u4                      frequency;
207         s8                      cycles;
208
209         frequency = 0;
210         cycles    = 0;
211
212 #if 0
213         /* create new method list */
214         // TODO Use a sorted container.
215         List* l = List_new();
216
217         /* iterate through all classes and methods */
218
219         for (slot = 0; slot < hashtable_classcache.size; slot++) {
220                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
221
222                 for (; nmen; nmen = nmen->hashlink) {
223                         /* iterate over all class entries */
224
225                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
226                                 c = clsen->classobj;
227
228                                 if (c == NULL)
229                                         continue;
230
231                                 /* interate over all class methods */
232
233                                 for (i = 0; i < c->methodscount; i++) {
234                                         m = &(c->methods[i]);
235
236                                         code = m->code;
237
238                                         /* was this method actually called? */
239
240                                         if ((code != NULL) && (code->frequency > 0)) {
241                                                 /* add to overall stats */
242
243                                                 frequency += code->frequency;
244                                                 cycles    += code->cycles;
245
246                                                 /* sort the new entry into the list */
247                                                 
248                                                 if (List_empty(l) == NULL) {
249                                                         List_push_back(l, m);
250                                                 }
251                                                 else {
252                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
253                                                                 /* check the frequency */
254
255                                                                 if (code->frequency > tlme->m->code->frequency) {
256                                                                         list_add_before(l, tlme, lme);
257                                                                         break;
258                                                                 }
259                                                         }
260
261                                                         /* if we are at the end of the list, add
262                                                            it as last entry */
263
264                                                         if (tlme == NULL)
265                                                                 list_add_last(l, lme);
266                                                 }
267                                         }
268                                 }
269                         }
270                 }
271         }
272
273         /* print all methods sorted */
274
275         printf(" frequency     ratio         cycles     ratio   method name\n");
276         printf("----------- --------- -------------- --------- -------------\n");
277
278         /* now iterate through the list and print it */
279
280         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
281                 /* get method of the list element */
282
283                 m = lme->m;
284
285                 code = m->code;
286
287                 printf("%10d   %.5f   %12ld   %.5f   ",
288                            code->frequency,
289                            (double) code->frequency / (double) frequency,
290                            (long) code->cycles,
291                            (double) code->cycles / (double) cycles);
292
293                 method_println(m);
294
295                 /* print basic block frequencies */
296
297                 if (opt_prof_bb) {
298                         for (j = 0; j < code->basicblockcount; j++)
299                                 printf("                                                    L%03d: %10d\n",
300                                            j, code->bbfrequency[j]);
301                 }
302         }
303 #endif
304
305         printf("-----------           -------------- \n");
306         printf("%10d             %12ld\n", frequency, (long) cycles);
307
308         printf("\nruns  : %10d\n", runs);
309         printf("hits  : %10d\n", hits);
310         printf("misses: %10d\n", misses);
311 }
312 #endif /* !defined(NDEBUG) */
313
314
315 /*
316  * These are local overrides for various environment variables in Emacs.
317  * Please do not remove this and leave it at the end of the file, where
318  * Emacs will automagically detect them.
319  * ---------------------------------------------------------------------
320  * Local variables:
321  * mode: c
322  * indent-tabs-mode: t
323  * c-basic-offset: 4
324  * tab-width: 4
325  * End:
326  */