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
31 #ifdef HAVE_EXECINFO_H
38 #ifndef DISABLE_HELPER_THREAD
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <sys/select.h>
51 #ifdef HAVE_SYS_STAT_H
58 #if defined (HAVE_SYS_ZLIB)
62 /* the architecture needs a memory fence */
63 #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
64 #include "perf_event.h"
65 #define USE_PERF_EVENTS 1
66 static int read_perf_mmap (MonoProfiler* prof);
69 #define BUFFER_SIZE (4096 * 16)
70 static int nocalls = 0;
71 static int notraces = 0;
72 static int use_zip = 0;
73 static int do_report = 0;
74 static int do_heap_shot = 0;
75 static int max_call_depth = 100;
76 static int runtime_inited = 0;
77 static int command_port = 0;
78 static int heapshot_requested = 0;
79 static int sample_type = 0;
80 static int sample_freq = 0;
81 static int do_mono_sample = 0;
82 static int in_shutdown = 0;
83 static int do_debug = 0;
85 /* For linux compile with:
86 * gcc -fPIC -shared -o libmono-profiler-log.so proflog.c utils.c -Wall -g -lz `pkg-config --cflags --libs mono-2`
87 * gcc -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
89 * For osx compile with:
90 * 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
91 * gcc -m32 -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
94 * sudo cp mprof-report /usr/local/bin
95 * sudo cp libmono-profiler-log.so /usr/local/lib
99 typedef struct _LogBuffer LogBuffer;
105 * The file is composed by a header followed by 0 or more buffers.
106 * Each buffer contains events that happened on a thread: for a given thread
107 * buffers that appear later in the file are guaranteed to contain events
108 * that happened later in time. Buffers from separate threads could be interleaved,
110 * Buffers are not required to be aligned.
113 * [id: 4 bytes] constant value: LOG_HEADER_ID
114 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
115 * [format: 1 byte] version of the data format for the rest of the file
116 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
117 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
118 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
119 * [flags: 4 bytes] file format flags, should be 0 for now
120 * [pid: 4 bytes] pid of the profiled process
121 * [port: 2 bytes] tcp port for server if != 0
122 * [sysid: 2 bytes] operating system and architecture identifier
124 * The multiple byte integers are in little-endian format.
127 * [buffer header] [event]*
128 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
129 * Timing information and other values in the event data are usually stored
130 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
131 * some data is represented as a difference between the actual value and
132 * either the last value of the same type (like for timing information) or
133 * as the difference from a value stored in a buffer header.
135 * For timing information the data is stored as uleb128, since timing
136 * increases in a monotonic way in each thread: the value is the number of
137 * nanoseconds to add to the last seen timing data in a buffer. The first value
138 * in a buffer will be calculated from the time_base field in the buffer head.
140 * Object or heap sizes are stored as uleb128.
141 * Pointer differences are stored as sleb128, instead.
143 * If an unexpected value is found, the rest of the buffer should be ignored,
144 * as generally the later values need the former to be interpreted correctly.
146 * buffer header format:
147 * [bufid: 4 bytes] constant value: BUF_ID
148 * [len: 4 bytes] size of the data following the buffer header
149 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
150 * [ptr_base: 8 bytes] base value for pointers
151 * [obj_base: 8 bytes] base value for object addresses
152 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
153 * [method_base: 8 bytes] base value for MonoMethod pointers
156 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
157 * The data that follows depends on type and the extended info.
158 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
159 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
160 * The extended info bits are interpreted based on type, see
161 * each individual event description below.
162 * strings are represented as a 0-terminated utf8 sequence.
165 * [flags: uleb128] must be 0
166 * [num: uleb128] number of frames following
167 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
171 * exinfo: flags: TYPE_ALLOC_BT
172 * [time diff: uleb128] nanoseconds since last timing
173 * [ptr: sleb128] class as a byte difference from ptr_base
174 * [obj: sleb128] object address as a byte difference from obj_base
175 * [size: uleb128] size of the object in the heap
176 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
180 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED,
181 * TYPE_GC_HANDLE_DESTROYED
182 * [time diff: uleb128] nanoseconds since last timing
183 * if exinfo == TYPE_GC_RESIZE
184 * [heap_size: uleb128] new heap size
185 * if exinfo == TYPE_GC_EVENT
186 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
187 * [generation: uleb128] GC generation event refers to
188 * if exinfo == TYPE_GC_MOVE
189 * [num_objects: uleb128] number of object moves that follow
190 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
191 * num is always an even number: the even items are the old
192 * addresses, the odd numbers are the respective new object addresses
193 * if exinfo == TYPE_GC_HANDLE_CREATED
194 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
195 * upper bits reserved as flags
196 * [handle: uleb128] GC handle value
197 * [objaddr: sleb128] object pointer differences from obj_base
198 * if exinfo == TYPE_GC_HANDLE_DESTROYED
199 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
200 * upper bits reserved as flags
201 * [handle: uleb128] GC handle value
203 * type metadata format:
204 * type: TYPE_METADATA
205 * exinfo: flags: TYPE_LOAD_ERR
206 * [time diff: uleb128] nanoseconds since last timing
207 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
209 * [pointer: sleb128] pointer of the metadata type depending on mtype
210 * if mtype == TYPE_CLASS
211 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
212 * [flags: uleb128] must be 0
213 * [name: string] full class name
214 * if mtype == TYPE_IMAGE
215 * [flags: uleb128] must be 0
216 * [name: string] image file name
217 * if mtype == TYPE_THREAD
218 * [flags: uleb128] must be 0
219 * [name: string] thread name
221 * type method format:
223 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
224 * [time diff: uleb128] nanoseconds since last timing
225 * [method: sleb128] MonoMethod* as a pointer difference from the last such
226 * pointer or the buffer method_base
227 * if exinfo == TYPE_JIT
228 * [code address: sleb128] pointer to the native code as a diff from ptr_base
229 * [code size: uleb128] size of the generated code
230 * [name: string] full method name
232 * type exception format:
233 * type: TYPE_EXCEPTION
234 * exinfo: TYPE_EXCEPTION_BT flag and one of: TYPE_THROW, TYPE_CLAUSE
235 * [time diff: uleb128] nanoseconds since last timing
236 * if exinfo.low3bits == TYPE_CLAUSE
237 * [clause type: uleb128] finally/catch/fault/filter
238 * [clause num: uleb128] the clause number in the method header
239 * [method: sleb128] MonoMethod* as a pointer difference from the last such
240 * pointer or the buffer method_base
241 * if exinfo.low3bits == TYPE_THROW
242 * [object: sleb128] the object that was thrown as a difference from obj_base
243 * If the TYPE_EXCEPTION_BT flag is set, a backtrace follows.
245 * type monitor format:
247 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
248 * [time diff: uleb128] nanoseconds since last timing
249 * [object: sleb128] the lock object as a difference from obj_base
250 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
251 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
255 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
256 * if exinfo == TYPE_HEAP_START
257 * [time diff: uleb128] nanoseconds since last timing
258 * if exinfo == TYPE_HEAP_END
259 * [time diff: uleb128] nanoseconds since last timing
260 * if exinfo == TYPE_HEAP_OBJECT
261 * [object: sleb128] the object as a difference from obj_base
262 * [class: sleb128] the object MonoClass* as a difference from ptr_base
263 * [size: uleb128] size of the object on the heap
264 * [num_refs: uleb128] number of object references
265 * if (format version > 1) each referenced objref is preceded by a
266 * uleb128 encoded offset: the first offset is from the object address
267 * and each next offset is relative to the previous one
268 * [objrefs: sleb128]+ object referenced as a difference from obj_base
269 * The same object can appear multiple times, but only the first time
270 * with size != 0: in the other cases this data will only be used to
271 * provide additional referenced objects.
272 * if exinfo == TYPE_HEAP_ROOT
273 * [num_roots: uleb128] number of root references
274 * [num_gc: uleb128] number of major gcs
275 * [object: sleb128] the object as a difference from obj_base
276 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
277 * [extra_info: uleb128] the extra_info value
278 * object, root_type_extra_info are repeated num_roots times
282 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN
283 * if exinfo == TYPE_SAMPLE_HIT
284 * [sample_type: uleb128] type of sample (SAMPLE_*)
285 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
286 * [count: uleb128] number of following instruction addresses
287 * [ip: sleb128]* instruction pointer as difference from ptr_base
288 * if exinfo == TYPE_SAMPLE_USYM
289 * [address: sleb128] symbol address as a difference from ptr_base
290 * [size: uleb128] symbol size (may be 0 if unknown)
291 * [name: string] symbol name
292 * if exinfo == TYPE_SAMPLE_UBIN
293 * [time diff: uleb128] nanoseconds since last timing
294 * [address: sleb128] address where binary has been loaded
295 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
296 * [size: uleb128] memory size
297 * [name: string] binary name
305 uintptr_t method_base;
306 uintptr_t last_method;
309 unsigned char* data_end;
314 unsigned char buf [1];
317 #define ENTER_LOG(lb,str) if ((lb)->locked) {write(2, str, strlen(str)); write(2, "\n", 1);return;} else {(lb)->locked++;}
318 #define EXIT_LOG(lb) (lb)->locked--;
320 typedef struct _StatBuffer StatBuffer;
329 typedef struct _BinaryObject BinaryObject;
331 struct _BinaryObject {
337 struct _MonoProfiler {
339 StatBuffer *stat_buffers;
341 #if defined (HAVE_SYS_ZLIB)
344 uint64_t startup_time;
346 int last_gc_gen_started;
351 pthread_t helper_thread;
353 BinaryObject *binary_objects;
357 #define TLS_SET(x,y) TlsSetValue(x, y)
358 #define TLS_GET(x) ((LogBuffer *) TlsGetValue(x))
359 #define TLS_INIT(x) x = TlsAlloc ()
360 static int tlsbuffer;
362 #define TLS_SET(x,y) x = y
365 static __thread LogBuffer* tlsbuffer = NULL;
367 #define TLS_SET(x,y) pthread_setspecific(x, y)
368 #define TLS_GET(x) ((LogBuffer *) pthread_getspecific(x))
369 #define TLS_INIT(x) pthread_key_create(&x, NULL)
370 static pthread_key_t tlsbuffer;
374 pstrdup (const char *s)
376 int len = strlen (s) + 1;
377 char *p = malloc (len);
383 create_stat_buffer (void)
385 StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
386 buf->size = BUFFER_SIZE;
387 buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
388 buf->data = buf->buf;
395 LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
396 buf->size = BUFFER_SIZE;
397 buf->time_base = current_time ();
398 buf->last_time = buf->time_base;
399 buf->data_end = (unsigned char*)buf + buf->size;
400 buf->data = buf->buf;
407 LogBuffer *logbuffer;
408 if (TLS_GET (tlsbuffer))
410 logbuffer = create_buffer ();
411 TLS_SET (tlsbuffer, logbuffer);
412 logbuffer->thread_id = thread_id ();
413 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
417 ensure_logbuf (int bytes)
419 LogBuffer *old = TLS_GET (tlsbuffer);
420 if (old && old->data + bytes + 100 < old->data_end)
422 TLS_SET (tlsbuffer, NULL);
424 TLS_GET (tlsbuffer)->next = old;
426 TLS_GET (tlsbuffer)->call_depth = old->call_depth;
427 //printf ("new logbuffer\n");
428 return TLS_GET (tlsbuffer);
432 emit_byte (LogBuffer *logbuffer, int value)
434 logbuffer->data [0] = value;
436 assert (logbuffer->data <= logbuffer->data_end);
440 emit_value (LogBuffer *logbuffer, int value)
442 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
443 assert (logbuffer->data <= logbuffer->data_end);
447 emit_time (LogBuffer *logbuffer, uint64_t value)
449 uint64_t tdiff = value - logbuffer->last_time;
451 if (value < logbuffer->last_time)
452 printf ("time went backwards\n");
453 //if (tdiff > 1000000)
454 // printf ("large time offset: %llu\n", tdiff);
456 encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
457 /*if (tdiff != decode_uleb128 (p, &p))
458 printf ("incorrect encoding: %llu\n", tdiff);*/
459 logbuffer->last_time = value;
460 assert (logbuffer->data <= logbuffer->data_end);
464 emit_svalue (LogBuffer *logbuffer, int64_t value)
466 encode_sleb128 (value, logbuffer->data, &logbuffer->data);
467 assert (logbuffer->data <= logbuffer->data_end);
471 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
473 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
474 assert (logbuffer->data <= logbuffer->data_end);
478 emit_ptr (LogBuffer *logbuffer, void *ptr)
480 if (!logbuffer->ptr_base)
481 logbuffer->ptr_base = (uintptr_t)ptr;
482 emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
483 assert (logbuffer->data <= logbuffer->data_end);
487 emit_method (LogBuffer *logbuffer, void *method)
489 if (!logbuffer->method_base) {
490 logbuffer->method_base = (intptr_t)method;
491 logbuffer->last_method = (intptr_t)method;
493 encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->data, &logbuffer->data);
494 logbuffer->last_method = (intptr_t)method;
495 assert (logbuffer->data <= logbuffer->data_end);
499 emit_obj (LogBuffer *logbuffer, void *ptr)
501 if (!logbuffer->obj_base)
502 logbuffer->obj_base = (uintptr_t)ptr >> 3;
503 emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
504 assert (logbuffer->data <= logbuffer->data_end);
508 write_int16 (char *buf, int32_t value)
511 for (i = 0; i < 2; ++i) {
519 write_int32 (char *buf, int32_t value)
522 for (i = 0; i < 4; ++i) {
530 write_int64 (char *buf, int64_t value)
533 for (i = 0; i < 8; ++i) {
541 dump_header (MonoProfiler *profiler)
545 p = write_int32 (p, LOG_HEADER_ID);
546 *p++ = LOG_VERSION_MAJOR;
547 *p++ = LOG_VERSION_MINOR;
548 *p++ = LOG_DATA_VERSION;
549 *p++ = sizeof (void*);
550 p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
551 p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
552 p = write_int32 (p, 0); /* flags */
553 p = write_int32 (p, process_id ()); /* pid */
554 p = write_int16 (p, profiler->command_port); /* port */
555 p = write_int16 (p, 0); /* opsystem */
556 #if defined (HAVE_SYS_ZLIB)
557 if (profiler->gzfile) {
558 gzwrite (profiler->gzfile, hbuf, p - hbuf);
560 fwrite (hbuf, p - hbuf, 1, profiler->file);
563 fwrite (hbuf, p - hbuf, 1, profiler->file);
568 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
573 dump_buffer (profiler, buf->next);
574 p = write_int32 (p, BUF_ID);
575 p = write_int32 (p, buf->data - buf->buf);
576 p = write_int64 (p, buf->time_base);
577 p = write_int64 (p, buf->ptr_base);
578 p = write_int64 (p, buf->obj_base);
579 p = write_int64 (p, buf->thread_id);
580 p = write_int64 (p, buf->method_base);
581 #if defined (HAVE_SYS_ZLIB)
582 if (profiler->gzfile) {
583 gzwrite (profiler->gzfile, hbuf, p - hbuf);
584 gzwrite (profiler->gzfile, buf->buf, buf->data - buf->buf);
587 fwrite (hbuf, p - hbuf, 1, profiler->file);
588 fwrite (buf->buf, buf->data - buf->buf, 1, profiler->file);
589 #if defined (HAVE_SYS_ZLIB)
592 free_buffer (buf, buf->size);
596 process_requests (MonoProfiler *profiler)
598 if (heapshot_requested)
599 mono_gc_collect (mono_gc_max_generation ());
603 runtime_initialized (MonoProfiler *profiler)
609 * Can be called only at safe callback locations.
612 safe_dump (MonoProfiler *profiler, LogBuffer *logbuffer)
614 int cd = logbuffer->call_depth;
616 dump_buffer (profiler, TLS_GET (tlsbuffer));
618 TLS_SET (tlsbuffer, NULL);
620 TLS_GET (tlsbuffer)->call_depth = cd;
624 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
627 uintptr_t last_offset = 0;
628 //const char *name = mono_class_get_name (klass);
629 LogBuffer *logbuffer = ensure_logbuf (20 + num * 8);
630 emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
631 emit_obj (logbuffer, obj);
632 emit_ptr (logbuffer, klass);
633 /* account for object alignment in the heap */
636 emit_value (logbuffer, size);
637 emit_value (logbuffer, num);
638 for (i = 0; i < num; ++i) {
639 emit_value (logbuffer, offsets [i] - last_offset);
640 last_offset = offsets [i];
641 emit_obj (logbuffer, refs [i]);
644 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
648 static unsigned int hs_mode_ms = 0;
649 static unsigned int hs_mode_gc = 0;
650 static unsigned int hs_mode_ondemand = 0;
651 static unsigned int gc_count = 0;
652 static uint64_t last_hs_time = 0;
655 heap_walk (MonoProfiler *profiler)
659 LogBuffer *logbuffer;
662 logbuffer = ensure_logbuf (10);
663 now = current_time ();
664 if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
666 else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
668 else if (hs_mode_ondemand && heapshot_requested)
670 else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
675 heapshot_requested = 0;
676 emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
677 emit_time (logbuffer, now);
678 mono_gc_walk_heap (0, gc_reference, NULL);
679 logbuffer = ensure_logbuf (10);
680 now = current_time ();
681 emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
682 emit_time (logbuffer, now);
687 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
689 LogBuffer *logbuffer = ensure_logbuf (10);
690 now = current_time ();
691 ENTER_LOG (logbuffer, "gcevent");
692 emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
693 emit_time (logbuffer, now);
694 emit_value (logbuffer, ev);
695 emit_value (logbuffer, generation);
696 /* to deal with nested gen1 after gen0 started */
697 if (ev == MONO_GC_EVENT_START) {
698 profiler->last_gc_gen_started = generation;
699 if (generation == mono_gc_max_generation ())
702 if (ev == MONO_GC_EVENT_PRE_START_WORLD)
703 heap_walk (profiler);
704 EXIT_LOG (logbuffer);
705 if (ev == MONO_GC_EVENT_POST_START_WORLD)
706 safe_dump (profiler, logbuffer);
707 //printf ("gc event %d for generation %d\n", ev, generation);
711 gc_resize (MonoProfiler *profiler, int64_t new_size) {
713 LogBuffer *logbuffer = ensure_logbuf (10);
714 now = current_time ();
715 ENTER_LOG (logbuffer, "gcresize");
716 emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
717 emit_time (logbuffer, now);
718 emit_value (logbuffer, new_size);
719 //printf ("gc resized to %lld\n", new_size);
720 EXIT_LOG (logbuffer);
723 #define MAX_FRAMES 16
726 MonoMethod* methods [MAX_FRAMES];
728 static int num_frames = MAX_FRAMES / 2;
731 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
733 FrameData *frame = data;
734 if (method && frame->count < num_frames) {
735 frame->methods [frame->count++] = method;
736 //printf ("In %d %s\n", frame->count, mono_method_get_name (method));
738 return frame->count == num_frames;
742 * a note about stack walks: they can cause more profiler events to fire,
743 * so we need to make sure they don't happen after we started emitting an
744 * event, hence the collect_bt/emit_bt split.
747 collect_bt (FrameData *data)
750 mono_stack_walk_no_il (walk_stack, data);
754 emit_bt (LogBuffer *logbuffer, FrameData *data)
756 /* FIXME: this is actually tons of data and we should
757 * just output it the first time and use an id the next
759 if (data->count > num_frames)
760 printf ("bad num frames: %d\n", data->count);
761 emit_value (logbuffer, 0); /* flags */
762 emit_value (logbuffer, data->count);
763 //if (*p != data.count) {
764 // 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);}
765 while (data->count) {
766 emit_ptr (logbuffer, data->methods [--data->count]);
771 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
775 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_ALLOC_BT: 0;
777 LogBuffer *logbuffer;
778 len = mono_object_get_size (obj);
779 /* account for object alignment in the heap */
784 logbuffer = ensure_logbuf (32 + MAX_FRAMES * 8);
785 now = current_time ();
786 ENTER_LOG (logbuffer, "gcalloc");
787 emit_byte (logbuffer, do_bt | TYPE_ALLOC);
788 emit_time (logbuffer, now);
789 emit_ptr (logbuffer, klass);
790 emit_obj (logbuffer, obj);
791 emit_value (logbuffer, len);
793 emit_bt (logbuffer, &data);
794 EXIT_LOG (logbuffer);
796 safe_dump (prof, logbuffer);
797 process_requests (prof);
798 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
802 gc_moves (MonoProfiler *prof, void **objects, int num)
806 LogBuffer *logbuffer = ensure_logbuf (10 + num * 8);
807 now = current_time ();
808 ENTER_LOG (logbuffer, "gcmove");
809 emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
810 emit_time (logbuffer, now);
811 emit_value (logbuffer, num);
812 for (i = 0; i < num; ++i)
813 emit_obj (logbuffer, objects [i]);
814 //printf ("gc moved %d objects\n", num/2);
815 EXIT_LOG (logbuffer);
819 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
822 LogBuffer *logbuffer = ensure_logbuf (5 + num * 18);
823 ENTER_LOG (logbuffer, "gcroots");
824 emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
825 emit_value (logbuffer, num);
826 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
827 for (i = 0; i < num; ++i) {
828 emit_obj (logbuffer, objects [i]);
829 emit_value (logbuffer, root_types [i]);
830 emit_value (logbuffer, extra_info [i]);
832 EXIT_LOG (logbuffer);
836 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
839 LogBuffer *logbuffer = ensure_logbuf (16);
840 now = current_time ();
841 ENTER_LOG (logbuffer, "gchandle");
842 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
843 emit_byte (logbuffer, TYPE_GC_HANDLE_CREATED | TYPE_GC);
844 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
845 emit_byte (logbuffer, TYPE_GC_HANDLE_DESTROYED | TYPE_GC);
848 emit_time (logbuffer, now);
849 emit_value (logbuffer, type);
850 emit_value (logbuffer, handle);
851 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
852 emit_obj (logbuffer, obj);
853 EXIT_LOG (logbuffer);
854 process_requests (prof);
858 push_nesting (char *p, MonoClass *klass)
863 nesting = mono_class_get_nesting_type (klass);
865 p = push_nesting (p, nesting);
869 name = mono_class_get_name (klass);
870 nspace = mono_class_get_namespace (klass);
873 p += strlen (nspace);
883 type_name (MonoClass *klass)
887 push_nesting (buf, klass);
888 p = malloc (strlen (buf) + 1);
894 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
899 LogBuffer *logbuffer;
900 if (result != MONO_PROFILE_OK)
902 name = mono_image_get_filename (image);
903 nlen = strlen (name) + 1;
904 logbuffer = ensure_logbuf (16 + nlen);
905 now = current_time ();
906 ENTER_LOG (logbuffer, "image");
907 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
908 emit_time (logbuffer, now);
909 emit_byte (logbuffer, TYPE_IMAGE);
910 emit_ptr (logbuffer, image);
911 emit_value (logbuffer, 0); /* flags */
912 memcpy (logbuffer->data, name, nlen);
913 logbuffer->data += nlen;
914 //printf ("loaded image %p (%s)\n", image, name);
915 EXIT_LOG (logbuffer);
917 safe_dump (prof, logbuffer);
918 process_requests (prof);
922 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
928 LogBuffer *logbuffer;
929 if (result != MONO_PROFILE_OK)
932 name = mono_type_get_name (mono_class_get_type (klass));
934 name = type_name (klass);
935 nlen = strlen (name) + 1;
936 image = mono_class_get_image (klass);
937 logbuffer = ensure_logbuf (24 + nlen);
938 now = current_time ();
939 ENTER_LOG (logbuffer, "class");
940 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
941 emit_time (logbuffer, now);
942 emit_byte (logbuffer, TYPE_CLASS);
943 emit_ptr (logbuffer, klass);
944 emit_ptr (logbuffer, image);
945 emit_value (logbuffer, 0); /* flags */
946 memcpy (logbuffer->data, name, nlen);
947 logbuffer->data += nlen;
948 //printf ("loaded class %p (%s)\n", klass, name);
953 EXIT_LOG (logbuffer);
955 safe_dump (prof, logbuffer);
956 process_requests (prof);
960 method_enter (MonoProfiler *prof, MonoMethod *method)
963 LogBuffer *logbuffer = ensure_logbuf (16);
964 if (logbuffer->call_depth++ > max_call_depth)
966 now = current_time ();
967 ENTER_LOG (logbuffer, "enter");
968 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
969 emit_time (logbuffer, now);
970 emit_method (logbuffer, method);
971 EXIT_LOG (logbuffer);
972 process_requests (prof);
976 method_leave (MonoProfiler *prof, MonoMethod *method)
979 LogBuffer *logbuffer = ensure_logbuf (16);
980 if (--logbuffer->call_depth > max_call_depth)
982 now = current_time ();
983 ENTER_LOG (logbuffer, "leave");
984 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
985 emit_time (logbuffer, now);
986 emit_method (logbuffer, method);
987 EXIT_LOG (logbuffer);
989 safe_dump (prof, logbuffer);
990 process_requests (prof);
994 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
997 LogBuffer *logbuffer;
1000 logbuffer = ensure_logbuf (16);
1001 if (--logbuffer->call_depth > max_call_depth)
1003 now = current_time ();
1004 ENTER_LOG (logbuffer, "eleave");
1005 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1006 emit_time (logbuffer, now);
1007 emit_method (logbuffer, method);
1008 EXIT_LOG (logbuffer);
1009 process_requests (prof);
1013 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result)
1018 LogBuffer *logbuffer;
1019 if (result != MONO_PROFILE_OK)
1021 name = mono_method_full_name (method, 1);
1022 nlen = strlen (name) + 1;
1023 logbuffer = ensure_logbuf (32 + nlen);
1024 now = current_time ();
1025 ENTER_LOG (logbuffer, "jit");
1026 emit_byte (logbuffer, TYPE_JIT | TYPE_METHOD);
1027 emit_time (logbuffer, now);
1028 emit_method (logbuffer, method);
1029 emit_ptr (logbuffer, mono_jit_info_get_code_start (jinfo));
1030 emit_value (logbuffer, mono_jit_info_get_code_size (jinfo));
1031 memcpy (logbuffer->data, name, nlen);
1032 logbuffer->data += nlen;
1034 EXIT_LOG (logbuffer);
1035 if (logbuffer->next)
1036 safe_dump (prof, logbuffer);
1037 process_requests (prof);
1041 throw_exc (MonoProfiler *prof, MonoObject *object)
1043 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_EXCEPTION_BT: 0;
1046 LogBuffer *logbuffer;
1049 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1050 now = current_time ();
1051 ENTER_LOG (logbuffer, "throw");
1052 emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1053 emit_time (logbuffer, now);
1054 emit_obj (logbuffer, object);
1056 emit_bt (logbuffer, &data);
1057 EXIT_LOG (logbuffer);
1058 process_requests (prof);
1062 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1065 LogBuffer *logbuffer = ensure_logbuf (16);
1066 now = current_time ();
1067 ENTER_LOG (logbuffer, "clause");
1068 emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1069 emit_time (logbuffer, now);
1070 emit_value (logbuffer, clause_type);
1071 emit_value (logbuffer, clause_num);
1072 emit_method (logbuffer, method);
1073 EXIT_LOG (logbuffer);
1077 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1079 int do_bt = (nocalls && runtime_inited && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1082 LogBuffer *logbuffer;
1085 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1086 now = current_time ();
1087 ENTER_LOG (logbuffer, "monitor");
1088 emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1089 emit_time (logbuffer, now);
1090 emit_obj (logbuffer, object);
1092 emit_bt (logbuffer, &data);
1093 EXIT_LOG (logbuffer);
1094 process_requests (profiler);
1098 thread_start (MonoProfiler *prof, uintptr_t tid)
1100 //printf ("thread start %p\n", (void*)tid);
1105 thread_end (MonoProfiler *prof, uintptr_t tid)
1108 if (TLS_GET (tlsbuffer))
1109 dump_buffer (prof, TLS_GET (tlsbuffer));
1111 TLS_SET (tlsbuffer, NULL);
1115 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1117 int len = strlen (name) + 1;
1119 LogBuffer *logbuffer;
1120 logbuffer = ensure_logbuf (10 + len);
1121 now = current_time ();
1122 ENTER_LOG (logbuffer, "tname");
1123 emit_byte (logbuffer, TYPE_METADATA);
1124 emit_time (logbuffer, now);
1125 emit_byte (logbuffer, TYPE_THREAD);
1126 emit_ptr (logbuffer, (void*)tid);
1127 emit_value (logbuffer, 0); /* flags */
1128 memcpy (logbuffer->data, name, len);
1129 logbuffer->data += len;
1130 EXIT_LOG (logbuffer);
1133 #include "mono/io-layer/atomic.h"
1135 #define cmp_exchange InterlockedCompareExchangePointer
1138 cmp_exchange (volatile void **dest, void *exch, void *comp)
1141 __asm__ __volatile__ ("lock; "
1148 : "=m" (*dest), "=a" (old)
1149 : "r" (exch), "m" (*dest), "a" (comp));
1156 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
1160 uintptr_t *data, *new_data, *old_data;
1163 now = current_time ();
1167 snprintf (buf, sizeof (buf), "hit at %p in thread %p at %llu\n", ip, (void*)thread_id (), now);
1169 write (2, buf, len);
1171 sbuf = profiler->stat_buffers;
1175 if (sbuf->data + 400 >= sbuf->data_end) {
1176 sbuf = create_stat_buffer ();
1177 sbuf->next = profiler->stat_buffers;
1178 profiler->stat_buffers = sbuf;
1180 write (2, "overflow\n", 9);
1181 /* notify the helper thread */
1182 if (sbuf->next->next) {
1184 write (profiler->pipes [1], &c, 1);
1186 write (2, "notify\n", 7);
1190 old_data = sbuf->data;
1191 new_data = old_data + 4;
1192 data = cmp_exchange ((volatile void**)&sbuf->data, new_data, old_data);
1193 } while (data != old_data);
1194 if (old_data >= sbuf->data_end)
1195 return; /* lost event */
1196 old_data [0] = 1 | (sample_type << 16);
1197 old_data [1] = thread_id ();
1198 old_data [2] = (now - profiler->startup_time) / 10000;
1199 old_data [3] = (uintptr_t)ip;
1202 static uintptr_t *code_pages = 0;
1203 static int num_code_pages = 0;
1204 static int size_code_pages = 0;
1205 #define CPAGE_SHIFT (9)
1206 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
1207 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
1208 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
1211 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
1214 uintptr_t start_pos;
1215 start_pos = (page >> CPAGE_SHIFT) % hsize;
1218 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
1220 } else if (!hash [i]) {
1227 } while (i != start_pos);
1228 /* should not happen */
1229 printf ("failed code page store\n");
1234 add_code_pointer (uintptr_t ip)
1237 if (num_code_pages * 2 >= size_code_pages) {
1239 uintptr_t old_size = size_code_pages;
1240 size_code_pages *= 2;
1241 if (size_code_pages == 0)
1242 size_code_pages = 16;
1243 n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
1244 for (i = 0; i < old_size; ++i) {
1246 add_code_page (n, size_code_pages, code_pages [i]);
1252 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
1256 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
1259 LogBuffer *logbuffer;
1261 len = strlen (filename) + 1;
1262 now = current_time ();
1263 logbuffer = ensure_logbuf (20 + len);
1264 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
1265 emit_time (logbuffer, now);
1266 emit_svalue (logbuffer, load_addr);
1267 emit_uvalue (logbuffer, offset);
1268 emit_uvalue (logbuffer, size);
1269 memcpy (logbuffer->data, filename, len);
1270 logbuffer->data += len;
1274 dump_usym (const char *name, uintptr_t value, uintptr_t size)
1276 LogBuffer *logbuffer;
1278 len = strlen (name) + 1;
1279 logbuffer = ensure_logbuf (20 + len);
1280 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
1281 emit_ptr (logbuffer, (void*)value);
1282 emit_value (logbuffer, size);
1283 memcpy (logbuffer->data, name, len);
1284 logbuffer->data += len;
1289 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
1292 for (i = 0; i < num_symbols; ++i) {
1294 sym = strtab + symbols [i].st_name;
1295 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
1297 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
1298 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
1303 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
1310 ElfW(Shdr) *sheader;
1311 ElfW(Shdr) *shstrtabh;
1312 ElfW(Shdr) *symtabh = NULL;
1313 ElfW(Shdr) *strtabh = NULL;
1314 ElfW(Sym) *symbols = NULL;
1318 fd = open (filename, O_RDONLY);
1321 if (fstat (fd, &statb) != 0) {
1325 file_size = statb.st_size;
1326 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1328 if (data == MAP_FAILED)
1331 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1332 header->e_ident [EI_MAG1] != ELFMAG1 ||
1333 header->e_ident [EI_MAG2] != ELFMAG2 ||
1334 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1335 munmap (data, file_size);
1338 sheader = (void*)((char*)data + header->e_shoff);
1339 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
1340 strtab = (const char*)data + shstrtabh->sh_offset;
1341 for (i = 0; i < header->e_shnum; ++i) {
1342 //printf ("section header: %d\n", sheader->sh_type);
1343 if (sheader->sh_type == SHT_SYMTAB) {
1345 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
1346 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
1349 sheader = (void*)((char*)sheader + header->e_shentsize);
1351 if (!symtabh || !strtabh) {
1352 munmap (data, file_size);
1355 strtab = (const char*)data + strtabh->sh_offset;
1356 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
1357 symbols = (void*)((char*)data + symtabh->sh_offset);
1358 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
1359 munmap (data, file_size);
1364 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1366 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
1368 MonoProfiler *prof = data;
1370 const char *filename;
1372 char *a = (void*)info->dlpi_addr;
1374 ElfW(Dyn) *dyn = NULL;
1375 ElfW(Sym) *symtab = NULL;
1376 ElfW(Word) *hash_table = NULL;
1377 ElfW(Ehdr) *header = NULL;
1378 const char* strtab = NULL;
1379 for (obj = prof->binary_objects; obj; obj = obj->next) {
1383 filename = info->dlpi_name;
1384 if (!info->dlpi_addr && !filename [0]) {
1385 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
1391 obj = calloc (sizeof (BinaryObject), 1);
1392 obj->addr = (void*)info->dlpi_addr;
1393 obj->name = pstrdup (filename);
1394 obj->next = prof->binary_objects;
1395 prof->binary_objects = obj;
1396 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
1398 for (i = 0; i < info->dlpi_phnum; ++i) {
1399 //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);
1400 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
1401 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1402 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1403 header->e_ident [EI_MAG1] != ELFMAG1 ||
1404 header->e_ident [EI_MAG2] != ELFMAG2 ||
1405 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1408 dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1409 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
1410 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1413 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
1415 if (!info->dlpi_name || !info->dlpi_name[0])
1419 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
1420 if (dyn [i].d_tag == DT_SYMTAB) {
1421 if (symtab && do_debug)
1422 printf ("multiple symtabs: %d\n", i);
1423 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
1424 } else if (dyn [i].d_tag == DT_HASH) {
1425 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
1426 } else if (dyn [i].d_tag == DT_STRTAB) {
1427 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
1432 num_sym = hash_table [1];
1433 dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
1438 load_binaries (MonoProfiler *prof)
1440 dl_iterate_phdr (elf_dl_callback, prof);
1445 load_binaries (MonoProfiler *prof)
1452 symbol_for (uintptr_t code)
1455 void *ip = (void*)code;
1457 if (dladdr (ip, &di)) {
1459 return di.dli_sname;
1462 names = backtrace_symbols (&ip, 1);
1464 const char* p = names [0];
1475 dump_unmanaged_coderefs (MonoProfiler *prof)
1478 const char* last_symbol;
1479 uintptr_t addr, page_end;
1481 if (load_binaries (prof))
1483 for (i = 0; i < size_code_pages; ++i) {
1485 if (!code_pages [i] || code_pages [i] & 1)
1488 addr = CPAGE_ADDR (code_pages [i]);
1489 page_end = addr + CPAGE_SIZE;
1490 code_pages [i] |= 1;
1491 /* we dump the symbols for the whole page */
1492 for (; addr < page_end; addr += 16) {
1493 sym = symbol_for (addr);
1494 if (sym && sym == last_symbol)
1499 dump_usym (sym, addr, 0); /* let's not guess the size */
1500 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
1506 dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf, int recurse)
1509 LogBuffer *logbuffer;
1512 if (recurse && sbuf->next) {
1513 dump_sample_hits (prof, sbuf->next, 1);
1514 free_buffer (sbuf->next, sbuf->next->size);
1517 for (sample = sbuf->buf; sample < sbuf->data;) {
1519 int count = sample [0] & 0xffff;
1520 int type = sample [0] >> 16;
1521 if (sample + count + 3 > sbuf->data)
1523 logbuffer = ensure_logbuf (20 + count * 8);
1524 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1525 emit_value (logbuffer, type);
1526 emit_uvalue (logbuffer, (prof->startup_time + sample [2]) * 10000);
1527 emit_value (logbuffer, count);
1528 for (i = 0; i < count; ++i) {
1529 emit_ptr (logbuffer, (void*)sample [i + 3]);
1530 add_code_pointer (sample [i + 3]);
1532 sample += count + 3;
1534 dump_unmanaged_coderefs (prof);
1538 #ifndef __NR_perf_event_open
1539 #define __NR_perf_event_open 241
1542 static int perf_fd = -1;
1543 static void *mmap_base;
1544 static struct perf_event_mmap_page *page_desc = NULL;
1545 static int num_pages = 64;
1546 static unsigned int mmap_mask;
1547 static unsigned int prev_pos = 0;
1550 struct perf_event_header h;
1560 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
1562 attr->size = PERF_ATTR_SIZE_VER0;
1563 //printf ("perf attr size: %d\n", attr->size);
1564 return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
1568 setup_perf_map (void)
1570 mmap_mask = num_pages * getpagesize () - 1;
1571 mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf_fd, 0);
1572 if (mmap_base == MAP_FAILED) {
1574 printf ("failed mmap\n");
1577 page_desc = mmap_base;
1579 printf ("mmap version: %d\n", page_desc->version);
1584 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
1586 LogBuffer *logbuffer;
1587 void *end = (char*)buf + size;
1589 int pid = getpid ();
1595 if (pid != s->pid) {
1597 printf ("event for different pid: %d\n", s->pid);
1598 buf = (char*)buf + s->h.size;
1601 /*ip = (void*)s->ip;
1602 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
1603 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
1604 logbuffer = ensure_logbuf (20 + s->nframes * 8);
1605 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1606 emit_value (logbuffer, sample_type);
1607 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
1608 emit_value (logbuffer, 1); /* count */
1609 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
1610 add_code_pointer (s->ip);
1611 buf = (char*)buf + s->h.size;
1615 printf ("dumped %d samples\n", samples);
1616 dump_unmanaged_coderefs (prof);
1619 /* read events from the ring buffer */
1621 read_perf_mmap (MonoProfiler* prof)
1624 unsigned char *data = (unsigned char*)mmap_base + getpagesize ();
1625 unsigned int head = page_desc->data_head;
1629 #if defined(__i386__)
1630 asm volatile("lock; addl $0,0(%%esp)":::"memory");
1631 #elif defined (__x86_64__)
1632 asm volatile("lfence":::"memory");
1639 printf ("lost mmap events: old: %d, head: %d\n", old, head);
1643 if ((old & mmap_mask) + size != (head & mmap_mask)) {
1644 buf = data + (old & mmap_mask);
1645 size = mmap_mask + 1 - (old & mmap_mask);
1647 /* size bytes at buf */
1649 printf ("found1 bytes of events: %d\n", size);
1650 dump_perf_hits (prof, buf, size);
1652 buf = data + (old & mmap_mask);
1654 /* size bytes at buf */
1656 printf ("found bytes of events: %d\n", size);
1657 dump_perf_hits (prof, buf, size);
1660 page_desc->data_tail = old;
1665 setup_perf_event (void)
1667 struct perf_event_attr attr;
1668 memset (&attr, 0, sizeof (attr));
1669 attr.type = PERF_TYPE_HARDWARE;
1670 switch (sample_type) {
1671 case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1672 case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
1673 case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
1674 case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
1675 case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
1676 case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
1677 default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1679 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
1680 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
1681 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
1684 attr.sample_freq = sample_freq;
1686 perf_fd = perf_event_syscall (&attr, getpid (), -1, -1, 0);
1688 printf ("perf fd: %d, freq: %d, event: %llu\n", perf_fd, sample_freq, attr.config);
1690 if (perf_fd == -EPERM) {
1691 fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
1694 perror ("open perf event");
1698 if (!setup_perf_map ()) {
1706 #endif /* USE_PERF_EVENTS */
1709 log_shutdown (MonoProfiler *prof)
1712 #ifndef DISABLE_HELPER_THREAD
1713 if (prof->command_port) {
1716 write (prof->pipes [1], &c, 1);
1717 pthread_join (prof->helper_thread, &res);
1722 read_perf_mmap (prof);
1724 dump_sample_hits (prof, prof->stat_buffers, 1);
1726 if (TLS_GET (tlsbuffer))
1727 dump_buffer (prof, TLS_GET (tlsbuffer));
1728 TLS_SET (tlsbuffer, NULL);
1730 #if defined (HAVE_SYS_ZLIB)
1732 gzclose (prof->gzfile);
1734 if (prof->pipe_output)
1735 pclose (prof->file);
1737 fclose (prof->file);
1742 new_filename (const char* filename)
1744 time_t t = time (NULL);
1745 int pid = process_id ();
1750 int count_dates = 0;
1754 for (p = filename; *p; p++) {
1765 if (!count_dates && !count_pids)
1766 return pstrdup (filename);
1767 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
1769 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
1770 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
1771 s_date = strlen (time_buf);
1772 s_pid = strlen (pid_buf);
1773 d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
1774 for (p = filename; *p; p++) {
1781 strcpy (d, time_buf);
1784 } else if (*p == 'p') {
1785 strcpy (d, pid_buf);
1788 } else if (*p == '%') {
1800 #ifndef DISABLE_HELPER_THREAD
1802 helper_thread (void* arg)
1804 MonoProfiler* prof = arg;
1808 MonoThread *thread = NULL;
1810 //fprintf (stderr, "Server listening\n");
1811 command_socket = -1;
1817 FD_SET (prof->server_socket, &rfds);
1818 max_fd = prof->server_socket;
1819 FD_SET (prof->pipes [0], &rfds);
1820 if (max_fd < prof->pipes [0])
1821 max_fd = prof->pipes [0];
1822 if (command_socket >= 0) {
1823 FD_SET (command_socket, &rfds);
1824 if (max_fd < command_socket)
1825 max_fd = command_socket;
1829 FD_SET (perf_fd, &rfds);
1830 if (max_fd < perf_fd)
1836 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
1837 if (FD_ISSET (prof->pipes [0], &rfds)) {
1839 int r = read (prof->pipes [0], &c, 1);
1840 if (r == 1 && c == 0) {
1841 StatBuffer *sbuf = prof->stat_buffers->next->next;
1842 prof->stat_buffers->next->next = NULL;
1844 fprintf (stderr, "stat buffer dump\n");
1845 dump_sample_hits (prof, sbuf, 1);
1846 free_buffer (sbuf, sbuf->size);
1849 /* time to shut down */
1851 mono_thread_detach (thread);
1853 fprintf (stderr, "helper shutdown\n");
1856 read_perf_mmap (prof);
1858 safe_dump (prof, ensure_logbuf (0));
1862 if (perf_fd >= 0 && FD_ISSET (perf_fd, &rfds)) {
1863 read_perf_mmap (prof);
1864 safe_dump (prof, ensure_logbuf (0));
1867 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
1868 len = read (command_socket, buf, sizeof (buf) - 1);
1872 close (command_socket);
1873 command_socket = -1;
1877 if (strcmp (buf, "heapshot\n") == 0) {
1878 heapshot_requested = 1;
1879 //fprintf (stderr, "perform heapshot\n");
1880 if (runtime_inited && !thread) {
1881 thread = mono_thread_attach (mono_get_root_domain ());
1882 /*fprintf (stderr, "attached\n");*/
1885 process_requests (prof);
1886 mono_thread_detach (thread);
1892 if (!FD_ISSET (prof->server_socket, &rfds)) {
1895 command_socket = accept (prof->server_socket, NULL, NULL);
1896 if (command_socket < 0)
1898 //fprintf (stderr, "Accepted connection\n");
1904 start_helper_thread (MonoProfiler* prof)
1906 struct sockaddr_in server_address;
1909 if (pipe (prof->pipes) < 0) {
1910 fprintf (stderr, "Cannot create pipe\n");
1913 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
1914 if (prof->server_socket < 0) {
1915 fprintf (stderr, "Cannot create server socket\n");
1918 memset (&server_address, 0, sizeof (server_address));
1919 server_address.sin_family = AF_INET;
1920 server_address.sin_addr.s_addr = INADDR_ANY;
1921 server_address.sin_port = htons (prof->command_port);
1922 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
1923 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
1924 close (prof->server_socket);
1927 if (listen (prof->server_socket, 1) < 0) {
1928 fprintf (stderr, "Cannot listen server socket\n");
1929 close (prof->server_socket);
1932 if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
1933 prof->command_port = ntohs (server_address.sin_port);
1934 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
1937 r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
1939 close (prof->server_socket);
1946 static MonoProfiler*
1947 create_profiler (const char *filename)
1951 int force_delete = 0;
1952 int need_helper_thread = 0;
1953 prof = calloc (1, sizeof (MonoProfiler));
1955 prof->command_port = command_port;
1956 if (filename && *filename == '-') {
1962 filename = "|mprof-report -";
1964 filename = "output.mlpd";
1967 nf = new_filename (filename);
1969 int s = strlen (nf) + 32;
1970 char *p = malloc (s);
1971 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
1977 prof->file = popen (nf + 1, "w");
1978 prof->pipe_output = 1;
1983 if ((f = fopen (nf, "r"))) {
1985 fprintf (stderr, "The Mono profiler won't overwrite existing filename: %s.\n", nf);
1986 fprintf (stderr, "Profiling disabled: use a different name or -FILENAME to force overwrite.\n");
1990 prof->file = fopen (nf, "wb");
1993 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
1996 #if defined (HAVE_SYS_ZLIB)
1998 prof->gzfile = gzdopen (fileno (prof->file), "wb");
2001 if (sample_type && !do_mono_sample)
2002 need_helper_thread = setup_perf_event ();
2004 /* FIXME: warn if different freq or sample type */
2008 if (do_mono_sample) {
2009 prof->stat_buffers = create_stat_buffer ();
2010 need_helper_thread = 1;
2012 #ifndef DISABLE_HELPER_THREAD
2013 if (hs_mode_ondemand || need_helper_thread) {
2014 if (!start_helper_thread (prof))
2015 prof->command_port = 0;
2018 if (hs_mode_ondemand)
2019 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
2021 prof->startup_time = current_time ();
2029 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
2030 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
2031 printf ("Options:\n");
2032 printf ("\thelp show this usage info\n");
2033 printf ("\t[no]alloc enable/disable recording allocation info\n");
2034 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
2035 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
2036 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
2037 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/1000)\n");
2038 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
2039 printf ("\t TYPE can be followed by /FREQUENCY\n");
2040 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
2041 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
2042 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
2043 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
2044 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
2045 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
2046 printf ("\treport create a report instead of writing the raw data to a file\n");
2047 printf ("\tzip compress the output data\n");
2048 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
2054 match_option (const char* p, const char *opt, char **rval)
2056 int len = strlen (opt);
2057 if (strncmp (p, opt, len) == 0) {
2059 if (p [len] == '=' && p [len + 1]) {
2060 const char *opt = p + len + 1;
2061 const char *end = strchr (opt, ',');
2069 val = malloc (l + 1);
2070 memcpy (val, opt, l);
2075 if (p [len] == 0 || p [len] == ',') {
2077 return p + len + (p [len] == ',');
2095 static const SampleMode sample_modes [] = {
2096 {"cycles", SAMPLE_CYCLES},
2097 {"instr", SAMPLE_INSTRUCTIONS},
2098 {"cachemiss", SAMPLE_CACHE_MISSES},
2099 {"cacherefs", SAMPLE_CACHE_REFS},
2100 {"branches", SAMPLE_BRANCHES},
2101 {"branchmiss", SAMPLE_BRANCH_MISSES},
2106 set_sample_mode (char* val, int allow_empty)
2109 char *maybe_freq = NULL;
2111 const SampleMode *smode = sample_modes;
2112 #ifndef USE_PERF_EVENTS
2115 if (allow_empty && !val) {
2116 sample_type = SAMPLE_CYCLES;
2120 if (strcmp (val, "mono") == 0) {
2122 sample_type = SAMPLE_CYCLES;
2126 for (smode = sample_modes; smode->name; smode++) {
2127 int l = strlen (smode->name);
2128 if (strncmp (val, smode->name, l) == 0) {
2129 sample_type = smode->sample_mode;
2130 maybe_freq = val + l;
2136 if (*maybe_freq == '/') {
2137 count = strtoul (maybe_freq + 1, &end, 10);
2138 if (maybe_freq + 1 == end)
2140 sample_freq = count;
2141 } else if (*maybe_freq != 0) {
2150 set_hsmode (char* val, int allow_empty)
2154 if (allow_empty && !val)
2156 if (strcmp (val, "ondemand") == 0) {
2157 hs_mode_ondemand = 1;
2161 count = strtoul (val, &end, 10);
2164 if (strcmp (end, "ms") == 0)
2166 else if (strcmp (end, "gc") == 0)
2174 * declaration to silence the compiler: this is the entry point that
2175 * mono will load from the shared library and call.
2178 mono_profiler_startup (const char *desc);
2181 mono_profiler_startup (const char *desc)
2184 char *filename = NULL;
2188 int calls_enabled = 0;
2189 int allocs_enabled = 0;
2190 int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
2191 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
2192 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
2193 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS;
2196 if (strncmp (p, "log", 3))
2201 for (; *p; p = opt) {
2207 if ((opt = match_option (p, "help", NULL)) != p) {
2211 if ((opt = match_option (p, "calls", NULL)) != p) {
2215 if ((opt = match_option (p, "nocalls", NULL)) != p) {
2216 events &= ~MONO_PROFILE_ENTER_LEAVE;
2220 if ((opt = match_option (p, "alloc", NULL)) != p) {
2224 if ((opt = match_option (p, "noalloc", NULL)) != p) {
2225 events &= ~MONO_PROFILE_ALLOCATIONS;
2228 if ((opt = match_option (p, "time", &val)) != p) {
2229 if (strcmp (val, "fast") == 0)
2231 else if (strcmp (val, "null") == 0)
2238 if ((opt = match_option (p, "report", NULL)) != p) {
2242 if ((opt = match_option (p, "debug", NULL)) != p) {
2246 if ((opt = match_option (p, "heapshot", &val)) != p) {
2247 events &= ~MONO_PROFILE_ALLOCATIONS;
2248 events &= ~MONO_PROFILE_ENTER_LEAVE;
2251 set_hsmode (val, 1);
2254 if ((opt = match_option (p, "sample", &val)) != p) {
2255 events &= ~MONO_PROFILE_ALLOCATIONS;
2256 events &= ~MONO_PROFILE_ENTER_LEAVE;
2258 set_sample_mode (val, 1);
2261 if ((opt = match_option (p, "hsmode", &val)) != p) {
2262 fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
2263 set_hsmode (val, 0);
2266 if ((opt = match_option (p, "zip", NULL)) != p) {
2270 if ((opt = match_option (p, "output", &val)) != p) {
2274 if ((opt = match_option (p, "port", &val)) != p) {
2276 command_port = strtoul (val, &end, 10);
2280 if ((opt = match_option (p, "maxframes", &val)) != p) {
2282 num_frames = strtoul (val, &end, 10);
2283 if (num_frames > MAX_FRAMES)
2284 num_frames = MAX_FRAMES;
2286 notraces = num_frames == 0;
2289 if ((opt = match_option (p, "calldepth", &val)) != p) {
2291 max_call_depth = strtoul (val, &end, 10);
2300 if (calls_enabled) {
2301 events |= MONO_PROFILE_ENTER_LEAVE;
2305 events |= MONO_PROFILE_ALLOCATIONS;
2306 utils_init (fast_time);
2308 prof = create_profiler (filename);
2313 mono_profiler_install (prof, log_shutdown);
2314 mono_profiler_install_gc (gc_event, gc_resize);
2315 mono_profiler_install_allocation (gc_alloc);
2316 mono_profiler_install_gc_moves (gc_moves);
2317 mono_profiler_install_gc_roots (gc_handle, gc_roots);
2318 mono_profiler_install_class (NULL, class_loaded, NULL, NULL);
2319 mono_profiler_install_module (NULL, image_loaded, NULL, NULL);
2320 mono_profiler_install_thread (thread_start, thread_end);
2321 mono_profiler_install_thread_name (thread_name);
2322 mono_profiler_install_enter_leave (method_enter, method_leave);
2323 mono_profiler_install_jit_end (method_jitted);
2324 mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
2325 mono_profiler_install_monitor (monitor_event);
2326 mono_profiler_install_runtime_initialized (runtime_initialized);
2329 if (do_mono_sample && sample_type == SAMPLE_CYCLES) {
2330 events |= MONO_PROFILE_STATISTICAL;
2331 mono_profiler_install_statistical (mono_sample_hit);
2334 mono_profiler_set_events (events);
2336 TLS_INIT (tlsbuffer);