1 /* src/vm/jit/optimizing/profile.c - runtime profiling
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
33 #include "mm/memory.h"
35 #include "threads/threadlist.h"
36 #include "threads/thread.hpp"
38 #include "vm/builtin.h"
40 #include "vm/classcache.h"
41 #include "vm/method.h"
42 #include "vm/options.h"
43 #include "vm/string.hpp"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/methodtree.h"
49 #include "vm/jit/optimizing/recompile.h"
52 /* profile_init ****************************************************************
54 Initializes the profile global lock.
56 *******************************************************************************/
58 bool profile_init(void)
66 /* profile_thread **************************************************************
70 *******************************************************************************/
76 #if defined(ENABLE_THREADS)
77 static void profile_thread(void)
87 /* sleep thread for 0.5-1.0 ms */
89 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
90 /* fprintf(stderr, "%d\n", nanos); */
92 threads_sleep(0, nanos);
95 /* lock the threads lists */
99 /* iterate over all started threads */
101 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
102 /* is this a Java thread? */
104 if (!(t->flags & THREAD_FLAG_JAVA))
107 /* send SIGUSR2 to thread to get the current PC */
108 /* XXX write a threads-function for that */
110 pthread_kill(t->tid, SIGUSR2);
112 /* the thread object now contains the current thread PC */
116 /* Get the PV for the current PC. */
118 pv = methodtree_find_nocheck(pc);
120 /* get methodinfo pointer from data segment */
126 code = *((codeinfo **) (pv + CodeinfoPointer));
128 /* For asm_vm_call_method the codeinfo pointer is NULL
129 (which is also in the method tree). */
134 /* native methods are never recompiled */
136 if (!(m->flags & ACC_NATIVE)) {
137 /* increase the method incovation counter */
142 if (code->frequency > 500) {
143 /* clear frequency count before
148 /* add this method to the method list and
149 start recompilation */
151 recompile_queue_method(m);
158 /* unlock the threads lists */
166 /* profile_start_thread ********************************************************
168 Starts the profile sampling thread.
170 *******************************************************************************/
172 #if defined(ENABLE_THREADS)
173 bool profile_start_thread(void)
177 name = utf_new_char("Profiling Sampler");
179 if (!threads_thread_start_internal(name, profile_thread))
182 /* everything's ok */
189 /* profile_printstats **********************************************************
191 Prints profiling statistics gathered during runtime.
193 *******************************************************************************/
196 void profile_printstats(void)
199 list_method_entry *lme;
200 list_method_entry *tlme;
205 classcache_name_entry *nmen;
206 classcache_class_entry *clsen;
215 /* create new method list */
217 l = list_create(OFFSET(list_method_entry, linkage));
219 /* iterate through all classes and methods */
221 for (slot = 0; slot < hashtable_classcache.size; slot++) {
222 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
224 for (; nmen; nmen = nmen->hashlink) {
225 /* iterate over all class entries */
227 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
233 /* interate over all class methods */
235 for (i = 0; i < c->methodscount; i++) {
236 m = &(c->methods[i]);
240 /* was this method actually called? */
242 if ((code != NULL) && (code->frequency > 0)) {
243 /* add to overall stats */
245 frequency += code->frequency;
246 cycles += code->cycles;
248 /* create new list entry */
250 lme = NEW(list_method_entry);
253 /* sort the new entry into the list */
255 if ((tlme = list_first(l)) == NULL) {
256 list_add_first(l, lme);
259 for (; tlme != NULL; tlme = list_next(l, tlme)) {
260 /* check the frequency */
262 if (code->frequency > tlme->m->code->frequency) {
263 list_add_before(l, tlme, lme);
268 /* if we are at the end of the list, add
272 list_add_last(l, lme);
280 /* print all methods sorted */
282 printf(" frequency ratio cycles ratio method name\n");
283 printf("----------- --------- -------------- --------- -------------\n");
285 /* now iterate through the list and print it */
287 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
288 /* get method of the list element */
294 printf("%10d %.5f %12ld %.5f ",
296 (double) code->frequency / (double) frequency,
298 (double) code->cycles / (double) cycles);
302 /* print basic block frequencies */
305 for (j = 0; j < code->basicblockcount; j++)
306 printf(" L%03d: %10d\n",
307 j, code->bbfrequency[j]);
311 printf("----------- -------------- \n");
312 printf("%10d %12ld\n", frequency, (long) cycles);
314 printf("\nruns : %10d\n", runs);
315 printf("hits : %10d\n", hits);
316 printf("misses: %10d\n", misses);
318 #endif /* !defined(NDEBUG) */
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 * ---------------------------------------------------------------------
328 * indent-tabs-mode: t