Merge pull request #1926 from alexrp/profiler-improvements
[mono.git] / mono / metadata / profiler.h
1 #ifndef __MONO_PROFILER_H__
2 #define __MONO_PROFILER_H__
3
4 #include <mono/metadata/object.h>
5 #include <mono/metadata/appdomain.h>
6
7 MONO_BEGIN_DECLS
8
9 #define MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH 128
10
11 typedef enum {
12         MONO_PROFILE_NONE = 0,
13         MONO_PROFILE_APPDOMAIN_EVENTS = 1 << 0,
14         MONO_PROFILE_ASSEMBLY_EVENTS  = 1 << 1,
15         MONO_PROFILE_MODULE_EVENTS    = 1 << 2,
16         MONO_PROFILE_CLASS_EVENTS     = 1 << 3,
17         MONO_PROFILE_JIT_COMPILATION  = 1 << 4,
18         MONO_PROFILE_INLINING         = 1 << 5,
19         MONO_PROFILE_EXCEPTIONS       = 1 << 6,
20         MONO_PROFILE_ALLOCATIONS      = 1 << 7,
21         MONO_PROFILE_GC               = 1 << 8,
22         MONO_PROFILE_THREADS          = 1 << 9,
23         MONO_PROFILE_REMOTING         = 1 << 10,
24         MONO_PROFILE_TRANSITIONS      = 1 << 11,
25         MONO_PROFILE_ENTER_LEAVE      = 1 << 12,
26         MONO_PROFILE_COVERAGE         = 1 << 13,
27         MONO_PROFILE_INS_COVERAGE     = 1 << 14,
28         MONO_PROFILE_STATISTICAL      = 1 << 15,
29         MONO_PROFILE_METHOD_EVENTS    = 1 << 16,
30         MONO_PROFILE_MONITOR_EVENTS   = 1 << 17,
31         MONO_PROFILE_IOMAP_EVENTS     = 1 << 18, /* this should likely be removed, too */
32         MONO_PROFILE_GC_MOVES         = 1 << 19,
33         MONO_PROFILE_GC_ROOTS         = 1 << 20,
34         MONO_PROFILE_CONTEXT_EVENTS   = 1 << 21
35 } MonoProfileFlags;
36
37 typedef enum {
38         MONO_PROFILE_OK,
39         MONO_PROFILE_FAILED
40 } MonoProfileResult;
41
42 typedef enum {
43         MONO_GC_EVENT_START,
44         MONO_GC_EVENT_MARK_START,
45         MONO_GC_EVENT_MARK_END,
46         MONO_GC_EVENT_RECLAIM_START,
47         MONO_GC_EVENT_RECLAIM_END,
48         MONO_GC_EVENT_END,
49         MONO_GC_EVENT_PRE_STOP_WORLD,
50         MONO_GC_EVENT_POST_STOP_WORLD,
51         MONO_GC_EVENT_PRE_START_WORLD,
52         MONO_GC_EVENT_POST_START_WORLD
53 } MonoGCEvent;
54
55 /* coverage info */
56 typedef struct {
57         MonoMethod *method;
58         int iloffset;
59         int counter;
60         const char *filename;
61         int line;
62         int col;
63 } MonoProfileCoverageEntry;
64
65 /* executable code buffer info */
66 typedef enum {
67         MONO_PROFILER_CODE_BUFFER_UNKNOWN,
68         MONO_PROFILER_CODE_BUFFER_METHOD,
69         MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE,
70         MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE,
71         MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE,
72         MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE,
73         MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
74         MONO_PROFILER_CODE_BUFFER_HELPER,
75         MONO_PROFILER_CODE_BUFFER_MONITOR,
76         MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE,
77         MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING,
78         MONO_PROFILER_CODE_BUFFER_LAST
79 } MonoProfilerCodeBufferType;
80
81 typedef struct _MonoProfiler MonoProfiler;
82
83 typedef enum {
84         MONO_PROFILER_MONITOR_CONTENTION = 1,
85         MONO_PROFILER_MONITOR_DONE = 2,
86         MONO_PROFILER_MONITOR_FAIL = 3
87 } MonoProfilerMonitorEvent;
88
89 typedef enum {
90         MONO_PROFILER_CALL_CHAIN_NONE = 0,
91         MONO_PROFILER_CALL_CHAIN_NATIVE = 1,
92         MONO_PROFILER_CALL_CHAIN_GLIBC = 2,
93         MONO_PROFILER_CALL_CHAIN_MANAGED = 3,
94         MONO_PROFILER_CALL_CHAIN_INVALID = 4
95 } MonoProfilerCallChainStrategy;
96
97 typedef enum {
98         MONO_PROFILER_GC_HANDLE_CREATED,
99         MONO_PROFILER_GC_HANDLE_DESTROYED
100 } MonoProfileGCHandleEvent;
101
102 typedef enum {
103         MONO_PROFILE_GC_ROOT_PINNING  = 1 << 8,
104         MONO_PROFILE_GC_ROOT_WEAKREF  = 2 << 8,
105         MONO_PROFILE_GC_ROOT_INTERIOR = 4 << 8,
106         /* the above are flags, the type is in the low 2 bytes */
107         MONO_PROFILE_GC_ROOT_STACK = 0,
108         MONO_PROFILE_GC_ROOT_FINALIZER = 1,
109         MONO_PROFILE_GC_ROOT_HANDLE = 2,
110         MONO_PROFILE_GC_ROOT_OTHER = 3,
111         MONO_PROFILE_GC_ROOT_MISC = 4, /* could be stack, handle, etc. */
112         MONO_PROFILE_GC_ROOT_TYPEMASK = 0xff
113 } MonoProfileGCRootType;
114
115 /*
116  * Functions that the runtime will call on the profiler.
117  */
118
119 typedef void (*MonoProfileFunc) (MonoProfiler *prof);
120
121 typedef void (*MonoProfileAppDomainFunc) (MonoProfiler *prof, MonoDomain   *domain);
122 typedef void (*MonoProfileContextFunc)   (MonoProfiler *prof, MonoAppContext *context);
123 typedef void (*MonoProfileMethodFunc)   (MonoProfiler *prof, MonoMethod   *method);
124 typedef void (*MonoProfileClassFunc)    (MonoProfiler *prof, MonoClass    *klass);
125 typedef void (*MonoProfileModuleFunc)   (MonoProfiler *prof, MonoImage    *module);
126 typedef void (*MonoProfileAssemblyFunc) (MonoProfiler *prof, MonoAssembly *assembly);
127 typedef void (*MonoProfileMonitorFunc)  (MonoProfiler *prof, MonoObject *obj, MonoProfilerMonitorEvent event);
128
129 typedef void (*MonoProfileExceptionFunc) (MonoProfiler *prof, MonoObject *object);
130 typedef void (*MonoProfileExceptionClauseFunc) (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num);
131
132 typedef void (*MonoProfileAppDomainResult)(MonoProfiler *prof, MonoDomain   *domain,   int result);
133 typedef void (*MonoProfileAppDomainFriendlyNameFunc) (MonoProfiler *prof, MonoDomain *domain, const char *name);
134 typedef void (*MonoProfileMethodResult)   (MonoProfiler *prof, MonoMethod   *method,   int result);
135 typedef void (*MonoProfileJitResult)      (MonoProfiler *prof, MonoMethod   *method,   MonoJitInfo* jinfo,   int result);
136 typedef void (*MonoProfileClassResult)    (MonoProfiler *prof, MonoClass    *klass,    int result);
137 typedef void (*MonoProfileModuleResult)   (MonoProfiler *prof, MonoImage    *module,   int result);
138 typedef void (*MonoProfileAssemblyResult) (MonoProfiler *prof, MonoAssembly *assembly, int result);
139
140 typedef void (*MonoProfileMethodInline)   (MonoProfiler *prof, MonoMethod   *parent, MonoMethod *child, int *ok);
141
142 typedef void (*MonoProfileThreadFunc)     (MonoProfiler *prof, uintptr_t tid);
143 typedef void (*MonoProfileThreadNameFunc) (MonoProfiler *prof, uintptr_t tid, const char *name);
144 typedef void (*MonoProfileAllocFunc)      (MonoProfiler *prof, MonoObject *obj, MonoClass *klass);
145 typedef void (*MonoProfileStatFunc)       (MonoProfiler *prof, mono_byte *ip, void *context);
146 typedef void (*MonoProfileStatCallChainFunc) (MonoProfiler *prof, int call_chain_depth, mono_byte **ip, void *context);
147 typedef void (*MonoProfileGCFunc)         (MonoProfiler *prof, MonoGCEvent event, int generation);
148 typedef void (*MonoProfileGCMoveFunc)     (MonoProfiler *prof, void **objects, int num);
149 typedef void (*MonoProfileGCResizeFunc)   (MonoProfiler *prof, int64_t new_size);
150 typedef void (*MonoProfileGCHandleFunc)   (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj);
151 typedef void (*MonoProfileGCRootFunc)     (MonoProfiler *prof, int num_roots, void **objects, int *root_types, uintptr_t *extra_info);
152
153 typedef void (*MonoProfileIomapFunc) (MonoProfiler *prof, const char *report, const char *pathname, const char *new_pathname);
154
155 typedef mono_bool (*MonoProfileCoverageFilterFunc)   (MonoProfiler *prof, MonoMethod *method);
156
157 typedef void (*MonoProfileCoverageFunc)   (MonoProfiler *prof, const MonoProfileCoverageEntry *entry);
158
159 typedef void (*MonoProfilerCodeChunkNew) (MonoProfiler *prof, void* chunk, int size);
160 typedef void (*MonoProfilerCodeChunkDestroy) (MonoProfiler *prof, void* chunk);
161 typedef void (*MonoProfilerCodeBufferNew) (MonoProfiler *prof, void* buffer, int size, MonoProfilerCodeBufferType type, void *data);
162
163 /*
164  * Function the profiler may call.
165  */
166 MONO_API void mono_profiler_install       (MonoProfiler *prof, MonoProfileFunc shutdown_callback);
167 MONO_API void mono_profiler_set_events    (MonoProfileFlags events);
168
169 MONO_API MonoProfileFlags mono_profiler_get_events (void);
170
171 MONO_API void mono_profiler_install_appdomain   (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
172                                         MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload);
173 MONO_API void mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb);
174 MONO_API void mono_profiler_install_context     (MonoProfileContextFunc load, MonoProfileContextFunc unload);
175 MONO_API void mono_profiler_install_assembly    (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
176                                         MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload);
177 MONO_API void mono_profiler_install_module      (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
178                                         MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload);
179 MONO_API void mono_profiler_install_class       (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
180                                         MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload);
181
182 MONO_API void mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end);
183 MONO_API void mono_profiler_install_jit_end (MonoProfileJitResult end);
184 MONO_API void mono_profiler_install_method_free (MonoProfileMethodFunc callback);
185 MONO_API void mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end);
186 MONO_API void mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave);
187 MONO_API void mono_profiler_install_thread      (MonoProfileThreadFunc start, MonoProfileThreadFunc end);
188 MONO_API void mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb);
189 MONO_API void mono_profiler_install_transition  (MonoProfileMethodResult callback);
190 MONO_API void mono_profiler_install_allocation  (MonoProfileAllocFunc callback);
191 MONO_API void mono_profiler_install_monitor     (MonoProfileMonitorFunc callback);
192 MONO_API void mono_profiler_install_statistical (MonoProfileStatFunc callback);
193 MONO_API void mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy);
194 MONO_API void mono_profiler_install_exception   (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback);
195 MONO_API void mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback);
196 MONO_API void mono_profiler_coverage_get  (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func);
197 MONO_API void mono_profiler_install_gc    (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback);
198 MONO_API void mono_profiler_install_gc_moves    (MonoProfileGCMoveFunc callback);
199 MONO_API void mono_profiler_install_gc_roots    (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback);
200 MONO_API void mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback);
201
202 MONO_API void mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback);
203 MONO_API void mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback);
204 MONO_API void mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback);
205
206 MONO_API void mono_profiler_install_iomap (MonoProfileIomapFunc callback);
207
208 MONO_API void mono_profiler_load             (const char *desc);
209
210 typedef enum {
211         /* Elapsed time is tracked by user+kernel time of the process - this is the default*/
212         MONO_PROFILER_STAT_MODE_PROCESS = 0,
213         /* Elapsed time is tracked by wallclock time */
214         MONO_PROFILER_STAT_MODE_REAL = 1,
215 } MonoProfileSamplingMode;
216
217 MONO_API void mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us);
218
219 MONO_END_DECLS
220
221 #endif /* __MONO_PROFILER_H__ */
222