[profiler] Rename mono_profiler_install to mono_profiler_create.
[mono.git] / mono / metadata / profiler.c
index a1e222fc1e66c2c8c39b26ac6bff50ab6c9aa88b..ccdb022a14fda51e6538da06f85329867d83e6eb 100644 (file)
@@ -11,6 +11,7 @@
 #include <mono/metadata/profiler-private.h>
 #include <mono/utils/mono-dl.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-logger-internals.h>
 
 MonoProfilerState mono_profiler_state;
 
@@ -20,24 +21,16 @@ typedef void (*MonoProfilerInitializer) (const char *);
 #define NEW_INITIALIZER_NAME "mono_profiler_init"
 
 static gboolean
-load_profiler (MonoDl *module, const char *desc, const char *suffix)
+load_profiler (MonoDl *module, const char *name, const char *desc)
 {
        if (!module)
                return FALSE;
 
-       char *old_name;
-
-       if (suffix)
-               old_name = g_strdup_printf (OLD_INITIALIZER_NAME "_%s", suffix);
-       else
-               old_name = g_strdup_printf (OLD_INITIALIZER_NAME);
-
+       char *err, *old_name = g_strdup_printf (OLD_INITIALIZER_NAME);
        MonoProfilerInitializer func;
 
-       char *err;
-
        if (!(err = mono_dl_symbol (module, old_name, (gpointer) &func))) {
-               g_warning ("Found old-style startup symbol %s; profiler has not been migrated to the new API.", old_name);
+               mono_profiler_printf_err ("Found old-style startup symbol '%s' for the '%s' profiler; it has not been migrated to the new API.", old_name, name);
                g_free (old_name);
                return FALSE;
        }
@@ -45,12 +38,7 @@ load_profiler (MonoDl *module, const char *desc, const char *suffix)
        g_free (err);
        g_free (old_name);
 
-       char *new_name;
-
-       if (suffix)
-               new_name = g_strdup_printf (NEW_INITIALIZER_NAME "_%s", suffix);
-       else
-               new_name = g_strdup_printf (NEW_INITIALIZER_NAME);
+       char *new_name = g_strdup_printf (NEW_INITIALIZER_NAME "_%s", name);
 
        if ((err = mono_dl_symbol (module, new_name, (gpointer *) &func))) {
                g_free (err);
@@ -66,7 +54,7 @@ load_profiler (MonoDl *module, const char *desc, const char *suffix)
 }
 
 static gboolean
-load_profiler_from_executable (const char *desc, const char *name)
+load_profiler_from_executable (const char *name, const char *desc)
 {
        char *err;
 
@@ -81,16 +69,16 @@ load_profiler_from_executable (const char *desc, const char *name)
        MonoDl *module = mono_dl_open (NULL, MONO_DL_EAGER, &err);
 
        if (!module) {
-               g_warning ("Could not open main executable (%s).", err);
+               mono_profiler_printf_err ("Could not open main executable: %s", err);
                g_free (err);
                return FALSE;
        }
 
-       return load_profiler (module, desc, name);
+       return load_profiler (module, name, desc);
 }
 
 static gboolean
-load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
+load_profiler_from_directory (const char *directory, const char *libname, const char *name, const char *desc)
 {
        char* path;
        void *iter = NULL;
@@ -102,14 +90,14 @@ load_profiler_from_directory (const char *directory, const char *libname, const
                g_free (path);
 
                if (module)
-                       return load_profiler (module, desc, NULL);
+                       return load_profiler (module, name, desc);
        }
 
        return FALSE;
 }
 
 static gboolean
-load_profiler_from_installation (const char *libname, const char *desc)
+load_profiler_from_installation (const char *libname, const char *name, const char *desc)
 {
        char *err;
        MonoDl *module = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
@@ -117,7 +105,7 @@ load_profiler_from_installation (const char *libname, const char *desc)
        g_free (err);
 
        if (module)
-               return load_profiler (module, desc, NULL);
+               return load_profiler (module, name, desc);
 
        return FALSE;
 }
@@ -125,8 +113,6 @@ load_profiler_from_installation (const char *libname, const char *desc)
 void
 mono_profiler_load (const char *desc)
 {
-       mono_gc_base_init ();
-
        if (!desc || !strcmp ("default", desc))
                desc = "log:report";
 
@@ -139,18 +125,18 @@ mono_profiler_load (const char *desc)
        } else
                mname = g_strdup (desc);
 
-       if (!load_profiler_from_executable (desc, mname)) {
+       if (!load_profiler_from_executable (mname, desc)) {
                char *libname = g_strdup_printf ("mono-profiler-%s", mname);
-               gboolean res = load_profiler_from_installation (libname, desc);
+               gboolean res = load_profiler_from_installation (libname, mname, desc);
 
                if (!res && mono_config_get_assemblies_dir ())
-                       res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
+                       res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, mname, desc);
 
                if (!res)
-                       res = load_profiler_from_directory (NULL, libname, desc);
+                       res = load_profiler_from_directory (NULL, libname, mname, desc);
 
                if (!res)
-                       g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
+                       mono_profiler_printf_err ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
 
                g_free (libname);
        }
@@ -159,7 +145,7 @@ mono_profiler_load (const char *desc)
 }
 
 MonoProfilerHandle
-mono_profiler_install (MonoProfiler *prof)
+mono_profiler_create (MonoProfiler *prof)
 {
        MonoProfilerHandle handle = g_new0 (struct _MonoProfilerDesc, 1);
 
@@ -326,7 +312,7 @@ mono_profiler_enable_sampling (MonoProfilerHandle handle)
 }
 
 mono_bool
-mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint64_t freq)
+mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq)
 {
        if (handle != mono_profiler_state.sampling_owner)
                return FALSE;
@@ -334,13 +320,13 @@ mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode
        mono_profiler_state.sample_mode = mode;
        mono_profiler_state.sample_freq = freq;
 
-       mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
+       mono_profiler_sampling_thread_post ();
 
        return TRUE;
 }
 
 mono_bool
-mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint64_t *freq)
+mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq)
 {
        if (mode)
                *mode = mono_profiler_state.sample_mode;
@@ -358,7 +344,13 @@ mono_profiler_sampling_enabled (void)
 }
 
 void
-mono_profiler_sampling_thread_sleep (void)
+mono_profiler_sampling_thread_post (void)
+{
+       mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
+}
+
+void
+mono_profiler_sampling_thread_wait (void)
 {
        mono_os_sem_wait (&mono_profiler_state.sampling_semaphore, MONO_SEM_FLAGS_NONE);
 }