This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / metadata / profiler.c
index 95ef185ecbccad0d034a56289b5b1097e74a3803..e31492883f046dd4166a2bfd90a30a75906c197b 100644 (file)
@@ -2,6 +2,8 @@
 #include "mono/metadata/profiler-private.h"
 #include "mono/metadata/debug-helpers.h"
 #include "mono/metadata/mono-debug.h"
+#include "mono/metadata/class-internals.h"
+#include "mono/io-layer/io-layer.h"
 #include <string.h>
 #include <gmodule.h>
 
@@ -41,6 +43,8 @@ static MonoProfileCoverageFilterFunc coverage_filter_cb;
 
 static MonoProfileFunc shutdown_callback;
 
+static CRITICAL_SECTION profiler_coverage_mutex;
+
 /* this is directly accessible to other mono libs. */
 MonoProfileFlags mono_profiler_events;
 
@@ -51,6 +55,7 @@ mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
                g_error ("profiler already setup");
        current_profiler = prof;
        shutdown_callback = callback;
+       InitializeCriticalSection (&profiler_coverage_mutex);
 }
 
 void
@@ -343,6 +348,7 @@ mono_profiler_coverage_alloc (MonoMethod *method, int entries)
                if (! (*coverage_filter_cb) (current_profiler, method))
                        return NULL;
 
+       EnterCriticalSection (&profiler_coverage_mutex);
        if (!coverage_hash)
                coverage_hash = g_hash_table_new (NULL, NULL);
 
@@ -351,6 +357,7 @@ mono_profiler_coverage_alloc (MonoMethod *method, int entries)
        res->entries = entries;
 
        g_hash_table_insert (coverage_hash, method, res);
+       LeaveCriticalSection (&profiler_coverage_mutex);
 
        return res;
 }
@@ -360,13 +367,19 @@ void
 mono_profiler_coverage_free (MonoMethod *method)
 {
        MonoProfileCoverageInfo* info;
-       if (!coverage_hash)
+
+       EnterCriticalSection (&profiler_coverage_mutex);
+       if (!coverage_hash) {
+               LeaveCriticalSection (&profiler_coverage_mutex);
                return;
+       }
+
        info = g_hash_table_lookup (coverage_hash, method);
-       if (!info)
-               return;
-       g_free (info);
-       g_hash_table_remove (coverage_hash, method);
+       if (info) {
+               g_free (info);
+               g_hash_table_remove (coverage_hash, method);
+       }
+       LeaveCriticalSection (&profiler_coverage_mutex);
 }
 
 void 
@@ -379,7 +392,10 @@ mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileC
        MonoMethodHeader *header;
        MonoProfileCoverageEntry entry;
 
+       EnterCriticalSection (&profiler_coverage_mutex);
        info = g_hash_table_lookup (coverage_hash, method);
+       LeaveCriticalSection (&profiler_coverage_mutex);
+
        if (!info)
                return;
 
@@ -654,8 +670,12 @@ output_profile (GList *funcs)
                if (!(gint)(p->total*1000))
                        continue;
                m = method_get_name (p->method);
-               printf ("########################\n% 8.3f %7llu % 8.3f  %s\n",
-                       (double)(p->total*1000), p->count, (double)(p->total*1000)/(double)p->count, m);
+               printf ("########################\n");
+               printf ("% 8.3f ", (double) (p->total * 1000));
+               printf ("%7llu ", p->count);
+               printf ("% 8.3f ", (double) (p->total * 1000)/(double)p->count);
+               printf ("  %s\n", m);
+
                g_free (m);
                /* callers */
                output_callers (p);
@@ -802,7 +822,7 @@ merge_methods (MonoMethod *method, MethodProfile *profile, MonoProfiler *prof)
        MethodProfile *mprof;
        AllocInfo *talloc_info, *alloc_info;
        CallerInfo *tcaller_info, *caller_info;
-       
+
        mprof = g_hash_table_lookup (prof->methods, method);
        if (!mprof) {
                /* the master thread didn't see this method, just transfer the info as is */
@@ -844,7 +864,7 @@ merge_methods (MonoMethod *method, MethodProfile *profile, MonoProfiler *prof)
                        caller_info = mono_mempool_alloc0 (prof->mempool, sizeof (CallerInfo));
                        *caller_info = *tcaller_info;
                        caller_info->next = mprof->caller_info;
-                       mprof->caller_info = caller_info->next;
+                       mprof->caller_info = caller_info;
                }
        }
 }
@@ -935,7 +955,13 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
 
        GET_THREAD_PROF (prof);
        if (prof->callers) {
-               if (!(profile_info = g_hash_table_lookup (prof->methods, prof->callers->method)))
+               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)
+                       caller = prof->callers->next->method;
+
+               if (!(profile_info = g_hash_table_lookup (prof->methods, caller)))
                        g_assert_not_reached ();
        } else {
                return; /* fine for now */
@@ -1016,9 +1042,34 @@ static void
 mono_profiler_install_simple (const char *desc)
 {
        MonoProfiler *prof;
+       gchar **args, **ptr;
+       MonoProfileFlags flags = MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_ALLOCATIONS;
 
        MONO_TIMER_STARTUP;
 
+       if (desc) {
+               /* Parse options */
+               if (strstr (desc, ":"))
+                       desc = strstr (desc, ":") + 1;
+               else
+                       desc = NULL;
+               args = g_strsplit (desc ? desc : "", ",", -1);
+
+               for (ptr = args; ptr && *ptr; ptr++) {
+                       const char *arg = *ptr;
+
+                       if (!strcmp (arg, "-time"))
+                               flags &= ~MONO_PROFILE_ENTER_LEAVE;
+                       else
+                          if (!strcmp (arg, "-alloc"))
+                                  flags &= ~MONO_PROFILE_ALLOCATIONS;
+                          else {
+                                  fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
+                                  return;
+                          }
+               }
+       }
+
        prof = create_profiler ();
        prof->tls_id = TlsAlloc ();
        TlsSetValue (prof->tls_id, prof);
@@ -1028,7 +1079,7 @@ mono_profiler_install_simple (const char *desc)
        mono_profiler_install_enter_leave (simple_method_enter, simple_method_leave);
        mono_profiler_install_jit_compile (simple_method_jit, simple_method_end_jit);
        mono_profiler_install_allocation (simple_allocation);
-       mono_profiler_set_events (MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_ALLOCATIONS);
+       mono_profiler_set_events (flags);
 }
 
 typedef void (*ProfilerInitializer) (const char*);