* src/vm/jit/codegen-common.cpp (codegen_emit): New generic version of the
[cacao.git] / src / vm / jit / optimizing / profile.c
index cf5771c95848b46c2a527fe9d302c756e94f8cd5..5268c8d75db6ab52a9f728ddef26b8b0e79c0672 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/jit/optimizing/profile.c - runtime profiling
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Christian Thalinger
-
-   Changes:
-
-   $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
-
 */
 
 
 
 #include "vm/types.h"
 
-#include "mm/memory.h"
-#include "native/jni.h"
-#include "native/include/java_lang_Thread.h"
-#include "native/include/java_lang_VMThread.h"
+#include "mm/memory.hpp"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#include "threads/threadlist.hpp"
+#include "threads/thread.hpp"
 
-#include "vm/builtin.h"
-#include "vm/class.h"
-#include "vm/classcache.h"
-#include "vm/method.h"
+#include "vm/jit/builtin.hpp"
+#include "vm/class.hpp"
+#include "vm/classcache.hpp"
+#include "vm/method.hpp"
 #include "vm/options.h"
-#include "vm/stringlocal.h"
-#include "vm/jit/jit.h"
-#include "vm/jit/methodheader.h"
-#include "vm/jit/optimizing/recompile.h"
-
+#include "vm/string.hpp"
 
-/* global variables ***********************************************************/
+#include "vm/jit/jit.hpp"
+#include "vm/jit/methodheader.h"
+#include "vm/jit/methodtree.h"
 
-#if defined(ENABLE_THREADS)
-static java_lang_VMThread *profile_vmthread;
-#endif
+#include "vm/jit/optimizing/recompiler.hpp"
 
 
 /* profile_init ****************************************************************
@@ -110,66 +92,72 @@ static void profile_thread(void)
                threads_sleep(0, nanos);
                runs++;
 
-               /* iterate over all started threads */
+               // Lock the thread lists.
+               ThreadList_lock();
 
-               t = mainthreadobj;
+#if 0
+               /* iterate over all started threads */
 
-               do {
+               for (t = ThreadList_first(); t != NULL; t = ThreadList_next(t)) {
                        /* is this a Java thread? */
 
-                       if (t->flags & THREAD_FLAG_JAVA) {
-                               /* send SIGUSR2 to thread to get the current PC */
+                       if (!(t->flags & THREAD_FLAG_JAVA))
+                               continue;
 
-                               pthread_kill(t->tid, SIGUSR2);
+                       /* send SIGUSR2 to thread to get the current PC */
+                       /* XXX write a threads-function for that */
 
-                               /* the thread object now contains the current thread PC */
+                       pthread_kill(t->tid, SIGUSR2);
 
-                               pc = t->pc;
+                       /* the thread object now contains the current thread PC */
 
-                               /* get the PV for the current PC */
+                       pc = t->pc;
 
-                               pv = codegen_get_pv_from_pc_nocheck(pc);
+                       /* Get the PV for the current PC. */
 
-                               /* get methodinfo pointer from data segment */
+                       pv = methodtree_find_nocheck(pc);
 
-                               if (pv == NULL) {
-                                       misses++;
-                               }
-                               else {
-                                       code = *((codeinfo **) (pv + CodeinfoPointer));
+                       /* get methodinfo pointer from data segment */
 
-                                       /* For asm_vm_call_method the codeinfo pointer is
-                                          NULL (which is also in the method tree). */
+                       if (pv == NULL) {
+                               misses++;
+                       }
+                       else {
+                               code = *((codeinfo **) (pv + CodeinfoPointer));
 
-                                       if (code != NULL) {
-                                               m = code->m;
+                               /* For asm_vm_call_method the codeinfo pointer is NULL
+                                  (which is also in the method tree). */
 
-                                               /* native methods are never recompiled */
+                               if (code != NULL) {
+                                       m = code->m;
 
-                                               if (!(m->flags & ACC_NATIVE)) {
-                                                       /* increase the method incovation counter */
+                                       /* native methods are never recompiled */
 
-                                                       code->frequency++;
-                                                       hits++;
+                                       if (!(m->flags & ACC_NATIVE)) {
+                                               /* increase the method incovation counter */
 
-                                                       if (code->frequency > 500) {
-                                                               /* clear frequency count before
-                                                                  recompilation */
+                                               code->frequency++;
+                                               hits++;
 
-                                                               code->frequency = 0;
+                                               if (code->frequency > 500) {
+                                                       /* clear frequency count before
+                                                          recompilation */
 
-                                                               /* add this method to the method list
-                                                                  and start recompilation */
+                                                       code->frequency = 0;
 
-                                                               recompile_queue_method(m);
-                                                       }
+                                                       /* add this method to the method list and
+                                                          start recompilation */
+
+                                                       Recompiler_queue_method(m);
                                                }
                                        }
                                }
                        }
+               }
+#endif
 
-                       t = t->next;
-               } while ((t != NULL) && (t != mainthreadobj));
+               // Unlock the thread lists.
+               ThreadList_unlock();
        }
 }
 #endif
@@ -184,29 +172,13 @@ static void profile_thread(void)
 #if defined(ENABLE_THREADS)
 bool profile_start_thread(void)
 {
-       java_lang_Thread *t;
+       utf *name;
 
-       /* create the profile object */
+       name = utf_new_char("Profiling Sampler");
 
-       profile_vmthread =
-               (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
-
-       if (profile_vmthread == NULL)
+       if (!threads_thread_start_internal(name, profile_thread))
                return false;
 
-       t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
-
-       t->vmThread = profile_vmthread;
-       t->name     = javastring_new_from_ascii("Profiling Sampler");
-       t->daemon   = true;
-       t->priority = 5;
-
-       profile_vmthread->thread = t;
-
-       /* actually start the profile sampling thread */
-
-       threads_start_thread(t, profile_thread);
-
        /* everything's ok */
 
        return true;
@@ -223,9 +195,6 @@ bool profile_start_thread(void)
 #if !defined(NDEBUG)
 void profile_printstats(void)
 {
-       list                   *l;
-       list_method_entry      *lme;
-       list_method_entry      *tlme;
        classinfo              *c;
        methodinfo             *m;
        codeinfo               *code;
@@ -240,9 +209,10 @@ void profile_printstats(void)
        frequency = 0;
        cycles    = 0;
 
+#if 0
        /* create new method list */
-
-       l = list_create(OFFSET(list_method_entry, linkage));
+       // TODO Use a sorted container.
+       List* l = List_new();
 
        /* iterate through all classes and methods */
 
@@ -273,15 +243,10 @@ void profile_printstats(void)
                                                frequency += code->frequency;
                                                cycles    += code->cycles;
 
-                                               /* create new list entry */
-
-                                               lme = NEW(list_method_entry);
-                                               lme->m = m;
-
                                                /* sort the new entry into the list */
                                                
-                                               if ((tlme = list_first(l)) == NULL) {
-                                                       list_add_first(l, lme);
+                                               if (List_empty(l) == NULL) {
+                                                       List_push_back(l, m);
                                                }
                                                else {
                                                        for (; tlme != NULL; tlme = list_next(l, tlme)) {
@@ -335,6 +300,7 @@ void profile_printstats(void)
                                           j, code->bbfrequency[j]);
                }
        }
+#endif
 
        printf("-----------           -------------- \n");
        printf("%10d             %12ld\n", frequency, (long) cycles);