[profiler] No longer use enter/leave instrumentation for code coverage.
[mono.git] / mono / profiler / log.h
1 #ifndef __MONO_PROFLOG_H__
2 #define __MONO_PROFLOG_H__
3
4 #include <glib.h>
5 #include <mono/metadata/profiler.h>
6
7 #define BUF_ID 0x4D504C01
8 #define LOG_HEADER_ID 0x4D505A01
9 #define LOG_VERSION_MAJOR 2
10 #define LOG_VERSION_MINOR 0
11 #define LOG_DATA_VERSION 14
12
13 /*
14  * Changes in major/minor versions:
15  * version 1.0: removed sysid field from header
16  *              added args, arch, os fields to header
17  *
18  * Changes in data versions:
19  * version 2: added offsets in heap walk
20  * version 3: added GC roots
21  * version 4: added sample/statistical profiling
22  * version 5: added counters sampling
23  * version 6: added optional backtrace in sampling info
24  * version 8: added TYPE_RUNTIME and JIT helpers/trampolines
25  * version 9: added MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
26  * version 10: added TYPE_COVERAGE
27  * version 11: added thread ID to TYPE_SAMPLE_HIT
28                added more load/unload events
29                    unload for class
30                    unload for image
31                    load/unload for appdomain
32                    load/unload for contexts
33                    load/unload/name for assemblies
34                removed TYPE_LOAD_ERR flag (profiler never generated it, now removed from the format itself)
35                added TYPE_GC_HANDLE_{CREATED,DESTROYED}_BT
36                TYPE_JIT events are no longer guaranteed to have code start/size info (can be zero)
37  * version 12: added MONO_COUNTER_PROFILER
38  * version 13: added MONO_GC_EVENT_{PRE_STOP_WORLD_LOCKED,POST_START_WORLD_UNLOCKED}
39                added TYPE_META + TYPE_SYNC_POINT
40                removed il and native offset in TYPE_SAMPLE_HIT
41                methods in backtraces are now encoded as proper method pointers
42                removed flags in backtrace format
43                removed flags in metadata events
44                changed the following fields to a single byte rather than leb128
45                    TYPE_GC_EVENT: event_type, generation
46                    TYPE_HEAP_ROOT: root_type
47                    TYPE_JITHELPER: type
48                    TYPE_SAMPLE_HIT: sample_type
49                    TYPE_CLAUSE: clause_type
50                    TYPE_SAMPLE_COUNTERS_DESC: type, unit, variance
51                    TYPE_SAMPLE_COUNTERS: type
52                added time fields to all events that were missing one
53                    TYPE_HEAP_OBJECT
54                    TYPE_HEAP_ROOT
55                    TYPE_SAMPLE_USYM
56                    TYPE_SAMPLE_COUNTERS_DESC
57                    TYPE_COVERAGE_METHOD
58                    TYPE_COVERAGE_STATEMENT
59                    TYPE_COVERAGE_CLASS
60                    TYPE_COVERAGE_ASSEMBLY
61                moved the time field in TYPE_SAMPLE_HIT to right after the event byte, now encoded as a regular time field
62                changed the time field in TYPE_SAMPLE_COUNTERS to be encoded as a regular time field (in nanoseconds)
63                added TYPE_GC_FINALIZE_{START,END,OBJECT_START,OBJECT_END}
64  * version 14: added event field to TYPE_MONITOR instead of encoding it in the extended info
65                all TYPE_MONITOR events can now contain backtraces
66                changed address field in TYPE_SAMPLE_UBIN to be based on ptr_base
67                added an image pointer field to assembly load events
68                added an exception object field to TYPE_CLAUSE
69                class unload events no longer exist (they were never emitted)
70  */
71
72 enum {
73         TYPE_ALLOC,
74         TYPE_GC,
75         TYPE_METADATA,
76         TYPE_METHOD,
77         TYPE_EXCEPTION,
78         TYPE_MONITOR,
79         TYPE_HEAP,
80         TYPE_SAMPLE,
81         TYPE_RUNTIME,
82         TYPE_COVERAGE,
83         TYPE_META,
84         /* extended type for TYPE_HEAP */
85         TYPE_HEAP_START  = 0 << 4,
86         TYPE_HEAP_END    = 1 << 4,
87         TYPE_HEAP_OBJECT = 2 << 4,
88         TYPE_HEAP_ROOT   = 3 << 4,
89         /* extended type for TYPE_METADATA */
90         TYPE_END_LOAD     = 2 << 4,
91         TYPE_END_UNLOAD   = 4 << 4,
92         /* extended type for TYPE_GC */
93         TYPE_GC_EVENT  = 1 << 4,
94         TYPE_GC_RESIZE = 2 << 4,
95         TYPE_GC_MOVE   = 3 << 4,
96         TYPE_GC_HANDLE_CREATED      = 4 << 4,
97         TYPE_GC_HANDLE_DESTROYED    = 5 << 4,
98         TYPE_GC_HANDLE_CREATED_BT   = 6 << 4,
99         TYPE_GC_HANDLE_DESTROYED_BT = 7 << 4,
100         TYPE_GC_FINALIZE_START = 8 << 4,
101         TYPE_GC_FINALIZE_END = 9 << 4,
102         TYPE_GC_FINALIZE_OBJECT_START = 10 << 4,
103         TYPE_GC_FINALIZE_OBJECT_END = 11 << 4,
104         /* extended type for TYPE_METHOD */
105         TYPE_LEAVE     = 1 << 4,
106         TYPE_ENTER     = 2 << 4,
107         TYPE_EXC_LEAVE = 3 << 4,
108         TYPE_JIT       = 4 << 4,
109         /* extended type for TYPE_EXCEPTION */
110         TYPE_THROW_NO_BT = 0 << 7,
111         TYPE_THROW_BT    = 1 << 7,
112         TYPE_CLAUSE      = 1 << 4,
113         /* extended type for TYPE_ALLOC */
114         TYPE_ALLOC_NO_BT  = 0 << 4,
115         TYPE_ALLOC_BT     = 1 << 4,
116         /* extended type for TYPE_MONITOR */
117         TYPE_MONITOR_NO_BT  = 0 << 7,
118         TYPE_MONITOR_BT     = 1 << 7,
119         /* extended type for TYPE_SAMPLE */
120         TYPE_SAMPLE_HIT           = 0 << 4,
121         TYPE_SAMPLE_USYM          = 1 << 4,
122         TYPE_SAMPLE_UBIN          = 2 << 4,
123         TYPE_SAMPLE_COUNTERS_DESC = 3 << 4,
124         TYPE_SAMPLE_COUNTERS      = 4 << 4,
125         /* extended type for TYPE_RUNTIME */
126         TYPE_JITHELPER = 1 << 4,
127         /* extended type for TYPE_COVERAGE */
128         TYPE_COVERAGE_ASSEMBLY = 0 << 4,
129         TYPE_COVERAGE_METHOD   = 1 << 4,
130         TYPE_COVERAGE_STATEMENT = 2 << 4,
131         TYPE_COVERAGE_CLASS = 3 << 4,
132         /* extended type for TYPE_META */
133         TYPE_SYNC_POINT = 0 << 4,
134         TYPE_END
135 };
136
137 enum {
138         /* metadata type byte for TYPE_METADATA */
139         TYPE_CLASS    = 1,
140         TYPE_IMAGE    = 2,
141         TYPE_ASSEMBLY = 3,
142         TYPE_DOMAIN   = 4,
143         TYPE_THREAD   = 5,
144         TYPE_CONTEXT  = 6,
145 };
146
147 typedef enum {
148         SYNC_POINT_PERIODIC,
149         SYNC_POINT_WORLD_STOP,
150         SYNC_POINT_WORLD_START
151 } MonoProfilerSyncPointType;
152
153 typedef enum {
154         MONO_PROFILER_MONITOR_CONTENTION = 1,
155         MONO_PROFILER_MONITOR_DONE = 2,
156         MONO_PROFILER_MONITOR_FAIL = 3,
157 } MonoProfilerMonitorEvent;
158
159 enum {
160         MONO_PROFILER_GC_HANDLE_CREATED,
161         MONO_PROFILER_GC_HANDLE_DESTROYED,
162 };
163
164 // Sampling sources
165 // Unless you have compiled with --enable-perf-events, only SAMPLE_CYCLES is available
166 enum {
167         SAMPLE_CYCLES = 1,
168         SAMPLE_INSTRUCTIONS,
169         SAMPLE_CACHE_MISSES,
170         SAMPLE_CACHE_REFS,
171         SAMPLE_BRANCHES,
172         SAMPLE_BRANCH_MISSES,
173         SAMPLE_LAST
174 };
175
176
177 // If you alter MAX_FRAMES, you may need to alter SAMPLE_BLOCK_SIZE too.
178 #define MAX_FRAMES 32
179
180 //The following flags control emitting individual events
181 #define PROFLOG_DOMAIN_EVENTS (1 << 0)
182 #define PROFLOG_ASSEMBLY_EVENTS (1 << 1)
183 #define PROFLOG_MODULE_EVENTS (1 << 2)
184 #define PROFLOG_CLASS_EVENTS (1 << 3)
185 #define PROFLOG_JIT_COMPILATION_EVENTS (1 << 4)
186 #define PROFLOG_EXCEPTION_EVENTS (1 << 5)
187 #define PROFLOG_ALLOCATION_EVENTS (1 << 6)
188 #define PROFLOG_GC_EVENTS (1 << 7)
189 #define PROFLOG_THREAD_EVENTS (1 << 8)
190 //This generate enter/leave events
191 #define PROFLOG_CALL_EVENTS (1 << 9)
192 #define PROFLOG_INS_COVERAGE_EVENTS (1 << 10)
193 #define PROFLOG_SAMPLING_EVENTS (1 << 11)
194 #define PROFLOG_MONITOR_EVENTS (1 << 12)
195 #define PROFLOG_GC_MOVES_EVENTS (1 << 13)
196
197 #define PROFLOG_GC_ROOT_EVENTS (1 << 14)
198 #define PROFLOG_CONTEXT_EVENTS (1 << 15)
199 #define PROFLOG_FINALIZATION_EVENTS (1 << 16)
200 #define PROFLOG_COUNTER_EVENTS (1 << 17)
201 #define PROFLOG_GC_HANDLE_EVENTS (1 << 18)
202
203 //The following flags control whole subsystems
204 //Enables code coverage generation
205 #define PROFLOG_CODE_COV_FEATURE (1 << 19)
206 //This enables sampling to be generated
207 #define PROFLOG_SAMPLING_FEATURE (1 << 20)
208 //This enable heap dumping during GCs and filter GCRoots and GCHandle events outside of the dumped collections
209 #define PROFLOG_HEAPSHOT_FEATURE (1 << 21)
210
211
212
213 //The follow flags are the common aliases we want ppl to use
214 #define PROFLOG_TYPELOADING_ALIAS (PROFLOG_DOMAIN_EVENTS | PROFLOG_ASSEMBLY_EVENTS | PROFLOG_MODULE_EVENTS | PROFLOG_CLASS_EVENTS)
215 #define PROFLOG_CODECOV_ALIAS (PROFLOG_GC_EVENTS | PROFLOG_THREAD_EVENTS | PROFLOG_INS_COVERAGE_EVENTS | PROFLOG_CODE_COV_FEATURE)
216 #define PROFLOG_PERF_SAMPLING_ALIAS (PROFLOG_TYPELOADING_ALIAS | PROFLOG_THREAD_EVENTS | PROFLOG_SAMPLING_EVENTS | PROFLOG_SAMPLING_FEATURE)
217 #define PROFLOG_GC_ALLOC_ALIAS (PROFLOG_TYPELOADING_ALIAS | PROFLOG_THREAD_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_ALLOCATION_EVENTS)
218 #define PROFLOG_HEAPSHOT_ALIAS (PROFLOG_TYPELOADING_ALIAS | PROFLOG_THREAD_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_HEAPSHOT_FEATURE)
219 #define PROFLOG_LEGACY_ALIAS (PROFLOG_TYPELOADING_ALIAS | PROFLOG_GC_EVENTS | PROFLOG_THREAD_EVENTS | PROFLOG_JIT_COMPILATION_EVENTS | PROFLOG_EXCEPTION_EVENTS | PROFLOG_MONITOR_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_CONTEXT_EVENTS | PROFLOG_FINALIZATION_EVENTS | PROFLOG_COUNTER_EVENTS)
220
221
222 typedef struct {
223         //Events explicitly enabled
224         int enable_mask;
225         //Events explicitly disabled
226         int disable_mask;
227
228         //Actual mask the profiler should use
229         int effective_mask;
230
231         //Emit a report at the end of execution
232         gboolean do_report;
233
234         //Enable profiler internal debugging
235         gboolean do_debug;
236
237         //Enable code coverage specific debugging
238         gboolean debug_coverage;
239
240         //Where to compress the output file
241         gboolean use_zip;
242
243         //If true, don't generate stacktraces
244         gboolean notraces;
245
246         //If true, heapshots are generated on demand only
247         gboolean hs_mode_ondemand;
248
249         //HeapShort frequency in milliseconds
250         unsigned int hs_mode_ms;
251
252         //HeapShort frequency in number of collections
253         unsigned int hs_mode_gc;
254
255         //Sample frequency in Hertz
256         int sample_freq;
257
258         //Maximum number of frames to collect
259         int num_frames;
260
261         //Max depth to record enter/leave events
262         int max_call_depth;
263
264         //Name of the generated mlpd file
265         const char *output_filename;
266
267         //Filter files used by the code coverage mode
268         GPtrArray *cov_filter_files;
269
270         //Port to listen for profiling commands
271         int command_port;
272
273         //Max size of the sample hit buffer, we'll drop frames if it's reached
274         int max_allocated_sample_hits;
275
276         MonoProfilerSampleMode sampling_mode;
277 } ProfilerConfig;
278
279 void proflog_parse_args (ProfilerConfig *config, const char *desc);
280
281 #endif /* __MONO_PROFLOG_H__ */