2 * mono-profiler-log.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)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/debug-helpers.h>
16 #include "../metadata/metadata-internals.h"
17 #include <mono/metadata/mono-config.h>
18 #include <mono/metadata/mono-gc.h>
19 #include <mono/metadata/mono-perfcounters.h>
20 #include <mono/utils/atomic.h>
21 #include <mono/utils/hazard-pointer.h>
22 #include <mono/utils/lock-free-alloc.h>
23 #include <mono/utils/lock-free-queue.h>
24 #include <mono/utils/mono-conc-hashtable.h>
25 #include <mono/utils/mono-counters.h>
26 #include <mono/utils/mono-linked-list-set.h>
27 #include <mono/utils/mono-membar.h>
28 #include <mono/utils/mono-mmap.h>
29 #include <mono/utils/mono-os-mutex.h>
30 #include <mono/utils/mono-os-semaphore.h>
31 #include <mono/utils/mono-threads.h>
32 #include <mono/utils/mono-threads-api.h>
45 #if defined(__APPLE__)
46 #include <mach/mach_time.h>
48 #include <netinet/in.h>
49 #ifdef HAVE_SYS_MMAN_H
52 #include <sys/socket.h>
53 #if defined (HAVE_SYS_ZLIB)
57 #define BUFFER_SIZE (4096 * 16)
59 /* Worst-case size in bytes of a 64-bit value encoded with LEB128. */
60 #define LEB128_SIZE 10
62 /* Size of a value encoded as a single byte. */
63 #undef BYTE_SIZE // mach/i386/vm_param.h on OS X defines this to 8, but it isn't used for anything.
66 /* Size in bytes of the event prefix (ID + time). */
67 #define EVENT_SIZE (BYTE_SIZE + LEB128_SIZE)
69 static volatile gint32 runtime_inited;
70 static volatile gint32 in_shutdown;
72 static ProfilerConfig config;
73 static int nocalls = 0;
74 static int notraces = 0;
75 static int use_zip = 0;
76 static int do_report = 0;
77 static int do_heap_shot = 0;
78 static int max_call_depth = 0;
79 static int command_port = 0;
80 static int heapshot_requested = 0;
81 static int do_mono_sample = 0;
82 static int do_debug = 0;
83 static int do_coverage = 0;
84 static gboolean no_counters = FALSE;
85 static gboolean only_coverage = FALSE;
86 static gboolean debug_coverage = FALSE;
87 static int max_allocated_sample_hits;
89 #define ENABLED(EVT) (config.effective_mask & (EVT))
91 // Statistics for internal profiler data structures.
92 static gint32 sample_allocations_ctr,
93 buffer_allocations_ctr;
95 // Statistics for profiler events.
96 static gint32 sync_points_ctr,
105 gc_handle_creations_ctr,
106 gc_handle_deletions_ctr,
109 finalize_object_begins_ctr,
110 finalize_object_ends_ctr,
114 assembly_unloads_ctr,
119 method_exception_exits_ctr,
122 exception_throws_ctr,
123 exception_clauses_ctr,
136 counter_descriptors_ctr,
138 perfcounter_descriptors_ctr,
139 perfcounter_samples_ctr,
140 coverage_methods_ctr,
141 coverage_statements_ctr,
142 coverage_classes_ctr,
143 coverage_assemblies_ctr;
145 static MonoLinkedListSet profiler_thread_list;
151 * The file is composed by a header followed by 0 or more buffers.
152 * Each buffer contains events that happened on a thread: for a given thread
153 * buffers that appear later in the file are guaranteed to contain events
154 * that happened later in time. Buffers from separate threads could be interleaved,
156 * Buffers are not required to be aligned.
159 * [id: 4 bytes] constant value: LOG_HEADER_ID
160 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
161 * [format: 1 byte] version of the data format for the rest of the file
162 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
163 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
164 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
165 * [flags: 4 bytes] file format flags, should be 0 for now
166 * [pid: 4 bytes] pid of the profiled process
167 * [port: 2 bytes] tcp port for server if != 0
168 * [args size: 4 bytes] size of args
169 * [args: string] arguments passed to the profiler
170 * [arch size: 4 bytes] size of arch
171 * [arch: string] architecture the profiler is running on
172 * [os size: 4 bytes] size of os
173 * [os: string] operating system the profiler is running on
175 * The multiple byte integers are in little-endian format.
178 * [buffer header] [event]*
179 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
180 * Timing information and other values in the event data are usually stored
181 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
182 * some data is represented as a difference between the actual value and
183 * either the last value of the same type (like for timing information) or
184 * as the difference from a value stored in a buffer header.
186 * For timing information the data is stored as uleb128, since timing
187 * increases in a monotonic way in each thread: the value is the number of
188 * nanoseconds to add to the last seen timing data in a buffer. The first value
189 * in a buffer will be calculated from the time_base field in the buffer head.
191 * Object or heap sizes are stored as uleb128.
192 * Pointer differences are stored as sleb128, instead.
194 * If an unexpected value is found, the rest of the buffer should be ignored,
195 * as generally the later values need the former to be interpreted correctly.
197 * buffer header format:
198 * [bufid: 4 bytes] constant value: BUF_ID
199 * [len: 4 bytes] size of the data following the buffer header
200 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
201 * [ptr_base: 8 bytes] base value for pointers
202 * [obj_base: 8 bytes] base value for object addresses
203 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
204 * [method_base: 8 bytes] base value for MonoMethod pointers
207 * [extended info: upper 4 bits] [type: lower 4 bits]
208 * [time diff: uleb128] nanoseconds since last timing
210 * The data that follows depends on type and the extended info.
211 * Type is one of the enum values in mono-profiler-log.h: TYPE_ALLOC, TYPE_GC,
212 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
213 * The extended info bits are interpreted based on type, see
214 * each individual event description below.
215 * strings are represented as a 0-terminated utf8 sequence.
218 * [num: uleb128] number of frames following
219 * [frame: sleb128]* mum MonoMethod* as a pointer difference from the last such
220 * pointer or the buffer method_base
224 * exinfo: zero or TYPE_ALLOC_BT
225 * [ptr: sleb128] class as a byte difference from ptr_base
226 * [obj: sleb128] object address as a byte difference from obj_base
227 * [size: uleb128] size of the object in the heap
228 * If exinfo == TYPE_ALLOC_BT, a backtrace follows.
232 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED[_BT],
233 * TYPE_GC_HANDLE_DESTROYED[_BT], TYPE_GC_FINALIZE_START, TYPE_GC_FINALIZE_END,
234 * TYPE_GC_FINALIZE_OBJECT_START, TYPE_GC_FINALIZE_OBJECT_END
235 * if exinfo == TYPE_GC_RESIZE
236 * [heap_size: uleb128] new heap size
237 * if exinfo == TYPE_GC_EVENT
238 * [event type: byte] GC event (MONO_GC_EVENT_* from profiler.h)
239 * [generation: byte] GC generation event refers to
240 * if exinfo == TYPE_GC_MOVE
241 * [num_objects: uleb128] number of object moves that follow
242 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
243 * num is always an even number: the even items are the old
244 * addresses, the odd numbers are the respective new object addresses
245 * if exinfo == TYPE_GC_HANDLE_CREATED[_BT]
246 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
247 * upper bits reserved as flags
248 * [handle: uleb128] GC handle value
249 * [objaddr: sleb128] object pointer differences from obj_base
250 * If exinfo == TYPE_GC_HANDLE_CREATED_BT, a backtrace follows.
251 * if exinfo == TYPE_GC_HANDLE_DESTROYED[_BT]
252 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
253 * upper bits reserved as flags
254 * [handle: uleb128] GC handle value
255 * If exinfo == TYPE_GC_HANDLE_DESTROYED_BT, a backtrace follows.
256 * if exinfo == TYPE_GC_FINALIZE_OBJECT_{START,END}
257 * [object: sleb128] the object as a difference from obj_base
259 * type metadata format:
260 * type: TYPE_METADATA
261 * exinfo: one of: TYPE_END_LOAD, TYPE_END_UNLOAD (optional for TYPE_THREAD and TYPE_DOMAIN)
262 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
263 * TYPE_THREAD, TYPE_CONTEXT
264 * [pointer: sleb128] pointer of the metadata type depending on mtype
265 * if mtype == TYPE_CLASS
266 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
267 * [name: string] full class name
268 * if mtype == TYPE_IMAGE
269 * [name: string] image file name
270 * if mtype == TYPE_ASSEMBLY
271 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
272 * [name: string] assembly name
273 * if mtype == TYPE_DOMAIN && exinfo == 0
274 * [name: string] domain friendly name
275 * if mtype == TYPE_CONTEXT
276 * [domain: sleb128] domain id as pointer
277 * if mtype == TYPE_THREAD && exinfo == 0
278 * [name: string] thread name
280 * type method format:
282 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
283 * [method: sleb128] MonoMethod* as a pointer difference from the last such
284 * pointer or the buffer method_base
285 * if exinfo == TYPE_JIT
286 * [code address: sleb128] pointer to the native code as a diff from ptr_base
287 * [code size: uleb128] size of the generated code
288 * [name: string] full method name
290 * type exception format:
291 * type: TYPE_EXCEPTION
292 * exinfo: zero, TYPE_CLAUSE, or TYPE_THROW_BT
293 * if exinfo == TYPE_CLAUSE
294 * [clause type: byte] MonoExceptionEnum enum value
295 * [clause index: uleb128] index of the current clause
296 * [method: sleb128] MonoMethod* as a pointer difference from the last such
297 * pointer or the buffer method_base
298 * [object: sleb128] the exception object as a difference from obj_base
300 * [object: sleb128] the exception object as a difference from obj_base
301 * If exinfo == TYPE_THROW_BT, a backtrace follows.
303 * type runtime format:
305 * exinfo: one of: TYPE_JITHELPER
306 * if exinfo == TYPE_JITHELPER
307 * [type: byte] MonoProfilerCodeBufferType enum value
308 * [buffer address: sleb128] pointer to the native code as a diff from ptr_base
309 * [buffer size: uleb128] size of the generated code
310 * if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
311 * [name: string] buffer description name
313 * type monitor format:
315 * exinfo: zero or TYPE_MONITOR_BT
316 * [type: byte] MONO_PROFILER_MONITOR_{CONTENTION,FAIL,DONE}
317 * [object: sleb128] the lock object as a difference from obj_base
318 * If exinfo == TYPE_MONITOR_BT, a backtrace follows.
322 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
323 * if exinfo == TYPE_HEAP_OBJECT
324 * [object: sleb128] the object as a difference from obj_base
325 * [class: sleb128] the object MonoClass* as a difference from ptr_base
326 * [size: uleb128] size of the object on the heap
327 * [num_refs: uleb128] number of object references
328 * each referenced objref is preceded by a uleb128 encoded offset: the
329 * first offset is from the object address and each next offset is relative
330 * to the previous one
331 * [objrefs: sleb128]+ object referenced as a difference from obj_base
332 * The same object can appear multiple times, but only the first time
333 * with size != 0: in the other cases this data will only be used to
334 * provide additional referenced objects.
335 * if exinfo == TYPE_HEAP_ROOT
336 * [num_roots: uleb128] number of root references
337 * [num_gc: uleb128] number of major gcs
338 * [object: sleb128] the object as a difference from obj_base
339 * [root_type: byte] the root_type: MonoProfileGCRootType (profiler.h)
340 * [extra_info: uleb128] the extra_info value
341 * object, root_type and extra_info are repeated num_roots times
345 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
346 * if exinfo == TYPE_SAMPLE_HIT
347 * [thread: sleb128] thread id as difference from ptr_base
348 * [count: uleb128] number of following instruction addresses
349 * [ip: sleb128]* instruction pointer as difference from ptr_base
350 * [mbt_count: uleb128] number of managed backtrace frames
351 * [method: sleb128]* MonoMethod* as a pointer difference from the last such
352 * pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
353 * if exinfo == TYPE_SAMPLE_USYM
354 * [address: sleb128] symbol address as a difference from ptr_base
355 * [size: uleb128] symbol size (may be 0 if unknown)
356 * [name: string] symbol name
357 * if exinfo == TYPE_SAMPLE_UBIN
358 * [address: sleb128] address where binary has been loaded as a difference from ptr_base
359 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
360 * [size: uleb128] memory size
361 * [name: string] binary name
362 * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
363 * [len: uleb128] number of counters
365 * [section: uleb128] section of counter
366 * if section == MONO_COUNTER_PERFCOUNTERS:
367 * [section_name: string] section name of counter
368 * [name: string] name of counter
369 * [type: byte] type of counter
370 * [unit: byte] unit of counter
371 * [variance: byte] variance of counter
372 * [index: uleb128] unique index of counter
373 * if exinfo == TYPE_SAMPLE_COUNTERS
375 * [index: uleb128] unique index of counter
378 * [type: byte] type of counter value
381 * [0: byte] 0 -> value is null
383 * [1: byte] 1 -> value is not null
384 * [value: string] counter value
386 * [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
388 * type coverage format
389 * type: TYPE_COVERAGE
390 * exinfo: one of TYPE_COVERAGE_METHOD, TYPE_COVERAGE_STATEMENT, TYPE_COVERAGE_ASSEMBLY, TYPE_COVERAGE_CLASS
391 * if exinfo == TYPE_COVERAGE_METHOD
392 * [assembly: string] name of assembly
393 * [class: string] name of the class
394 * [name: string] name of the method
395 * [signature: string] the signature of the method
396 * [filename: string] the file path of the file that contains this method
397 * [token: uleb128] the method token
398 * [method_id: uleb128] an ID for this data to associate with the buffers of TYPE_COVERAGE_STATEMENTS
399 * [len: uleb128] the number of TYPE_COVERAGE_BUFFERS associated with this method
400 * if exinfo == TYPE_COVERAGE_STATEMENTS
401 * [method_id: uleb128] an the TYPE_COVERAGE_METHOD buffer to associate this with
402 * [offset: uleb128] the il offset relative to the previous offset
403 * [counter: uleb128] the counter for this instruction
404 * [line: uleb128] the line of filename containing this instruction
405 * [column: uleb128] the column containing this instruction
406 * if exinfo == TYPE_COVERAGE_ASSEMBLY
407 * [name: string] assembly name
408 * [guid: string] assembly GUID
409 * [filename: string] assembly filename
410 * [number_of_methods: uleb128] the number of methods in this assembly
411 * [fully_covered: uleb128] the number of fully covered methods
412 * [partially_covered: uleb128] the number of partially covered methods
413 * currently partially_covered will always be 0, and fully_covered is the
414 * number of methods that are fully and partially covered.
415 * if exinfo == TYPE_COVERAGE_CLASS
416 * [name: string] assembly name
417 * [class: string] class name
418 * [number_of_methods: uleb128] the number of methods in this class
419 * [fully_covered: uleb128] the number of fully covered methods
420 * [partially_covered: uleb128] the number of partially covered methods
421 * currently partially_covered will always be 0, and fully_covered is the
422 * number of methods that are fully and partially covered.
426 * exinfo: one of: TYPE_SYNC_POINT
427 * if exinfo == TYPE_SYNC_POINT
428 * [type: byte] MonoProfilerSyncPointType enum value
431 // Pending data to be written to the log, for a single thread.
432 // Threads periodically flush their own LogBuffers by calling safe_send
433 typedef struct _LogBuffer LogBuffer;
435 // Next (older) LogBuffer in processing queue
441 uintptr_t method_base;
442 uintptr_t last_method;
446 // Bytes allocated for this LogBuffer
449 // Start of currently unused space in buffer
450 unsigned char* cursor;
452 // Pointer to start-of-structure-plus-size (for convenience)
453 unsigned char* buf_end;
455 // Start of data in buffer. Contents follow "buffer format" described above.
456 unsigned char buf [1];
460 MonoLinkedListSetNode node;
462 // Convenience pointer to the profiler structure.
463 MonoProfiler *profiler;
465 // Was this thread added to the LLS?
468 // The current log buffer for this thread.
471 // Methods referenced by events in `buffer`, see `MethodInfo`.
474 // Current call depth for enter/leave events.
477 // Indicates whether this thread is currently writing to its `buffer`.
480 // Has this thread written a thread end event to `buffer`?
483 // Stored in `buffer_lock_state` to take the exclusive lock.
485 } MonoProfilerThread;
487 // Do not use these TLS macros directly unless you know what you're doing.
491 #define PROF_TLS_SET(VAL) (TlsSetValue (profiler_tls, (VAL)))
492 #define PROF_TLS_GET() ((MonoProfilerThread *) TlsGetValue (profiler_tls))
493 #define PROF_TLS_INIT() (profiler_tls = TlsAlloc ())
494 #define PROF_TLS_FREE() (TlsFree (profiler_tls))
496 static DWORD profiler_tls;
500 #define PROF_TLS_SET(VAL) (profiler_tls = (VAL))
501 #define PROF_TLS_GET() (profiler_tls)
502 #define PROF_TLS_INIT()
503 #define PROF_TLS_FREE()
505 static __thread MonoProfilerThread *profiler_tls;
509 #define PROF_TLS_SET(VAL) (pthread_setspecific (profiler_tls, (VAL)))
510 #define PROF_TLS_GET() ((MonoProfilerThread *) pthread_getspecific (profiler_tls))
511 #define PROF_TLS_INIT() (pthread_key_create (&profiler_tls, NULL))
512 #define PROF_TLS_FREE() (pthread_key_delete (profiler_tls))
514 static pthread_key_t profiler_tls;
521 return (uintptr_t) mono_native_thread_id_get ();
528 return (uintptr_t) GetCurrentProcessId ();
530 return (uintptr_t) getpid ();
535 static mach_timebase_info_data_t timebase_info;
536 #elif defined (HOST_WIN32)
537 static LARGE_INTEGER pcounter_freq;
540 #define TICKS_PER_SEC 1000000000LL
546 uint64_t time = mach_absolute_time ();
548 time *= timebase_info.numer;
549 time /= timebase_info.denom;
552 #elif defined (HOST_WIN32)
555 QueryPerformanceCounter (&value);
557 return value.QuadPart * TICKS_PER_SEC / pcounter_freq.QuadPart;
558 #elif defined (CLOCK_MONOTONIC)
559 struct timespec tspec;
561 clock_gettime (CLOCK_MONOTONIC, &tspec);
563 return ((uint64_t) tspec.tv_sec * TICKS_PER_SEC + tspec.tv_nsec);
567 gettimeofday (&tv, NULL);
569 return ((uint64_t) tv.tv_sec * TICKS_PER_SEC + tv.tv_usec * 1000);
573 static int timer_overhead;
579 mach_timebase_info (&timebase_info);
580 #elif defined (HOST_WIN32)
581 QueryPerformanceFrequency (&pcounter_freq);
584 uint64_t time_start = current_time ();
586 for (int i = 0; i < 256; ++i)
589 uint64_t time_end = current_time ();
591 timer_overhead = (time_end - time_start) / 256;
595 * These macros should be used when writing an event to a log buffer. They
596 * take care of a bunch of stuff that can be repetitive and error-prone, such
597 * as attaching the current thread, acquiring/releasing the buffer lock,
598 * incrementing the event counter, expanding the log buffer, etc. They also
599 * create a scope so that it's harder to leak the LogBuffer pointer, which can
600 * be problematic as the pointer is unstable when the buffer lock isn't
603 * If the calling thread is already attached, these macros will not alter its
604 * attach mode (i.e. whether it's added to the LLS). If the thread is not
605 * attached, init_thread () will be called with add_to_lls = TRUE.
608 #define ENTER_LOG(COUNTER, BUFFER, SIZE) \
610 MonoProfilerThread *thread__ = get_thread (); \
611 if (thread__->attached) \
613 g_assert (!thread__->busy && "Why are we trying to write a new event while already writing one?"); \
614 thread__->busy = TRUE; \
615 InterlockedIncrement ((COUNTER)); \
616 LogBuffer *BUFFER = ensure_logbuf_unsafe (thread__, (SIZE))
618 #define EXIT_LOG_EXPLICIT(SEND) \
619 thread__->busy = FALSE; \
621 send_log_unsafe (TRUE); \
622 if (thread__->attached) \
626 // Pass these to EXIT_LOG_EXPLICIT () for easier reading.
628 #define NO_SEND FALSE
630 #define EXIT_LOG EXIT_LOG_EXPLICIT (DO_SEND)
632 typedef struct _BinaryObject BinaryObject;
633 struct _BinaryObject {
639 static MonoProfiler *log_profiler;
641 struct _MonoProfiler {
643 #if defined (HAVE_SYS_ZLIB)
647 uint64_t startup_time;
652 MonoNativeThreadId helper_thread;
653 MonoNativeThreadId writer_thread;
654 MonoNativeThreadId dumper_thread;
655 volatile gint32 run_writer_thread;
656 MonoLockFreeAllocSizeClass writer_entry_size_class;
657 MonoLockFreeAllocator writer_entry_allocator;
658 MonoLockFreeQueue writer_queue;
659 MonoSemType writer_queue_sem;
660 MonoConcurrentHashTable *method_table;
661 mono_mutex_t method_table_mutex;
662 volatile gint32 run_dumper_thread;
663 MonoLockFreeQueue dumper_queue;
664 MonoSemType dumper_queue_sem;
665 MonoLockFreeAllocSizeClass sample_size_class;
666 MonoLockFreeAllocator sample_allocator;
667 MonoLockFreeQueue sample_reuse_queue;
668 BinaryObject *binary_objects;
669 GPtrArray *coverage_filters;
673 MonoLockFreeQueueNode node;
678 #define WRITER_ENTRY_BLOCK_SIZE (mono_pagesize ())
687 pstrdup (const char *s)
689 int len = strlen (s) + 1;
690 char *p = (char *) g_malloc (len);
696 alloc_buffer (int size)
698 return mono_valloc (NULL, size, MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_ANON | MONO_MMAP_PRIVATE, MONO_MEM_ACCOUNT_PROFILER);
702 free_buffer (void *buf, int size)
704 mono_vfree (buf, size, MONO_MEM_ACCOUNT_PROFILER);
708 create_buffer (uintptr_t tid, int bytes)
710 LogBuffer* buf = (LogBuffer *) alloc_buffer (MAX (BUFFER_SIZE, bytes));
712 InterlockedIncrement (&buffer_allocations_ctr);
714 buf->size = BUFFER_SIZE;
715 buf->time_base = current_time ();
716 buf->last_time = buf->time_base;
717 buf->buf_end = (unsigned char *) buf + buf->size;
718 buf->cursor = buf->buf;
719 buf->thread_id = tid;
725 * Must be called with the reader lock held if thread is the current thread, or
726 * the exclusive lock if thread is a different thread. However, if thread is
727 * the current thread, and init_thread () was called with add_to_lls = FALSE,
728 * then no locking is necessary.
731 init_buffer_state (MonoProfilerThread *thread)
733 thread->buffer = create_buffer (thread->node.key, 0);
734 thread->methods = NULL;
738 clear_hazard_pointers (MonoThreadHazardPointers *hp)
740 mono_hazard_pointer_clear (hp, 0);
741 mono_hazard_pointer_clear (hp, 1);
742 mono_hazard_pointer_clear (hp, 2);
745 static MonoProfilerThread *
746 init_thread (MonoProfiler *prof, gboolean add_to_lls)
748 MonoProfilerThread *thread = PROF_TLS_GET ();
751 * Sometimes we may try to initialize a thread twice. One example is the
752 * main thread: We initialize it when setting up the profiler, but we will
753 * also get a thread_start () callback for it. Another example is when
754 * attaching new threads to the runtime: We may get a gc_alloc () callback
755 * for that thread's thread object (where we initialize it), soon followed
756 * by a thread_start () callback.
758 * These cases are harmless anyhow. Just return if we've already done the
759 * initialization work.
764 thread = g_malloc (sizeof (MonoProfilerThread));
765 thread->node.key = thread_id ();
766 thread->profiler = prof;
767 thread->attached = add_to_lls;
768 thread->call_depth = 0;
770 thread->ended = FALSE;
772 init_buffer_state (thread);
774 thread->small_id = mono_thread_info_register_small_id ();
777 * Some internal profiler threads don't need to be cleaned up
778 * by the main thread on shutdown.
781 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
782 g_assert (mono_lls_insert (&profiler_thread_list, hp, &thread->node) && "Why can't we insert the thread in the LLS?");
783 clear_hazard_pointers (hp);
786 PROF_TLS_SET (thread);
791 // Only valid if init_thread () was called with add_to_lls = FALSE.
793 deinit_thread (MonoProfilerThread *thread)
795 g_assert (!thread->attached && "Why are we manually freeing an attached thread?");
801 static MonoProfilerThread *
804 return init_thread (log_profiler, TRUE);
807 // Only valid if init_thread () was called with add_to_lls = FALSE.
809 ensure_logbuf_unsafe (MonoProfilerThread *thread, int bytes)
811 LogBuffer *old = thread->buffer;
813 if (old->cursor + bytes < old->buf_end)
816 LogBuffer *new_ = create_buffer (thread->node.key, bytes);
818 thread->buffer = new_;
824 * This is a reader/writer spin lock of sorts used to protect log buffers.
825 * When a thread modifies its own log buffer, it increments the reader
826 * count. When a thread wants to access log buffers of other threads, it
827 * takes the exclusive lock.
829 * `buffer_lock_state` holds the reader count in its lower 16 bits, and
830 * the small ID of the thread currently holding the exclusive (writer)
831 * lock in its upper 16 bits. Both can be zero. It's important that the
832 * whole lock state is a single word that can be read/written atomically
833 * to avoid race conditions where there could end up being readers while
834 * the writer lock is held.
836 * The lock is writer-biased. When a thread wants to take the exclusive
837 * lock, it increments `buffer_lock_exclusive_intent` which will make new
838 * readers spin until it's back to zero, then takes the exclusive lock
839 * once the reader count has reached zero. After releasing the exclusive
840 * lock, it decrements `buffer_lock_exclusive_intent`, which, when it
841 * reaches zero again, allows readers to increment the reader count.
843 * The writer bias is necessary because we take the exclusive lock in
844 * `gc_event ()` during STW. If the writer bias was not there, and a
845 * program had a large number of threads, STW-induced pauses could be
846 * significantly longer than they have to be. Also, we emit periodic
847 * sync points from the helper thread, which requires taking the
848 * exclusive lock, and we need those to arrive with a reasonably
849 * consistent frequency so that readers don't have to queue up too many
850 * events between sync points.
852 * The lock does not support recursion.
854 static volatile gint32 buffer_lock_state;
855 static volatile gint32 buffer_lock_exclusive_intent;
861 * If the thread holding the exclusive lock tries to modify the
862 * reader count, just make it a no-op. This way, we also avoid
863 * invoking the GC safe point macros below, which could break if
864 * done from a thread that is currently the initiator of STW.
866 * In other words, we rely on the fact that the GC thread takes
867 * the exclusive lock in the gc_event () callback when the world
870 if (InterlockedRead (&buffer_lock_state) != get_thread ()->small_id << 16) {
877 // Hold off if a thread wants to take the exclusive lock.
878 while (InterlockedRead (&buffer_lock_exclusive_intent))
879 mono_thread_info_yield ();
881 old = InterlockedRead (&buffer_lock_state);
883 // Is a thread holding the exclusive lock?
885 mono_thread_info_yield ();
890 } while (InterlockedCompareExchange (&buffer_lock_state, new_, old) != old);
895 mono_memory_barrier ();
901 mono_memory_barrier ();
903 gint32 state = InterlockedRead (&buffer_lock_state);
905 // See the comment in buffer_lock ().
906 if (state == PROF_TLS_GET ()->small_id << 16)
909 g_assert (state && "Why are we decrementing a zero reader count?");
910 g_assert (!(state >> 16) && "Why is the exclusive lock held?");
912 InterlockedDecrement (&buffer_lock_state);
916 buffer_lock_excl (void)
918 gint32 new_ = get_thread ()->small_id << 16;
920 g_assert (InterlockedRead (&buffer_lock_state) != new_ && "Why are we taking the exclusive lock twice?");
922 InterlockedIncrement (&buffer_lock_exclusive_intent);
926 while (InterlockedCompareExchange (&buffer_lock_state, new_, 0))
927 mono_thread_info_yield ();
931 mono_memory_barrier ();
935 buffer_unlock_excl (void)
937 mono_memory_barrier ();
939 gint32 state = InterlockedRead (&buffer_lock_state);
940 gint32 excl = state >> 16;
942 g_assert (excl && "Why is the exclusive lock not held?");
943 g_assert (excl == PROF_TLS_GET ()->small_id && "Why does another thread hold the exclusive lock?");
944 g_assert (!(state & 0xFFFF) && "Why are there readers when the exclusive lock is held?");
946 InterlockedWrite (&buffer_lock_state, 0);
947 InterlockedDecrement (&buffer_lock_exclusive_intent);
951 encode_uleb128 (uint64_t value, uint8_t *buf, uint8_t **endbuf)
956 uint8_t b = value & 0x7f;
959 if (value != 0) /* more bytes to come */
969 encode_sleb128 (intptr_t value, uint8_t *buf, uint8_t **endbuf)
972 int negative = (value < 0);
973 unsigned int size = sizeof (intptr_t) * 8;
981 /* the following is unnecessary if the
982 * implementation of >>= uses an arithmetic rather
983 * than logical shift for a signed left operand
987 value |= - ((intptr_t) 1 <<(size - 7));
989 /* sign bit of byte is second high order bit (0x40) */
990 if ((value == 0 && !(byte & 0x40)) ||
991 (value == -1 && (byte & 0x40)))
1003 emit_byte (LogBuffer *logbuffer, int value)
1005 logbuffer->cursor [0] = value;
1006 logbuffer->cursor++;
1008 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1012 emit_value (LogBuffer *logbuffer, int value)
1014 encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
1016 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1020 emit_time (LogBuffer *logbuffer, uint64_t value)
1022 uint64_t tdiff = value - logbuffer->last_time;
1023 encode_uleb128 (tdiff, logbuffer->cursor, &logbuffer->cursor);
1024 logbuffer->last_time = value;
1026 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1030 emit_event_time (LogBuffer *logbuffer, int event, uint64_t time)
1032 emit_byte (logbuffer, event);
1033 emit_time (logbuffer, time);
1037 emit_event (LogBuffer *logbuffer, int event)
1039 emit_event_time (logbuffer, event, current_time ());
1043 emit_svalue (LogBuffer *logbuffer, int64_t value)
1045 encode_sleb128 (value, logbuffer->cursor, &logbuffer->cursor);
1047 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1051 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
1053 encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
1055 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1059 emit_ptr (LogBuffer *logbuffer, void *ptr)
1061 if (!logbuffer->ptr_base)
1062 logbuffer->ptr_base = (uintptr_t) ptr;
1064 emit_svalue (logbuffer, (intptr_t) ptr - logbuffer->ptr_base);
1066 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1070 emit_method_inner (LogBuffer *logbuffer, void *method)
1072 if (!logbuffer->method_base) {
1073 logbuffer->method_base = (intptr_t) method;
1074 logbuffer->last_method = (intptr_t) method;
1077 encode_sleb128 ((intptr_t) ((char *) method - (char *) logbuffer->last_method), logbuffer->cursor, &logbuffer->cursor);
1078 logbuffer->last_method = (intptr_t) method;
1080 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1083 // The reader lock must be held.
1085 register_method_local (MonoMethod *method, MonoJitInfo *ji)
1087 MonoProfilerThread *thread = get_thread ();
1089 if (!mono_conc_hashtable_lookup (thread->profiler->method_table, method)) {
1090 MethodInfo *info = (MethodInfo *) g_malloc (sizeof (MethodInfo));
1092 info->method = method;
1094 info->time = current_time ();
1096 GPtrArray *arr = thread->methods ? thread->methods : (thread->methods = g_ptr_array_new ());
1097 g_ptr_array_add (arr, info);
1102 emit_method (LogBuffer *logbuffer, MonoMethod *method)
1104 register_method_local (method, NULL);
1105 emit_method_inner (logbuffer, method);
1109 emit_obj (LogBuffer *logbuffer, void *ptr)
1111 if (!logbuffer->obj_base)
1112 logbuffer->obj_base = (uintptr_t) ptr >> 3;
1114 emit_svalue (logbuffer, ((uintptr_t) ptr >> 3) - logbuffer->obj_base);
1116 g_assert (logbuffer->cursor <= logbuffer->buf_end && "Why are we writing past the buffer end?");
1120 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
1124 for (; i < size; i++) {
1127 emit_byte (logbuffer, str [i]);
1130 emit_byte (logbuffer, '\0');
1134 emit_double (LogBuffer *logbuffer, double value)
1137 unsigned char buffer[8];
1138 memcpy (buffer, &value, 8);
1139 #if G_BYTE_ORDER == G_BIG_ENDIAN
1140 for (i = 7; i >= 0; i--)
1142 for (i = 0; i < 8; i++)
1144 emit_byte (logbuffer, buffer[i]);
1148 write_int16 (char *buf, int32_t value)
1151 for (i = 0; i < 2; ++i) {
1159 write_int32 (char *buf, int32_t value)
1162 for (i = 0; i < 4; ++i) {
1170 write_int64 (char *buf, int64_t value)
1173 for (i = 0; i < 8; ++i) {
1181 write_header_string (char *p, const char *str)
1183 size_t len = strlen (str) + 1;
1185 p = write_int32 (p, len);
1192 dump_header (MonoProfiler *profiler)
1194 const char *args = profiler->args;
1195 const char *arch = mono_config_get_cpu ();
1196 const char *os = mono_config_get_os ();
1198 char *hbuf = g_malloc (
1199 sizeof (gint32) /* header id */ +
1200 sizeof (gint8) /* major version */ +
1201 sizeof (gint8) /* minor version */ +
1202 sizeof (gint8) /* data version */ +
1203 sizeof (gint8) /* word size */ +
1204 sizeof (gint64) /* startup time */ +
1205 sizeof (gint32) /* timer overhead */ +
1206 sizeof (gint32) /* flags */ +
1207 sizeof (gint32) /* process id */ +
1208 sizeof (gint16) /* command port */ +
1209 sizeof (gint32) + strlen (args) + 1 /* arguments */ +
1210 sizeof (gint32) + strlen (arch) + 1 /* architecture */ +
1211 sizeof (gint32) + strlen (os) + 1 /* operating system */
1215 p = write_int32 (p, LOG_HEADER_ID);
1216 *p++ = LOG_VERSION_MAJOR;
1217 *p++ = LOG_VERSION_MINOR;
1218 *p++ = LOG_DATA_VERSION;
1219 *p++ = sizeof (void *);
1220 p = write_int64 (p, ((uint64_t) time (NULL)) * 1000);
1221 p = write_int32 (p, timer_overhead);
1222 p = write_int32 (p, 0); /* flags */
1223 p = write_int32 (p, process_id ());
1224 p = write_int16 (p, profiler->command_port);
1225 p = write_header_string (p, args);
1226 p = write_header_string (p, arch);
1227 p = write_header_string (p, os);
1229 #if defined (HAVE_SYS_ZLIB)
1230 if (profiler->gzfile) {
1231 gzwrite (profiler->gzfile, hbuf, p - hbuf);
1235 fwrite (hbuf, p - hbuf, 1, profiler->file);
1236 fflush (profiler->file);
1243 * Must be called with the reader lock held if thread is the current thread, or
1244 * the exclusive lock if thread is a different thread. However, if thread is
1245 * the current thread, and init_thread () was called with add_to_lls = FALSE,
1246 * then no locking is necessary.
1249 send_buffer (MonoProfilerThread *thread)
1251 WriterQueueEntry *entry = mono_lock_free_alloc (&thread->profiler->writer_entry_allocator);
1252 entry->methods = thread->methods;
1253 entry->buffer = thread->buffer;
1255 mono_lock_free_queue_node_init (&entry->node, FALSE);
1257 mono_lock_free_queue_enqueue (&thread->profiler->writer_queue, &entry->node);
1258 mono_os_sem_post (&thread->profiler->writer_queue_sem);
1262 free_thread (gpointer p)
1264 MonoProfilerThread *thread = p;
1266 if (!thread->ended) {
1268 * The thread is being cleaned up by the main thread during
1269 * shutdown. This typically happens for internal runtime
1270 * threads. We need to synthesize a thread end event.
1273 InterlockedIncrement (&thread_ends_ctr);
1275 if (ENABLED (PROFLOG_THREAD_EVENTS)) {
1276 LogBuffer *buf = ensure_logbuf_unsafe (thread,
1277 EVENT_SIZE /* event */ +
1278 BYTE_SIZE /* type */ +
1279 LEB128_SIZE /* tid */
1282 emit_event (buf, TYPE_END_UNLOAD | TYPE_METADATA);
1283 emit_byte (buf, TYPE_THREAD);
1284 emit_ptr (buf, (void *) thread->node.key);
1288 send_buffer (thread);
1294 remove_thread (MonoProfilerThread *thread)
1296 MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
1298 if (mono_lls_remove (&profiler_thread_list, hp, &thread->node))
1299 mono_thread_hazardous_try_free (thread, free_thread);
1301 clear_hazard_pointers (hp);
1305 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
1311 dump_buffer (profiler, buf->next);
1313 if (buf->cursor - buf->buf) {
1314 p = write_int32 (p, BUF_ID);
1315 p = write_int32 (p, buf->cursor - buf->buf);
1316 p = write_int64 (p, buf->time_base);
1317 p = write_int64 (p, buf->ptr_base);
1318 p = write_int64 (p, buf->obj_base);
1319 p = write_int64 (p, buf->thread_id);
1320 p = write_int64 (p, buf->method_base);
1322 #if defined (HAVE_SYS_ZLIB)
1323 if (profiler->gzfile) {
1324 gzwrite (profiler->gzfile, hbuf, p - hbuf);
1325 gzwrite (profiler->gzfile, buf->buf, buf->cursor - buf->buf);
1329 fwrite (hbuf, p - hbuf, 1, profiler->file);
1330 fwrite (buf->buf, buf->cursor - buf->buf, 1, profiler->file);
1331 fflush (profiler->file);
1335 free_buffer (buf, buf->size);
1339 dump_buffer_threadless (MonoProfiler *profiler, LogBuffer *buf)
1341 for (LogBuffer *iter = buf; iter; iter = iter->next)
1342 iter->thread_id = 0;
1344 dump_buffer (profiler, buf);
1347 // Only valid if init_thread () was called with add_to_lls = FALSE.
1349 send_log_unsafe (gboolean if_needed)
1351 MonoProfilerThread *thread = PROF_TLS_GET ();
1353 if (!if_needed || (if_needed && thread->buffer->next)) {
1354 if (!thread->attached)
1355 for (LogBuffer *iter = thread->buffer; iter; iter = iter->next)
1356 iter->thread_id = 0;
1358 send_buffer (thread);
1359 init_buffer_state (thread);
1363 // Assumes that the exclusive lock is held.
1365 sync_point_flush (void)
1367 g_assert (InterlockedRead (&buffer_lock_state) == PROF_TLS_GET ()->small_id << 16 && "Why don't we hold the exclusive lock?");
1369 MONO_LLS_FOREACH_SAFE (&profiler_thread_list, MonoProfilerThread, thread) {
1370 g_assert (thread->attached && "Why is a thread in the LLS not attached?");
1372 send_buffer (thread);
1373 init_buffer_state (thread);
1374 } MONO_LLS_FOREACH_SAFE_END
1377 // Assumes that the exclusive lock is held.
1379 sync_point_mark (MonoProfilerSyncPointType type)
1381 g_assert (InterlockedRead (&buffer_lock_state) == PROF_TLS_GET ()->small_id << 16 && "Why don't we hold the exclusive lock?");
1383 ENTER_LOG (&sync_points_ctr, logbuffer,
1384 EVENT_SIZE /* event */ +
1385 LEB128_SIZE /* type */
1388 emit_event (logbuffer, TYPE_META | TYPE_SYNC_POINT);
1389 emit_byte (logbuffer, type);
1391 EXIT_LOG_EXPLICIT (NO_SEND);
1393 send_log_unsafe (FALSE);
1396 // Assumes that the exclusive lock is held.
1398 sync_point (MonoProfilerSyncPointType type)
1400 sync_point_flush ();
1401 sync_point_mark (type);
1405 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
1407 /* account for object alignment in the heap */
1411 ENTER_LOG (&heap_objects_ctr, logbuffer,
1412 EVENT_SIZE /* event */ +
1413 LEB128_SIZE /* obj */ +
1414 LEB128_SIZE /* klass */ +
1415 LEB128_SIZE /* size */ +
1416 LEB128_SIZE /* num */ +
1418 LEB128_SIZE /* offset */ +
1419 LEB128_SIZE /* ref */
1423 emit_event (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
1424 emit_obj (logbuffer, obj);
1425 emit_ptr (logbuffer, klass);
1426 emit_value (logbuffer, size);
1427 emit_value (logbuffer, num);
1429 uintptr_t last_offset = 0;
1431 for (int i = 0; i < num; ++i) {
1432 emit_value (logbuffer, offsets [i] - last_offset);
1433 last_offset = offsets [i];
1434 emit_obj (logbuffer, refs [i]);
1437 EXIT_LOG_EXPLICIT (DO_SEND);
1442 static unsigned int hs_mode_ms = 0;
1443 static unsigned int hs_mode_gc = 0;
1444 static unsigned int hs_mode_ondemand = 0;
1445 static unsigned int gc_count = 0;
1446 static uint64_t last_hs_time = 0;
1447 static gboolean do_heap_walk = FALSE;
1448 static gboolean ignore_heap_events;
1451 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
1453 if (ignore_heap_events)
1456 ENTER_LOG (&heap_roots_ctr, logbuffer,
1457 EVENT_SIZE /* event */ +
1458 LEB128_SIZE /* num */ +
1459 LEB128_SIZE /* collections */ +
1461 LEB128_SIZE /* object */ +
1462 LEB128_SIZE /* root type */ +
1463 LEB128_SIZE /* extra info */
1467 emit_event (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
1468 emit_value (logbuffer, num);
1469 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
1471 for (int i = 0; i < num; ++i) {
1472 emit_obj (logbuffer, objects [i]);
1473 emit_byte (logbuffer, root_types [i]);
1474 emit_value (logbuffer, extra_info [i]);
1477 EXIT_LOG_EXPLICIT (DO_SEND);
1482 trigger_on_demand_heapshot (void)
1484 if (heapshot_requested)
1485 mono_gc_collect (mono_gc_max_generation ());
1488 #define ALL_GC_EVENTS_MASK (PROFLOG_GC_MOVES_EVENTS | PROFLOG_GC_ROOT_EVENTS | PROFLOG_GC_EVENTS | PROFLOG_HEAPSHOT_FEATURE)
1491 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation)
1493 if (ev == MONO_GC_EVENT_START) {
1494 uint64_t now = current_time ();
1496 if (hs_mode_ms && (now - last_hs_time) / 1000 * 1000 >= hs_mode_ms)
1497 do_heap_walk = TRUE;
1498 else if (hs_mode_gc && !(gc_count % hs_mode_gc))
1499 do_heap_walk = TRUE;
1500 else if (hs_mode_ondemand)
1501 do_heap_walk = heapshot_requested;
1502 else if (!hs_mode_ms && !hs_mode_gc && generation == mono_gc_max_generation ())
1503 do_heap_walk = TRUE;
1505 //If using heapshot, ignore events for collections we don't care
1506 if (ENABLED (PROFLOG_HEAPSHOT_FEATURE)) {
1507 // Ignore events generated during the collection itself (IE GC ROOTS)
1508 ignore_heap_events = !do_heap_walk;
1513 if (ENABLED (PROFLOG_GC_EVENTS)) {
1514 ENTER_LOG (&gc_events_ctr, logbuffer,
1515 EVENT_SIZE /* event */ +
1516 BYTE_SIZE /* gc event */ +
1517 BYTE_SIZE /* generation */
1520 emit_event (logbuffer, TYPE_GC_EVENT | TYPE_GC);
1521 emit_byte (logbuffer, ev);
1522 emit_byte (logbuffer, generation);
1524 EXIT_LOG_EXPLICIT (NO_SEND);
1528 case MONO_GC_EVENT_START:
1529 if (generation == mono_gc_max_generation ())
1533 case MONO_GC_EVENT_PRE_STOP_WORLD_LOCKED:
1535 * Ensure that no thread can be in the middle of writing to
1536 * a buffer when the world stops...
1538 buffer_lock_excl ();
1540 case MONO_GC_EVENT_POST_STOP_WORLD:
1542 * ... So that we now have a consistent view of all buffers.
1543 * This allows us to flush them. We need to do this because
1544 * they may contain object allocation events that need to be
1545 * committed to the log file before any object move events
1546 * that will be produced during this GC.
1548 if (ENABLED (ALL_GC_EVENTS_MASK))
1549 sync_point (SYNC_POINT_WORLD_STOP);
1552 * All heap events are surrounded by a HEAP_START and a HEAP_ENV event.
1553 * Right now, that's the case for GC Moves, GC Roots or heapshots.
1555 if (ENABLED (PROFLOG_GC_MOVES_EVENTS | PROFLOG_GC_ROOT_EVENTS) || do_heap_walk) {
1556 ENTER_LOG (&heap_starts_ctr, logbuffer,
1557 EVENT_SIZE /* event */
1560 emit_event (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
1562 EXIT_LOG_EXPLICIT (DO_SEND);
1566 case MONO_GC_EVENT_PRE_START_WORLD:
1567 if (do_heap_shot && do_heap_walk)
1568 mono_gc_walk_heap (0, gc_reference, NULL);
1570 /* Matching HEAP_END to the HEAP_START from above */
1571 if (ENABLED (PROFLOG_GC_MOVES_EVENTS | PROFLOG_GC_ROOT_EVENTS) || do_heap_walk) {
1572 ENTER_LOG (&heap_ends_ctr, logbuffer,
1573 EVENT_SIZE /* event */
1576 emit_event (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
1578 EXIT_LOG_EXPLICIT (DO_SEND);
1581 if (do_heap_shot && do_heap_walk) {
1582 do_heap_walk = FALSE;
1583 heapshot_requested = 0;
1584 last_hs_time = current_time ();
1588 * Similarly, we must now make sure that any object moves
1589 * written to the GC thread's buffer are flushed. Otherwise,
1590 * object allocation events for certain addresses could come
1591 * after the move events that made those addresses available.
1593 if (ENABLED (ALL_GC_EVENTS_MASK))
1594 sync_point_mark (SYNC_POINT_WORLD_START);
1596 case MONO_GC_EVENT_POST_START_WORLD_UNLOCKED:
1598 * Finally, it is safe to allow other threads to write to
1599 * their buffers again.
1601 buffer_unlock_excl ();
1609 gc_resize (MonoProfiler *profiler, int64_t new_size)
1611 ENTER_LOG (&gc_resizes_ctr, logbuffer,
1612 EVENT_SIZE /* event */ +
1613 LEB128_SIZE /* new size */
1616 emit_event (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
1617 emit_value (logbuffer, new_size);
1619 EXIT_LOG_EXPLICIT (DO_SEND);
1624 MonoMethod* methods [MAX_FRAMES];
1625 int32_t il_offsets [MAX_FRAMES];
1626 int32_t native_offsets [MAX_FRAMES];
1629 static int num_frames = MAX_FRAMES;
1632 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
1634 FrameData *frame = (FrameData *)data;
1635 if (method && frame->count < num_frames) {
1636 frame->il_offsets [frame->count] = il_offset;
1637 frame->native_offsets [frame->count] = native_offset;
1638 frame->methods [frame->count++] = method;
1639 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
1641 return frame->count == num_frames;
1645 * a note about stack walks: they can cause more profiler events to fire,
1646 * so we need to make sure they don't happen after we started emitting an
1647 * event, hence the collect_bt/emit_bt split.
1650 collect_bt (FrameData *data)
1653 mono_stack_walk_no_il (walk_stack, data);
1657 emit_bt (MonoProfiler *prof, LogBuffer *logbuffer, FrameData *data)
1659 if (data->count > num_frames)
1660 printf ("bad num frames: %d\n", data->count);
1662 emit_value (logbuffer, data->count);
1665 emit_method (logbuffer, data->methods [--data->count]);
1669 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
1671 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_ALLOC_BT : 0;
1673 uintptr_t len = mono_object_get_size (obj);
1674 /* account for object alignment in the heap */
1681 ENTER_LOG (&gc_allocs_ctr, logbuffer,
1682 EVENT_SIZE /* event */ +
1683 LEB128_SIZE /* klass */ +
1684 LEB128_SIZE /* obj */ +
1685 LEB128_SIZE /* size */ +
1687 LEB128_SIZE /* count */ +
1689 LEB128_SIZE /* method */
1694 emit_event (logbuffer, do_bt | TYPE_ALLOC);
1695 emit_ptr (logbuffer, klass);
1696 emit_obj (logbuffer, obj);
1697 emit_value (logbuffer, len);
1700 emit_bt (prof, logbuffer, &data);
1706 gc_moves (MonoProfiler *prof, void **objects, int num)
1708 ENTER_LOG (&gc_moves_ctr, logbuffer,
1709 EVENT_SIZE /* event */ +
1710 LEB128_SIZE /* num */ +
1712 LEB128_SIZE /* object */
1716 emit_event (logbuffer, TYPE_GC_MOVE | TYPE_GC);
1717 emit_value (logbuffer, num);
1719 for (int i = 0; i < num; ++i)
1720 emit_obj (logbuffer, objects [i]);
1722 EXIT_LOG_EXPLICIT (DO_SEND);
1726 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
1728 int do_bt = nocalls && InterlockedRead (&runtime_inited) && !notraces;
1734 gint32 *ctr = op == MONO_PROFILER_GC_HANDLE_CREATED ? &gc_handle_creations_ctr : &gc_handle_deletions_ctr;
1736 ENTER_LOG (ctr, logbuffer,
1737 EVENT_SIZE /* event */ +
1738 LEB128_SIZE /* type */ +
1739 LEB128_SIZE /* handle */ +
1740 (op == MONO_PROFILER_GC_HANDLE_CREATED ? (
1741 LEB128_SIZE /* obj */
1744 LEB128_SIZE /* count */ +
1746 LEB128_SIZE /* method */
1751 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1752 emit_event (logbuffer, (do_bt ? TYPE_GC_HANDLE_CREATED_BT : TYPE_GC_HANDLE_CREATED) | TYPE_GC);
1753 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
1754 emit_event (logbuffer, (do_bt ? TYPE_GC_HANDLE_DESTROYED_BT : TYPE_GC_HANDLE_DESTROYED) | TYPE_GC);
1756 g_assert_not_reached ();
1758 emit_value (logbuffer, type);
1759 emit_value (logbuffer, handle);
1761 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1762 emit_obj (logbuffer, obj);
1765 emit_bt (prof, logbuffer, &data);
1771 finalize_begin (MonoProfiler *prof)
1773 ENTER_LOG (&finalize_begins_ctr, buf,
1774 EVENT_SIZE /* event */
1777 emit_event (buf, TYPE_GC_FINALIZE_START | TYPE_GC);
1783 finalize_end (MonoProfiler *prof)
1785 trigger_on_demand_heapshot ();
1786 if (ENABLED (PROFLOG_FINALIZATION_EVENTS)) {
1787 ENTER_LOG (&finalize_ends_ctr, buf,
1788 EVENT_SIZE /* event */
1791 emit_event (buf, TYPE_GC_FINALIZE_END | TYPE_GC);
1798 finalize_object_begin (MonoProfiler *prof, MonoObject *obj)
1800 ENTER_LOG (&finalize_object_begins_ctr, buf,
1801 EVENT_SIZE /* event */ +
1802 LEB128_SIZE /* obj */
1805 emit_event (buf, TYPE_GC_FINALIZE_OBJECT_START | TYPE_GC);
1806 emit_obj (buf, obj);
1812 finalize_object_end (MonoProfiler *prof, MonoObject *obj)
1814 ENTER_LOG (&finalize_object_ends_ctr, buf,
1815 EVENT_SIZE /* event */ +
1816 LEB128_SIZE /* obj */
1819 emit_event (buf, TYPE_GC_FINALIZE_OBJECT_END | TYPE_GC);
1820 emit_obj (buf, obj);
1826 push_nesting (char *p, MonoClass *klass)
1831 nesting = mono_class_get_nesting_type (klass);
1833 p = push_nesting (p, nesting);
1837 name = mono_class_get_name (klass);
1838 nspace = mono_class_get_namespace (klass);
1841 p += strlen (nspace);
1851 type_name (MonoClass *klass)
1855 push_nesting (buf, klass);
1856 p = (char *) g_malloc (strlen (buf) + 1);
1862 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
1864 if (result != MONO_PROFILE_OK)
1867 const char *name = mono_image_get_filename (image);
1868 int nlen = strlen (name) + 1;
1870 ENTER_LOG (&image_loads_ctr, logbuffer,
1871 EVENT_SIZE /* event */ +
1872 BYTE_SIZE /* type */ +
1873 LEB128_SIZE /* image */ +
1877 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1878 emit_byte (logbuffer, TYPE_IMAGE);
1879 emit_ptr (logbuffer, image);
1880 memcpy (logbuffer->cursor, name, nlen);
1881 logbuffer->cursor += nlen;
1887 image_unloaded (MonoProfiler *prof, MonoImage *image)
1889 const char *name = mono_image_get_filename (image);
1890 int nlen = strlen (name) + 1;
1892 ENTER_LOG (&image_unloads_ctr, logbuffer,
1893 EVENT_SIZE /* event */ +
1894 BYTE_SIZE /* type */ +
1895 LEB128_SIZE /* image */ +
1899 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1900 emit_byte (logbuffer, TYPE_IMAGE);
1901 emit_ptr (logbuffer, image);
1902 memcpy (logbuffer->cursor, name, nlen);
1903 logbuffer->cursor += nlen;
1909 assembly_loaded (MonoProfiler *prof, MonoAssembly *assembly, int result)
1911 if (result != MONO_PROFILE_OK)
1914 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1915 int nlen = strlen (name) + 1;
1916 MonoImage *image = mono_assembly_get_image (assembly);
1918 ENTER_LOG (&assembly_loads_ctr, logbuffer,
1919 EVENT_SIZE /* event */ +
1920 BYTE_SIZE /* type */ +
1921 LEB128_SIZE /* assembly */ +
1922 LEB128_SIZE /* image */ +
1926 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1927 emit_byte (logbuffer, TYPE_ASSEMBLY);
1928 emit_ptr (logbuffer, assembly);
1929 emit_ptr (logbuffer, image);
1930 memcpy (logbuffer->cursor, name, nlen);
1931 logbuffer->cursor += nlen;
1939 assembly_unloaded (MonoProfiler *prof, MonoAssembly *assembly)
1941 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1942 int nlen = strlen (name) + 1;
1943 MonoImage *image = mono_assembly_get_image (assembly);
1945 ENTER_LOG (&assembly_unloads_ctr, logbuffer,
1946 EVENT_SIZE /* event */ +
1947 BYTE_SIZE /* type */ +
1948 LEB128_SIZE /* assembly */ +
1949 LEB128_SIZE /* image */ +
1953 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1954 emit_byte (logbuffer, TYPE_ASSEMBLY);
1955 emit_ptr (logbuffer, assembly);
1956 emit_ptr (logbuffer, image);
1957 memcpy (logbuffer->cursor, name, nlen);
1958 logbuffer->cursor += nlen;
1966 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1968 if (result != MONO_PROFILE_OK)
1973 if (InterlockedRead (&runtime_inited))
1974 name = mono_type_get_name (mono_class_get_type (klass));
1976 name = type_name (klass);
1978 int nlen = strlen (name) + 1;
1979 MonoImage *image = mono_class_get_image (klass);
1981 ENTER_LOG (&class_loads_ctr, logbuffer,
1982 EVENT_SIZE /* event */ +
1983 BYTE_SIZE /* type */ +
1984 LEB128_SIZE /* klass */ +
1985 LEB128_SIZE /* image */ +
1989 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1990 emit_byte (logbuffer, TYPE_CLASS);
1991 emit_ptr (logbuffer, klass);
1992 emit_ptr (logbuffer, image);
1993 memcpy (logbuffer->cursor, name, nlen);
1994 logbuffer->cursor += nlen;
2005 class_unloaded (MonoProfiler *prof, MonoClass *klass)
2009 if (InterlockedRead (&runtime_inited))
2010 name = mono_type_get_name (mono_class_get_type (klass));
2012 name = type_name (klass);
2014 int nlen = strlen (name) + 1;
2015 MonoImage *image = mono_class_get_image (klass);
2017 ENTER_LOG (&class_unloads_ctr, logbuffer,
2018 EVENT_SIZE /* event */ +
2019 BYTE_SIZE /* type */ +
2020 LEB128_SIZE /* klass */ +
2021 LEB128_SIZE /* image */ +
2025 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2026 emit_byte (logbuffer, TYPE_CLASS);
2027 emit_ptr (logbuffer, klass);
2028 emit_ptr (logbuffer, image);
2029 memcpy (logbuffer->cursor, name, nlen);
2030 logbuffer->cursor += nlen;
2040 static void process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method);
2043 method_enter (MonoProfiler *prof, MonoMethod *method)
2045 process_method_enter_coverage (prof, method);
2047 if (!only_coverage && get_thread ()->call_depth++ <= max_call_depth) {
2048 ENTER_LOG (&method_entries_ctr, logbuffer,
2049 EVENT_SIZE /* event */ +
2050 LEB128_SIZE /* method */
2053 emit_event (logbuffer, TYPE_ENTER | TYPE_METHOD);
2054 emit_method (logbuffer, method);
2061 method_leave (MonoProfiler *prof, MonoMethod *method)
2063 if (!only_coverage && --get_thread ()->call_depth <= max_call_depth) {
2064 ENTER_LOG (&method_exits_ctr, logbuffer,
2065 EVENT_SIZE /* event */ +
2066 LEB128_SIZE /* method */
2069 emit_event (logbuffer, TYPE_LEAVE | TYPE_METHOD);
2070 emit_method (logbuffer, method);
2077 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
2079 if (!only_coverage && !nocalls && --get_thread ()->call_depth <= max_call_depth) {
2080 ENTER_LOG (&method_exception_exits_ctr, logbuffer,
2081 EVENT_SIZE /* event */ +
2082 LEB128_SIZE /* method */
2085 emit_event (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
2086 emit_method (logbuffer, method);
2093 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji, int result)
2095 if (result != MONO_PROFILE_OK)
2100 register_method_local (method, ji);
2106 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
2111 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
2112 name = (char *) data;
2113 nlen = strlen (name) + 1;
2119 ENTER_LOG (&code_buffers_ctr, logbuffer,
2120 EVENT_SIZE /* event */ +
2121 BYTE_SIZE /* type */ +
2122 LEB128_SIZE /* buffer */ +
2123 LEB128_SIZE /* size */ +
2129 emit_event (logbuffer, TYPE_JITHELPER | TYPE_RUNTIME);
2130 emit_byte (logbuffer, type);
2131 emit_ptr (logbuffer, buffer);
2132 emit_value (logbuffer, size);
2135 memcpy (logbuffer->cursor, name, nlen);
2136 logbuffer->cursor += nlen;
2143 throw_exc (MonoProfiler *prof, MonoObject *object)
2145 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_THROW_BT : 0;
2151 ENTER_LOG (&exception_throws_ctr, logbuffer,
2152 EVENT_SIZE /* event */ +
2153 LEB128_SIZE /* object */ +
2155 LEB128_SIZE /* count */ +
2157 LEB128_SIZE /* method */
2162 emit_event (logbuffer, do_bt | TYPE_EXCEPTION);
2163 emit_obj (logbuffer, object);
2166 emit_bt (prof, logbuffer, &data);
2172 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num, MonoObject *exc)
2174 ENTER_LOG (&exception_clauses_ctr, logbuffer,
2175 EVENT_SIZE /* event */ +
2176 BYTE_SIZE /* clause type */ +
2177 LEB128_SIZE /* clause num */ +
2178 LEB128_SIZE /* method */
2181 emit_event (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
2182 emit_byte (logbuffer, clause_type);
2183 emit_value (logbuffer, clause_num);
2184 emit_method (logbuffer, method);
2185 emit_obj (logbuffer, exc);
2191 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent ev)
2193 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces) ? TYPE_MONITOR_BT : 0;
2199 ENTER_LOG (&monitor_events_ctr, logbuffer,
2200 EVENT_SIZE /* event */ +
2201 BYTE_SIZE /* ev */ +
2202 LEB128_SIZE /* object */ +
2204 LEB128_SIZE /* count */ +
2206 LEB128_SIZE /* method */
2211 emit_event (logbuffer, do_bt | TYPE_MONITOR);
2212 emit_byte (logbuffer, ev);
2213 emit_obj (logbuffer, object);
2216 emit_bt (profiler, logbuffer, &data);
2222 thread_start (MonoProfiler *prof, uintptr_t tid)
2224 if (ENABLED (PROFLOG_THREAD_EVENTS)) {
2225 ENTER_LOG (&thread_starts_ctr, logbuffer,
2226 EVENT_SIZE /* event */ +
2227 BYTE_SIZE /* type */ +
2228 LEB128_SIZE /* tid */
2231 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2232 emit_byte (logbuffer, TYPE_THREAD);
2233 emit_ptr (logbuffer, (void*) tid);
2240 thread_end (MonoProfiler *prof, uintptr_t tid)
2242 if (ENABLED (PROFLOG_THREAD_EVENTS)) {
2243 ENTER_LOG (&thread_ends_ctr, logbuffer,
2244 EVENT_SIZE /* event */ +
2245 BYTE_SIZE /* type */ +
2246 LEB128_SIZE /* tid */
2249 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2250 emit_byte (logbuffer, TYPE_THREAD);
2251 emit_ptr (logbuffer, (void*) tid);
2253 EXIT_LOG_EXPLICIT (NO_SEND);
2256 MonoProfilerThread *thread = get_thread ();
2258 thread->ended = TRUE;
2259 remove_thread (thread);
2261 PROF_TLS_SET (NULL);
2265 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
2267 int len = strlen (name) + 1;
2269 if (ENABLED (PROFLOG_THREAD_EVENTS)) {
2270 ENTER_LOG (&thread_names_ctr, logbuffer,
2271 EVENT_SIZE /* event */ +
2272 BYTE_SIZE /* type */ +
2273 LEB128_SIZE /* tid */ +
2277 emit_event (logbuffer, TYPE_METADATA);
2278 emit_byte (logbuffer, TYPE_THREAD);
2279 emit_ptr (logbuffer, (void*)tid);
2280 memcpy (logbuffer->cursor, name, len);
2281 logbuffer->cursor += len;
2288 domain_loaded (MonoProfiler *prof, MonoDomain *domain, int result)
2290 if (result != MONO_PROFILE_OK)
2293 ENTER_LOG (&domain_loads_ctr, logbuffer,
2294 EVENT_SIZE /* event */ +
2295 BYTE_SIZE /* type */ +
2296 LEB128_SIZE /* domain id */
2299 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2300 emit_byte (logbuffer, TYPE_DOMAIN);
2301 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2307 domain_unloaded (MonoProfiler *prof, MonoDomain *domain)
2309 ENTER_LOG (&domain_unloads_ctr, logbuffer,
2310 EVENT_SIZE /* event */ +
2311 BYTE_SIZE /* type */ +
2312 LEB128_SIZE /* domain id */
2315 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2316 emit_byte (logbuffer, TYPE_DOMAIN);
2317 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2323 domain_name (MonoProfiler *prof, MonoDomain *domain, const char *name)
2325 int nlen = strlen (name) + 1;
2327 ENTER_LOG (&domain_names_ctr, logbuffer,
2328 EVENT_SIZE /* event */ +
2329 BYTE_SIZE /* type */ +
2330 LEB128_SIZE /* domain id */ +
2334 emit_event (logbuffer, TYPE_METADATA);
2335 emit_byte (logbuffer, TYPE_DOMAIN);
2336 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
2337 memcpy (logbuffer->cursor, name, nlen);
2338 logbuffer->cursor += nlen;
2344 context_loaded (MonoProfiler *prof, MonoAppContext *context)
2346 ENTER_LOG (&context_loads_ctr, logbuffer,
2347 EVENT_SIZE /* event */ +
2348 BYTE_SIZE /* type */ +
2349 LEB128_SIZE /* context id */ +
2350 LEB128_SIZE /* domain id */
2353 emit_event (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
2354 emit_byte (logbuffer, TYPE_CONTEXT);
2355 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
2356 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
2362 context_unloaded (MonoProfiler *prof, MonoAppContext *context)
2364 ENTER_LOG (&context_unloads_ctr, logbuffer,
2365 EVENT_SIZE /* event */ +
2366 BYTE_SIZE /* type */ +
2367 LEB128_SIZE /* context id */ +
2368 LEB128_SIZE /* domain id */
2371 emit_event (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
2372 emit_byte (logbuffer, TYPE_CONTEXT);
2373 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
2374 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
2387 MonoLockFreeQueueNode node;
2393 AsyncFrameInfo frames [MONO_ZERO_LEN_ARRAY];
2397 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
2399 SampleHit *sample = (SampleHit *) data;
2401 if (sample->count < num_frames) {
2402 int i = sample->count;
2404 sample->frames [i].method = method;
2405 sample->frames [i].domain = domain;
2406 sample->frames [i].base_address = base_address;
2407 sample->frames [i].offset = offset;
2412 return sample->count == num_frames;
2415 #define SAMPLE_SLOT_SIZE(FRAMES) (sizeof (SampleHit) + sizeof (AsyncFrameInfo) * (FRAMES - MONO_ZERO_LEN_ARRAY))
2416 #define SAMPLE_BLOCK_SIZE (mono_pagesize ())
2419 enqueue_sample_hit (gpointer p)
2421 SampleHit *sample = p;
2423 mono_lock_free_queue_node_unpoison (&sample->node);
2424 mono_lock_free_queue_enqueue (&sample->prof->dumper_queue, &sample->node);
2425 mono_os_sem_post (&sample->prof->dumper_queue_sem);
2429 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
2432 * Please note: We rely on the runtime loading the profiler with
2433 * MONO_DL_EAGER (RTLD_NOW) so that references to runtime functions within
2434 * this function (and its siblings) are resolved when the profiler is
2435 * loaded. Otherwise, we would potentially invoke the dynamic linker when
2436 * invoking runtime functions, which is not async-signal-safe.
2439 if (InterlockedRead (&in_shutdown))
2442 SampleHit *sample = (SampleHit *) mono_lock_free_queue_dequeue (&profiler->sample_reuse_queue);
2446 * If we're out of reusable sample events and we're not allowed to
2447 * allocate more, we have no choice but to drop the event.
2449 if (InterlockedRead (&sample_allocations_ctr) >= max_allocated_sample_hits)
2452 sample = mono_lock_free_alloc (&profiler->sample_allocator);
2453 sample->prof = profiler;
2454 mono_lock_free_queue_node_init (&sample->node, TRUE);
2456 InterlockedIncrement (&sample_allocations_ctr);
2460 mono_stack_walk_async_safe (&async_walk_stack, context, sample);
2462 sample->time = current_time ();
2463 sample->tid = thread_id ();
2466 mono_thread_hazardous_try_free (sample, enqueue_sample_hit);
2469 static uintptr_t *code_pages = 0;
2470 static int num_code_pages = 0;
2471 static int size_code_pages = 0;
2472 #define CPAGE_SHIFT (9)
2473 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
2474 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
2475 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
2478 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
2481 uintptr_t start_pos;
2482 start_pos = (page >> CPAGE_SHIFT) % hsize;
2485 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
2487 } else if (!hash [i]) {
2494 } while (i != start_pos);
2495 /* should not happen */
2496 printf ("failed code page store\n");
2501 add_code_pointer (uintptr_t ip)
2504 if (num_code_pages * 2 >= size_code_pages) {
2506 uintptr_t old_size = size_code_pages;
2507 size_code_pages *= 2;
2508 if (size_code_pages == 0)
2509 size_code_pages = 16;
2510 n = (uintptr_t *) g_calloc (sizeof (uintptr_t) * size_code_pages, 1);
2511 for (i = 0; i < old_size; ++i) {
2513 add_code_page (n, size_code_pages, code_pages [i]);
2516 g_free (code_pages);
2519 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
2522 /* ELF code crashes on some systems. */
2523 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2526 dump_ubin (MonoProfiler *prof, const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
2528 int len = strlen (filename) + 1;
2530 ENTER_LOG (&sample_ubins_ctr, logbuffer,
2531 EVENT_SIZE /* event */ +
2532 LEB128_SIZE /* load address */ +
2533 LEB128_SIZE /* offset */ +
2534 LEB128_SIZE /* size */ +
2535 nlen /* file name */
2538 emit_event (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
2539 emit_ptr (logbuffer, load_addr);
2540 emit_uvalue (logbuffer, offset);
2541 emit_uvalue (logbuffer, size);
2542 memcpy (logbuffer->cursor, filename, len);
2543 logbuffer->cursor += len;
2545 EXIT_LOG_EXPLICIT (DO_SEND);
2550 dump_usym (MonoProfiler *prof, const char *name, uintptr_t value, uintptr_t size)
2552 int len = strlen (name) + 1;
2554 ENTER_LOG (&sample_usyms_ctr, logbuffer,
2555 EVENT_SIZE /* event */ +
2556 LEB128_SIZE /* value */ +
2557 LEB128_SIZE /* size */ +
2561 emit_event (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
2562 emit_ptr (logbuffer, (void*)value);
2563 emit_value (logbuffer, size);
2564 memcpy (logbuffer->cursor, name, len);
2565 logbuffer->cursor += len;
2567 EXIT_LOG_EXPLICIT (DO_SEND);
2570 /* ELF code crashes on some systems. */
2571 //#if defined(ELFMAG0)
2574 #if SIZEOF_VOID_P == 4
2575 #define ELF_WSIZE 32
2577 #define ELF_WSIZE 64
2580 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
2581 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
2582 #define _ElfW_1(e,w,t) e##w##t
2586 dump_elf_symbols (MonoProfiler *prof, ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
2589 for (i = 0; i < num_symbols; ++i) {
2591 sym = strtab + symbols [i].st_name;
2592 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
2594 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
2595 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
2600 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
2607 ElfW(Shdr) *sheader;
2608 ElfW(Shdr) *shstrtabh;
2609 ElfW(Shdr) *symtabh = NULL;
2610 ElfW(Shdr) *strtabh = NULL;
2611 ElfW(Sym) *symbols = NULL;
2615 fd = open (filename, O_RDONLY);
2618 if (fstat (fd, &statb) != 0) {
2622 file_size = statb.st_size;
2623 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2625 if (data == MAP_FAILED)
2628 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2629 header->e_ident [EI_MAG1] != ELFMAG1 ||
2630 header->e_ident [EI_MAG2] != ELFMAG2 ||
2631 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2632 munmap (data, file_size);
2635 sheader = (void*)((char*)data + header->e_shoff);
2636 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
2637 strtab = (const char*)data + shstrtabh->sh_offset;
2638 for (i = 0; i < header->e_shnum; ++i) {
2639 //printf ("section header: %d\n", sheader->sh_type);
2640 if (sheader->sh_type == SHT_SYMTAB) {
2642 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
2643 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
2646 sheader = (void*)((char*)sheader + header->e_shentsize);
2648 if (!symtabh || !strtabh) {
2649 munmap (data, file_size);
2652 strtab = (const char*)data + strtabh->sh_offset;
2653 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
2654 symbols = (void*)((char*)data + symtabh->sh_offset);
2655 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
2656 munmap (data, file_size);
2661 /* ELF code crashes on some systems. */
2662 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2665 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
2667 MonoProfiler *prof = data;
2669 const char *filename;
2671 char *a = (void*)info->dlpi_addr;
2673 ElfW(Dyn) *dyn = NULL;
2674 ElfW(Sym) *symtab = NULL;
2675 ElfW(Word) *hash_table = NULL;
2676 ElfW(Ehdr) *header = NULL;
2677 const char* strtab = NULL;
2678 for (obj = prof->binary_objects; obj; obj = obj->next) {
2682 filename = info->dlpi_name;
2685 if (!info->dlpi_addr && !filename [0]) {
2686 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
2692 obj = g_calloc (sizeof (BinaryObject), 1);
2693 obj->addr = (void*)info->dlpi_addr;
2694 obj->name = pstrdup (filename);
2695 obj->next = prof->binary_objects;
2696 prof->binary_objects = obj;
2697 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
2699 for (i = 0; i < info->dlpi_phnum; ++i) {
2700 //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);
2701 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
2702 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2703 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2704 header->e_ident [EI_MAG1] != ELFMAG1 ||
2705 header->e_ident [EI_MAG2] != ELFMAG2 ||
2706 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2709 dump_ubin (prof, filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
2710 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
2711 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2714 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
2716 if (!info->dlpi_name || !info->dlpi_name[0])
2720 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
2721 if (dyn [i].d_tag == DT_SYMTAB) {
2722 if (symtab && do_debug)
2723 printf ("multiple symtabs: %d\n", i);
2724 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
2725 } else if (dyn [i].d_tag == DT_HASH) {
2726 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
2727 } else if (dyn [i].d_tag == DT_STRTAB) {
2728 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
2733 num_sym = hash_table [1];
2734 dump_elf_symbols (prof, symtab, num_sym, strtab, (void*)info->dlpi_addr);
2739 load_binaries (MonoProfiler *prof)
2741 dl_iterate_phdr (elf_dl_callback, prof);
2746 load_binaries (MonoProfiler *prof)
2753 symbol_for (uintptr_t code)
2756 void *ip = (void*)code;
2758 if (dladdr (ip, &di)) {
2760 return di.dli_sname;
2763 names = backtrace_symbols (&ip, 1);
2765 const char* p = names [0];
2776 dump_unmanaged_coderefs (MonoProfiler *prof)
2779 const char* last_symbol;
2780 uintptr_t addr, page_end;
2782 if (load_binaries (prof))
2784 for (i = 0; i < size_code_pages; ++i) {
2786 if (!code_pages [i] || code_pages [i] & 1)
2789 addr = CPAGE_ADDR (code_pages [i]);
2790 page_end = addr + CPAGE_SIZE;
2791 code_pages [i] |= 1;
2792 /* we dump the symbols for the whole page */
2793 for (; addr < page_end; addr += 16) {
2794 sym = symbol_for (addr);
2795 if (sym && sym == last_symbol)
2800 dump_usym (prof, sym, addr, 0); /* let's not guess the size */
2801 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
2806 typedef struct MonoCounterAgent {
2807 MonoCounter *counter;
2808 // MonoCounterAgent specific data :
2813 struct MonoCounterAgent *next;
2816 static MonoCounterAgent* counters;
2817 static int counters_index = 1;
2818 static mono_mutex_t counters_mutex;
2821 counters_add_agent (MonoCounter *counter)
2823 if (InterlockedRead (&in_shutdown))
2826 MonoCounterAgent *agent, *item;
2828 mono_os_mutex_lock (&counters_mutex);
2830 for (agent = counters; agent; agent = agent->next) {
2831 if (agent->counter == counter) {
2832 agent->value_size = 0;
2834 g_free (agent->value);
2835 agent->value = NULL;
2841 agent = (MonoCounterAgent *) g_malloc (sizeof (MonoCounterAgent));
2842 agent->counter = counter;
2843 agent->value = NULL;
2844 agent->value_size = 0;
2845 agent->index = counters_index++;
2859 mono_os_mutex_unlock (&counters_mutex);
2863 counters_init_foreach_callback (MonoCounter *counter, gpointer data)
2865 counters_add_agent (counter);
2870 counters_init (MonoProfiler *profiler)
2872 mono_os_mutex_init (&counters_mutex);
2874 mono_counters_on_register (&counters_add_agent);
2875 mono_counters_foreach (counters_init_foreach_callback, NULL);
2879 counters_emit (MonoProfiler *profiler)
2881 MonoCounterAgent *agent;
2884 EVENT_SIZE /* event */ +
2885 LEB128_SIZE /* len */
2888 mono_os_mutex_lock (&counters_mutex);
2890 for (agent = counters; agent; agent = agent->next) {
2895 LEB128_SIZE /* section */ +
2896 strlen (mono_counter_get_name (agent->counter)) + 1 /* name */ +
2897 BYTE_SIZE /* type */ +
2898 BYTE_SIZE /* unit */ +
2899 BYTE_SIZE /* variance */ +
2900 LEB128_SIZE /* index */
2909 ENTER_LOG (&counter_descriptors_ctr, logbuffer, size);
2911 emit_event (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
2912 emit_value (logbuffer, len);
2914 for (agent = counters; agent; agent = agent->next) {
2920 name = mono_counter_get_name (agent->counter);
2921 emit_value (logbuffer, mono_counter_get_section (agent->counter));
2922 emit_string (logbuffer, name, strlen (name) + 1);
2923 emit_byte (logbuffer, mono_counter_get_type (agent->counter));
2924 emit_byte (logbuffer, mono_counter_get_unit (agent->counter));
2925 emit_byte (logbuffer, mono_counter_get_variance (agent->counter));
2926 emit_value (logbuffer, agent->index);
2931 EXIT_LOG_EXPLICIT (DO_SEND);
2934 mono_os_mutex_unlock (&counters_mutex);
2938 counters_sample (MonoProfiler *profiler, uint64_t timestamp)
2940 MonoCounterAgent *agent;
2941 MonoCounter *counter;
2947 counters_emit (profiler);
2950 buffer = g_calloc (1, buffer_size);
2952 mono_os_mutex_lock (&counters_mutex);
2955 EVENT_SIZE /* event */
2958 for (agent = counters; agent; agent = agent->next) {
2960 LEB128_SIZE /* index */ +
2961 BYTE_SIZE /* type */ +
2962 mono_counter_get_size (agent->counter) /* value */
2967 LEB128_SIZE /* stop marker */
2970 ENTER_LOG (&counter_samples_ctr, logbuffer, size);
2972 emit_event_time (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE, timestamp);
2974 for (agent = counters; agent; agent = agent->next) {
2977 counter = agent->counter;
2979 size = mono_counter_get_size (counter);
2981 if (size > buffer_size) {
2983 buffer = g_realloc (buffer, buffer_size);
2986 memset (buffer, 0, buffer_size);
2988 g_assert (mono_counters_sample (counter, buffer, size));
2990 type = mono_counter_get_type (counter);
2992 if (!agent->value) {
2993 agent->value = g_calloc (1, size);
2994 agent->value_size = size;
2996 if (type == MONO_COUNTER_STRING) {
2997 if (strcmp (agent->value, buffer) == 0)
3000 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
3005 emit_uvalue (logbuffer, agent->index);
3006 emit_byte (logbuffer, type);
3008 case MONO_COUNTER_INT:
3009 #if SIZEOF_VOID_P == 4
3010 case MONO_COUNTER_WORD:
3012 emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
3014 case MONO_COUNTER_UINT:
3015 emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
3017 case MONO_COUNTER_TIME_INTERVAL:
3018 case MONO_COUNTER_LONG:
3019 #if SIZEOF_VOID_P == 8
3020 case MONO_COUNTER_WORD:
3022 emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
3024 case MONO_COUNTER_ULONG:
3025 emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
3027 case MONO_COUNTER_DOUBLE:
3028 emit_double (logbuffer, *(double*)buffer);
3030 case MONO_COUNTER_STRING:
3032 emit_byte (logbuffer, 0);
3034 emit_byte (logbuffer, 1);
3035 emit_string (logbuffer, (char*)buffer, size);
3039 g_assert_not_reached ();
3042 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
3043 agent->value = g_realloc (agent->value, size);
3044 agent->value_size = size;
3048 memcpy (agent->value, buffer, size);
3052 emit_value (logbuffer, 0);
3054 EXIT_LOG_EXPLICIT (DO_SEND);
3056 mono_os_mutex_unlock (&counters_mutex);
3059 typedef struct _PerfCounterAgent PerfCounterAgent;
3060 struct _PerfCounterAgent {
3061 PerfCounterAgent *next;
3063 char *category_name;
3072 static PerfCounterAgent *perfcounters = NULL;
3075 perfcounters_emit (MonoProfiler *profiler)
3077 PerfCounterAgent *pcagent;
3080 EVENT_SIZE /* event */ +
3081 LEB128_SIZE /* len */
3084 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3085 if (pcagent->emitted)
3089 LEB128_SIZE /* section */ +
3090 strlen (pcagent->category_name) + 1 /* category name */ +
3091 strlen (pcagent->name) + 1 /* name */ +
3092 BYTE_SIZE /* type */ +
3093 BYTE_SIZE /* unit */ +
3094 BYTE_SIZE /* variance */ +
3095 LEB128_SIZE /* index */
3104 ENTER_LOG (&perfcounter_descriptors_ctr, logbuffer, size);
3106 emit_event (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
3107 emit_value (logbuffer, len);
3109 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3110 if (pcagent->emitted)
3113 emit_value (logbuffer, MONO_COUNTER_PERFCOUNTERS);
3114 emit_string (logbuffer, pcagent->category_name, strlen (pcagent->category_name) + 1);
3115 emit_string (logbuffer, pcagent->name, strlen (pcagent->name) + 1);
3116 emit_byte (logbuffer, MONO_COUNTER_LONG);
3117 emit_byte (logbuffer, MONO_COUNTER_RAW);
3118 emit_byte (logbuffer, MONO_COUNTER_VARIABLE);
3119 emit_value (logbuffer, pcagent->index);
3121 pcagent->emitted = 1;
3124 EXIT_LOG_EXPLICIT (DO_SEND);
3128 perfcounters_foreach (char *category_name, char *name, unsigned char type, gint64 value, gpointer user_data)
3130 PerfCounterAgent *pcagent;
3132 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3133 if (strcmp (pcagent->category_name, category_name) != 0 || strcmp (pcagent->name, name) != 0)
3135 if (pcagent->value == value)
3138 pcagent->value = value;
3139 pcagent->updated = 1;
3140 pcagent->deleted = 0;
3144 pcagent = g_new0 (PerfCounterAgent, 1);
3145 pcagent->next = perfcounters;
3146 pcagent->index = counters_index++;
3147 pcagent->category_name = g_strdup (category_name);
3148 pcagent->name = g_strdup (name);
3149 pcagent->type = (int) type;
3150 pcagent->value = value;
3151 pcagent->emitted = 0;
3152 pcagent->updated = 1;
3153 pcagent->deleted = 0;
3155 perfcounters = pcagent;
3161 perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp)
3163 PerfCounterAgent *pcagent;
3167 mono_os_mutex_lock (&counters_mutex);
3169 /* mark all perfcounters as deleted, foreach will unmark them as necessary */
3170 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
3171 pcagent->deleted = 1;
3173 mono_perfcounter_foreach (perfcounters_foreach, perfcounters);
3175 perfcounters_emit (profiler);
3178 EVENT_SIZE /* event */
3181 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3182 if (pcagent->deleted || !pcagent->updated)
3186 LEB128_SIZE /* index */ +
3187 BYTE_SIZE /* type */ +
3188 LEB128_SIZE /* value */
3198 LEB128_SIZE /* stop marker */
3201 ENTER_LOG (&perfcounter_samples_ctr, logbuffer, size);
3203 emit_event_time (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE, timestamp);
3205 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3206 if (pcagent->deleted || !pcagent->updated)
3208 emit_uvalue (logbuffer, pcagent->index);
3209 emit_byte (logbuffer, MONO_COUNTER_LONG);
3210 emit_svalue (logbuffer, pcagent->value);
3212 pcagent->updated = 0;
3215 emit_value (logbuffer, 0);
3217 EXIT_LOG_EXPLICIT (DO_SEND);
3220 mono_os_mutex_unlock (&counters_mutex);
3224 counters_and_perfcounters_sample (MonoProfiler *prof)
3226 uint64_t now = current_time ();
3228 counters_sample (prof, now);
3229 perfcounters_sample (prof, now);
3232 #define COVERAGE_DEBUG(x) if (debug_coverage) {x}
3233 static mono_mutex_t coverage_mutex;
3234 static MonoConcurrentHashTable *coverage_methods = NULL;
3235 static MonoConcurrentHashTable *coverage_assemblies = NULL;
3236 static MonoConcurrentHashTable *coverage_classes = NULL;
3238 static MonoConcurrentHashTable *filtered_classes = NULL;
3239 static MonoConcurrentHashTable *entered_methods = NULL;
3240 static MonoConcurrentHashTable *image_to_methods = NULL;
3241 static MonoConcurrentHashTable *suppressed_assemblies = NULL;
3242 static gboolean coverage_initialized = FALSE;
3244 static GPtrArray *coverage_data = NULL;
3245 static int previous_offset = 0;
3248 MonoLockFreeQueueNode node;
3261 free_coverage_entry (gpointer data, gpointer userdata)
3263 CoverageEntry *entry = (CoverageEntry *)data;
3264 g_free (entry->filename);
3269 obtain_coverage_for_method (MonoProfiler *prof, const MonoProfileCoverageEntry *entry)
3271 int offset = entry->iloffset - previous_offset;
3272 CoverageEntry *e = g_new (CoverageEntry, 1);
3274 previous_offset = entry->iloffset;
3277 e->counter = entry->counter;
3278 e->filename = g_strdup(entry->filename ? entry->filename : "");
3279 e->line = entry->line;
3280 e->column = entry->col;
3282 g_ptr_array_add (coverage_data, e);
3286 parse_generic_type_names(char *name)
3288 char *new_name, *ret;
3289 int within_generic_declaration = 0, generic_members = 1;
3291 if (name == NULL || *name == '\0')
3292 return g_strdup ("");
3294 if (!(ret = new_name = (char *) g_calloc (strlen (name) * 4 + 1, sizeof (char))))
3300 within_generic_declaration = 1;
3304 within_generic_declaration = 0;
3306 if (*(name - 1) != '<') {
3308 *new_name++ = '0' + generic_members;
3310 memcpy (new_name, "<>", 8);
3314 generic_members = 0;
3322 if (!within_generic_declaration)
3323 *new_name++ = *name;
3332 static int method_id;
3334 build_method_buffer (gpointer key, gpointer value, gpointer userdata)
3336 MonoMethod *method = (MonoMethod *)value;
3337 MonoProfiler *prof = (MonoProfiler *)userdata;
3341 const char *image_name, *method_name, *sig, *first_filename;
3344 previous_offset = 0;
3345 coverage_data = g_ptr_array_new ();
3347 mono_profiler_coverage_get (prof, method, obtain_coverage_for_method);
3349 klass = mono_method_get_class (method);
3350 image = mono_class_get_image (klass);
3351 image_name = mono_image_get_name (image);
3353 sig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3354 class_name = parse_generic_type_names (mono_type_get_name (mono_class_get_type (klass)));
3355 method_name = mono_method_get_name (method);
3357 if (coverage_data->len != 0) {
3358 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[0];
3359 first_filename = entry->filename ? entry->filename : "";
3361 first_filename = "";
3363 image_name = image_name ? image_name : "";
3364 sig = sig ? sig : "";
3365 method_name = method_name ? method_name : "";
3367 ENTER_LOG (&coverage_methods_ctr, logbuffer,
3368 EVENT_SIZE /* event */ +
3369 strlen (image_name) + 1 /* image name */ +
3370 strlen (class_name) + 1 /* class name */ +
3371 strlen (method_name) + 1 /* method name */ +
3372 strlen (sig) + 1 /* signature */ +
3373 strlen (first_filename) + 1 /* first file name */ +
3374 LEB128_SIZE /* token */ +
3375 LEB128_SIZE /* method id */ +
3376 LEB128_SIZE /* entries */
3379 emit_event (logbuffer, TYPE_COVERAGE_METHOD | TYPE_COVERAGE);
3380 emit_string (logbuffer, image_name, strlen (image_name) + 1);
3381 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3382 emit_string (logbuffer, method_name, strlen (method_name) + 1);
3383 emit_string (logbuffer, sig, strlen (sig) + 1);
3384 emit_string (logbuffer, first_filename, strlen (first_filename) + 1);
3386 emit_uvalue (logbuffer, mono_method_get_token (method));
3387 emit_uvalue (logbuffer, method_id);
3388 emit_value (logbuffer, coverage_data->len);
3390 EXIT_LOG_EXPLICIT (DO_SEND);
3392 for (i = 0; i < coverage_data->len; i++) {
3393 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[i];
3395 ENTER_LOG (&coverage_statements_ctr, logbuffer,
3396 EVENT_SIZE /* event */ +
3397 LEB128_SIZE /* method id */ +
3398 LEB128_SIZE /* offset */ +
3399 LEB128_SIZE /* counter */ +
3400 LEB128_SIZE /* line */ +
3401 LEB128_SIZE /* column */
3404 emit_event (logbuffer, TYPE_COVERAGE_STATEMENT | TYPE_COVERAGE);
3405 emit_uvalue (logbuffer, method_id);
3406 emit_uvalue (logbuffer, entry->offset);
3407 emit_uvalue (logbuffer, entry->counter);
3408 emit_uvalue (logbuffer, entry->line);
3409 emit_uvalue (logbuffer, entry->column);
3411 EXIT_LOG_EXPLICIT (DO_SEND);
3416 g_free (class_name);
3418 g_ptr_array_foreach (coverage_data, free_coverage_entry, NULL);
3419 g_ptr_array_free (coverage_data, TRUE);
3420 coverage_data = NULL;
3423 /* This empties the queue */
3425 count_queue (MonoLockFreeQueue *queue)
3427 MonoLockFreeQueueNode *node;
3430 while ((node = mono_lock_free_queue_dequeue (queue))) {
3432 mono_thread_hazardous_try_free (node, g_free);
3439 build_class_buffer (gpointer key, gpointer value, gpointer userdata)
3441 MonoClass *klass = (MonoClass *)key;
3442 MonoLockFreeQueue *class_methods = (MonoLockFreeQueue *)value;
3445 const char *assembly_name;
3446 int number_of_methods, partially_covered;
3447 guint fully_covered;
3449 image = mono_class_get_image (klass);
3450 assembly_name = mono_image_get_name (image);
3451 class_name = mono_type_get_name (mono_class_get_type (klass));
3453 assembly_name = assembly_name ? assembly_name : "";
3454 number_of_methods = mono_class_num_methods (klass);
3455 fully_covered = count_queue (class_methods);
3456 /* We don't handle partial covered yet */
3457 partially_covered = 0;
3459 ENTER_LOG (&coverage_classes_ctr, logbuffer,
3460 EVENT_SIZE /* event */ +
3461 strlen (assembly_name) + 1 /* assembly name */ +
3462 strlen (class_name) + 1 /* class name */ +
3463 LEB128_SIZE /* no. methods */ +
3464 LEB128_SIZE /* fully covered */ +
3465 LEB128_SIZE /* partially covered */
3468 emit_event (logbuffer, TYPE_COVERAGE_CLASS | TYPE_COVERAGE);
3469 emit_string (logbuffer, assembly_name, strlen (assembly_name) + 1);
3470 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3471 emit_uvalue (logbuffer, number_of_methods);
3472 emit_uvalue (logbuffer, fully_covered);
3473 emit_uvalue (logbuffer, partially_covered);
3475 EXIT_LOG_EXPLICIT (DO_SEND);
3477 g_free (class_name);
3481 get_coverage_for_image (MonoImage *image, int *number_of_methods, guint *fully_covered, int *partially_covered)
3483 MonoLockFreeQueue *image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
3485 *number_of_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
3487 *fully_covered = count_queue (image_methods);
3491 // FIXME: We don't handle partially covered yet.
3492 *partially_covered = 0;
3496 build_assembly_buffer (gpointer key, gpointer value, gpointer userdata)
3498 MonoAssembly *assembly = (MonoAssembly *)value;
3499 MonoImage *image = mono_assembly_get_image (assembly);
3500 const char *name, *guid, *filename;
3501 int number_of_methods = 0, partially_covered = 0;
3502 guint fully_covered = 0;
3504 name = mono_image_get_name (image);
3505 guid = mono_image_get_guid (image);
3506 filename = mono_image_get_filename (image);
3508 name = name ? name : "";
3509 guid = guid ? guid : "";
3510 filename = filename ? filename : "";
3512 get_coverage_for_image (image, &number_of_methods, &fully_covered, &partially_covered);
3514 ENTER_LOG (&coverage_assemblies_ctr, logbuffer,
3515 EVENT_SIZE /* event */ +
3516 strlen (name) + 1 /* name */ +
3517 strlen (guid) + 1 /* guid */ +
3518 strlen (filename) + 1 /* file name */ +
3519 LEB128_SIZE /* no. methods */ +
3520 LEB128_SIZE /* fully covered */ +
3521 LEB128_SIZE /* partially covered */
3524 emit_event (logbuffer, TYPE_COVERAGE_ASSEMBLY | TYPE_COVERAGE);
3525 emit_string (logbuffer, name, strlen (name) + 1);
3526 emit_string (logbuffer, guid, strlen (guid) + 1);
3527 emit_string (logbuffer, filename, strlen (filename) + 1);
3528 emit_uvalue (logbuffer, number_of_methods);
3529 emit_uvalue (logbuffer, fully_covered);
3530 emit_uvalue (logbuffer, partially_covered);
3532 EXIT_LOG_EXPLICIT (DO_SEND);
3536 dump_coverage (MonoProfiler *prof)
3538 if (!coverage_initialized)
3541 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Started dump\n");)
3544 mono_os_mutex_lock (&coverage_mutex);
3545 mono_conc_hashtable_foreach (coverage_assemblies, build_assembly_buffer, NULL);
3546 mono_conc_hashtable_foreach (coverage_classes, build_class_buffer, NULL);
3547 mono_conc_hashtable_foreach (coverage_methods, build_method_buffer, prof);
3548 mono_os_mutex_unlock (&coverage_mutex);
3550 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Finished dump\n");)
3554 process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method)
3559 if (!coverage_initialized)
3562 klass = mono_method_get_class (method);
3563 image = mono_class_get_image (klass);
3565 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)))
3568 mono_os_mutex_lock (&coverage_mutex);
3569 mono_conc_hashtable_insert (entered_methods, method, method);
3570 mono_os_mutex_unlock (&coverage_mutex);
3573 static MonoLockFreeQueueNode *
3574 create_method_node (MonoMethod *method)
3576 MethodNode *node = (MethodNode *) g_malloc (sizeof (MethodNode));
3577 mono_lock_free_queue_node_init ((MonoLockFreeQueueNode *) node, FALSE);
3578 node->method = method;
3580 return (MonoLockFreeQueueNode *) node;
3584 coverage_filter (MonoProfiler *prof, MonoMethod *method)
3589 MonoAssembly *assembly;
3590 MonoMethodHeader *header;
3591 guint32 iflags, flags, code_size;
3592 char *fqn, *classname;
3593 gboolean has_positive, found;
3594 MonoLockFreeQueue *image_methods, *class_methods;
3595 MonoLockFreeQueueNode *node;
3597 g_assert (coverage_initialized && "Why are we being asked for coverage filter info when we're not doing coverage?");
3599 COVERAGE_DEBUG(fprintf (stderr, "Coverage filter for %s\n", mono_method_get_name (method));)
3601 flags = mono_method_get_flags (method, &iflags);
3602 if ((iflags & 0x1000 /*METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL*/) ||
3603 (flags & 0x2000 /*METHOD_ATTRIBUTE_PINVOKE_IMPL*/)) {
3604 COVERAGE_DEBUG(fprintf (stderr, " Internal call or pinvoke - ignoring\n");)
3608 // Don't need to do anything else if we're already tracking this method
3609 if (mono_conc_hashtable_lookup (coverage_methods, method)) {
3610 COVERAGE_DEBUG(fprintf (stderr, " Already tracking\n");)
3614 klass = mono_method_get_class (method);
3615 image = mono_class_get_image (klass);
3617 // Don't handle coverage for the core assemblies
3618 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)) != NULL)
3621 if (prof->coverage_filters) {
3622 /* Check already filtered classes first */
3623 if (mono_conc_hashtable_lookup (filtered_classes, klass)) {
3624 COVERAGE_DEBUG(fprintf (stderr, " Already filtered\n");)
3628 classname = mono_type_get_name (mono_class_get_type (klass));
3630 fqn = g_strdup_printf ("[%s]%s", mono_image_get_name (image), classname);
3632 COVERAGE_DEBUG(fprintf (stderr, " Looking for %s in filter\n", fqn);)
3633 // Check positive filters first
3634 has_positive = FALSE;
3636 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3637 char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
3639 if (filter [0] == '+') {
3640 filter = &filter [1];
3642 COVERAGE_DEBUG(fprintf (stderr, " Checking against +%s ...", filter);)
3644 if (strstr (fqn, filter) != NULL) {
3645 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3648 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3650 has_positive = TRUE;
3654 if (has_positive && !found) {
3655 COVERAGE_DEBUG(fprintf (stderr, " Positive match was not found\n");)
3657 mono_os_mutex_lock (&coverage_mutex);
3658 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3659 mono_os_mutex_unlock (&coverage_mutex);
3666 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3667 // FIXME: Is substring search sufficient?
3668 char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
3669 if (filter [0] == '+')
3673 filter = &filter [1];
3674 COVERAGE_DEBUG(fprintf (stderr, " Checking against -%s ...", filter);)
3676 if (strstr (fqn, filter) != NULL) {
3677 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3679 mono_os_mutex_lock (&coverage_mutex);
3680 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3681 mono_os_mutex_unlock (&coverage_mutex);
3687 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3695 COVERAGE_DEBUG(fprintf (stderr, " Handling coverage for %s\n", mono_method_get_name (method));)
3696 header = mono_method_get_header_checked (method, &error);
3697 mono_error_cleanup (&error);
3699 mono_method_header_get_code (header, &code_size, NULL);
3701 assembly = mono_image_get_assembly (image);
3703 // Need to keep the assemblies around for as long as they are kept in the hashtable
3704 // Nunit, for example, has a habit of unloading them before the coverage statistics are
3705 // generated causing a crash. See https://bugzilla.xamarin.com/show_bug.cgi?id=39325
3706 mono_assembly_addref (assembly);
3708 mono_os_mutex_lock (&coverage_mutex);
3709 mono_conc_hashtable_insert (coverage_methods, method, method);
3710 mono_conc_hashtable_insert (coverage_assemblies, assembly, assembly);
3711 mono_os_mutex_unlock (&coverage_mutex);
3713 image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
3715 if (image_methods == NULL) {
3716 image_methods = (MonoLockFreeQueue *) g_malloc (sizeof (MonoLockFreeQueue));
3717 mono_lock_free_queue_init (image_methods);
3718 mono_os_mutex_lock (&coverage_mutex);
3719 mono_conc_hashtable_insert (image_to_methods, image, image_methods);
3720 mono_os_mutex_unlock (&coverage_mutex);
3723 node = create_method_node (method);
3724 mono_lock_free_queue_enqueue (image_methods, node);
3726 class_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (coverage_classes, klass);
3728 if (class_methods == NULL) {
3729 class_methods = (MonoLockFreeQueue *) g_malloc (sizeof (MonoLockFreeQueue));
3730 mono_lock_free_queue_init (class_methods);
3731 mono_os_mutex_lock (&coverage_mutex);
3732 mono_conc_hashtable_insert (coverage_classes, klass, class_methods);
3733 mono_os_mutex_unlock (&coverage_mutex);
3736 node = create_method_node (method);
3737 mono_lock_free_queue_enqueue (class_methods, node);
3742 #define LINE_BUFFER_SIZE 4096
3743 /* Max file limit of 128KB */
3744 #define MAX_FILE_SIZE 128 * 1024
3746 get_file_content (FILE *stream)
3751 int res, offset = 0;
3753 res = fseek (stream, 0, SEEK_END);
3757 filesize = ftell (stream);
3761 res = fseek (stream, 0, SEEK_SET);
3765 if (filesize > MAX_FILE_SIZE)
3768 buffer = (char *) g_malloc ((filesize + 1) * sizeof (char));
3769 while ((bytes_read = fread (buffer + offset, 1, LINE_BUFFER_SIZE, stream)) > 0)
3770 offset += bytes_read;
3772 /* NULL terminate our buffer */
3773 buffer[filesize] = '\0';
3778 get_next_line (char *contents, char **next_start)
3782 if (p == NULL || *p == '\0') {
3787 while (*p != '\n' && *p != '\0')
3792 *next_start = p + 1;
3800 init_suppressed_assemblies (void)
3806 suppressed_assemblies = mono_conc_hashtable_new (g_str_hash, g_str_equal);
3807 sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
3808 if (sa_file == NULL)
3811 /* Don't need to free @content as it is referred to by the lines stored in @suppressed_assemblies */
3812 content = get_file_content (sa_file);
3813 if (content == NULL) {
3814 g_error ("mono-profiler-log.suppression is greater than 128kb - aborting\n");
3817 while ((line = get_next_line (content, &content))) {
3818 line = g_strchomp (g_strchug (line));
3819 /* No locking needed as we're doing initialization */
3820 mono_conc_hashtable_insert (suppressed_assemblies, line, line);
3827 parse_cov_filter_file (GPtrArray *filters, const char *file)
3830 char *line, *content;
3832 filter_file = fopen (file, "r");
3833 if (filter_file == NULL) {
3834 fprintf (stderr, "Unable to open %s\n", file);
3838 /* Don't need to free content as it is referred to by the lines stored in @filters */
3839 content = get_file_content (filter_file);
3840 if (content == NULL)
3841 fprintf (stderr, "WARNING: %s is greater than 128kb - ignoring\n", file);
3843 while ((line = get_next_line (content, &content)))
3844 g_ptr_array_add (filters, g_strchug (g_strchomp (line)));
3846 fclose (filter_file);
3850 coverage_init (MonoProfiler *prof)
3852 g_assert (!coverage_initialized && "Why are we initializing coverage twice?");
3854 COVERAGE_DEBUG(fprintf (stderr, "Coverage initialized\n");)
3856 mono_os_mutex_init (&coverage_mutex);
3857 coverage_methods = mono_conc_hashtable_new (NULL, NULL);
3858 coverage_assemblies = mono_conc_hashtable_new (NULL, NULL);
3859 coverage_classes = mono_conc_hashtable_new (NULL, NULL);
3860 filtered_classes = mono_conc_hashtable_new (NULL, NULL);
3861 entered_methods = mono_conc_hashtable_new (NULL, NULL);
3862 image_to_methods = mono_conc_hashtable_new (NULL, NULL);
3863 init_suppressed_assemblies ();
3865 coverage_initialized = TRUE;
3869 unref_coverage_assemblies (gpointer key, gpointer value, gpointer userdata)
3871 MonoAssembly *assembly = (MonoAssembly *)value;
3872 mono_assembly_close (assembly);
3876 free_sample_hit (gpointer p)
3878 mono_lock_free_free (p, SAMPLE_BLOCK_SIZE);
3882 cleanup_reusable_samples (MonoProfiler *prof)
3886 while ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->sample_reuse_queue)))
3887 mono_thread_hazardous_try_free (sample, free_sample_hit);
3891 log_shutdown (MonoProfiler *prof)
3893 InterlockedWrite (&in_shutdown, 1);
3896 counters_and_perfcounters_sample (prof);
3898 dump_coverage (prof);
3902 if (write (prof->pipes [1], &c, 1) != 1) {
3903 fprintf (stderr, "Could not write to pipe: %s\n", strerror (errno));
3907 mono_native_thread_join (prof->helper_thread);
3909 mono_os_mutex_destroy (&counters_mutex);
3911 MonoCounterAgent *mc_next;
3913 for (MonoCounterAgent *cur = counters; cur; cur = mc_next) {
3914 mc_next = cur->next;
3918 PerfCounterAgent *pc_next;
3920 for (PerfCounterAgent *cur = perfcounters; cur; cur = pc_next) {
3921 pc_next = cur->next;
3926 * Ensure that we empty the LLS completely, even if some nodes are
3927 * not immediately removed upon calling mono_lls_remove (), by
3928 * iterating until the head is NULL.
3930 while (profiler_thread_list.head) {
3931 MONO_LLS_FOREACH_SAFE (&profiler_thread_list, MonoProfilerThread, thread) {
3932 g_assert (thread->attached && "Why is a thread in the LLS not attached?");
3934 remove_thread (thread);
3935 } MONO_LLS_FOREACH_SAFE_END
3939 * Ensure that all threads have been freed, so that we don't miss any
3940 * buffers when we shut down the writer thread below.
3942 mono_thread_hazardous_try_free_all ();
3944 InterlockedWrite (&prof->run_dumper_thread, 0);
3945 mono_os_sem_post (&prof->dumper_queue_sem);
3946 mono_native_thread_join (prof->dumper_thread);
3947 mono_os_sem_destroy (&prof->dumper_queue_sem);
3949 InterlockedWrite (&prof->run_writer_thread, 0);
3950 mono_os_sem_post (&prof->writer_queue_sem);
3951 mono_native_thread_join (prof->writer_thread);
3952 mono_os_sem_destroy (&prof->writer_queue_sem);
3955 * Free all writer queue entries, and ensure that all sample hits will be
3956 * added to the sample reuse queue.
3958 mono_thread_hazardous_try_free_all ();
3960 cleanup_reusable_samples (prof);
3963 * Finally, make sure that all sample hits are freed. This should cover all
3964 * hazardous data from the profiler. We can now be sure that the runtime
3965 * won't later invoke free functions in the profiler library after it has
3968 mono_thread_hazardous_try_free_all ();
3970 gint32 state = InterlockedRead (&buffer_lock_state);
3972 g_assert (!(state & 0xFFFF) && "Why is the reader count still non-zero?");
3973 g_assert (!(state >> 16) && "Why is the exclusive lock still held?");
3975 #if defined (HAVE_SYS_ZLIB)
3977 gzclose (prof->gzfile);
3979 if (prof->pipe_output)
3980 pclose (prof->file);
3982 fclose (prof->file);
3984 mono_conc_hashtable_destroy (prof->method_table);
3985 mono_os_mutex_destroy (&prof->method_table_mutex);
3987 if (coverage_initialized) {
3988 mono_os_mutex_lock (&coverage_mutex);
3989 mono_conc_hashtable_foreach (coverage_assemblies, unref_coverage_assemblies, prof);
3990 mono_os_mutex_unlock (&coverage_mutex);
3992 mono_conc_hashtable_destroy (coverage_methods);
3993 mono_conc_hashtable_destroy (coverage_assemblies);
3994 mono_conc_hashtable_destroy (coverage_classes);
3995 mono_conc_hashtable_destroy (filtered_classes);
3997 mono_conc_hashtable_destroy (entered_methods);
3998 mono_conc_hashtable_destroy (image_to_methods);
3999 mono_conc_hashtable_destroy (suppressed_assemblies);
4000 mono_os_mutex_destroy (&coverage_mutex);
4005 g_free (prof->args);
4010 new_filename (const char* filename)
4012 time_t t = time (NULL);
4013 int pid = process_id ();
4018 int count_dates = 0;
4022 for (p = filename; *p; p++) {
4033 if (!count_dates && !count_pids)
4034 return pstrdup (filename);
4035 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
4037 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
4038 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
4039 s_date = strlen (time_buf);
4040 s_pid = strlen (pid_buf);
4041 d = res = (char *) g_malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
4042 for (p = filename; *p; p++) {
4049 strcpy (d, time_buf);
4052 } else if (*p == 'p') {
4053 strcpy (d, pid_buf);
4056 } else if (*p == '%') {
4069 add_to_fd_set (fd_set *set, int fd, int *max_fd)
4072 * This should only trigger for the basic FDs (server socket, pipes) at
4073 * startup if for some mysterious reason they're too large. In this case,
4074 * the profiler really can't function, and we're better off printing an
4075 * error and exiting.
4077 if (fd >= FD_SETSIZE) {
4078 fprintf (stderr, "File descriptor is out of bounds for fd_set: %d\n", fd);
4089 helper_thread (void *arg)
4091 MonoProfiler *prof = (MonoProfiler *) arg;
4093 mono_threads_attach_tools_thread ();
4094 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler helper");
4096 MonoProfilerThread *thread = init_thread (prof, FALSE);
4098 GArray *command_sockets = g_array_new (FALSE, FALSE, sizeof (int));
4106 add_to_fd_set (&rfds, prof->server_socket, &max_fd);
4107 add_to_fd_set (&rfds, prof->pipes [0], &max_fd);
4109 for (gint i = 0; i < command_sockets->len; i++)
4110 add_to_fd_set (&rfds, g_array_index (command_sockets, int, i), &max_fd);
4112 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
4114 // Sleep for 1sec or until a file descriptor has data.
4115 if (select (max_fd + 1, &rfds, NULL, NULL, &tv) == -1) {
4119 fprintf (stderr, "Error in mono-profiler-log server: %s", strerror (errno));
4124 counters_and_perfcounters_sample (prof);
4126 buffer_lock_excl ();
4128 sync_point (SYNC_POINT_PERIODIC);
4130 buffer_unlock_excl ();
4132 // Are we shutting down?
4133 if (FD_ISSET (prof->pipes [0], &rfds)) {
4135 read (prof->pipes [0], &c, 1);
4139 for (gint i = 0; i < command_sockets->len; i++) {
4140 int fd = g_array_index (command_sockets, int, i);
4142 if (!FD_ISSET (fd, &rfds))
4146 int len = read (fd, buf, sizeof (buf) - 1);
4152 // The other end disconnected.
4153 g_array_remove_index (command_sockets, i);
4161 if (!strcmp (buf, "heapshot\n") && hs_mode_ondemand) {
4162 // Rely on the finalization callback triggering a GC.
4163 heapshot_requested = 1;
4164 mono_gc_finalize_notify ();
4168 if (FD_ISSET (prof->server_socket, &rfds)) {
4169 int fd = accept (prof->server_socket, NULL, NULL);
4172 if (fd >= FD_SETSIZE)
4175 g_array_append_val (command_sockets, fd);
4180 for (gint i = 0; i < command_sockets->len; i++)
4181 close (g_array_index (command_sockets, int, i));
4183 g_array_free (command_sockets, TRUE);
4185 send_log_unsafe (FALSE);
4186 deinit_thread (thread);
4188 mono_thread_info_detach ();
4194 start_helper_thread (MonoProfiler* prof)
4196 if (pipe (prof->pipes) == -1) {
4197 fprintf (stderr, "Cannot create pipe: %s\n", strerror (errno));
4201 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
4203 if (prof->server_socket == -1) {
4204 fprintf (stderr, "Cannot create server socket: %s\n", strerror (errno));
4208 struct sockaddr_in server_address;
4210 memset (&server_address, 0, sizeof (server_address));
4211 server_address.sin_family = AF_INET;
4212 server_address.sin_addr.s_addr = INADDR_ANY;
4213 server_address.sin_port = htons (prof->command_port);
4215 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) == -1) {
4216 fprintf (stderr, "Cannot bind server socket on port %d: %s\n", prof->command_port, strerror (errno));
4217 close (prof->server_socket);
4221 if (listen (prof->server_socket, 1) == -1) {
4222 fprintf (stderr, "Cannot listen on server socket: %s\n", strerror (errno));
4223 close (prof->server_socket);
4227 socklen_t slen = sizeof (server_address);
4229 if (getsockname (prof->server_socket, (struct sockaddr *) &server_address, &slen)) {
4230 fprintf (stderr, "Could not get assigned port: %s\n", strerror (errno));
4231 close (prof->server_socket);
4235 prof->command_port = ntohs (server_address.sin_port);
4237 if (!mono_native_thread_create (&prof->helper_thread, helper_thread, prof)) {
4238 fprintf (stderr, "Could not start helper thread\n");
4239 close (prof->server_socket);
4245 free_writer_entry (gpointer p)
4247 mono_lock_free_free (p, WRITER_ENTRY_BLOCK_SIZE);
4251 handle_writer_queue_entry (MonoProfiler *prof)
4253 WriterQueueEntry *entry;
4255 if ((entry = (WriterQueueEntry *) mono_lock_free_queue_dequeue (&prof->writer_queue))) {
4256 if (!entry->methods)
4259 gboolean wrote_methods = FALSE;
4262 * Encode the method events in a temporary log buffer that we
4263 * flush to disk before the main buffer, ensuring that all
4264 * methods have metadata emitted before they're referenced.
4266 * We use a 'proper' thread-local buffer for this as opposed
4267 * to allocating and freeing a buffer by hand because the call
4268 * to mono_method_full_name () below may trigger class load
4269 * events when it retrieves the signature of the method. So a
4270 * thread-local buffer needs to exist when such events occur.
4272 for (guint i = 0; i < entry->methods->len; i++) {
4273 MethodInfo *info = (MethodInfo *) g_ptr_array_index (entry->methods, i);
4275 if (mono_conc_hashtable_lookup (prof->method_table, info->method))
4276 goto free_info; // This method already has metadata emitted.
4279 * Other threads use this hash table to get a general
4280 * idea of whether a method has already been emitted to
4281 * the stream. Due to the way we add to this table, it
4282 * can easily happen that multiple threads queue up the
4283 * same methods, but that's OK since eventually all
4284 * methods will be in this table and the thread-local
4285 * method lists will just be empty for the rest of the
4288 mono_os_mutex_lock (&prof->method_table_mutex);
4289 mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
4290 mono_os_mutex_unlock (&prof->method_table_mutex);
4292 char *name = mono_method_full_name (info->method, 1);
4293 int nlen = strlen (name) + 1;
4294 void *cstart = info->ji ? mono_jit_info_get_code_start (info->ji) : NULL;
4295 int csize = info->ji ? mono_jit_info_get_code_size (info->ji) : 0;
4297 ENTER_LOG (&method_jits_ctr, logbuffer,
4298 EVENT_SIZE /* event */ +
4299 LEB128_SIZE /* method */ +
4300 LEB128_SIZE /* start */ +
4301 LEB128_SIZE /* size */ +
4305 emit_event_time (logbuffer, TYPE_JIT | TYPE_METHOD, info->time);
4306 emit_method_inner (logbuffer, info->method);
4307 emit_ptr (logbuffer, cstart);
4308 emit_value (logbuffer, csize);
4310 memcpy (logbuffer->cursor, name, nlen);
4311 logbuffer->cursor += nlen;
4313 EXIT_LOG_EXPLICIT (NO_SEND);
4317 wrote_methods = TRUE;
4323 g_ptr_array_free (entry->methods, TRUE);
4325 if (wrote_methods) {
4326 MonoProfilerThread *thread = PROF_TLS_GET ();
4328 dump_buffer_threadless (prof, thread->buffer);
4329 init_buffer_state (thread);
4333 dump_buffer (prof, entry->buffer);
4335 mono_thread_hazardous_try_free (entry, free_writer_entry);
4344 writer_thread (void *arg)
4346 MonoProfiler *prof = (MonoProfiler *)arg;
4348 mono_threads_attach_tools_thread ();
4349 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler writer");
4353 MonoProfilerThread *thread = init_thread (prof, FALSE);
4355 while (InterlockedRead (&prof->run_writer_thread)) {
4356 mono_os_sem_wait (&prof->writer_queue_sem, MONO_SEM_FLAGS_NONE);
4357 handle_writer_queue_entry (prof);
4360 /* Drain any remaining entries on shutdown. */
4361 while (handle_writer_queue_entry (prof));
4363 free_buffer (thread->buffer, thread->buffer->size);
4364 deinit_thread (thread);
4366 mono_thread_info_detach ();
4372 start_writer_thread (MonoProfiler* prof)
4374 InterlockedWrite (&prof->run_writer_thread, 1);
4376 if (!mono_native_thread_create (&prof->writer_thread, writer_thread, prof)) {
4377 fprintf (stderr, "Could not start writer thread\n");
4383 reuse_sample_hit (gpointer p)
4385 SampleHit *sample = p;
4387 mono_lock_free_queue_node_unpoison (&sample->node);
4388 mono_lock_free_queue_enqueue (&sample->prof->sample_reuse_queue, &sample->node);
4392 handle_dumper_queue_entry (MonoProfiler *prof)
4396 if ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->dumper_queue))) {
4397 for (int i = 0; i < sample->count; ++i) {
4398 MonoMethod *method = sample->frames [i].method;
4399 MonoDomain *domain = sample->frames [i].domain;
4400 void *address = sample->frames [i].base_address;
4403 g_assert (domain && "What happened to the domain pointer?");
4404 g_assert (address && "What happened to the instruction pointer?");
4406 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *) address);
4409 sample->frames [i].method = mono_jit_info_get_method (ji);
4413 ENTER_LOG (&sample_hits_ctr, logbuffer,
4414 EVENT_SIZE /* event */ +
4415 BYTE_SIZE /* type */ +
4416 LEB128_SIZE /* tid */ +
4417 LEB128_SIZE /* count */ +
4419 LEB128_SIZE /* ip */
4421 LEB128_SIZE /* managed count */ +
4423 LEB128_SIZE /* method */
4427 emit_event_time (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT, sample->time);
4428 emit_byte (logbuffer, SAMPLE_CYCLES);
4429 emit_ptr (logbuffer, (void *) sample->tid);
4430 emit_value (logbuffer, 1);
4432 // TODO: Actual native unwinding.
4433 for (int i = 0; i < 1; ++i) {
4434 emit_ptr (logbuffer, sample->ip);
4435 add_code_pointer ((uintptr_t) sample->ip);
4438 /* new in data version 6 */
4439 emit_uvalue (logbuffer, sample->count);
4441 for (int i = 0; i < sample->count; ++i)
4442 emit_method (logbuffer, sample->frames [i].method);
4444 EXIT_LOG_EXPLICIT (DO_SEND);
4446 mono_thread_hazardous_try_free (sample, reuse_sample_hit);
4448 dump_unmanaged_coderefs (prof);
4455 dumper_thread (void *arg)
4457 MonoProfiler *prof = (MonoProfiler *)arg;
4459 mono_threads_attach_tools_thread ();
4460 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler dumper");
4462 MonoProfilerThread *thread = init_thread (prof, FALSE);
4464 while (InterlockedRead (&prof->run_dumper_thread)) {
4466 * Flush samples every second so it doesn't seem like the profiler is
4467 * not working if the program is mostly idle.
4469 if (mono_os_sem_timedwait (&prof->dumper_queue_sem, 1000, MONO_SEM_FLAGS_NONE) == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT)
4470 send_log_unsafe (FALSE);
4472 handle_dumper_queue_entry (prof);
4475 /* Drain any remaining entries on shutdown. */
4476 while (handle_dumper_queue_entry (prof));
4478 send_log_unsafe (FALSE);
4479 deinit_thread (thread);
4481 mono_thread_info_detach ();
4487 start_dumper_thread (MonoProfiler* prof)
4489 InterlockedWrite (&prof->run_dumper_thread, 1);
4491 if (!mono_native_thread_create (&prof->dumper_thread, dumper_thread, prof)) {
4492 fprintf (stderr, "Could not start dumper thread\n");
4498 register_counter (const char *name, gint32 *counter)
4500 mono_counters_register (name, MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, counter);
4504 runtime_initialized (MonoProfiler *profiler)
4506 InterlockedWrite (&runtime_inited, 1);
4508 register_counter ("Sample events allocated", &sample_allocations_ctr);
4509 register_counter ("Log buffers allocated", &buffer_allocations_ctr);
4511 register_counter ("Event: Sync points", &sync_points_ctr);
4512 register_counter ("Event: Heap objects", &heap_objects_ctr);
4513 register_counter ("Event: Heap starts", &heap_starts_ctr);
4514 register_counter ("Event: Heap ends", &heap_ends_ctr);
4515 register_counter ("Event: Heap roots", &heap_roots_ctr);
4516 register_counter ("Event: GC events", &gc_events_ctr);
4517 register_counter ("Event: GC resizes", &gc_resizes_ctr);
4518 register_counter ("Event: GC allocations", &gc_allocs_ctr);
4519 register_counter ("Event: GC moves", &gc_moves_ctr);
4520 register_counter ("Event: GC handle creations", &gc_handle_creations_ctr);
4521 register_counter ("Event: GC handle deletions", &gc_handle_deletions_ctr);
4522 register_counter ("Event: GC finalize starts", &finalize_begins_ctr);
4523 register_counter ("Event: GC finalize ends", &finalize_ends_ctr);
4524 register_counter ("Event: GC finalize object starts", &finalize_object_begins_ctr);
4525 register_counter ("Event: GC finalize object ends", &finalize_object_ends_ctr);
4526 register_counter ("Event: Image loads", &image_loads_ctr);
4527 register_counter ("Event: Image unloads", &image_unloads_ctr);
4528 register_counter ("Event: Assembly loads", &assembly_loads_ctr);
4529 register_counter ("Event: Assembly unloads", &assembly_unloads_ctr);
4530 register_counter ("Event: Class loads", &class_loads_ctr);
4531 register_counter ("Event: Class unloads", &class_unloads_ctr);
4532 register_counter ("Event: Method entries", &method_entries_ctr);
4533 register_counter ("Event: Method exits", &method_exits_ctr);
4534 register_counter ("Event: Method exception leaves", &method_exception_exits_ctr);
4535 register_counter ("Event: Method JITs", &method_jits_ctr);
4536 register_counter ("Event: Code buffers", &code_buffers_ctr);
4537 register_counter ("Event: Exception throws", &exception_throws_ctr);
4538 register_counter ("Event: Exception clauses", &exception_clauses_ctr);
4539 register_counter ("Event: Monitor events", &monitor_events_ctr);
4540 register_counter ("Event: Thread starts", &thread_starts_ctr);
4541 register_counter ("Event: Thread ends", &thread_ends_ctr);
4542 register_counter ("Event: Thread names", &thread_names_ctr);
4543 register_counter ("Event: Domain loads", &domain_loads_ctr);
4544 register_counter ("Event: Domain unloads", &domain_unloads_ctr);
4545 register_counter ("Event: Domain names", &domain_names_ctr);
4546 register_counter ("Event: Context loads", &context_loads_ctr);
4547 register_counter ("Event: Context unloads", &context_unloads_ctr);
4548 register_counter ("Event: Sample binaries", &sample_ubins_ctr);
4549 register_counter ("Event: Sample symbols", &sample_usyms_ctr);
4550 register_counter ("Event: Sample hits", &sample_hits_ctr);
4551 register_counter ("Event: Counter descriptors", &counter_descriptors_ctr);
4552 register_counter ("Event: Counter samples", &counter_samples_ctr);
4553 register_counter ("Event: Performance counter descriptors", &perfcounter_descriptors_ctr);
4554 register_counter ("Event: Performance counter samples", &perfcounter_samples_ctr);
4555 register_counter ("Event: Coverage methods", &coverage_methods_ctr);
4556 register_counter ("Event: Coverage statements", &coverage_statements_ctr);
4557 register_counter ("Event: Coverage classes", &coverage_classes_ctr);
4558 register_counter ("Event: Coverage assemblies", &coverage_assemblies_ctr);
4560 counters_init (profiler);
4563 * We must start the helper thread before the writer thread. This is
4564 * because the helper thread sets up the command port which is written to
4565 * the log header by the writer thread.
4567 start_helper_thread (profiler);
4568 start_writer_thread (profiler);
4569 start_dumper_thread (profiler);
4573 create_profiler (const char *args, const char *filename, GPtrArray *filters)
4576 int force_delete = 0;
4578 log_profiler = (MonoProfiler *) g_calloc (1, sizeof (MonoProfiler));
4579 log_profiler->args = pstrdup (args);
4580 log_profiler->command_port = command_port;
4582 if (filename && *filename == '-') {
4585 g_warning ("WARNING: the output:-FILENAME option is deprecated, the profiler now always overrides the output file\n");
4588 //If filename begin with +, append the pid at the end
4589 if (filename && *filename == '+')
4590 filename = g_strdup_printf ("%s.%d", filename + 1, getpid ());
4595 filename = "|mprof-report -";
4597 filename = "output.mlpd";
4598 nf = (char*)filename;
4600 nf = new_filename (filename);
4602 int s = strlen (nf) + 32;
4603 char *p = (char *) g_malloc (s);
4604 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
4610 log_profiler->file = popen (nf + 1, "w");
4611 log_profiler->pipe_output = 1;
4612 } else if (*nf == '#') {
4613 int fd = strtol (nf + 1, NULL, 10);
4614 log_profiler->file = fdopen (fd, "a");
4618 log_profiler->file = fopen (nf, "wb");
4620 if (!log_profiler->file) {
4621 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
4625 #if defined (HAVE_SYS_ZLIB)
4627 log_profiler->gzfile = gzdopen (fileno (log_profiler->file), "wb");
4631 * If you hit this assert while increasing MAX_FRAMES, you need to increase
4632 * SAMPLE_BLOCK_SIZE as well.
4634 g_assert (SAMPLE_SLOT_SIZE (MAX_FRAMES) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (SAMPLE_BLOCK_SIZE));
4636 // FIXME: We should free this stuff too.
4637 mono_lock_free_allocator_init_size_class (&log_profiler->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
4638 mono_lock_free_allocator_init_allocator (&log_profiler->sample_allocator, &log_profiler->sample_size_class, MONO_MEM_ACCOUNT_PROFILER);
4640 mono_lock_free_queue_init (&log_profiler->sample_reuse_queue);
4642 g_assert (sizeof (WriterQueueEntry) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (WRITER_ENTRY_BLOCK_SIZE));
4644 // FIXME: We should free this stuff too.
4645 mono_lock_free_allocator_init_size_class (&log_profiler->writer_entry_size_class, sizeof (WriterQueueEntry), WRITER_ENTRY_BLOCK_SIZE);
4646 mono_lock_free_allocator_init_allocator (&log_profiler->writer_entry_allocator, &log_profiler->writer_entry_size_class, MONO_MEM_ACCOUNT_PROFILER);
4648 mono_lock_free_queue_init (&log_profiler->writer_queue);
4649 mono_os_sem_init (&log_profiler->writer_queue_sem, 0);
4651 mono_lock_free_queue_init (&log_profiler->dumper_queue);
4652 mono_os_sem_init (&log_profiler->dumper_queue_sem, 0);
4654 mono_os_mutex_init (&log_profiler->method_table_mutex);
4655 log_profiler->method_table = mono_conc_hashtable_new (NULL, NULL);
4658 coverage_init (log_profiler);
4659 log_profiler->coverage_filters = filters;
4661 log_profiler->startup_time = current_time ();
4665 * declaration to silence the compiler: this is the entry point that
4666 * mono will load from the shared library and call.
4669 mono_profiler_startup (const char *desc);
4672 mono_profiler_startup_log (const char *desc);
4675 * this is the entry point that will be used when the profiler
4676 * is embedded inside the main executable.
4679 mono_profiler_startup_log (const char *desc)
4681 mono_profiler_startup (desc);
4685 mono_profiler_startup (const char *desc)
4687 GPtrArray *filters = NULL;
4689 proflog_parse_args (&config, desc [3] == ':' ? desc + 4 : "");
4691 //XXX maybe later cleanup to use config directly
4692 nocalls = !(config.effective_mask & PROFLOG_CALL_EVENTS);
4693 no_counters = !(config.effective_mask & PROFLOG_COUNTER_EVENTS);
4694 do_report = config.do_report;
4695 do_debug = config.do_debug;
4696 do_heap_shot = (config.effective_mask & PROFLOG_HEAPSHOT_FEATURE);
4697 hs_mode_ondemand = config.hs_mode_ondemand;
4698 hs_mode_ms = config.hs_mode_ms;
4699 hs_mode_gc = config.hs_mode_gc;
4700 do_mono_sample = (config.effective_mask & PROFLOG_SAMPLING_FEATURE);
4701 use_zip = config.use_zip;
4702 command_port = config.command_port;
4703 num_frames = config.num_frames;
4704 notraces = config.notraces;
4705 max_allocated_sample_hits = config.max_allocated_sample_hits;
4706 max_call_depth = config.max_call_depth;
4707 do_coverage = (config.effective_mask & PROFLOG_CODE_COV_FEATURE);
4708 debug_coverage = config.debug_coverage;
4709 only_coverage = config.only_coverage;
4711 if (config.cov_filter_files) {
4712 filters = g_ptr_array_new ();
4714 for (i = 0; i < config.cov_filter_files->len; ++i) {
4715 const char *name = config.cov_filter_files->pdata [i];
4716 parse_cov_filter_file (filters, name);
4724 create_profiler (desc, config.output_filename, filters);
4726 mono_lls_init (&profiler_thread_list, NULL);
4728 //This two events are required for the profiler to work
4729 int events = MONO_PROFILE_THREADS | MONO_PROFILE_GC;
4731 //Required callbacks
4732 mono_profiler_install (log_profiler, log_shutdown);
4733 mono_profiler_install_runtime_initialized (runtime_initialized);
4735 mono_profiler_install_gc (gc_event, gc_resize);
4736 mono_profiler_install_thread (thread_start, thread_end);
4738 //It's questionable whether we actually want this to be mandatory, maybe put it behind the actual event?
4739 mono_profiler_install_thread_name (thread_name);
4742 if (config.effective_mask & PROFLOG_DOMAIN_EVENTS) {
4743 events |= MONO_PROFILE_APPDOMAIN_EVENTS;
4744 mono_profiler_install_appdomain (NULL, domain_loaded, domain_unloaded, NULL);
4745 mono_profiler_install_appdomain_name (domain_name);
4748 if (config.effective_mask & PROFLOG_ASSEMBLY_EVENTS) {
4749 events |= MONO_PROFILE_ASSEMBLY_EVENTS;
4750 mono_profiler_install_assembly (NULL, assembly_loaded, assembly_unloaded, NULL);
4753 if (config.effective_mask & PROFLOG_MODULE_EVENTS) {
4754 events |= MONO_PROFILE_MODULE_EVENTS;
4755 mono_profiler_install_module (NULL, image_loaded, image_unloaded, NULL);
4758 if (config.effective_mask & PROFLOG_CLASS_EVENTS) {
4759 events |= MONO_PROFILE_CLASS_EVENTS;
4760 mono_profiler_install_class (NULL, class_loaded, class_unloaded, NULL);
4763 if (config.effective_mask & PROFLOG_JIT_COMPILATION_EVENTS) {
4764 events |= MONO_PROFILE_JIT_COMPILATION;
4765 mono_profiler_install_jit_end (method_jitted);
4766 mono_profiler_install_code_buffer_new (code_buffer_new);
4769 if (config.effective_mask & PROFLOG_EXCEPTION_EVENTS) {
4770 events |= MONO_PROFILE_EXCEPTIONS;
4771 mono_profiler_install_exception (throw_exc, method_exc_leave, NULL);
4772 mono_profiler_install_exception_clause (clause_exc);
4775 if (config.effective_mask & PROFLOG_ALLOCATION_EVENTS) {
4776 events |= MONO_PROFILE_ALLOCATIONS;
4777 mono_profiler_install_allocation (gc_alloc);
4780 //PROFLOG_GC_EVENTS is mandatory
4781 //PROFLOG_THREAD_EVENTS is mandatory
4783 if (config.effective_mask & PROFLOG_CALL_EVENTS) {
4784 events |= MONO_PROFILE_ENTER_LEAVE;
4785 mono_profiler_install_enter_leave (method_enter, method_leave);
4788 if (config.effective_mask & PROFLOG_INS_COVERAGE_EVENTS) {
4789 events |= MONO_PROFILE_INS_COVERAGE;
4790 mono_profiler_install_coverage_filter (coverage_filter);
4793 //XXX should we check for PROFLOG_SAMPLING_FEATURE instead??
4794 if (config.effective_mask & PROFLOG_SAMPLING_EVENTS) {
4795 events |= MONO_PROFILE_STATISTICAL;
4796 mono_profiler_set_statistical_mode (config.sampling_mode, config.sample_freq);
4797 mono_profiler_install_statistical (mono_sample_hit);
4800 if (config.effective_mask & PROFLOG_MONITOR_EVENTS) {
4801 events |= MONO_PROFILE_MONITOR_EVENTS;
4802 mono_profiler_install_monitor (monitor_event);
4805 if (config.effective_mask & PROFLOG_GC_MOVES_EVENTS) {
4806 events |= MONO_PROFILE_GC_MOVES;
4807 mono_profiler_install_gc_moves (gc_moves);
4810 // TODO split those in two profiler events
4811 if (config.effective_mask & (PROFLOG_GC_ROOT_EVENTS | PROFLOG_GC_HANDLE_EVENTS)) {
4812 events |= MONO_PROFILE_GC_ROOTS;
4813 mono_profiler_install_gc_roots (
4814 config.effective_mask & (PROFLOG_GC_HANDLE_EVENTS) ? gc_handle : NULL,
4815 (config.effective_mask & PROFLOG_GC_ROOT_EVENTS) ? gc_roots : NULL);
4818 if (config.effective_mask & PROFLOG_CONTEXT_EVENTS) {
4819 events |= MONO_PROFILE_CONTEXT_EVENTS;
4820 mono_profiler_install_context (context_loaded, context_unloaded);
4823 if (config.effective_mask & PROFLOG_FINALIZATION_EVENTS) {
4824 events |= MONO_PROFILE_GC_FINALIZATION;
4825 mono_profiler_install_gc_finalize (finalize_begin, finalize_object_begin, finalize_object_end, finalize_end);
4826 } else if (ENABLED (PROFLOG_HEAPSHOT_FEATURE) && config.hs_mode_ondemand) {
4827 //On Demand heapshot uses the finalizer thread to force a collection and thus a heapshot
4828 events |= MONO_PROFILE_GC_FINALIZATION;
4829 mono_profiler_install_gc_finalize (NULL, NULL, NULL, finalize_end);
4832 //PROFLOG_COUNTER_EVENTS is a pseudo event controled by the no_counters global var
4833 //PROFLOG_GC_HANDLE_EVENTS is handled together with PROFLOG_GC_ROOT_EVENTS
4835 mono_profiler_set_events ((MonoProfileFlags)events);