2009-05-25 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / profiler.c
index a07fb9510a6539cc9fa332e6c70e3d4304d76c88..ee5587fee31d23ccf3acb45c24502de04145d28a 100644 (file)
@@ -58,6 +58,7 @@ static MonoProfileJitResult    jit_end2;
 static MonoProfileMethodFunc   method_free;
 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;
@@ -201,6 +202,12 @@ 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)
 {
@@ -332,6 +339,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)
 {
@@ -789,6 +803,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;
@@ -1220,7 +1235,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)))
@@ -1365,7 +1380,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;
@@ -1373,12 +1388,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 {
@@ -1429,6 +1451,12 @@ 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)
 {
@@ -1459,7 +1487,7 @@ simple_shutdown (MonoProfiler *prof)
                return;
 
        if (mono_profiler_events & MONO_PROFILE_STATISTICAL) {
-               stat_prof_report ();
+               stat_prof_report (prof);
        }
 
        // Stop all incoming events
@@ -1554,7 +1582,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);
 }
@@ -1577,6 +1605,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);