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.hpp"
36 #include "threads/thread.hpp"
38 #include "vm/jit/builtin.hpp"
39 #include "vm/class.hpp"
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.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.
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 = methodtree_find_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 Recompiler_queue_method(m);
157 // Unlock the thread lists.
164 /* profile_start_thread ********************************************************
166 Starts the profile sampling thread.
168 *******************************************************************************/
170 #if defined(ENABLE_THREADS)
171 bool profile_start_thread(void)
175 name = utf_new_char("Profiling Sampler");
177 if (!threads_thread_start_internal(name, profile_thread))
180 /* everything's ok */
187 /* profile_printstats **********************************************************
189 Prints profiling statistics gathered during runtime.
191 *******************************************************************************/
194 void profile_printstats(void)
200 classcache_name_entry *nmen;
201 classcache_class_entry *clsen;
210 /* create new method list */
211 // TODO Use a sorted container.
212 List* l = List_new();
214 /* iterate through all classes and methods */
216 for (slot = 0; slot < hashtable_classcache.size; slot++) {
217 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
219 for (; nmen; nmen = nmen->hashlink) {
220 /* iterate over all class entries */
222 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
228 /* interate over all class methods */
230 for (i = 0; i < c->methodscount; i++) {
231 m = &(c->methods[i]);
235 /* was this method actually called? */
237 if ((code != NULL) && (code->frequency > 0)) {
238 /* add to overall stats */
240 frequency += code->frequency;
241 cycles += code->cycles;
243 /* sort the new entry into the list */
245 if (List_empty(l) == NULL) {
246 List_push_back(l, m);
249 for (; tlme != NULL; tlme = list_next(l, tlme)) {
250 /* check the frequency */
252 if (code->frequency > tlme->m->code->frequency) {
253 list_add_before(l, tlme, lme);
258 /* if we are at the end of the list, add
262 list_add_last(l, lme);
270 /* print all methods sorted */
272 printf(" frequency ratio cycles ratio method name\n");
273 printf("----------- --------- -------------- --------- -------------\n");
275 /* now iterate through the list and print it */
277 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
278 /* get method of the list element */
284 printf("%10d %.5f %12ld %.5f ",
286 (double) code->frequency / (double) frequency,
288 (double) code->cycles / (double) cycles);
292 /* print basic block frequencies */
295 for (j = 0; j < code->basicblockcount; j++)
296 printf(" L%03d: %10d\n",
297 j, code->bbfrequency[j]);
301 printf("----------- -------------- \n");
302 printf("%10d %12ld\n", frequency, (long) cycles);
304 printf("\nruns : %10d\n", runs);
305 printf("hits : %10d\n", hits);
306 printf("misses: %10d\n", misses);
308 #endif /* !defined(NDEBUG) */
312 * These are local overrides for various environment variables in Emacs.
313 * Please do not remove this and leave it at the end of the file, where
314 * Emacs will automagically detect them.
315 * ---------------------------------------------------------------------
318 * indent-tabs-mode: t