* Updated to jitcache-arm-x86 branch d4f6023b26c5+d1b5b1c106ac
[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.h"
34
35 #include "threads/threadlist.hpp"
36 #include "threads/thread.hpp"
37
38 #include "vm/jit/builtin.hpp"
39 #include "vm/class.h"
40 #include "vm/classcache.h"
41 #include "vm/method.h"
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                 /* iterate over all started threads */
99
100                 for (t = ThreadList_first(); t != NULL; t = ThreadList_next(t)) {
101                         /* is this a Java thread? */
102
103                         if (!(t->flags & THREAD_FLAG_JAVA))
104                                 continue;
105
106                         /* send SIGUSR2 to thread to get the current PC */
107                         /* XXX write a threads-function for that */
108
109                         pthread_kill(t->tid, SIGUSR2);
110
111                         /* the thread object now contains the current thread PC */
112
113                         pc = t->pc;
114
115                         /* Get the PV for the current PC. */
116
117                         pv = methodtree_find_nocheck(pc);
118
119                         /* get methodinfo pointer from data segment */
120
121                         if (pv == NULL) {
122                                 misses++;
123                         }
124                         else {
125                                 code = *((codeinfo **) (pv + CodeinfoPointer));
126
127                                 /* For asm_vm_call_method the codeinfo pointer is NULL
128                                    (which is also in the method tree). */
129
130                                 if (code != NULL) {
131                                         m = code->m;
132
133                                         /* native methods are never recompiled */
134
135                                         if (!(m->flags & ACC_NATIVE)) {
136                                                 /* increase the method incovation counter */
137
138                                                 code->frequency++;
139                                                 hits++;
140
141                                                 if (code->frequency > 500) {
142                                                         /* clear frequency count before
143                                                            recompilation */
144
145                                                         code->frequency = 0;
146
147                                                         /* add this method to the method list and
148                                                            start recompilation */
149
150                                                         Recompiler_queue_method(m);
151                                                 }
152                                         }
153                                 }
154                         }
155                 }
156
157                 // Unlock the thread lists.
158                 ThreadList_unlock();
159         }
160 }
161 #endif
162
163
164 /* profile_start_thread ********************************************************
165
166    Starts the profile sampling thread.
167
168 *******************************************************************************/
169
170 #if defined(ENABLE_THREADS)
171 bool profile_start_thread(void)
172 {
173         utf *name;
174
175         name = utf_new_char("Profiling Sampler");
176
177         if (!threads_thread_start_internal(name, profile_thread))
178                 return false;
179
180         /* everything's ok */
181
182         return true;
183 }
184 #endif
185
186
187 /* profile_printstats **********************************************************
188
189    Prints profiling statistics gathered during runtime.
190
191 *******************************************************************************/
192
193 #if !defined(NDEBUG)
194 void profile_printstats(void)
195 {
196         classinfo              *c;
197         methodinfo             *m;
198         codeinfo               *code;
199         u4                      slot;
200         classcache_name_entry  *nmen;
201         classcache_class_entry *clsen;
202         s4                      i;
203         s4                      j;
204         u4                      frequency;
205         s8                      cycles;
206
207         frequency = 0;
208         cycles    = 0;
209
210         /* create new method list */
211         // TODO Use a sorted container.
212         List* l = List_new();
213
214         /* iterate through all classes and methods */
215
216         for (slot = 0; slot < hashtable_classcache.size; slot++) {
217                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
218
219                 for (; nmen; nmen = nmen->hashlink) {
220                         /* iterate over all class entries */
221
222                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
223                                 c = clsen->classobj;
224
225                                 if (c == NULL)
226                                         continue;
227
228                                 /* interate over all class methods */
229
230                                 for (i = 0; i < c->methodscount; i++) {
231                                         m = &(c->methods[i]);
232
233                                         code = m->code;
234
235                                         /* was this method actually called? */
236
237                                         if ((code != NULL) && (code->frequency > 0)) {
238                                                 /* add to overall stats */
239
240                                                 frequency += code->frequency;
241                                                 cycles    += code->cycles;
242
243                                                 /* sort the new entry into the list */
244                                                 
245                                                 if (List_empty(l) == NULL) {
246                                                         List_push_back(l, m);
247                                                 }
248                                                 else {
249                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
250                                                                 /* check the frequency */
251
252                                                                 if (code->frequency > tlme->m->code->frequency) {
253                                                                         list_add_before(l, tlme, lme);
254                                                                         break;
255                                                                 }
256                                                         }
257
258                                                         /* if we are at the end of the list, add
259                                                            it as last entry */
260
261                                                         if (tlme == NULL)
262                                                                 list_add_last(l, lme);
263                                                 }
264                                         }
265                                 }
266                         }
267                 }
268         }
269
270         /* print all methods sorted */
271
272         printf(" frequency     ratio         cycles     ratio   method name\n");
273         printf("----------- --------- -------------- --------- -------------\n");
274
275         /* now iterate through the list and print it */
276
277         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
278                 /* get method of the list element */
279
280                 m = lme->m;
281
282                 code = m->code;
283
284                 printf("%10d   %.5f   %12ld   %.5f   ",
285                            code->frequency,
286                            (double) code->frequency / (double) frequency,
287                            (long) code->cycles,
288                            (double) code->cycles / (double) cycles);
289
290                 method_println(m);
291
292                 /* print basic block frequencies */
293
294                 if (opt_prof_bb) {
295                         for (j = 0; j < code->basicblockcount; j++)
296                                 printf("                                                    L%03d: %10d\n",
297                                            j, code->bbfrequency[j]);
298                 }
299         }
300
301         printf("-----------           -------------- \n");
302         printf("%10d             %12ld\n", frequency, (long) cycles);
303
304         printf("\nruns  : %10d\n", runs);
305         printf("hits  : %10d\n", hits);
306         printf("misses: %10d\n", misses);
307 }
308 #endif /* !defined(NDEBUG) */
309
310
311 /*
312  * These are local overrides for various environment variables in Emacs.
313  * Please do not remove this and leave it at the end of the file, where
314  * Emacs will automagically detect them.
315  * ---------------------------------------------------------------------
316  * Local variables:
317  * mode: c
318  * indent-tabs-mode: t
319  * c-basic-offset: 4
320  * tab-width: 4
321  * End:
322  */