2009-12-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / profiler.c
index 30ec536018b5eab2e73220dc502ea8724b605de0..d8ae324f2b86ba0b1e806dfd752b92b27dcaf790 100644 (file)
@@ -4,8 +4,8 @@
  * Author:
  *   Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2001-2003 Ximian, Inc.
- * (C) 2003-2006 Novell, Inc.
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include "config.h"
@@ -55,9 +55,16 @@ static MonoProfileClassFunc   class_end_unload;
 static MonoProfileMethodFunc   jit_start;
 static MonoProfileMethodResult jit_end;
 static MonoProfileJitResult    jit_end2;
+static MonoProfileMethodFunc   method_free;
+static MonoProfileMethodFunc   method_start_invoke;
+static MonoProfileMethodFunc   method_end_invoke;
 static MonoProfileMethodResult man_unman_transition;
 static MonoProfileAllocFunc    allocation_cb;
+static MonoProfileMonitorFunc  monitor_event_cb;
 static MonoProfileStatFunc     statistical_cb;
+static MonoProfileStatCallChainFunc statistical_call_chain_cb;
+static int                     statistical_call_chain_depth;
+static MonoProfilerCallChainStrategy  statistical_call_chain_strategy;
 static MonoProfileMethodFunc   method_enter;
 static MonoProfileMethodFunc   method_leave;
 
@@ -75,6 +82,8 @@ static MonoProfileFunc shutdown_callback;
 static MonoProfileGCFunc        gc_event;
 static MonoProfileGCResizeFunc  gc_heap_resize;
 
+static MonoProfileFunc          runtime_initialized_event;
+
 #define mono_profiler_coverage_lock() EnterCriticalSection (&profiler_coverage_mutex)
 #define mono_profiler_coverage_unlock() LeaveCriticalSection (&profiler_coverage_mutex)
 static CRITICAL_SECTION profiler_coverage_mutex;
@@ -171,6 +180,19 @@ mono_profiler_install_jit_end (MonoProfileJitResult end)
        jit_end2 = end;
 }
 
+void 
+mono_profiler_install_method_free (MonoProfileMethodFunc callback)
+{
+       method_free = callback;
+}
+
+void
+mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
+{
+       method_start_invoke = start;
+       method_end_invoke = end;
+}
+
 void 
 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
 {
@@ -190,12 +212,49 @@ mono_profiler_install_allocation (MonoProfileAllocFunc callback)
        allocation_cb = callback;
 }
 
+void
+mono_profiler_install_monitor  (MonoProfileMonitorFunc callback)
+{
+       monitor_event_cb = callback;
+}
+
 void 
 mono_profiler_install_statistical (MonoProfileStatFunc callback)
 {
        statistical_cb = callback;
 }
 
+void 
+mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
+       statistical_call_chain_cb = callback;
+       statistical_call_chain_depth = call_chain_depth;
+       if (statistical_call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
+               statistical_call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
+       }
+       statistical_call_chain_strategy = call_chain_strategy;
+       if ((statistical_call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (statistical_call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
+               statistical_call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
+       }
+}
+
+int
+mono_profiler_stat_get_call_chain_depth (void) {
+       if (statistical_call_chain_cb != NULL) {
+               return statistical_call_chain_depth;
+       } else {
+               return 0;
+       }
+}
+
+MonoProfilerCallChainStrategy
+mono_profiler_stat_get_call_chain_strategy (void) {
+       if (statistical_call_chain_cb != NULL) {
+               return statistical_call_chain_strategy;
+       } else {
+               return MONO_PROFILER_CALL_CHAIN_NONE;
+       }
+}
+
 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
 {
        exception_throw_cb = throw_callback;
@@ -282,6 +341,27 @@ mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result
        }
 }
 
+void 
+mono_profiler_method_free (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_free)
+               method_free (current_profiler, method);
+}
+
+void
+mono_profiler_method_start_invoke (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_start_invoke)
+               method_start_invoke (current_profiler, method);
+}
+
+void
+mono_profiler_method_end_invoke (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_end_invoke)
+               method_end_invoke (current_profiler, method);
+}
+
 void 
 mono_profiler_code_transition (MonoMethod *method, int result)
 {
@@ -296,6 +376,13 @@ mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
                allocation_cb (current_profiler, obj, klass);
 }
 
+void
+mono_profiler_monitor_event      (MonoObject *obj, MonoProfilerMonitorEvent event) {
+       if ((mono_profiler_events & MONO_PROFILE_MONITOR_EVENTS) && monitor_event_cb) {
+               monitor_event_cb (current_profiler, obj, event);
+       }
+}
+
 void
 mono_profiler_stat_hit (guchar *ip, void *context)
 {
@@ -303,6 +390,13 @@ mono_profiler_stat_hit (guchar *ip, void *context)
                statistical_cb (current_profiler, ip, context);
 }
 
+void
+mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
+{
+       if ((mono_profiler_events & MONO_PROFILE_STATISTICAL) && statistical_call_chain_cb)
+               statistical_call_chain_cb (current_profiler, call_chain_depth, ips, context);
+}
+
 void
 mono_profiler_exception_thrown (MonoObject *exception)
 {
@@ -491,6 +585,51 @@ mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc he
        gc_heap_resize = heap_resize_callback;
 }
 
+void
+mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
+{
+       runtime_initialized_event = runtime_initialized_callback;
+}
+
+void
+mono_profiler_runtime_initialized (void) {
+       if (runtime_initialized_event)
+               runtime_initialized_event (current_profiler);
+}
+
+static MonoProfilerCodeChunkNew code_chunk_new = NULL;
+void
+mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
+       code_chunk_new = callback;
+}
+void
+mono_profiler_code_chunk_new (gpointer chunk, int size) {
+       if (code_chunk_new)
+               code_chunk_new (current_profiler, chunk, size);
+}
+
+static MonoProfilerCodeChunkDestroy code_chunk_destroy = NULL;
+void
+mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
+       code_chunk_destroy = callback;
+}
+void
+mono_profiler_code_chunk_destroy (gpointer chunk) {
+       if (code_chunk_destroy)
+               code_chunk_destroy (current_profiler, chunk);
+}
+
+static MonoProfilerCodeBufferNew code_buffer_new = NULL;
+void
+mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
+       code_buffer_new = callback;
+}
+void
+mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, void *data) {
+       if (code_buffer_new)
+               code_buffer_new (current_profiler, buffer, size, type, data);
+}
+
 static GHashTable *coverage_hash = NULL;
 
 MonoProfileCoverageInfo* 
@@ -733,6 +872,7 @@ typedef struct _LastCallerInfo LastCallerInfo;
 struct _MonoProfiler {
        GHashTable *methods;
        MonoMemPool *mempool;
+       GSList *domains;
        /* info about JIT time */
        MONO_TIMER_TYPE jit_timer;
        double      jit_time;
@@ -875,7 +1015,7 @@ output_profile (GList *funcs)
                m = method_get_name (p->method);
                fprintf (poutput, "########################\n");
                fprintf (poutput, "% 8.3f ", (double) (p->total * 1000));
-               fprintf (poutput, "%7" G_GUINT64_FORMAT " ", (unsigned long long)p->count);
+               fprintf (poutput, "%7" G_GUINT64_FORMAT " ", (guint64)p->count);
                fprintf (poutput, "% 8.3f ", (double) (p->total * 1000)/(double)p->count);
                fprintf (poutput, "  %s\n", m);
 
@@ -883,7 +1023,7 @@ output_profile (GList *funcs)
                /* callers */
                output_callers (p);
        }
-       fprintf (poutput, "Total number of calls: %" G_GINT64_FORMAT "\n", (long long)total_calls);
+       fprintf (poutput, "Total number of calls: %" G_GINT64_FORMAT "\n", (gint64)total_calls);
 }
 
 typedef struct {
@@ -1164,7 +1304,7 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
                MonoMethod *caller = prof->callers->method;
 
                /* Otherwise all allocations are attributed to icall_wrapper_mono_object_new */
-               if (caller->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
+               if (caller->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && prof->callers->next)
                        caller = prof->callers->next->method;
 
                if (!(profile_info = g_hash_table_lookup (prof->methods, caller)))
@@ -1309,7 +1449,7 @@ try_addr2line (const char* binary, gpointer ip)
 }
 
 static void
-stat_prof_report (void)
+stat_prof_report (MonoProfiler *prof)
 {
        MonoJitInfo *ji;
        int count = prof_counts;
@@ -1317,12 +1457,19 @@ stat_prof_report (void)
        char *mn;
        gpointer ip;
        GList *tmp, *sorted = NULL;
+       GSList *l;
        int pcount = ++ prof_counts;
 
        prof_counts = MAX_PROF_SAMPLES;
        for (i = 0; i < count; ++i) {
                ip = prof_addresses [i];
                ji = mono_jit_info_table_find (mono_domain_get (), ip);
+
+               if (!ji) {
+                       for (l = prof->domains; l && !ji; l = l->next)
+                               ji = mono_jit_info_table_find (l->data, ip);
+               }
+
                if (ji) {
                        mn = mono_method_full_name (ji->method, TRUE);
                } else {
@@ -1373,16 +1520,23 @@ stat_prof_report (void)
        g_list_free (sorted);
 }
 
+static void
+simple_appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result)
+{
+       prof->domains = g_slist_prepend (prof->domains, domain);
+}
+
 static void
 simple_appdomain_unload (MonoProfiler *prof, MonoDomain *domain)
 {
        /* FIXME: we should actually record partial data for each domain, 
-        * since the ip->ji->method mappings are going away at domain unload time.
+        * but at this point it's must easier using the new logging profiler.
         */
-       if (domain == mono_get_root_domain ())
-               stat_prof_report ();
+       mono_profiler_shutdown ();
 }
 
+static gint32 simple_shutdown_done = FALSE;
+
 static void
 simple_shutdown (MonoProfiler *prof)
 {
@@ -1390,7 +1544,24 @@ simple_shutdown (MonoProfiler *prof)
        MonoProfiler *tprof;
        GSList *tmp;
        char *str;
+       gint32 see_shutdown_done;
+
+#ifndef HOST_WIN32
+       mono_thread_attach(mono_get_root_domain());
+#endif
+
+       // Make sure we execute simple_shutdown only once
+       see_shutdown_done = InterlockedExchange(& simple_shutdown_done, TRUE);
+       if (see_shutdown_done)
+               return;
+
+       if (mono_profiler_events & MONO_PROFILE_STATISTICAL) {
+               stat_prof_report (prof);
+       }
 
+       // Stop all incoming events
+       mono_profiler_set_events (0);
+       
        for (tmp = prof->per_thread; tmp; tmp = tmp->next) {
                tprof = tmp->data;
                merge_thread_data (prof, tprof);
@@ -1440,12 +1611,14 @@ mono_profiler_install_simple (const char *desc)
                for (ptr = args; ptr && *ptr; ptr++) {
                        const char *arg = *ptr;
 
+                       // Alwais listen to appdomaon events to shutdown at the first unload
+                       flags |= MONO_PROFILE_APPDOMAIN_EVENTS;
                        if (!strcmp (arg, "time"))
                                flags |= MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_EXCEPTIONS;
                        else if (!strcmp (arg, "alloc"))
                                flags |= MONO_PROFILE_ALLOCATIONS;
                        else if (!strcmp (arg, "stat"))
-                               flags |= MONO_PROFILE_STATISTICAL | MONO_PROFILE_APPDOMAIN_EVENTS;
+                               flags |= MONO_PROFILE_STATISTICAL;
                        else if (!strcmp (arg, "jit"))
                                flags |= MONO_PROFILE_JIT_COMPILATION;
                        else if (strncmp (arg, "file=", 5) == 0) {
@@ -1478,7 +1651,7 @@ mono_profiler_install_simple (const char *desc)
        mono_profiler_install_exception (NULL, simple_method_leave, NULL);
        mono_profiler_install_jit_compile (simple_method_jit, simple_method_end_jit);
        mono_profiler_install_allocation (simple_allocation);
-       mono_profiler_install_appdomain (NULL, NULL, simple_appdomain_unload, NULL);
+       mono_profiler_install_appdomain (NULL, simple_appdomain_load, simple_appdomain_unload, NULL);
        mono_profiler_install_statistical (simple_stat_hit);
        mono_profiler_set_events (flags);
 }
@@ -1501,6 +1674,8 @@ typedef void (*ProfilerInitializer) (const char*);
 void 
 mono_profiler_load (const char *desc)
 {
+       mono_gc_base_init ();
+
 #ifndef DISABLE_PROFILER
        if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
                mono_profiler_install_simple (desc);
@@ -1512,7 +1687,7 @@ mono_profiler_load (const char *desc)
        }
 #endif
        {
-               MonoDl *pmodule;
+               MonoDl *pmodule = NULL;
                const char* col = strchr (desc, ':');
                char* libname;
                char* path;
@@ -1520,7 +1695,7 @@ mono_profiler_load (const char *desc)
                char *err;
                void *iter;
                if (col != NULL) {
-                       mname = g_memdup (desc, col - desc);
+                       mname = g_memdup (desc, col - desc + 1);
                        mname [col - desc] = 0;
                } else {
                        mname = g_strdup (desc);