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.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/methodtree.h"
45 #include "vm/jit/optimizing/recompile.h"
47 #include "vmcore/class.h"
48 #include "vmcore/classcache.h"
49 #include "vmcore/method.h"
50 #include "vmcore/options.h"
53 /* profile_init ****************************************************************
55 Initializes the profile global lock.
57 *******************************************************************************/
59 bool profile_init(void)
67 /* profile_thread **************************************************************
71 *******************************************************************************/
77 #if defined(ENABLE_THREADS)
78 static void profile_thread(void)
88 /* sleep thread for 0.5-1.0 ms */
90 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
91 /* fprintf(stderr, "%d\n", nanos); */
93 threads_sleep(0, nanos);
96 /* lock the threads lists */
100 /* iterate over all started threads */
102 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
103 /* is this a Java thread? */
105 if (!(t->flags & THREAD_FLAG_JAVA))
108 /* send SIGUSR2 to thread to get the current PC */
109 /* XXX write a threads-function for that */
111 pthread_kill(t->tid, SIGUSR2);
113 /* the thread object now contains the current thread PC */
117 /* Get the PV for the current PC. */
119 pv = methodtree_find_nocheck(pc);
121 /* get methodinfo pointer from data segment */
127 code = *((codeinfo **) (pv + CodeinfoPointer));
129 /* For asm_vm_call_method the codeinfo pointer is NULL
130 (which is also in the method tree). */
135 /* native methods are never recompiled */
137 if (!(m->flags & ACC_NATIVE)) {
138 /* increase the method incovation counter */
143 if (code->frequency > 500) {
144 /* clear frequency count before
149 /* add this method to the method list and
150 start recompilation */
152 recompile_queue_method(m);
159 /* unlock the threads lists */
167 /* profile_start_thread ********************************************************
169 Starts the profile sampling thread.
171 *******************************************************************************/
173 #if defined(ENABLE_THREADS)
174 bool profile_start_thread(void)
178 name = utf_new_char("Profiling Sampler");
180 if (!threads_thread_start_internal(name, profile_thread))
183 /* everything's ok */
190 /* profile_printstats **********************************************************
192 Prints profiling statistics gathered during runtime.
194 *******************************************************************************/
197 void profile_printstats(void)
200 list_method_entry *lme;
201 list_method_entry *tlme;
206 classcache_name_entry *nmen;
207 classcache_class_entry *clsen;
216 /* create new method list */
218 l = list_create(OFFSET(list_method_entry, linkage));
220 /* iterate through all classes and methods */
222 for (slot = 0; slot < hashtable_classcache.size; slot++) {
223 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
225 for (; nmen; nmen = nmen->hashlink) {
226 /* iterate over all class entries */
228 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
234 /* interate over all class methods */
236 for (i = 0; i < c->methodscount; i++) {
237 m = &(c->methods[i]);
241 /* was this method actually called? */
243 if ((code != NULL) && (code->frequency > 0)) {
244 /* add to overall stats */
246 frequency += code->frequency;
247 cycles += code->cycles;
249 /* create new list entry */
251 lme = NEW(list_method_entry);
254 /* sort the new entry into the list */
256 if ((tlme = list_first(l)) == NULL) {
257 list_add_first(l, lme);
260 for (; tlme != NULL; tlme = list_next(l, tlme)) {
261 /* check the frequency */
263 if (code->frequency > tlme->m->code->frequency) {
264 list_add_before(l, tlme, lme);
269 /* if we are at the end of the list, add
273 list_add_last(l, lme);
281 /* print all methods sorted */
283 printf(" frequency ratio cycles ratio method name\n");
284 printf("----------- --------- -------------- --------- -------------\n");
286 /* now iterate through the list and print it */
288 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
289 /* get method of the list element */
295 printf("%10d %.5f %12ld %.5f ",
297 (double) code->frequency / (double) frequency,
299 (double) code->cycles / (double) cycles);
303 /* print basic block frequencies */
306 for (j = 0; j < code->basicblockcount; j++)
307 printf(" L%03d: %10d\n",
308 j, code->bbfrequency[j]);
312 printf("----------- -------------- \n");
313 printf("%10d %12ld\n", frequency, (long) cycles);
315 printf("\nruns : %10d\n", runs);
316 printf("hits : %10d\n", hits);
317 printf("misses: %10d\n", misses);
319 #endif /* !defined(NDEBUG) */
323 * These are local overrides for various environment variables in Emacs.
324 * Please do not remove this and leave it at the end of the file, where
325 * Emacs will automagically detect them.
326 * ---------------------------------------------------------------------
329 * indent-tabs-mode: t