31ddd481dcb5923b7850e8c7b34e19d08efb337e
[mono.git] / mono / metadata / profiler-legacy.c
1 /*
2  * Licensed to the .NET Foundation under one or more agreements.
3  * The .NET Foundation licenses this file to you under the MIT license.
4  * See the LICENSE file in the project root for more information.
5  */
6
7 #include <mono/metadata/profiler-private.h>
8
9 /*
10  * The point of this file is to maintain compatibility with a few profiler API
11  * functions used by Xamarin.{Android,iOS,Mac} so that they keep working
12  * regardless of which system Mono version is used.
13  *
14  * TODO: Remove this some day if we're OK with breaking compatibility.
15  */
16
17 typedef void *MonoLegacyProfiler;
18
19 typedef void (*MonoProfileFunc) (MonoLegacyProfiler *prof);
20 typedef void (*MonoProfileThreadFunc) (MonoLegacyProfiler *prof, uintptr_t tid);
21 typedef void (*MonoProfileGCFunc) (MonoLegacyProfiler *prof, MonoProfilerGCEvent event, int generation);
22 typedef void (*MonoProfileGCResizeFunc) (MonoLegacyProfiler *prof, int64_t new_size);
23 typedef void (*MonoProfileJitResult) (MonoLegacyProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
24
25 struct _MonoProfiler {
26         MonoProfilerHandle handle;
27         MonoLegacyProfiler *profiler;
28         MonoProfileFunc shutdown_callback;
29         MonoProfileThreadFunc thread_start, thread_end;
30         MonoProfileGCFunc gc_event;
31         MonoProfileGCResizeFunc gc_heap_resize;
32         MonoProfileJitResult jit_end2;
33 };
34
35 static MonoProfiler *current;
36
37 MONO_API void mono_profiler_install (MonoLegacyProfiler *prof, MonoProfileFunc callback);
38 MONO_API void mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end);
39 MONO_API void mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback);
40 MONO_API void mono_profiler_install_jit_end (MonoProfileJitResult end);
41 MONO_API void mono_profiler_set_events (int flags);
42
43 static void
44 shutdown_cb (MonoProfiler *prof)
45 {
46         prof->shutdown_callback (prof->profiler);
47 }
48
49 void
50 mono_profiler_install (MonoLegacyProfiler *prof, MonoProfileFunc callback)
51 {
52         current = g_new0 (MonoProfiler, 1);
53         current->handle = mono_profiler_create (current);
54         current->profiler = prof;
55         current->shutdown_callback = callback;
56
57         if (callback)
58                 mono_profiler_set_runtime_shutdown_end_callback (current->handle, shutdown_cb);
59 }
60
61 static void
62 thread_start_cb (MonoProfiler *prof, uintptr_t tid)
63 {
64         prof->thread_start (prof->profiler, tid);
65 }
66
67 static void
68 thread_stop_cb (MonoProfiler *prof, uintptr_t tid)
69 {
70         prof->thread_end (prof->profiler, tid);
71 }
72
73 void
74 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
75 {
76         current->thread_start = start;
77         current->thread_end = end;
78
79         if (start)
80                 mono_profiler_set_thread_started_callback (current->handle, thread_start_cb);
81
82         if (end)
83                 mono_profiler_set_thread_stopped_callback (current->handle, thread_stop_cb);
84 }
85
86 static void
87 gc_event_cb (MonoProfiler *prof, MonoProfilerGCEvent event, uint32_t generation)
88 {
89         prof->gc_event (prof->profiler, event, generation);
90 }
91
92 static void
93 gc_resize_cb (MonoProfiler *prof, uintptr_t size)
94 {
95         prof->gc_heap_resize (prof->profiler, size);
96 }
97
98 void
99 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
100 {
101         current->gc_event = callback;
102         current->gc_heap_resize = heap_resize_callback;
103
104         if (callback)
105                 mono_profiler_set_gc_event_callback (current->handle, gc_event_cb);
106
107         if (heap_resize_callback)
108                 mono_profiler_set_gc_resize_callback (current->handle, gc_resize_cb);
109 }
110
111 static void
112 jit_done_cb (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo)
113 {
114         prof->jit_end2 (prof->profiler, method, jinfo, 0);
115 }
116
117 static void
118 jit_failed_cb (MonoProfiler *prof, MonoMethod *method)
119 {
120         prof->jit_end2 (prof->profiler, method, NULL, 1);
121 }
122
123 void
124 mono_profiler_install_jit_end (MonoProfileJitResult end)
125 {
126         current->jit_end2 = end;
127
128         if (end) {
129                 mono_profiler_set_jit_done_callback (current->handle, jit_done_cb);
130                 mono_profiler_set_jit_failed_callback (current->handle, jit_failed_cb);
131         }
132 }
133
134 void
135 mono_profiler_set_events (int flags)
136 {
137         /* Do nothing. */
138 }