2004-05-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mono / metadata / profiler.c
index eb913e6cd96b66526d603bac8f239d292dd68415..3743623259ae94f0d18ce8966fc02cec0d19a47f 100644 (file)
@@ -1,6 +1,8 @@
 
 #include "mono/metadata/profiler-private.h"
 #include "mono/metadata/debug-helpers.h"
+#include "mono/metadata/mono-debug.h"
+#include "mono/io-layer/io-layer.h"
 #include <string.h>
 #include <gmodule.h>
 
@@ -36,8 +38,12 @@ static MonoProfileMethodFunc   method_leave;
 static MonoProfileThreadFunc   thread_start;
 static MonoProfileThreadFunc   thread_end;
 
+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;
 
@@ -48,6 +54,7 @@ mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
                g_error ("profiler already setup");
        current_profiler = prof;
        shutdown_callback = callback;
+       InitializeCriticalSection (&profiler_coverage_mutex);
 }
 
 void
@@ -95,6 +102,12 @@ mono_profiler_install_allocation (MonoProfileAllocFunc callback)
        allocation_cb = callback;
 }
 
+void 
+mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
+{
+       coverage_filter_cb = callback;
+}
+
 void 
 mono_profiler_install_appdomain   (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
                                    MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
@@ -323,12 +336,94 @@ mono_profiler_shutdown (void)
                shutdown_callback (current_profiler);
 }
 
+static GHashTable *coverage_hash = NULL;
+
+MonoProfileCoverageInfo* 
+mono_profiler_coverage_alloc (MonoMethod *method, int entries)
+{
+       MonoProfileCoverageInfo *res;
+
+       if (coverage_filter_cb)
+               if (! (*coverage_filter_cb) (current_profiler, method))
+                       return NULL;
+
+       EnterCriticalSection (&profiler_coverage_mutex);
+       if (!coverage_hash)
+               coverage_hash = g_hash_table_new (NULL, NULL);
+
+       res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
+
+       res->entries = entries;
+
+       g_hash_table_insert (coverage_hash, method, res);
+       LeaveCriticalSection (&profiler_coverage_mutex);
+
+       return res;
+}
+
+/* safe only when the method antive code has been unloaded */
+void
+mono_profiler_coverage_free (MonoMethod *method)
+{
+       MonoProfileCoverageInfo* info;
+
+       EnterCriticalSection (&profiler_coverage_mutex);
+       if (!coverage_hash) {
+               LeaveCriticalSection (&profiler_coverage_mutex);
+               return;
+       }
+
+       info = g_hash_table_lookup (coverage_hash, method);
+       if (info) {
+               g_free (info);
+               g_hash_table_remove (coverage_hash, method);
+       }
+       LeaveCriticalSection (&profiler_coverage_mutex);
+}
+
+void 
+mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
+{
+       MonoProfileCoverageInfo* info;
+       int i, offset;
+       guint32 line, col;
+       unsigned char *start, *end, *cil_code;
+       MonoMethodHeader *header;
+       MonoProfileCoverageEntry entry;
+
+       EnterCriticalSection (&profiler_coverage_mutex);
+       info = g_hash_table_lookup (coverage_hash, method);
+       LeaveCriticalSection (&profiler_coverage_mutex);
+
+       if (!info)
+               return;
+
+       header = ((MonoMethodNormal *)method)->header;
+       start = (unsigned char*)header->code;
+       end = start + header->code_size;
+       for (i = 0; i < info->entries; ++i) {
+               cil_code = info->data [i].cil_code;
+               if (cil_code && cil_code >= start && cil_code < end) {
+                       offset = cil_code - start;
+                       entry.iloffset = offset;
+                       entry.method = method;
+                       entry.counter = info->data [i].count;
+                       /* the debug interface doesn't support column info, sigh */
+                       col = line = 1;
+                       entry.filename = mono_debug_source_location_from_il_offset (method, offset, &line);
+                       entry.line = line;
+                       entry.col = col;
+                       func (prof, &entry);
+               }
+       }
+}
+
 /*
  * Small profiler extracted from mint: we should move it in a loadable module
  * and improve it to do graphs and more accurate timestamping with rdtsc.
  */
 
-#define USE_X86TSC 1
+#define USE_X86TSC 0
 #define USE_WIN32COUNTER 0
 #if USE_X86TSC
 
@@ -511,7 +606,7 @@ create_profiler (void)
 {
        MonoProfiler *prof = g_new0 (MonoProfiler, 1);
 
-       prof->methods = g_hash_table_new (g_direct_hash, g_direct_equal);
+       prof->methods = g_hash_table_new (NULL, NULL);
        MONO_TIMER_INIT (prof->jit_timer);
        prof->mempool = mono_mempool_new ();
        return prof;
@@ -574,8 +669,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);
@@ -722,7 +821,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 */
@@ -764,7 +863,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;
                }
        }
 }
@@ -936,9 +1035,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);
@@ -948,7 +1072,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*);
@@ -963,6 +1087,7 @@ mono_profiler_load (const char *desc)
                GModule *pmodule;
                const char* col = strchr (desc, ':');
                char* libname;
+               char* path;
                char *mname;
                if (col != NULL) {
                        mname = g_memdup (desc, col - desc);
@@ -971,7 +1096,8 @@ mono_profiler_load (const char *desc)
                        mname = g_strdup (desc);
                }
                libname = g_strdup_printf ("mono-profiler-%s", mname);
-               pmodule = g_module_open (libname, G_MODULE_BIND_LAZY);
+               path = g_module_build_path (NULL, libname);
+               pmodule = g_module_open (path, G_MODULE_BIND_LAZY);
                if (pmodule) {
                        ProfilerInitializer func;
                        if (!g_module_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func)) {
@@ -980,11 +1106,12 @@ mono_profiler_load (const char *desc)
                                func (desc);
                        }
                } else {
-                       g_warning ("Error loading profiler module: %s", libname);
+                       g_warning ("Error loading profiler module '%s': %s", libname, g_module_error ());
                }
 
                g_free (libname);
                g_free (mname);
+               g_free (path);
        }
 }