* Removed all Id tags.
[cacao.git] / src / vm / jit / optimizing / profile.c
index cf5771c95848b46c2a527fe9d302c756e94f8cd5..0880a21a64344227374f1b418cc49396932b6bc4 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/optimizing/profile.c - runtime profiling
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 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
    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"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#endif
+#include "threads/threads-common.h"
 
 #include "vm/builtin.h"
-#include "vm/class.h"
-#include "vm/classcache.h"
-#include "vm/method.h"
-#include "vm/options.h"
 #include "vm/stringlocal.h"
+
 #include "vm/jit/jit.h"
 #include "vm/jit/methodheader.h"
 #include "vm/jit/optimizing/recompile.h"
 
-
-/* global variables ***********************************************************/
-
-#if defined(ENABLE_THREADS)
-static java_lang_VMThread *profile_vmthread;
-#endif
+#include "vmcore/class.h"
+#include "vmcore/classcache.h"
+#include "vmcore/method.h"
+#include "vmcore/options.h"
 
 
 /* profile_init ****************************************************************
@@ -110,66 +92,72 @@ static void profile_thread(void)
                threads_sleep(0, nanos);
                runs++;
 
-               /* iterate over all started threads */
+               /* lock the threads lists */
+
+               threads_list_lock();
 
-               t = mainthreadobj;
+               /* iterate over all started threads */
 
-               do {
+               for (t = threads_list_first(); t != NULL; t = threads_list_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 = codegen_get_pv_from_pc_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 */
+
+                                                       recompile_queue_method(m);
                                                }
                                        }
                                }
                        }
+               }
+
+               /* unlock the threads lists */
 
-                       t = t->next;
-               } while ((t != NULL) && (t != mainthreadobj));
+               threads_list_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,7 +195,7 @@ bool profile_start_thread(void)
 #if !defined(NDEBUG)
 void profile_printstats(void)
 {
-       list                   *l;
+       list_t                 *l;
        list_method_entry      *lme;
        list_method_entry      *tlme;
        classinfo              *c;