e087be07eb604fbfa0dc320637dfec8bd333998c
[mono.git] / mono / metadata / profiler-private.h
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 #ifndef __MONO_PROFILER_PRIVATE_H__
8 #define __MONO_PROFILER_PRIVATE_H__
9
10 #define MONO_PROFILER_UNSTABLE_GC_ROOTS
11 #include <mono/metadata/profiler.h>
12 #include <mono/utils/mono-lazy-init.h>
13 #include <mono/utils/mono-os-mutex.h>
14 #include <mono/utils/mono-os-semaphore.h>
15
16 struct _MonoProfilerDesc {
17         MonoProfilerHandle next;
18         MonoProfiler *prof;
19         volatile gpointer coverage_filter;
20         volatile gpointer call_instrumentation_filter;
21
22 #define _MONO_PROFILER_EVENT(name) \
23         volatile gpointer name ## _cb;
24 #define MONO_PROFILER_EVENT_0(name, type) \
25         _MONO_PROFILER_EVENT(name)
26 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
27         _MONO_PROFILER_EVENT(name)
28 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
29         _MONO_PROFILER_EVENT(name)
30 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
31         _MONO_PROFILER_EVENT(name)
32 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
33         _MONO_PROFILER_EVENT(name)
34 #include <mono/metadata/profiler-events.h>
35 #undef MONO_PROFILER_EVENT_0
36 #undef MONO_PROFILER_EVENT_1
37 #undef MONO_PROFILER_EVENT_2
38 #undef MONO_PROFILER_EVENT_3
39 #undef MONO_PROFILER_EVENT_4
40 #undef _MONO_PROFILER_EVENT
41 };
42
43 typedef struct {
44         gboolean startup_done;
45         MonoProfilerHandle profilers;
46         mono_lazy_init_t coverage_status;
47         mono_mutex_t coverage_mutex;
48         GHashTable *coverage_hash;
49         MonoProfilerHandle sampling_owner;
50         MonoSemType sampling_semaphore;
51         MonoProfilerSampleMode sample_mode;
52         guint32 sample_freq;
53         gboolean allocations;
54
55 #define _MONO_PROFILER_EVENT(name) \
56         volatile gint32 name ## _count;
57 #define MONO_PROFILER_EVENT_0(name, type) \
58         _MONO_PROFILER_EVENT(name)
59 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
60         _MONO_PROFILER_EVENT(name)
61 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
62         _MONO_PROFILER_EVENT(name)
63 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
64         _MONO_PROFILER_EVENT(name)
65 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
66         _MONO_PROFILER_EVENT(name)
67 #include <mono/metadata/profiler-events.h>
68 #undef MONO_PROFILER_EVENT_0
69 #undef MONO_PROFILER_EVENT_1
70 #undef MONO_PROFILER_EVENT_2
71 #undef MONO_PROFILER_EVENT_3
72 #undef MONO_PROFILER_EVENT_4
73 #undef _MONO_PROFILER_EVENT
74 } MonoProfilerState;
75
76 extern MonoProfilerState mono_profiler_state;
77
78 typedef struct {
79         guint32 entries;
80         struct {
81                 guchar *cil_code;
82                 guint32 count;
83         } data [1];
84 } MonoProfilerCoverageInfo;
85
86 void mono_profiler_started (void);
87 void mono_profiler_cleanup (void);
88
89 static inline gboolean
90 mono_profiler_installed (void)
91 {
92         return !!mono_profiler_state.profilers;
93 }
94
95 MonoProfilerCoverageInfo *mono_profiler_coverage_alloc (MonoMethod *method, guint32 entries);
96 void mono_profiler_coverage_free (MonoMethod *method);
97
98 gboolean mono_profiler_should_instrument_method (MonoMethod *method, gboolean entry);
99
100 gboolean mono_profiler_sampling_enabled (void);
101 void mono_profiler_sampling_thread_post (void);
102 void mono_profiler_sampling_thread_wait (void);
103
104 static inline gboolean
105 mono_profiler_allocations_enabled (void)
106 {
107         return mono_profiler_state.allocations;
108 }
109
110 #define _MONO_PROFILER_EVENT(name, ...) \
111         void mono_profiler_raise_ ## name (__VA_ARGS__);
112 #define MONO_PROFILER_EVENT_0(name, type) \
113         _MONO_PROFILER_EVENT(name, void)
114 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
115         _MONO_PROFILER_EVENT(name, arg1_type arg1_name)
116 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
117         _MONO_PROFILER_EVENT(name, arg1_type arg1_name, arg2_type arg2_name)
118 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
119         _MONO_PROFILER_EVENT(name, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name)
120 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
121         _MONO_PROFILER_EVENT(name, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name)
122 #include <mono/metadata/profiler-events.h>
123 #undef MONO_PROFILER_EVENT_0
124 #undef MONO_PROFILER_EVENT_1
125 #undef MONO_PROFILER_EVENT_2
126 #undef MONO_PROFILER_EVENT_3
127 #undef MONO_PROFILER_EVENT_4
128 #undef _MONO_PROFILER_EVENT
129
130 // These are the macros the rest of the runtime should use.
131
132 #define MONO_PROFILER_ENABLED(name) \
133         G_UNLIKELY (mono_profiler_state.name ## _count)
134
135 #define MONO_PROFILER_RAISE(name, args) \
136         do { \
137                 if (MONO_PROFILER_ENABLED (name)) \
138                         mono_profiler_raise_ ## name args; \
139         } while (0)
140
141 #endif // __MONO_PROFILER_PRIVATE_H__