2 * proflog.c: mono log profiler
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
11 #include <mono/metadata/profiler.h>
12 #include <mono/metadata/threads.h>
13 #include <mono/metadata/mono-gc.h>
14 #include <mono/metadata/debug-helpers.h>
23 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
24 #define DISABLE_HELPER_THREAD 1
33 #ifdef HAVE_EXECINFO_H
40 #ifndef DISABLE_HELPER_THREAD
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <sys/select.h>
53 #ifdef HAVE_SYS_STAT_H
60 #if defined (HAVE_SYS_ZLIB)
64 /* the architecture needs a memory fence */
65 #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
66 #include "perf_event.h"
67 #define USE_PERF_EVENTS 1
68 static int read_perf_mmap (MonoProfiler
* prof
);
71 #define BUFFER_SIZE (4096 * 16)
72 static int nocalls
= 0;
73 static int notraces
= 0;
74 static int use_zip
= 0;
75 static int do_report
= 0;
76 static int do_heap_shot
= 0;
77 static int max_call_depth
= 100;
78 static int runtime_inited
= 0;
79 static int command_port
= 0;
80 static int heapshot_requested
= 0;
81 static int sample_type
= 0;
82 static int sample_freq
= 0;
83 static int do_mono_sample
= 0;
84 static int in_shutdown
= 0;
85 static int do_debug
= 0;
87 /* For linux compile with:
88 * gcc -fPIC -shared -o libmono-profiler-log.so proflog.c utils.c -Wall -g -lz `pkg-config --cflags --libs mono-2`
89 * gcc -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
91 * For osx compile with:
92 * gcc -m32 -Dmono_free=free shared -o libmono-profiler-log.dylib proflog.c utils.c -Wall -g -lz `pkg-config --cflags mono-2` -undefined suppress -flat_namespace
93 * gcc -m32 -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
96 * sudo cp mprof-report /usr/local/bin
97 * sudo cp libmono-profiler-log.so /usr/local/lib
101 typedef struct _LogBuffer LogBuffer
;
107 * The file is composed by a header followed by 0 or more buffers.
108 * Each buffer contains events that happened on a thread: for a given thread
109 * buffers that appear later in the file are guaranteed to contain events
110 * that happened later in time. Buffers from separate threads could be interleaved,
112 * Buffers are not required to be aligned.
115 * [id: 4 bytes] constant value: LOG_HEADER_ID
116 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
117 * [format: 1 byte] version of the data format for the rest of the file
118 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
119 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
120 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
121 * [flags: 4 bytes] file format flags, should be 0 for now
122 * [pid: 4 bytes] pid of the profiled process
123 * [port: 2 bytes] tcp port for server if != 0
124 * [sysid: 2 bytes] operating system and architecture identifier
126 * The multiple byte integers are in little-endian format.
129 * [buffer header] [event]*
130 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
131 * Timing information and other values in the event data are usually stored
132 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
133 * some data is represented as a difference between the actual value and
134 * either the last value of the same type (like for timing information) or
135 * as the difference from a value stored in a buffer header.
137 * For timing information the data is stored as uleb128, since timing
138 * increases in a monotonic way in each thread: the value is the number of
139 * nanoseconds to add to the last seen timing data in a buffer. The first value
140 * in a buffer will be calculated from the time_base field in the buffer head.
142 * Object or heap sizes are stored as uleb128.
143 * Pointer differences are stored as sleb128, instead.
145 * If an unexpected value is found, the rest of the buffer should be ignored,
146 * as generally the later values need the former to be interpreted correctly.
148 * buffer header format:
149 * [bufid: 4 bytes] constant value: BUF_ID
150 * [len: 4 bytes] size of the data following the buffer header
151 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
152 * [ptr_base: 8 bytes] base value for pointers
153 * [obj_base: 8 bytes] base value for object addresses
154 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
155 * [method_base: 8 bytes] base value for MonoMethod pointers
158 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
159 * The data that follows depends on type and the extended info.
160 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
161 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
162 * The extended info bits are interpreted based on type, see
163 * each individual event description below.
164 * strings are represented as a 0-terminated utf8 sequence.
167 * [flags: uleb128] must be 0
168 * [num: uleb128] number of frames following
169 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
173 * exinfo: flags: TYPE_ALLOC_BT
174 * [time diff: uleb128] nanoseconds since last timing
175 * [ptr: sleb128] class as a byte difference from ptr_base
176 * [obj: sleb128] object address as a byte difference from obj_base
177 * [size: uleb128] size of the object in the heap
178 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
182 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED,
183 * TYPE_GC_HANDLE_DESTROYED
184 * [time diff: uleb128] nanoseconds since last timing
185 * if exinfo == TYPE_GC_RESIZE
186 * [heap_size: uleb128] new heap size
187 * if exinfo == TYPE_GC_EVENT
188 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
189 * [generation: uleb128] GC generation event refers to
190 * if exinfo == TYPE_GC_MOVE
191 * [num_objects: uleb128] number of object moves that follow
192 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
193 * num is always an even number: the even items are the old
194 * addresses, the odd numbers are the respective new object addresses
195 * if exinfo == TYPE_GC_HANDLE_CREATED
196 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
197 * upper bits reserved as flags
198 * [handle: uleb128] GC handle value
199 * [objaddr: sleb128] object pointer differences from obj_base
200 * if exinfo == TYPE_GC_HANDLE_DESTROYED
201 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
202 * upper bits reserved as flags
203 * [handle: uleb128] GC handle value
205 * type metadata format:
206 * type: TYPE_METADATA
207 * exinfo: flags: TYPE_LOAD_ERR
208 * [time diff: uleb128] nanoseconds since last timing
209 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
211 * [pointer: sleb128] pointer of the metadata type depending on mtype
212 * if mtype == TYPE_CLASS
213 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
214 * [flags: uleb128] must be 0
215 * [name: string] full class name
216 * if mtype == TYPE_IMAGE
217 * [flags: uleb128] must be 0
218 * [name: string] image file name
219 * if mtype == TYPE_THREAD
220 * [flags: uleb128] must be 0
221 * [name: string] thread name
223 * type method format:
225 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
226 * [time diff: uleb128] nanoseconds since last timing
227 * [method: sleb128] MonoMethod* as a pointer difference from the last such
228 * pointer or the buffer method_base
229 * if exinfo == TYPE_JIT
230 * [code address: sleb128] pointer to the native code as a diff from ptr_base
231 * [code size: uleb128] size of the generated code
232 * [name: string] full method name
234 * type exception format:
235 * type: TYPE_EXCEPTION
236 * exinfo: TYPE_EXCEPTION_BT flag and one of: TYPE_THROW, TYPE_CLAUSE
237 * [time diff: uleb128] nanoseconds since last timing
238 * if exinfo.low3bits == TYPE_CLAUSE
239 * [clause type: uleb128] finally/catch/fault/filter
240 * [clause num: uleb128] the clause number in the method header
241 * [method: sleb128] MonoMethod* as a pointer difference from the last such
242 * pointer or the buffer method_base
243 * if exinfo.low3bits == TYPE_THROW
244 * [object: sleb128] the object that was thrown as a difference from obj_base
245 * If the TYPE_EXCEPTION_BT flag is set, a backtrace follows.
247 * type monitor format:
249 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
250 * [time diff: uleb128] nanoseconds since last timing
251 * [object: sleb128] the lock object as a difference from obj_base
252 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
253 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
257 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
258 * if exinfo == TYPE_HEAP_START
259 * [time diff: uleb128] nanoseconds since last timing
260 * if exinfo == TYPE_HEAP_END
261 * [time diff: uleb128] nanoseconds since last timing
262 * if exinfo == TYPE_HEAP_OBJECT
263 * [object: sleb128] the object as a difference from obj_base
264 * [class: sleb128] the object MonoClass* as a difference from ptr_base
265 * [size: uleb128] size of the object on the heap
266 * [num_refs: uleb128] number of object references
267 * if (format version > 1) each referenced objref is preceded by a
268 * uleb128 encoded offset: the first offset is from the object address
269 * and each next offset is relative to the previous one
270 * [objrefs: sleb128]+ object referenced as a difference from obj_base
271 * The same object can appear multiple times, but only the first time
272 * with size != 0: in the other cases this data will only be used to
273 * provide additional referenced objects.
274 * if exinfo == TYPE_HEAP_ROOT
275 * [num_roots: uleb128] number of root references
276 * [num_gc: uleb128] number of major gcs
277 * [object: sleb128] the object as a difference from obj_base
278 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
279 * [extra_info: uleb128] the extra_info value
280 * object, root_type_extra_info are repeated num_roots times
284 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN
285 * if exinfo == TYPE_SAMPLE_HIT
286 * [sample_type: uleb128] type of sample (SAMPLE_*)
287 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
288 * [count: uleb128] number of following instruction addresses
289 * [ip: sleb128]* instruction pointer as difference from ptr_base
290 * if exinfo == TYPE_SAMPLE_USYM
291 * [address: sleb128] symbol address as a difference from ptr_base
292 * [size: uleb128] symbol size (may be 0 if unknown)
293 * [name: string] symbol name
294 * if exinfo == TYPE_SAMPLE_UBIN
295 * [time diff: uleb128] nanoseconds since last timing
296 * [address: sleb128] address where binary has been loaded
297 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
298 * [size: uleb128] memory size
299 * [name: string] binary name
307 uintptr_t method_base
;
308 uintptr_t last_method
;
311 unsigned char* data_end
;
316 unsigned char buf
[1];
319 #define ENTER_LOG(lb,str) if ((lb)->locked) {write(2, str, strlen(str)); write(2, "\n", 1);return;} else {(lb)->locked++;}
320 #define EXIT_LOG(lb) (lb)->locked--;
322 typedef struct _StatBuffer StatBuffer
;
331 typedef struct _BinaryObject BinaryObject
;
333 struct _BinaryObject
{
339 struct _MonoProfiler
{
341 StatBuffer
*stat_buffers
;
343 #if defined (HAVE_SYS_ZLIB)
346 uint64_t startup_time
;
348 int last_gc_gen_started
;
353 pthread_t helper_thread
;
355 BinaryObject
*binary_objects
;
359 #define TLS_SET(x,y) TlsSetValue(x, y)
360 #define TLS_GET(x) ((LogBuffer *) TlsGetValue(x))
361 #define TLS_INIT(x) x = TlsAlloc ()
362 static int tlsbuffer
;
364 #define TLS_SET(x,y) x = y
367 static __thread LogBuffer
* tlsbuffer
= NULL
;
369 #define TLS_SET(x,y) pthread_setspecific(x, y)
370 #define TLS_GET(x) ((LogBuffer *) pthread_getspecific(x))
371 #define TLS_INIT(x) pthread_key_create(&x, NULL)
372 static pthread_key_t tlsbuffer
;
376 pstrdup (const char *s
)
378 int len
= strlen (s
) + 1;
379 char *p
= malloc (len
);
385 create_stat_buffer (void)
387 StatBuffer
* buf
= alloc_buffer (BUFFER_SIZE
);
388 buf
->size
= BUFFER_SIZE
;
389 buf
->data_end
= (uintptr_t*)((unsigned char*)buf
+ buf
->size
);
390 buf
->data
= buf
->buf
;
397 LogBuffer
* buf
= alloc_buffer (BUFFER_SIZE
);
398 buf
->size
= BUFFER_SIZE
;
399 buf
->time_base
= current_time ();
400 buf
->last_time
= buf
->time_base
;
401 buf
->data_end
= (unsigned char*)buf
+ buf
->size
;
402 buf
->data
= buf
->buf
;
409 LogBuffer
*logbuffer
;
410 if (TLS_GET (tlsbuffer
))
412 logbuffer
= create_buffer ();
413 TLS_SET (tlsbuffer
, logbuffer
);
414 logbuffer
->thread_id
= thread_id ();
415 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
419 ensure_logbuf (int bytes
)
421 LogBuffer
*old
= TLS_GET (tlsbuffer
);
422 if (old
&& old
->data
+ bytes
+ 100 < old
->data_end
)
424 TLS_SET (tlsbuffer
, NULL
);
426 TLS_GET (tlsbuffer
)->next
= old
;
428 TLS_GET (tlsbuffer
)->call_depth
= old
->call_depth
;
429 //printf ("new logbuffer\n");
430 return TLS_GET (tlsbuffer
);
434 emit_byte (LogBuffer
*logbuffer
, int value
)
436 logbuffer
->data
[0] = value
;
438 assert (logbuffer
->data
<= logbuffer
->data_end
);
442 emit_value (LogBuffer
*logbuffer
, int value
)
444 encode_uleb128 (value
, logbuffer
->data
, &logbuffer
->data
);
445 assert (logbuffer
->data
<= logbuffer
->data_end
);
449 emit_time (LogBuffer
*logbuffer
, uint64_t value
)
451 uint64_t tdiff
= value
- logbuffer
->last_time
;
453 if (value
< logbuffer
->last_time
)
454 printf ("time went backwards\n");
455 //if (tdiff > 1000000)
456 // printf ("large time offset: %llu\n", tdiff);
458 encode_uleb128 (tdiff
, logbuffer
->data
, &logbuffer
->data
);
459 /*if (tdiff != decode_uleb128 (p, &p))
460 printf ("incorrect encoding: %llu\n", tdiff);*/
461 logbuffer
->last_time
= value
;
462 assert (logbuffer
->data
<= logbuffer
->data_end
);
466 emit_svalue (LogBuffer
*logbuffer
, int64_t value
)
468 encode_sleb128 (value
, logbuffer
->data
, &logbuffer
->data
);
469 assert (logbuffer
->data
<= logbuffer
->data_end
);
473 emit_uvalue (LogBuffer
*logbuffer
, uint64_t value
)
475 encode_uleb128 (value
, logbuffer
->data
, &logbuffer
->data
);
476 assert (logbuffer
->data
<= logbuffer
->data_end
);
480 emit_ptr (LogBuffer
*logbuffer
, void *ptr
)
482 if (!logbuffer
->ptr_base
)
483 logbuffer
->ptr_base
= (uintptr_t)ptr
;
484 emit_svalue (logbuffer
, (intptr_t)ptr
- logbuffer
->ptr_base
);
485 assert (logbuffer
->data
<= logbuffer
->data_end
);
489 emit_method (LogBuffer
*logbuffer
, void *method
)
491 if (!logbuffer
->method_base
) {
492 logbuffer
->method_base
= (intptr_t)method
;
493 logbuffer
->last_method
= (intptr_t)method
;
495 encode_sleb128 ((intptr_t)((char*)method
- (char*)logbuffer
->last_method
), logbuffer
->data
, &logbuffer
->data
);
496 logbuffer
->last_method
= (intptr_t)method
;
497 assert (logbuffer
->data
<= logbuffer
->data_end
);
501 emit_obj (LogBuffer
*logbuffer
, void *ptr
)
503 if (!logbuffer
->obj_base
)
504 logbuffer
->obj_base
= (uintptr_t)ptr
>> 3;
505 emit_svalue (logbuffer
, ((uintptr_t)ptr
>> 3) - logbuffer
->obj_base
);
506 assert (logbuffer
->data
<= logbuffer
->data_end
);
510 write_int16 (char *buf
, int32_t value
)
513 for (i
= 0; i
< 2; ++i
) {
521 write_int32 (char *buf
, int32_t value
)
524 for (i
= 0; i
< 4; ++i
) {
532 write_int64 (char *buf
, int64_t value
)
535 for (i
= 0; i
< 8; ++i
) {
543 dump_header (MonoProfiler
*profiler
)
547 p
= write_int32 (p
, LOG_HEADER_ID
);
548 *p
++ = LOG_VERSION_MAJOR
;
549 *p
++ = LOG_VERSION_MINOR
;
550 *p
++ = LOG_DATA_VERSION
;
551 *p
++ = sizeof (void*);
552 p
= write_int64 (p
, ((uint64_t)time (NULL
)) * 1000); /* startup time */
553 p
= write_int32 (p
, get_timer_overhead ()); /* timer overhead */
554 p
= write_int32 (p
, 0); /* flags */
555 p
= write_int32 (p
, process_id ()); /* pid */
556 p
= write_int16 (p
, profiler
->command_port
); /* port */
557 p
= write_int16 (p
, 0); /* opsystem */
558 #if defined (HAVE_SYS_ZLIB)
559 if (profiler
->gzfile
) {
560 gzwrite (profiler
->gzfile
, hbuf
, p
- hbuf
);
562 fwrite (hbuf
, p
- hbuf
, 1, profiler
->file
);
565 fwrite (hbuf
, p
- hbuf
, 1, profiler
->file
);
570 dump_buffer (MonoProfiler
*profiler
, LogBuffer
*buf
)
575 dump_buffer (profiler
, buf
->next
);
576 p
= write_int32 (p
, BUF_ID
);
577 p
= write_int32 (p
, buf
->data
- buf
->buf
);
578 p
= write_int64 (p
, buf
->time_base
);
579 p
= write_int64 (p
, buf
->ptr_base
);
580 p
= write_int64 (p
, buf
->obj_base
);
581 p
= write_int64 (p
, buf
->thread_id
);
582 p
= write_int64 (p
, buf
->method_base
);
583 #if defined (HAVE_SYS_ZLIB)
584 if (profiler
->gzfile
) {
585 gzwrite (profiler
->gzfile
, hbuf
, p
- hbuf
);
586 gzwrite (profiler
->gzfile
, buf
->buf
, buf
->data
- buf
->buf
);
589 fwrite (hbuf
, p
- hbuf
, 1, profiler
->file
);
590 fwrite (buf
->buf
, buf
->data
- buf
->buf
, 1, profiler
->file
);
591 #if defined (HAVE_SYS_ZLIB)
594 free_buffer (buf
, buf
->size
);
598 process_requests (MonoProfiler
*profiler
)
600 if (heapshot_requested
)
601 mono_gc_collect (mono_gc_max_generation ());
605 runtime_initialized (MonoProfiler
*profiler
)
611 * Can be called only at safe callback locations.
614 safe_dump (MonoProfiler
*profiler
, LogBuffer
*logbuffer
)
616 int cd
= logbuffer
->call_depth
;
618 dump_buffer (profiler
, TLS_GET (tlsbuffer
));
620 TLS_SET (tlsbuffer
, NULL
);
622 TLS_GET (tlsbuffer
)->call_depth
= cd
;
626 gc_reference (MonoObject
*obj
, MonoClass
*klass
, uintptr_t size
, uintptr_t num
, MonoObject
**refs
, uintptr_t *offsets
, void *data
)
629 uintptr_t last_offset
= 0;
630 //const char *name = mono_class_get_name (klass);
631 LogBuffer
*logbuffer
= ensure_logbuf (20 + num
* 8);
632 emit_byte (logbuffer
, TYPE_HEAP_OBJECT
| TYPE_HEAP
);
633 emit_obj (logbuffer
, obj
);
634 emit_ptr (logbuffer
, klass
);
635 /* account for object alignment in the heap */
638 emit_value (logbuffer
, size
);
639 emit_value (logbuffer
, num
);
640 for (i
= 0; i
< num
; ++i
) {
641 emit_value (logbuffer
, offsets
[i
] - last_offset
);
642 last_offset
= offsets
[i
];
643 emit_obj (logbuffer
, refs
[i
]);
646 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
650 static unsigned int hs_mode_ms
= 0;
651 static unsigned int hs_mode_gc
= 0;
652 static unsigned int hs_mode_ondemand
= 0;
653 static unsigned int gc_count
= 0;
654 static uint64_t last_hs_time
= 0;
657 heap_walk (MonoProfiler
*profiler
)
661 LogBuffer
*logbuffer
;
664 logbuffer
= ensure_logbuf (10);
665 now
= current_time ();
666 if (hs_mode_ms
&& (now
- last_hs_time
)/1000000 >= hs_mode_ms
)
668 else if (hs_mode_gc
&& (gc_count
% hs_mode_gc
) == 0)
670 else if (hs_mode_ondemand
&& heapshot_requested
)
672 else if (!hs_mode_ms
&& !hs_mode_gc
&& profiler
->last_gc_gen_started
== mono_gc_max_generation ())
677 heapshot_requested
= 0;
678 emit_byte (logbuffer
, TYPE_HEAP_START
| TYPE_HEAP
);
679 emit_time (logbuffer
, now
);
680 mono_gc_walk_heap (0, gc_reference
, NULL
);
681 logbuffer
= ensure_logbuf (10);
682 now
= current_time ();
683 emit_byte (logbuffer
, TYPE_HEAP_END
| TYPE_HEAP
);
684 emit_time (logbuffer
, now
);
689 gc_event (MonoProfiler
*profiler
, MonoGCEvent ev
, int generation
) {
691 LogBuffer
*logbuffer
= ensure_logbuf (10);
692 now
= current_time ();
693 ENTER_LOG (logbuffer
, "gcevent");
694 emit_byte (logbuffer
, TYPE_GC_EVENT
| TYPE_GC
);
695 emit_time (logbuffer
, now
);
696 emit_value (logbuffer
, ev
);
697 emit_value (logbuffer
, generation
);
698 /* to deal with nested gen1 after gen0 started */
699 if (ev
== MONO_GC_EVENT_START
) {
700 profiler
->last_gc_gen_started
= generation
;
701 if (generation
== mono_gc_max_generation ())
704 if (ev
== MONO_GC_EVENT_PRE_START_WORLD
)
705 heap_walk (profiler
);
706 EXIT_LOG (logbuffer
);
707 if (ev
== MONO_GC_EVENT_POST_START_WORLD
)
708 safe_dump (profiler
, logbuffer
);
709 //printf ("gc event %d for generation %d\n", ev, generation);
713 gc_resize (MonoProfiler
*profiler
, int64_t new_size
) {
715 LogBuffer
*logbuffer
= ensure_logbuf (10);
716 now
= current_time ();
717 ENTER_LOG (logbuffer
, "gcresize");
718 emit_byte (logbuffer
, TYPE_GC_RESIZE
| TYPE_GC
);
719 emit_time (logbuffer
, now
);
720 emit_value (logbuffer
, new_size
);
721 //printf ("gc resized to %lld\n", new_size);
722 EXIT_LOG (logbuffer
);
725 #define MAX_FRAMES 16
728 MonoMethod
* methods
[MAX_FRAMES
];
730 static int num_frames
= MAX_FRAMES
/ 2;
733 walk_stack (MonoMethod
*method
, int32_t native_offset
, int32_t il_offset
, mono_bool managed
, void* data
)
735 FrameData
*frame
= data
;
736 if (method
&& frame
->count
< num_frames
) {
737 frame
->methods
[frame
->count
++] = method
;
738 //printf ("In %d %s\n", frame->count, mono_method_get_name (method));
740 return frame
->count
== num_frames
;
744 * a note about stack walks: they can cause more profiler events to fire,
745 * so we need to make sure they don't happen after we started emitting an
746 * event, hence the collect_bt/emit_bt split.
749 collect_bt (FrameData
*data
)
752 mono_stack_walk_no_il (walk_stack
, data
);
756 emit_bt (LogBuffer
*logbuffer
, FrameData
*data
)
758 /* FIXME: this is actually tons of data and we should
759 * just output it the first time and use an id the next
761 if (data
->count
> num_frames
)
762 printf ("bad num frames: %d\n", data
->count
);
763 emit_value (logbuffer
, 0); /* flags */
764 emit_value (logbuffer
, data
->count
);
765 //if (*p != data.count) {
766 // printf ("bad num frames enc at %d: %d -> %d\n", count, data.count, *p); printf ("frames end: %p->%p\n", p, logbuffer->data); exit(0);}
767 while (data
->count
) {
768 emit_ptr (logbuffer
, data
->methods
[--data
->count
]);
773 gc_alloc (MonoProfiler
*prof
, MonoObject
*obj
, MonoClass
*klass
)
777 int do_bt
= (nocalls
&& runtime_inited
&& !notraces
)? TYPE_ALLOC_BT
: 0;
779 LogBuffer
*logbuffer
;
780 len
= mono_object_get_size (obj
);
781 /* account for object alignment in the heap */
786 logbuffer
= ensure_logbuf (32 + MAX_FRAMES
* 8);
787 now
= current_time ();
788 ENTER_LOG (logbuffer
, "gcalloc");
789 emit_byte (logbuffer
, do_bt
| TYPE_ALLOC
);
790 emit_time (logbuffer
, now
);
791 emit_ptr (logbuffer
, klass
);
792 emit_obj (logbuffer
, obj
);
793 emit_value (logbuffer
, len
);
795 emit_bt (logbuffer
, &data
);
796 EXIT_LOG (logbuffer
);
798 safe_dump (prof
, logbuffer
);
799 process_requests (prof
);
800 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
804 gc_moves (MonoProfiler
*prof
, void **objects
, int num
)
808 LogBuffer
*logbuffer
= ensure_logbuf (10 + num
* 8);
809 now
= current_time ();
810 ENTER_LOG (logbuffer
, "gcmove");
811 emit_byte (logbuffer
, TYPE_GC_MOVE
| TYPE_GC
);
812 emit_time (logbuffer
, now
);
813 emit_value (logbuffer
, num
);
814 for (i
= 0; i
< num
; ++i
)
815 emit_obj (logbuffer
, objects
[i
]);
816 //printf ("gc moved %d objects\n", num/2);
817 EXIT_LOG (logbuffer
);
821 gc_roots (MonoProfiler
*prof
, int num
, void **objects
, int *root_types
, uintptr_t *extra_info
)
824 LogBuffer
*logbuffer
= ensure_logbuf (5 + num
* 18);
825 ENTER_LOG (logbuffer
, "gcroots");
826 emit_byte (logbuffer
, TYPE_HEAP_ROOT
| TYPE_HEAP
);
827 emit_value (logbuffer
, num
);
828 emit_value (logbuffer
, mono_gc_collection_count (mono_gc_max_generation ()));
829 for (i
= 0; i
< num
; ++i
) {
830 emit_obj (logbuffer
, objects
[i
]);
831 emit_value (logbuffer
, root_types
[i
]);
832 emit_value (logbuffer
, extra_info
[i
]);
834 EXIT_LOG (logbuffer
);
838 gc_handle (MonoProfiler
*prof
, int op
, int type
, uintptr_t handle
, MonoObject
*obj
)
841 LogBuffer
*logbuffer
= ensure_logbuf (16);
842 now
= current_time ();
843 ENTER_LOG (logbuffer
, "gchandle");
844 if (op
== MONO_PROFILER_GC_HANDLE_CREATED
)
845 emit_byte (logbuffer
, TYPE_GC_HANDLE_CREATED
| TYPE_GC
);
846 else if (op
== MONO_PROFILER_GC_HANDLE_DESTROYED
)
847 emit_byte (logbuffer
, TYPE_GC_HANDLE_DESTROYED
| TYPE_GC
);
850 emit_time (logbuffer
, now
);
851 emit_value (logbuffer
, type
);
852 emit_value (logbuffer
, handle
);
853 if (op
== MONO_PROFILER_GC_HANDLE_CREATED
)
854 emit_obj (logbuffer
, obj
);
855 EXIT_LOG (logbuffer
);
856 process_requests (prof
);
860 push_nesting (char *p
, MonoClass
*klass
)
865 nesting
= mono_class_get_nesting_type (klass
);
867 p
= push_nesting (p
, nesting
);
871 name
= mono_class_get_name (klass
);
872 nspace
= mono_class_get_namespace (klass
);
875 p
+= strlen (nspace
);
885 type_name (MonoClass
*klass
)
889 push_nesting (buf
, klass
);
890 p
= malloc (strlen (buf
) + 1);
896 image_loaded (MonoProfiler
*prof
, MonoImage
*image
, int result
)
901 LogBuffer
*logbuffer
;
902 if (result
!= MONO_PROFILE_OK
)
904 name
= mono_image_get_filename (image
);
905 nlen
= strlen (name
) + 1;
906 logbuffer
= ensure_logbuf (16 + nlen
);
907 now
= current_time ();
908 ENTER_LOG (logbuffer
, "image");
909 emit_byte (logbuffer
, TYPE_END_LOAD
| TYPE_METADATA
);
910 emit_time (logbuffer
, now
);
911 emit_byte (logbuffer
, TYPE_IMAGE
);
912 emit_ptr (logbuffer
, image
);
913 emit_value (logbuffer
, 0); /* flags */
914 memcpy (logbuffer
->data
, name
, nlen
);
915 logbuffer
->data
+= nlen
;
916 //printf ("loaded image %p (%s)\n", image, name);
917 EXIT_LOG (logbuffer
);
919 safe_dump (prof
, logbuffer
);
920 process_requests (prof
);
924 class_loaded (MonoProfiler
*prof
, MonoClass
*klass
, int result
)
930 LogBuffer
*logbuffer
;
931 if (result
!= MONO_PROFILE_OK
)
934 name
= mono_type_get_name (mono_class_get_type (klass
));
936 name
= type_name (klass
);
937 nlen
= strlen (name
) + 1;
938 image
= mono_class_get_image (klass
);
939 logbuffer
= ensure_logbuf (24 + nlen
);
940 now
= current_time ();
941 ENTER_LOG (logbuffer
, "class");
942 emit_byte (logbuffer
, TYPE_END_LOAD
| TYPE_METADATA
);
943 emit_time (logbuffer
, now
);
944 emit_byte (logbuffer
, TYPE_CLASS
);
945 emit_ptr (logbuffer
, klass
);
946 emit_ptr (logbuffer
, image
);
947 emit_value (logbuffer
, 0); /* flags */
948 memcpy (logbuffer
->data
, name
, nlen
);
949 logbuffer
->data
+= nlen
;
950 //printf ("loaded class %p (%s)\n", klass, name);
955 EXIT_LOG (logbuffer
);
957 safe_dump (prof
, logbuffer
);
958 process_requests (prof
);
962 method_enter (MonoProfiler
*prof
, MonoMethod
*method
)
965 LogBuffer
*logbuffer
= ensure_logbuf (16);
966 if (logbuffer
->call_depth
++ > max_call_depth
)
968 now
= current_time ();
969 ENTER_LOG (logbuffer
, "enter");
970 emit_byte (logbuffer
, TYPE_ENTER
| TYPE_METHOD
);
971 emit_time (logbuffer
, now
);
972 emit_method (logbuffer
, method
);
973 EXIT_LOG (logbuffer
);
974 process_requests (prof
);
978 method_leave (MonoProfiler
*prof
, MonoMethod
*method
)
981 LogBuffer
*logbuffer
= ensure_logbuf (16);
982 if (--logbuffer
->call_depth
> max_call_depth
)
984 now
= current_time ();
985 ENTER_LOG (logbuffer
, "leave");
986 emit_byte (logbuffer
, TYPE_LEAVE
| TYPE_METHOD
);
987 emit_time (logbuffer
, now
);
988 emit_method (logbuffer
, method
);
989 EXIT_LOG (logbuffer
);
991 safe_dump (prof
, logbuffer
);
992 process_requests (prof
);
996 method_exc_leave (MonoProfiler
*prof
, MonoMethod
*method
)
999 LogBuffer
*logbuffer
;
1002 logbuffer
= ensure_logbuf (16);
1003 if (--logbuffer
->call_depth
> max_call_depth
)
1005 now
= current_time ();
1006 ENTER_LOG (logbuffer
, "eleave");
1007 emit_byte (logbuffer
, TYPE_EXC_LEAVE
| TYPE_METHOD
);
1008 emit_time (logbuffer
, now
);
1009 emit_method (logbuffer
, method
);
1010 EXIT_LOG (logbuffer
);
1011 process_requests (prof
);
1015 method_jitted (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
* jinfo
, int result
)
1020 LogBuffer
*logbuffer
;
1021 if (result
!= MONO_PROFILE_OK
)
1023 name
= mono_method_full_name (method
, 1);
1024 nlen
= strlen (name
) + 1;
1025 logbuffer
= ensure_logbuf (32 + nlen
);
1026 now
= current_time ();
1027 ENTER_LOG (logbuffer
, "jit");
1028 emit_byte (logbuffer
, TYPE_JIT
| TYPE_METHOD
);
1029 emit_time (logbuffer
, now
);
1030 emit_method (logbuffer
, method
);
1031 emit_ptr (logbuffer
, mono_jit_info_get_code_start (jinfo
));
1032 emit_value (logbuffer
, mono_jit_info_get_code_size (jinfo
));
1033 memcpy (logbuffer
->data
, name
, nlen
);
1034 logbuffer
->data
+= nlen
;
1036 EXIT_LOG (logbuffer
);
1037 if (logbuffer
->next
)
1038 safe_dump (prof
, logbuffer
);
1039 process_requests (prof
);
1043 throw_exc (MonoProfiler
*prof
, MonoObject
*object
)
1045 int do_bt
= (nocalls
&& runtime_inited
&& !notraces
)? TYPE_EXCEPTION_BT
: 0;
1048 LogBuffer
*logbuffer
;
1051 logbuffer
= ensure_logbuf (16 + MAX_FRAMES
* 8);
1052 now
= current_time ();
1053 ENTER_LOG (logbuffer
, "throw");
1054 emit_byte (logbuffer
, do_bt
| TYPE_EXCEPTION
);
1055 emit_time (logbuffer
, now
);
1056 emit_obj (logbuffer
, object
);
1058 emit_bt (logbuffer
, &data
);
1059 EXIT_LOG (logbuffer
);
1060 process_requests (prof
);
1064 clause_exc (MonoProfiler
*prof
, MonoMethod
*method
, int clause_type
, int clause_num
)
1067 LogBuffer
*logbuffer
= ensure_logbuf (16);
1068 now
= current_time ();
1069 ENTER_LOG (logbuffer
, "clause");
1070 emit_byte (logbuffer
, TYPE_EXCEPTION
| TYPE_CLAUSE
);
1071 emit_time (logbuffer
, now
);
1072 emit_value (logbuffer
, clause_type
);
1073 emit_value (logbuffer
, clause_num
);
1074 emit_method (logbuffer
, method
);
1075 EXIT_LOG (logbuffer
);
1079 monitor_event (MonoProfiler
*profiler
, MonoObject
*object
, MonoProfilerMonitorEvent event
)
1081 int do_bt
= (nocalls
&& runtime_inited
&& !notraces
&& event
== MONO_PROFILER_MONITOR_CONTENTION
)? TYPE_MONITOR_BT
: 0;
1084 LogBuffer
*logbuffer
;
1087 logbuffer
= ensure_logbuf (16 + MAX_FRAMES
* 8);
1088 now
= current_time ();
1089 ENTER_LOG (logbuffer
, "monitor");
1090 emit_byte (logbuffer
, (event
<< 4) | do_bt
| TYPE_MONITOR
);
1091 emit_time (logbuffer
, now
);
1092 emit_obj (logbuffer
, object
);
1094 emit_bt (logbuffer
, &data
);
1095 EXIT_LOG (logbuffer
);
1096 process_requests (profiler
);
1100 thread_start (MonoProfiler
*prof
, uintptr_t tid
)
1102 //printf ("thread start %p\n", (void*)tid);
1107 thread_end (MonoProfiler
*prof
, uintptr_t tid
)
1110 if (TLS_GET (tlsbuffer
))
1111 dump_buffer (prof
, TLS_GET (tlsbuffer
));
1113 TLS_SET (tlsbuffer
, NULL
);
1117 thread_name (MonoProfiler
*prof
, uintptr_t tid
, const char *name
)
1119 int len
= strlen (name
) + 1;
1121 LogBuffer
*logbuffer
;
1122 logbuffer
= ensure_logbuf (10 + len
);
1123 now
= current_time ();
1124 ENTER_LOG (logbuffer
, "tname");
1125 emit_byte (logbuffer
, TYPE_METADATA
);
1126 emit_time (logbuffer
, now
);
1127 emit_byte (logbuffer
, TYPE_THREAD
);
1128 emit_ptr (logbuffer
, (void*)tid
);
1129 emit_value (logbuffer
, 0); /* flags */
1130 memcpy (logbuffer
->data
, name
, len
);
1131 logbuffer
->data
+= len
;
1132 EXIT_LOG (logbuffer
);
1136 #include "mono/io-layer/atomic.h"
1138 #define cmp_exchange InterlockedCompareExchangePointer
1141 cmp_exchange (volatile void **dest, void *exch, void *comp)
1144 __asm__ __volatile__ ("lock; "
1151 : "=m" (*dest), "=a" (old)
1152 : "r" (exch), "m" (*dest), "a" (comp));
1159 mono_sample_hit (MonoProfiler
*profiler
, unsigned char *ip
, void *context
)
1163 uintptr_t *data
, *new_data
, *old_data
;
1166 now
= current_time ();
1170 snprintf (buf
, sizeof (buf
), "hit at %p in thread %p at %llu\n", ip
, (void*)thread_id (), (unsigned long long int)now
);
1172 write (2, buf
, len
);
1174 sbuf
= profiler
->stat_buffers
;
1178 if (sbuf
->data
+ 400 >= sbuf
->data_end
) {
1179 sbuf
= create_stat_buffer ();
1180 sbuf
->next
= profiler
->stat_buffers
;
1181 profiler
->stat_buffers
= sbuf
;
1183 write (2, "overflow\n", 9);
1184 /* notify the helper thread */
1185 if (sbuf
->next
->next
) {
1187 write (profiler
->pipes
[1], &c
, 1);
1189 write (2, "notify\n", 7);
1193 old_data
= sbuf
->data
;
1194 new_data
= old_data
+ 4;
1195 data
= cmp_exchange ((volatile void**)&sbuf
->data
, new_data
, old_data
);
1196 } while (data
!= old_data
);
1197 if (old_data
>= sbuf
->data_end
)
1198 return; /* lost event */
1199 old_data
[0] = 1 | (sample_type
<< 16);
1200 old_data
[1] = thread_id ();
1201 old_data
[2] = (now
- profiler
->startup_time
) / 10000;
1202 old_data
[3] = (uintptr_t)ip
;
1205 static uintptr_t *code_pages
= 0;
1206 static int num_code_pages
= 0;
1207 static int size_code_pages
= 0;
1208 #define CPAGE_SHIFT (9)
1209 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
1210 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
1211 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
1214 add_code_page (uintptr_t *hash
, uintptr_t hsize
, uintptr_t page
)
1217 uintptr_t start_pos
;
1218 start_pos
= (page
>> CPAGE_SHIFT
) % hsize
;
1221 if (hash
[i
] && CPAGE_ADDR (hash
[i
]) == CPAGE_ADDR (page
)) {
1223 } else if (!hash
[i
]) {
1230 } while (i
!= start_pos
);
1231 /* should not happen */
1232 printf ("failed code page store\n");
1237 add_code_pointer (uintptr_t ip
)
1240 if (num_code_pages
* 2 >= size_code_pages
) {
1242 uintptr_t old_size
= size_code_pages
;
1243 size_code_pages
*= 2;
1244 if (size_code_pages
== 0)
1245 size_code_pages
= 16;
1246 n
= calloc (sizeof (uintptr_t) * size_code_pages
, 1);
1247 for (i
= 0; i
< old_size
; ++i
) {
1249 add_code_page (n
, size_code_pages
, code_pages
[i
]);
1255 num_code_pages
+= add_code_page (code_pages
, size_code_pages
, ip
& CPAGE_MASK
);
1259 dump_ubin (const char *filename
, uintptr_t load_addr
, uint64_t offset
, uintptr_t size
)
1262 LogBuffer
*logbuffer
;
1264 len
= strlen (filename
) + 1;
1265 now
= current_time ();
1266 logbuffer
= ensure_logbuf (20 + len
);
1267 emit_byte (logbuffer
, TYPE_SAMPLE
| TYPE_SAMPLE_UBIN
);
1268 emit_time (logbuffer
, now
);
1269 emit_svalue (logbuffer
, load_addr
);
1270 emit_uvalue (logbuffer
, offset
);
1271 emit_uvalue (logbuffer
, size
);
1272 memcpy (logbuffer
->data
, filename
, len
);
1273 logbuffer
->data
+= len
;
1277 dump_usym (const char *name
, uintptr_t value
, uintptr_t size
)
1279 LogBuffer
*logbuffer
;
1281 len
= strlen (name
) + 1;
1282 logbuffer
= ensure_logbuf (20 + len
);
1283 emit_byte (logbuffer
, TYPE_SAMPLE
| TYPE_SAMPLE_USYM
);
1284 emit_ptr (logbuffer
, (void*)value
);
1285 emit_value (logbuffer
, size
);
1286 memcpy (logbuffer
->data
, name
, len
);
1287 logbuffer
->data
+= len
;
1292 #if SIZEOF_VOID_P == 4
1293 #define ELF_WSIZE 32
1295 #define ELF_WSIZE 64
1298 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
1299 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
1300 #define _ElfW_1(e,w,t) e##w##t
1304 dump_elf_symbols (ElfW(Sym
) *symbols
, int num_symbols
, const char *strtab
, void *load_addr
)
1307 for (i
= 0; i
< num_symbols
; ++i
) {
1309 sym
= strtab
+ symbols
[i
].st_name
;
1310 if (!symbols
[i
].st_name
|| !symbols
[i
].st_size
|| (symbols
[i
].st_info
& 0xf) != STT_FUNC
)
1312 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
1313 dump_usym (sym
, (uintptr_t)load_addr
+ symbols
[i
].st_value
, symbols
[i
].st_size
);
1318 read_elf_symbols (MonoProfiler
*prof
, const char *filename
, void *load_addr
)
1325 ElfW(Shdr
) *sheader
;
1326 ElfW(Shdr
) *shstrtabh
;
1327 ElfW(Shdr
) *symtabh
= NULL
;
1328 ElfW(Shdr
) *strtabh
= NULL
;
1329 ElfW(Sym
) *symbols
= NULL
;
1333 fd
= open (filename
, O_RDONLY
);
1336 if (fstat (fd
, &statb
) != 0) {
1340 file_size
= statb
.st_size
;
1341 data
= mmap (NULL
, file_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1343 if (data
== MAP_FAILED
)
1346 if (header
->e_ident
[EI_MAG0
] != ELFMAG0
||
1347 header
->e_ident
[EI_MAG1
] != ELFMAG1
||
1348 header
->e_ident
[EI_MAG2
] != ELFMAG2
||
1349 header
->e_ident
[EI_MAG3
] != ELFMAG3
) {
1350 munmap (data
, file_size
);
1353 sheader
= (void*)((char*)data
+ header
->e_shoff
);
1354 shstrtabh
= (void*)((char*)sheader
+ (header
->e_shentsize
* header
->e_shstrndx
));
1355 strtab
= (const char*)data
+ shstrtabh
->sh_offset
;
1356 for (i
= 0; i
< header
->e_shnum
; ++i
) {
1357 //printf ("section header: %d\n", sheader->sh_type);
1358 if (sheader
->sh_type
== SHT_SYMTAB
) {
1360 strtabh
= (void*)((char*)data
+ header
->e_shoff
+ sheader
->sh_link
* header
->e_shentsize
);
1361 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
1364 sheader
= (void*)((char*)sheader
+ header
->e_shentsize
);
1366 if (!symtabh
|| !strtabh
) {
1367 munmap (data
, file_size
);
1370 strtab
= (const char*)data
+ strtabh
->sh_offset
;
1371 num_symbols
= symtabh
->sh_size
/ symtabh
->sh_entsize
;
1372 symbols
= (void*)((char*)data
+ symtabh
->sh_offset
);
1373 dump_elf_symbols (symbols
, num_symbols
, strtab
, load_addr
);
1374 munmap (data
, file_size
);
1379 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1381 elf_dl_callback (struct dl_phdr_info
*info
, size_t size
, void *data
)
1383 MonoProfiler
*prof
= data
;
1385 const char *filename
;
1387 char *a
= (void*)info
->dlpi_addr
;
1389 ElfW(Dyn
) *dyn
= NULL
;
1390 ElfW(Sym
) *symtab
= NULL
;
1391 ElfW(Word
) *hash_table
= NULL
;
1392 ElfW(Ehdr
) *header
= NULL
;
1393 const char* strtab
= NULL
;
1394 for (obj
= prof
->binary_objects
; obj
; obj
= obj
->next
) {
1398 filename
= info
->dlpi_name
;
1399 if (!info
->dlpi_addr
&& !filename
[0]) {
1400 int l
= readlink ("/proc/self/exe", buf
, sizeof (buf
) - 1);
1406 obj
= calloc (sizeof (BinaryObject
), 1);
1407 obj
->addr
= (void*)info
->dlpi_addr
;
1408 obj
->name
= pstrdup (filename
);
1409 obj
->next
= prof
->binary_objects
;
1410 prof
->binary_objects
= obj
;
1411 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
1413 for (i
= 0; i
< info
->dlpi_phnum
; ++i
) {
1414 //printf ("segment type %d file offset: %d, size: %d\n", info->dlpi_phdr[i].p_type, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1415 if (info
->dlpi_phdr
[i
].p_type
== PT_LOAD
&& !header
) {
1416 header
= (ElfW(Ehdr
)*)(info
->dlpi_addr
+ info
->dlpi_phdr
[i
].p_vaddr
);
1417 if (header
->e_ident
[EI_MAG0
] != ELFMAG0
||
1418 header
->e_ident
[EI_MAG1
] != ELFMAG1
||
1419 header
->e_ident
[EI_MAG2
] != ELFMAG2
||
1420 header
->e_ident
[EI_MAG3
] != ELFMAG3
) {
1423 dump_ubin (filename
, info
->dlpi_addr
+ info
->dlpi_phdr
[i
].p_vaddr
, info
->dlpi_phdr
[i
].p_offset
, info
->dlpi_phdr
[i
].p_memsz
);
1424 } else if (info
->dlpi_phdr
[i
].p_type
== PT_DYNAMIC
) {
1425 dyn
= (ElfW(Dyn
) *)(info
->dlpi_addr
+ info
->dlpi_phdr
[i
].p_vaddr
);
1428 if (read_elf_symbols (prof
, filename
, (void*)info
->dlpi_addr
))
1430 if (!info
->dlpi_name
|| !info
->dlpi_name
[0])
1434 for (i
= 0; dyn
[i
].d_tag
!= DT_NULL
; ++i
) {
1435 if (dyn
[i
].d_tag
== DT_SYMTAB
) {
1436 if (symtab
&& do_debug
)
1437 printf ("multiple symtabs: %d\n", i
);
1438 symtab
= (ElfW(Sym
) *)(a
+ dyn
[i
].d_un
.d_ptr
);
1439 } else if (dyn
[i
].d_tag
== DT_HASH
) {
1440 hash_table
= (ElfW(Word
) *)(a
+ dyn
[i
].d_un
.d_ptr
);
1441 } else if (dyn
[i
].d_tag
== DT_STRTAB
) {
1442 strtab
= (const char*)(a
+ dyn
[i
].d_un
.d_ptr
);
1447 num_sym
= hash_table
[1];
1448 dump_elf_symbols (symtab
, num_sym
, strtab
, (void*)info
->dlpi_addr
);
1453 load_binaries (MonoProfiler
*prof
)
1455 dl_iterate_phdr (elf_dl_callback
, prof
);
1460 load_binaries (MonoProfiler
*prof
)
1467 symbol_for (uintptr_t code
)
1470 void *ip
= (void*)code
;
1472 if (dladdr (ip
, &di
)) {
1474 return di
.dli_sname
;
1477 names = backtrace_symbols (&ip, 1);
1479 const char* p = names [0];
1490 dump_unmanaged_coderefs (MonoProfiler
*prof
)
1493 const char* last_symbol
;
1494 uintptr_t addr
, page_end
;
1496 if (load_binaries (prof
))
1498 for (i
= 0; i
< size_code_pages
; ++i
) {
1500 if (!code_pages
[i
] || code_pages
[i
] & 1)
1503 addr
= CPAGE_ADDR (code_pages
[i
]);
1504 page_end
= addr
+ CPAGE_SIZE
;
1505 code_pages
[i
] |= 1;
1506 /* we dump the symbols for the whole page */
1507 for (; addr
< page_end
; addr
+= 16) {
1508 sym
= symbol_for (addr
);
1509 if (sym
&& sym
== last_symbol
)
1514 dump_usym (sym
, addr
, 0); /* let's not guess the size */
1515 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
1521 dump_sample_hits (MonoProfiler
*prof
, StatBuffer
*sbuf
, int recurse
)
1524 LogBuffer
*logbuffer
;
1527 if (recurse
&& sbuf
->next
) {
1528 dump_sample_hits (prof
, sbuf
->next
, 1);
1529 free_buffer (sbuf
->next
, sbuf
->next
->size
);
1532 for (sample
= sbuf
->buf
; sample
< sbuf
->data
;) {
1534 int count
= sample
[0] & 0xffff;
1535 int type
= sample
[0] >> 16;
1536 if (sample
+ count
+ 3 > sbuf
->data
)
1538 logbuffer
= ensure_logbuf (20 + count
* 8);
1539 emit_byte (logbuffer
, TYPE_SAMPLE
| TYPE_SAMPLE_HIT
);
1540 emit_value (logbuffer
, type
);
1541 emit_uvalue (logbuffer
, (prof
->startup_time
+ sample
[2]) * 10000);
1542 emit_value (logbuffer
, count
);
1543 for (i
= 0; i
< count
; ++i
) {
1544 emit_ptr (logbuffer
, (void*)sample
[i
+ 3]);
1545 add_code_pointer (sample
[i
+ 3]);
1547 sample
+= count
+ 3;
1549 dump_unmanaged_coderefs (prof
);
1553 #ifndef __NR_perf_event_open
1554 #define __NR_perf_event_open 241
1557 static int perf_fd
= -1;
1558 static void *mmap_base
;
1559 static struct perf_event_mmap_page
*page_desc
= NULL
;
1560 static int num_pages
= 64;
1561 static unsigned int mmap_mask
;
1562 static unsigned int prev_pos
= 0;
1565 struct perf_event_header h
;
1575 perf_event_syscall (struct perf_event_attr
*attr
, pid_t pid
, int cpu
, int group_fd
, unsigned long flags
)
1577 attr
->size
= PERF_ATTR_SIZE_VER0
;
1578 //printf ("perf attr size: %d\n", attr->size);
1579 #if defined(__x86_64__)
1580 return syscall(/*__NR_perf_event_open*/ 298, attr
, pid
, cpu
, group_fd
, flags
);
1581 #elif defined(__i386__)
1582 return syscall(/*__NR_perf_event_open*/ 336, attr
, pid
, cpu
, group_fd
, flags
);
1589 setup_perf_map (void)
1591 mmap_mask
= num_pages
* getpagesize () - 1;
1592 mmap_base
= mmap (NULL
, (num_pages
+ 1) * getpagesize (), PROT_READ
|PROT_WRITE
, MAP_SHARED
, perf_fd
, 0);
1593 if (mmap_base
== MAP_FAILED
) {
1595 printf ("failed mmap\n");
1598 page_desc
= mmap_base
;
1600 printf ("mmap version: %d\n", page_desc
->version
);
1605 dump_perf_hits (MonoProfiler
*prof
, void *buf
, int size
)
1607 LogBuffer
*logbuffer
;
1608 void *end
= (char*)buf
+ size
;
1610 int pid
= getpid ();
1616 if (pid
!= s
->pid
) {
1618 printf ("event for different pid: %d\n", s
->pid
);
1619 buf
= (char*)buf
+ s
->h
.size
;
1622 /*ip = (void*)s->ip;
1623 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
1624 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
1625 logbuffer
= ensure_logbuf (20 + s
->nframes
* 8);
1626 emit_byte (logbuffer
, TYPE_SAMPLE
| TYPE_SAMPLE_HIT
);
1627 emit_value (logbuffer
, sample_type
);
1628 emit_uvalue (logbuffer
, s
->timestamp
- prof
->startup_time
);
1629 emit_value (logbuffer
, 1); /* count */
1630 emit_ptr (logbuffer
, (void*)(uintptr_t)s
->ip
);
1631 add_code_pointer (s
->ip
);
1632 buf
= (char*)buf
+ s
->h
.size
;
1636 printf ("dumped %d samples\n", samples
);
1637 dump_unmanaged_coderefs (prof
);
1640 /* read events from the ring buffer */
1642 read_perf_mmap (MonoProfiler
* prof
)
1645 unsigned char *data
= (unsigned char*)mmap_base
+ getpagesize ();
1646 unsigned int head
= page_desc
->data_head
;
1650 #if defined(__i386__)
1651 asm volatile("lock; addl $0,0(%%esp)":::"memory");
1652 #elif defined (__x86_64__)
1653 asm volatile("lfence":::"memory");
1660 printf ("lost mmap events: old: %d, head: %d\n", old
, head
);
1664 if ((old
& mmap_mask
) + size
!= (head
& mmap_mask
)) {
1665 buf
= data
+ (old
& mmap_mask
);
1666 size
= mmap_mask
+ 1 - (old
& mmap_mask
);
1668 /* size bytes at buf */
1670 printf ("found1 bytes of events: %d\n", size
);
1671 dump_perf_hits (prof
, buf
, size
);
1673 buf
= data
+ (old
& mmap_mask
);
1675 /* size bytes at buf */
1677 printf ("found bytes of events: %d\n", size
);
1678 dump_perf_hits (prof
, buf
, size
);
1681 page_desc
->data_tail
= old
;
1686 setup_perf_event (void)
1688 struct perf_event_attr attr
;
1689 memset (&attr
, 0, sizeof (attr
));
1690 attr
.type
= PERF_TYPE_HARDWARE
;
1691 switch (sample_type
) {
1692 case SAMPLE_CYCLES
: attr
.config
= PERF_COUNT_HW_CPU_CYCLES
; break;
1693 case SAMPLE_INSTRUCTIONS
: attr
.config
= PERF_COUNT_HW_INSTRUCTIONS
; break;
1694 case SAMPLE_CACHE_MISSES
: attr
.config
= PERF_COUNT_HW_CACHE_MISSES
; break;
1695 case SAMPLE_CACHE_REFS
: attr
.config
= PERF_COUNT_HW_CACHE_REFERENCES
; break;
1696 case SAMPLE_BRANCHES
: attr
.config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
; break;
1697 case SAMPLE_BRANCH_MISSES
: attr
.config
= PERF_COUNT_HW_BRANCH_MISSES
; break;
1698 default: attr
.config
= PERF_COUNT_HW_CPU_CYCLES
; break;
1700 attr
.sample_type
= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
| PERF_SAMPLE_PERIOD
| PERF_SAMPLE_TIME
;
1701 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
1702 attr
.read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
| PERF_FORMAT_TOTAL_TIME_RUNNING
| PERF_FORMAT_ID
;
1705 attr
.sample_freq
= sample_freq
;
1707 perf_fd
= perf_event_syscall (&attr
, getpid (), -1, -1, 0);
1709 printf ("perf fd: %d, freq: %d, event: %llu\n", perf_fd
, sample_freq
, attr
.config
);
1711 if (perf_fd
== -EPERM
) {
1712 fprintf (stderr
, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
1715 perror ("open perf event");
1719 if (!setup_perf_map ()) {
1727 #endif /* USE_PERF_EVENTS */
1730 log_shutdown (MonoProfiler
*prof
)
1733 #ifndef DISABLE_HELPER_THREAD
1734 if (prof
->command_port
) {
1737 write (prof
->pipes
[1], &c
, 1);
1738 pthread_join (prof
->helper_thread
, &res
);
1743 read_perf_mmap (prof
);
1745 dump_sample_hits (prof
, prof
->stat_buffers
, 1);
1747 if (TLS_GET (tlsbuffer
))
1748 dump_buffer (prof
, TLS_GET (tlsbuffer
));
1749 TLS_SET (tlsbuffer
, NULL
);
1751 #if defined (HAVE_SYS_ZLIB)
1753 gzclose (prof
->gzfile
);
1755 if (prof
->pipe_output
)
1756 pclose (prof
->file
);
1758 fclose (prof
->file
);
1763 new_filename (const char* filename
)
1765 time_t t
= time (NULL
);
1766 int pid
= process_id ();
1771 int count_dates
= 0;
1775 for (p
= filename
; *p
; p
++) {
1786 if (!count_dates
&& !count_pids
)
1787 return pstrdup (filename
);
1788 snprintf (pid_buf
, sizeof (pid_buf
), "%d", pid
);
1790 snprintf (time_buf
, sizeof (time_buf
), "%d%02d%02d%02d%02d%02d",
1791 1900 + ts
->tm_year
, 1 + ts
->tm_mon
, ts
->tm_mday
, ts
->tm_hour
, ts
->tm_min
, ts
->tm_sec
);
1792 s_date
= strlen (time_buf
);
1793 s_pid
= strlen (pid_buf
);
1794 d
= res
= malloc (strlen (filename
) + s_date
* count_dates
+ s_pid
* count_pids
);
1795 for (p
= filename
; *p
; p
++) {
1802 strcpy (d
, time_buf
);
1805 } else if (*p
== 'p') {
1806 strcpy (d
, pid_buf
);
1809 } else if (*p
== '%') {
1821 #ifndef DISABLE_HELPER_THREAD
1823 helper_thread (void* arg
)
1825 MonoProfiler
* prof
= arg
;
1829 MonoThread
*thread
= NULL
;
1831 //fprintf (stderr, "Server listening\n");
1832 command_socket
= -1;
1838 FD_SET (prof
->server_socket
, &rfds
);
1839 max_fd
= prof
->server_socket
;
1840 FD_SET (prof
->pipes
[0], &rfds
);
1841 if (max_fd
< prof
->pipes
[0])
1842 max_fd
= prof
->pipes
[0];
1843 if (command_socket
>= 0) {
1844 FD_SET (command_socket
, &rfds
);
1845 if (max_fd
< command_socket
)
1846 max_fd
= command_socket
;
1850 FD_SET (perf_fd
, &rfds
);
1851 if (max_fd
< perf_fd
)
1857 len
= select (max_fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
1858 if (FD_ISSET (prof
->pipes
[0], &rfds
)) {
1860 int r
= read (prof
->pipes
[0], &c
, 1);
1861 if (r
== 1 && c
== 0) {
1862 StatBuffer
*sbuf
= prof
->stat_buffers
->next
->next
;
1863 prof
->stat_buffers
->next
->next
= NULL
;
1865 fprintf (stderr
, "stat buffer dump\n");
1866 dump_sample_hits (prof
, sbuf
, 1);
1867 free_buffer (sbuf
, sbuf
->size
);
1870 /* time to shut down */
1872 mono_thread_detach (thread
);
1874 fprintf (stderr
, "helper shutdown\n");
1877 read_perf_mmap (prof
);
1879 safe_dump (prof
, ensure_logbuf (0));
1883 if (perf_fd
>= 0 && FD_ISSET (perf_fd
, &rfds
)) {
1884 read_perf_mmap (prof
);
1885 safe_dump (prof
, ensure_logbuf (0));
1888 if (command_socket
>= 0 && FD_ISSET (command_socket
, &rfds
)) {
1889 len
= read (command_socket
, buf
, sizeof (buf
) - 1);
1893 close (command_socket
);
1894 command_socket
= -1;
1898 if (strcmp (buf
, "heapshot\n") == 0) {
1899 heapshot_requested
= 1;
1900 //fprintf (stderr, "perform heapshot\n");
1901 if (runtime_inited
&& !thread
) {
1902 thread
= mono_thread_attach (mono_get_root_domain ());
1903 /*fprintf (stderr, "attached\n");*/
1906 process_requests (prof
);
1907 mono_thread_detach (thread
);
1913 if (!FD_ISSET (prof
->server_socket
, &rfds
)) {
1916 command_socket
= accept (prof
->server_socket
, NULL
, NULL
);
1917 if (command_socket
< 0)
1919 //fprintf (stderr, "Accepted connection\n");
1925 start_helper_thread (MonoProfiler
* prof
)
1927 struct sockaddr_in server_address
;
1930 if (pipe (prof
->pipes
) < 0) {
1931 fprintf (stderr
, "Cannot create pipe\n");
1934 prof
->server_socket
= socket (PF_INET
, SOCK_STREAM
, 0);
1935 if (prof
->server_socket
< 0) {
1936 fprintf (stderr
, "Cannot create server socket\n");
1939 memset (&server_address
, 0, sizeof (server_address
));
1940 server_address
.sin_family
= AF_INET
;
1941 server_address
.sin_addr
.s_addr
= INADDR_ANY
;
1942 server_address
.sin_port
= htons (prof
->command_port
);
1943 if (bind (prof
->server_socket
, (struct sockaddr
*) &server_address
, sizeof (server_address
)) < 0) {
1944 fprintf (stderr
, "Cannot bind server socket, port: %d: %s\n", prof
->command_port
, strerror (errno
));
1945 close (prof
->server_socket
);
1948 if (listen (prof
->server_socket
, 1) < 0) {
1949 fprintf (stderr
, "Cannot listen server socket\n");
1950 close (prof
->server_socket
);
1953 if (getsockname (prof
->server_socket
, (struct sockaddr
*)&server_address
, &slen
) == 0) {
1954 prof
->command_port
= ntohs (server_address
.sin_port
);
1955 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
1958 r
= pthread_create (&prof
->helper_thread
, NULL
, helper_thread
, prof
);
1960 close (prof
->server_socket
);
1967 static MonoProfiler
*
1968 create_profiler (const char *filename
)
1972 int force_delete
= 0;
1973 int need_helper_thread
= 0;
1974 prof
= calloc (1, sizeof (MonoProfiler
));
1976 prof
->command_port
= command_port
;
1977 if (filename
&& *filename
== '-') {
1983 filename
= "|mprof-report -";
1985 filename
= "output.mlpd";
1986 nf
= (char*)filename
;
1988 nf
= new_filename (filename
);
1990 int s
= strlen (nf
) + 32;
1991 char *p
= malloc (s
);
1992 snprintf (p
, s
, "|mprof-report '--out=%s' -", nf
);
1998 prof
->file
= popen (nf
+ 1, "w");
1999 prof
->pipe_output
= 1;
2004 if ((f
= fopen (nf
, "r"))) {
2006 fprintf (stderr
, "The Mono profiler won't overwrite existing filename: %s.\n", nf
);
2007 fprintf (stderr
, "Profiling disabled: use a different name or -FILENAME to force overwrite.\n");
2011 prof
->file
= fopen (nf
, "wb");
2014 fprintf (stderr
, "Cannot create profiler output: %s\n", nf
);
2017 #if defined (HAVE_SYS_ZLIB)
2019 prof
->gzfile
= gzdopen (fileno (prof
->file
), "wb");
2022 if (sample_type
&& !do_mono_sample
)
2023 need_helper_thread
= setup_perf_event ();
2025 /* FIXME: warn if different freq or sample type */
2029 if (do_mono_sample
) {
2030 prof
->stat_buffers
= create_stat_buffer ();
2031 need_helper_thread
= 1;
2033 #ifndef DISABLE_HELPER_THREAD
2034 if (hs_mode_ondemand
|| need_helper_thread
) {
2035 if (!start_helper_thread (prof
))
2036 prof
->command_port
= 0;
2039 if (hs_mode_ondemand
)
2040 fprintf (stderr
, "Ondemand heapshot unavailable on this arch.\n");
2042 prof
->startup_time
= current_time ();
2050 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR
, LOG_VERSION_MINOR
, LOG_DATA_VERSION
);
2051 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
2052 printf ("Options:\n");
2053 printf ("\thelp show this usage info\n");
2054 printf ("\t[no]alloc enable/disable recording allocation info\n");
2055 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
2056 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
2057 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
2058 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/1000)\n");
2059 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
2060 printf ("\t TYPE can be followed by /FREQUENCY\n");
2061 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
2062 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
2063 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
2064 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
2065 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
2066 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
2067 printf ("\treport create a report instead of writing the raw data to a file\n");
2068 printf ("\tzip compress the output data\n");
2069 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
2075 match_option (const char* p
, const char *opt
, char **rval
)
2077 int len
= strlen (opt
);
2078 if (strncmp (p
, opt
, len
) == 0) {
2080 if (p
[len
] == '=' && p
[len
+ 1]) {
2081 const char *opt
= p
+ len
+ 1;
2082 const char *end
= strchr (opt
, ',');
2090 val
= malloc (l
+ 1);
2091 memcpy (val
, opt
, l
);
2096 if (p
[len
] == 0 || p
[len
] == ',') {
2098 return p
+ len
+ (p
[len
] == ',');
2116 static const SampleMode sample_modes
[] = {
2117 {"cycles", SAMPLE_CYCLES
},
2118 {"instr", SAMPLE_INSTRUCTIONS
},
2119 {"cachemiss", SAMPLE_CACHE_MISSES
},
2120 {"cacherefs", SAMPLE_CACHE_REFS
},
2121 {"branches", SAMPLE_BRANCHES
},
2122 {"branchmiss", SAMPLE_BRANCH_MISSES
},
2127 set_sample_mode (char* val
, int allow_empty
)
2130 char *maybe_freq
= NULL
;
2132 const SampleMode
*smode
= sample_modes
;
2133 #ifndef USE_PERF_EVENTS
2136 if (allow_empty
&& !val
) {
2137 sample_type
= SAMPLE_CYCLES
;
2141 if (strcmp (val
, "mono") == 0) {
2143 sample_type
= SAMPLE_CYCLES
;
2147 for (smode
= sample_modes
; smode
->name
; smode
++) {
2148 int l
= strlen (smode
->name
);
2149 if (strncmp (val
, smode
->name
, l
) == 0) {
2150 sample_type
= smode
->sample_mode
;
2151 maybe_freq
= val
+ l
;
2157 if (*maybe_freq
== '/') {
2158 count
= strtoul (maybe_freq
+ 1, &end
, 10);
2159 if (maybe_freq
+ 1 == end
)
2161 sample_freq
= count
;
2162 } else if (*maybe_freq
!= 0) {
2171 set_hsmode (char* val
, int allow_empty
)
2175 if (allow_empty
&& !val
)
2177 if (strcmp (val
, "ondemand") == 0) {
2178 hs_mode_ondemand
= 1;
2182 count
= strtoul (val
, &end
, 10);
2185 if (strcmp (end
, "ms") == 0)
2187 else if (strcmp (end
, "gc") == 0)
2195 * declaration to silence the compiler: this is the entry point that
2196 * mono will load from the shared library and call.
2199 mono_profiler_startup (const char *desc
);
2202 mono_profiler_startup (const char *desc
)
2205 char *filename
= NULL
;
2209 int calls_enabled
= 0;
2210 int allocs_enabled
= 0;
2211 int events
= MONO_PROFILE_GC
|MONO_PROFILE_ALLOCATIONS
|
2212 MONO_PROFILE_GC_MOVES
|MONO_PROFILE_CLASS_EVENTS
|MONO_PROFILE_THREADS
|
2213 MONO_PROFILE_ENTER_LEAVE
|MONO_PROFILE_JIT_COMPILATION
|MONO_PROFILE_EXCEPTIONS
|
2214 MONO_PROFILE_MONITOR_EVENTS
|MONO_PROFILE_MODULE_EVENTS
|MONO_PROFILE_GC_ROOTS
;
2217 if (strncmp (p
, "log", 3))
2222 for (; *p
; p
= opt
) {
2228 if ((opt
= match_option (p
, "help", NULL
)) != p
) {
2232 if ((opt
= match_option (p
, "calls", NULL
)) != p
) {
2236 if ((opt
= match_option (p
, "nocalls", NULL
)) != p
) {
2237 events
&= ~MONO_PROFILE_ENTER_LEAVE
;
2241 if ((opt
= match_option (p
, "alloc", NULL
)) != p
) {
2245 if ((opt
= match_option (p
, "noalloc", NULL
)) != p
) {
2246 events
&= ~MONO_PROFILE_ALLOCATIONS
;
2249 if ((opt
= match_option (p
, "time", &val
)) != p
) {
2250 if (strcmp (val
, "fast") == 0)
2252 else if (strcmp (val
, "null") == 0)
2259 if ((opt
= match_option (p
, "report", NULL
)) != p
) {
2263 if ((opt
= match_option (p
, "debug", NULL
)) != p
) {
2267 if ((opt
= match_option (p
, "heapshot", &val
)) != p
) {
2268 events
&= ~MONO_PROFILE_ALLOCATIONS
;
2269 events
&= ~MONO_PROFILE_ENTER_LEAVE
;
2272 set_hsmode (val
, 1);
2275 if ((opt
= match_option (p
, "sample", &val
)) != p
) {
2276 events
&= ~MONO_PROFILE_ALLOCATIONS
;
2277 events
&= ~MONO_PROFILE_ENTER_LEAVE
;
2279 set_sample_mode (val
, 1);
2282 if ((opt
= match_option (p
, "hsmode", &val
)) != p
) {
2283 fprintf (stderr
, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
2284 set_hsmode (val
, 0);
2287 if ((opt
= match_option (p
, "zip", NULL
)) != p
) {
2291 if ((opt
= match_option (p
, "output", &val
)) != p
) {
2295 if ((opt
= match_option (p
, "port", &val
)) != p
) {
2297 command_port
= strtoul (val
, &end
, 10);
2301 if ((opt
= match_option (p
, "maxframes", &val
)) != p
) {
2303 num_frames
= strtoul (val
, &end
, 10);
2304 if (num_frames
> MAX_FRAMES
)
2305 num_frames
= MAX_FRAMES
;
2307 notraces
= num_frames
== 0;
2310 if ((opt
= match_option (p
, "calldepth", &val
)) != p
) {
2312 max_call_depth
= strtoul (val
, &end
, 10);
2321 if (calls_enabled
) {
2322 events
|= MONO_PROFILE_ENTER_LEAVE
;
2326 events
|= MONO_PROFILE_ALLOCATIONS
;
2327 utils_init (fast_time
);
2329 prof
= create_profiler (filename
);
2334 mono_profiler_install (prof
, log_shutdown
);
2335 mono_profiler_install_gc (gc_event
, gc_resize
);
2336 mono_profiler_install_allocation (gc_alloc
);
2337 mono_profiler_install_gc_moves (gc_moves
);
2338 mono_profiler_install_gc_roots (gc_handle
, gc_roots
);
2339 mono_profiler_install_class (NULL
, class_loaded
, NULL
, NULL
);
2340 mono_profiler_install_module (NULL
, image_loaded
, NULL
, NULL
);
2341 mono_profiler_install_thread (thread_start
, thread_end
);
2342 mono_profiler_install_thread_name (thread_name
);
2343 mono_profiler_install_enter_leave (method_enter
, method_leave
);
2344 mono_profiler_install_jit_end (method_jitted
);
2345 mono_profiler_install_exception (throw_exc
, method_exc_leave
, clause_exc
);
2346 mono_profiler_install_monitor (monitor_event
);
2347 mono_profiler_install_runtime_initialized (runtime_initialized
);
2350 if (do_mono_sample
&& sample_type
== SAMPLE_CYCLES
) {
2351 events
|= MONO_PROFILE_STATISTICAL
;
2352 mono_profiler_install_statistical (mono_sample_hit
);
2355 mono_profiler_set_events (events
);
2357 TLS_INIT (tlsbuffer
);