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 /* global variables ***********************************************************/
58 #if defined(ENABLE_THREADS)
59 static threadobject *thread_profile;
63 /* profile_init ****************************************************************
65 Initializes the profile global lock.
67 *******************************************************************************/
69 bool profile_init(void)
77 /* profile_thread **************************************************************
81 *******************************************************************************/
87 #if defined(ENABLE_THREADS)
88 static void profile_thread(void)
98 /* sleep thread for 0.5-1.0 ms */
100 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
101 /* fprintf(stderr, "%d\n", nanos); */
103 threads_sleep(0, nanos);
106 /* iterate over all started threads */
111 /* is this a Java thread? */
113 if (t->flags & THREAD_FLAG_JAVA) {
114 /* send SIGUSR2 to thread to get the current PC */
116 pthread_kill(t->tid, SIGUSR2);
118 /* the thread object now contains the current thread PC */
122 /* get the PV for the current PC */
124 pv = codegen_get_pv_from_pc_nocheck(pc);
126 /* get methodinfo pointer from data segment */
132 code = *((codeinfo **) (pv + CodeinfoPointer));
134 /* For asm_vm_call_method the codeinfo pointer is
135 NULL (which is also in the method tree). */
140 /* native methods are never recompiled */
142 if (!(m->flags & ACC_NATIVE)) {
143 /* increase the method incovation counter */
148 if (code->frequency > 500) {
149 /* clear frequency count before
154 /* add this method to the method list
155 and start recompilation */
157 recompile_queue_method(m);
165 } while ((t != NULL) && (t != mainthreadobj));
171 /* profile_start_thread ********************************************************
173 Starts the profile sampling thread.
175 *******************************************************************************/
177 #if defined(ENABLE_THREADS)
178 bool profile_start_thread(void)
182 name = utf_new_char("Profiling Sampler");
184 thread_profile = threads_create_thread(name);
186 if (thread_profile == NULL)
189 /* actually start the profile sampling thread */
191 threads_start_thread(thread_profile, profile_thread);
193 /* everything's ok */
200 /* profile_printstats **********************************************************
202 Prints profiling statistics gathered during runtime.
204 *******************************************************************************/
207 void profile_printstats(void)
210 list_method_entry *lme;
211 list_method_entry *tlme;
216 classcache_name_entry *nmen;
217 classcache_class_entry *clsen;
226 /* create new method list */
228 l = list_create(OFFSET(list_method_entry, linkage));
230 /* iterate through all classes and methods */
232 for (slot = 0; slot < hashtable_classcache.size; slot++) {
233 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
235 for (; nmen; nmen = nmen->hashlink) {
236 /* iterate over all class entries */
238 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
244 /* interate over all class methods */
246 for (i = 0; i < c->methodscount; i++) {
247 m = &(c->methods[i]);
251 /* was this method actually called? */
253 if ((code != NULL) && (code->frequency > 0)) {
254 /* add to overall stats */
256 frequency += code->frequency;
257 cycles += code->cycles;
259 /* create new list entry */
261 lme = NEW(list_method_entry);
264 /* sort the new entry into the list */
266 if ((tlme = list_first(l)) == NULL) {
267 list_add_first(l, lme);
270 for (; tlme != NULL; tlme = list_next(l, tlme)) {
271 /* check the frequency */
273 if (code->frequency > tlme->m->code->frequency) {
274 list_add_before(l, tlme, lme);
279 /* if we are at the end of the list, add
283 list_add_last(l, lme);
291 /* print all methods sorted */
293 printf(" frequency ratio cycles ratio method name\n");
294 printf("----------- --------- -------------- --------- -------------\n");
296 /* now iterate through the list and print it */
298 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
299 /* get method of the list element */
305 printf("%10d %.5f %12ld %.5f ",
307 (double) code->frequency / (double) frequency,
309 (double) code->cycles / (double) cycles);
313 /* print basic block frequencies */
316 for (j = 0; j < code->basicblockcount; j++)
317 printf(" L%03d: %10d\n",
318 j, code->bbfrequency[j]);
322 printf("----------- -------------- \n");
323 printf("%10d %12ld\n", frequency, (long) cycles);
325 printf("\nruns : %10d\n", runs);
326 printf("hits : %10d\n", hits);
327 printf("misses: %10d\n", misses);
329 #endif /* !defined(NDEBUG) */
333 * These are local overrides for various environment variables in Emacs.
334 * Please do not remove this and leave it at the end of the file, where
335 * Emacs will automagically detect them.
336 * ---------------------------------------------------------------------
339 * indent-tabs-mode: t