2 * proflog.c: mono log profiler
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2010 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
13 #include "../mini/jit.h"
14 #include <mono/metadata/profiler.h>
15 #include <mono/metadata/threads.h>
16 #include <mono/metadata/mono-gc.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/mono-perfcounters.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/utils/atomic.h>
24 #include <mono/utils/mono-membar.h>
25 #include <mono/utils/mono-counters.h>
26 #include <mono/utils/mono-mutex.h>
27 #include <mono/utils/mono-conc-hashtable.h>
28 #include <mono/utils/lock-free-queue.h>
38 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
39 #define DISABLE_HELPER_THREAD 1
48 #ifdef HAVE_EXECINFO_H
55 #ifndef DISABLE_HELPER_THREAD
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <sys/select.h>
68 #ifdef HAVE_SYS_STAT_H
75 #if defined (HAVE_SYS_ZLIB)
79 #if defined(__linux__)
82 #include <sys/syscall.h>
83 #include "perf_event.h"
85 #ifdef ENABLE_PERF_EVENTS
86 #define USE_PERF_EVENTS 1
88 static int read_perf_mmap (MonoProfiler* prof, int cpu);
93 #define BUFFER_SIZE (4096 * 16)
95 /* Worst-case size in bytes of a 64-bit value encoded with LEB128. */
96 #define LEB128_SIZE 10
97 /* Size in bytes of the event ID prefix. */
100 static int nocalls = 0;
101 static int notraces = 0;
102 static int use_zip = 0;
103 static int do_report = 0;
104 static int do_heap_shot = 0;
105 static int max_call_depth = 100;
106 static volatile int runtime_inited = 0;
107 static int need_helper_thread = 0;
108 static int command_port = 0;
109 static int heapshot_requested = 0;
110 static int sample_type = 0;
111 static int sample_freq = 0;
112 static int do_mono_sample = 0;
113 static int in_shutdown = 0;
114 static int do_debug = 0;
115 static int do_counters = 0;
116 static int do_coverage = 0;
117 static gboolean debug_coverage = FALSE;
118 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
120 typedef struct _LogBuffer LogBuffer;
126 * The file is composed by a header followed by 0 or more buffers.
127 * Each buffer contains events that happened on a thread: for a given thread
128 * buffers that appear later in the file are guaranteed to contain events
129 * that happened later in time. Buffers from separate threads could be interleaved,
131 * Buffers are not required to be aligned.
134 * [id: 4 bytes] constant value: LOG_HEADER_ID
135 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
136 * [format: 1 byte] version of the data format for the rest of the file
137 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
138 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
139 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
140 * [flags: 4 bytes] file format flags, should be 0 for now
141 * [pid: 4 bytes] pid of the profiled process
142 * [port: 2 bytes] tcp port for server if != 0
143 * [sysid: 2 bytes] operating system and architecture identifier
145 * The multiple byte integers are in little-endian format.
148 * [buffer header] [event]*
149 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
150 * Timing information and other values in the event data are usually stored
151 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
152 * some data is represented as a difference between the actual value and
153 * either the last value of the same type (like for timing information) or
154 * as the difference from a value stored in a buffer header.
156 * For timing information the data is stored as uleb128, since timing
157 * increases in a monotonic way in each thread: the value is the number of
158 * nanoseconds to add to the last seen timing data in a buffer. The first value
159 * in a buffer will be calculated from the time_base field in the buffer head.
161 * Object or heap sizes are stored as uleb128.
162 * Pointer differences are stored as sleb128, instead.
164 * If an unexpected value is found, the rest of the buffer should be ignored,
165 * as generally the later values need the former to be interpreted correctly.
167 * buffer header format:
168 * [bufid: 4 bytes] constant value: BUF_ID
169 * [len: 4 bytes] size of the data following the buffer header
170 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
171 * [ptr_base: 8 bytes] base value for pointers
172 * [obj_base: 8 bytes] base value for object addresses
173 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
174 * [method_base: 8 bytes] base value for MonoMethod pointers
177 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
178 * The data that follows depends on type and the extended info.
179 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
180 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
181 * The extended info bits are interpreted based on type, see
182 * each individual event description below.
183 * strings are represented as a 0-terminated utf8 sequence.
186 * [flags: uleb128] must be 0
187 * [num: uleb128] number of frames following
188 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
192 * exinfo: flags: TYPE_ALLOC_BT
193 * [time diff: uleb128] nanoseconds since last timing
194 * [ptr: sleb128] class as a byte difference from ptr_base
195 * [obj: sleb128] object address as a byte difference from obj_base
196 * [size: uleb128] size of the object in the heap
197 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
201 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED[_BT],
202 * TYPE_GC_HANDLE_DESTROYED[_BT]
203 * [time diff: uleb128] nanoseconds since last timing
204 * if exinfo == TYPE_GC_RESIZE
205 * [heap_size: uleb128] new heap size
206 * if exinfo == TYPE_GC_EVENT
207 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
208 * [generation: uleb128] GC generation event refers to
209 * if exinfo == TYPE_GC_MOVE
210 * [num_objects: uleb128] number of object moves that follow
211 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
212 * num is always an even number: the even items are the old
213 * addresses, the odd numbers are the respective new object addresses
214 * if exinfo == TYPE_GC_HANDLE_CREATED[_BT]
215 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
216 * upper bits reserved as flags
217 * [handle: uleb128] GC handle value
218 * [objaddr: sleb128] object pointer differences from obj_base
219 * If exinfo == TYPE_GC_HANDLE_CREATED_BT, a backtrace follows.
220 * if exinfo == TYPE_GC_HANDLE_DESTROYED[_BT]
221 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
222 * upper bits reserved as flags
223 * [handle: uleb128] GC handle value
224 * If exinfo == TYPE_GC_HANDLE_DESTROYED_BT, a backtrace follows.
226 * type metadata format:
227 * type: TYPE_METADATA
228 * exinfo: one of: TYPE_END_LOAD, TYPE_END_UNLOAD (optional for TYPE_THREAD and TYPE_DOMAIN)
229 * [time diff: uleb128] nanoseconds since last timing
230 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
231 * TYPE_THREAD, TYPE_CONTEXT
232 * [pointer: sleb128] pointer of the metadata type depending on mtype
233 * if mtype == TYPE_CLASS
234 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
235 * [flags: uleb128] must be 0
236 * [name: string] full class name
237 * if mtype == TYPE_IMAGE
238 * [flags: uleb128] must be 0
239 * [name: string] image file name
240 * if mtype == TYPE_ASSEMBLY
241 * [flags: uleb128] must be 0
242 * [name: string] assembly name
243 * if mtype == TYPE_DOMAIN
244 * [flags: uleb128] must be 0
245 * if mtype == TYPE_DOMAIN && exinfo == 0
246 * [name: string] domain friendly name
247 * if mtype == TYPE_CONTEXT
248 * [flags: uleb128] must be 0
249 * [domain: sleb128] domain id as pointer
250 * if mtype == TYPE_THREAD && (format_version < 11 || (format_version > 10 && exinfo == 0))
251 * [flags: uleb128] must be 0
252 * [name: string] thread name
254 * type method format:
256 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
257 * [time diff: uleb128] nanoseconds since last timing
258 * [method: sleb128] MonoMethod* as a pointer difference from the last such
259 * pointer or the buffer method_base
260 * if exinfo == TYPE_JIT
261 * [code address: sleb128] pointer to the native code as a diff from ptr_base
262 * [code size: uleb128] size of the generated code
263 * [name: string] full method name
265 * type runtime format:
267 * exinfo: one of: TYPE_JITHELPER
268 * [time diff: uleb128] nanoseconds since last timing
269 * if exinfo == TYPE_JITHELPER
270 * [type: uleb128] MonoProfilerCodeBufferType enum value
271 * [buffer address: sleb128] pointer to the native code as a diff from ptr_base
272 * [buffer size: uleb128] size of the generated code
273 * if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
274 * [name: string] buffer description name
276 * type monitor format:
278 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
279 * [time diff: uleb128] nanoseconds since last timing
280 * [object: sleb128] the lock object as a difference from obj_base
281 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
282 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
286 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
287 * if exinfo == TYPE_HEAP_START
288 * [time diff: uleb128] nanoseconds since last timing
289 * if exinfo == TYPE_HEAP_END
290 * [time diff: uleb128] nanoseconds since last timing
291 * if exinfo == TYPE_HEAP_OBJECT
292 * [object: sleb128] the object as a difference from obj_base
293 * [class: sleb128] the object MonoClass* as a difference from ptr_base
294 * [size: uleb128] size of the object on the heap
295 * [num_refs: uleb128] number of object references
296 * if (format version > 1) each referenced objref is preceded by a
297 * uleb128 encoded offset: the first offset is from the object address
298 * and each next offset is relative to the previous one
299 * [objrefs: sleb128]+ object referenced as a difference from obj_base
300 * The same object can appear multiple times, but only the first time
301 * with size != 0: in the other cases this data will only be used to
302 * provide additional referenced objects.
303 * if exinfo == TYPE_HEAP_ROOT
304 * [num_roots: uleb128] number of root references
305 * [num_gc: uleb128] number of major gcs
306 * [object: sleb128] the object as a difference from obj_base
307 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
308 * [extra_info: uleb128] the extra_info value
309 * object, root_type and extra_info are repeated num_roots times
313 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
314 * if exinfo == TYPE_SAMPLE_HIT
315 * [sample_type: uleb128] type of sample (SAMPLE_*)
316 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
317 * if (format_version > 10)
318 * [thread: sleb128] thread id as difference from ptr_base
319 * [count: uleb128] number of following instruction addresses
320 * [ip: sleb128]* instruction pointer as difference from ptr_base
321 * if (format_version > 5)
322 * [mbt_count: uleb128] number of managed backtrace info triplets (method + IL offset + native offset)
323 * [method: sleb128]* MonoMethod* as a pointer difference from the last such
324 * pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
325 * [il_offset: sleb128]* IL offset inside method where the hit occurred
326 * [native_offset: sleb128]* native offset inside method where the hit occurred
327 * if exinfo == TYPE_SAMPLE_USYM
328 * [address: sleb128] symbol address as a difference from ptr_base
329 * [size: uleb128] symbol size (may be 0 if unknown)
330 * [name: string] symbol name
331 * if exinfo == TYPE_SAMPLE_UBIN
332 * [time diff: uleb128] nanoseconds since last timing
333 * [address: sleb128] address where binary has been loaded
334 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
335 * [size: uleb128] memory size
336 * [name: string] binary name
337 * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
338 * [len: uleb128] number of counters
340 * [section: uleb128] section of counter
341 * if section == MONO_COUNTER_PERFCOUNTERS:
342 * [section_name: string] section name of counter
343 * [name: string] name of counter
344 * [type: uleb128] type of counter
345 * [unit: uleb128] unit of counter
346 * [variance: uleb128] variance of counter
347 * [index: uleb128] unique index of counter
348 * if exinfo == TYPE_SAMPLE_COUNTERS
349 * [timestamp: uleb128] sampling timestamp
351 * [index: uleb128] unique index of counter
354 * [type: uleb128] type of counter value
357 * [0: uleb128] 0 -> value is null
359 * [1: uleb128] 1 -> value is not null
360 * [value: string] counter value
362 * [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
364 * type coverage format
365 * type: TYPE_COVERAGE
366 * exinfo: one of TYPE_COVERAGE_METHOD, TYPE_COVERAGE_STATEMENT, TYPE_COVERAGE_ASSEMBLY, TYPE_COVERAGE_CLASS
367 * if exinfo == TYPE_COVERAGE_METHOD
368 * [assembly: string] name of assembly
369 * [class: string] name of the class
370 * [name: string] name of the method
371 * [signature: string] the signature of the method
372 * [filename: string] the file path of the file that contains this method
373 * [token: uleb128] the method token
374 * [method_id: uleb128] an ID for this data to associate with the buffers of TYPE_COVERAGE_STATEMENTS
375 * [len: uleb128] the number of TYPE_COVERAGE_BUFFERS associated with this method
376 * if exinfo == TYPE_COVERAGE_STATEMENTS
377 * [method_id: uleb128] an the TYPE_COVERAGE_METHOD buffer to associate this with
378 * [offset: uleb128] the il offset relative to the previous offset
379 * [counter: uleb128] the counter for this instruction
380 * [line: uleb128] the line of filename containing this instruction
381 * [column: uleb128] the column containing this instruction
382 * if exinfo == TYPE_COVERAGE_ASSEMBLY
383 * [name: string] assembly name
384 * [guid: string] assembly GUID
385 * [filename: string] assembly filename
386 * [number_of_methods: uleb128] the number of methods in this assembly
387 * [fully_covered: uleb128] the number of fully covered methods
388 * [partially_covered: uleb128] the number of partially covered methods
389 * currently partially_covered will always be 0, and fully_covered is the
390 * number of methods that are fully and partially covered.
391 * if exinfo == TYPE_COVERAGE_CLASS
392 * [name: string] assembly name
393 * [class: string] class name
394 * [number_of_methods: uleb128] the number of methods in this class
395 * [fully_covered: uleb128] the number of fully covered methods
396 * [partially_covered: uleb128] the number of partially covered methods
397 * currently partially_covered will always be 0, and fully_covered is the
398 * number of methods that are fully and partially covered.
402 * Format oddities that we ought to fix:
404 * - Methods written in emit_bt () should be based on the buffer's base
405 * method instead of the base pointer.
406 * - The TYPE_SAMPLE_HIT event contains (currently) pointless data like
407 * always-one unmanaged frame count and always-zero IL offsets.
409 * These are mostly small things and are not worth a format change by
410 * themselves. They should be done when some other major change has to
411 * be done to the format.
419 uintptr_t method_base;
420 uintptr_t last_method;
423 unsigned char* data_end;
428 unsigned char buf [1];
432 ign_res (int G_GNUC_UNUSED unused, ...)
436 #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++;}
437 #define EXIT_LOG(lb) (lb)->locked--;
439 typedef struct _StatBuffer StatBuffer;
448 typedef struct _BinaryObject BinaryObject;
450 struct _BinaryObject {
456 struct _MonoProfiler {
457 StatBuffer *stat_buffers;
459 #if defined (HAVE_SYS_ZLIB)
462 uint64_t startup_time;
464 int last_gc_gen_started;
469 pthread_t helper_thread;
470 pthread_t writer_thread;
472 volatile gint32 run_writer_thread;
473 MonoLockFreeQueue writer_queue;
474 MonoConcurrentHashTable *method_table;
475 mono_mutex_t method_table_mutex;
476 BinaryObject *binary_objects;
477 GPtrArray *coverage_filters;
478 GPtrArray *sorted_sample_events;
481 typedef struct _WriterQueueEntry WriterQueueEntry;
482 struct _WriterQueueEntry {
483 MonoLockFreeQueueNode node;
488 typedef struct _MethodInfo MethodInfo;
500 #define TLS_SET(x,y) (TlsSetValue (x, y))
501 #define TLS_GET(t,x) ((t *) TlsGetValue (x))
502 #define TLS_INIT(x) (x = TlsAlloc ())
503 static int tlsbuffer;
504 static int tlsmethodlist;
506 #define TLS_SET(x,y) (x = y)
507 #define TLS_GET(t,x) (x)
509 static __thread LogBuffer* tlsbuffer = NULL;
510 static __thread GPtrArray* tlsmethodlist = NULL;
512 #define TLS_SET(x,y) (pthread_setspecific (x, y))
513 #define TLS_GET(t,x) ((t *) pthread_getspecific (x))
514 #define TLS_INIT(x) (pthread_key_create (&x, NULL))
515 static pthread_key_t tlsbuffer;
516 static pthread_key_t tlsmethodlist;
519 static void safe_send (MonoProfiler *profiler, LogBuffer *logbuffer);
522 pstrdup (const char *s)
524 int len = strlen (s) + 1;
525 char *p = malloc (len);
531 create_stat_buffer (void)
533 StatBuffer* buf = alloc_buffer (BUFFER_SIZE);
534 buf->size = BUFFER_SIZE;
535 buf->data_end = (uintptr_t*)((unsigned char*)buf + buf->size);
536 buf->data = buf->buf;
543 LogBuffer* buf = alloc_buffer (BUFFER_SIZE);
544 buf->size = BUFFER_SIZE;
545 buf->time_base = current_time ();
546 buf->last_time = buf->time_base;
547 buf->data_end = (unsigned char*)buf + buf->size;
548 buf->data = buf->buf;
555 if (!TLS_GET (LogBuffer, tlsbuffer)) {
556 LogBuffer *logbuffer = create_buffer ();
557 TLS_SET (tlsbuffer, logbuffer);
558 logbuffer->thread_id = thread_id ();
560 if (!TLS_GET (GPtrArray, tlsmethodlist)) {
561 GPtrArray *methodlist = g_ptr_array_new ();
562 TLS_SET (tlsmethodlist, methodlist);
565 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
569 ensure_logbuf_inner (LogBuffer *old, int bytes)
571 if (old && old->data + bytes + 100 < old->data_end)
574 LogBuffer *new = create_buffer ();
575 new->thread_id = thread_id ();
579 new->call_depth = old->call_depth;
585 ensure_logbuf (int bytes)
587 LogBuffer *old = TLS_GET (LogBuffer, tlsbuffer);
588 LogBuffer *new = ensure_logbuf_inner (old, bytes);
591 return old; // Still enough space.
593 TLS_SET (tlsbuffer, new);
600 emit_byte (LogBuffer *logbuffer, int value)
602 logbuffer->data [0] = value;
604 assert (logbuffer->data <= logbuffer->data_end);
608 emit_value (LogBuffer *logbuffer, int value)
610 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
611 assert (logbuffer->data <= logbuffer->data_end);
615 emit_time (LogBuffer *logbuffer, uint64_t value)
617 uint64_t tdiff = value - logbuffer->last_time;
618 if (value < logbuffer->last_time)
619 printf ("time went backwards\n");
620 //if (tdiff > 1000000)
621 // printf ("large time offset: %llu\n", tdiff);
622 encode_uleb128 (tdiff, logbuffer->data, &logbuffer->data);
623 /*if (tdiff != decode_uleb128 (p, &p))
624 printf ("incorrect encoding: %llu\n", tdiff);*/
625 logbuffer->last_time = value;
626 assert (logbuffer->data <= logbuffer->data_end);
630 emit_svalue (LogBuffer *logbuffer, int64_t value)
632 encode_sleb128 (value, logbuffer->data, &logbuffer->data);
633 assert (logbuffer->data <= logbuffer->data_end);
637 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
639 encode_uleb128 (value, logbuffer->data, &logbuffer->data);
640 assert (logbuffer->data <= logbuffer->data_end);
644 emit_ptr (LogBuffer *logbuffer, void *ptr)
646 if (!logbuffer->ptr_base)
647 logbuffer->ptr_base = (uintptr_t)ptr;
648 emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
649 assert (logbuffer->data <= logbuffer->data_end);
653 emit_method_inner (LogBuffer *logbuffer, void *method)
655 if (!logbuffer->method_base) {
656 logbuffer->method_base = (intptr_t)method;
657 logbuffer->last_method = (intptr_t)method;
659 encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->data, &logbuffer->data);
660 logbuffer->last_method = (intptr_t)method;
661 assert (logbuffer->data <= logbuffer->data_end);
670 find_method (MonoDomain *domain, void *user_data)
672 MethodSearch *search = user_data;
677 MonoJitInfo *ji = mono_get_jit_info_from_method (domain, search->method);
679 // It could be AOT'd, so we need to get it from the AOT runtime's cache.
681 void *ip = mono_aot_get_method (domain, search->method);
683 // Avoid a slow path in mono_jit_info_table_find ().
685 ji = mono_jit_info_table_find (domain, ip);
693 register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
695 if (!mono_conc_hashtable_lookup (prof->method_table, method)) {
697 MethodSearch search = { method, NULL };
699 mono_domain_foreach (find_method, &search);
705 * FIXME: We can't always find JIT info for a generic shared method, especially
706 * if we obtained the MonoMethod during an async stack walk. For now, we deal
707 * with this by giving the generic shared method name and dummy code start/size
708 * information (i.e. zeroes).
712 MethodInfo *info = malloc (sizeof (MethodInfo));
714 info->method = method;
716 info->time = current_time ();
718 g_ptr_array_add (TLS_GET (GPtrArray, tlsmethodlist), info);
723 emit_method (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
725 register_method_local (prof, method, NULL);
726 emit_method_inner (logbuffer, method);
730 emit_method_as_ptr (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
732 register_method_local (prof, method, NULL);
733 emit_ptr (logbuffer, method);
737 emit_obj (LogBuffer *logbuffer, void *ptr)
739 if (!logbuffer->obj_base)
740 logbuffer->obj_base = (uintptr_t)ptr >> 3;
741 emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
742 assert (logbuffer->data <= logbuffer->data_end);
746 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
750 for (; i < size; i++) {
753 emit_byte (logbuffer, str [i]);
756 emit_byte (logbuffer, '\0');
760 emit_double (LogBuffer *logbuffer, double value)
763 unsigned char buffer[8];
764 memcpy (buffer, &value, 8);
765 #if G_BYTE_ORDER == G_BIG_ENDIAN
766 for (i = 7; i >= 0; i--)
768 for (i = 0; i < 8; i++)
770 emit_byte (logbuffer, buffer[i]);
774 write_int16 (char *buf, int32_t value)
777 for (i = 0; i < 2; ++i) {
785 write_int32 (char *buf, int32_t value)
788 for (i = 0; i < 4; ++i) {
796 write_int64 (char *buf, int64_t value)
799 for (i = 0; i < 8; ++i) {
807 dump_header (MonoProfiler *profiler)
811 p = write_int32 (p, LOG_HEADER_ID);
812 *p++ = LOG_VERSION_MAJOR;
813 *p++ = LOG_VERSION_MINOR;
814 *p++ = LOG_DATA_VERSION;
815 *p++ = sizeof (void*);
816 p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
817 p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
818 p = write_int32 (p, 0); /* flags */
819 p = write_int32 (p, process_id ()); /* pid */
820 p = write_int16 (p, profiler->command_port); /* port */
821 p = write_int16 (p, 0); /* opsystem */
822 #if defined (HAVE_SYS_ZLIB)
823 if (profiler->gzfile) {
824 gzwrite (profiler->gzfile, hbuf, p - hbuf);
826 fwrite (hbuf, p - hbuf, 1, profiler->file);
829 fwrite (hbuf, p - hbuf, 1, profiler->file);
830 fflush (profiler->file);
835 send_buffer (MonoProfiler *prof, GPtrArray *methods, LogBuffer *buffer)
837 WriterQueueEntry *entry = calloc (1, sizeof (WriterQueueEntry));
838 mono_lock_free_queue_node_init (&entry->node, FALSE);
839 entry->methods = methods;
840 entry->buffer = buffer;
841 mono_lock_free_queue_enqueue (&prof->writer_queue, &entry->node);
845 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
850 dump_buffer (profiler, buf->next);
851 p = write_int32 (p, BUF_ID);
852 p = write_int32 (p, buf->data - buf->buf);
853 p = write_int64 (p, buf->time_base);
854 p = write_int64 (p, buf->ptr_base);
855 p = write_int64 (p, buf->obj_base);
856 p = write_int64 (p, buf->thread_id);
857 p = write_int64 (p, buf->method_base);
858 #if defined (HAVE_SYS_ZLIB)
859 if (profiler->gzfile) {
860 gzwrite (profiler->gzfile, hbuf, p - hbuf);
861 gzwrite (profiler->gzfile, buf->buf, buf->data - buf->buf);
864 fwrite (hbuf, p - hbuf, 1, profiler->file);
865 fwrite (buf->buf, buf->data - buf->buf, 1, profiler->file);
866 fflush (profiler->file);
867 #if defined (HAVE_SYS_ZLIB)
870 free_buffer (buf, buf->size);
874 process_requests (MonoProfiler *profiler)
876 if (heapshot_requested)
877 mono_gc_collect (mono_gc_max_generation ());
880 static void counters_init (MonoProfiler *profiler);
881 static void counters_sample (MonoProfiler *profiler, uint64_t timestamp);
884 * Can be called only at safe callback locations.
887 safe_send (MonoProfiler *profiler, LogBuffer *logbuffer)
889 /* We need the runtime initialized so that we have threads and hazard
890 * pointers available. Otherwise, the lock free queue will not work and
891 * there won't be a thread to process the data.
893 * While the runtime isn't initialized, we just accumulate data in the
894 * thread local buffer list.
896 if (!InterlockedRead (&runtime_inited))
899 int cd = logbuffer->call_depth;
901 send_buffer (profiler, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
903 TLS_SET (tlsbuffer, NULL);
904 TLS_SET (tlsmethodlist, NULL);
908 TLS_GET (LogBuffer, tlsbuffer)->call_depth = cd;
912 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
915 uintptr_t last_offset = 0;
916 //const char *name = mono_class_get_name (klass);
917 LogBuffer *logbuffer = ensure_logbuf (
918 EVENT_SIZE /* event */ +
919 LEB128_SIZE /* obj */ +
920 LEB128_SIZE /* klass */ +
921 LEB128_SIZE /* size */ +
922 LEB128_SIZE /* num */ +
924 LEB128_SIZE /* offset */ +
925 LEB128_SIZE /* ref */
928 emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
929 emit_obj (logbuffer, obj);
930 emit_ptr (logbuffer, klass);
931 /* account for object alignment in the heap */
934 emit_value (logbuffer, size);
935 emit_value (logbuffer, num);
936 for (i = 0; i < num; ++i) {
937 emit_value (logbuffer, offsets [i] - last_offset);
938 last_offset = offsets [i];
939 emit_obj (logbuffer, refs [i]);
942 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
946 static unsigned int hs_mode_ms = 0;
947 static unsigned int hs_mode_gc = 0;
948 static unsigned int hs_mode_ondemand = 0;
949 static unsigned int gc_count = 0;
950 static uint64_t last_hs_time = 0;
953 heap_walk (MonoProfiler *profiler)
957 LogBuffer *logbuffer;
960 logbuffer = ensure_logbuf (
961 EVENT_SIZE /* event */ +
962 LEB128_SIZE /* time */
964 now = current_time ();
965 if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
967 else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
969 else if (hs_mode_ondemand)
970 do_walk = heapshot_requested;
971 else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
976 heapshot_requested = 0;
977 emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
978 emit_time (logbuffer, now);
979 mono_gc_walk_heap (0, gc_reference, NULL);
980 logbuffer = ensure_logbuf (
981 EVENT_SIZE /* event */ +
982 LEB128_SIZE /* time */
984 now = current_time ();
985 emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
986 emit_time (logbuffer, now);
991 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
993 LogBuffer *logbuffer = ensure_logbuf (
994 EVENT_SIZE /* event */ +
995 LEB128_SIZE /* time */ +
996 LEB128_SIZE /* gc event */ +
997 LEB128_SIZE /* generation */
999 now = current_time ();
1000 ENTER_LOG (logbuffer, "gcevent");
1001 emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
1002 emit_time (logbuffer, now);
1003 emit_value (logbuffer, ev);
1004 emit_value (logbuffer, generation);
1005 /* to deal with nested gen1 after gen0 started */
1006 if (ev == MONO_GC_EVENT_START) {
1007 profiler->last_gc_gen_started = generation;
1008 if (generation == mono_gc_max_generation ())
1011 if (ev == MONO_GC_EVENT_PRE_START_WORLD)
1012 heap_walk (profiler);
1013 EXIT_LOG (logbuffer);
1014 if (ev == MONO_GC_EVENT_POST_START_WORLD)
1015 safe_send (profiler, logbuffer);
1016 //printf ("gc event %d for generation %d\n", ev, generation);
1020 gc_resize (MonoProfiler *profiler, int64_t new_size) {
1022 LogBuffer *logbuffer = ensure_logbuf (
1023 EVENT_SIZE /* event */ +
1024 LEB128_SIZE /* time */ +
1025 LEB128_SIZE /* new size */
1027 now = current_time ();
1028 ENTER_LOG (logbuffer, "gcresize");
1029 emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
1030 emit_time (logbuffer, now);
1031 emit_value (logbuffer, new_size);
1032 //printf ("gc resized to %lld\n", new_size);
1033 EXIT_LOG (logbuffer);
1036 #define MAX_FRAMES 32
1039 MonoMethod* methods [MAX_FRAMES];
1040 int32_t il_offsets [MAX_FRAMES];
1041 int32_t native_offsets [MAX_FRAMES];
1043 static int num_frames = MAX_FRAMES;
1046 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
1048 FrameData *frame = data;
1049 if (method && frame->count < num_frames) {
1050 frame->il_offsets [frame->count] = il_offset;
1051 frame->native_offsets [frame->count] = native_offset;
1052 frame->methods [frame->count++] = method;
1053 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
1055 return frame->count == num_frames;
1059 * a note about stack walks: they can cause more profiler events to fire,
1060 * so we need to make sure they don't happen after we started emitting an
1061 * event, hence the collect_bt/emit_bt split.
1064 collect_bt (FrameData *data)
1067 mono_stack_walk_no_il (walk_stack, data);
1071 emit_bt (MonoProfiler *prof, LogBuffer *logbuffer, FrameData *data)
1073 /* FIXME: this is actually tons of data and we should
1074 * just output it the first time and use an id the next
1076 if (data->count > num_frames)
1077 printf ("bad num frames: %d\n", data->count);
1078 emit_value (logbuffer, 0); /* flags */
1079 emit_value (logbuffer, data->count);
1080 //if (*p != data.count) {
1081 // 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);}
1082 while (data->count) {
1083 emit_method_as_ptr (prof, logbuffer, data->methods [--data->count]);
1088 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
1092 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_ALLOC_BT: 0;
1094 LogBuffer *logbuffer;
1095 len = mono_object_get_size (obj);
1096 /* account for object alignment in the heap */
1101 logbuffer = ensure_logbuf (
1102 EVENT_SIZE /* event */ +
1103 LEB128_SIZE /* time */ +
1104 LEB128_SIZE /* klass */ +
1105 LEB128_SIZE /* obj */ +
1106 LEB128_SIZE /* size */ +
1108 LEB128_SIZE /* flags */ +
1109 LEB128_SIZE /* count */ +
1111 LEB128_SIZE /* method */
1115 now = current_time ();
1116 ENTER_LOG (logbuffer, "gcalloc");
1117 emit_byte (logbuffer, do_bt | TYPE_ALLOC);
1118 emit_time (logbuffer, now);
1119 emit_ptr (logbuffer, klass);
1120 emit_obj (logbuffer, obj);
1121 emit_value (logbuffer, len);
1123 emit_bt (prof, logbuffer, &data);
1124 EXIT_LOG (logbuffer);
1125 if (logbuffer->next)
1126 safe_send (prof, logbuffer);
1127 process_requests (prof);
1128 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
1132 gc_moves (MonoProfiler *prof, void **objects, int num)
1136 LogBuffer *logbuffer = ensure_logbuf (
1137 EVENT_SIZE /* event */ +
1138 LEB128_SIZE /* time */ +
1139 LEB128_SIZE /* num */ +
1141 LEB128_SIZE /* object */
1144 now = current_time ();
1145 ENTER_LOG (logbuffer, "gcmove");
1146 emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
1147 emit_time (logbuffer, now);
1148 emit_value (logbuffer, num);
1149 for (i = 0; i < num; ++i)
1150 emit_obj (logbuffer, objects [i]);
1151 //printf ("gc moved %d objects\n", num/2);
1152 EXIT_LOG (logbuffer);
1156 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
1159 LogBuffer *logbuffer = ensure_logbuf (
1160 EVENT_SIZE /* event */ +
1161 LEB128_SIZE /* num */ +
1162 LEB128_SIZE /* collections */ +
1164 LEB128_SIZE /* object */ +
1165 LEB128_SIZE /* root type */ +
1166 LEB128_SIZE /* extra info */
1169 ENTER_LOG (logbuffer, "gcroots");
1170 emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
1171 emit_value (logbuffer, num);
1172 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
1173 for (i = 0; i < num; ++i) {
1174 emit_obj (logbuffer, objects [i]);
1175 emit_value (logbuffer, root_types [i]);
1176 emit_value (logbuffer, extra_info [i]);
1178 EXIT_LOG (logbuffer);
1182 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
1184 int do_bt = nocalls && InterlockedRead (&runtime_inited) && !notraces;
1191 LogBuffer *logbuffer = ensure_logbuf (
1192 EVENT_SIZE /* event */ +
1193 LEB128_SIZE /* time */ +
1194 LEB128_SIZE /* type */ +
1195 LEB128_SIZE /* handle */ +
1196 (op == MONO_PROFILER_GC_HANDLE_CREATED ? (
1197 LEB128_SIZE /* obj */
1200 LEB128_SIZE /* flags */ +
1201 LEB128_SIZE /* count */ +
1203 LEB128_SIZE /* method */
1208 now = current_time ();
1209 ENTER_LOG (logbuffer, "gchandle");
1211 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1212 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_CREATED_BT : TYPE_GC_HANDLE_CREATED) | TYPE_GC);
1213 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
1214 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_DESTROYED_BT : TYPE_GC_HANDLE_DESTROYED) | TYPE_GC);
1216 g_assert_not_reached ();
1218 emit_time (logbuffer, now);
1219 emit_value (logbuffer, type);
1220 emit_value (logbuffer, handle);
1222 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1223 emit_obj (logbuffer, obj);
1226 emit_bt (prof, logbuffer, &data);
1228 EXIT_LOG (logbuffer);
1229 process_requests (prof);
1233 push_nesting (char *p, MonoClass *klass)
1238 nesting = mono_class_get_nesting_type (klass);
1240 p = push_nesting (p, nesting);
1244 name = mono_class_get_name (klass);
1245 nspace = mono_class_get_namespace (klass);
1248 p += strlen (nspace);
1258 type_name (MonoClass *klass)
1262 push_nesting (buf, klass);
1263 p = malloc (strlen (buf) + 1);
1269 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
1274 LogBuffer *logbuffer;
1275 if (result != MONO_PROFILE_OK)
1277 name = mono_image_get_filename (image);
1278 nlen = strlen (name) + 1;
1279 logbuffer = ensure_logbuf (
1280 EVENT_SIZE /* event */ +
1281 LEB128_SIZE /* time */ +
1282 EVENT_SIZE /* type */ +
1283 LEB128_SIZE /* image */ +
1284 LEB128_SIZE /* flags */ +
1287 now = current_time ();
1288 ENTER_LOG (logbuffer, "image");
1289 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1290 emit_time (logbuffer, now);
1291 emit_byte (logbuffer, TYPE_IMAGE);
1292 emit_ptr (logbuffer, image);
1293 emit_value (logbuffer, 0); /* flags */
1294 memcpy (logbuffer->data, name, nlen);
1295 logbuffer->data += nlen;
1296 //printf ("loaded image %p (%s)\n", image, name);
1297 EXIT_LOG (logbuffer);
1298 if (logbuffer->next)
1299 safe_send (prof, logbuffer);
1300 process_requests (prof);
1304 image_unloaded (MonoProfiler *prof, MonoImage *image)
1306 const char *name = mono_image_get_filename (image);
1307 int nlen = strlen (name) + 1;
1308 LogBuffer *logbuffer = ensure_logbuf (
1309 EVENT_SIZE /* event */ +
1310 LEB128_SIZE /* time */ +
1311 EVENT_SIZE /* type */ +
1312 LEB128_SIZE /* image */ +
1313 LEB128_SIZE /* flags */ +
1316 uint64_t now = current_time ();
1318 ENTER_LOG (logbuffer, "image-unload");
1319 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1320 emit_time (logbuffer, now);
1321 emit_byte (logbuffer, TYPE_IMAGE);
1322 emit_ptr (logbuffer, image);
1323 emit_value (logbuffer, 0); /* flags */
1324 memcpy (logbuffer->data, name, nlen);
1325 logbuffer->data += nlen;
1326 EXIT_LOG (logbuffer);
1328 if (logbuffer->next)
1329 safe_send (prof, logbuffer);
1331 process_requests (prof);
1335 assembly_loaded (MonoProfiler *prof, MonoAssembly *assembly, int result)
1337 if (result != MONO_PROFILE_OK)
1340 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1341 int nlen = strlen (name) + 1;
1342 LogBuffer *logbuffer = ensure_logbuf (
1343 EVENT_SIZE /* event */ +
1344 LEB128_SIZE /* time */ +
1345 EVENT_SIZE /* type */ +
1346 LEB128_SIZE /* assembly */ +
1347 LEB128_SIZE /* flags */ +
1350 uint64_t now = current_time ();
1352 ENTER_LOG (logbuffer, "assembly-load");
1353 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1354 emit_time (logbuffer, now);
1355 emit_byte (logbuffer, TYPE_ASSEMBLY);
1356 emit_ptr (logbuffer, assembly);
1357 emit_value (logbuffer, 0); /* flags */
1358 memcpy (logbuffer->data, name, nlen);
1359 logbuffer->data += nlen;
1360 EXIT_LOG (logbuffer);
1364 if (logbuffer->next)
1365 safe_send (prof, logbuffer);
1367 process_requests (prof);
1371 assembly_unloaded (MonoProfiler *prof, MonoAssembly *assembly)
1373 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1374 int nlen = strlen (name) + 1;
1375 LogBuffer *logbuffer = ensure_logbuf (
1376 EVENT_SIZE /* event */ +
1377 LEB128_SIZE /* time */ +
1378 EVENT_SIZE /* type */ +
1379 LEB128_SIZE /* assembly */ +
1380 LEB128_SIZE /* flags */ +
1383 uint64_t now = current_time ();
1385 ENTER_LOG (logbuffer, "assembly-unload");
1386 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1387 emit_time (logbuffer, now);
1388 emit_byte (logbuffer, TYPE_ASSEMBLY);
1389 emit_ptr (logbuffer, assembly);
1390 emit_value (logbuffer, 0); /* flags */
1391 memcpy (logbuffer->data, name, nlen);
1392 logbuffer->data += nlen;
1393 EXIT_LOG (logbuffer);
1397 if (logbuffer->next)
1398 safe_send (prof, logbuffer);
1400 process_requests (prof);
1404 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1410 LogBuffer *logbuffer;
1411 if (result != MONO_PROFILE_OK)
1413 if (InterlockedRead (&runtime_inited))
1414 name = mono_type_get_name (mono_class_get_type (klass));
1416 name = type_name (klass);
1417 nlen = strlen (name) + 1;
1418 image = mono_class_get_image (klass);
1419 logbuffer = ensure_logbuf (
1420 EVENT_SIZE /* event */ +
1421 LEB128_SIZE /* time */ +
1422 EVENT_SIZE /* type */ +
1423 LEB128_SIZE /* klass */ +
1424 LEB128_SIZE /* image */ +
1425 LEB128_SIZE /* flags */ +
1428 now = current_time ();
1429 ENTER_LOG (logbuffer, "class");
1430 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1431 emit_time (logbuffer, now);
1432 emit_byte (logbuffer, TYPE_CLASS);
1433 emit_ptr (logbuffer, klass);
1434 emit_ptr (logbuffer, image);
1435 emit_value (logbuffer, 0); /* flags */
1436 memcpy (logbuffer->data, name, nlen);
1437 logbuffer->data += nlen;
1438 //printf ("loaded class %p (%s)\n", klass, name);
1443 EXIT_LOG (logbuffer);
1444 if (logbuffer->next)
1445 safe_send (prof, logbuffer);
1446 process_requests (prof);
1450 class_unloaded (MonoProfiler *prof, MonoClass *klass)
1454 if (InterlockedRead (&runtime_inited))
1455 name = mono_type_get_name (mono_class_get_type (klass));
1457 name = type_name (klass);
1459 int nlen = strlen (name) + 1;
1460 MonoImage *image = mono_class_get_image (klass);
1461 LogBuffer *logbuffer = ensure_logbuf (
1462 EVENT_SIZE /* event */ +
1463 LEB128_SIZE /* time */ +
1464 EVENT_SIZE /* type */ +
1465 LEB128_SIZE /* klass */ +
1466 LEB128_SIZE /* image */ +
1467 LEB128_SIZE /* flags */ +
1470 uint64_t now = current_time ();
1472 ENTER_LOG (logbuffer, "class-unload");
1473 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1474 emit_time (logbuffer, now);
1475 emit_byte (logbuffer, TYPE_CLASS);
1476 emit_ptr (logbuffer, klass);
1477 emit_ptr (logbuffer, image);
1478 emit_value (logbuffer, 0); /* flags */
1479 memcpy (logbuffer->data, name, nlen);
1480 logbuffer->data += nlen;
1481 EXIT_LOG (logbuffer);
1488 if (logbuffer->next)
1489 safe_send (prof, logbuffer);
1491 process_requests (prof);
1494 #ifndef DISABLE_HELPER_THREAD
1495 static void process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method);
1496 #endif /* DISABLE_HELPER_THREAD */
1499 method_enter (MonoProfiler *prof, MonoMethod *method)
1501 uint64_t now = current_time ();
1503 #ifndef DISABLE_HELPER_THREAD
1504 process_method_enter_coverage (prof, method);
1505 #endif /* DISABLE_HELPER_THREAD */
1507 LogBuffer *logbuffer = ensure_logbuf (
1508 EVENT_SIZE /* event */ +
1509 LEB128_SIZE /* time */ +
1510 LEB128_SIZE /* method */
1512 if (logbuffer->call_depth++ > max_call_depth)
1514 ENTER_LOG (logbuffer, "enter");
1515 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
1516 emit_time (logbuffer, now);
1517 emit_method (prof, logbuffer, method);
1518 EXIT_LOG (logbuffer);
1520 process_requests (prof);
1524 method_leave (MonoProfiler *prof, MonoMethod *method)
1527 LogBuffer *logbuffer = ensure_logbuf (
1528 EVENT_SIZE /* event */ +
1529 LEB128_SIZE /* time */ +
1530 LEB128_SIZE /* method */
1532 if (--logbuffer->call_depth > max_call_depth)
1534 now = current_time ();
1535 ENTER_LOG (logbuffer, "leave");
1536 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
1537 emit_time (logbuffer, now);
1538 emit_method (prof, logbuffer, method);
1539 EXIT_LOG (logbuffer);
1540 if (logbuffer->next)
1541 safe_send (prof, logbuffer);
1542 process_requests (prof);
1546 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
1549 LogBuffer *logbuffer;
1552 logbuffer = ensure_logbuf (
1553 EVENT_SIZE /* event */ +
1554 LEB128_SIZE /* time */ +
1555 LEB128_SIZE /* method */
1557 if (--logbuffer->call_depth > max_call_depth)
1559 now = current_time ();
1560 ENTER_LOG (logbuffer, "eleave");
1561 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1562 emit_time (logbuffer, now);
1563 emit_method (prof, logbuffer, method);
1564 EXIT_LOG (logbuffer);
1565 process_requests (prof);
1569 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji, int result)
1571 if (result != MONO_PROFILE_OK)
1574 register_method_local (prof, method, ji);
1576 process_requests (prof);
1580 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
1585 LogBuffer *logbuffer;
1586 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
1588 nlen = strlen (name) + 1;
1593 logbuffer = ensure_logbuf (
1594 EVENT_SIZE /* event */ +
1595 LEB128_SIZE /* time */ +
1596 LEB128_SIZE /* type */ +
1597 LEB128_SIZE /* buffer */ +
1598 LEB128_SIZE /* size */ +
1603 now = current_time ();
1604 ENTER_LOG (logbuffer, "code buffer");
1605 emit_byte (logbuffer, TYPE_JITHELPER | TYPE_RUNTIME);
1606 emit_time (logbuffer, now);
1607 emit_value (logbuffer, type);
1608 emit_ptr (logbuffer, buffer);
1609 emit_value (logbuffer, size);
1611 memcpy (logbuffer->data, name, nlen);
1612 logbuffer->data += nlen;
1614 EXIT_LOG (logbuffer);
1615 process_requests (prof);
1619 throw_exc (MonoProfiler *prof, MonoObject *object)
1621 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_EXCEPTION_BT: 0;
1624 LogBuffer *logbuffer;
1627 logbuffer = ensure_logbuf (
1628 EVENT_SIZE /* event */ +
1629 LEB128_SIZE /* time */ +
1630 LEB128_SIZE /* object */ +
1632 LEB128_SIZE /* flags */ +
1633 LEB128_SIZE /* count */ +
1635 LEB128_SIZE /* method */
1639 now = current_time ();
1640 ENTER_LOG (logbuffer, "throw");
1641 emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1642 emit_time (logbuffer, now);
1643 emit_obj (logbuffer, object);
1645 emit_bt (prof, logbuffer, &data);
1646 EXIT_LOG (logbuffer);
1647 process_requests (prof);
1651 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1654 LogBuffer *logbuffer = ensure_logbuf (
1655 EVENT_SIZE /* event */ +
1656 LEB128_SIZE /* time */ +
1657 LEB128_SIZE /* clause type */ +
1658 LEB128_SIZE /* clause num */ +
1659 LEB128_SIZE /* method */
1661 now = current_time ();
1662 ENTER_LOG (logbuffer, "clause");
1663 emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1664 emit_time (logbuffer, now);
1665 emit_value (logbuffer, clause_type);
1666 emit_value (logbuffer, clause_num);
1667 emit_method (prof, logbuffer, method);
1668 EXIT_LOG (logbuffer);
1670 process_requests (prof);
1674 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1676 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1679 LogBuffer *logbuffer;
1682 logbuffer = ensure_logbuf (
1683 EVENT_SIZE /* event */ +
1684 LEB128_SIZE /* time */ +
1685 LEB128_SIZE /* object */ +
1687 LEB128_SIZE /* flags */ +
1688 LEB128_SIZE /* count */ +
1690 LEB128_SIZE /* method */
1694 now = current_time ();
1695 ENTER_LOG (logbuffer, "monitor");
1696 emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1697 emit_time (logbuffer, now);
1698 emit_obj (logbuffer, object);
1700 emit_bt (profiler, logbuffer, &data);
1701 EXIT_LOG (logbuffer);
1702 process_requests (profiler);
1706 thread_start (MonoProfiler *prof, uintptr_t tid)
1708 //printf ("thread start %p\n", (void*)tid);
1711 LogBuffer *logbuffer = ensure_logbuf (
1712 EVENT_SIZE /* event */ +
1713 LEB128_SIZE /* time */ +
1714 EVENT_SIZE /* type */ +
1715 LEB128_SIZE /* tid */ +
1716 LEB128_SIZE /* flags */
1718 uint64_t now = current_time ();
1720 ENTER_LOG (logbuffer, "thread-start");
1721 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1722 emit_time (logbuffer, now);
1723 emit_byte (logbuffer, TYPE_THREAD);
1724 emit_ptr (logbuffer, (void*) tid);
1725 emit_value (logbuffer, 0); /* flags */
1726 EXIT_LOG (logbuffer);
1728 if (logbuffer->next)
1729 safe_send (prof, logbuffer);
1731 process_requests (prof);
1735 thread_end (MonoProfiler *prof, uintptr_t tid)
1737 if (TLS_GET (LogBuffer, tlsbuffer)) {
1738 LogBuffer *logbuffer = ensure_logbuf (
1739 EVENT_SIZE /* event */ +
1740 LEB128_SIZE /* time */ +
1741 EVENT_SIZE /* type */ +
1742 LEB128_SIZE /* tid */ +
1743 LEB128_SIZE /* flags */
1745 uint64_t now = current_time ();
1747 ENTER_LOG (logbuffer, "thread-end");
1748 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1749 emit_time (logbuffer, now);
1750 emit_byte (logbuffer, TYPE_THREAD);
1751 emit_ptr (logbuffer, (void*) tid);
1752 emit_value (logbuffer, 0); /* flags */
1753 EXIT_LOG (logbuffer);
1755 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), logbuffer);
1757 /* Don't process requests as the thread is detached from the runtime. */
1760 TLS_SET (tlsbuffer, NULL);
1761 TLS_SET (tlsmethodlist, NULL);
1765 domain_loaded (MonoProfiler *prof, MonoDomain *domain, int result)
1767 if (result != MONO_PROFILE_OK)
1770 LogBuffer *logbuffer = ensure_logbuf (
1771 EVENT_SIZE /* event */ +
1772 LEB128_SIZE /* time */ +
1773 EVENT_SIZE /* type */ +
1774 LEB128_SIZE /* domain id */ +
1775 LEB128_SIZE /* flags */
1777 uint64_t now = current_time ();
1779 ENTER_LOG (logbuffer, "domain-start");
1780 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1781 emit_time (logbuffer, now);
1782 emit_byte (logbuffer, TYPE_DOMAIN);
1783 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1784 emit_value (logbuffer, 0); /* flags */
1785 EXIT_LOG (logbuffer);
1787 if (logbuffer->next)
1788 safe_send (prof, logbuffer);
1790 process_requests (prof);
1794 domain_unloaded (MonoProfiler *prof, MonoDomain *domain)
1796 LogBuffer *logbuffer = ensure_logbuf (
1797 EVENT_SIZE /* event */ +
1798 LEB128_SIZE /* time */ +
1799 EVENT_SIZE /* type */ +
1800 LEB128_SIZE /* domain id */ +
1801 LEB128_SIZE /* flags */
1803 uint64_t now = current_time ();
1805 ENTER_LOG (logbuffer, "domain-end");
1806 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1807 emit_time (logbuffer, now);
1808 emit_byte (logbuffer, TYPE_DOMAIN);
1809 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1810 emit_value (logbuffer, 0); /* flags */
1811 EXIT_LOG (logbuffer);
1813 if (logbuffer->next)
1814 safe_send (prof, logbuffer);
1816 process_requests (prof);
1820 domain_name (MonoProfiler *prof, MonoDomain *domain, const char *name)
1822 int nlen = strlen (name) + 1;
1823 LogBuffer *logbuffer = ensure_logbuf (
1824 EVENT_SIZE /* event */ +
1825 LEB128_SIZE /* time */ +
1826 EVENT_SIZE /* type */ +
1827 LEB128_SIZE /* domain id */ +
1828 LEB128_SIZE /* flags */ +
1831 uint64_t now = current_time ();
1833 ENTER_LOG (logbuffer, "domain-name");
1834 emit_byte (logbuffer, TYPE_METADATA);
1835 emit_time (logbuffer, now);
1836 emit_byte (logbuffer, TYPE_DOMAIN);
1837 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1838 emit_value (logbuffer, 0); /* flags */
1839 memcpy (logbuffer->data, name, nlen);
1840 logbuffer->data += nlen;
1841 EXIT_LOG (logbuffer);
1843 if (logbuffer->next)
1844 safe_send (prof, logbuffer);
1846 process_requests (prof);
1850 context_loaded (MonoProfiler *prof, MonoAppContext *context)
1852 LogBuffer *logbuffer = ensure_logbuf (
1853 EVENT_SIZE /* event */ +
1854 LEB128_SIZE /* time */ +
1855 EVENT_SIZE /* type */ +
1856 LEB128_SIZE /* context id */ +
1857 LEB128_SIZE /* flags */ +
1858 LEB128_SIZE /* domain id */
1860 uint64_t now = current_time ();
1862 ENTER_LOG (logbuffer, "context-start");
1863 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1864 emit_time (logbuffer, now);
1865 emit_byte (logbuffer, TYPE_CONTEXT);
1866 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
1867 emit_value (logbuffer, 0); /* flags */
1868 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
1869 EXIT_LOG (logbuffer);
1871 if (logbuffer->next)
1872 safe_send (prof, logbuffer);
1874 process_requests (prof);
1878 context_unloaded (MonoProfiler *prof, MonoAppContext *context)
1880 LogBuffer *logbuffer = ensure_logbuf (
1881 EVENT_SIZE /* event */ +
1882 LEB128_SIZE /* time */ +
1883 EVENT_SIZE /* type */ +
1884 LEB128_SIZE /* context id */ +
1885 LEB128_SIZE /* flags */ +
1886 LEB128_SIZE /* domain id */
1888 uint64_t now = current_time ();
1890 ENTER_LOG (logbuffer, "context-end");
1891 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1892 emit_time (logbuffer, now);
1893 emit_byte (logbuffer, TYPE_CONTEXT);
1894 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
1895 emit_value (logbuffer, 0); /* flags */
1896 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
1897 EXIT_LOG (logbuffer);
1899 if (logbuffer->next)
1900 safe_send (prof, logbuffer);
1902 process_requests (prof);
1906 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1908 int len = strlen (name) + 1;
1910 LogBuffer *logbuffer;
1911 logbuffer = ensure_logbuf (
1912 EVENT_SIZE /* event */ +
1913 LEB128_SIZE /* time */ +
1914 EVENT_SIZE /* type */ +
1915 LEB128_SIZE /* tid */ +
1916 LEB128_SIZE /* flags */ +
1919 now = current_time ();
1920 ENTER_LOG (logbuffer, "tname");
1921 emit_byte (logbuffer, TYPE_METADATA);
1922 emit_time (logbuffer, now);
1923 emit_byte (logbuffer, TYPE_THREAD);
1924 emit_ptr (logbuffer, (void*)tid);
1925 emit_value (logbuffer, 0); /* flags */
1926 memcpy (logbuffer->data, name, len);
1927 logbuffer->data += len;
1928 EXIT_LOG (logbuffer);
1930 if (logbuffer->next)
1931 safe_send (prof, logbuffer);
1933 process_requests (prof);
1945 AsyncFrameInfo *data;
1949 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
1951 AsyncFrameData *frame = data;
1952 if (frame->count < num_frames) {
1953 frame->data [frame->count].method = method;
1954 frame->data [frame->count].domain = domain;
1955 frame->data [frame->count].base_address = base_address;
1956 frame->data [frame->count].offset = offset;
1957 // printf ("In %d at %p (dom %p) (native: %p)\n", frame->count, method, domain, base_address);
1960 return frame->count == num_frames;
1964 (type | frame count), tid, time, ip, [method, domain, base address, offset] * frames
1966 #define SAMPLE_EVENT_SIZE_IN_SLOTS(FRAMES) (4 + (FRAMES) * 4)
1969 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
1972 AsyncFrameInfo frames [num_frames];
1973 AsyncFrameData bt_data = { 0, &frames [0]};
1975 uintptr_t *data, *new_data, *old_data;
1981 now = current_time ();
1983 mono_stack_walk_async_safe (&async_walk_stack, context, &bt_data);
1985 elapsed = (now - profiler->startup_time) / 10000;
1989 snprintf (buf, sizeof (buf), "hit at %p in thread %p after %llu ms\n", ip, (void*)thread_id (), (unsigned long long int)elapsed/100);
1991 ign_res (write (2, buf, len));
1993 sbuf = profiler->stat_buffers;
1996 /* flush the buffer at 1 second intervals */
1997 if (sbuf->data > sbuf->buf && (elapsed - sbuf->buf [2]) > 100000) {
2000 /* overflow: 400 slots is a big enough number to reduce the chance of losing this event if many
2001 * threads hit this same spot at the same time
2003 if (timedout || (sbuf->data + 400 >= sbuf->data_end)) {
2004 StatBuffer *oldsb, *foundsb;
2005 sbuf = create_stat_buffer ();
2007 oldsb = profiler->stat_buffers;
2009 foundsb = InterlockedCompareExchangePointer ((void * volatile*)&profiler->stat_buffers, sbuf, oldsb);
2010 } while (foundsb != oldsb);
2012 ign_res (write (2, "overflow\n", 9));
2013 /* notify the helper thread */
2014 if (sbuf->next->next) {
2016 ign_res (write (profiler->pipes [1], &c, 1));
2018 ign_res (write (2, "notify\n", 7));
2022 old_data = sbuf->data;
2023 new_data = old_data + SAMPLE_EVENT_SIZE_IN_SLOTS (bt_data.count);
2024 data = InterlockedCompareExchangePointer ((void * volatile*)&sbuf->data, new_data, old_data);
2025 } while (data != old_data);
2026 if (old_data >= sbuf->data_end)
2027 return; /* lost event */
2028 old_data [0] = 1 | (sample_type << 16) | (bt_data.count << 8);
2029 old_data [1] = thread_id ();
2030 old_data [2] = elapsed;
2031 old_data [3] = (uintptr_t)ip;
2032 for (i = 0; i < bt_data.count; ++i) {
2033 old_data [4 + 4 * i + 0] = (uintptr_t)frames [i].method;
2034 old_data [4 + 4 * i + 1] = (uintptr_t)frames [i].domain;
2035 old_data [4 + 4 * i + 2] = (uintptr_t)frames [i].base_address;
2036 old_data [4 + 4 * i + 3] = (uintptr_t)frames [i].offset;
2040 static uintptr_t *code_pages = 0;
2041 static int num_code_pages = 0;
2042 static int size_code_pages = 0;
2043 #define CPAGE_SHIFT (9)
2044 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
2045 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
2046 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
2049 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
2052 uintptr_t start_pos;
2053 start_pos = (page >> CPAGE_SHIFT) % hsize;
2056 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
2058 } else if (!hash [i]) {
2065 } while (i != start_pos);
2066 /* should not happen */
2067 printf ("failed code page store\n");
2072 add_code_pointer (uintptr_t ip)
2075 if (num_code_pages * 2 >= size_code_pages) {
2077 uintptr_t old_size = size_code_pages;
2078 size_code_pages *= 2;
2079 if (size_code_pages == 0)
2080 size_code_pages = 16;
2081 n = calloc (sizeof (uintptr_t) * size_code_pages, 1);
2082 for (i = 0; i < old_size; ++i) {
2084 add_code_page (n, size_code_pages, code_pages [i]);
2090 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
2093 /* ELF code crashes on some systems. */
2094 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2097 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
2100 LogBuffer *logbuffer;
2102 len = strlen (filename) + 1;
2103 now = current_time ();
2104 logbuffer = ensure_logbuf (
2105 EVENT_SIZE /* event */ +
2106 LEB128_SIZE /* time */ +
2107 LEB128_SIZE /* load address */ +
2108 LEB128_SIZE /* offset */ +
2109 LEB128_SIZE /* size */ +
2110 nlen /* file name */
2112 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
2113 emit_time (logbuffer, now);
2114 emit_svalue (logbuffer, load_addr);
2115 emit_uvalue (logbuffer, offset);
2116 emit_uvalue (logbuffer, size);
2117 memcpy (logbuffer->data, filename, len);
2118 logbuffer->data += len;
2123 dump_usym (const char *name, uintptr_t value, uintptr_t size)
2125 LogBuffer *logbuffer;
2127 len = strlen (name) + 1;
2128 logbuffer = ensure_logbuf (
2129 EVENT_SIZE /* event */ +
2130 LEB128_SIZE /* value */ +
2131 LEB128_SIZE /* size */ +
2134 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
2135 emit_ptr (logbuffer, (void*)value);
2136 emit_value (logbuffer, size);
2137 memcpy (logbuffer->data, name, len);
2138 logbuffer->data += len;
2141 /* ELF code crashes on some systems. */
2142 //#if defined(ELFMAG0)
2145 #if SIZEOF_VOID_P == 4
2146 #define ELF_WSIZE 32
2148 #define ELF_WSIZE 64
2151 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
2152 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
2153 #define _ElfW_1(e,w,t) e##w##t
2157 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
2160 for (i = 0; i < num_symbols; ++i) {
2162 sym = strtab + symbols [i].st_name;
2163 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
2165 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
2166 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
2171 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
2178 ElfW(Shdr) *sheader;
2179 ElfW(Shdr) *shstrtabh;
2180 ElfW(Shdr) *symtabh = NULL;
2181 ElfW(Shdr) *strtabh = NULL;
2182 ElfW(Sym) *symbols = NULL;
2186 fd = open (filename, O_RDONLY);
2189 if (fstat (fd, &statb) != 0) {
2193 file_size = statb.st_size;
2194 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2196 if (data == MAP_FAILED)
2199 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2200 header->e_ident [EI_MAG1] != ELFMAG1 ||
2201 header->e_ident [EI_MAG2] != ELFMAG2 ||
2202 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2203 munmap (data, file_size);
2206 sheader = (void*)((char*)data + header->e_shoff);
2207 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
2208 strtab = (const char*)data + shstrtabh->sh_offset;
2209 for (i = 0; i < header->e_shnum; ++i) {
2210 //printf ("section header: %d\n", sheader->sh_type);
2211 if (sheader->sh_type == SHT_SYMTAB) {
2213 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
2214 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
2217 sheader = (void*)((char*)sheader + header->e_shentsize);
2219 if (!symtabh || !strtabh) {
2220 munmap (data, file_size);
2223 strtab = (const char*)data + strtabh->sh_offset;
2224 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
2225 symbols = (void*)((char*)data + symtabh->sh_offset);
2226 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
2227 munmap (data, file_size);
2232 /* ELF code crashes on some systems. */
2233 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2236 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
2238 MonoProfiler *prof = data;
2240 const char *filename;
2242 char *a = (void*)info->dlpi_addr;
2244 ElfW(Dyn) *dyn = NULL;
2245 ElfW(Sym) *symtab = NULL;
2246 ElfW(Word) *hash_table = NULL;
2247 ElfW(Ehdr) *header = NULL;
2248 const char* strtab = NULL;
2249 for (obj = prof->binary_objects; obj; obj = obj->next) {
2253 filename = info->dlpi_name;
2256 if (!info->dlpi_addr && !filename [0]) {
2257 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
2263 obj = calloc (sizeof (BinaryObject), 1);
2264 obj->addr = (void*)info->dlpi_addr;
2265 obj->name = pstrdup (filename);
2266 obj->next = prof->binary_objects;
2267 prof->binary_objects = obj;
2268 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
2270 for (i = 0; i < info->dlpi_phnum; ++i) {
2271 //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);
2272 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
2273 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2274 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2275 header->e_ident [EI_MAG1] != ELFMAG1 ||
2276 header->e_ident [EI_MAG2] != ELFMAG2 ||
2277 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2280 dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
2281 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
2282 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2285 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
2287 if (!info->dlpi_name || !info->dlpi_name[0])
2291 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
2292 if (dyn [i].d_tag == DT_SYMTAB) {
2293 if (symtab && do_debug)
2294 printf ("multiple symtabs: %d\n", i);
2295 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
2296 } else if (dyn [i].d_tag == DT_HASH) {
2297 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
2298 } else if (dyn [i].d_tag == DT_STRTAB) {
2299 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
2304 num_sym = hash_table [1];
2305 dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
2310 load_binaries (MonoProfiler *prof)
2312 dl_iterate_phdr (elf_dl_callback, prof);
2317 load_binaries (MonoProfiler *prof)
2324 symbol_for (uintptr_t code)
2327 void *ip = (void*)code;
2329 if (dladdr (ip, &di)) {
2331 return di.dli_sname;
2334 names = backtrace_symbols (&ip, 1);
2336 const char* p = names [0];
2347 dump_unmanaged_coderefs (MonoProfiler *prof)
2350 const char* last_symbol;
2351 uintptr_t addr, page_end;
2353 if (load_binaries (prof))
2355 for (i = 0; i < size_code_pages; ++i) {
2357 if (!code_pages [i] || code_pages [i] & 1)
2360 addr = CPAGE_ADDR (code_pages [i]);
2361 page_end = addr + CPAGE_SIZE;
2362 code_pages [i] |= 1;
2363 /* we dump the symbols for the whole page */
2364 for (; addr < page_end; addr += 16) {
2365 sym = symbol_for (addr);
2366 if (sym && sym == last_symbol)
2371 dump_usym (sym, addr, 0); /* let's not guess the size */
2372 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
2378 compare_sample_events (gconstpointer a, gconstpointer b)
2380 uintptr_t tid1 = (*(uintptr_t **) a) [1];
2381 uintptr_t tid2 = (*(uintptr_t **) b) [1];
2383 return tid1 > tid2 ? 1 :
2389 dump_sample_hits (MonoProfiler *prof, StatBuffer *sbuf)
2391 LogBuffer *logbuffer;
2395 dump_sample_hits (prof, sbuf->next);
2396 free_buffer (sbuf->next, sbuf->next->size);
2400 g_ptr_array_set_size (prof->sorted_sample_events, 0);
2402 for (uintptr_t *sample = sbuf->buf; sample < sbuf->data;) {
2403 int count = sample [0] & 0xff;
2404 int mbt_count = (sample [0] & 0xff00) >> 8;
2406 if (sample + SAMPLE_EVENT_SIZE_IN_SLOTS (mbt_count) > sbuf->data)
2409 g_ptr_array_add (prof->sorted_sample_events, sample);
2411 sample += count + 3 + 4 * mbt_count;
2414 g_ptr_array_sort (prof->sorted_sample_events, compare_sample_events);
2416 for (guint sidx = 0; sidx < prof->sorted_sample_events->len; sidx++) {
2417 uintptr_t *sample = g_ptr_array_index (prof->sorted_sample_events, sidx);
2418 int count = sample [0] & 0xff;
2419 int mbt_count = (sample [0] & 0xff00) >> 8;
2420 int type = sample [0] >> 16;
2421 uintptr_t *managed_sample_base = sample + count + 3;
2422 uintptr_t thread_id = sample [1];
2424 for (int i = 0; i < mbt_count; ++i) {
2425 MonoMethod *method = (MonoMethod*)managed_sample_base [i * 4 + 0];
2426 MonoDomain *domain = (MonoDomain*)managed_sample_base [i * 4 + 1];
2427 void *address = (void*)managed_sample_base [i * 4 + 2];
2430 MonoJitInfo *ji = mono_jit_info_table_find (domain, address);
2433 managed_sample_base [i * 4 + 0] = (uintptr_t)mono_jit_info_get_method (ji);
2437 logbuffer = ensure_logbuf (
2438 EVENT_SIZE /* event */ +
2439 LEB128_SIZE /* type */ +
2440 LEB128_SIZE /* time */ +
2441 LEB128_SIZE /* tid */ +
2442 LEB128_SIZE /* count */ +
2444 LEB128_SIZE /* ip */
2446 LEB128_SIZE /* managed count */ +
2448 LEB128_SIZE /* method */ +
2449 LEB128_SIZE /* il offset */ +
2450 LEB128_SIZE /* native offset */
2453 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
2454 emit_value (logbuffer, type);
2455 emit_uvalue (logbuffer, prof->startup_time + (uint64_t)sample [2] * (uint64_t)10000);
2456 emit_ptr (logbuffer, (void *) thread_id);
2457 emit_value (logbuffer, count);
2458 for (int i = 0; i < count; ++i) {
2459 emit_ptr (logbuffer, (void*)sample [i + 3]);
2460 add_code_pointer (sample [i + 3]);
2463 sample += count + 3;
2464 /* new in data version 6 */
2465 emit_uvalue (logbuffer, mbt_count);
2466 for (int i = 0; i < mbt_count; ++i) {
2467 MonoMethod *method = (MonoMethod *) sample [i * 4 + 0];
2468 uintptr_t native_offset = sample [i * 4 + 3];
2470 emit_method (prof, logbuffer, method);
2471 emit_svalue (logbuffer, 0); /* il offset will always be 0 from now on */
2472 emit_svalue (logbuffer, native_offset);
2476 dump_unmanaged_coderefs (prof);
2482 mono_cpu_count (void)
2485 #ifdef PLATFORM_ANDROID
2486 /* Android tries really hard to save power by powering off CPUs on SMP phones which
2487 * means the normal way to query cpu count returns a wrong value with userspace API.
2488 * Instead we use /sys entries to query the actual hardware CPU count.
2490 char buffer[8] = {'\0'};
2491 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
2492 /* Format of the /sys entry is a cpulist of indexes which in the case
2493 * of present is always of the form "0-(n-1)" when there is more than
2494 * 1 core, n being the number of CPU cores in the system. Otherwise
2495 * the value is simply 0
2497 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
2498 count = strtol (((char*)buffer) + 2, NULL, 10);
2504 #ifdef _SC_NPROCESSORS_ONLN
2505 count = sysconf (_SC_NPROCESSORS_ONLN);
2512 size_t len = sizeof (int);
2515 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
2522 GetSystemInfo (&info);
2523 return info.dwNumberOfProcessors;
2532 unsigned int prev_pos;
2534 struct perf_event_mmap_page *page_desc;
2537 static PerfData *perf_data = NULL;
2538 static int num_perf;
2539 #define PERF_PAGES_SHIFT 4
2540 static int num_pages = 1 << PERF_PAGES_SHIFT;
2541 static unsigned int mmap_mask;
2544 struct perf_event_header h;
2554 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
2556 attr->size = PERF_ATTR_SIZE_VER0;
2557 //printf ("perf attr size: %d\n", attr->size);
2558 #if defined(__x86_64__)
2559 return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
2560 #elif defined(__i386__)
2561 return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
2562 #elif defined(__arm__) || defined (__aarch64__)
2563 return syscall(/*__NR_perf_event_open*/ 364, attr, pid, cpu, group_fd, flags);
2570 setup_perf_map (PerfData *perf)
2572 perf->mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf->perf_fd, 0);
2573 if (perf->mmap_base == MAP_FAILED) {
2575 printf ("failed mmap\n");
2578 perf->page_desc = perf->mmap_base;
2580 printf ("mmap version: %d\n", perf->page_desc->version);
2585 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
2587 LogBuffer *logbuffer;
2590 void *end = (char*)buf + size;
2592 int pid = getpid ();
2598 if (pid != s->pid) {
2600 printf ("event for different pid: %d\n", s->pid);
2601 buf = (char*)buf + s->h.size;
2604 /*ip = (void*)s->ip;
2605 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
2606 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
2607 logbuffer = ensure_logbuf (
2608 EVENT_SIZE /* event */ +
2609 LEB128_SIZE /* type */ +
2610 LEB128_SIZE /* time */ +
2611 LEB128_SIZE /* tid */ +
2612 LEB128_SIZE /* count */ +
2614 LEB128_SIZE /* ip */
2616 LEB128_SIZE /* managed count */ +
2618 LEB128_SIZE /* method */ +
2619 LEB128_SIZE /* il offset */ +
2620 LEB128_SIZE /* native offset */
2623 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
2624 emit_value (logbuffer, sample_type);
2625 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
2627 * No useful thread ID to write here, since throughout the
2628 * profiler we use pthread_self () but the ID we get from
2629 * perf is the kernel's thread ID.
2631 emit_ptr (logbuffer, 0);
2632 emit_value (logbuffer, count);
2633 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
2634 add_code_pointer (s->ip);
2635 /* no support here yet for the managed backtrace */
2636 emit_uvalue (logbuffer, mbt_count);
2637 buf = (char*)buf + s->h.size;
2641 printf ("dumped %d samples\n", samples);
2642 dump_unmanaged_coderefs (prof);
2645 /* read events from the ring buffer */
2647 read_perf_mmap (MonoProfiler* prof, int cpu)
2649 PerfData *perf = perf_data + cpu;
2651 unsigned char *data = (unsigned char*)perf->mmap_base + getpagesize ();
2652 unsigned int head = perf->page_desc->data_head;
2656 mono_memory_read_barrier ();
2658 old = perf->prev_pos;
2662 printf ("lost mmap events: old: %d, head: %d\n", old, head);
2666 if ((old & mmap_mask) + size != (head & mmap_mask)) {
2667 buf = data + (old & mmap_mask);
2668 size = mmap_mask + 1 - (old & mmap_mask);
2670 /* size bytes at buf */
2672 printf ("found1 bytes of events: %d\n", size);
2673 dump_perf_hits (prof, buf, size);
2675 buf = data + (old & mmap_mask);
2677 /* size bytes at buf */
2679 printf ("found bytes of events: %d\n", size);
2680 dump_perf_hits (prof, buf, size);
2682 perf->prev_pos = old;
2683 perf->page_desc->data_tail = old;
2688 setup_perf_event_for_cpu (PerfData *perf, int cpu)
2690 struct perf_event_attr attr;
2691 memset (&attr, 0, sizeof (attr));
2692 attr.type = PERF_TYPE_HARDWARE;
2693 switch (sample_type) {
2694 case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2695 case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
2696 case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
2697 case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
2698 case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
2699 case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
2700 default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2702 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
2703 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2704 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
2707 attr.sample_freq = sample_freq;
2709 perf->perf_fd = perf_event_syscall (&attr, getpid (), cpu, -1, 0);
2711 printf ("perf fd: %d, freq: %d, event: %llu\n", perf->perf_fd, sample_freq, attr.config);
2712 if (perf->perf_fd < 0) {
2713 if (perf->perf_fd == -EPERM) {
2714 fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
2717 perror ("open perf event");
2721 if (!setup_perf_map (perf)) {
2722 close (perf->perf_fd);
2730 setup_perf_event (void)
2733 mmap_mask = num_pages * getpagesize () - 1;
2734 num_perf = mono_cpu_count ();
2735 perf_data = calloc (num_perf, sizeof (PerfData));
2736 for (i = 0; i < num_perf; ++i) {
2737 count += setup_perf_event_for_cpu (perf_data + i, i);
2746 #endif /* USE_PERF_EVENTS */
2748 #ifndef DISABLE_HELPER_THREAD
2750 typedef struct MonoCounterAgent {
2751 MonoCounter *counter;
2752 // MonoCounterAgent specific data :
2757 struct MonoCounterAgent *next;
2760 static MonoCounterAgent* counters;
2761 static gboolean counters_initialized = FALSE;
2762 static int counters_index = 1;
2763 static mono_mutex_t counters_mutex;
2766 counters_add_agent (MonoCounter *counter)
2768 MonoCounterAgent *agent, *item;
2770 if (!counters_initialized)
2773 mono_mutex_lock (&counters_mutex);
2775 for (agent = counters; agent; agent = agent->next) {
2776 if (agent->counter == counter) {
2777 agent->value_size = 0;
2779 free (agent->value);
2780 agent->value = NULL;
2782 mono_mutex_unlock (&counters_mutex);
2787 agent = malloc (sizeof (MonoCounterAgent));
2788 agent->counter = counter;
2789 agent->value = NULL;
2790 agent->value_size = 0;
2791 agent->index = counters_index++;
2804 mono_mutex_unlock (&counters_mutex);
2808 counters_init_foreach_callback (MonoCounter *counter, gpointer data)
2810 counters_add_agent (counter);
2815 counters_init (MonoProfiler *profiler)
2817 assert (!counters_initialized);
2819 mono_mutex_init (&counters_mutex);
2821 counters_initialized = TRUE;
2823 mono_counters_on_register (&counters_add_agent);
2824 mono_counters_foreach (counters_init_foreach_callback, NULL);
2828 counters_emit (MonoProfiler *profiler)
2830 MonoCounterAgent *agent;
2831 LogBuffer *logbuffer;
2834 EVENT_SIZE /* event */ +
2835 LEB128_SIZE /* len */
2838 if (!counters_initialized)
2841 mono_mutex_lock (&counters_mutex);
2843 for (agent = counters; agent; agent = agent->next) {
2848 LEB128_SIZE /* section */ +
2849 strlen (mono_counter_get_name (agent->counter)) + 1 /* name */ +
2850 LEB128_SIZE /* type */ +
2851 LEB128_SIZE /* unit */ +
2852 LEB128_SIZE /* variance */ +
2853 LEB128_SIZE /* index */
2860 mono_mutex_unlock (&counters_mutex);
2864 logbuffer = ensure_logbuf (size);
2866 ENTER_LOG (logbuffer, "counters");
2867 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
2868 emit_value (logbuffer, len);
2869 for (agent = counters; agent; agent = agent->next) {
2875 name = mono_counter_get_name (agent->counter);
2876 emit_value (logbuffer, mono_counter_get_section (agent->counter));
2877 emit_string (logbuffer, name, strlen (name) + 1);
2878 emit_value (logbuffer, mono_counter_get_type (agent->counter));
2879 emit_value (logbuffer, mono_counter_get_unit (agent->counter));
2880 emit_value (logbuffer, mono_counter_get_variance (agent->counter));
2881 emit_value (logbuffer, agent->index);
2885 EXIT_LOG (logbuffer);
2887 safe_send (profiler, logbuffer);
2889 mono_mutex_unlock (&counters_mutex);
2893 counters_sample (MonoProfiler *profiler, uint64_t timestamp)
2895 MonoCounterAgent *agent;
2896 MonoCounter *counter;
2897 LogBuffer *logbuffer;
2903 if (!counters_initialized)
2906 counters_emit (profiler);
2909 buffer = calloc (1, buffer_size);
2911 mono_mutex_lock (&counters_mutex);
2914 EVENT_SIZE /* event */ +
2915 LEB128_SIZE /* time */
2918 for (agent = counters; agent; agent = agent->next) {
2920 LEB128_SIZE /* index */ +
2921 LEB128_SIZE /* type */ +
2922 mono_counter_get_size (agent->counter) /* value */
2927 LEB128_SIZE /* stop marker */
2930 logbuffer = ensure_logbuf (size);
2932 ENTER_LOG (logbuffer, "counters");
2933 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
2934 emit_uvalue (logbuffer, timestamp);
2935 for (agent = counters; agent; agent = agent->next) {
2938 counter = agent->counter;
2940 size = mono_counter_get_size (counter);
2942 continue; // FIXME error
2943 } else if (size > buffer_size) {
2945 buffer = realloc (buffer, buffer_size);
2948 memset (buffer, 0, buffer_size);
2950 if (mono_counters_sample (counter, buffer, size) < 0)
2951 continue; // FIXME error
2953 type = mono_counter_get_type (counter);
2955 if (!agent->value) {
2956 agent->value = calloc (1, size);
2957 agent->value_size = size;
2959 if (type == MONO_COUNTER_STRING) {
2960 if (strcmp (agent->value, buffer) == 0)
2963 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
2968 emit_uvalue (logbuffer, agent->index);
2969 emit_uvalue (logbuffer, type);
2971 case MONO_COUNTER_INT:
2972 #if SIZEOF_VOID_P == 4
2973 case MONO_COUNTER_WORD:
2975 emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
2977 case MONO_COUNTER_UINT:
2978 emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
2980 case MONO_COUNTER_TIME_INTERVAL:
2981 case MONO_COUNTER_LONG:
2982 #if SIZEOF_VOID_P == 8
2983 case MONO_COUNTER_WORD:
2985 emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
2987 case MONO_COUNTER_ULONG:
2988 emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
2990 case MONO_COUNTER_DOUBLE:
2991 emit_double (logbuffer, *(double*)buffer);
2993 case MONO_COUNTER_STRING:
2995 emit_byte (logbuffer, 0);
2997 emit_byte (logbuffer, 1);
2998 emit_string (logbuffer, (char*)buffer, size);
3005 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
3006 agent->value = realloc (agent->value, size);
3007 agent->value_size = size;
3011 memcpy (agent->value, buffer, size);
3015 emit_value (logbuffer, 0);
3016 EXIT_LOG (logbuffer);
3018 safe_send (profiler, logbuffer);
3020 mono_mutex_unlock (&counters_mutex);
3023 typedef struct _PerfCounterAgent PerfCounterAgent;
3024 struct _PerfCounterAgent {
3025 PerfCounterAgent *next;
3027 char *category_name;
3036 static PerfCounterAgent *perfcounters = NULL;
3039 perfcounters_emit (MonoProfiler *profiler)
3041 PerfCounterAgent *pcagent;
3042 LogBuffer *logbuffer;
3045 EVENT_SIZE /* event */ +
3046 LEB128_SIZE /* len */
3049 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3050 if (pcagent->emitted)
3054 LEB128_SIZE /* section */ +
3055 strlen (pcagent->category_name) + 1 /* category name */ +
3056 strlen (pcagent->name) + 1 /* name */ +
3057 LEB128_SIZE /* type */ +
3058 LEB128_SIZE /* unit */ +
3059 LEB128_SIZE /* variance */ +
3060 LEB128_SIZE /* index */
3069 logbuffer = ensure_logbuf (size);
3071 ENTER_LOG (logbuffer, "perfcounters");
3072 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
3073 emit_value (logbuffer, len);
3074 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3075 if (pcagent->emitted)
3078 emit_value (logbuffer, MONO_COUNTER_PERFCOUNTERS);
3079 emit_string (logbuffer, pcagent->category_name, strlen (pcagent->category_name) + 1);
3080 emit_string (logbuffer, pcagent->name, strlen (pcagent->name) + 1);
3081 emit_value (logbuffer, MONO_COUNTER_LONG);
3082 emit_value (logbuffer, MONO_COUNTER_RAW);
3083 emit_value (logbuffer, MONO_COUNTER_VARIABLE);
3084 emit_value (logbuffer, pcagent->index);
3086 pcagent->emitted = 1;
3088 EXIT_LOG (logbuffer);
3090 safe_send (profiler, logbuffer);
3094 perfcounters_foreach (char *category_name, char *name, unsigned char type, gint64 value, gpointer user_data)
3096 PerfCounterAgent *pcagent;
3098 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3099 if (strcmp (pcagent->category_name, category_name) != 0 || strcmp (pcagent->name, name) != 0)
3101 if (pcagent->value == value)
3104 pcagent->value = value;
3105 pcagent->updated = 1;
3106 pcagent->deleted = 0;
3110 pcagent = g_new0 (PerfCounterAgent, 1);
3111 pcagent->next = perfcounters;
3112 pcagent->index = counters_index++;
3113 pcagent->category_name = g_strdup (category_name);
3114 pcagent->name = g_strdup (name);
3115 pcagent->type = (int) type;
3116 pcagent->value = value;
3117 pcagent->emitted = 0;
3118 pcagent->updated = 1;
3119 pcagent->deleted = 0;
3121 perfcounters = pcagent;
3127 perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp)
3129 PerfCounterAgent *pcagent;
3130 LogBuffer *logbuffer;
3133 if (!counters_initialized)
3136 mono_mutex_lock (&counters_mutex);
3138 /* mark all perfcounters as deleted, foreach will unmark them as necessary */
3139 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
3140 pcagent->deleted = 1;
3142 mono_perfcounter_foreach (perfcounters_foreach, perfcounters);
3144 perfcounters_emit (profiler);
3147 EVENT_SIZE /* event */ +
3148 LEB128_SIZE /* time */
3151 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3152 if (pcagent->deleted || !pcagent->updated)
3156 LEB128_SIZE /* index */ +
3157 LEB128_SIZE /* type */ +
3158 LEB128_SIZE /* value */
3163 LEB128_SIZE /* stop marker */
3166 logbuffer = ensure_logbuf (size);
3168 ENTER_LOG (logbuffer, "perfcounters");
3169 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
3170 emit_uvalue (logbuffer, timestamp);
3171 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3172 if (pcagent->deleted || !pcagent->updated)
3174 emit_uvalue (logbuffer, pcagent->index);
3175 emit_uvalue (logbuffer, MONO_COUNTER_LONG);
3176 emit_svalue (logbuffer, pcagent->value);
3178 pcagent->updated = 0;
3181 emit_value (logbuffer, 0);
3182 EXIT_LOG (logbuffer);
3184 safe_send (profiler, logbuffer);
3186 mono_mutex_unlock (&counters_mutex);
3190 counters_and_perfcounters_sample (MonoProfiler *prof)
3192 static uint64_t start = -1;
3196 start = current_time ();
3198 now = current_time ();
3199 counters_sample (prof, (now - start) / 1000/ 1000);
3200 perfcounters_sample (prof, (now - start) / 1000/ 1000);
3203 #define COVERAGE_DEBUG(x) if (debug_coverage) {x}
3204 static mono_mutex_t coverage_mutex;
3205 static MonoConcurrentHashTable *coverage_methods = NULL;
3206 static MonoConcurrentHashTable *coverage_assemblies = NULL;
3207 static MonoConcurrentHashTable *coverage_classes = NULL;
3209 static MonoConcurrentHashTable *filtered_classes = NULL;
3210 static MonoConcurrentHashTable *entered_methods = NULL;
3211 static MonoConcurrentHashTable *image_to_methods = NULL;
3212 static MonoConcurrentHashTable *suppressed_assemblies = NULL;
3213 static gboolean coverage_initialized = FALSE;
3215 static GPtrArray *coverage_data = NULL;
3216 static int previous_offset = 0;
3218 typedef struct _MethodNode MethodNode;
3219 struct _MethodNode {
3220 MonoLockFreeQueueNode node;
3224 typedef struct _CoverageEntry CoverageEntry;
3225 struct _CoverageEntry {
3234 free_coverage_entry (gpointer data, gpointer userdata)
3236 CoverageEntry *entry = (CoverageEntry *)data;
3237 g_free (entry->filename);
3242 obtain_coverage_for_method (MonoProfiler *prof, const MonoProfileCoverageEntry *entry)
3244 int offset = entry->iloffset - previous_offset;
3245 CoverageEntry *e = g_new (CoverageEntry, 1);
3247 previous_offset = entry->iloffset;
3250 e->counter = entry->counter;
3251 e->filename = g_strdup(entry->filename ? entry->filename : "");
3252 e->line = entry->line;
3253 e->column = entry->col;
3255 g_ptr_array_add (coverage_data, e);
3259 parse_generic_type_names(char *name)
3261 char *new_name, *ret;
3262 int within_generic_declaration = 0, generic_members = 1;
3264 if (name == NULL || *name == '\0')
3265 return g_strdup ("");
3267 if (!(ret = new_name = calloc (strlen (name) * 4 + 1, sizeof (char))))
3273 within_generic_declaration = 1;
3277 within_generic_declaration = 0;
3279 if (*(name - 1) != '<') {
3281 *new_name++ = '0' + generic_members;
3283 memcpy (new_name, "<>", 8);
3287 generic_members = 0;
3295 if (!within_generic_declaration)
3296 *new_name++ = *name;
3305 static int method_id;
3307 build_method_buffer (gpointer key, gpointer value, gpointer userdata)
3309 MonoMethod *method = (MonoMethod *)value;
3310 MonoProfiler *prof = (MonoProfiler *)userdata;
3314 const char *image_name, *method_name, *sig, *first_filename;
3315 LogBuffer *logbuffer;
3318 previous_offset = 0;
3319 coverage_data = g_ptr_array_new ();
3321 mono_profiler_coverage_get (prof, method, obtain_coverage_for_method);
3323 klass = mono_method_get_class (method);
3324 image = mono_class_get_image (klass);
3325 image_name = mono_image_get_name (image);
3327 sig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3328 class_name = parse_generic_type_names (mono_type_get_name (mono_class_get_type (klass)));
3329 method_name = mono_method_get_name (method);
3331 if (coverage_data->len != 0) {
3332 CoverageEntry *entry = coverage_data->pdata[0];
3333 first_filename = entry->filename ? entry->filename : "";
3335 first_filename = "";
3337 image_name = image_name ? image_name : "";
3338 sig = sig ? sig : "";
3339 method_name = method_name ? method_name : "";
3341 logbuffer = ensure_logbuf (
3342 EVENT_SIZE /* event */ +
3343 strlen (image_name) + 1 /* image name */ +
3344 strlen (class_name) + 1 /* class name */ +
3345 strlen (method_name) + 1 /* method name */ +
3346 strlen (sig) + 1 /* signature */ +
3347 strlen (first_filename) + 1 /* first file name */ +
3348 LEB128_SIZE /* token */ +
3349 LEB128_SIZE /* method id */ +
3350 LEB128_SIZE /* entries */
3352 ENTER_LOG (logbuffer, "coverage-methods");
3354 emit_byte (logbuffer, TYPE_COVERAGE_METHOD | TYPE_COVERAGE);
3355 emit_string (logbuffer, image_name, strlen (image_name) + 1);
3356 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3357 emit_string (logbuffer, method_name, strlen (method_name) + 1);
3358 emit_string (logbuffer, sig, strlen (sig) + 1);
3359 emit_string (logbuffer, first_filename, strlen (first_filename) + 1);
3361 emit_uvalue (logbuffer, mono_method_get_token (method));
3362 emit_uvalue (logbuffer, method_id);
3363 emit_value (logbuffer, coverage_data->len);
3365 EXIT_LOG (logbuffer);
3366 safe_send (prof, logbuffer);
3368 for (i = 0; i < coverage_data->len; i++) {
3369 CoverageEntry *entry = coverage_data->pdata[i];
3371 logbuffer = ensure_logbuf (
3372 EVENT_SIZE /* event */ +
3373 LEB128_SIZE /* method id */ +
3374 LEB128_SIZE /* offset */ +
3375 LEB128_SIZE /* counter */ +
3376 LEB128_SIZE /* line */ +
3377 LEB128_SIZE /* column */
3379 ENTER_LOG (logbuffer, "coverage-statement");
3381 emit_byte (logbuffer, TYPE_COVERAGE_STATEMENT | TYPE_COVERAGE);
3382 emit_uvalue (logbuffer, method_id);
3383 emit_uvalue (logbuffer, entry->offset);
3384 emit_uvalue (logbuffer, entry->counter);
3385 emit_uvalue (logbuffer, entry->line);
3386 emit_uvalue (logbuffer, entry->column);
3388 EXIT_LOG (logbuffer);
3389 safe_send (prof, logbuffer);
3394 g_free (class_name);
3396 g_ptr_array_foreach (coverage_data, free_coverage_entry, NULL);
3397 g_ptr_array_free (coverage_data, TRUE);
3398 coverage_data = NULL;
3401 /* This empties the queue */
3403 count_queue (MonoLockFreeQueue *queue)
3405 MonoLockFreeQueueNode *node;
3408 while ((node = mono_lock_free_queue_dequeue (queue))) {
3410 mono_lock_free_queue_node_free (node);
3417 build_class_buffer (gpointer key, gpointer value, gpointer userdata)
3419 MonoClass *klass = (MonoClass *)key;
3420 MonoLockFreeQueue *class_methods = (MonoLockFreeQueue *)value;
3421 MonoProfiler *prof = (MonoProfiler *)userdata;
3424 const char *assembly_name;
3425 int number_of_methods, partially_covered;
3426 guint fully_covered;
3427 LogBuffer *logbuffer;
3429 image = mono_class_get_image (klass);
3430 assembly_name = mono_image_get_name (image);
3431 class_name = mono_type_get_name (mono_class_get_type (klass));
3433 assembly_name = assembly_name ? assembly_name : "";
3434 number_of_methods = mono_class_num_methods (klass);
3435 fully_covered = count_queue (class_methods);
3436 /* We don't handle partial covered yet */
3437 partially_covered = 0;
3439 logbuffer = ensure_logbuf (
3440 EVENT_SIZE /* event */ +
3441 strlen (assembly_name) + 1 /* assembly name */ +
3442 strlen (class_name) + 1 /* class name */ +
3443 LEB128_SIZE /* no. methods */ +
3444 LEB128_SIZE /* fully covered */ +
3445 LEB128_SIZE /* partially covered */
3448 ENTER_LOG (logbuffer, "coverage-class");
3449 emit_byte (logbuffer, TYPE_COVERAGE_CLASS | TYPE_COVERAGE);
3450 emit_string (logbuffer, assembly_name, strlen (assembly_name) + 1);
3451 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3452 emit_uvalue (logbuffer, number_of_methods);
3453 emit_uvalue (logbuffer, fully_covered);
3454 emit_uvalue (logbuffer, partially_covered);
3455 EXIT_LOG (logbuffer);
3457 safe_send (prof, logbuffer);
3459 g_free (class_name);
3463 get_coverage_for_image (MonoImage *image, int *number_of_methods, guint *fully_covered, int *partially_covered)
3465 MonoLockFreeQueue *image_methods = mono_conc_hashtable_lookup (image_to_methods, image);
3467 *number_of_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
3469 *fully_covered = count_queue (image_methods);
3473 // FIXME: We don't handle partially covered yet.
3474 *partially_covered = 0;
3478 build_assembly_buffer (gpointer key, gpointer value, gpointer userdata)
3480 MonoAssembly *assembly = (MonoAssembly *)value;
3481 MonoProfiler *prof = (MonoProfiler *)userdata;
3482 MonoImage *image = mono_assembly_get_image (assembly);
3483 LogBuffer *logbuffer;
3484 const char *name, *guid, *filename;
3485 int number_of_methods = 0, partially_covered = 0;
3486 guint fully_covered = 0;
3488 name = mono_image_get_name (image);
3489 guid = mono_image_get_guid (image);
3490 filename = mono_image_get_filename (image);
3492 name = name ? name : "";
3493 guid = guid ? guid : "";
3494 filename = filename ? filename : "";
3496 get_coverage_for_image (image, &number_of_methods, &fully_covered, &partially_covered);
3498 logbuffer = ensure_logbuf (
3499 EVENT_SIZE /* event */ +
3500 strlen (name) + 1 /* name */ +
3501 strlen (guid) + 1 /* guid */ +
3502 strlen (filename) + 1 /* file name */ +
3503 LEB128_SIZE /* no. methods */ +
3504 LEB128_SIZE /* fully covered */ +
3505 LEB128_SIZE /* partially covered */
3508 ENTER_LOG (logbuffer, "coverage-assemblies");
3509 emit_byte (logbuffer, TYPE_COVERAGE_ASSEMBLY | TYPE_COVERAGE);
3510 emit_string (logbuffer, name, strlen (name) + 1);
3511 emit_string (logbuffer, guid, strlen (guid) + 1);
3512 emit_string (logbuffer, filename, strlen (filename) + 1);
3513 emit_uvalue (logbuffer, number_of_methods);
3514 emit_uvalue (logbuffer, fully_covered);
3515 emit_uvalue (logbuffer, partially_covered);
3516 EXIT_LOG (logbuffer);
3518 safe_send (prof, logbuffer);
3522 dump_coverage (MonoProfiler *prof)
3524 if (!coverage_initialized)
3527 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Started dump\n");)
3530 mono_mutex_lock (&coverage_mutex);
3531 mono_conc_hashtable_foreach (coverage_assemblies, build_assembly_buffer, prof);
3532 mono_conc_hashtable_foreach (coverage_classes, build_class_buffer, prof);
3533 mono_conc_hashtable_foreach (coverage_methods, build_method_buffer, prof);
3534 mono_mutex_unlock (&coverage_mutex);
3536 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Finished dump\n");)
3540 process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method)
3545 if (!coverage_initialized)
3548 klass = mono_method_get_class (method);
3549 image = mono_class_get_image (klass);
3551 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)))
3554 mono_mutex_lock (&coverage_mutex);
3555 mono_conc_hashtable_insert (entered_methods, method, method);
3556 mono_mutex_unlock (&coverage_mutex);
3559 static MonoLockFreeQueueNode *
3560 create_method_node (MonoMethod *method)
3562 MethodNode *node = g_malloc (sizeof (MethodNode));
3563 mono_lock_free_queue_node_init ((MonoLockFreeQueueNode *) node, FALSE);
3564 node->method = method;
3566 return (MonoLockFreeQueueNode *) node;
3570 coverage_filter (MonoProfiler *prof, MonoMethod *method)
3574 MonoAssembly *assembly;
3575 MonoMethodHeader *header;
3576 guint32 iflags, flags, code_size;
3577 char *fqn, *classname;
3578 gboolean has_positive, found;
3579 MonoLockFreeQueue *image_methods, *class_methods;
3580 MonoLockFreeQueueNode *node;
3582 if (!coverage_initialized)
3585 COVERAGE_DEBUG(fprintf (stderr, "Coverage filter for %s\n", mono_method_get_name (method));)
3587 flags = mono_method_get_flags (method, &iflags);
3588 if ((iflags & 0x1000 /*METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL*/) ||
3589 (flags & 0x2000 /*METHOD_ATTRIBUTE_PINVOKE_IMPL*/)) {
3590 COVERAGE_DEBUG(fprintf (stderr, " Internal call or pinvoke - ignoring\n");)
3594 // Don't need to do anything else if we're already tracking this method
3595 if (mono_conc_hashtable_lookup (coverage_methods, method)) {
3596 COVERAGE_DEBUG(fprintf (stderr, " Already tracking\n");)
3600 klass = mono_method_get_class (method);
3601 image = mono_class_get_image (klass);
3603 // Don't handle coverage for the core assemblies
3604 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)) != NULL)
3607 if (prof->coverage_filters) {
3608 /* Check already filtered classes first */
3609 if (mono_conc_hashtable_lookup (filtered_classes, klass)) {
3610 COVERAGE_DEBUG(fprintf (stderr, " Already filtered\n");)
3614 classname = mono_type_get_name (mono_class_get_type (klass));
3616 fqn = g_strdup_printf ("[%s]%s", mono_image_get_name (image), classname);
3618 COVERAGE_DEBUG(fprintf (stderr, " Looking for %s in filter\n", fqn);)
3619 // Check positive filters first
3620 has_positive = FALSE;
3622 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3623 char *filter = g_ptr_array_index (prof->coverage_filters, i);
3625 if (filter [0] == '+') {
3626 filter = &filter [1];
3628 COVERAGE_DEBUG(fprintf (stderr, " Checking against +%s ...", filter);)
3630 if (strstr (fqn, filter) != NULL) {
3631 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3634 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3636 has_positive = TRUE;
3640 if (has_positive && !found) {
3641 COVERAGE_DEBUG(fprintf (stderr, " Positive match was not found\n");)
3643 mono_mutex_lock (&coverage_mutex);
3644 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3645 mono_mutex_unlock (&coverage_mutex);
3652 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3653 // FIXME: Is substring search sufficient?
3654 char *filter = g_ptr_array_index (prof->coverage_filters, i);
3655 if (filter [0] == '+')
3659 filter = &filter [1];
3660 COVERAGE_DEBUG(fprintf (stderr, " Checking against -%s ...", filter);)
3662 if (strstr (fqn, filter) != NULL) {
3663 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3665 mono_mutex_lock (&coverage_mutex);
3666 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3667 mono_mutex_unlock (&coverage_mutex);
3673 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3681 COVERAGE_DEBUG(fprintf (stderr, " Handling coverage for %s\n", mono_method_get_name (method));)
3682 header = mono_method_get_header (method);
3684 mono_method_header_get_code (header, &code_size, NULL);
3686 assembly = mono_image_get_assembly (image);
3688 mono_mutex_lock (&coverage_mutex);
3689 mono_conc_hashtable_insert (coverage_methods, method, method);
3690 mono_conc_hashtable_insert (coverage_assemblies, assembly, assembly);
3691 mono_mutex_unlock (&coverage_mutex);
3693 image_methods = mono_conc_hashtable_lookup (image_to_methods, image);
3695 if (image_methods == NULL) {
3696 image_methods = g_malloc (sizeof (MonoLockFreeQueue));
3697 mono_lock_free_queue_init (image_methods);
3698 mono_mutex_lock (&coverage_mutex);
3699 mono_conc_hashtable_insert (image_to_methods, image, image_methods);
3700 mono_mutex_unlock (&coverage_mutex);
3703 node = create_method_node (method);
3704 mono_lock_free_queue_enqueue (image_methods, node);
3706 class_methods = mono_conc_hashtable_lookup (coverage_classes, klass);
3708 if (class_methods == NULL) {
3709 class_methods = g_malloc (sizeof (MonoLockFreeQueue));
3710 mono_lock_free_queue_init (class_methods);
3711 mono_mutex_lock (&coverage_mutex);
3712 mono_conc_hashtable_insert (coverage_classes, klass, class_methods);
3713 mono_mutex_unlock (&coverage_mutex);
3716 node = create_method_node (method);
3717 mono_lock_free_queue_enqueue (class_methods, node);
3722 #define LINE_BUFFER_SIZE 4096
3723 /* Max file limit of 128KB */
3724 #define MAX_FILE_SIZE 128 * 1024
3726 get_file_content (FILE *stream)
3731 int res, offset = 0;
3733 res = fseek (stream, 0, SEEK_END);
3737 filesize = ftell (stream);
3741 res = fseek (stream, 0, SEEK_SET);
3745 if (filesize > MAX_FILE_SIZE)
3748 buffer = g_malloc ((filesize + 1) * sizeof (char));
3749 while ((bytes_read = fread (buffer + offset, 1, LINE_BUFFER_SIZE, stream)) > 0)
3750 offset += bytes_read;
3752 /* NULL terminate our buffer */
3753 buffer[filesize] = '\0';
3758 get_next_line (char *contents, char **next_start)
3762 if (p == NULL || *p == '\0') {
3767 while (*p != '\n' && *p != '\0')
3772 *next_start = p + 1;
3780 init_suppressed_assemblies (void)
3786 suppressed_assemblies = mono_conc_hashtable_new (g_str_hash, g_str_equal);
3787 sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
3788 if (sa_file == NULL)
3791 /* Don't need to free @content as it is referred to by the lines stored in @suppressed_assemblies */
3792 content = get_file_content (sa_file);
3793 if (content == NULL) {
3794 g_error ("mono-profiler-log.suppression is greater than 128kb - aborting\n");
3797 while ((line = get_next_line (content, &content))) {
3798 line = g_strchomp (g_strchug (line));
3799 /* No locking needed as we're doing initialization */
3800 mono_conc_hashtable_insert (suppressed_assemblies, line, line);
3806 #endif /* DISABLE_HELPER_THREAD */
3809 coverage_init (MonoProfiler *prof)
3811 #ifndef DISABLE_HELPER_THREAD
3812 assert (!coverage_initialized);
3814 COVERAGE_DEBUG(fprintf (stderr, "Coverage initialized\n");)
3816 mono_mutex_init (&coverage_mutex);
3817 coverage_methods = mono_conc_hashtable_new (NULL, NULL);
3818 coverage_assemblies = mono_conc_hashtable_new (NULL, NULL);
3819 coverage_classes = mono_conc_hashtable_new (NULL, NULL);
3820 filtered_classes = mono_conc_hashtable_new (NULL, NULL);
3821 entered_methods = mono_conc_hashtable_new (NULL, NULL);
3822 image_to_methods = mono_conc_hashtable_new (NULL, NULL);
3823 init_suppressed_assemblies ();
3825 coverage_initialized = TRUE;
3826 #endif /* DISABLE_HELPER_THREAD */
3830 log_shutdown (MonoProfiler *prof)
3835 #ifndef DISABLE_HELPER_THREAD
3836 counters_and_perfcounters_sample (prof);
3838 dump_coverage (prof);
3840 if (prof->command_port) {
3842 ign_res (write (prof->pipes [1], &c, 1));
3843 pthread_join (prof->helper_thread, &res);
3849 for (i = 0; i < num_perf; ++i)
3850 read_perf_mmap (prof, i);
3854 g_ptr_array_free (prof->sorted_sample_events, TRUE);
3856 if (TLS_GET (LogBuffer, tlsbuffer))
3857 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
3859 TLS_SET (tlsbuffer, NULL);
3860 TLS_SET (tlsmethodlist, NULL);
3862 InterlockedWrite (&prof->run_writer_thread, 0);
3863 pthread_join (prof->writer_thread, &res);
3865 #if defined (HAVE_SYS_ZLIB)
3867 gzclose (prof->gzfile);
3869 if (prof->pipe_output)
3870 pclose (prof->file);
3872 fclose (prof->file);
3874 mono_conc_hashtable_destroy (prof->method_table);
3875 mono_mutex_destroy (&prof->method_table_mutex);
3877 if (coverage_initialized) {
3878 mono_conc_hashtable_destroy (coverage_methods);
3879 mono_conc_hashtable_destroy (coverage_assemblies);
3880 mono_conc_hashtable_destroy (coverage_classes);
3881 mono_conc_hashtable_destroy (filtered_classes);
3883 mono_conc_hashtable_destroy (entered_methods);
3884 mono_conc_hashtable_destroy (image_to_methods);
3885 mono_conc_hashtable_destroy (suppressed_assemblies);
3886 mono_mutex_destroy (&coverage_mutex);
3893 new_filename (const char* filename)
3895 time_t t = time (NULL);
3896 int pid = process_id ();
3901 int count_dates = 0;
3905 for (p = filename; *p; p++) {
3916 if (!count_dates && !count_pids)
3917 return pstrdup (filename);
3918 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
3920 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
3921 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
3922 s_date = strlen (time_buf);
3923 s_pid = strlen (pid_buf);
3924 d = res = malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
3925 for (p = filename; *p; p++) {
3932 strcpy (d, time_buf);
3935 } else if (*p == 'p') {
3936 strcpy (d, pid_buf);
3939 } else if (*p == '%') {
3951 //this is exposed by the JIT, but it's not meant to be a supported API for now.
3952 extern void mono_threads_attach_tools_thread (void);
3954 #ifndef DISABLE_HELPER_THREAD
3957 helper_thread (void* arg)
3959 MonoProfiler* prof = arg;
3963 MonoThread *thread = NULL;
3965 mono_threads_attach_tools_thread ();
3966 //fprintf (stderr, "Server listening\n");
3967 command_socket = -1;
3973 FD_SET (prof->server_socket, &rfds);
3974 max_fd = prof->server_socket;
3975 FD_SET (prof->pipes [0], &rfds);
3976 if (max_fd < prof->pipes [0])
3977 max_fd = prof->pipes [0];
3978 if (command_socket >= 0) {
3979 FD_SET (command_socket, &rfds);
3980 if (max_fd < command_socket)
3981 max_fd = command_socket;
3986 for ( i = 0; i < num_perf; ++i) {
3987 if (perf_data [i].perf_fd < 0)
3989 FD_SET (perf_data [i].perf_fd, &rfds);
3990 if (max_fd < perf_data [i].perf_fd)
3991 max_fd = perf_data [i].perf_fd;
3996 counters_and_perfcounters_sample (prof);
4000 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
4006 g_warning ("Error in proflog server: %s", strerror (errno));
4010 if (FD_ISSET (prof->pipes [0], &rfds)) {
4012 int r = read (prof->pipes [0], &c, 1);
4013 if (r == 1 && c == 0) {
4014 StatBuffer *sbufbase = prof->stat_buffers;
4016 if (!sbufbase->next)
4018 sbuf = sbufbase->next->next;
4019 sbufbase->next->next = NULL;
4021 fprintf (stderr, "stat buffer dump\n");
4023 dump_sample_hits (prof, sbuf);
4024 free_buffer (sbuf, sbuf->size);
4025 safe_send (prof, ensure_logbuf (0));
4029 /* time to shut down */
4030 dump_sample_hits (prof, prof->stat_buffers);
4032 mono_thread_detach (thread);
4034 fprintf (stderr, "helper shutdown\n");
4038 for ( i = 0; i < num_perf; ++i) {
4039 if (perf_data [i].perf_fd < 0)
4041 if (FD_ISSET (perf_data [i].perf_fd, &rfds))
4042 read_perf_mmap (prof, i);
4046 safe_send (prof, ensure_logbuf (0));
4052 for ( i = 0; i < num_perf; ++i) {
4053 if (perf_data [i].perf_fd < 0)
4055 if (FD_ISSET (perf_data [i].perf_fd, &rfds)) {
4056 read_perf_mmap (prof, i);
4057 safe_send (prof, ensure_logbuf (0));
4062 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
4063 len = read (command_socket, buf, sizeof (buf) - 1);
4067 close (command_socket);
4068 command_socket = -1;
4072 if (strcmp (buf, "heapshot\n") == 0) {
4073 heapshot_requested = 1;
4074 //fprintf (stderr, "perform heapshot\n");
4075 if (InterlockedRead (&runtime_inited) && !thread) {
4076 thread = mono_thread_attach (mono_get_root_domain ());
4077 /*fprintf (stderr, "attached\n");*/
4080 process_requests (prof);
4081 mono_thread_detach (thread);
4087 if (!FD_ISSET (prof->server_socket, &rfds)) {
4090 command_socket = accept (prof->server_socket, NULL, NULL);
4091 if (command_socket < 0)
4093 //fprintf (stderr, "Accepted connection\n");
4099 start_helper_thread (MonoProfiler* prof)
4101 struct sockaddr_in server_address;
4104 if (pipe (prof->pipes) < 0) {
4105 fprintf (stderr, "Cannot create pipe\n");
4108 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
4109 if (prof->server_socket < 0) {
4110 fprintf (stderr, "Cannot create server socket\n");
4113 memset (&server_address, 0, sizeof (server_address));
4114 server_address.sin_family = AF_INET;
4115 server_address.sin_addr.s_addr = INADDR_ANY;
4116 server_address.sin_port = htons (prof->command_port);
4117 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
4118 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
4119 close (prof->server_socket);
4122 if (listen (prof->server_socket, 1) < 0) {
4123 fprintf (stderr, "Cannot listen server socket\n");
4124 close (prof->server_socket);
4127 slen = sizeof (server_address);
4128 if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
4129 prof->command_port = ntohs (server_address.sin_port);
4130 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
4133 r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
4135 close (prof->server_socket);
4143 writer_thread (void *arg)
4145 MonoProfiler *prof = arg;
4147 mono_threads_attach_tools_thread ();
4151 while (InterlockedRead (&prof->run_writer_thread)) {
4152 WriterQueueEntry *entry;
4154 while ((entry = (WriterQueueEntry *) mono_lock_free_queue_dequeue (&prof->writer_queue))) {
4155 LogBuffer *method_buffer = NULL;
4156 gboolean new_methods = FALSE;
4158 if (entry->methods->len)
4159 method_buffer = create_buffer ();
4162 * Encode the method events in a temporary log buffer that we
4163 * flush to disk before the main buffer, ensuring that all
4164 * methods have metadata emitted before they're referenced.
4166 for (guint i = 0; i < entry->methods->len; i++) {
4167 MethodInfo *info = g_ptr_array_index (entry->methods, i);
4169 if (mono_conc_hashtable_lookup (prof->method_table, info->method))
4175 * Other threads use this hash table to get a general
4176 * idea of whether a method has already been emitted to
4177 * the stream. Due to the way we add to this table, it
4178 * can easily happen that multiple threads queue up the
4179 * same methods, but that's OK since eventually all
4180 * methods will be in this table and the thread-local
4181 * method lists will just be empty for the rest of the
4184 mono_mutex_lock (&prof->method_table_mutex);
4185 mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
4186 mono_mutex_unlock (&prof->method_table_mutex);
4188 char *name = mono_method_full_name (info->method, 1);
4189 int nlen = strlen (name) + 1;
4190 void *cstart = info->ji ? mono_jit_info_get_code_start (info->ji) : NULL;
4191 int csize = info->ji ? mono_jit_info_get_code_size (info->ji) : 0;
4193 method_buffer = ensure_logbuf_inner (method_buffer,
4194 EVENT_SIZE /* event */ +
4195 LEB128_SIZE /* time */ +
4196 LEB128_SIZE /* method */ +
4197 LEB128_SIZE /* start */ +
4198 LEB128_SIZE /* size */ +
4202 emit_byte (method_buffer, TYPE_JIT | TYPE_METHOD);
4203 emit_time (method_buffer, info->time);
4204 emit_method_inner (method_buffer, info->method);
4205 emit_ptr (method_buffer, cstart);
4206 emit_value (method_buffer, csize);
4208 memcpy (method_buffer->data, name, nlen);
4209 method_buffer->data += nlen;
4215 g_ptr_array_free (entry->methods, TRUE);
4218 dump_buffer (prof, method_buffer);
4219 else if (method_buffer)
4220 free_buffer (method_buffer, method_buffer->size);
4222 dump_buffer (prof, entry->buffer);
4232 start_writer_thread (MonoProfiler* prof)
4234 InterlockedWrite (&prof->run_writer_thread, 1);
4236 return !pthread_create (&prof->writer_thread, NULL, writer_thread, prof);
4240 runtime_initialized (MonoProfiler *profiler)
4242 #ifndef DISABLE_HELPER_THREAD
4243 if (hs_mode_ondemand || need_helper_thread) {
4244 if (!start_helper_thread (profiler))
4245 profiler->command_port = 0;
4249 start_writer_thread (profiler);
4251 InterlockedWrite (&runtime_inited, 1);
4252 #ifndef DISABLE_HELPER_THREAD
4253 counters_init (profiler);
4254 counters_sample (profiler, 0);
4256 /* ensure the main thread data and startup are available soon */
4257 safe_send (profiler, ensure_logbuf (0));
4260 static MonoProfiler*
4261 create_profiler (const char *filename, GPtrArray *filters)
4265 int force_delete = 0;
4266 prof = calloc (1, sizeof (MonoProfiler));
4268 prof->command_port = command_port;
4269 if (filename && *filename == '-') {
4275 filename = "|mprof-report -";
4277 filename = "output.mlpd";
4278 nf = (char*)filename;
4280 nf = new_filename (filename);
4282 int s = strlen (nf) + 32;
4283 char *p = malloc (s);
4284 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
4290 prof->file = popen (nf + 1, "w");
4291 prof->pipe_output = 1;
4292 } else if (*nf == '#') {
4293 int fd = strtol (nf + 1, NULL, 10);
4294 prof->file = fdopen (fd, "a");
4298 prof->file = fopen (nf, "wb");
4301 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
4304 #if defined (HAVE_SYS_ZLIB)
4306 prof->gzfile = gzdopen (fileno (prof->file), "wb");
4309 if (sample_type && !do_mono_sample)
4310 need_helper_thread = setup_perf_event ();
4312 /* FIXME: warn if different freq or sample type */
4316 if (do_mono_sample) {
4317 prof->stat_buffers = create_stat_buffer ();
4318 need_helper_thread = 1;
4320 if (do_counters && !need_helper_thread) {
4321 need_helper_thread = 1;
4324 prof->sorted_sample_events = g_ptr_array_sized_new (BUFFER_SIZE / SAMPLE_EVENT_SIZE_IN_SLOTS (0));
4326 #ifdef DISABLE_HELPER_THREAD
4327 if (hs_mode_ondemand)
4328 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
4331 fprintf (stderr, "Coverage unavailable on this arch.\n");
4335 mono_lock_free_queue_init (&prof->writer_queue);
4336 mono_mutex_init (&prof->method_table_mutex);
4337 prof->method_table = mono_conc_hashtable_new (NULL, NULL);
4340 coverage_init (prof);
4341 prof->coverage_filters = filters;
4343 prof->startup_time = current_time ();
4350 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
4351 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
4352 printf ("Options:\n");
4353 printf ("\thelp show this usage info\n");
4354 printf ("\t[no]alloc enable/disable recording allocation info\n");
4355 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
4356 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
4357 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
4358 printf ("\tcounters sample counters every 1s\n");
4359 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/1000)\n");
4360 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
4361 printf ("\t TYPE can be followed by /FREQUENCY\n");
4362 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
4363 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
4364 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
4365 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
4366 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
4367 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
4368 printf ("\treport create a report instead of writing the raw data to a file\n");
4369 printf ("\tzip compress the output data\n");
4370 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
4371 printf ("\tcoverage enable collection of code coverage data\n");
4372 printf ("\tcovfilter=ASSEMBLY add an assembly to the code coverage filters\n");
4373 printf ("\t add a + to include the assembly or a - to exclude it\n");
4374 printf ("\t filter=-mscorlib\n");
4375 printf ("\tcovfilter-file=FILE use FILE to generate the list of assemblies to be filtered\n");
4381 match_option (const char* p, const char *opt, char **rval)
4383 int len = strlen (opt);
4384 if (strncmp (p, opt, len) == 0) {
4386 if (p [len] == '=' && p [len + 1]) {
4387 const char *opt = p + len + 1;
4388 const char *end = strchr (opt, ',');
4396 val = malloc (l + 1);
4397 memcpy (val, opt, l);
4402 if (p [len] == 0 || p [len] == ',') {
4404 return p + len + (p [len] == ',');
4422 static const SampleMode sample_modes [] = {
4423 {"cycles", SAMPLE_CYCLES},
4424 {"instr", SAMPLE_INSTRUCTIONS},
4425 {"cachemiss", SAMPLE_CACHE_MISSES},
4426 {"cacherefs", SAMPLE_CACHE_REFS},
4427 {"branches", SAMPLE_BRANCHES},
4428 {"branchmiss", SAMPLE_BRANCH_MISSES},
4433 set_sample_mode (char* val, int allow_empty)
4436 char *maybe_freq = NULL;
4438 const SampleMode *smode = sample_modes;
4439 #ifndef USE_PERF_EVENTS
4442 if (allow_empty && !val) {
4443 sample_type = SAMPLE_CYCLES;
4447 if (strcmp (val, "mono") == 0) {
4449 sample_type = SAMPLE_CYCLES;
4453 for (smode = sample_modes; smode->name; smode++) {
4454 int l = strlen (smode->name);
4455 if (strncmp (val, smode->name, l) == 0) {
4456 sample_type = smode->sample_mode;
4457 maybe_freq = val + l;
4463 if (*maybe_freq == '/') {
4464 count = strtoul (maybe_freq + 1, &end, 10);
4465 if (maybe_freq + 1 == end)
4467 sample_freq = count;
4468 } else if (*maybe_freq != 0) {
4477 set_hsmode (char* val, int allow_empty)
4481 if (allow_empty && !val)
4483 if (strcmp (val, "ondemand") == 0) {
4484 hs_mode_ondemand = 1;
4488 count = strtoul (val, &end, 10);
4491 if (strcmp (end, "ms") == 0)
4493 else if (strcmp (end, "gc") == 0)
4501 * declaration to silence the compiler: this is the entry point that
4502 * mono will load from the shared library and call.
4505 mono_profiler_startup (const char *desc);
4508 mono_profiler_startup_log (const char *desc);
4511 * this is the entry point that will be used when the profiler
4512 * is embedded inside the main executable.
4515 mono_profiler_startup_log (const char *desc)
4517 mono_profiler_startup (desc);
4521 mono_profiler_startup (const char *desc)
4524 GPtrArray *filters = NULL;
4525 char *filename = NULL;
4529 int calls_enabled = 0;
4530 int allocs_enabled = 0;
4531 int only_counters = 0;
4532 int only_coverage = 0;
4533 int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
4534 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
4535 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
4536 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS|
4537 MONO_PROFILE_INS_COVERAGE|MONO_PROFILE_APPDOMAIN_EVENTS|MONO_PROFILE_CONTEXT_EVENTS|
4538 MONO_PROFILE_ASSEMBLY_EVENTS;
4541 if (strncmp (p, "log", 3))
4546 for (; *p; p = opt) {
4552 if ((opt = match_option (p, "help", NULL)) != p) {
4556 if ((opt = match_option (p, "calls", NULL)) != p) {
4560 if ((opt = match_option (p, "nocalls", NULL)) != p) {
4561 events &= ~MONO_PROFILE_ENTER_LEAVE;
4565 if ((opt = match_option (p, "alloc", NULL)) != p) {
4569 if ((opt = match_option (p, "noalloc", NULL)) != p) {
4570 events &= ~MONO_PROFILE_ALLOCATIONS;
4573 if ((opt = match_option (p, "time", &val)) != p) {
4574 if (strcmp (val, "fast") == 0)
4576 else if (strcmp (val, "null") == 0)
4583 if ((opt = match_option (p, "report", NULL)) != p) {
4587 if ((opt = match_option (p, "debug", NULL)) != p) {
4591 if ((opt = match_option (p, "sampling-real", NULL)) != p) {
4592 sampling_mode = MONO_PROFILER_STAT_MODE_REAL;
4595 if ((opt = match_option (p, "sampling-process", NULL)) != p) {
4596 sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
4599 if ((opt = match_option (p, "heapshot", &val)) != p) {
4600 events &= ~MONO_PROFILE_ALLOCATIONS;
4601 events &= ~MONO_PROFILE_ENTER_LEAVE;
4604 set_hsmode (val, 1);
4607 if ((opt = match_option (p, "sample", &val)) != p) {
4608 events &= ~MONO_PROFILE_ALLOCATIONS;
4609 events &= ~MONO_PROFILE_ENTER_LEAVE;
4611 set_sample_mode (val, 1);
4614 if ((opt = match_option (p, "hsmode", &val)) != p) {
4615 fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
4616 set_hsmode (val, 0);
4619 if ((opt = match_option (p, "zip", NULL)) != p) {
4623 if ((opt = match_option (p, "output", &val)) != p) {
4627 if ((opt = match_option (p, "port", &val)) != p) {
4629 command_port = strtoul (val, &end, 10);
4633 if ((opt = match_option (p, "maxframes", &val)) != p) {
4635 num_frames = strtoul (val, &end, 10);
4636 if (num_frames > MAX_FRAMES)
4637 num_frames = MAX_FRAMES;
4639 notraces = num_frames == 0;
4642 if ((opt = match_option (p, "calldepth", &val)) != p) {
4644 max_call_depth = strtoul (val, &end, 10);
4648 if ((opt = match_option (p, "counters", NULL)) != p) {
4652 if ((opt = match_option (p, "countersonly", NULL)) != p) {
4656 if ((opt = match_option (p, "coverage", NULL)) != p) {
4658 events |= MONO_PROFILE_ENTER_LEAVE;
4659 debug_coverage = (g_getenv ("MONO_PROFILER_DEBUG_COVERAGE") != NULL);
4662 if ((opt = match_option (p, "onlycoverage", NULL)) != p) {
4666 if ((opt = match_option (p, "covfilter-file", &val)) != p) {
4668 char *line, *content;
4670 if (filters == NULL)
4671 filters = g_ptr_array_new ();
4673 filter_file = fopen (val, "r");
4674 if (filter_file == NULL) {
4675 fprintf (stderr, "Unable to open %s\n", val);
4679 /* Don't need to free content as it is referred to by the lines stored in @filters */
4680 content = get_file_content (filter_file);
4681 if (content == NULL)
4682 fprintf (stderr, "WARNING: %s is greater than 128kb - ignoring\n", val);
4684 while ((line = get_next_line (content, &content)))
4685 g_ptr_array_add (filters, g_strchug (g_strchomp (line)));
4687 fclose (filter_file);
4690 if ((opt = match_option (p, "covfilter", &val)) != p) {
4691 if (filters == NULL)
4692 filters = g_ptr_array_new ();
4694 g_ptr_array_add (filters, val);
4702 if (calls_enabled) {
4703 events |= MONO_PROFILE_ENTER_LEAVE;
4707 events |= MONO_PROFILE_ALLOCATIONS;
4711 events = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_INS_COVERAGE;
4713 utils_init (fast_time);
4715 prof = create_profiler (filename, filters);
4720 mono_profiler_install (prof, log_shutdown);
4721 mono_profiler_install_gc (gc_event, gc_resize);
4722 mono_profiler_install_allocation (gc_alloc);
4723 mono_profiler_install_gc_moves (gc_moves);
4724 mono_profiler_install_gc_roots (gc_handle, gc_roots);
4725 mono_profiler_install_appdomain (NULL, domain_loaded, NULL, domain_unloaded);
4726 mono_profiler_install_appdomain_name (domain_name);
4727 mono_profiler_install_context (context_loaded, context_unloaded);
4728 mono_profiler_install_class (NULL, class_loaded, NULL, class_unloaded);
4729 mono_profiler_install_module (NULL, image_loaded, NULL, image_unloaded);
4730 mono_profiler_install_assembly (NULL, assembly_loaded, NULL, assembly_unloaded);
4731 mono_profiler_install_thread (thread_start, thread_end);
4732 mono_profiler_install_thread_name (thread_name);
4733 mono_profiler_install_enter_leave (method_enter, method_leave);
4734 mono_profiler_install_jit_end (method_jitted);
4735 mono_profiler_install_code_buffer_new (code_buffer_new);
4736 mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
4737 mono_profiler_install_monitor (monitor_event);
4738 mono_profiler_install_runtime_initialized (runtime_initialized);
4740 mono_profiler_install_coverage_filter (coverage_filter);
4742 if (do_mono_sample && sample_type == SAMPLE_CYCLES && !only_counters) {
4743 events |= MONO_PROFILE_STATISTICAL;
4744 mono_profiler_set_statistical_mode (sampling_mode, 1000000 / sample_freq);
4745 mono_profiler_install_statistical (mono_sample_hit);
4748 mono_profiler_set_events (events);
4750 TLS_INIT (tlsbuffer);
4751 TLS_INIT (tlsmethodlist);