* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include "vm/types.h"
34
35 #include "mm/memory.h"
36
37 #include "threads/threads-common.h"
38
39 #include "vm/builtin.h"
40 #include "vm/stringlocal.h"
41
42 #include "vm/jit/jit.h"
43 #include "vm/jit/methodheader.h"
44 #include "vm/jit/optimizing/recompile.h"
45
46 #include "vmcore/class.h"
47 #include "vmcore/classcache.h"
48 #include "vmcore/method.h"
49 #include "vmcore/options.h"
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 threads lists */
96
97                 threads_list_lock();
98
99                 /* iterate over all started threads */
100
101                 for (t = threads_list_first(); t != NULL; t = threads_list_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 = codegen_get_pv_from_pc_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                                                         recompile_queue_method(m);
152                                                 }
153                                         }
154                                 }
155                         }
156                 }
157
158                 /* unlock the threads lists */
159
160                 threads_list_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         list_t                 *l;
199         list_method_entry      *lme;
200         list_method_entry      *tlme;
201         classinfo              *c;
202         methodinfo             *m;
203         codeinfo               *code;
204         u4                      slot;
205         classcache_name_entry  *nmen;
206         classcache_class_entry *clsen;
207         s4                      i;
208         s4                      j;
209         u4                      frequency;
210         s8                      cycles;
211
212         frequency = 0;
213         cycles    = 0;
214
215         /* create new method list */
216
217         l = list_create(OFFSET(list_method_entry, linkage));
218
219         /* iterate through all classes and methods */
220
221         for (slot = 0; slot < hashtable_classcache.size; slot++) {
222                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
223
224                 for (; nmen; nmen = nmen->hashlink) {
225                         /* iterate over all class entries */
226
227                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
228                                 c = clsen->classobj;
229
230                                 if (c == NULL)
231                                         continue;
232
233                                 /* interate over all class methods */
234
235                                 for (i = 0; i < c->methodscount; i++) {
236                                         m = &(c->methods[i]);
237
238                                         code = m->code;
239
240                                         /* was this method actually called? */
241
242                                         if ((code != NULL) && (code->frequency > 0)) {
243                                                 /* add to overall stats */
244
245                                                 frequency += code->frequency;
246                                                 cycles    += code->cycles;
247
248                                                 /* create new list entry */
249
250                                                 lme = NEW(list_method_entry);
251                                                 lme->m = m;
252
253                                                 /* sort the new entry into the list */
254                                                 
255                                                 if ((tlme = list_first(l)) == NULL) {
256                                                         list_add_first(l, lme);
257                                                 }
258                                                 else {
259                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
260                                                                 /* check the frequency */
261
262                                                                 if (code->frequency > tlme->m->code->frequency) {
263                                                                         list_add_before(l, tlme, lme);
264                                                                         break;
265                                                                 }
266                                                         }
267
268                                                         /* if we are at the end of the list, add
269                                                            it as last entry */
270
271                                                         if (tlme == NULL)
272                                                                 list_add_last(l, lme);
273                                                 }
274                                         }
275                                 }
276                         }
277                 }
278         }
279
280         /* print all methods sorted */
281
282         printf(" frequency     ratio         cycles     ratio   method name\n");
283         printf("----------- --------- -------------- --------- -------------\n");
284
285         /* now iterate through the list and print it */
286
287         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
288                 /* get method of the list element */
289
290                 m = lme->m;
291
292                 code = m->code;
293
294                 printf("%10d   %.5f   %12ld   %.5f   ",
295                            code->frequency,
296                            (double) code->frequency / (double) frequency,
297                            (long) code->cycles,
298                            (double) code->cycles / (double) cycles);
299
300                 method_println(m);
301
302                 /* print basic block frequencies */
303
304                 if (opt_prof_bb) {
305                         for (j = 0; j < code->basicblockcount; j++)
306                                 printf("                                                    L%03d: %10d\n",
307                                            j, code->bbfrequency[j]);
308                 }
309         }
310
311         printf("-----------           -------------- \n");
312         printf("%10d             %12ld\n", frequency, (long) cycles);
313
314         printf("\nruns  : %10d\n", runs);
315         printf("hits  : %10d\n", hits);
316         printf("misses: %10d\n", misses);
317 }
318 #endif /* !defined(NDEBUG) */
319
320
321 /*
322  * These are local overrides for various environment variables in Emacs.
323  * Please do not remove this and leave it at the end of the file, where
324  * Emacs will automagically detect them.
325  * ---------------------------------------------------------------------
326  * Local variables:
327  * mode: c
328  * indent-tabs-mode: t
329  * c-basic-offset: 4
330  * tab-width: 4
331  * End:
332  */