2005-01-08 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mono / metadata / profiler.c
index 2a7498096a26e8f5511db451f4b328c071723df4..6dd31b9cccd743bb4d27f23a3457fcc6fb509e85 100644 (file)
@@ -1,7 +1,17 @@
 
+#include "config.h"
 #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/metadata/domain-internals.h"
+#include "mono/io-layer/io-layer.h"
 #include <string.h>
+#include <sys/time.h>
+#include <gmodule.h>
+#ifdef HAVE_BACKTRACE_SYMBOLS
+#include <execinfo.h>
+#endif
 
 static MonoProfiler * current_profiler = NULL;
 
@@ -29,14 +39,19 @@ static MonoProfileMethodFunc   jit_start;
 static MonoProfileMethodResult jit_end;
 static MonoProfileMethodResult man_unman_transition;
 static MonoProfileAllocFunc    allocation_cb;
+static MonoProfileStatFunc     statistical_cb;
 static MonoProfileMethodFunc   method_enter;
 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;
 
@@ -47,6 +62,7 @@ mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
                g_error ("profiler already setup");
        current_profiler = prof;
        shutdown_callback = callback;
+       InitializeCriticalSection (&profiler_coverage_mutex);
 }
 
 void
@@ -62,10 +78,10 @@ mono_profiler_get_events (void)
 }
 
 void
-mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc leave)
+mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
 {
        method_enter = enter;
-       method_leave = leave;
+       method_leave = fleave;
 }
 
 void 
@@ -94,6 +110,18 @@ mono_profiler_install_allocation (MonoProfileAllocFunc callback)
        allocation_cb = callback;
 }
 
+void 
+mono_profiler_install_statistical (MonoProfileStatFunc callback)
+{
+       statistical_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)
@@ -177,6 +205,13 @@ mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
                allocation_cb (current_profiler, obj, klass);
 }
 
+void
+mono_profiler_stat_hit (guchar *ip, void *context)
+{
+       if ((mono_profiler_events & MONO_PROFILE_STATISTICAL) && statistical_cb)
+               statistical_cb (current_profiler, ip, context);
+}
+
 void
 mono_profiler_thread_start (guint32 tid)
 {
@@ -322,6 +357,89 @@ 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 = mono_method_get_header (method);
+       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);
+               }
+       }
+}
+
+#ifndef DISABLE_PROFILER
 /*
  * 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.
@@ -448,22 +566,28 @@ timeval_elapsed (MonoGLibTimer *t)
 
 typedef struct _AllocInfo AllocInfo;
 typedef struct _CallerInfo CallerInfo;
+typedef struct _LastCallerInfo LastCallerInfo;
 
 struct _MonoProfiler {
        GHashTable *methods;
+       MonoMemPool *mempool;
+       /* info about JIT time */
        MONO_TIMER_TYPE jit_timer;
        double      jit_time;
+       double      max_jit_time;
+       MonoMethod *max_jit_method;
        int         methods_jitted;
-       GSList     *callers;
-       /* allocations unassigned to an IL method */
-       AllocInfo  *alloc_info;
+       
+       GSList     *per_thread;
+       
+       /* chain of callers for the current thread */
+       LastCallerInfo *callers;
+       /* LastCallerInfo nodes for faster allocation */
+       LastCallerInfo *cstorage;
 };
 
 typedef struct {
-       union {
-               MONO_TIMER_TYPE timer;
-               MonoMethod *method;
-       } u;
+       MonoMethod *method;
        guint64 count;
        double total;
        AllocInfo *alloc_info;
@@ -491,6 +615,50 @@ struct _CallerInfo {
        guint count;
 };
 
+struct _LastCallerInfo {
+       LastCallerInfo *next;
+       MonoMethod *method;
+       MONO_TIMER_TYPE timer;
+};
+
+static MonoProfiler*
+create_profiler (void)
+{
+       MonoProfiler *prof = g_new0 (MonoProfiler, 1);
+
+       prof->methods = g_hash_table_new (NULL, NULL);
+       MONO_TIMER_INIT (prof->jit_timer);
+       prof->mempool = mono_mempool_new ();
+       return prof;
+}
+#if 1
+
+#ifdef HAVE_KW_THREAD
+       static __thread MonoProfiler * tls_profiler;
+#      define GET_PROFILER() tls_profiler
+#      define SET_PROFILER(x) tls_profiler = (x)
+#      define ALLOC_PROFILER() /* nop */
+#else
+       static guint32 profiler_thread_id = -1;
+#      define GET_PROFILER() ((MonoProfiler *)TlsGetValue (profiler_thread_id))
+#      define SET_PROFILER(x) TlsSetValue (profiler_thread_id, x);
+#      define ALLOC_PROFILER() profiler_thread_id = TlsAlloc ()
+#endif
+
+#define GET_THREAD_PROF(prof) do {                                                           \
+               MonoProfiler *_tprofiler = GET_PROFILER ();                                  \
+               if (!_tprofiler) {                                                           \
+                       _tprofiler = create_profiler ();                                     \
+                       prof->per_thread = g_slist_prepend (prof->per_thread, _tprofiler);   \
+                       SET_PROFILER (_tprofiler);                                           \
+               }                                                                            \
+               prof = _tprofiler;                                                           \
+       } while (0)
+#else
+/* thread unsafe but faster variant */
+#define GET_THREAD_PROF(prof)
+#endif
+
 static gint
 compare_profile (MethodProfile *profa, MethodProfile *profb)
 {
@@ -500,8 +668,7 @@ compare_profile (MethodProfile *profa, MethodProfile *profb)
 static void
 build_profile (MonoMethod *m, MethodProfile *prof, GList **funcs)
 {
-       MONO_TIMER_DESTROY (prof->u.timer);
-       prof->u.method = m;
+       prof->method = m;
        *funcs = g_list_insert_sorted (*funcs, prof, (GCompareFunc)compare_profile);
 }
 
@@ -534,9 +701,13 @@ output_profile (GList *funcs)
                total_calls += p->count;
                if (!(gint)(p->total*1000))
                        continue;
-               m = method_get_name (p->u.method);
-               printf ("########################\n% 8.3f %7llu % 8.3f  %s\n",
-                       (double)(p->total*1000), p->count, (double)(p->total*1000)/(double)p->count, m);
+               m = method_get_name (p->method);
+               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);
@@ -652,7 +823,7 @@ output_newobj_profile (GList *proflist)
                if (p->count < 50000)
                        continue;
                mp = p->mp;
-               m = method_get_name (mp->u.method);
+               m = method_get_name (mp->method);
                g_print ("########################\n%8d KB %s\n", p->count / 1024, m);
                g_free (m);
                sorted = sort_alloc_list (mp->alloc_info);
@@ -677,45 +848,135 @@ output_newobj_profile (GList *proflist)
        g_print ("Total memory allocated: %d KB\n", total / 1024);
 }
 
+static void
+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 */
+               g_hash_table_insert (prof->methods, method, profile);
+               return;
+       }
+       /* merge the info from profile into mprof */
+       mprof->count += profile->count;
+       mprof->total += profile->total;
+       /* merge alloc info */
+       for (talloc_info = profile->alloc_info; talloc_info; talloc_info = talloc_info->next) {
+               for (alloc_info = mprof->alloc_info; alloc_info; alloc_info = alloc_info->next) {
+                       if (alloc_info->klass == talloc_info->klass) {
+                               /* mprof already has a record for the klass, merge */
+                               alloc_info->count += talloc_info->count;
+                               alloc_info->mem += talloc_info->mem;
+                               break;
+                       }
+               }
+               if (!alloc_info) {
+                       /* mprof didn't have the info, just copy it over */
+                       alloc_info = mono_mempool_alloc0 (prof->mempool, sizeof (AllocInfo));
+                       *alloc_info = *talloc_info;
+                       alloc_info->next = mprof->alloc_info;
+                       mprof->alloc_info = alloc_info->next;
+               }
+       }
+       /* merge callers info */
+       for (tcaller_info = profile->caller_info; tcaller_info; tcaller_info = tcaller_info->next) {
+               for (caller_info = mprof->caller_info; caller_info; caller_info = caller_info->next) {
+                       if (caller_info->caller == tcaller_info->caller) {
+                               /* mprof already has a record for the caller method, merge */
+                               caller_info->count += tcaller_info->count;
+                               break;
+                       }
+               }
+               if (!caller_info) {
+                       /* mprof didn't have the info, just copy it over */
+                       caller_info = mono_mempool_alloc0 (prof->mempool, sizeof (CallerInfo));
+                       *caller_info = *tcaller_info;
+                       caller_info->next = mprof->caller_info;
+                       mprof->caller_info = caller_info;
+               }
+       }
+}
+
+static void
+merge_thread_data (MonoProfiler *master, MonoProfiler *tprof)
+{
+       master->jit_time += tprof->jit_time;
+       master->methods_jitted += tprof->methods_jitted;
+       if (master->max_jit_time < tprof->max_jit_time) {
+               master->max_jit_time = tprof->max_jit_time;
+               master->max_jit_method = tprof->max_jit_method;
+       }
+
+       g_hash_table_foreach (tprof->methods, (GHFunc)merge_methods, master);
+}
+
 static void
 simple_method_enter (MonoProfiler *prof, MonoMethod *method)
 {
        MethodProfile *profile_info;
+       LastCallerInfo *callinfo;
+       GET_THREAD_PROF (prof);
+       /*g_print ("enter %p %s::%s in %d (%p)\n", method, method->klass->name, method->name, GetCurrentThreadId (), prof);*/
        if (!(profile_info = g_hash_table_lookup (prof->methods, method))) {
-               profile_info = g_new0 (MethodProfile, 1);
+               profile_info = mono_mempool_alloc0 (prof->mempool, sizeof (MethodProfile));
                MONO_TIMER_INIT (profile_info->u.timer);
                g_hash_table_insert (prof->methods, method, profile_info);
        }
        profile_info->count++;
-       MONO_TIMER_START (profile_info->u.timer);
        if (prof->callers) {
                CallerInfo *cinfo;
-               MonoMethod *caller = prof->callers->data;
+               MonoMethod *caller = prof->callers->method;
                for (cinfo = profile_info->caller_info; cinfo; cinfo = cinfo->next) {
                        if (cinfo->caller == caller)
                                break;
                }
                if (!cinfo) {
-                       cinfo = g_new0 (CallerInfo, 1);
+                       cinfo = mono_mempool_alloc0 (prof->mempool, sizeof (CallerInfo));
                        cinfo->caller = caller;
                        cinfo->next = profile_info->caller_info;
                        profile_info->caller_info = cinfo;
                }
                cinfo->count++;
        }
-       prof->callers = g_slist_prepend (prof->callers, method);
+       if (!(callinfo = prof->cstorage)) {
+               callinfo = mono_mempool_alloc (prof->mempool, sizeof (LastCallerInfo));
+               MONO_TIMER_INIT (callinfo->timer);
+       } else {
+               prof->cstorage = prof->cstorage->next;
+       }
+       callinfo->method = method;
+       callinfo->next = prof->callers;
+       prof->callers = callinfo;
+       MONO_TIMER_START (callinfo->timer);
 }
 
 static void
 simple_method_leave (MonoProfiler *prof, MonoMethod *method)
 {
        MethodProfile *profile_info;
-       if (!(profile_info = g_hash_table_lookup (prof->methods, method)))
-               g_assert_not_reached ();
-
-       MONO_TIMER_STOP (profile_info->u.timer);
-       profile_info->total += MONO_TIMER_ELAPSED (profile_info->u.timer);
-       prof->callers = g_slist_remove (prof->callers, method);
+       LastCallerInfo *callinfo, *newcallinfo = NULL;
+       
+       GET_THREAD_PROF (prof);
+       /*g_print ("leave %p %s::%s in %d (%p)\n", method, method->klass->name, method->name, GetCurrentThreadId (), prof);*/
+       callinfo = prof->callers;
+       /* should really not happen, but we don't catch exceptions events, yet ... */
+       while (callinfo) {
+               MONO_TIMER_STOP (callinfo->timer);
+               profile_info = g_hash_table_lookup (prof->methods, callinfo->method);
+               if (profile_info)
+                       profile_info->total += MONO_TIMER_ELAPSED (callinfo->timer);
+               newcallinfo = callinfo->next;
+               callinfo->next = prof->cstorage;
+               prof->cstorage = callinfo;
+               if (callinfo->method == method)
+                       break;
+               callinfo = newcallinfo;
+       }
+       prof->callers = newcallinfo;
 }
 
 static void
@@ -724,8 +985,15 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
        MethodProfile *profile_info;
        AllocInfo *tmp;
 
+       GET_THREAD_PROF (prof);
        if (prof->callers) {
-               if (!(profile_info = g_hash_table_lookup (prof->methods, prof->callers->data)))
+               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 */
@@ -736,7 +1004,7 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
                        break;
        }
        if (!tmp) {
-               tmp = g_new0 (AllocInfo, 1);
+               tmp = mono_mempool_alloc0 (prof->mempool, sizeof (AllocInfo));
                tmp->klass = klass;
                tmp->next = profile_info->alloc_info;
                profile_info->alloc_info = tmp;
@@ -754,6 +1022,7 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
 static void
 simple_method_jit (MonoProfiler *prof, MonoMethod *method)
 {
+       GET_THREAD_PROF (prof);
        prof->methods_jitted++;
        MONO_TIMER_START (prof->jit_timer);
 }
@@ -761,16 +1030,198 @@ simple_method_jit (MonoProfiler *prof, MonoMethod *method)
 static void
 simple_method_end_jit (MonoProfiler *prof, MonoMethod *method, int result)
 {
+       double jtime;
+       GET_THREAD_PROF (prof);
        MONO_TIMER_STOP (prof->jit_timer);
-       prof->jit_time += MONO_TIMER_ELAPSED (prof->jit_timer);
+       jtime = MONO_TIMER_ELAPSED (prof->jit_timer);
+       prof->jit_time += jtime;
+       if (jtime > prof->max_jit_time) {
+               prof->max_jit_time = jtime;
+               prof->max_jit_method = method;
+       }
+}
+
+/* about 10 minutes of samples */
+#define MAX_PROF_SAMPLES (1000*60*10)
+static int prof_counts = 0;
+static int prof_ucounts = 0;
+static gpointer* prof_addresses = NULL;
+static GHashTable *prof_table = NULL;
+
+static void
+simple_stat_hit (MonoProfiler *prof, guchar *ip, void *context)
+{
+       int pos;
+
+       if (prof_counts >= MAX_PROF_SAMPLES)
+               return;
+       pos = InterlockedIncrement (&prof_counts);
+       prof_addresses [pos - 1] = ip;
+}
+
+static int
+compare_methods_prof (gconstpointer a, gconstpointer b)
+{
+       int ca = GPOINTER_TO_UINT (g_hash_table_lookup (prof_table, a));
+       int cb = GPOINTER_TO_UINT (g_hash_table_lookup (prof_table, b));
+       return cb-ca;
+}
+
+static void
+prof_foreach (char *method, gpointer c, gpointer data)
+{
+       GList **list = data;
+       *list = g_list_insert_sorted (*list, method, compare_methods_prof);
+}
+
+typedef struct Addr2LineData Addr2LineData;
+
+struct Addr2LineData {
+       Addr2LineData *next;
+       FILE *pipein;
+       FILE *pipeout;
+       char *binary;
+       int child_pid;
+};
+
+static Addr2LineData *addr2line_pipes = NULL;
+
+static char*
+try_addr2line (const char* binary, gpointer ip)
+{
+       char buf [1024];
+       char *res;
+       Addr2LineData *addr2line;
+
+       for (addr2line = addr2line_pipes; addr2line; addr2line = addr2line->next) {
+               if (strcmp (binary, addr2line->binary) == 0)
+                       break;
+       }
+       if (!addr2line) {
+               const char *addr_argv[] = {"addr2line", "-f", "-e", binary, NULL};
+               int child_pid;
+               int ch_in, ch_out;
+               if (!g_spawn_async_with_pipes (NULL, (char**)addr_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
+                               &child_pid, &ch_in, &ch_out, NULL, NULL)) {
+                       return g_strdup (binary);
+               }
+               addr2line = g_new0 (Addr2LineData, 1);
+               addr2line->child_pid = child_pid;
+               addr2line->binary = g_strdup (binary);
+               addr2line->pipein = fdopen (ch_in, "w");
+               addr2line->pipeout = fdopen (ch_out, "r");
+               addr2line->next = addr2line_pipes;
+               addr2line_pipes = addr2line;
+       }
+       fprintf (addr2line->pipein, "%p\n", ip);
+       fflush (addr2line->pipein);
+       /* we first get the func name and then file:lineno in a second line */
+       if (fgets (buf, sizeof (buf), addr2line->pipeout) && buf [0] != '?') {
+               char *end = strchr (buf, '\n');
+               if (end)
+                       *end = 0;
+               res = g_strdup_printf ("%s(%s", binary, buf);
+               /* discard the filename/line info */
+               fgets (buf, sizeof (buf), addr2line->pipeout);
+       } else {
+               res = g_strdup (binary);
+       }
+       return res;
+}
+
+static void
+stat_prof_report (void)
+{
+       MonoJitInfo *ji;
+       int count = prof_counts;
+       int i, c;
+       char *mn;
+       gpointer ip;
+       GList *tmp, *sorted = NULL;
+
+       prof_counts ++;
+       for (i = 0; i < count; ++i) {
+               ip = prof_addresses [i];
+               ji = mono_jit_info_table_find (mono_domain_get (), ip);
+               if (ji) {
+                       mn = mono_method_full_name (ji->method, TRUE);
+               } else {
+#ifdef HAVE_BACKTRACE_SYMBOLS
+                       char **names;
+                       char *send;
+                       int no_func;
+                       prof_ucounts++;
+                       names = backtrace_symbols (&ip, 1);
+                       send = strchr (names [0], '+');
+                       if (send) {
+                               *send = 0;
+                               no_func = 0;
+                       } else {
+                               no_func = 1;
+                       }
+                       send = strchr (names [0], '[');
+                       if (send)
+                               *send = 0;
+                       if (no_func && names [0][0]) {
+                               char *endp = strchr (names [0], 0);
+                               while (--endp >= names [0] && g_ascii_isspace (*endp))
+                                       *endp = 0;
+                               mn = try_addr2line (names [0], ip);
+                       } else {
+                               mn = g_strdup (names [0]);
+                       }
+                       free (names);
+#else
+                       prof_ucounts++;
+                       mn = g_strdup_printf ("unmanaged [%p]", ip);
+#endif
+               }
+               c = GPOINTER_TO_UINT (g_hash_table_lookup (prof_table, mn));
+               c++;
+               g_hash_table_insert (prof_table, mn, GUINT_TO_POINTER (c));
+               if (c > 1)
+                       g_free (mn);
+       }
+       g_print ("prof counts: total/unmanaged: %d/%d\n", prof_counts, prof_ucounts);
+       g_hash_table_foreach (prof_table, (GHFunc)prof_foreach, &sorted);
+       for (tmp = sorted; tmp; tmp = tmp->next) {
+               double perc;
+               c = GPOINTER_TO_UINT (g_hash_table_lookup (prof_table, tmp->data));
+               perc = c*100.0/count;
+               g_print ("%7d\t%5.2f %% %s\n", c, perc, (char*)tmp->data);
+       }
+       g_list_free (sorted);
+}
+
+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.
+        */
+       if (domain == mono_get_root_domain ())
+               stat_prof_report ();
 }
 
 static void
 simple_shutdown (MonoProfiler *prof)
 {
        GList *profile = NULL;
+       MonoProfiler *tprof;
+       GSList *tmp;
+       char *str;
+
+       for (tmp = prof->per_thread; tmp; tmp = tmp->next) {
+               tprof = tmp->data;
+               merge_thread_data (prof, tprof);
+       }
 
        printf("Total time spent compiling %d methods (sec): %.4g\n", prof->methods_jitted, prof->jit_time);
+       if (prof->max_jit_method) {
+               str = method_get_name (prof->max_jit_method);
+               printf("Slowest method to compile (sec): %.4g: %s\n", prof->max_jit_time, str);
+               g_free (str);
+       }
        g_hash_table_foreach (prof->methods, (GHFunc)build_profile, &profile);
        output_profile (profile);
        g_list_free (profile);
@@ -779,23 +1230,108 @@ simple_shutdown (MonoProfiler *prof)
        g_hash_table_foreach (prof->methods, (GHFunc)build_newobj_profile, &profile);
        output_newobj_profile (profile);
        g_list_free (profile);
+
+       g_free (prof_addresses);
+       g_hash_table_destroy (prof_table);
 }
 
-void
-mono_profiler_install_simple (void)
+static void
+mono_profiler_install_simple (const char *desc)
 {
-       MonoProfiler *prof = g_new0 (MonoProfiler, 1);
+       MonoProfiler *prof;
+       gchar **args, **ptr;
+       MonoProfileFlags flags = MONO_PROFILE_JIT_COMPILATION;
 
        MONO_TIMER_STARTUP;
 
-       prof->methods = g_hash_table_new (g_direct_hash, g_direct_equal);
-       MONO_TIMER_INIT (prof->jit_timer);
+       if (desc) {
+               /* Parse options */
+               if (strstr (desc, ":"))
+                       desc = strstr (desc, ":") + 1;
+               else
+                       desc = "alloc,time";
+               args = g_strsplit (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 if (!strcmp (arg, "stat"))
+                               flags |= MONO_PROFILE_STATISTICAL | MONO_PROFILE_APPDOMAIN_EVENTS;
+                       else {
+                               fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
+                               return;
+                       }
+               }
+       }
+
+       prof = create_profiler ();
+       ALLOC_PROFILER ();
+       SET_PROFILER (prof);
+
+       /* statistical profiler data */
+       prof_addresses = g_new0 (gpointer, MAX_PROF_SAMPLES);
+       prof_table = g_hash_table_new (g_str_hash, g_str_equal);
 
        mono_profiler_install (prof, simple_shutdown);
-       /* later do also object creation */
        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_install_appdomain (NULL, NULL, simple_appdomain_unload, NULL);
+       mono_profiler_install_statistical (simple_stat_hit);
+       mono_profiler_set_events (flags);
+}
+
+#endif /* DISABLE_PROFILER */
+
+typedef void (*ProfilerInitializer) (const char*);
+#define INITIALIZER_NAME "mono_profiler_startup"
+
+void 
+mono_profiler_load (const char *desc)
+{
+#ifndef DISABLE_PROFILER
+       if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
+               mono_profiler_install_simple (desc);
+               return;
+       }
+#else
+       if (!desc) {
+               desc = "default";
+       }
+#endif
+       {
+               GModule *pmodule;
+               const char* col = strchr (desc, ':');
+               char* libname;
+               char* path;
+               char *mname;
+               if (col != NULL) {
+                       mname = g_memdup (desc, col - desc);
+                       mname [col - desc] = 0;
+               } else {
+                       mname = g_strdup (desc);
+               }
+               libname = g_strdup_printf ("mono-profiler-%s", mname);
+               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)) {
+                               g_warning ("Cannot find initializer function %s in profiler module: %s", INITIALIZER_NAME, libname);
+                       } else {
+                               func (desc);
+                       }
+               } else {
+                       g_warning ("Error loading profiler module '%s': %s", libname, g_module_error ());
+               }
+
+               g_free (libname);
+               g_free (mname);
+               g_free (path);
+       }
 }