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/threads-common.h"
38 #include "vm/builtin.h"
39 #include "vm/stringlocal.h"
41 #include "vm/jit/jit.h"
42 #include "vm/jit/methodheader.h"
43 #include "vm/jit/optimizing/recompile.h"
45 #include "vmcore/class.h"
46 #include "vmcore/classcache.h"
47 #include "vmcore/method.h"
48 #include "vmcore/options.h"
51 /* profile_init ****************************************************************
53 Initializes the profile global lock.
55 *******************************************************************************/
57 bool profile_init(void)
65 /* profile_thread **************************************************************
69 *******************************************************************************/
75 #if defined(ENABLE_THREADS)
76 static void profile_thread(void)
86 /* sleep thread for 0.5-1.0 ms */
88 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
89 /* fprintf(stderr, "%d\n", nanos); */
91 threads_sleep(0, nanos);
94 /* lock the threads lists */
98 /* iterate over all started threads */
100 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
101 /* is this a Java thread? */
103 if (!(t->flags & THREAD_FLAG_JAVA))
106 /* send SIGUSR2 to thread to get the current PC */
107 /* XXX write a threads-function for that */
109 pthread_kill(t->tid, SIGUSR2);
111 /* the thread object now contains the current thread PC */
115 /* get the PV for the current PC */
117 pv = codegen_get_pv_from_pc_nocheck(pc);
119 /* get methodinfo pointer from data segment */
125 code = *((codeinfo **) (pv + CodeinfoPointer));
127 /* For asm_vm_call_method the codeinfo pointer is NULL
128 (which is also in the method tree). */
133 /* native methods are never recompiled */
135 if (!(m->flags & ACC_NATIVE)) {
136 /* increase the method incovation counter */
141 if (code->frequency > 500) {
142 /* clear frequency count before
147 /* add this method to the method list and
148 start recompilation */
150 recompile_queue_method(m);
157 /* unlock the threads lists */
159 threads_list_unlock();
165 /* profile_start_thread ********************************************************
167 Starts the profile sampling thread.
169 *******************************************************************************/
171 #if defined(ENABLE_THREADS)
172 bool profile_start_thread(void)
176 name = utf_new_char("Profiling Sampler");
178 if (!threads_thread_start_internal(name, profile_thread))
181 /* everything's ok */
188 /* profile_printstats **********************************************************
190 Prints profiling statistics gathered during runtime.
192 *******************************************************************************/
195 void profile_printstats(void)
198 list_method_entry *lme;
199 list_method_entry *tlme;
204 classcache_name_entry *nmen;
205 classcache_class_entry *clsen;
214 /* create new method list */
216 l = list_create(OFFSET(list_method_entry, linkage));
218 /* iterate through all classes and methods */
220 for (slot = 0; slot < hashtable_classcache.size; slot++) {
221 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
223 for (; nmen; nmen = nmen->hashlink) {
224 /* iterate over all class entries */
226 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
232 /* interate over all class methods */
234 for (i = 0; i < c->methodscount; i++) {
235 m = &(c->methods[i]);
239 /* was this method actually called? */
241 if ((code != NULL) && (code->frequency > 0)) {
242 /* add to overall stats */
244 frequency += code->frequency;
245 cycles += code->cycles;
247 /* create new list entry */
249 lme = NEW(list_method_entry);
252 /* sort the new entry into the list */
254 if ((tlme = list_first(l)) == NULL) {
255 list_add_first(l, lme);
258 for (; tlme != NULL; tlme = list_next(l, tlme)) {
259 /* check the frequency */
261 if (code->frequency > tlme->m->code->frequency) {
262 list_add_before(l, tlme, lme);
267 /* if we are at the end of the list, add
271 list_add_last(l, lme);
279 /* print all methods sorted */
281 printf(" frequency ratio cycles ratio method name\n");
282 printf("----------- --------- -------------- --------- -------------\n");
284 /* now iterate through the list and print it */
286 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
287 /* get method of the list element */
293 printf("%10d %.5f %12ld %.5f ",
295 (double) code->frequency / (double) frequency,
297 (double) code->cycles / (double) cycles);
301 /* print basic block frequencies */
304 for (j = 0; j < code->basicblockcount; j++)
305 printf(" L%03d: %10d\n",
306 j, code->bbfrequency[j]);
310 printf("----------- -------------- \n");
311 printf("%10d %12ld\n", frequency, (long) cycles);
313 printf("\nruns : %10d\n", runs);
314 printf("hits : %10d\n", hits);
315 printf("misses: %10d\n", misses);
317 #endif /* !defined(NDEBUG) */
321 * These are local overrides for various environment variables in Emacs.
322 * Please do not remove this and leave it at the end of the file, where
323 * Emacs will automagically detect them.
324 * ---------------------------------------------------------------------
327 * indent-tabs-mode: t