Finish spliting sgen-nursery-allocator into a separate compilation unit
[mono-project.git] / mono / profiler / proflog.c
blobcb8d91695c349425a47559473e31bac76769d987
1 /*
2 * proflog.c: mono log profiler
4 * Author:
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2010 Novell, Inc (http://www.novell.com)
8 */
10 #include <config.h>
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>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <assert.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <fcntl.h>
22 #include <errno.h>
23 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
24 #define DISABLE_HELPER_THREAD 1
25 #endif
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30 #ifdef HAVE_DLFCN_H
31 #include <dlfcn.h>
32 #endif
33 #ifdef HAVE_EXECINFO_H
34 #include <execinfo.h>
35 #endif
36 #ifdef HAVE_LINK_H
37 #include <link.h>
38 #endif
40 #ifndef DISABLE_HELPER_THREAD
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <sys/select.h>
45 #endif
47 #ifdef HOST_WIN32
48 #include <windows.h>
49 #else
50 #include <pthread.h>
51 #endif
53 #ifdef HAVE_SYS_STAT_H
54 #include <sys/stat.h>
55 #endif
57 #include "utils.c"
58 #include "proflog.h"
60 #if defined (HAVE_SYS_ZLIB)
61 #include <zlib.h>
62 #endif
64 /* the architecture needs a memory fence */
65 #if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
66 #include "perf_event.h"
67 #define USE_PERF_EVENTS 1
68 static int read_perf_mmap (MonoProfiler* prof);
69 #endif
71 #define BUFFER_SIZE (4096 * 16)
72 static int nocalls = 0;
73 static int notraces = 0;
74 static int use_zip = 0;
75 static int do_report = 0;
76 static int do_heap_shot = 0;
77 static int max_call_depth = 100;
78 static int runtime_inited = 0;
79 static int command_port = 0;
80 static int heapshot_requested = 0;
81 static int sample_type = 0;
82 static int sample_freq = 0;
83 static int do_mono_sample = 0;
84 static int in_shutdown = 0;
85 static int do_debug = 0;
87 /* For linux compile with:
88 * gcc -fPIC -shared -o libmono-profiler-log.so proflog.c utils.c -Wall -g -lz `pkg-config --cflags --libs mono-2`
89 * gcc -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
91 * For osx compile with:
92 * gcc -m32 -Dmono_free=free shared -o libmono-profiler-log.dylib proflog.c utils.c -Wall -g -lz `pkg-config --cflags mono-2` -undefined suppress -flat_namespace
93 * gcc -m32 -o mprof-report decode.c utils.c -Wall -g -lz -lrt -lpthread `pkg-config --cflags mono-2`
95 * Install with:
96 * sudo cp mprof-report /usr/local/bin
97 * sudo cp libmono-profiler-log.so /usr/local/lib
98 * sudo ldconfig
101 typedef struct _LogBuffer LogBuffer;
104 * file format:
105 * [header] [buffer]*
107 * The file is composed by a header followed by 0 or more buffers.
108 * Each buffer contains events that happened on a thread: for a given thread
109 * buffers that appear later in the file are guaranteed to contain events
110 * that happened later in time. Buffers from separate threads could be interleaved,
111 * though.
112 * Buffers are not required to be aligned.
114 * header format:
115 * [id: 4 bytes] constant value: LOG_HEADER_ID
116 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
117 * [format: 1 byte] version of the data format for the rest of the file
118 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
119 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
120 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
121 * [flags: 4 bytes] file format flags, should be 0 for now
122 * [pid: 4 bytes] pid of the profiled process
123 * [port: 2 bytes] tcp port for server if != 0
124 * [sysid: 2 bytes] operating system and architecture identifier
126 * The multiple byte integers are in little-endian format.
128 * buffer format:
129 * [buffer header] [event]*
130 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
131 * Timing information and other values in the event data are usually stored
132 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
133 * some data is represented as a difference between the actual value and
134 * either the last value of the same type (like for timing information) or
135 * as the difference from a value stored in a buffer header.
137 * For timing information the data is stored as uleb128, since timing
138 * increases in a monotonic way in each thread: the value is the number of
139 * nanoseconds to add to the last seen timing data in a buffer. The first value
140 * in a buffer will be calculated from the time_base field in the buffer head.
142 * Object or heap sizes are stored as uleb128.
143 * Pointer differences are stored as sleb128, instead.
145 * If an unexpected value is found, the rest of the buffer should be ignored,
146 * as generally the later values need the former to be interpreted correctly.
148 * buffer header format:
149 * [bufid: 4 bytes] constant value: BUF_ID
150 * [len: 4 bytes] size of the data following the buffer header
151 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
152 * [ptr_base: 8 bytes] base value for pointers
153 * [obj_base: 8 bytes] base value for object addresses
154 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
155 * [method_base: 8 bytes] base value for MonoMethod pointers
157 * event format:
158 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
159 * The data that follows depends on type and the extended info.
160 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
161 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
162 * The extended info bits are interpreted based on type, see
163 * each individual event description below.
164 * strings are represented as a 0-terminated utf8 sequence.
166 * backtrace format:
167 * [flags: uleb128] must be 0
168 * [num: uleb128] number of frames following
169 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
171 * type alloc format:
172 * type: TYPE_ALLOC
173 * exinfo: flags: TYPE_ALLOC_BT
174 * [time diff: uleb128] nanoseconds since last timing
175 * [ptr: sleb128] class as a byte difference from ptr_base
176 * [obj: sleb128] object address as a byte difference from obj_base
177 * [size: uleb128] size of the object in the heap
178 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
180 * type GC format:
181 * type: TYPE_GC
182 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED,
183 * TYPE_GC_HANDLE_DESTROYED
184 * [time diff: uleb128] nanoseconds since last timing
185 * if exinfo == TYPE_GC_RESIZE
186 * [heap_size: uleb128] new heap size
187 * if exinfo == TYPE_GC_EVENT
188 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
189 * [generation: uleb128] GC generation event refers to
190 * if exinfo == TYPE_GC_MOVE
191 * [num_objects: uleb128] number of object moves that follow
192 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
193 * num is always an even number: the even items are the old
194 * addresses, the odd numbers are the respective new object addresses
195 * if exinfo == TYPE_GC_HANDLE_CREATED
196 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
197 * upper bits reserved as flags
198 * [handle: uleb128] GC handle value
199 * [objaddr: sleb128] object pointer differences from obj_base
200 * if exinfo == TYPE_GC_HANDLE_DESTROYED
201 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
202 * upper bits reserved as flags
203 * [handle: uleb128] GC handle value
205 * type metadata format:
206 * type: TYPE_METADATA
207 * exinfo: flags: TYPE_LOAD_ERR
208 * [time diff: uleb128] nanoseconds since last timing
209 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
210 * TYPE_THREAD
211 * [pointer: sleb128] pointer of the metadata type depending on mtype
212 * if mtype == TYPE_CLASS
213 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
214 * [flags: uleb128] must be 0
215 * [name: string] full class name
216 * if mtype == TYPE_IMAGE
217 * [flags: uleb128] must be 0
218 * [name: string] image file name
219 * if mtype == TYPE_THREAD
220 * [flags: uleb128] must be 0
221 * [name: string] thread name
223 * type method format:
224 * type: TYPE_METHOD
225 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
226 * [time diff: uleb128] nanoseconds since last timing
227 * [method: sleb128] MonoMethod* as a pointer difference from the last such
228 * pointer or the buffer method_base
229 * if exinfo == TYPE_JIT
230 * [code address: sleb128] pointer to the native code as a diff from ptr_base
231 * [code size: uleb128] size of the generated code
232 * [name: string] full method name
234 * type exception format:
235 * type: TYPE_EXCEPTION
236 * exinfo: TYPE_EXCEPTION_BT flag and one of: TYPE_THROW, TYPE_CLAUSE
237 * [time diff: uleb128] nanoseconds since last timing
238 * if exinfo.low3bits == TYPE_CLAUSE
239 * [clause type: uleb128] finally/catch/fault/filter
240 * [clause num: uleb128] the clause number in the method header
241 * [method: sleb128] MonoMethod* as a pointer difference from the last such
242 * pointer or the buffer method_base
243 * if exinfo.low3bits == TYPE_THROW
244 * [object: sleb128] the object that was thrown as a difference from obj_base
245 * If the TYPE_EXCEPTION_BT flag is set, a backtrace follows.
247 * type monitor format:
248 * type: TYPE_MONITOR
249 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
250 * [time diff: uleb128] nanoseconds since last timing
251 * [object: sleb128] the lock object as a difference from obj_base
252 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
253 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
255 * type heap format
256 * type: TYPE_HEAP
257 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
258 * if exinfo == TYPE_HEAP_START
259 * [time diff: uleb128] nanoseconds since last timing
260 * if exinfo == TYPE_HEAP_END
261 * [time diff: uleb128] nanoseconds since last timing
262 * if exinfo == TYPE_HEAP_OBJECT
263 * [object: sleb128] the object as a difference from obj_base
264 * [class: sleb128] the object MonoClass* as a difference from ptr_base
265 * [size: uleb128] size of the object on the heap
266 * [num_refs: uleb128] number of object references
267 * if (format version > 1) each referenced objref is preceded by a
268 * uleb128 encoded offset: the first offset is from the object address
269 * and each next offset is relative to the previous one
270 * [objrefs: sleb128]+ object referenced as a difference from obj_base
271 * The same object can appear multiple times, but only the first time
272 * with size != 0: in the other cases this data will only be used to
273 * provide additional referenced objects.
274 * if exinfo == TYPE_HEAP_ROOT
275 * [num_roots: uleb128] number of root references
276 * [num_gc: uleb128] number of major gcs
277 * [object: sleb128] the object as a difference from obj_base
278 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
279 * [extra_info: uleb128] the extra_info value
280 * object, root_type_extra_info are repeated num_roots times
282 * type sample format
283 * type: TYPE_SAMPLE
284 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN
285 * if exinfo == TYPE_SAMPLE_HIT
286 * [sample_type: uleb128] type of sample (SAMPLE_*)
287 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
288 * [count: uleb128] number of following instruction addresses
289 * [ip: sleb128]* instruction pointer as difference from ptr_base
290 * if exinfo == TYPE_SAMPLE_USYM
291 * [address: sleb128] symbol address as a difference from ptr_base
292 * [size: uleb128] symbol size (may be 0 if unknown)
293 * [name: string] symbol name
294 * if exinfo == TYPE_SAMPLE_UBIN
295 * [time diff: uleb128] nanoseconds since last timing
296 * [address: sleb128] address where binary has been loaded
297 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
298 * [size: uleb128] memory size
299 * [name: string] binary name
302 struct _LogBuffer {
303 LogBuffer *next;
304 uint64_t time_base;
305 uint64_t last_time;
306 uintptr_t ptr_base;
307 uintptr_t method_base;
308 uintptr_t last_method;
309 uintptr_t obj_base;
310 uintptr_t thread_id;
311 unsigned char* data_end;
312 unsigned char* data;
313 int locked;
314 int size;
315 int call_depth;
316 unsigned char buf [1];
319 #define ENTER_LOG(lb,str) if ((lb)->locked) {write(2, str, strlen(str)); write(2, "\n", 1);return;} else {(lb)->locked++;}
320 #define EXIT_LOG(lb) (lb)->locked--;
322 typedef struct _StatBuffer StatBuffer;
323 struct _StatBuffer {
324 StatBuffer *next;
325 uintptr_t size;
326 uintptr_t *data_end;
327 uintptr_t *data;
328 uintptr_t buf [1];
331 typedef struct _BinaryObject BinaryObject;
333 struct _BinaryObject {
334 BinaryObject *next;
335 void *addr;
336 char *name;
339 struct _MonoProfiler {
340 LogBuffer *buffers;
341 StatBuffer *stat_buffers;
342 FILE* file;
343 #if defined (HAVE_SYS_ZLIB)
344 gzFile *gzfile;
345 #endif
346 uint64_t startup_time;
347 int pipe_output;
348 int last_gc_gen_started;
349 int command_port;
350 int server_socket;
351 int pipes [2];
352 #ifndef HOST_WIN32
353 pthread_t helper_thread;
354 #endif
355 BinaryObject *binary_objects;
358 #ifdef HOST_WIN32
359 #define TLS_SET(x,y) TlsSetValue(x, y)
360 #define TLS_GET(x) ((LogBuffer *) TlsGetValue(x))
361 #define TLS_INIT(x) x = TlsAlloc ()
362 static int tlsbuffer;
363 #elif HAVE_KW_THREAD
364 #define TLS_SET(x,y) x = y
365 #define TLS_GET(x) x
366 #define TLS_INIT(x)
367 static __thread LogBuffer* tlsbuffer = NULL;
368 #else
369 #define TLS_SET(x,y) pthread_setspecific(x, y)
370 #define TLS_GET(x) ((LogBuffer *) pthread_getspecific(x))
371 #define TLS_INIT(x) pthread_key_create(&x, NULL)
372 static pthread_key_t tlsbuffer;
373 #endif
375 static char*
376 pstrdup (const char *s)
378 int len = strlen (s) + 1;
379 char *p = malloc (len);
380 memcpy (p, s, len);
381 return p;
384 static StatBuffer*
385 create_stat_buffer (void)
387 StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
388 buf->size = BUFFER_SIZE;
389 buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
390 buf->data = buf->buf;
391 return buf;
394 static LogBuffer*
395 create_buffer (void)
397 LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
398 buf->size = BUFFER_SIZE;
399 buf->time_base = current_time ();
400 buf->last_time = buf->time_base;
401 buf->data_end = (unsigned char*)buf + buf->size;
402 buf->data = buf->buf;
403 return buf;
406 static void
407 init_thread (void)
409 LogBuffer *logbuffer;
410 if (TLS_GET (tlsbuffer))
411 return;
412 logbuffer = create_buffer ();
413 TLS_SET (tlsbuffer, logbuffer);
414 logbuffer->thread_id = thread_id ();
415 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
418 static LogBuffer*
419 ensure_logbuf (int bytes)
421 LogBuffer *old = TLS_GET (tlsbuffer);
422 if (old && old->data + bytes + 100 < old->data_end)
423 return old;
424 TLS_SET (tlsbuffer, NULL);
425 init_thread ();
426 TLS_GET (tlsbuffer)->next = old;
427 if (old)
428 TLS_GET (tlsbuffer)->call_depth = old->call_depth;
429 //printf ("new logbuffer\n");
430 return TLS_GET (tlsbuffer);
433 static void
434 emit_byte (LogBuffer *logbuffer, int value)
436 logbuffer->data [0] = value;
437 logbuffer->data++;
438 assert (logbuffer->data <= logbuffer->data_end);
441 static void
442 emit_value (LogBuffer *logbuffer, int value)
444 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
445 assert (logbuffer->data <= logbuffer->data_end);
448 static void
449 emit_time (LogBuffer *logbuffer, uint64_t value)
451 uint64_t tdiff = value - logbuffer->last_time;
452 unsigned char *p;
453 if (value < logbuffer->last_time)
454 printf ("time went backwards\n");
455 //if (tdiff > 1000000)
456 // printf ("large time offset: %llu\n", tdiff);
457 p = logbuffer->data;
458 encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
459 /*if (tdiff != decode_uleb128 (p, &p))
460 printf ("incorrect encoding: %llu\n", tdiff);*/
461 logbuffer->last_time = value;
462 assert (logbuffer->data <= logbuffer->data_end);
465 static void
466 emit_svalue (LogBuffer *logbuffer, int64_t value)
468 encode_sleb128 (value, logbuffer->data, &logbuffer->data);
469 assert (logbuffer->data <= logbuffer->data_end);
472 static void
473 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
475 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
476 assert (logbuffer->data <= logbuffer->data_end);
479 static void
480 emit_ptr (LogBuffer *logbuffer, void *ptr)
482 if (!logbuffer->ptr_base)
483 logbuffer->ptr_base = (uintptr_t)ptr;
484 emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
485 assert (logbuffer->data <= logbuffer->data_end);
488 static void
489 emit_method (LogBuffer *logbuffer, void *method)
491 if (!logbuffer->method_base) {
492 logbuffer->method_base = (intptr_t)method;
493 logbuffer->last_method = (intptr_t)method;
495 encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->data, &logbuffer->data);
496 logbuffer->last_method = (intptr_t)method;
497 assert (logbuffer->data <= logbuffer->data_end);
500 static void
501 emit_obj (LogBuffer *logbuffer, void *ptr)
503 if (!logbuffer->obj_base)
504 logbuffer->obj_base = (uintptr_t)ptr >> 3;
505 emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
506 assert (logbuffer->data <= logbuffer->data_end);
509 static char*
510 write_int16 (char *buf, int32_t value)
512 int i;
513 for (i = 0; i < 2; ++i) {
514 buf [i] = value;
515 value >>= 8;
517 return buf + 2;
520 static char*
521 write_int32 (char *buf, int32_t value)
523 int i;
524 for (i = 0; i < 4; ++i) {
525 buf [i] = value;
526 value >>= 8;
528 return buf + 4;
531 static char*
532 write_int64 (char *buf, int64_t value)
534 int i;
535 for (i = 0; i < 8; ++i) {
536 buf [i] = value;
537 value >>= 8;
539 return buf + 8;
542 static void
543 dump_header (MonoProfiler *profiler)
545 char hbuf [128];
546 char *p = hbuf;
547 p = write_int32 (p, LOG_HEADER_ID);
548 *p++ = LOG_VERSION_MAJOR;
549 *p++ = LOG_VERSION_MINOR;
550 *p++ = LOG_DATA_VERSION;
551 *p++ = sizeof (void*);
552 p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
553 p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
554 p = write_int32 (p, 0); /* flags */
555 p = write_int32 (p, process_id ()); /* pid */
556 p = write_int16 (p, profiler->command_port); /* port */
557 p = write_int16 (p, 0); /* opsystem */
558 #if defined (HAVE_SYS_ZLIB)
559 if (profiler->gzfile) {
560 gzwrite (profiler->gzfile, hbuf, p - hbuf);
561 } else {
562 fwrite (hbuf, p - hbuf, 1, profiler->file);
564 #else
565 fwrite (hbuf, p - hbuf, 1, profiler->file);
566 #endif
569 static void
570 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
572 char hbuf [128];
573 char *p = hbuf;
574 if (buf->next)
575 dump_buffer (profiler, buf->next);
576 p = write_int32 (p, BUF_ID);
577 p = write_int32 (p, buf->data - buf->buf);
578 p = write_int64 (p, buf->time_base);
579 p = write_int64 (p, buf->ptr_base);
580 p = write_int64 (p, buf->obj_base);
581 p = write_int64 (p, buf->thread_id);
582 p = write_int64 (p, buf->method_base);
583 #if defined (HAVE_SYS_ZLIB)
584 if (profiler->gzfile) {
585 gzwrite (profiler->gzfile, hbuf, p - hbuf);
586 gzwrite (profiler->gzfile, buf->buf, buf->data - buf->buf);
587 } else {
588 #endif
589 fwrite (hbuf, p - hbuf, 1, profiler->file);
590 fwrite (buf->buf, buf->data - buf->buf, 1, profiler->file);
591 #if defined (HAVE_SYS_ZLIB)
593 #endif
594 free_buffer (buf, buf->size);
597 static void
598 process_requests (MonoProfiler *profiler)
600 if (heapshot_requested)
601 mono_gc_collect (mono_gc_max_generation ());
604 static void
605 runtime_initialized (MonoProfiler *profiler)
607 runtime_inited = 1;
611 * Can be called only at safe callback locations.
613 static void
614 safe_dump (MonoProfiler *profiler, LogBuffer *logbuffer)
616 int cd = logbuffer->call_depth;
617 take_lock ();
618 dump_buffer (profiler, TLS_GET (tlsbuffer));
619 release_lock ();
620 TLS_SET (tlsbuffer, NULL);
621 init_thread ();
622 TLS_GET (tlsbuffer)->call_depth = cd;
625 static int
626 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
628 int i;
629 uintptr_t last_offset = 0;
630 //const char *name = mono_class_get_name (klass);
631 LogBuffer *logbuffer = ensure_logbuf (20 + num * 8);
632 emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
633 emit_obj (logbuffer, obj);
634 emit_ptr (logbuffer, klass);
635 /* account for object alignment in the heap */
636 size += 7;
637 size &= ~7;
638 emit_value (logbuffer, size);
639 emit_value (logbuffer, num);
640 for (i = 0; i < num; ++i) {
641 emit_value (logbuffer, offsets [i] - last_offset);
642 last_offset = offsets [i];
643 emit_obj (logbuffer, refs [i]);
645 //if (num)
646 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
647 return 0;
650 static unsigned int hs_mode_ms = 0;
651 static unsigned int hs_mode_gc = 0;
652 static unsigned int hs_mode_ondemand = 0;
653 static unsigned int gc_count = 0;
654 static uint64_t last_hs_time = 0;
656 static void
657 heap_walk (MonoProfiler *profiler)
659 int do_walk = 0;
660 uint64_t now;
661 LogBuffer *logbuffer;
662 if (!do_heap_shot)
663 return;
664 logbuffer = ensure_logbuf (10);
665 now = current_time ();
666 if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
667 do_walk = 1;
668 else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
669 do_walk = 1;
670 else if (hs_mode_ondemand && heapshot_requested)
671 do_walk = 1;
672 else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
673 do_walk = 1;
675 if (!do_walk)
676 return;
677 heapshot_requested = 0;
678 emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
679 emit_time (logbuffer, now);
680 mono_gc_walk_heap (0, gc_reference, NULL);
681 logbuffer = ensure_logbuf (10);
682 now = current_time ();
683 emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
684 emit_time (logbuffer, now);
685 last_hs_time = now;
688 static void
689 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
690 uint64_t now;
691 LogBuffer *logbuffer = ensure_logbuf (10);
692 now = current_time ();
693 ENTER_LOG (logbuffer, "gcevent");
694 emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
695 emit_time (logbuffer, now);
696 emit_value (logbuffer, ev);
697 emit_value (logbuffer, generation);
698 /* to deal with nested gen1 after gen0 started */
699 if (ev == MONO_GC_EVENT_START) {
700 profiler->last_gc_gen_started = generation;
701 if (generation == mono_gc_max_generation ())
702 gc_count++;
704 if (ev == MONO_GC_EVENT_PRE_START_WORLD)
705 heap_walk (profiler);
706 EXIT_LOG (logbuffer);
707 if (ev == MONO_GC_EVENT_POST_START_WORLD)
708 safe_dump (profiler, logbuffer);
709 //printf ("gc event %d for generation %d\n", ev, generation);
712 static void
713 gc_resize (MonoProfiler *profiler, int64_t new_size) {
714 uint64_t now;
715 LogBuffer *logbuffer = ensure_logbuf (10);
716 now = current_time ();
717 ENTER_LOG (logbuffer, "gcresize");
718 emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
719 emit_time (logbuffer, now);
720 emit_value (logbuffer, new_size);
721 //printf ("gc resized to %lld\n", new_size);
722 EXIT_LOG (logbuffer);
725 #define MAX_FRAMES 16
726 typedef struct {
727 int count;
728 MonoMethod* methods [MAX_FRAMES];
729 } FrameData;
730 static int num_frames = MAX_FRAMES / 2;
732 static mono_bool
733 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
735 FrameData *frame = data;
736 if (method && frame->count < num_frames) {
737 frame->methods [frame->count++] = method;
738 //printf ("In %d %s\n", frame->count, mono_method_get_name (method));
740 return frame->count == num_frames;
744 * a note about stack walks: they can cause more profiler events to fire,
745 * so we need to make sure they don't happen after we started emitting an
746 * event, hence the collect_bt/emit_bt split.
748 static void
749 collect_bt (FrameData *data)
751 data->count = 0;
752 mono_stack_walk_no_il (walk_stack, data);
755 static void
756 emit_bt (LogBuffer *logbuffer, FrameData *data)
758 /* FIXME: this is actually tons of data and we should
759 * just output it the first time and use an id the next
761 if (data->count > num_frames)
762 printf ("bad num frames: %d\n", data->count);
763 emit_value (logbuffer, 0); /* flags */
764 emit_value (logbuffer, data->count);
765 //if (*p != data.count) {
766 // printf ("bad num frames enc at %d: %d -> %d\n", count, data.count, *p); printf ("frames end: %p->%p\n", p, logbuffer->data); exit(0);}
767 while (data->count) {
768 emit_ptr (logbuffer, data->methods [--data->count]);
772 static void
773 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
775 uint64_t now;
776 uintptr_t len;
777 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_ALLOC_BT: 0;
778 FrameData data;
779 LogBuffer *logbuffer;
780 len = mono_object_get_size (obj);
781 /* account for object alignment in the heap */
782 len += 7;
783 len &= ~7;
784 if (do_bt)
785 collect_bt (&data);
786 logbuffer = ensure_logbuf (32 + MAX_FRAMES * 8);
787 now = current_time ();
788 ENTER_LOG (logbuffer, "gcalloc");
789 emit_byte (logbuffer, do_bt | TYPE_ALLOC);
790 emit_time (logbuffer, now);
791 emit_ptr (logbuffer, klass);
792 emit_obj (logbuffer, obj);
793 emit_value (logbuffer, len);
794 if (do_bt)
795 emit_bt (logbuffer, &data);
796 EXIT_LOG (logbuffer);
797 if (logbuffer->next)
798 safe_dump (prof, logbuffer);
799 process_requests (prof);
800 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
803 static void
804 gc_moves (MonoProfiler *prof, void **objects, int num)
806 int i;
807 uint64_t now;
808 LogBuffer *logbuffer = ensure_logbuf (10 + num * 8);
809 now = current_time ();
810 ENTER_LOG (logbuffer, "gcmove");
811 emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
812 emit_time (logbuffer, now);
813 emit_value (logbuffer, num);
814 for (i = 0; i < num; ++i)
815 emit_obj (logbuffer, objects [i]);
816 //printf ("gc moved %d objects\n", num/2);
817 EXIT_LOG (logbuffer);
820 static void
821 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
823 int i;
824 LogBuffer *logbuffer = ensure_logbuf (5 + num * 18);
825 ENTER_LOG (logbuffer, "gcroots");
826 emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
827 emit_value (logbuffer, num);
828 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
829 for (i = 0; i < num; ++i) {
830 emit_obj (logbuffer, objects [i]);
831 emit_value (logbuffer, root_types [i]);
832 emit_value (logbuffer, extra_info [i]);
834 EXIT_LOG (logbuffer);
837 static void
838 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
840 uint64_t now;
841 LogBuffer *logbuffer = ensure_logbuf (16);
842 now = current_time ();
843 ENTER_LOG (logbuffer, "gchandle");
844 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
845 emit_byte (logbuffer, TYPE_GC_HANDLE_CREATED | TYPE_GC);
846 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
847 emit_byte (logbuffer, TYPE_GC_HANDLE_DESTROYED | TYPE_GC);
848 else
849 return;
850 emit_time (logbuffer, now);
851 emit_value (logbuffer, type);
852 emit_value (logbuffer, handle);
853 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
854 emit_obj (logbuffer, obj);
855 EXIT_LOG (logbuffer);
856 process_requests (prof);
859 static char*
860 push_nesting (char *p, MonoClass *klass)
862 MonoClass *nesting;
863 const char *name;
864 const char *nspace;
865 nesting = mono_class_get_nesting_type (klass);
866 if (nesting) {
867 p = push_nesting (p, nesting);
868 *p++ = '/';
869 *p = 0;
871 name = mono_class_get_name (klass);
872 nspace = mono_class_get_namespace (klass);
873 if (*nspace) {
874 strcpy (p, nspace);
875 p += strlen (nspace);
876 *p++ = '.';
877 *p = 0;
879 strcpy (p, name);
880 p += strlen (name);
881 return p;
884 static char*
885 type_name (MonoClass *klass)
887 char buf [1024];
888 char *p;
889 push_nesting (buf, klass);
890 p = malloc (strlen (buf) + 1);
891 strcpy (p, buf);
892 return p;
895 static void
896 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
898 uint64_t now;
899 const char *name;
900 int nlen;
901 LogBuffer *logbuffer;
902 if (result != MONO_PROFILE_OK)
903 return;
904 name = mono_image_get_filename (image);
905 nlen = strlen (name) + 1;
906 logbuffer = ensure_logbuf (16 + nlen);
907 now = current_time ();
908 ENTER_LOG (logbuffer, "image");
909 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
910 emit_time (logbuffer, now);
911 emit_byte (logbuffer, TYPE_IMAGE);
912 emit_ptr (logbuffer, image);
913 emit_value (logbuffer, 0); /* flags */
914 memcpy (logbuffer->data, name, nlen);
915 logbuffer->data += nlen;
916 //printf ("loaded image %p (%s)\n", image, name);
917 EXIT_LOG (logbuffer);
918 if (logbuffer->next)
919 safe_dump (prof, logbuffer);
920 process_requests (prof);
923 static void
924 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
926 uint64_t now;
927 char *name;
928 int nlen;
929 MonoImage *image;
930 LogBuffer *logbuffer;
931 if (result != MONO_PROFILE_OK)
932 return;
933 if (runtime_inited)
934 name = mono_type_get_name (mono_class_get_type (klass));
935 else
936 name = type_name (klass);
937 nlen = strlen (name) + 1;
938 image = mono_class_get_image (klass);
939 logbuffer = ensure_logbuf (24 + nlen);
940 now = current_time ();
941 ENTER_LOG (logbuffer, "class");
942 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
943 emit_time (logbuffer, now);
944 emit_byte (logbuffer, TYPE_CLASS);
945 emit_ptr (logbuffer, klass);
946 emit_ptr (logbuffer, image);
947 emit_value (logbuffer, 0); /* flags */
948 memcpy (logbuffer->data, name, nlen);
949 logbuffer->data += nlen;
950 //printf ("loaded class %p (%s)\n", klass, name);
951 if (runtime_inited)
952 mono_free (name);
953 else
954 free (name);
955 EXIT_LOG (logbuffer);
956 if (logbuffer->next)
957 safe_dump (prof, logbuffer);
958 process_requests (prof);
961 static void
962 method_enter (MonoProfiler *prof, MonoMethod *method)
964 uint64_t now;
965 LogBuffer *logbuffer = ensure_logbuf (16);
966 if (logbuffer->call_depth++ > max_call_depth)
967 return;
968 now = current_time ();
969 ENTER_LOG (logbuffer, "enter");
970 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
971 emit_time (logbuffer, now);
972 emit_method (logbuffer, method);
973 EXIT_LOG (logbuffer);
974 process_requests (prof);
977 static void
978 method_leave (MonoProfiler *prof, MonoMethod *method)
980 uint64_t now;
981 LogBuffer *logbuffer = ensure_logbuf (16);
982 if (--logbuffer->call_depth > max_call_depth)
983 return;
984 now = current_time ();
985 ENTER_LOG (logbuffer, "leave");
986 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
987 emit_time (logbuffer, now);
988 emit_method (logbuffer, method);
989 EXIT_LOG (logbuffer);
990 if (logbuffer->next)
991 safe_dump (prof, logbuffer);
992 process_requests (prof);
995 static void
996 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
998 uint64_t now;
999 LogBuffer *logbuffer;
1000 if (nocalls)
1001 return;
1002 logbuffer = ensure_logbuf (16);
1003 if (--logbuffer->call_depth > max_call_depth)
1004 return;
1005 now = current_time ();
1006 ENTER_LOG (logbuffer, "eleave");
1007 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1008 emit_time (logbuffer, now);
1009 emit_method (logbuffer, method);
1010 EXIT_LOG (logbuffer);
1011 process_requests (prof);
1014 static void
1015 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result)
1017 uint64_t now;
1018 char *name;
1019 int nlen;
1020 LogBuffer *logbuffer;
1021 if (result != MONO_PROFILE_OK)
1022 return;
1023 name = mono_method_full_name (method, 1);
1024 nlen = strlen (name) + 1;
1025 logbuffer = ensure_logbuf (32 + nlen);
1026 now = current_time ();
1027 ENTER_LOG (logbuffer, "jit");
1028 emit_byte (logbuffer, TYPE_JIT | TYPE_METHOD);
1029 emit_time (logbuffer, now);
1030 emit_method (logbuffer, method);
1031 emit_ptr (logbuffer, mono_jit_info_get_code_start (jinfo));
1032 emit_value (logbuffer, mono_jit_info_get_code_size (jinfo));
1033 memcpy (logbuffer->data, name, nlen);
1034 logbuffer->data += nlen;
1035 mono_free (name);
1036 EXIT_LOG (logbuffer);
1037 if (logbuffer->next)
1038 safe_dump (prof, logbuffer);
1039 process_requests (prof);
1042 static void
1043 throw_exc (MonoProfiler *prof, MonoObject *object)
1045 int do_bt = (nocalls && runtime_inited && !notraces)? TYPE_EXCEPTION_BT: 0;
1046 uint64_t now;
1047 FrameData data;
1048 LogBuffer *logbuffer;
1049 if (do_bt)
1050 collect_bt (&data);
1051 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1052 now = current_time ();
1053 ENTER_LOG (logbuffer, "throw");
1054 emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1055 emit_time (logbuffer, now);
1056 emit_obj (logbuffer, object);
1057 if (do_bt)
1058 emit_bt (logbuffer, &data);
1059 EXIT_LOG (logbuffer);
1060 process_requests (prof);
1063 static void
1064 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1066 uint64_t now;
1067 LogBuffer *logbuffer = ensure_logbuf (16);
1068 now = current_time ();
1069 ENTER_LOG (logbuffer, "clause");
1070 emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1071 emit_time (logbuffer, now);
1072 emit_value (logbuffer, clause_type);
1073 emit_value (logbuffer, clause_num);
1074 emit_method (logbuffer, method);
1075 EXIT_LOG (logbuffer);
1078 static void
1079 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1081 int do_bt = (nocalls && runtime_inited && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1082 uint64_t now;
1083 FrameData data;
1084 LogBuffer *logbuffer;
1085 if (do_bt)
1086 collect_bt (&data);
1087 logbuffer = ensure_logbuf (16 + MAX_FRAMES * 8);
1088 now = current_time ();
1089 ENTER_LOG (logbuffer, "monitor");
1090 emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1091 emit_time (logbuffer, now);
1092 emit_obj (logbuffer, object);
1093 if (do_bt)
1094 emit_bt (logbuffer, &data);
1095 EXIT_LOG (logbuffer);
1096 process_requests (profiler);
1099 static void
1100 thread_start (MonoProfiler *prof, uintptr_t tid)
1102 //printf ("thread start %p\n", (void*)tid);
1103 init_thread ();
1106 static void
1107 thread_end (MonoProfiler *prof, uintptr_t tid)
1109 take_lock ();
1110 if (TLS_GET (tlsbuffer))
1111 dump_buffer (prof, TLS_GET (tlsbuffer));
1112 release_lock ();
1113 TLS_SET (tlsbuffer, NULL);
1116 static void
1117 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1119 int len = strlen (name) + 1;
1120 uint64_t now;
1121 LogBuffer *logbuffer;
1122 logbuffer = ensure_logbuf (10 + len);
1123 now = current_time ();
1124 ENTER_LOG (logbuffer, "tname");
1125 emit_byte (logbuffer, TYPE_METADATA);
1126 emit_time (logbuffer, now);
1127 emit_byte (logbuffer, TYPE_THREAD);
1128 emit_ptr (logbuffer, (void*)tid);
1129 emit_value (logbuffer, 0); /* flags */
1130 memcpy (logbuffer->data, name, len);
1131 logbuffer->data += len;
1132 EXIT_LOG (logbuffer);
1135 #ifndef HOST_WIN32
1136 #include "mono/io-layer/atomic.h"
1137 #endif
1138 #define cmp_exchange InterlockedCompareExchangePointer
1139 /*#else
1140 static void*
1141 cmp_exchange (volatile void **dest, void *exch, void *comp)
1143 void *old;
1144 __asm__ __volatile__ ("lock; "
1145 #ifdef __x86_64__
1146 "cmpxchgq"
1147 #else
1148 "cmpxchgl"
1149 #endif
1150 " %2, %0"
1151 : "=m" (*dest), "=a" (old)
1152 : "r" (exch), "m" (*dest), "a" (comp));
1153 return old;
1155 #endif
1158 static void
1159 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
1161 StatBuffer *sbuf;
1162 uint64_t now;
1163 uintptr_t *data, *new_data, *old_data;
1164 if (in_shutdown)
1165 return;
1166 now = current_time ();
1167 if (do_debug) {
1168 int len;
1169 char buf [256];
1170 snprintf (buf, sizeof (buf), "hit at %p in thread %p at %llu\n", ip, (void*)thread_id (), (unsigned long long int)now);
1171 len = strlen (buf);
1172 write (2, buf, len);
1174 sbuf = profiler->stat_buffers;
1175 if (!sbuf)
1176 return;
1177 /* overflow */
1178 if (sbuf->data + 400 >= sbuf->data_end) {
1179 sbuf = create_stat_buffer ();
1180 sbuf->next = profiler->stat_buffers;
1181 profiler->stat_buffers = sbuf;
1182 if (do_debug)
1183 write (2, "overflow\n", 9);
1184 /* notify the helper thread */
1185 if (sbuf->next->next) {
1186 char c = 0;
1187 write (profiler->pipes [1], &c, 1);
1188 if (do_debug)
1189 write (2, "notify\n", 7);
1192 do {
1193 old_data = sbuf->data;
1194 new_data = old_data + 4;
1195 data = cmp_exchange ((volatile void**)&sbuf->data, new_data, old_data);
1196 } while (data != old_data);
1197 if (old_data >= sbuf->data_end)
1198 return; /* lost event */
1199 old_data [0] = 1 | (sample_type << 16);
1200 old_data [1] = thread_id ();
1201 old_data [2] = (now - profiler->startup_time) / 10000;
1202 old_data [3] = (uintptr_t)ip;
1205 static uintptr_t *code_pages = 0;
1206 static int num_code_pages = 0;
1207 static int size_code_pages = 0;
1208 #define CPAGE_SHIFT (9)
1209 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
1210 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
1211 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
1213 static uintptr_t
1214 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
1216 uintptr_t i;
1217 uintptr_t start_pos;
1218 start_pos = (page >> CPAGE_SHIFT) % hsize;
1219 i = start_pos;
1220 do {
1221 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
1222 return 0;
1223 } else if (!hash [i]) {
1224 hash [i] = page;
1225 return 1;
1227 /* wrap around */
1228 if (++i == hsize)
1229 i = 0;
1230 } while (i != start_pos);
1231 /* should not happen */
1232 printf ("failed code page store\n");
1233 return 0;
1236 static void
1237 add_code_pointer (uintptr_t ip)
1239 uintptr_t i;
1240 if (num_code_pages * 2 >= size_code_pages) {
1241 uintptr_t *n;
1242 uintptr_t old_size = size_code_pages;
1243 size_code_pages *= 2;
1244 if (size_code_pages == 0)
1245 size_code_pages = 16;
1246 n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
1247 for (i = 0; i < old_size; ++i) {
1248 if (code_pages [i])
1249 add_code_page (n, size_code_pages, code_pages [i]);
1251 if (code_pages)
1252 free (code_pages);
1253 code_pages = n;
1255 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
1258 static void
1259 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
1261 uint64_t now;
1262 LogBuffer *logbuffer;
1263 int len;
1264 len = strlen (filename) + 1;
1265 now = current_time ();
1266 logbuffer = ensure_logbuf (20 + len);
1267 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
1268 emit_time (logbuffer, now);
1269 emit_svalue (logbuffer, load_addr);
1270 emit_uvalue (logbuffer, offset);
1271 emit_uvalue (logbuffer, size);
1272 memcpy (logbuffer->data, filename, len);
1273 logbuffer->data += len;
1276 static void
1277 dump_usym (const char *name, uintptr_t value, uintptr_t size)
1279 LogBuffer *logbuffer;
1280 int len;
1281 len = strlen (name) + 1;
1282 logbuffer = ensure_logbuf (20 + len);
1283 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
1284 emit_ptr (logbuffer, (void*)value);
1285 emit_value (logbuffer, size);
1286 memcpy (logbuffer->data, name, len);
1287 logbuffer->data += len;
1290 #ifdef ELFMAG0
1292 #if SIZEOF_VOID_P == 4
1293 #define ELF_WSIZE 32
1294 #else
1295 #define ELF_WSIZE 64
1296 #endif
1297 #ifndef ElfW
1298 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
1299 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
1300 #define _ElfW_1(e,w,t) e##w##t
1301 #endif
1303 static void
1304 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
1306 int i;
1307 for (i = 0; i < num_symbols; ++i) {
1308 const char* sym;
1309 sym = strtab + symbols [i].st_name;
1310 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
1311 continue;
1312 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
1313 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
1317 static int
1318 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
1320 int fd, i;
1321 void *data;
1322 struct stat statb;
1323 uint64_t file_size;
1324 ElfW(Ehdr) *header;
1325 ElfW(Shdr) *sheader;
1326 ElfW(Shdr) *shstrtabh;
1327 ElfW(Shdr) *symtabh = NULL;
1328 ElfW(Shdr) *strtabh = NULL;
1329 ElfW(Sym) *symbols = NULL;
1330 const char *strtab;
1331 int num_symbols;
1333 fd = open (filename, O_RDONLY);
1334 if (fd < 0)
1335 return 0;
1336 if (fstat (fd, &statb) != 0) {
1337 close (fd);
1338 return 0;
1340 file_size = statb.st_size;
1341 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
1342 close (fd);
1343 if (data == MAP_FAILED)
1344 return 0;
1345 header = data;
1346 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1347 header->e_ident [EI_MAG1] != ELFMAG1 ||
1348 header->e_ident [EI_MAG2] != ELFMAG2 ||
1349 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1350 munmap (data, file_size);
1351 return 0;
1353 sheader = (void*)((char*)data + header->e_shoff);
1354 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
1355 strtab = (const char*)data + shstrtabh->sh_offset;
1356 for (i = 0; i < header->e_shnum; ++i) {
1357 //printf ("section header: %d\n", sheader->sh_type);
1358 if (sheader->sh_type == SHT_SYMTAB) {
1359 symtabh = sheader;
1360 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
1361 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
1362 break;
1364 sheader = (void*)((char*)sheader + header->e_shentsize);
1366 if (!symtabh || !strtabh) {
1367 munmap (data, file_size);
1368 return 0;
1370 strtab = (const char*)data + strtabh->sh_offset;
1371 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
1372 symbols = (void*)((char*)data + symtabh->sh_offset);
1373 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
1374 munmap (data, file_size);
1375 return 1;
1377 #endif
1379 #if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
1380 static int
1381 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
1383 MonoProfiler *prof = data;
1384 char buf [256];
1385 const char *filename;
1386 BinaryObject *obj;
1387 char *a = (void*)info->dlpi_addr;
1388 int i, num_sym;
1389 ElfW(Dyn) *dyn = NULL;
1390 ElfW(Sym) *symtab = NULL;
1391 ElfW(Word) *hash_table = NULL;
1392 ElfW(Ehdr) *header = NULL;
1393 const char* strtab = NULL;
1394 for (obj = prof->binary_objects; obj; obj = obj->next) {
1395 if (obj->addr == a)
1396 return 0;
1398 filename = info->dlpi_name;
1399 if (!info->dlpi_addr && !filename [0]) {
1400 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
1401 if (l > 0) {
1402 buf [l] = 0;
1403 filename = buf;
1406 obj = calloc (sizeof (BinaryObject), 1);
1407 obj->addr = (void*)info->dlpi_addr;
1408 obj->name = pstrdup (filename);
1409 obj->next = prof->binary_objects;
1410 prof->binary_objects = obj;
1411 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
1412 a = NULL;
1413 for (i = 0; i < info->dlpi_phnum; ++i) {
1414 //printf ("segment type %d file offset: %d, size: %d\n", info->dlpi_phdr[i].p_type, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1415 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
1416 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1417 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
1418 header->e_ident [EI_MAG1] != ELFMAG1 ||
1419 header->e_ident [EI_MAG2] != ELFMAG2 ||
1420 header->e_ident [EI_MAG3] != ELFMAG3 ) {
1421 header = NULL;
1423 dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
1424 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
1425 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1428 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
1429 return 0;
1430 if (!info->dlpi_name || !info->dlpi_name[0])
1431 return 0;
1432 if (!dyn)
1433 return 0;
1434 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
1435 if (dyn [i].d_tag == DT_SYMTAB) {
1436 if (symtab && do_debug)
1437 printf ("multiple symtabs: %d\n", i);
1438 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
1439 } else if (dyn [i].d_tag == DT_HASH) {
1440 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
1441 } else if (dyn [i].d_tag == DT_STRTAB) {
1442 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
1445 if (!hash_table)
1446 return 0;
1447 num_sym = hash_table [1];
1448 dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
1449 return 0;
1452 static int
1453 load_binaries (MonoProfiler *prof)
1455 dl_iterate_phdr (elf_dl_callback, prof);
1456 return 1;
1458 #else
1459 static int
1460 load_binaries (MonoProfiler *prof)
1462 return 0;
1464 #endif
1466 static const char*
1467 symbol_for (uintptr_t code)
1469 #ifdef HAVE_DLADDR
1470 void *ip = (void*)code;
1471 Dl_info di;
1472 if (dladdr (ip, &di)) {
1473 if (di.dli_sname)
1474 return di.dli_sname;
1475 } else {
1476 /* char **names;
1477 names = backtrace_symbols (&ip, 1);
1478 if (names) {
1479 const char* p = names [0];
1480 free (names);
1481 return p;
1485 #endif
1486 return NULL;
1489 static void
1490 dump_unmanaged_coderefs (MonoProfiler *prof)
1492 int i;
1493 const char* last_symbol;
1494 uintptr_t addr, page_end;
1496 if (load_binaries (prof))
1497 return;
1498 for (i = 0; i < size_code_pages; ++i) {
1499 const char* sym;
1500 if (!code_pages [i] || code_pages [i] & 1)
1501 continue;
1502 last_symbol = NULL;
1503 addr = CPAGE_ADDR (code_pages [i]);
1504 page_end = addr + CPAGE_SIZE;
1505 code_pages [i] |= 1;
1506 /* we dump the symbols for the whole page */
1507 for (; addr < page_end; addr += 16) {
1508 sym = symbol_for (addr);
1509 if (sym && sym == last_symbol)
1510 continue;
1511 last_symbol = sym;
1512 if (!sym)
1513 continue;
1514 dump_usym (sym, addr, 0); /* let's not guess the size */
1515 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
1520 static void
1521 dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf, int recurse)
1523 uintptr_t *sample;
1524 LogBuffer *logbuffer;
1525 if (!sbuf)
1526 return;
1527 if (recurse && sbuf->next) {
1528 dump_sample_hits (prof, sbuf->next, 1);
1529 free_buffer (sbuf->next, sbuf->next->size);
1530 sbuf->next = NULL;
1532 for (sample = sbuf->buf; sample < sbuf->data;) {
1533 int i;
1534 int count = sample [0] & 0xffff;
1535 int type = sample [0] >> 16;
1536 if (sample + count + 3 > sbuf->data)
1537 break;
1538 logbuffer = ensure_logbuf (20 + count * 8);
1539 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1540 emit_value (logbuffer, type);
1541 emit_uvalue (logbuffer, (prof->startup_time + sample [2]) * 10000);
1542 emit_value (logbuffer, count);
1543 for (i = 0; i < count; ++i) {
1544 emit_ptr (logbuffer, (void*)sample [i + 3]);
1545 add_code_pointer (sample [i + 3]);
1547 sample += count + 3;
1549 dump_unmanaged_coderefs (prof);
1552 #if USE_PERF_EVENTS
1553 #ifndef __NR_perf_event_open
1554 #define __NR_perf_event_open 241
1555 #endif
1557 static int perf_fd = -1;
1558 static void *mmap_base;
1559 static struct perf_event_mmap_page *page_desc = NULL;
1560 static int num_pages = 64;
1561 static unsigned int mmap_mask;
1562 static unsigned int prev_pos = 0;
1564 typedef struct {
1565 struct perf_event_header h;
1566 uint64_t ip;
1567 uint32_t pid;
1568 uint32_t tid;
1569 uint64_t timestamp;
1570 uint64_t period;
1571 uint64_t nframes;
1572 } PSample;
1574 static int
1575 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
1577 attr->size = PERF_ATTR_SIZE_VER0;
1578 //printf ("perf attr size: %d\n", attr->size);
1579 #if defined(__x86_64__)
1580 return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
1581 #elif defined(__i386__)
1582 return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
1583 #else
1584 return -1;
1585 #endif
1588 static int
1589 setup_perf_map (void)
1591 mmap_mask = num_pages * getpagesize () - 1;
1592 mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf_fd, 0);
1593 if (mmap_base == MAP_FAILED) {
1594 if (do_debug)
1595 printf ("failed mmap\n");
1596 return 0;
1598 page_desc = mmap_base;
1599 if (do_debug)
1600 printf ("mmap version: %d\n", page_desc->version);
1601 return 1;
1604 static void
1605 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
1607 LogBuffer *logbuffer;
1608 void *end = (char*)buf + size;
1609 int samples = 0;
1610 int pid = getpid ();
1612 while (buf < end) {
1613 PSample *s = buf;
1614 if (s->h.size == 0)
1615 break;
1616 if (pid != s->pid) {
1617 if (do_debug)
1618 printf ("event for different pid: %d\n", s->pid);
1619 buf = (char*)buf + s->h.size;
1620 continue;
1622 /*ip = (void*)s->ip;
1623 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
1624 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
1625 logbuffer = ensure_logbuf (20 + s->nframes * 8);
1626 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
1627 emit_value (logbuffer, sample_type);
1628 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
1629 emit_value (logbuffer, 1); /* count */
1630 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
1631 add_code_pointer (s->ip);
1632 buf = (char*)buf + s->h.size;
1633 samples++;
1635 if (do_debug)
1636 printf ("dumped %d samples\n", samples);
1637 dump_unmanaged_coderefs (prof);
1640 /* read events from the ring buffer */
1641 static int
1642 read_perf_mmap (MonoProfiler* prof)
1644 unsigned char *buf;
1645 unsigned char *data = (unsigned char*)mmap_base + getpagesize ();
1646 unsigned int head = page_desc->data_head;
1647 int diff, size;
1648 unsigned int old;
1650 #if defined(__i386__)
1651 asm volatile("lock; addl $0,0(%%esp)":::"memory");
1652 #elif defined (__x86_64__)
1653 asm volatile("lfence":::"memory");
1654 #endif
1656 old = prev_pos;
1657 diff = head - old;
1658 if (diff < 0) {
1659 if (do_debug)
1660 printf ("lost mmap events: old: %d, head: %d\n", old, head);
1661 old = head;
1663 size = head - old;
1664 if ((old & mmap_mask) + size != (head & mmap_mask)) {
1665 buf = data + (old & mmap_mask);
1666 size = mmap_mask + 1 - (old & mmap_mask);
1667 old += size;
1668 /* size bytes at buf */
1669 if (do_debug)
1670 printf ("found1 bytes of events: %d\n", size);
1671 dump_perf_hits (prof, buf, size);
1673 buf = data + (old & mmap_mask);
1674 size = head - old;
1675 /* size bytes at buf */
1676 if (do_debug)
1677 printf ("found bytes of events: %d\n", size);
1678 dump_perf_hits (prof, buf, size);
1679 old += size;
1680 prev_pos = old;
1681 page_desc->data_tail = old;
1682 return 0;
1685 static int
1686 setup_perf_event (void)
1688 struct perf_event_attr attr;
1689 memset (&attr, 0, sizeof (attr));
1690 attr.type = PERF_TYPE_HARDWARE;
1691 switch (sample_type) {
1692 case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1693 case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
1694 case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
1695 case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
1696 case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
1697 case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
1698 default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
1700 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
1701 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
1702 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
1703 attr.inherit = 1;
1704 attr.freq = 1;
1705 attr.sample_freq = sample_freq;
1707 perf_fd = perf_event_syscall (&attr, getpid (), -1, -1, 0);
1708 if (do_debug)
1709 printf ("perf fd: %d, freq: %d, event: %llu\n", perf_fd, sample_freq, attr.config);
1710 if (perf_fd < 0) {
1711 if (perf_fd == -EPERM) {
1712 fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
1713 } else {
1714 if (do_debug)
1715 perror ("open perf event");
1717 return 0;
1719 if (!setup_perf_map ()) {
1720 close (perf_fd);
1721 perf_fd = -1;
1722 return 0;
1724 return 1;
1727 #endif /* USE_PERF_EVENTS */
1729 static void
1730 log_shutdown (MonoProfiler *prof)
1732 in_shutdown = 1;
1733 #ifndef DISABLE_HELPER_THREAD
1734 if (prof->command_port) {
1735 char c = 1;
1736 void *res;
1737 write (prof->pipes [1], &c, 1);
1738 pthread_join (prof->helper_thread, &res);
1740 #endif
1741 #if USE_PERF_EVENTS
1742 if (page_desc)
1743 read_perf_mmap (prof);
1744 #endif
1745 dump_sample_hits (prof, prof->stat_buffers, 1);
1746 take_lock ();
1747 if (TLS_GET (tlsbuffer))
1748 dump_buffer (prof, TLS_GET (tlsbuffer));
1749 TLS_SET (tlsbuffer, NULL);
1750 release_lock ();
1751 #if defined (HAVE_SYS_ZLIB)
1752 if (prof->gzfile)
1753 gzclose (prof->gzfile);
1754 #endif
1755 if (prof->pipe_output)
1756 pclose (prof->file);
1757 else
1758 fclose (prof->file);
1759 free (prof);
1762 static char*
1763 new_filename (const char* filename)
1765 time_t t = time (NULL);
1766 int pid = process_id ();
1767 char pid_buf [16];
1768 char time_buf [16];
1769 char *res, *d;
1770 const char *p;
1771 int count_dates = 0;
1772 int count_pids = 0;
1773 int s_date, s_pid;
1774 struct tm *ts;
1775 for (p = filename; *p; p++) {
1776 if (*p != '%')
1777 continue;
1778 p++;
1779 if (*p == 't')
1780 count_dates++;
1781 else if (*p == 'p')
1782 count_pids++;
1783 else if (*p == 0)
1784 break;
1786 if (!count_dates && !count_pids)
1787 return pstrdup (filename);
1788 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
1789 ts = gmtime (&t);
1790 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
1791 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
1792 s_date = strlen (time_buf);
1793 s_pid = strlen (pid_buf);
1794 d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
1795 for (p = filename; *p; p++) {
1796 if (*p != '%') {
1797 *d++ = *p;
1798 continue;
1800 p++;
1801 if (*p == 't') {
1802 strcpy (d, time_buf);
1803 d += s_date;
1804 continue;
1805 } else if (*p == 'p') {
1806 strcpy (d, pid_buf);
1807 d += s_pid;
1808 continue;
1809 } else if (*p == '%') {
1810 *d++ = '%';
1811 continue;
1812 } else if (*p == 0)
1813 break;
1814 *d++ = '%';
1815 *d++ = *p;
1817 *d = 0;
1818 return res;
1821 #ifndef DISABLE_HELPER_THREAD
1822 static void*
1823 helper_thread (void* arg)
1825 MonoProfiler* prof = arg;
1826 int command_socket;
1827 int len;
1828 char buf [64];
1829 MonoThread *thread = NULL;
1831 //fprintf (stderr, "Server listening\n");
1832 command_socket = -1;
1833 while (1) {
1834 fd_set rfds;
1835 struct timeval tv;
1836 int max_fd = -1;
1837 FD_ZERO (&rfds);
1838 FD_SET (prof->server_socket, &rfds);
1839 max_fd = prof->server_socket;
1840 FD_SET (prof->pipes [0], &rfds);
1841 if (max_fd < prof->pipes [0])
1842 max_fd = prof->pipes [0];
1843 if (command_socket >= 0) {
1844 FD_SET (command_socket, &rfds);
1845 if (max_fd < command_socket)
1846 max_fd = command_socket;
1848 #if USE_PERF_EVENTS
1849 if (perf_fd >= 0) {
1850 FD_SET (perf_fd, &rfds);
1851 if (max_fd < perf_fd)
1852 max_fd = perf_fd;
1854 #endif
1855 tv.tv_sec = 1;
1856 tv.tv_usec = 0;
1857 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
1858 if (FD_ISSET (prof->pipes [0], &rfds)) {
1859 char c;
1860 int r = read (prof->pipes [0], &c, 1);
1861 if (r == 1 && c == 0) {
1862 StatBuffer *sbuf = prof->stat_buffers->next->next;
1863 prof->stat_buffers->next->next = NULL;
1864 if (do_debug)
1865 fprintf (stderr, "stat buffer dump\n");
1866 dump_sample_hits (prof, sbuf, 1);
1867 free_buffer (sbuf, sbuf->size);
1868 continue;
1870 /* time to shut down */
1871 if (thread)
1872 mono_thread_detach (thread);
1873 if (do_debug)
1874 fprintf (stderr, "helper shutdown\n");
1875 #if USE_PERF_EVENTS
1876 if (perf_fd >= 0)
1877 read_perf_mmap (prof);
1878 #endif
1879 safe_dump (prof, ensure_logbuf (0));
1880 return NULL;
1882 #if USE_PERF_EVENTS
1883 if (perf_fd >= 0 && FD_ISSET (perf_fd, &rfds)) {
1884 read_perf_mmap (prof);
1885 safe_dump (prof, ensure_logbuf (0));
1887 #endif
1888 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
1889 len = read (command_socket, buf, sizeof (buf) - 1);
1890 if (len < 0)
1891 continue;
1892 if (len == 0) {
1893 close (command_socket);
1894 command_socket = -1;
1895 continue;
1897 buf [len] = 0;
1898 if (strcmp (buf, "heapshot\n") == 0) {
1899 heapshot_requested = 1;
1900 //fprintf (stderr, "perform heapshot\n");
1901 if (runtime_inited && !thread) {
1902 thread = mono_thread_attach (mono_get_root_domain ());
1903 /*fprintf (stderr, "attached\n");*/
1905 if (thread) {
1906 process_requests (prof);
1907 mono_thread_detach (thread);
1908 thread = NULL;
1911 continue;
1913 if (!FD_ISSET (prof->server_socket, &rfds)) {
1914 continue;
1916 command_socket = accept (prof->server_socket, NULL, NULL);
1917 if (command_socket < 0)
1918 continue;
1919 //fprintf (stderr, "Accepted connection\n");
1921 return NULL;
1924 static int
1925 start_helper_thread (MonoProfiler* prof)
1927 struct sockaddr_in server_address;
1928 int r;
1929 socklen_t slen;
1930 if (pipe (prof->pipes) < 0) {
1931 fprintf (stderr, "Cannot create pipe\n");
1932 return 0;
1934 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
1935 if (prof->server_socket < 0) {
1936 fprintf (stderr, "Cannot create server socket\n");
1937 return 0;
1939 memset (&server_address, 0, sizeof (server_address));
1940 server_address.sin_family = AF_INET;
1941 server_address.sin_addr.s_addr = INADDR_ANY;
1942 server_address.sin_port = htons (prof->command_port);
1943 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
1944 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
1945 close (prof->server_socket);
1946 return 0;
1948 if (listen (prof->server_socket, 1) < 0) {
1949 fprintf (stderr, "Cannot listen server socket\n");
1950 close (prof->server_socket);
1951 return 0;
1953 if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
1954 prof->command_port = ntohs (server_address.sin_port);
1955 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
1958 r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
1959 if (r) {
1960 close (prof->server_socket);
1961 return 0;
1963 return 1;
1965 #endif
1967 static MonoProfiler*
1968 create_profiler (const char *filename)
1970 MonoProfiler *prof;
1971 char *nf;
1972 int force_delete = 0;
1973 int need_helper_thread = 0;
1974 prof = calloc (1, sizeof (MonoProfiler));
1976 prof->command_port = command_port;
1977 if (filename && *filename == '-') {
1978 force_delete = 1;
1979 filename++;
1981 if (!filename) {
1982 if (do_report)
1983 filename = "|mprof-report -";
1984 else
1985 filename = "output.mlpd";
1986 nf = (char*)filename;
1987 } else {
1988 nf = new_filename (filename);
1989 if (do_report) {
1990 int s = strlen (nf) + 32;
1991 char *p = malloc (s);
1992 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
1993 free (nf);
1994 nf = p;
1997 if (*nf == '|') {
1998 prof->file = popen (nf + 1, "w");
1999 prof->pipe_output = 1;
2000 } else {
2001 FILE *f;
2002 if (force_delete)
2003 unlink (nf);
2004 if ((f = fopen (nf, "r"))) {
2005 fclose (f);
2006 fprintf (stderr, "The Mono profiler won't overwrite existing filename: %s.\n", nf);
2007 fprintf (stderr, "Profiling disabled: use a different name or -FILENAME to force overwrite.\n");
2008 free (prof);
2009 return NULL;
2011 prof->file = fopen (nf, "wb");
2013 if (!prof->file) {
2014 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
2015 exit (1);
2017 #if defined (HAVE_SYS_ZLIB)
2018 if (use_zip)
2019 prof->gzfile = gzdopen (fileno (prof->file), "wb");
2020 #endif
2021 #if USE_PERF_EVENTS
2022 if (sample_type && !do_mono_sample)
2023 need_helper_thread = setup_perf_event ();
2024 if (perf_fd < 0) {
2025 /* FIXME: warn if different freq or sample type */
2026 do_mono_sample = 1;
2028 #endif
2029 if (do_mono_sample) {
2030 prof->stat_buffers = create_stat_buffer ();
2031 need_helper_thread = 1;
2033 #ifndef DISABLE_HELPER_THREAD
2034 if (hs_mode_ondemand || need_helper_thread) {
2035 if (!start_helper_thread (prof))
2036 prof->command_port = 0;
2038 #else
2039 if (hs_mode_ondemand)
2040 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
2041 #endif
2042 prof->startup_time = current_time ();
2043 dump_header (prof);
2044 return prof;
2047 static void
2048 usage (int do_exit)
2050 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
2051 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
2052 printf ("Options:\n");
2053 printf ("\thelp show this usage info\n");
2054 printf ("\t[no]alloc enable/disable recording allocation info\n");
2055 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
2056 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
2057 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
2058 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/1000)\n");
2059 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
2060 printf ("\t TYPE can be followed by /FREQUENCY\n");
2061 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
2062 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
2063 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
2064 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
2065 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
2066 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
2067 printf ("\treport create a report instead of writing the raw data to a file\n");
2068 printf ("\tzip compress the output data\n");
2069 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
2070 if (do_exit)
2071 exit (1);
2074 static const char*
2075 match_option (const char* p, const char *opt, char **rval)
2077 int len = strlen (opt);
2078 if (strncmp (p, opt, len) == 0) {
2079 if (rval) {
2080 if (p [len] == '=' && p [len + 1]) {
2081 const char *opt = p + len + 1;
2082 const char *end = strchr (opt, ',');
2083 char *val;
2084 int l;
2085 if (end == NULL) {
2086 l = strlen (opt);
2087 } else {
2088 l = end - opt;
2090 val = malloc (l + 1);
2091 memcpy (val, opt, l);
2092 val [l] = 0;
2093 *rval = val;
2094 return opt + l;
2096 if (p [len] == 0 || p [len] == ',') {
2097 *rval = NULL;
2098 return p + len + (p [len] == ',');
2100 usage (1);
2101 } else {
2102 if (p [len] == 0)
2103 return p + len;
2104 if (p [len] == ',')
2105 return p + len + 1;
2108 return p;
2111 typedef struct {
2112 const char *name;
2113 int sample_mode;
2114 } SampleMode;
2116 static const SampleMode sample_modes [] = {
2117 {"cycles", SAMPLE_CYCLES},
2118 {"instr", SAMPLE_INSTRUCTIONS},
2119 {"cachemiss", SAMPLE_CACHE_MISSES},
2120 {"cacherefs", SAMPLE_CACHE_REFS},
2121 {"branches", SAMPLE_BRANCHES},
2122 {"branchmiss", SAMPLE_BRANCH_MISSES},
2123 {NULL, 0}
2126 static void
2127 set_sample_mode (char* val, int allow_empty)
2129 char *end;
2130 char *maybe_freq = NULL;
2131 unsigned int count;
2132 const SampleMode *smode = sample_modes;
2133 #ifndef USE_PERF_EVENTS
2134 do_mono_sample = 1;
2135 #endif
2136 if (allow_empty && !val) {
2137 sample_type = SAMPLE_CYCLES;
2138 sample_freq = 1000;
2139 return;
2141 if (strcmp (val, "mono") == 0) {
2142 do_mono_sample = 1;
2143 sample_type = SAMPLE_CYCLES;
2144 free (val);
2145 return;
2147 for (smode = sample_modes; smode->name; smode++) {
2148 int l = strlen (smode->name);
2149 if (strncmp (val, smode->name, l) == 0) {
2150 sample_type = smode->sample_mode;
2151 maybe_freq = val + l;
2152 break;
2155 if (!smode->name)
2156 usage (1);
2157 if (*maybe_freq == '/') {
2158 count = strtoul (maybe_freq + 1, &end, 10);
2159 if (maybe_freq + 1 == end)
2160 usage (1);
2161 sample_freq = count;
2162 } else if (*maybe_freq != 0) {
2163 usage (1);
2164 } else {
2165 sample_freq = 1000;
2167 free (val);
2170 static void
2171 set_hsmode (char* val, int allow_empty)
2173 char *end;
2174 unsigned int count;
2175 if (allow_empty && !val)
2176 return;
2177 if (strcmp (val, "ondemand") == 0) {
2178 hs_mode_ondemand = 1;
2179 free (val);
2180 return;
2182 count = strtoul (val, &end, 10);
2183 if (val == end)
2184 usage (1);
2185 if (strcmp (end, "ms") == 0)
2186 hs_mode_ms = count;
2187 else if (strcmp (end, "gc") == 0)
2188 hs_mode_gc = count;
2189 else
2190 usage (1);
2191 free (val);
2195 * declaration to silence the compiler: this is the entry point that
2196 * mono will load from the shared library and call.
2198 extern void
2199 mono_profiler_startup (const char *desc);
2201 void
2202 mono_profiler_startup (const char *desc)
2204 MonoProfiler *prof;
2205 char *filename = NULL;
2206 const char *p;
2207 const char *opt;
2208 int fast_time = 0;
2209 int calls_enabled = 0;
2210 int allocs_enabled = 0;
2211 int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
2212 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
2213 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
2214 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS;
2216 p = desc;
2217 if (strncmp (p, "log", 3))
2218 usage (1);
2219 p += 3;
2220 if (*p == ':')
2221 p++;
2222 for (; *p; p = opt) {
2223 char *val;
2224 if (*p == ',') {
2225 opt = p + 1;
2226 continue;
2228 if ((opt = match_option (p, "help", NULL)) != p) {
2229 usage (0);
2230 continue;
2232 if ((opt = match_option (p, "calls", NULL)) != p) {
2233 calls_enabled = 1;
2234 continue;
2236 if ((opt = match_option (p, "nocalls", NULL)) != p) {
2237 events &= ~MONO_PROFILE_ENTER_LEAVE;
2238 nocalls = 1;
2239 continue;
2241 if ((opt = match_option (p, "alloc", NULL)) != p) {
2242 allocs_enabled = 1;
2243 continue;
2245 if ((opt = match_option (p, "noalloc", NULL)) != p) {
2246 events &= ~MONO_PROFILE_ALLOCATIONS;
2247 continue;
2249 if ((opt = match_option (p, "time", &val)) != p) {
2250 if (strcmp (val, "fast") == 0)
2251 fast_time = 1;
2252 else if (strcmp (val, "null") == 0)
2253 fast_time = 2;
2254 else
2255 usage (1);
2256 free (val);
2257 continue;
2259 if ((opt = match_option (p, "report", NULL)) != p) {
2260 do_report = 1;
2261 continue;
2263 if ((opt = match_option (p, "debug", NULL)) != p) {
2264 do_debug = 1;
2265 continue;
2267 if ((opt = match_option (p, "heapshot", &val)) != p) {
2268 events &= ~MONO_PROFILE_ALLOCATIONS;
2269 events &= ~MONO_PROFILE_ENTER_LEAVE;
2270 nocalls = 1;
2271 do_heap_shot = 1;
2272 set_hsmode (val, 1);
2273 continue;
2275 if ((opt = match_option (p, "sample", &val)) != p) {
2276 events &= ~MONO_PROFILE_ALLOCATIONS;
2277 events &= ~MONO_PROFILE_ENTER_LEAVE;
2278 nocalls = 1;
2279 set_sample_mode (val, 1);
2280 continue;
2282 if ((opt = match_option (p, "hsmode", &val)) != p) {
2283 fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
2284 set_hsmode (val, 0);
2285 continue;
2287 if ((opt = match_option (p, "zip", NULL)) != p) {
2288 use_zip = 1;
2289 continue;
2291 if ((opt = match_option (p, "output", &val)) != p) {
2292 filename = val;
2293 continue;
2295 if ((opt = match_option (p, "port", &val)) != p) {
2296 char *end;
2297 command_port = strtoul (val, &end, 10);
2298 free (val);
2299 continue;
2301 if ((opt = match_option (p, "maxframes", &val)) != p) {
2302 char *end;
2303 num_frames = strtoul (val, &end, 10);
2304 if (num_frames > MAX_FRAMES)
2305 num_frames = MAX_FRAMES;
2306 free (val);
2307 notraces = num_frames == 0;
2308 continue;
2310 if ((opt = match_option (p, "calldepth", &val)) != p) {
2311 char *end;
2312 max_call_depth = strtoul (val, &end, 10);
2313 free (val);
2314 continue;
2316 if (opt == p) {
2317 usage (0);
2318 exit (0);
2321 if (calls_enabled) {
2322 events |= MONO_PROFILE_ENTER_LEAVE;
2323 nocalls = 0;
2325 if (allocs_enabled)
2326 events |= MONO_PROFILE_ALLOCATIONS;
2327 utils_init (fast_time);
2329 prof = create_profiler (filename);
2330 if (!prof)
2331 return;
2332 init_thread ();
2334 mono_profiler_install (prof, log_shutdown);
2335 mono_profiler_install_gc (gc_event, gc_resize);
2336 mono_profiler_install_allocation (gc_alloc);
2337 mono_profiler_install_gc_moves (gc_moves);
2338 mono_profiler_install_gc_roots (gc_handle, gc_roots);
2339 mono_profiler_install_class (NULL, class_loaded, NULL, NULL);
2340 mono_profiler_install_module (NULL, image_loaded, NULL, NULL);
2341 mono_profiler_install_thread (thread_start, thread_end);
2342 mono_profiler_install_thread_name (thread_name);
2343 mono_profiler_install_enter_leave (method_enter, method_leave);
2344 mono_profiler_install_jit_end (method_jitted);
2345 mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
2346 mono_profiler_install_monitor (monitor_event);
2347 mono_profiler_install_runtime_initialized (runtime_initialized);
2350 if (do_mono_sample && sample_type == SAMPLE_CYCLES) {
2351 events |= MONO_PROFILE_STATISTICAL;
2352 mono_profiler_install_statistical (mono_sample_hit);
2355 mono_profiler_set_events (events);
2357 TLS_INIT (tlsbuffer);