1 /* src/vm/jit/optimizing/profile.c - runtime profiling
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
37 #include "mm/memory.h"
39 #include "threads/threads-common.h"
41 #include "threads/native/threads.h"
43 #include "vm/builtin.h"
44 #include "vm/stringlocal.h"
46 #include "vm/jit/jit.h"
47 #include "vm/jit/methodheader.h"
48 #include "vm/jit/optimizing/recompile.h"
50 #include "vmcore/class.h"
51 #include "vmcore/classcache.h"
52 #include "vmcore/method.h"
53 #include "vmcore/options.h"
56 /* profile_init ****************************************************************
58 Initializes the profile global lock.
60 *******************************************************************************/
62 bool profile_init(void)
70 /* profile_thread **************************************************************
74 *******************************************************************************/
80 #if defined(ENABLE_THREADS)
81 static void profile_thread(void)
91 /* sleep thread for 0.5-1.0 ms */
93 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
94 /* fprintf(stderr, "%d\n", nanos); */
96 threads_sleep(0, nanos);
99 /* iterate over all started threads */
104 /* is this a Java thread? */
106 if (t->flags & THREAD_FLAG_JAVA) {
107 /* send SIGUSR2 to thread to get the current PC */
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
128 NULL (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
148 and start recompilation */
150 recompile_queue_method(m);
158 } while ((t != NULL) && (t != mainthreadobj));
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)
197 list_method_entry *lme;
198 list_method_entry *tlme;
203 classcache_name_entry *nmen;
204 classcache_class_entry *clsen;
213 /* create new method list */
215 l = list_create(OFFSET(list_method_entry, linkage));
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 /* create new list entry */
248 lme = NEW(list_method_entry);
251 /* sort the new entry into the list */
253 if ((tlme = list_first(l)) == NULL) {
254 list_add_first(l, lme);
257 for (; tlme != NULL; tlme = list_next(l, tlme)) {
258 /* check the frequency */
260 if (code->frequency > tlme->m->code->frequency) {
261 list_add_before(l, tlme, lme);
266 /* if we are at the end of the list, add
270 list_add_last(l, lme);
278 /* print all methods sorted */
280 printf(" frequency ratio cycles ratio method name\n");
281 printf("----------- --------- -------------- --------- -------------\n");
283 /* now iterate through the list and print it */
285 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
286 /* get method of the list element */
292 printf("%10d %.5f %12ld %.5f ",
294 (double) code->frequency / (double) frequency,
296 (double) code->cycles / (double) cycles);
300 /* print basic block frequencies */
303 for (j = 0; j < code->basicblockcount; j++)
304 printf(" L%03d: %10d\n",
305 j, code->bbfrequency[j]);
309 printf("----------- -------------- \n");
310 printf("%10d %12ld\n", frequency, (long) cycles);
312 printf("\nruns : %10d\n", runs);
313 printf("hits : %10d\n", hits);
314 printf("misses: %10d\n", misses);
316 #endif /* !defined(NDEBUG) */
320 * These are local overrides for various environment variables in Emacs.
321 * Please do not remove this and leave it at the end of the file, where
322 * Emacs will automagically detect them.
323 * ---------------------------------------------------------------------
326 * indent-tabs-mode: t