* src/threads/threads-common.c (threads_thread_create_internal):
[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 /* profile_init ****************************************************************
57
58    Initializes the profile global lock.
59
60 *******************************************************************************/
61
62 bool profile_init(void)
63 {
64         /* everything's ok */
65
66         return true;
67 }
68
69
70 /* profile_thread **************************************************************
71
72    XXX
73
74 *******************************************************************************/
75
76 static s4 runs = 0;
77 static s4 hits = 0;
78 static s4 misses = 0;
79
80 #if defined(ENABLE_THREADS)
81 static void profile_thread(void)
82 {
83         threadobject *t;
84         s4            nanos;
85         u1           *pc;
86         u1           *pv;
87         methodinfo   *m;
88         codeinfo     *code;
89
90         while (true) {
91                 /* sleep thread for 0.5-1.0 ms */
92
93                 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
94 /*              fprintf(stderr, "%d\n", nanos); */
95
96                 threads_sleep(0, nanos);
97                 runs++;
98
99                 /* iterate over all started threads */
100
101                 t = mainthreadobj;
102
103                 do {
104                         /* is this a Java thread? */
105
106                         if (t->flags & THREAD_FLAG_JAVA) {
107                                 /* send SIGUSR2 to thread to get the current PC */
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 = codegen_get_pv_from_pc_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
128                                            NULL (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
148                                                                    and start recompilation */
149
150                                                                 recompile_queue_method(m);
151                                                         }
152                                                 }
153                                         }
154                                 }
155                         }
156
157                         t = t->next;
158                 } while ((t != NULL) && (t != mainthreadobj));
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         list_t                 *l;
197         list_method_entry      *lme;
198         list_method_entry      *tlme;
199         classinfo              *c;
200         methodinfo             *m;
201         codeinfo               *code;
202         u4                      slot;
203         classcache_name_entry  *nmen;
204         classcache_class_entry *clsen;
205         s4                      i;
206         s4                      j;
207         u4                      frequency;
208         s8                      cycles;
209
210         frequency = 0;
211         cycles    = 0;
212
213         /* create new method list */
214
215         l = list_create(OFFSET(list_method_entry, linkage));
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                                                 /* create new list entry */
247
248                                                 lme = NEW(list_method_entry);
249                                                 lme->m = m;
250
251                                                 /* sort the new entry into the list */
252                                                 
253                                                 if ((tlme = list_first(l)) == NULL) {
254                                                         list_add_first(l, lme);
255                                                 }
256                                                 else {
257                                                         for (; tlme != NULL; tlme = list_next(l, tlme)) {
258                                                                 /* check the frequency */
259
260                                                                 if (code->frequency > tlme->m->code->frequency) {
261                                                                         list_add_before(l, tlme, lme);
262                                                                         break;
263                                                                 }
264                                                         }
265
266                                                         /* if we are at the end of the list, add
267                                                            it as last entry */
268
269                                                         if (tlme == NULL)
270                                                                 list_add_last(l, lme);
271                                                 }
272                                         }
273                                 }
274                         }
275                 }
276         }
277
278         /* print all methods sorted */
279
280         printf(" frequency     ratio         cycles     ratio   method name\n");
281         printf("----------- --------- -------------- --------- -------------\n");
282
283         /* now iterate through the list and print it */
284
285         for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
286                 /* get method of the list element */
287
288                 m = lme->m;
289
290                 code = m->code;
291
292                 printf("%10d   %.5f   %12ld   %.5f   ",
293                            code->frequency,
294                            (double) code->frequency / (double) frequency,
295                            (long) code->cycles,
296                            (double) code->cycles / (double) cycles);
297
298                 method_println(m);
299
300                 /* print basic block frequencies */
301
302                 if (opt_prof_bb) {
303                         for (j = 0; j < code->basicblockcount; j++)
304                                 printf("                                                    L%03d: %10d\n",
305                                            j, code->bbfrequency[j]);
306                 }
307         }
308
309         printf("-----------           -------------- \n");
310         printf("%10d             %12ld\n", frequency, (long) cycles);
311
312         printf("\nruns  : %10d\n", runs);
313         printf("hits  : %10d\n", hits);
314         printf("misses: %10d\n", misses);
315 }
316 #endif /* !defined(NDEBUG) */
317
318
319 /*
320  * These are local overrides for various environment variables in Emacs.
321  * Please do not remove this and leave it at the end of the file, where
322  * Emacs will automagically detect them.
323  * ---------------------------------------------------------------------
324  * Local variables:
325  * mode: c
326  * indent-tabs-mode: t
327  * c-basic-offset: 4
328  * tab-width: 4
329  * End:
330  */