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.hpp"
35 #include "threads/threadlist.hpp"
36 #include "threads/thread.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/class.hpp"
40 #include "vm/classcache.hpp"
41 #include "vm/method.hpp"
42 #include "vm/options.h"
43 #include "vm/string.hpp"
45 #include "vm/jit/jit.hpp"
46 #include "vm/jit/methodheader.h"
47 #include "vm/jit/methodtree.h"
49 #include "vm/jit/optimizing/recompiler.hpp"
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 thread 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 Recompiler_queue_method(m);
159 // Unlock the thread 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)
202 classcache_name_entry *nmen;
203 classcache_class_entry *clsen;
213 /* create new method list */
214 // TODO Use a sorted container.
215 List* l = List_new();
217 /* iterate through all classes and methods */
219 for (slot = 0; slot < hashtable_classcache.size; slot++) {
220 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
222 for (; nmen; nmen = nmen->hashlink) {
223 /* iterate over all class entries */
225 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
231 /* interate over all class methods */
233 for (i = 0; i < c->methodscount; i++) {
234 m = &(c->methods[i]);
238 /* was this method actually called? */
240 if ((code != NULL) && (code->frequency > 0)) {
241 /* add to overall stats */
243 frequency += code->frequency;
244 cycles += code->cycles;
246 /* sort the new entry into the list */
248 if (List_empty(l) == NULL) {
249 List_push_back(l, m);
252 for (; tlme != NULL; tlme = list_next(l, tlme)) {
253 /* check the frequency */
255 if (code->frequency > tlme->m->code->frequency) {
256 list_add_before(l, tlme, lme);
261 /* if we are at the end of the list, add
265 list_add_last(l, lme);
273 /* print all methods sorted */
275 printf(" frequency ratio cycles ratio method name\n");
276 printf("----------- --------- -------------- --------- -------------\n");
278 /* now iterate through the list and print it */
280 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
281 /* get method of the list element */
287 printf("%10d %.5f %12ld %.5f ",
289 (double) code->frequency / (double) frequency,
291 (double) code->cycles / (double) cycles);
295 /* print basic block frequencies */
298 for (j = 0; j < code->basicblockcount; j++)
299 printf(" L%03d: %10d\n",
300 j, code->bbfrequency[j]);
305 printf("----------- -------------- \n");
306 printf("%10d %12ld\n", frequency, (long) cycles);
308 printf("\nruns : %10d\n", runs);
309 printf("hits : %10d\n", hits);
310 printf("misses: %10d\n", misses);
312 #endif /* !defined(NDEBUG) */
316 * These are local overrides for various environment variables in Emacs.
317 * Please do not remove this and leave it at the end of the file, where
318 * Emacs will automagically detect them.
319 * ---------------------------------------------------------------------
322 * indent-tabs-mode: t