1 /* src/vm/jit/optimizing/profile.cpp - runtime profiling
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2009 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "mm/memory.hpp"
36 #include "threads/threadlist.hpp"
37 #include "threads/thread.hpp"
39 #include "vm/jit/builtin.hpp"
40 #include "vm/class.hpp"
41 #include "vm/classcache.hpp"
42 #include "vm/method.hpp"
43 #include "vm/options.h"
44 #include "vm/string.hpp"
46 #include "vm/jit/jit.hpp"
47 #include "vm/jit/methodheader.h"
48 #include "vm/jit/methodtree.h"
50 #include "vm/jit/optimizing/recompiler.hpp"
53 /* profile_init ****************************************************************
55 Initializes the profile global lock.
57 *******************************************************************************/
59 bool profile_init(void)
67 /* profile_thread **************************************************************
71 *******************************************************************************/
77 #if defined(ENABLE_THREADS)
78 static void profile_thread(void)
88 /* sleep thread for 0.5-1.0 ms */
90 nanos = 500 + (int) (500.0 * (rand() / (RAND_MAX + 1.0)));
91 /* fprintf(stderr, "%d\n", nanos); */
93 threads_sleep(0, nanos);
96 // Lock the thread lists.
100 /* iterate over all started threads */
102 for (t = ThreadList_first(); t != NULL; t = ThreadList_next(t)) {
103 /* is this a Java thread? */
105 if (!(t->flags & THREAD_FLAG_JAVA))
108 /* send SIGUSR2 to thread to get the current PC */
109 /* XXX write a threads-function for that */
111 pthread_kill(t->tid, SIGUSR2);
113 /* the thread object now contains the current thread PC */
117 /* Get the PV for the current PC. */
119 pv = methodtree_find_nocheck(pc);
121 /* get methodinfo pointer from data segment */
127 code = *((codeinfo **) (pv + CodeinfoPointer));
129 /* For asm_vm_call_method the codeinfo pointer is NULL
130 (which is also in the method tree). */
135 /* native methods are never recompiled */
137 if (!(m->flags & ACC_NATIVE)) {
138 /* increase the method incovation counter */
143 if (code->frequency > 500) {
144 /* clear frequency count before
149 /* add this method to the method list and
150 start recompilation */
152 Recompiler_queue_method(m);
160 // Unlock the thread lists.
161 ThreadList::unlock();
167 /* profile_start_thread ********************************************************
169 Starts the profile sampling thread.
171 *******************************************************************************/
173 #if defined(ENABLE_THREADS)
174 bool profile_start_thread(void)
178 name = utf_new_char("Profiling Sampler");
180 if (!threads_thread_start_internal(name, profile_thread))
183 /* everything's ok */
191 * Comparison function used to sort a method list from higher to lower by
192 * comparing the method call frequencies.
194 * @param m1 First method to be compared.
195 * @param m2 Second method to be compared.
196 * @return Returns true if the first method goes before the second method in
197 * the specific order, and false otherwise.
200 static bool profile_compare_frequency(methodinfo* m1, methodinfo* m2)
202 return (m1->code->frequency > m2->code->frequency);
208 * Prints profiling statistics gathered during runtime.
211 void profile_printstats(void)
217 classcache_name_entry *nmen;
218 classcache_class_entry *clsen;
227 /* create new method list */
229 std::list<methodinfo*> l;
230 //DumpList<methodinfo*> l; // XXX currently the DumpList doesn't work here.
232 /* iterate through all classes and methods */
234 for (slot = 0; slot < hashtable_classcache.size; slot++) {
235 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
237 for (; nmen; nmen = nmen->hashlink) {
238 /* iterate over all class entries */
240 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
246 /* interate over all class methods */
248 for (i = 0; i < c->methodscount; i++) {
249 m = &(c->methods[i]);
253 /* was this method actually called? */
255 if ((code != NULL) && (code->frequency > 0)) {
256 /* add to overall stats */
258 frequency += code->frequency;
259 cycles += code->cycles;
261 /* add new entry into method list */
270 /* sort the method list */
272 l.sort(profile_compare_frequency);
274 /* print all methods sorted */
276 printf(" frequency ratio cycles ratio method name\n");
277 printf("----------- --------- -------------- --------- -------------\n");
279 /* now iterate through the list and print it */
281 for (DumpList<methodinfo*>::iterator it = l.begin(); it != l.end(); ++it) {
286 printf("%10d %.5f %12ld %.5f ",
288 (double) code->frequency / (double) frequency,
290 (double) code->cycles / (double) cycles);
294 /* print basic block frequencies */
297 for (j = 0; j < code->basicblockcount; j++)
298 printf(" L%03d: %10d\n",
299 j, code->bbfrequency[j]);
303 printf("----------- -------------- \n");
304 printf("%10d %12ld\n", frequency, (long) cycles);
306 printf("\nruns : %10d\n", runs);
307 printf("hits : %10d\n", hits);
308 printf("misses: %10d\n", misses);
310 #endif /* !defined(NDEBUG) */
314 * These are local overrides for various environment variables in Emacs.
315 * Please do not remove this and leave it at the end of the file, where
316 * Emacs will automagically detect them.
317 * ---------------------------------------------------------------------
320 * indent-tabs-mode: t