2 * proflog.c: mono log profiler
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
8 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
12 #include <mono/metadata/profiler.h>
13 #include <mono/metadata/threads.h>
14 #include <mono/metadata/mono-gc.h>
15 #include <mono/metadata/debug-helpers.h>
16 #include <mono/utils/atomic.h>
17 #include <mono/utils/mono-membar.h>
18 #include <mono/utils/mono-counters.h>
28 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
29 #define DISABLE_HELPER_THREAD 1
38 #ifdef HAVE_EXECINFO_H
45 #ifndef DISABLE_HELPER_THREAD
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <sys/select.h>
58 #ifdef HAVE_SYS_STAT_H
65 #if defined (HAVE_SYS_ZLIB)
69 #if defined(__linux__)
71 #include <sys/syscall.h>
72 #include "perf_event.h"
73 #define USE_PERF_EVENTS 1
74 static int read_perf_mmap (MonoProfiler* prof, int cpu);
77 #define BUFFER_SIZE (4096 * 16)
78 static int nocalls = 0;
79 static int notraces = 0;
80 static int use_zip = 0;
81 static int do_report = 0;
82 static int do_heap_shot = 0;
83 static int max_call_depth = 100;
84 static int runtime_inited = 0;
85 static int command_port = 0;
86 static int heapshot_requested = 0;
87 static int sample_type = 0;
88 static int sample_freq = 0;
89 static int do_mono_sample = 0;
90 static int in_shutdown = 0;
91 static int do_debug = 0;
92 static int do_counters = 0;
94 /* For linux compile with:
95 * gcc -fPIC -shared -o libmono-profiler-log.so proflog.c utils.c -Wall -g -lz `pkg-config --cflags --libs mono-2`
96 * gcc -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
98 * For osx compile with:
99 * 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
100 * gcc -m32 -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
103 * sudo cp mprof-report /usr/local/bin
104 * sudo cp libmono-profiler-log.so /usr/local/lib
108 typedef struct _LogBuffer LogBuffer;
114 * The file is composed by a header followed by 0 or more buffers.
115 * Each buffer contains events that happened on a thread: for a given thread
116 * buffers that appear later in the file are guaranteed to contain events
117 * that happened later in time. Buffers from separate threads could be interleaved,
119 * Buffers are not required to be aligned.
122 * [id: 4 bytes] constant value: LOG_HEADER_ID
123 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
124 * [format: 1 byte] version of the data format for the rest of the file
125 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
126 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
127 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
128 * [flags: 4 bytes] file format flags, should be 0 for now
129 * [pid: 4 bytes] pid of the profiled process
130 * [port: 2 bytes] tcp port for server if != 0
131 * [sysid: 2 bytes] operating system and architecture identifier
133 * The multiple byte integers are in little-endian format.
136 * [buffer header] [event]*
137 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
138 * Timing information and other values in the event data are usually stored
139 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
140 * some data is represented as a difference between the actual value and
141 * either the last value of the same type (like for timing information) or
142 * as the difference from a value stored in a buffer header.
144 * For timing information the data is stored as uleb128, since timing
145 * increases in a monotonic way in each thread: the value is the number of
146 * nanoseconds to add to the last seen timing data in a buffer. The first value
147 * in a buffer will be calculated from the time_base field in the buffer head.
149 * Object or heap sizes are stored as uleb128.
150 * Pointer differences are stored as sleb128, instead.
152 * If an unexpected value is found, the rest of the buffer should be ignored,
153 * as generally the later values need the former to be interpreted correctly.
155 * buffer header format:
156 * [bufid: 4 bytes] constant value: BUF_ID
157 * [len: 4 bytes] size of the data following the buffer header
158 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
159 * [ptr_base: 8 bytes] base value for pointers
160 * [obj_base: 8 bytes] base value for object addresses
161 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
162 * [method_base: 8 bytes] base value for MonoMethod pointers
165 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
166 * The data that follows depends on type and the extended info.
167 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
168 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
169 * The extended info bits are interpreted based on type, see
170 * each individual event description below.
171 * strings are represented as a 0-terminated utf8 sequence.
174 * [flags: uleb128] must be 0
175 * [num: uleb128] number of frames following
176 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
180 * exinfo: flags: TYPE_ALLOC_BT
181 * [time diff: uleb128] nanoseconds since last timing
182 * [ptr: sleb128] class as a byte difference from ptr_base
183 * [obj: sleb128] object address as a byte difference from obj_base
184 * [size: uleb128] size of the object in the heap
185 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
189 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED,
190 * TYPE_GC_HANDLE_DESTROYED
191 * [time diff: uleb128] nanoseconds since last timing
192 * if exinfo == TYPE_GC_RESIZE
193 * [heap_size: uleb128] new heap size
194 * if exinfo == TYPE_GC_EVENT
195 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
196 * [generation: uleb128] GC generation event refers to
197 * if exinfo == TYPE_GC_MOVE
198 * [num_objects: uleb128] number of object moves that follow
199 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
200 * num is always an even number: the even items are the old
201 * addresses, the odd numbers are the respective new object addresses
202 * if exinfo == TYPE_GC_HANDLE_CREATED
203 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
204 * upper bits reserved as flags
205 * [handle: uleb128] GC handle value
206 * [objaddr: sleb128] object pointer differences from obj_base
207 * if exinfo == TYPE_GC_HANDLE_DESTROYED
208 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
209 * upper bits reserved as flags
210 * [handle: uleb128] GC handle value
212 * type metadata format:
213 * type: TYPE_METADATA
214 * exinfo: flags: TYPE_LOAD_ERR
215 * [time diff: uleb128] nanoseconds since last timing
216 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
218 * [pointer: sleb128] pointer of the metadata type depending on mtype
219 * if mtype == TYPE_CLASS
220 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
221 * [flags: uleb128] must be 0
222 * [name: string] full class name
223 * if mtype == TYPE_IMAGE
224 * [flags: uleb128] must be 0
225 * [name: string] image file name
226 * if mtype == TYPE_THREAD
227 * [flags: uleb128] must be 0
228 * [name: string] thread name
230 * type method format:
232 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
233 * [time diff: uleb128] nanoseconds since last timing
234 * [method: sleb128] MonoMethod* as a pointer difference from the last such
235 * pointer or the buffer method_base
236 * if exinfo == TYPE_JIT
237 * [code address: sleb128] pointer to the native code as a diff from ptr_base
238 * [code size: uleb128] size of the generated code
239 * [name: string] full method name
241 * type exception format:
242 * type: TYPE_EXCEPTION
243 * exinfo: TYPE_EXCEPTION_BT flag and one of: TYPE_THROW, TYPE_CLAUSE
244 * [time diff: uleb128] nanoseconds since last timing
245 * if exinfo.low3bits == TYPE_CLAUSE
246 * [clause type: uleb128] finally/catch/fault/filter
247 * [clause num: uleb128] the clause number in the method header
248 * [method: sleb128] MonoMethod* as a pointer difference from the last such
249 * pointer or the buffer method_base
250 * if exinfo.low3bits == TYPE_THROW
251 * [object: sleb128] the object that was thrown as a difference from obj_base
252 * If the TYPE_EXCEPTION_BT flag is set, a backtrace follows.
254 * type monitor format:
256 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
257 * [time diff: uleb128] nanoseconds since last timing
258 * [object: sleb128] the lock object as a difference from obj_base
259 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
260 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
264 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
265 * if exinfo == TYPE_HEAP_START
266 * [time diff: uleb128] nanoseconds since last timing
267 * if exinfo == TYPE_HEAP_END
268 * [time diff: uleb128] nanoseconds since last timing
269 * if exinfo == TYPE_HEAP_OBJECT
270 * [object: sleb128] the object as a difference from obj_base
271 * [class: sleb128] the object MonoClass* as a difference from ptr_base
272 * [size: uleb128] size of the object on the heap
273 * [num_refs: uleb128] number of object references
274 * if (format version > 1) each referenced objref is preceded by a
275 * uleb128 encoded offset: the first offset is from the object address
276 * and each next offset is relative to the previous one
277 * [objrefs: sleb128]+ object referenced as a difference from obj_base
278 * The same object can appear multiple times, but only the first time
279 * with size != 0: in the other cases this data will only be used to
280 * provide additional referenced objects.
281 * if exinfo == TYPE_HEAP_ROOT
282 * [num_roots: uleb128] number of root references
283 * [num_gc: uleb128] number of major gcs
284 * [object: sleb128] the object as a difference from obj_base
285 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
286 * [extra_info: uleb128] the extra_info value
287 * object, root_type and extra_info are repeated num_roots times
291 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
292 * if exinfo == TYPE_SAMPLE_HIT
293 * [sample_type: uleb128] type of sample (SAMPLE_*)
294 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
295 * [count: uleb128] number of following instruction addresses
296 * [ip: sleb128]* instruction pointer as difference from ptr_base
297 * if (format_version > 5)
298 * [mbt_count: uleb128] number of managed backtrace info triplets (method + IL offset + native offset)
299 * [method: sleb128]* MonoMethod* as a pointer difference from the last such
300 * pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
301 * [il_offset: sleb128]* IL offset inside method where the hit occurred
302 * [native_offset: sleb128]* native offset inside method where the hit occurred
303 * if exinfo == TYPE_SAMPLE_USYM
304 * [address: sleb128] symbol address as a difference from ptr_base
305 * [size: uleb128] symbol size (may be 0 if unknown)
306 * [name: string] symbol name
307 * if exinfo == TYPE_SAMPLE_UBIN
308 * [time diff: uleb128] nanoseconds since last timing
309 * [address: sleb128] address where binary has been loaded
310 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
311 * [size: uleb128] memory size
312 * [name: string] binary name
313 * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
314 * [len: uleb128] number of counters
316 * [section: uleb128] section name of counter
317 * [name: string] name of counter
318 * [type: uleb128] type name of counter
319 * [unit: uleb128] unit name of counter
320 * [variance: uleb128] variance name of counter
321 * [index: uleb128] unique index of counter
322 * if exinfo == TYPE_SAMPLE_COUNTERS
323 * [timestamp: uleb128] sampling timestamp
325 * [index: uleb128] unique index of counter
328 * [type: uleb128] type of counter value
331 * [0: uleb128] 0 -> value is null
333 * [1: uleb128] 1 -> value is not null
334 * [value: string] counter value
336 * [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
344 uintptr_t method_base;
345 uintptr_t last_method;
348 unsigned char* data_end;
353 unsigned char buf [1];
357 ign_res (int G_GNUC_UNUSED unused, ...)
361 #define ENTER_LOG(lb,str) if ((lb)->locked) {ign_res (write(2, str, strlen(str))); ign_res (write(2, "\n", 1));return;} else {(lb)->locked++;}
362 #define EXIT_LOG(lb) (lb)->locked--;
364 typedef struct _StatBuffer StatBuffer;
373 typedef struct _BinaryObject BinaryObject;
375 struct _BinaryObject {
381 struct _MonoProfiler {
383 StatBuffer *stat_buffers;
385 #if defined (HAVE_SYS_ZLIB)
388 uint64_t startup_time;
390 int last_gc_gen_started;
395 pthread_t helper_thread;
397 BinaryObject *binary_objects;
401 #define TLS_SET(x,y) TlsSetValue(x, y)
402 #define TLS_GET(x) ((LogBuffer *) TlsGetValue(x))
403 #define TLS_INIT(x) x = TlsAlloc ()
404 static int tlsbuffer;
406 #define TLS_SET(x,y) x = y
409 static __thread LogBuffer* tlsbuffer = NULL;
411 #define TLS_SET(x,y) pthread_setspecific(x, y)
412 #define TLS_GET(x) ((LogBuffer *) pthread_getspecific(x))
413 #define TLS_INIT(x) pthread_key_create(&x, NULL)
414 static pthread_key_t tlsbuffer;
417 static void safe_dump (MonoProfiler *profiler, LogBuffer *logbuffer);
420 pstrdup (const char *s)
422 int len = strlen (s) + 1;
423 char *p = malloc (len);
429 create_stat_buffer (void)
431 StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
432 buf->size = BUFFER_SIZE;
433 buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
434 buf->data = buf->buf;
441 LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
442 buf->size = BUFFER_SIZE;
443 buf->time_base = current_time ();
444 buf->last_time = buf->time_base;
445 buf->data_end = (unsigned char*)buf + buf->size;
446 buf->data = buf->buf;
453 LogBuffer *logbuffer;
454 if (TLS_GET (tlsbuffer))
456 logbuffer = create_buffer ();
457 TLS_SET (tlsbuffer, logbuffer);
458 logbuffer->thread_id = thread_id ();
459 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
463 ensure_logbuf (int bytes)
465 LogBuffer *old = TLS_GET (tlsbuffer);
466 if (old && old->data + bytes + 100 < old->data_end)
468 TLS_SET (tlsbuffer, NULL);
470 TLS_GET (tlsbuffer)->next = old;
472 TLS_GET (tlsbuffer)->call_depth = old->call_depth;
473 //printf ("new logbuffer\n");
474 return TLS_GET (tlsbuffer);
478 emit_byte (LogBuffer *logbuffer, int value)
480 logbuffer->data [0] = value;
482 assert (logbuffer->data <= logbuffer->data_end);
486 emit_value (LogBuffer *logbuffer, int value)
488 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
489 assert (logbuffer->data <= logbuffer->data_end);
493 emit_time (LogBuffer *logbuffer, uint64_t value)
495 uint64_t tdiff = value - logbuffer->last_time;
497 if (value < logbuffer->last_time)
498 printf ("time went backwards\n");
499 //if (tdiff > 1000000)
500 // printf ("large time offset: %llu\n", tdiff);
502 encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
503 /*if (tdiff != decode_uleb128 (p, &p))
504 printf ("incorrect encoding: %llu\n", tdiff);*/
505 logbuffer->last_time = value;
506 assert (logbuffer->data <= logbuffer->data_end);
510 emit_svalue (LogBuffer *logbuffer, int64_t value)
512 encode_sleb128 (value, logbuffer->data, &logbuffer->data);
513 assert (logbuffer->data <= logbuffer->data_end);
517 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
519 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
520 assert (logbuffer->data <= logbuffer->data_end);
524 emit_ptr (LogBuffer *logbuffer, void *ptr)
526 if (!logbuffer->ptr_base)
527 logbuffer->ptr_base = (uintptr_t)ptr;
528 emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
529 assert (logbuffer->data <= logbuffer->data_end);
533 emit_method (LogBuffer *logbuffer, void *method)
535 if (!logbuffer->method_base) {
536 logbuffer->method_base = (intptr_t)method;
537 logbuffer->last_method = (intptr_t)method;
539 encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->data, &logbuffer->data);
540 logbuffer->last_method = (intptr_t)method;
541 assert (logbuffer->data <= logbuffer->data_end);
545 emit_obj (LogBuffer *logbuffer, void *ptr)
547 if (!logbuffer->obj_base)
548 logbuffer->obj_base = (uintptr_t)ptr >> 3;
549 emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
550 assert (logbuffer->data <= logbuffer->data_end);
554 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
558 for (; i < size; i++) {
561 emit_byte (logbuffer, str [i]);
564 emit_byte (logbuffer, '\0');
568 emit_double (LogBuffer *logbuffer, double value)
571 unsigned char buffer[8];
572 memcpy (buffer, &value, 8);
573 #if G_BYTE_ORDER == G_BIG_ENDIAN
574 for (i = 7; i >= 0; i--)
576 for (i = 0; i < 8; i++)
578 emit_byte (logbuffer, buffer[i]);
582 write_int16 (char *buf, int32_t value)
585 for (i = 0; i < 2; ++i) {
593 write_int32 (char *buf, int32_t value)
596 for (i = 0; i < 4; ++i) {
604 write_int64 (char *buf, int64_t value)
607 for (i = 0; i < 8; ++i) {
615 dump_header (MonoProfiler *profiler)
619 p = write_int32 (p, LOG_HEADER_ID);
620 *p++ = LOG_VERSION_MAJOR;
621 *p++ = LOG_VERSION_MINOR;
622 *p++ = LOG_DATA_VERSION;
623 *p++ = sizeof (void*);
624 p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
625 p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
626 p = write_int32 (p, 0); /* flags */
627 p = write_int32 (p, process_id ()); /* pid */
628 p = write_int16 (p, profiler->command_port); /* port */
629 p = write_int16 (p, 0); /* opsystem */
630 #if defined (HAVE_SYS_ZLIB)
631 if (profiler->gzfile) {
632 gzwrite (profiler->gzfile, hbuf, p - hbuf);
634 fwrite (hbuf, p - hbuf, 1, profiler->file);
637 fwrite (hbuf, p - hbuf, 1, profiler->file);
638 fflush (profiler->file);
643 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
648 dump_buffer (profiler, buf->next);
649 p = write_int32 (p, BUF_ID);
650 p = write_int32 (p, buf->data - buf->buf);
651 p = write_int64 (p, buf->time_base);
652 p = write_int64 (p, buf->ptr_base);
653 p = write_int64 (p, buf->obj_base);
654 p = write_int64 (p, buf->thread_id);
655 p = write_int64 (p, buf->method_base);
656 #if defined (HAVE_SYS_ZLIB)
657 if (profiler->gzfile) {
658 gzwrite (profiler->gzfile, hbuf, p - hbuf);
659 gzwrite (profiler->gzfile, buf->buf, buf->data - buf->buf);
662 fwrite (hbuf, p - hbuf, 1, profiler->file);
663 fwrite (buf->buf, buf->data - buf->buf, 1, profiler->file);
664 fflush (profiler->file);
665 #if defined (HAVE_SYS_ZLIB)
668 free_buffer (buf, buf->size);
672 process_requests (MonoProfiler *profiler)
674 if (heapshot_requested)
675 mono_gc_collect (mono_gc_max_generation ());
678 static void counters_init (MonoProfiler *profiler);
681 runtime_initialized (MonoProfiler *profiler)
684 #ifndef DISABLE_HELPER_THREAD
685 counters_init (profiler);
687 /* ensure the main thread data and startup are available soon */
688 safe_dump (profiler, ensure_logbuf (0));
692 * Can be called only at safe callback locations.
695 safe_dump (MonoProfiler *profiler, LogBuffer *logbuffer)
697 int cd = logbuffer->call_depth;
699 dump_buffer (profiler, TLS_GET (tlsbuffer));
701 TLS_SET (tlsbuffer, NULL);
703 TLS_GET (tlsbuffer)->call_depth = cd;
707 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
710 uintptr_t last_offset = 0;
711 //const char *name = mono_class_get_name (klass);
712 LogBuffer *logbuffer = ensure_logbuf (20 + num * 8);
713 emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
714 emit_obj (logbuffer, obj);
715 emit_ptr (logbuffer, klass);
716 /* account for object alignment in the heap */
719 emit_value (logbuffer, size);
720 emit_value (logbuffer, num);
721 for (i = 0; i < num; ++i) {
722 emit_value (logbuffer, offsets [i] - last_offset);
723 last_offset = offsets [i];
724 emit_obj (logbuffer, refs [i]);
727 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
731 static unsigned int hs_mode_ms = 0;
732 static unsigned int hs_mode_gc = 0;
733 static unsigned int hs_mode_ondemand = 0;
734 static unsigned int gc_count = 0;
735 static uint64_t last_hs_time = 0;
738 heap_walk (MonoProfiler *profiler)
742 LogBuffer *logbuffer;
745 logbuffer = ensure_logbuf (10);
746 now = current_time ();
747 if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
749 else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
751 else if (hs_mode_ondemand)
752 do_walk = heapshot_requested;
753 else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
758 heapshot_requested = 0;
759 emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
760 emit_time (logbuffer, now);
761 mono_gc_walk_heap (0, gc_reference, NULL);
762 logbuffer = ensure_logbuf (10);
763 now = current_time ();
764 emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
765 emit_time (logbuffer, now);
770 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
772 LogBuffer *logbuffer = ensure_logbuf (10);
773 now = current_time ();
774 ENTER_LOG (logbuffer, "gcevent");
775 emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
776 emit_time (logbuffer, now);
777 emit_value (logbuffer, ev);
778 emit_value (logbuffer, generation);
779 /* to deal with nested gen1 after gen0 started */
780 if (ev == MONO_GC_EVENT_START) {
781 profiler->last_gc_gen_started = generation;
782 if (generation == mono_gc_max_generation ())
785 if (ev == MONO_GC_EVENT_PRE_START_WORLD)
786 heap_walk (profiler);
787 EXIT_LOG (logbuffer);
788 if (ev == MONO_GC_EVENT_POST_START_WORLD)
789 safe_dump (profiler, logbuffer);
790 //printf ("gc event %d for generation %d\n", ev, generation);
794 gc_resize (MonoProfiler *profiler, int64_t new_size) {
796 LogBuffer *logbuffer = ensure_logbuf (10);
797 now = current_time ();
798 ENTER_LOG (logbuffer, "gcresize");
799 emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
800 emit_time (logbuffer, now);
801 emit_value (logbuffer, new_size);
802 //printf ("gc resized to %lld\n", new_size);
803 EXIT_LOG (logbuffer);
806 #define MAX_FRAMES 16
809 MonoMethod* methods [MAX_FRAMES];
810 int32_t il_offsets [MAX_FRAMES];
811 int32_t native_offsets [MAX_FRAMES];
813 static int num_frames = MAX_FRAMES / 2;
816 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
818 FrameData *frame = data;
819 if (method && frame->count < num_frames) {
820 frame->il_offsets [frame->count] = il_offset;
821 frame->native_offsets [frame->count] = native_offset;
822 frame->methods [frame->count++] = method;
823 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
825 return frame->count == num_frames;
829 * a note about stack walks: they can cause more profiler events to fire,
830 * so we need to make sure they don't happen after we started emitting an
831 * event, hence the collect_bt/emit_bt split.
834 collect_bt (FrameData *data)
837 mono_stack_walk_no_il (walk_stack, data);
841 emit_bt (LogBuffer *logbuffer, FrameData *data)
843 /* FIXME: this is actually tons of data and we should
844 * just output it the first time and use an id the next
846 if (data->count > num_frames)
847 printf ("bad num frames: %d\n", data->count);
848 emit_value (logbuffer, 0); /* flags */
849 emit_value (logbuffer, data->count);
850 //if (*p != data.count) {
851 // 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);}
852 while (data->count) {
853 emit_ptr (logbuffer, data->methods [--data->count]);
858 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
862 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_ALLOC_BT: 0;
864 LogBuffer *logbuffer;
865 len = mono_object_get_size (obj);
866 /* account for object alignment in the heap */
871 logbuffer = ensure_logbuf (32 + MAX_FRAMES * 8);
872 now = current_time ();
873 ENTER_LOG (logbuffer, "gcalloc");
874 emit_byte (logbuffer, do_bt | TYPE_ALLOC);
875 emit_time (logbuffer, now);
876 emit_ptr (logbuffer, klass);
877 emit_obj (logbuffer, obj);
878 emit_value (logbuffer, len);
880 emit_bt (logbuffer, &data);
881 EXIT_LOG (logbuffer);
883 safe_dump (prof, logbuffer);
884 process_requests (prof);
885 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
889 gc_moves (MonoProfiler *prof, void **objects, int num)
893 LogBuffer *logbuffer = ensure_logbuf (10 + num * 8);
894 now = current_time ();
895 ENTER_LOG (logbuffer, "gcmove");
896 emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
897 emit_time (logbuffer, now);
898 emit_value (logbuffer, num);
899 for (i = 0; i < num; ++i)
900 emit_obj (logbuffer, objects [i]);
901 //printf ("gc moved %d objects\n", num/2);
902 EXIT_LOG (logbuffer);
906 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
909 LogBuffer *logbuffer = ensure_logbuf (5 + num * 18);
910 ENTER_LOG (logbuffer, "gcroots");
911 emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
912 emit_value (logbuffer, num);
913 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
914 for (i = 0; i < num; ++i) {
915 emit_obj (logbuffer, objects [i]);
916 emit_value (logbuffer, root_types [i]);
917 emit_value (logbuffer, extra_info [i]);
919 EXIT_LOG (logbuffer);
923 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
926 LogBuffer *logbuffer = ensure_logbuf (16);
927 now = current_time ();
928 ENTER_LOG (logbuffer, "gchandle");
929 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
930 emit_byte (logbuffer, TYPE_GC_HANDLE_CREATED | TYPE_GC);
931 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
932 emit_byte (logbuffer, TYPE_GC_HANDLE_DESTROYED | TYPE_GC);
935 emit_time (logbuffer, now);
936 emit_value (logbuffer, type);
937 emit_value (logbuffer, handle);
938 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
939 emit_obj (logbuffer, obj);
940 EXIT_LOG (logbuffer);
941 process_requests (prof);
945 push_nesting (char *p, MonoClass *klass)
950 nesting = mono_class_get_nesting_type (klass);
952 p = push_nesting (p, nesting);
956 name = mono_class_get_name (klass);
957 nspace = mono_class_get_namespace (klass);
960 p += strlen (nspace);
970 type_name (MonoClass *klass)
974 push_nesting (buf, klass);
975 p = malloc (strlen (buf) + 1);
981 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
986 LogBuffer *logbuffer;
987 if (result != MONO_PROFILE_OK)
989 name = mono_image_get_filename (image);
990 nlen = strlen (name) + 1;
991 logbuffer = ensure_logbuf (16 + nlen);
992 now = current_time ();
993 ENTER_LOG (logbuffer, "image");
994 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
995 emit_time (logbuffer, now);
996 emit_byte (logbuffer, TYPE_IMAGE);
997 emit_ptr (logbuffer, image);
998 emit_value (logbuffer, 0); /* flags */
999 memcpy (logbuffer->data, name, nlen);
1000 logbuffer->data += nlen;
1001 //printf ("loaded image %p (%s)\n", image, name);
1002 EXIT_LOG (logbuffer);
1003 if (logbuffer->next)
1004 safe_dump (prof, logbuffer);
1005 process_requests (prof);
1009 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1015 LogBuffer *logbuffer;
1016 if (result != MONO_PROFILE_OK)
1019 name = mono_type_get_name (mono_class_get_type (klass));
1021 name = type_name (klass);
1022 nlen = strlen (name) + 1;
1023 image = mono_class_get_image (klass);
1024 logbuffer = ensure_logbuf (24 + nlen);
1025 now = current_time ();
1026 ENTER_LOG (logbuffer, "class");
1027 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1028 emit_time (logbuffer, now);
1029 emit_byte (logbuffer, TYPE_CLASS);
1030 emit_ptr (logbuffer, klass);
1031 emit_ptr (logbuffer, image);
1032 emit_value (logbuffer, 0); /* flags */
1033 memcpy (logbuffer->data, name, nlen);
1034 logbuffer->data += nlen;
1035 //printf ("loaded class %p (%s)\n", klass, name);
1040 EXIT_LOG (logbuffer);
1041 if (logbuffer->next)
1042 safe_dump (prof, logbuffer);
1043 process_requests (prof);
1047 method_enter (MonoProfiler *prof, MonoMethod *method)
1050 LogBuffer *logbuffer = ensure_logbuf (16);
1051 if (logbuffer->call_depth++ > max_call_depth)
1053 now = current_time ();
1054 ENTER_LOG (logbuffer, "enter");
1055 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
1056 emit_time (logbuffer, now);
1057 emit_method (logbuffer, method);
1058 EXIT_LOG (logbuffer);
1059 process_requests (prof);
1063 method_leave (MonoProfiler *prof, MonoMethod *method)
1066 LogBuffer *logbuffer = ensure_logbuf (16);
1067 if (--logbuffer->call_depth > max_call_depth)
1069 now = current_time ();
1070 ENTER_LOG (logbuffer, "leave");
1071 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
1072 emit_time (logbuffer, now);
1073 emit_method (logbuffer, method);
1074 EXIT_LOG (logbuffer);
1075 if (logbuffer->next)
1076 safe_dump (prof, logbuffer);
1077 process_requests (prof);
1081 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
1084 LogBuffer *logbuffer;
1087 logbuffer = ensure_logbuf (16);
1088 if (--logbuffer->call_depth > max_call_depth)
1090 now = current_time ();
1091 ENTER_LOG (logbuffer, "eleave");
1092 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1093 emit_time (logbuffer, now);
1094 emit_method (logbuffer, method);
1095 EXIT_LOG (logbuffer);
1096 process_requests (prof);
1100 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result)
1105 LogBuffer *logbuffer;
1106 if (result != MONO_PROFILE_OK)
1108 name = mono_method_full_name (method, 1);
1109 nlen = strlen (name) + 1;
1110 logbuffer = ensure_logbuf (32 + nlen);
1111 now = current_time ();
1112 ENTER_LOG (logbuffer, "jit");
1113 emit_byte (logbuffer, TYPE_JIT | TYPE_METHOD);
1114 emit_time (logbuffer, now);
1115 emit_method (logbuffer, method);
1116 emit_ptr (logbuffer, mono_jit_info_get_code_start (jinfo));
1117 emit_value (logbuffer, mono_jit_info_get_code_size (jinfo));
1118 memcpy (logbuffer->data, name, nlen);
1119 logbuffer->data += nlen;
1121 EXIT_LOG (logbuffer);
1122 if (logbuffer->next)
1123 safe_dump (prof, logbuffer);
1124 process_requests (prof);
1128 throw_exc (MonoProfiler *prof, MonoObject *object)
1130 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_EXCEPTION_BT: 0;
1133 LogBuffer *logbuffer;
1136 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1137 now = current_time ();
1138 ENTER_LOG (logbuffer, "throw");
1139 emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1140 emit_time (logbuffer, now);
1141 emit_obj (logbuffer, object);
1143 emit_bt (logbuffer, &data);
1144 EXIT_LOG (logbuffer);
1145 process_requests (prof);
1149 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1152 LogBuffer *logbuffer = ensure_logbuf (16);
1153 now = current_time ();
1154 ENTER_LOG (logbuffer, "clause");
1155 emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1156 emit_time (logbuffer, now);
1157 emit_value (logbuffer, clause_type);
1158 emit_value (logbuffer, clause_num);
1159 emit_method (logbuffer, method);
1160 EXIT_LOG (logbuffer);
1164 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1166 int do_bt = (nocalls && runtime_inited && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1169 LogBuffer *logbuffer;
1172 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1173 now = current_time ();
1174 ENTER_LOG (logbuffer, "monitor");
1175 emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1176 emit_time (logbuffer, now);
1177 emit_obj (logbuffer, object);
1179 emit_bt (logbuffer, &data);
1180 EXIT_LOG (logbuffer);
1181 process_requests (profiler);
1185 thread_start (MonoProfiler *prof, uintptr_t tid)
1187 //printf ("thread start %p\n", (void*)tid);
1192 thread_end (MonoProfiler *prof, uintptr_t tid)
1195 if (TLS_GET (tlsbuffer))
1196 dump_buffer (prof, TLS_GET (tlsbuffer));
1198 TLS_SET (tlsbuffer, NULL);
1202 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1204 int len = strlen (name) + 1;
1206 LogBuffer *logbuffer;
1207 logbuffer = ensure_logbuf (10 + len);
1208 now = current_time ();
1209 ENTER_LOG (logbuffer, "tname");
1210 emit_byte (logbuffer, TYPE_METADATA);
1211 emit_time (logbuffer, now);
1212 emit_byte (logbuffer, TYPE_THREAD);
1213 emit_ptr (logbuffer, (void*)tid);
1214 emit_value (logbuffer, 0); /* flags */
1215 memcpy (logbuffer->data, name, len);
1216 logbuffer->data += len;
1217 EXIT_LOG (logbuffer);
1221 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
1226 uintptr_t *data, *new_data, *old_data;
1232 now = current_time ();
1233 collect_bt (&bt_data);
1234 elapsed = (now - profiler->startup_time) / 10000;
1238 snprintf (buf, sizeof (buf), "hit at %p in thread %p after %llu ms\n", ip, (void*)thread_id (), (unsigned long long int)elapsed/100);
1240 ign_res (write (2, buf, len));
1242 sbuf = profiler->stat_buffers;
1245 /* flush the buffer at 1 second intervals */
1246 if (sbuf->data > sbuf->buf && (elapsed - sbuf->buf [2]) > 100000) {
1249 /* overflow: 400 slots is a big enough number to reduce the chance of losing this event if many
1250 * threads hit this same spot at the same time
1252 if (timedout || (sbuf->data + 400 >= sbuf->data_end)) {
1253 StatBuffer *oldsb, *foundsb;
1254 sbuf = create_stat_buffer ();
1256 oldsb = profiler->stat_buffers;
1258 foundsb = InterlockedCompareExchangePointer ((void * volatile*)&profiler->stat_buffers, sbuf, oldsb);
1259 } while (foundsb != oldsb);
1261 ign_res (write (2, "overflow\n", 9));
1262 /* notify the helper thread */
1263 if (sbuf->next->next) {
1265 ign_res (write (profiler->pipes [1], &c, 1));
1267 ign_res (write (2, "notify\n", 7));
1271 old_data = sbuf->data;
1272 new_data = old_data + 4 + bt_data.count * 3;
1273 data = InterlockedCompareExchangePointer ((void * volatile*)&sbuf->data, new_data, old_data);
1274 } while (data != old_data);
1275 if (old_data >= sbuf->data_end)
1276 return; /* lost event */
1277 old_data [0] = 1 | (sample_type << 16) | (bt_data.count << 8);
1278 old_data [1] = thread_id ();
1279 old_data [2] = elapsed;
1280 old_data [3] = (uintptr_t)ip;
1281 for (i = 0; i < bt_data.count; ++i) {
1282 old_data [4+3*i] = (uintptr_t)bt_data.methods [i];
1283 old_data [4+3*i+1] = (uintptr_t)bt_data.il_offsets [i];
1284 old_data [4+3*i+2] = (uintptr_t)bt_data.native_offsets [i];
1288 static uintptr_t *code_pages = 0;
1289 static int num_code_pages = 0;
1290 static int size_code_pages = 0;
1291 #define CPAGE_SHIFT (9)
1292 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
1293 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
1294 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
1297 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
1300 uintptr_t start_pos;
1301 start_pos = (page >> CPAGE_SHIFT) % hsize;
1304 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
1306 } else if (!hash [i]) {
1313 } while (i != start_pos);
1314 /* should not happen */
1315 printf ("failed code page store\n");
1320 add_code_pointer (uintptr_t ip)
1323 if (num_code_pages * 2 >= size_code_pages) {
1325 uintptr_t old_size = size_code_pages;
1326 size_code_pages *= 2;
1327 if (size_code_pages == 0)
1328 size_code_pages = 16;
1329 n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
1330 for (i = 0; i < old_size; ++i) {
1332 add_code_page (n, size_code_pages, code_pages [i]);
1338 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
1341 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1343 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
1346 LogBuffer *logbuffer;
1348 len = strlen (filename) + 1;
1349 now = current_time ();
1350 logbuffer = ensure_logbuf (20 + len);
1351 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
1352 emit_time (logbuffer, now);
1353 emit_svalue (logbuffer, load_addr);
1354 emit_uvalue (logbuffer, offset);
1355 emit_uvalue (logbuffer, size);
1356 memcpy (logbuffer->data, filename, len);
1357 logbuffer->data += len;
1362 dump_usym (const char *name, uintptr_t value, uintptr_t size)
1364 LogBuffer *logbuffer;
1366 len = strlen (name) + 1;
1367 logbuffer = ensure_logbuf (20 + len);
1368 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
1369 emit_ptr (logbuffer, (void*)value);
1370 emit_value (logbuffer, size);
1371 memcpy (logbuffer->data, name, len);
1372 logbuffer->data += len;
1377 #if SIZEOF_VOID_P == 4
1378 #define ELF_WSIZE 32
1380 #define ELF_WSIZE 64
1383 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
1384 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
1385 #define _ElfW_1(e,w,t) e##w##t
1389 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
1392 for (i = 0; i < num_symbols; ++i) {
1394 sym = strtab + symbols [i].st_name;
1395 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
1397 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
1398 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
1403 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
1410 ElfW(Shdr) *sheader;
1411 ElfW(Shdr) *shstrtabh;
1412 ElfW(Shdr) *symtabh = NULL;
1413 ElfW(Shdr) *strtabh = NULL;
1414 ElfW(Sym) *symbols = NULL;
1418 fd = open (filename, O_RDONLY);
1421 if (fstat (fd, &statb) != 0) {
1425 file_size = statb.st_size;
1426 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1428 if (data == MAP_FAILED)
1431 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1432 header->e_ident [EI_MAG1] != ELFMAG1 ||
1433 header->e_ident [EI_MAG2] != ELFMAG2 ||
1434 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1435 munmap (data, file_size);
1438 sheader = (void*)((char*)data + header->e_shoff);
1439 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
1440 strtab = (const char*)data + shstrtabh->sh_offset;
1441 for (i = 0; i < header->e_shnum; ++i) {
1442 //printf ("section header: %d\n", sheader->sh_type);
1443 if (sheader->sh_type == SHT_SYMTAB) {
1445 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
1446 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
1449 sheader = (void*)((char*)sheader + header->e_shentsize);
1451 if (!symtabh || !strtabh) {
1452 munmap (data, file_size);
1455 strtab = (const char*)data + strtabh->sh_offset;
1456 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
1457 symbols = (void*)((char*)data + symtabh->sh_offset);
1458 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
1459 munmap (data, file_size);
1464 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1466 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
1468 MonoProfiler *prof = data;
1470 const char *filename;
1472 char *a = (void*)info->dlpi_addr;
1474 ElfW(Dyn) *dyn = NULL;
1475 ElfW(Sym) *symtab = NULL;
1476 ElfW(Word) *hash_table = NULL;
1477 ElfW(Ehdr) *header = NULL;
1478 const char* strtab = NULL;
1479 for (obj = prof->binary_objects; obj; obj = obj->next) {
1483 filename = info->dlpi_name;
1484 if (!info->dlpi_addr && !filename [0]) {
1485 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
1491 obj = calloc (sizeof (BinaryObject), 1);
1492 obj->addr = (void*)info->dlpi_addr;
1493 obj->name = pstrdup (filename);
1494 obj->next = prof->binary_objects;
1495 prof->binary_objects = obj;
1496 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
1498 for (i = 0; i < info->dlpi_phnum; ++i) {
1499 //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);
1500 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
1501 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1502 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1503 header->e_ident [EI_MAG1] != ELFMAG1 ||
1504 header->e_ident [EI_MAG2] != ELFMAG2 ||
1505 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1508 dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1509 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
1510 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1513 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
1515 if (!info->dlpi_name || !info->dlpi_name[0])
1519 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
1520 if (dyn [i].d_tag == DT_SYMTAB) {
1521 if (symtab && do_debug)
1522 printf ("multiple symtabs: %d\n", i);
1523 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
1524 } else if (dyn [i].d_tag == DT_HASH) {
1525 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
1526 } else if (dyn [i].d_tag == DT_STRTAB) {
1527 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
1532 num_sym = hash_table [1];
1533 dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
1538 load_binaries (MonoProfiler *prof)
1540 dl_iterate_phdr (elf_dl_callback, prof);
1545 load_binaries (MonoProfiler *prof)
1552 symbol_for (uintptr_t code)
1555 void *ip = (void*)code;
1557 if (dladdr (ip, &di)) {
1559 return di.dli_sname;
1562 names = backtrace_symbols (&ip, 1);
1564 const char* p = names [0];
1575 dump_unmanaged_coderefs (MonoProfiler *prof)
1578 const char* last_symbol;
1579 uintptr_t addr, page_end;
1581 if (load_binaries (prof))
1583 for (i = 0; i < size_code_pages; ++i) {
1585 if (!code_pages [i] || code_pages [i] & 1)
1588 addr = CPAGE_ADDR (code_pages [i]);
1589 page_end = addr + CPAGE_SIZE;
1590 code_pages [i] |= 1;
1591 /* we dump the symbols for the whole page */
1592 for (; addr < page_end; addr += 16) {
1593 sym = symbol_for (addr);
1594 if (sym && sym == last_symbol)
1599 dump_usym (sym, addr, 0); /* let's not guess the size */
1600 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
1606 dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf, int recurse)
1609 LogBuffer *logbuffer;
1612 if (recurse && sbuf->next) {
1613 dump_sample_hits (prof, sbuf->next, 1);
1614 free_buffer (sbuf->next, sbuf->next->size);
1617 for (sample = sbuf->buf; sample < sbuf->data;) {
1619 int count = sample [0] & 0xff;
1620 int mbt_count = (sample [0] & 0xff00) >> 8;
1621 int type = sample [0] >> 16;
1622 if (sample + count + 3 + mbt_count * 3 > sbuf->data)
1624 logbuffer = ensure_logbuf (20 + count * 8);
1625 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1626 emit_value (logbuffer, type);
1627 emit_uvalue (logbuffer, prof->startup_time + (uint64_t)sample [2] * (uint64_t)10000);
1628 emit_value (logbuffer, count);
1629 for (i = 0; i < count; ++i) {
1630 emit_ptr (logbuffer, (void*)sample [i + 3]);
1631 add_code_pointer (sample [i + 3]);
1633 sample += count + 3;
1634 /* new in data version 6 */
1635 emit_uvalue (logbuffer, mbt_count);
1636 for (i = 0; i < mbt_count; ++i) {
1637 emit_method (logbuffer, (void*)sample [i * 3]); /* method */
1638 emit_svalue (logbuffer, sample [i * 3 + 1]); /* il offset */
1639 emit_svalue (logbuffer, sample [i * 3 + 2]); /* native offset */
1641 sample += 3 * mbt_count;
1643 dump_unmanaged_coderefs (prof);
1647 #ifndef __NR_perf_event_open
1649 #define __NR_perf_event_open 364
1651 #define __NR_perf_event_open 241
1656 mono_cpu_count (void)
1659 #ifdef PLATFORM_ANDROID
1660 /* Android tries really hard to save power by powering off CPUs on SMP phones which
1661 * means the normal way to query cpu count returns a wrong value with userspace API.
1662 * Instead we use /sys entries to query the actual hardware CPU count.
1664 char buffer[8] = {'\0'};
1665 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
1666 /* Format of the /sys entry is a cpulist of indexes which in the case
1667 * of present is always of the form "0-(n-1)" when there is more than
1668 * 1 core, n being the number of CPU cores in the system. Otherwise
1669 * the value is simply 0
1671 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
1672 count = strtol (((char*)buffer) + 2, NULL, 10);
1678 #ifdef _SC_NPROCESSORS_ONLN
1679 count = sysconf (_SC_NPROCESSORS_ONLN);
1686 size_t len = sizeof (int);
1689 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
1696 GetSystemInfo (&info);
1697 return info.dwNumberOfProcessors;
1706 unsigned int prev_pos;
1708 struct perf_event_mmap_page *page_desc;
1711 static PerfData *perf_data = NULL;
1712 static int num_perf;
1713 #define PERF_PAGES_SHIFT 4
1714 static int num_pages = 1 << PERF_PAGES_SHIFT;
1715 static unsigned int mmap_mask;
1718 struct perf_event_header h;
1728 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
1730 attr->size = PERF_ATTR_SIZE_VER0;
1731 //printf ("perf attr size: %d\n", attr->size);
1732 #if defined(__x86_64__)
1733 return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
1734 #elif defined(__i386__)
1735 return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
1736 #elif defined(__arm__)
1737 return syscall(/*__NR_perf_event_open*/ 364, attr, pid, cpu, group_fd, flags);
1744 setup_perf_map (PerfData *perf)
1746 perf->mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf->perf_fd, 0);
1747 if (perf->mmap_base == MAP_FAILED) {
1749 printf ("failed mmap\n");
1752 perf->page_desc = perf->mmap_base;
1754 printf ("mmap version: %d\n", perf->page_desc->version);
1759 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
1761 LogBuffer *logbuffer;
1762 void *end = (char*)buf + size;
1764 int pid = getpid ();
1770 if (pid != s->pid) {
1772 printf ("event for different pid: %d\n", s->pid);
1773 buf = (char*)buf + s->h.size;
1776 /*ip = (void*)s->ip;
1777 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
1778 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
1779 logbuffer = ensure_logbuf (20 + s->nframes * 8);
1780 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1781 emit_value (logbuffer, sample_type);
1782 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
1783 emit_value (logbuffer, 1); /* count */
1784 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
1785 /* no support here yet for the managed backtrace */
1786 emit_uvalue (logbuffer, 0);
1787 add_code_pointer (s->ip);
1788 buf = (char*)buf + s->h.size;
1792 printf ("dumped %d samples\n", samples);
1793 dump_unmanaged_coderefs (prof);
1796 /* read events from the ring buffer */
1798 read_perf_mmap (MonoProfiler* prof, int cpu)
1800 PerfData *perf = perf_data + cpu;
1802 unsigned char *data = (unsigned char*)perf->mmap_base + getpagesize ();
1803 unsigned int head = perf->page_desc->data_head;
1807 mono_memory_read_barrier ();
1809 old = perf->prev_pos;
1813 printf ("lost mmap events: old: %d, head: %d\n", old, head);
1817 if ((old & mmap_mask) + size != (head & mmap_mask)) {
1818 buf = data + (old & mmap_mask);
1819 size = mmap_mask + 1 - (old & mmap_mask);
1821 /* size bytes at buf */
1823 printf ("found1 bytes of events: %d\n", size);
1824 dump_perf_hits (prof, buf, size);
1826 buf = data + (old & mmap_mask);
1828 /* size bytes at buf */
1830 printf ("found bytes of events: %d\n", size);
1831 dump_perf_hits (prof, buf, size);
1833 perf->prev_pos = old;
1834 perf->page_desc->data_tail = old;
1839 setup_perf_event_for_cpu (PerfData *perf, int cpu)
1841 struct perf_event_attr attr;
1842 memset (&attr, 0, sizeof (attr));
1843 attr.type = PERF_TYPE_HARDWARE;
1844 switch (sample_type) {
1845 case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1846 case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
1847 case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
1848 case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
1849 case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
1850 case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
1851 default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1853 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
1854 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
1855 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
1858 attr.sample_freq = sample_freq;
1860 perf->perf_fd = perf_event_syscall (&attr, getpid (), cpu, -1, 0);
1862 printf ("perf fd: %d, freq: %d, event: %llu\n", perf->perf_fd, sample_freq, attr.config);
1863 if (perf->perf_fd < 0) {
1864 if (perf->perf_fd == -EPERM) {
1865 fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
1868 perror ("open perf event");
1872 if (!setup_perf_map (perf)) {
1873 close (perf->perf_fd);
1881 setup_perf_event (void)
1884 mmap_mask = num_pages * getpagesize () - 1;
1885 num_perf = mono_cpu_count ();
1886 perf_data = calloc (num_perf, sizeof (PerfData));
1887 for (i = 0; i < num_perf; ++i) {
1888 count += setup_perf_event_for_cpu (perf_data + i, i);
1897 #endif /* USE_PERF_EVENTS */
1899 #ifndef DISABLE_HELPER_THREAD
1901 typedef struct MonoCounterAgent {
1902 MonoCounter *counter;
1903 // MonoCounterAgent specific data :
1907 struct MonoCounterAgent *next;
1910 static MonoCounterAgent* counters;
1911 static gboolean counters_initialized = FALSE;
1912 static int counters_index = 1;
1915 counters_init_add_counter (MonoCounter *counter, gpointer data)
1917 MonoCounterAgent *agent, *item;
1919 for (agent = counters; agent; agent = agent->next) {
1920 if (agent->counter == counter)
1924 agent = malloc (sizeof (MonoCounterAgent));
1925 agent->counter = counter;
1926 agent->value = NULL;
1927 agent->value_size = 0;
1928 agent->index = counters_index++;
1944 counters_init (MonoProfiler *profiler)
1946 MonoCounterAgent *agent;
1947 LogBuffer *logbuffer;
1948 int size = 1 + 5, len = 0;
1950 mono_counters_foreach (counters_init_add_counter, NULL);
1952 for (agent = counters; agent; agent = agent->next) {
1953 size += strlen (mono_counter_get_name (agent->counter)) + 1 + 5 * 5;
1957 logbuffer = ensure_logbuf (size);
1959 ENTER_LOG (logbuffer, "counters");
1960 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
1961 emit_value (logbuffer, len);
1962 for (agent = counters; agent; agent = agent->next) {
1963 const char *name = mono_counter_get_name (agent->counter);
1964 emit_value (logbuffer, mono_counter_get_section (agent->counter));
1965 emit_string (logbuffer, name, strlen (name) + 1);
1966 emit_value (logbuffer, mono_counter_get_type (agent->counter));
1967 emit_value (logbuffer, mono_counter_get_unit (agent->counter));
1968 emit_value (logbuffer, mono_counter_get_variance (agent->counter));
1969 emit_value (logbuffer, agent->index);
1971 EXIT_LOG (logbuffer);
1973 counters_initialized = TRUE;
1977 counters_sample (MonoProfiler *profiler, uint64_t timestamp)
1979 MonoCounterAgent *agent;
1980 MonoCounter *counter;
1981 LogBuffer *logbuffer;
1987 if (!counters_initialized)
1991 buffer = calloc (1, buffer_size);
1994 for (agent = counters; agent; agent = agent->next)
1995 size += 10 * 2 + mono_counter_get_size (agent->counter);
1997 logbuffer = ensure_logbuf (size);
1999 ENTER_LOG (logbuffer, "counters");
2000 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
2001 emit_uvalue (logbuffer, timestamp);
2002 for (agent = counters; agent; agent = agent->next) {
2005 counter = agent->counter;
2007 size = mono_counter_get_size (counter);
2009 continue; // FIXME error
2010 } else if (size > buffer_size) {
2012 buffer = realloc (buffer, buffer_size);
2015 memset (buffer, 0, buffer_size);
2017 if (mono_counters_sample (counter, buffer, size) < 0)
2018 continue; // FIXME error
2020 type = mono_counter_get_type (counter);
2022 if (!agent->value) {
2023 agent->value = calloc (1, size);
2024 agent->value_size = size;
2026 if (type == MONO_COUNTER_STRING) {
2027 if (strcmp (agent->value, buffer) == 0)
2030 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
2035 emit_uvalue (logbuffer, agent->index);
2036 emit_uvalue (logbuffer, type);
2038 case MONO_COUNTER_INT:
2039 #if SIZEOF_VOID_P == 4
2040 case MONO_COUNTER_WORD:
2042 emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
2044 case MONO_COUNTER_UINT:
2045 emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
2047 case MONO_COUNTER_TIME_INTERVAL:
2048 case MONO_COUNTER_LONG:
2049 #if SIZEOF_VOID_P == 8
2050 case MONO_COUNTER_WORD:
2052 emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
2054 case MONO_COUNTER_ULONG:
2055 emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
2057 case MONO_COUNTER_DOUBLE:
2058 emit_double (logbuffer, *(double*)buffer);
2060 case MONO_COUNTER_STRING:
2062 emit_byte (logbuffer, 0);
2064 emit_byte (logbuffer, 1);
2065 emit_string (logbuffer, (char*)buffer, size);
2072 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
2073 agent->value = realloc (agent->value, size);
2074 agent->value_size = size;
2078 memcpy (agent->value, buffer, size);
2082 emit_value (logbuffer, 0);
2083 EXIT_LOG (logbuffer);
2085 safe_dump (profiler, ensure_logbuf (0));
2088 #endif /* DISABLE_HELPER_THREAD */
2091 log_shutdown (MonoProfiler *prof)
2094 #ifndef DISABLE_HELPER_THREAD
2095 if (prof->command_port) {
2098 ign_res (write (prof->pipes [1], &c, 1));
2099 pthread_join (prof->helper_thread, &res);
2105 for (i = 0; i < num_perf; ++i)
2106 read_perf_mmap (prof, i);
2109 dump_sample_hits (prof, prof->stat_buffers, 1);
2111 if (TLS_GET (tlsbuffer))
2112 dump_buffer (prof, TLS_GET (tlsbuffer));
2113 TLS_SET (tlsbuffer, NULL);
2115 #if defined (HAVE_SYS_ZLIB)
2117 gzclose (prof->gzfile);
2119 if (prof->pipe_output)
2120 pclose (prof->file);
2122 fclose (prof->file);
2127 new_filename (const char* filename)
2129 time_t t = time (NULL);
2130 int pid = process_id ();
2135 int count_dates = 0;
2139 for (p = filename; *p; p++) {
2150 if (!count_dates && !count_pids)
2151 return pstrdup (filename);
2152 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
2154 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
2155 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
2156 s_date = strlen (time_buf);
2157 s_pid = strlen (pid_buf);
2158 d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
2159 for (p = filename; *p; p++) {
2166 strcpy (d, time_buf);
2169 } else if (*p == 'p') {
2170 strcpy (d, pid_buf);
2173 } else if (*p == '%') {
2185 #ifndef DISABLE_HELPER_THREAD
2187 helper_thread (void* arg)
2189 MonoProfiler* prof = arg;
2193 MonoThread *thread = NULL;
2194 uint64_t start, now;
2196 //fprintf (stderr, "Server listening\n");
2197 start = current_time ();
2198 command_socket = -1;
2204 FD_SET (prof->server_socket, &rfds);
2205 max_fd = prof->server_socket;
2206 FD_SET (prof->pipes [0], &rfds);
2207 if (max_fd < prof->pipes [0])
2208 max_fd = prof->pipes [0];
2209 if (command_socket >= 0) {
2210 FD_SET (command_socket, &rfds);
2211 if (max_fd < command_socket)
2212 max_fd = command_socket;
2217 for ( i = 0; i < num_perf; ++i) {
2218 if (perf_data [i].perf_fd < 0)
2220 FD_SET (perf_data [i].perf_fd, &rfds);
2221 if (max_fd < perf_data [i].perf_fd)
2222 max_fd = perf_data [i].perf_fd;
2226 now = current_time ();
2227 counters_sample (prof, (now - start) / 1000/ 1000);
2231 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
2237 g_warning ("Error in proflog server: %s", strerror (errno));
2241 if (FD_ISSET (prof->pipes [0], &rfds)) {
2243 int r = read (prof->pipes [0], &c, 1);
2244 if (r == 1 && c == 0) {
2245 StatBuffer *sbufbase = prof->stat_buffers;
2247 if (!sbufbase->next)
2249 sbuf = sbufbase->next->next;
2250 sbufbase->next->next = NULL;
2252 fprintf (stderr, "stat buffer dump\n");
2253 dump_sample_hits (prof, sbuf, 1);
2254 free_buffer (sbuf, sbuf->size);
2255 safe_dump (prof, ensure_logbuf (0));
2258 /* time to shut down */
2260 mono_thread_detach (thread);
2262 fprintf (stderr, "helper shutdown\n");
2266 for ( i = 0; i < num_perf; ++i) {
2267 if (perf_data [i].perf_fd < 0)
2269 if (FD_ISSET (perf_data [i].perf_fd, &rfds))
2270 read_perf_mmap (prof, i);
2274 safe_dump (prof, ensure_logbuf (0));
2280 for ( i = 0; i < num_perf; ++i) {
2281 if (perf_data [i].perf_fd < 0)
2283 if (FD_ISSET (perf_data [i].perf_fd, &rfds)) {
2284 read_perf_mmap (prof, i);
2285 safe_dump (prof, ensure_logbuf (0));
2290 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
2291 len = read (command_socket, buf, sizeof (buf) - 1);
2295 close (command_socket);
2296 command_socket = -1;
2300 if (strcmp (buf, "heapshot\n") == 0) {
2301 heapshot_requested = 1;
2302 //fprintf (stderr, "perform heapshot\n");
2303 if (runtime_inited && !thread) {
2304 thread = mono_thread_attach (mono_get_root_domain ());
2305 /*fprintf (stderr, "attached\n");*/
2308 process_requests (prof);
2309 mono_thread_detach (thread);
2315 if (!FD_ISSET (prof->server_socket, &rfds)) {
2318 command_socket = accept (prof->server_socket, NULL, NULL);
2319 if (command_socket < 0)
2321 //fprintf (stderr, "Accepted connection\n");
2327 start_helper_thread (MonoProfiler* prof)
2329 struct sockaddr_in server_address;
2332 if (pipe (prof->pipes) < 0) {
2333 fprintf (stderr, "Cannot create pipe\n");
2336 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
2337 if (prof->server_socket < 0) {
2338 fprintf (stderr, "Cannot create server socket\n");
2341 memset (&server_address, 0, sizeof (server_address));
2342 server_address.sin_family = AF_INET;
2343 server_address.sin_addr.s_addr = INADDR_ANY;
2344 server_address.sin_port = htons (prof->command_port);
2345 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
2346 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
2347 close (prof->server_socket);
2350 if (listen (prof->server_socket, 1) < 0) {
2351 fprintf (stderr, "Cannot listen server socket\n");
2352 close (prof->server_socket);
2355 slen = sizeof (server_address);
2356 if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
2357 prof->command_port = ntohs (server_address.sin_port);
2358 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
2361 r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
2363 close (prof->server_socket);
2370 static MonoProfiler*
2371 create_profiler (const char *filename)
2375 int force_delete = 0;
2376 int need_helper_thread = 0;
2377 prof = calloc (1, sizeof (MonoProfiler));
2379 prof->command_port = command_port;
2380 if (filename && *filename == '-') {
2386 filename = "|mprof-report -";
2388 filename = "output.mlpd";
2389 nf = (char*)filename;
2391 nf = new_filename (filename);
2393 int s = strlen (nf) + 32;
2394 char *p = malloc (s);
2395 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
2401 prof->file = popen (nf + 1, "w");
2402 prof->pipe_output = 1;
2403 } else if (*nf == '#') {
2404 int fd = strtol (nf + 1, NULL, 10);
2405 prof->file = fdopen (fd, "a");
2410 if ((f = fopen (nf, "r"))) {
2412 fprintf (stderr, "The Mono profiler won't overwrite existing filename: %s.\n", nf);
2413 fprintf (stderr, "Profiling disabled: use a different name or -FILENAME to force overwrite.\n");
2417 prof->file = fopen (nf, "wb");
2420 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
2423 #if defined (HAVE_SYS_ZLIB)
2425 prof->gzfile = gzdopen (fileno (prof->file), "wb");
2428 if (sample_type && !do_mono_sample)
2429 need_helper_thread = setup_perf_event ();
2431 /* FIXME: warn if different freq or sample type */
2435 if (do_mono_sample) {
2436 prof->stat_buffers = create_stat_buffer ();
2437 need_helper_thread = 1;
2439 if (do_counters && !need_helper_thread) {
2440 need_helper_thread = 1;
2442 #ifndef DISABLE_HELPER_THREAD
2443 if (hs_mode_ondemand || need_helper_thread) {
2444 if (!start_helper_thread (prof))
2445 prof->command_port = 0;
2448 if (hs_mode_ondemand)
2449 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
2451 prof->startup_time = current_time ();
2459 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
2460 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
2461 printf ("Options:\n");
2462 printf ("\thelp show this usage info\n");
2463 printf ("\t[no]alloc enable/disable recording allocation info\n");
2464 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
2465 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
2466 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
2467 printf ("\tcounters sample counters every 1s\n");
2468 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/1000)\n");
2469 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
2470 printf ("\t TYPE can be followed by /FREQUENCY\n");
2471 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
2472 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
2473 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
2474 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
2475 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
2476 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
2477 printf ("\treport create a report instead of writing the raw data to a file\n");
2478 printf ("\tzip compress the output data\n");
2479 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
2485 match_option (const char* p, const char *opt, char **rval)
2487 int len = strlen (opt);
2488 if (strncmp (p, opt, len) == 0) {
2490 if (p [len] == '=' && p [len + 1]) {
2491 const char *opt = p + len + 1;
2492 const char *end = strchr (opt, ',');
2500 val = malloc (l + 1);
2501 memcpy (val, opt, l);
2506 if (p [len] == 0 || p [len] == ',') {
2508 return p + len + (p [len] == ',');
2526 static const SampleMode sample_modes [] = {
2527 {"cycles", SAMPLE_CYCLES},
2528 {"instr", SAMPLE_INSTRUCTIONS},
2529 {"cachemiss", SAMPLE_CACHE_MISSES},
2530 {"cacherefs", SAMPLE_CACHE_REFS},
2531 {"branches", SAMPLE_BRANCHES},
2532 {"branchmiss", SAMPLE_BRANCH_MISSES},
2537 set_sample_mode (char* val, int allow_empty)
2540 char *maybe_freq = NULL;
2542 const SampleMode *smode = sample_modes;
2543 #ifndef USE_PERF_EVENTS
2546 if (allow_empty && !val) {
2547 sample_type = SAMPLE_CYCLES;
2551 if (strcmp (val, "mono") == 0) {
2553 sample_type = SAMPLE_CYCLES;
2557 for (smode = sample_modes; smode->name; smode++) {
2558 int l = strlen (smode->name);
2559 if (strncmp (val, smode->name, l) == 0) {
2560 sample_type = smode->sample_mode;
2561 maybe_freq = val + l;
2567 if (*maybe_freq == '/') {
2568 count = strtoul (maybe_freq + 1, &end, 10);
2569 if (maybe_freq + 1 == end)
2571 sample_freq = count;
2572 } else if (*maybe_freq != 0) {
2581 set_hsmode (char* val, int allow_empty)
2585 if (allow_empty && !val)
2587 if (strcmp (val, "ondemand") == 0) {
2588 hs_mode_ondemand = 1;
2592 count = strtoul (val, &end, 10);
2595 if (strcmp (end, "ms") == 0)
2597 else if (strcmp (end, "gc") == 0)
2605 * declaration to silence the compiler: this is the entry point that
2606 * mono will load from the shared library and call.
2609 mono_profiler_startup (const char *desc);
2612 mono_profiler_startup_log (const char *desc);
2615 * this is the entry point that will be used when the profiler
2616 * is embedded inside the main executable.
2619 mono_profiler_startup_log (const char *desc)
2621 mono_profiler_startup (desc);
2625 mono_profiler_startup (const char *desc)
2628 char *filename = NULL;
2632 int calls_enabled = 0;
2633 int allocs_enabled = 0;
2634 int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
2635 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
2636 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
2637 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS;
2640 if (strncmp (p, "log", 3))
2645 for (; *p; p = opt) {
2651 if ((opt = match_option (p, "help", NULL)) != p) {
2655 if ((opt = match_option (p, "calls", NULL)) != p) {
2659 if ((opt = match_option (p, "nocalls", NULL)) != p) {
2660 events &= ~MONO_PROFILE_ENTER_LEAVE;
2664 if ((opt = match_option (p, "alloc", NULL)) != p) {
2668 if ((opt = match_option (p, "noalloc", NULL)) != p) {
2669 events &= ~MONO_PROFILE_ALLOCATIONS;
2672 if ((opt = match_option (p, "time", &val)) != p) {
2673 if (strcmp (val, "fast") == 0)
2675 else if (strcmp (val, "null") == 0)
2682 if ((opt = match_option (p, "report", NULL)) != p) {
2686 if ((opt = match_option (p, "debug", NULL)) != p) {
2690 if ((opt = match_option (p, "heapshot", &val)) != p) {
2691 events &= ~MONO_PROFILE_ALLOCATIONS;
2692 events &= ~MONO_PROFILE_ENTER_LEAVE;
2695 set_hsmode (val, 1);
2698 if ((opt = match_option (p, "sample", &val)) != p) {
2699 events &= ~MONO_PROFILE_ALLOCATIONS;
2700 events &= ~MONO_PROFILE_ENTER_LEAVE;
2702 set_sample_mode (val, 1);
2705 if ((opt = match_option (p, "hsmode", &val)) != p) {
2706 fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
2707 set_hsmode (val, 0);
2710 if ((opt = match_option (p, "zip", NULL)) != p) {
2714 if ((opt = match_option (p, "output", &val)) != p) {
2718 if ((opt = match_option (p, "port", &val)) != p) {
2720 command_port = strtoul (val, &end, 10);
2724 if ((opt = match_option (p, "maxframes", &val)) != p) {
2726 num_frames = strtoul (val, &end, 10);
2727 if (num_frames > MAX_FRAMES)
2728 num_frames = MAX_FRAMES;
2730 notraces = num_frames == 0;
2733 if ((opt = match_option (p, "calldepth", &val)) != p) {
2735 max_call_depth = strtoul (val, &end, 10);
2739 if ((opt = match_option (p, "counters", NULL)) != p) {
2748 if (calls_enabled) {
2749 events |= MONO_PROFILE_ENTER_LEAVE;
2753 events |= MONO_PROFILE_ALLOCATIONS;
2754 utils_init (fast_time);
2756 prof = create_profiler (filename);
2761 mono_profiler_install (prof, log_shutdown);
2762 mono_profiler_install_gc (gc_event, gc_resize);
2763 mono_profiler_install_allocation (gc_alloc);
2764 mono_profiler_install_gc_moves (gc_moves);
2765 mono_profiler_install_gc_roots (gc_handle, gc_roots);
2766 mono_profiler_install_class (NULL, class_loaded, NULL, NULL);
2767 mono_profiler_install_module (NULL, image_loaded, NULL, NULL);
2768 mono_profiler_install_thread (thread_start, thread_end);
2769 mono_profiler_install_thread_name (thread_name);
2770 mono_profiler_install_enter_leave (method_enter, method_leave);
2771 mono_profiler_install_jit_end (method_jitted);
2772 mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
2773 mono_profiler_install_monitor (monitor_event);
2774 mono_profiler_install_runtime_initialized (runtime_initialized);
2777 if (do_mono_sample && sample_type == SAMPLE_CYCLES) {
2778 events |= MONO_PROFILE_STATISTICAL;
2779 mono_profiler_install_statistical (mono_sample_hit);
2782 mono_profiler_set_events (events);
2784 TLS_INIT (tlsbuffer);