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: profile.c 7952 2007-05-23 17:53:13Z twisti $
37 #include "mm/memory.h"
39 #include "threads/threads-common.h"
41 #include "vm/builtin.h"
42 #include "vm/stringlocal.h"
44 #include "vm/jit/jit.h"
45 #include "vm/jit/methodheader.h"
46 #include "vm/jit/optimizing/recompile.h"
48 #include "vmcore/class.h"
49 #include "vmcore/classcache.h"
50 #include "vmcore/method.h"
51 #include "vmcore/options.h"
54 /* profile_init ****************************************************************
56 Initializes the profile global lock.
58 *******************************************************************************/
60 bool profile_init(void)
68 /* profile_thread **************************************************************
72 *******************************************************************************/
78 #if defined(ENABLE_THREADS)
79 static void profile_thread(void)
89 /* sleep thread for 0.5-1.0 ms */
91 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
92 /* fprintf(stderr, "%d\n", nanos); */
94 threads_sleep(0, nanos);
97 /* lock the threads lists */
101 /* iterate over all started threads */
103 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
104 /* is this a Java thread? */
106 if (!(t->flags & THREAD_FLAG_JAVA))
109 /* send SIGUSR2 to thread to get the current PC */
110 /* XXX write a threads-function for that */
112 pthread_kill(t->tid, SIGUSR2);
114 /* the thread object now contains the current thread PC */
118 /* get the PV for the current PC */
120 pv = codegen_get_pv_from_pc_nocheck(pc);
122 /* get methodinfo pointer from data segment */
128 code = *((codeinfo **) (pv + CodeinfoPointer));
130 /* For asm_vm_call_method the codeinfo pointer is NULL
131 (which is also in the method tree). */
136 /* native methods are never recompiled */
138 if (!(m->flags & ACC_NATIVE)) {
139 /* increase the method incovation counter */
144 if (code->frequency > 500) {
145 /* clear frequency count before
150 /* add this method to the method list and
151 start recompilation */
153 recompile_queue_method(m);
160 /* unlock the threads lists */
162 threads_list_unlock();
168 /* profile_start_thread ********************************************************
170 Starts the profile sampling thread.
172 *******************************************************************************/
174 #if defined(ENABLE_THREADS)
175 bool profile_start_thread(void)
179 name = utf_new_char("Profiling Sampler");
181 if (!threads_thread_start_internal(name, profile_thread))
184 /* everything's ok */
191 /* profile_printstats **********************************************************
193 Prints profiling statistics gathered during runtime.
195 *******************************************************************************/
198 void profile_printstats(void)
201 list_method_entry *lme;
202 list_method_entry *tlme;
207 classcache_name_entry *nmen;
208 classcache_class_entry *clsen;
217 /* create new method list */
219 l = list_create(OFFSET(list_method_entry, linkage));
221 /* iterate through all classes and methods */
223 for (slot = 0; slot < hashtable_classcache.size; slot++) {
224 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
226 for (; nmen; nmen = nmen->hashlink) {
227 /* iterate over all class entries */
229 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
235 /* interate over all class methods */
237 for (i = 0; i < c->methodscount; i++) {
238 m = &(c->methods[i]);
242 /* was this method actually called? */
244 if ((code != NULL) && (code->frequency > 0)) {
245 /* add to overall stats */
247 frequency += code->frequency;
248 cycles += code->cycles;
250 /* create new list entry */
252 lme = NEW(list_method_entry);
255 /* sort the new entry into the list */
257 if ((tlme = list_first(l)) == NULL) {
258 list_add_first(l, lme);
261 for (; tlme != NULL; tlme = list_next(l, tlme)) {
262 /* check the frequency */
264 if (code->frequency > tlme->m->code->frequency) {
265 list_add_before(l, tlme, lme);
270 /* if we are at the end of the list, add
274 list_add_last(l, lme);
282 /* print all methods sorted */
284 printf(" frequency ratio cycles ratio method name\n");
285 printf("----------- --------- -------------- --------- -------------\n");
287 /* now iterate through the list and print it */
289 for (lme = list_first(l); lme != NULL; lme = list_next(l, lme)) {
290 /* get method of the list element */
296 printf("%10d %.5f %12ld %.5f ",
298 (double) code->frequency / (double) frequency,
300 (double) code->cycles / (double) cycles);
304 /* print basic block frequencies */
307 for (j = 0; j < code->basicblockcount; j++)
308 printf(" L%03d: %10d\n",
309 j, code->bbfrequency[j]);
313 printf("----------- -------------- \n");
314 printf("%10d %12ld\n", frequency, (long) cycles);
316 printf("\nruns : %10d\n", runs);
317 printf("hits : %10d\n", hits);
318 printf("misses: %10d\n", misses);
320 #endif /* !defined(NDEBUG) */
324 * These are local overrides for various environment variables in Emacs.
325 * Please do not remove this and leave it at the end of the file, where
326 * Emacs will automagically detect them.
327 * ---------------------------------------------------------------------
330 * indent-tabs-mode: t