2 * proflog.c: mono log profiler
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2010 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include "../mini/jit.h"
15 #include "../metadata/metadata-internals.h"
16 #include <mono/metadata/profiler.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/mono-gc.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/mono-perfcounters.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/tokentype.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/utils/atomic.h>
26 #include <mono/utils/mono-membar.h>
27 #include <mono/utils/mono-mmap.h>
28 #include <mono/utils/mono-counters.h>
29 #include <mono/utils/mono-os-mutex.h>
30 #include <mono/utils/mono-os-semaphore.h>
31 #include <mono/utils/mono-conc-hashtable.h>
32 #include <mono/utils/lock-free-alloc.h>
33 #include <mono/utils/lock-free-queue.h>
34 #include <mono/utils/hazard-pointer.h>
35 #include <mono/utils/mono-threads.h>
43 #ifdef HAVE_SCHED_GETAFFINITY
48 #if defined(HOST_WIN32) || defined(DISABLE_SOCKETS)
49 #define DISABLE_HELPER_THREAD 1
58 #ifdef HAVE_EXECINFO_H
65 #ifndef DISABLE_HELPER_THREAD
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <netinet/in.h>
69 #include <sys/select.h>
78 #ifdef HAVE_SYS_STAT_H
85 #if defined (HAVE_SYS_ZLIB)
89 #if defined(__linux__)
92 #include <sys/syscall.h>
94 #ifdef ENABLE_PERF_EVENTS
95 #include <linux/perf_event.h>
97 #define USE_PERF_EVENTS 1
99 static int read_perf_mmap (MonoProfiler* prof, int cpu);
104 #define BUFFER_SIZE (4096 * 16)
106 /* Worst-case size in bytes of a 64-bit value encoded with LEB128. */
107 #define LEB128_SIZE 10
108 /* Size in bytes of the event ID prefix. */
111 static int nocalls = 0;
112 static int notraces = 0;
113 static int use_zip = 0;
114 static int do_report = 0;
115 static int do_heap_shot = 0;
116 static int max_call_depth = 100;
117 static volatile int runtime_inited = 0;
118 static int need_helper_thread = 0;
119 static int command_port = 0;
120 static int heapshot_requested = 0;
121 static int sample_type = 0;
122 static int sample_freq = 0;
123 static int do_mono_sample = 0;
124 static int in_shutdown = 0;
125 static int do_debug = 0;
126 static int do_counters = 0;
127 static int do_coverage = 0;
128 static gboolean debug_coverage = FALSE;
129 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
130 static int max_allocated_sample_hits;
132 static gint32 sample_hits;
133 static gint32 sample_flushes;
134 static gint32 sample_allocations;
135 static gint32 buffer_allocations;
136 static gint32 thread_starts;
137 static gint32 thread_ends;
138 static gint32 domain_loads;
139 static gint32 domain_unloads;
140 static gint32 context_loads;
141 static gint32 context_unloads;
142 static gint32 assembly_loads;
143 static gint32 assembly_unloads;
144 static gint32 image_loads;
145 static gint32 image_unloads;
146 static gint32 class_loads;
147 static gint32 class_unloads;
149 typedef struct _LogBuffer LogBuffer;
155 * The file is composed by a header followed by 0 or more buffers.
156 * Each buffer contains events that happened on a thread: for a given thread
157 * buffers that appear later in the file are guaranteed to contain events
158 * that happened later in time. Buffers from separate threads could be interleaved,
160 * Buffers are not required to be aligned.
163 * [id: 4 bytes] constant value: LOG_HEADER_ID
164 * [major: 1 byte] [minor: 1 byte] major and minor version of the log profiler
165 * [format: 1 byte] version of the data format for the rest of the file
166 * [ptrsize: 1 byte] size in bytes of a pointer in the profiled program
167 * [startup time: 8 bytes] time in milliseconds since the unix epoch when the program started
168 * [timer overhead: 4 bytes] approximate overhead in nanoseconds of the timer
169 * [flags: 4 bytes] file format flags, should be 0 for now
170 * [pid: 4 bytes] pid of the profiled process
171 * [port: 2 bytes] tcp port for server if != 0
172 * [sysid: 2 bytes] operating system and architecture identifier
174 * The multiple byte integers are in little-endian format.
177 * [buffer header] [event]*
178 * Buffers have a fixed-size header followed by 0 or more bytes of event data.
179 * Timing information and other values in the event data are usually stored
180 * as uleb128 or sleb128 integers. To save space, as noted for each item below,
181 * some data is represented as a difference between the actual value and
182 * either the last value of the same type (like for timing information) or
183 * as the difference from a value stored in a buffer header.
185 * For timing information the data is stored as uleb128, since timing
186 * increases in a monotonic way in each thread: the value is the number of
187 * nanoseconds to add to the last seen timing data in a buffer. The first value
188 * in a buffer will be calculated from the time_base field in the buffer head.
190 * Object or heap sizes are stored as uleb128.
191 * Pointer differences are stored as sleb128, instead.
193 * If an unexpected value is found, the rest of the buffer should be ignored,
194 * as generally the later values need the former to be interpreted correctly.
196 * buffer header format:
197 * [bufid: 4 bytes] constant value: BUF_ID
198 * [len: 4 bytes] size of the data following the buffer header
199 * [time_base: 8 bytes] time base in nanoseconds since an unspecified epoch
200 * [ptr_base: 8 bytes] base value for pointers
201 * [obj_base: 8 bytes] base value for object addresses
202 * [thread id: 8 bytes] system-specific thread ID (pthread_t for example)
203 * [method_base: 8 bytes] base value for MonoMethod pointers
206 * [extended info: upper 4 bits] [type: lower 4 bits] [data]*
207 * The data that follows depends on type and the extended info.
208 * Type is one of the enum values in proflog.h: TYPE_ALLOC, TYPE_GC,
209 * TYPE_METADATA, TYPE_METHOD, TYPE_EXCEPTION, TYPE_MONITOR, TYPE_HEAP.
210 * The extended info bits are interpreted based on type, see
211 * each individual event description below.
212 * strings are represented as a 0-terminated utf8 sequence.
215 * [flags: uleb128] must be 0
216 * [num: uleb128] number of frames following
217 * [frame: sleb128]* num MonoMethod pointers as differences from ptr_base
221 * exinfo: flags: TYPE_ALLOC_BT
222 * [time diff: uleb128] nanoseconds since last timing
223 * [ptr: sleb128] class as a byte difference from ptr_base
224 * [obj: sleb128] object address as a byte difference from obj_base
225 * [size: uleb128] size of the object in the heap
226 * If the TYPE_ALLOC_BT flag is set, a backtrace follows.
230 * exinfo: one of TYPE_GC_EVENT, TYPE_GC_RESIZE, TYPE_GC_MOVE, TYPE_GC_HANDLE_CREATED[_BT],
231 * TYPE_GC_HANDLE_DESTROYED[_BT]
232 * [time diff: uleb128] nanoseconds since last timing
233 * if exinfo == TYPE_GC_RESIZE
234 * [heap_size: uleb128] new heap size
235 * if exinfo == TYPE_GC_EVENT
236 * [event type: uleb128] GC event (MONO_GC_EVENT_* from profiler.h)
237 * [generation: uleb128] GC generation event refers to
238 * if exinfo == TYPE_GC_MOVE
239 * [num_objects: uleb128] number of object moves that follow
240 * [objaddr: sleb128]+ num_objects object pointer differences from obj_base
241 * num is always an even number: the even items are the old
242 * addresses, the odd numbers are the respective new object addresses
243 * if exinfo == TYPE_GC_HANDLE_CREATED[_BT]
244 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
245 * upper bits reserved as flags
246 * [handle: uleb128] GC handle value
247 * [objaddr: sleb128] object pointer differences from obj_base
248 * If exinfo == TYPE_GC_HANDLE_CREATED_BT, a backtrace follows.
249 * if exinfo == TYPE_GC_HANDLE_DESTROYED[_BT]
250 * [handle_type: uleb128] GC handle type (System.Runtime.InteropServices.GCHandleType)
251 * upper bits reserved as flags
252 * [handle: uleb128] GC handle value
253 * If exinfo == TYPE_GC_HANDLE_DESTROYED_BT, a backtrace follows.
255 * type metadata format:
256 * type: TYPE_METADATA
257 * exinfo: one of: TYPE_END_LOAD, TYPE_END_UNLOAD (optional for TYPE_THREAD and TYPE_DOMAIN)
258 * [time diff: uleb128] nanoseconds since last timing
259 * [mtype: byte] metadata type, one of: TYPE_CLASS, TYPE_IMAGE, TYPE_ASSEMBLY, TYPE_DOMAIN,
260 * TYPE_THREAD, TYPE_CONTEXT
261 * [pointer: sleb128] pointer of the metadata type depending on mtype
262 * if mtype == TYPE_CLASS
263 * [image: sleb128] MonoImage* as a pointer difference from ptr_base
264 * [flags: uleb128] must be 0
265 * [name: string] full class name
266 * if mtype == TYPE_IMAGE
267 * [flags: uleb128] must be 0
268 * [name: string] image file name
269 * if mtype == TYPE_ASSEMBLY
270 * [flags: uleb128] must be 0
271 * [name: string] assembly name
272 * if mtype == TYPE_DOMAIN
273 * [flags: uleb128] must be 0
274 * if mtype == TYPE_DOMAIN && exinfo == 0
275 * [name: string] domain friendly name
276 * if mtype == TYPE_CONTEXT
277 * [flags: uleb128] must be 0
278 * [domain: sleb128] domain id as pointer
279 * if mtype == TYPE_THREAD && (format_version < 11 || (format_version > 10 && exinfo == 0))
280 * [flags: uleb128] must be 0
281 * [name: string] thread name
283 * type method format:
285 * exinfo: one of: TYPE_LEAVE, TYPE_ENTER, TYPE_EXC_LEAVE, TYPE_JIT
286 * [time diff: uleb128] nanoseconds since last timing
287 * [method: sleb128] MonoMethod* as a pointer difference from the last such
288 * pointer or the buffer method_base
289 * if exinfo == TYPE_JIT
290 * [code address: sleb128] pointer to the native code as a diff from ptr_base
291 * [code size: uleb128] size of the generated code
292 * [name: string] full method name
294 * type runtime format:
296 * exinfo: one of: TYPE_JITHELPER
297 * [time diff: uleb128] nanoseconds since last timing
298 * if exinfo == TYPE_JITHELPER
299 * [type: uleb128] MonoProfilerCodeBufferType enum value
300 * [buffer address: sleb128] pointer to the native code as a diff from ptr_base
301 * [buffer size: uleb128] size of the generated code
302 * if type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
303 * [name: string] buffer description name
305 * type monitor format:
307 * exinfo: TYPE_MONITOR_BT flag and one of: MONO_PROFILER_MONITOR_(CONTENTION|FAIL|DONE)
308 * [time diff: uleb128] nanoseconds since last timing
309 * [object: sleb128] the lock object as a difference from obj_base
310 * if exinfo.low3bits == MONO_PROFILER_MONITOR_CONTENTION
311 * If the TYPE_MONITOR_BT flag is set, a backtrace follows.
315 * exinfo: one of TYPE_HEAP_START, TYPE_HEAP_END, TYPE_HEAP_OBJECT, TYPE_HEAP_ROOT
316 * if exinfo == TYPE_HEAP_START
317 * [time diff: uleb128] nanoseconds since last timing
318 * if exinfo == TYPE_HEAP_END
319 * [time diff: uleb128] nanoseconds since last timing
320 * if exinfo == TYPE_HEAP_OBJECT
321 * [object: sleb128] the object as a difference from obj_base
322 * [class: sleb128] the object MonoClass* as a difference from ptr_base
323 * [size: uleb128] size of the object on the heap
324 * [num_refs: uleb128] number of object references
325 * if (format version > 1) each referenced objref is preceded by a
326 * uleb128 encoded offset: the first offset is from the object address
327 * and each next offset is relative to the previous one
328 * [objrefs: sleb128]+ object referenced as a difference from obj_base
329 * The same object can appear multiple times, but only the first time
330 * with size != 0: in the other cases this data will only be used to
331 * provide additional referenced objects.
332 * if exinfo == TYPE_HEAP_ROOT
333 * [num_roots: uleb128] number of root references
334 * [num_gc: uleb128] number of major gcs
335 * [object: sleb128] the object as a difference from obj_base
336 * [root_type: uleb128] the root_type: MonoProfileGCRootType (profiler.h)
337 * [extra_info: uleb128] the extra_info value
338 * object, root_type and extra_info are repeated num_roots times
342 * exinfo: one of TYPE_SAMPLE_HIT, TYPE_SAMPLE_USYM, TYPE_SAMPLE_UBIN, TYPE_SAMPLE_COUNTERS_DESC, TYPE_SAMPLE_COUNTERS
343 * if exinfo == TYPE_SAMPLE_HIT
344 * [sample_type: uleb128] type of sample (SAMPLE_*)
345 * [timestamp: uleb128] nanoseconds since startup (note: different from other timestamps!)
346 * if (format_version > 10)
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 * if (format_version > 5)
351 * [mbt_count: uleb128] number of managed backtrace info triplets (method + IL offset + native offset)
352 * [method: sleb128]* MonoMethod* as a pointer difference from the last such
353 * pointer or the buffer method_base (the first such method can be also indentified by ip, but this is not neccessarily true)
354 * [il_offset: sleb128]* IL offset inside method where the hit occurred
355 * [native_offset: sleb128]* native offset inside method where the hit occurred
356 * if exinfo == TYPE_SAMPLE_USYM
357 * [address: sleb128] symbol address as a difference from ptr_base
358 * [size: uleb128] symbol size (may be 0 if unknown)
359 * [name: string] symbol name
360 * if exinfo == TYPE_SAMPLE_UBIN
361 * [time diff: uleb128] nanoseconds since last timing
362 * [address: sleb128] address where binary has been loaded
363 * [offset: uleb128] file offset of mapping (the same file can be mapped multiple times)
364 * [size: uleb128] memory size
365 * [name: string] binary name
366 * if exinfo == TYPE_SAMPLE_COUNTERS_DESC
367 * [len: uleb128] number of counters
369 * [section: uleb128] section of counter
370 * if section == MONO_COUNTER_PERFCOUNTERS:
371 * [section_name: string] section name of counter
372 * [name: string] name of counter
373 * [type: uleb128] type of counter
374 * [unit: uleb128] unit of counter
375 * [variance: uleb128] variance of counter
376 * [index: uleb128] unique index of counter
377 * if exinfo == TYPE_SAMPLE_COUNTERS
378 * [timestamp: uleb128] sampling timestamp
380 * [index: uleb128] unique index of counter
383 * [type: uleb128] type of counter value
386 * [0: uleb128] 0 -> value is null
388 * [1: uleb128] 1 -> value is not null
389 * [value: string] counter value
391 * [value: uleb128/sleb128/double] counter value, can be sleb128, uleb128 or double (determined by using type)
393 * type coverage format
394 * type: TYPE_COVERAGE
395 * exinfo: one of TYPE_COVERAGE_METHOD, TYPE_COVERAGE_STATEMENT, TYPE_COVERAGE_ASSEMBLY, TYPE_COVERAGE_CLASS
396 * if exinfo == TYPE_COVERAGE_METHOD
397 * [assembly: string] name of assembly
398 * [class: string] name of the class
399 * [name: string] name of the method
400 * [signature: string] the signature of the method
401 * [filename: string] the file path of the file that contains this method
402 * [token: uleb128] the method token
403 * [method_id: uleb128] an ID for this data to associate with the buffers of TYPE_COVERAGE_STATEMENTS
404 * [len: uleb128] the number of TYPE_COVERAGE_BUFFERS associated with this method
405 * if exinfo == TYPE_COVERAGE_STATEMENTS
406 * [method_id: uleb128] an the TYPE_COVERAGE_METHOD buffer to associate this with
407 * [offset: uleb128] the il offset relative to the previous offset
408 * [counter: uleb128] the counter for this instruction
409 * [line: uleb128] the line of filename containing this instruction
410 * [column: uleb128] the column containing this instruction
411 * if exinfo == TYPE_COVERAGE_ASSEMBLY
412 * [name: string] assembly name
413 * [guid: string] assembly GUID
414 * [filename: string] assembly filename
415 * [number_of_methods: uleb128] the number of methods in this assembly
416 * [fully_covered: uleb128] the number of fully covered methods
417 * [partially_covered: uleb128] the number of partially covered methods
418 * currently partially_covered will always be 0, and fully_covered is the
419 * number of methods that are fully and partially covered.
420 * if exinfo == TYPE_COVERAGE_CLASS
421 * [name: string] assembly name
422 * [class: string] class name
423 * [number_of_methods: uleb128] the number of methods in this class
424 * [fully_covered: uleb128] the number of fully covered methods
425 * [partially_covered: uleb128] the number of partially covered methods
426 * currently partially_covered will always be 0, and fully_covered is the
427 * number of methods that are fully and partially covered.
431 * Format oddities that we ought to fix:
433 * - Methods written in emit_bt () should be based on the buffer's base
434 * method instead of the base pointer.
435 * - The TYPE_SAMPLE_HIT event contains (currently) pointless data like
436 * always-one unmanaged frame count and always-zero IL offsets.
438 * These are mostly small things and are not worth a format change by
439 * themselves. They should be done when some other major change has to
440 * be done to the format.
443 // Pending data to be written to the log, for a single thread.
444 // Threads periodically flush their own LogBuffers by calling safe_send
446 // Next (older) LogBuffer in processing queue
452 uintptr_t method_base;
453 uintptr_t last_method;
459 // Bytes allocated for this LogBuffer
462 // Start of currently unused space in buffer
463 unsigned char* cursor;
465 // Pointer to start-of-structure-plus-size (for convenience)
466 unsigned char* buf_end;
468 // Start of data in buffer. Contents follow "buffer format" described above.
469 unsigned char buf [1];
473 ign_res (int G_GNUC_UNUSED unused, ...)
477 #define ENTER_LOG(lb,str) if ((lb)->locked) {ign_res (write(2, str, strlen(str))); ign_res (write(2, "\n", 1));return;} else {(lb)->locked++;}
478 #define EXIT_LOG(lb) (lb)->locked--;
480 typedef struct _BinaryObject BinaryObject;
482 struct _BinaryObject {
488 struct _MonoProfiler {
490 #if defined (HAVE_SYS_ZLIB)
493 uint64_t startup_time;
495 int last_gc_gen_started;
500 pthread_t helper_thread;
501 pthread_t writer_thread;
502 pthread_t dumper_thread;
504 volatile gint32 run_writer_thread;
505 MonoLockFreeQueue writer_queue;
506 MonoSemType writer_queue_sem;
507 MonoConcurrentHashTable *method_table;
508 mono_mutex_t method_table_mutex;
509 volatile gint32 run_dumper_thread;
510 MonoLockFreeQueue dumper_queue;
511 MonoSemType dumper_queue_sem;
512 MonoLockFreeAllocSizeClass sample_size_class;
513 MonoLockFreeAllocator sample_allocator;
514 MonoLockFreeQueue sample_reuse_queue;
515 BinaryObject *binary_objects;
516 GPtrArray *coverage_filters;
519 typedef struct _WriterQueueEntry WriterQueueEntry;
520 struct _WriterQueueEntry {
521 MonoLockFreeQueueNode node;
526 typedef struct _MethodInfo MethodInfo;
538 #define TLS_SET(x,y) (TlsSetValue (x, y))
539 #define TLS_GET(t,x) ((t *) TlsGetValue (x))
540 #define TLS_INIT(x) (x = TlsAlloc ())
541 static int tlsbuffer;
542 static int tlsmethodlist;
544 #define TLS_SET(x,y) (x = y)
545 #define TLS_GET(t,x) (x)
547 static __thread LogBuffer* tlsbuffer = NULL;
548 static __thread GPtrArray* tlsmethodlist = NULL;
550 #define TLS_SET(x,y) (pthread_setspecific (x, y))
551 #define TLS_GET(t,x) ((t *) pthread_getspecific (x))
552 #define TLS_INIT(x) (pthread_key_create (&x, NULL))
553 static pthread_key_t tlsbuffer;
554 static pthread_key_t tlsmethodlist;
557 static void safe_send (MonoProfiler *profiler, LogBuffer *logbuffer);
560 pstrdup (const char *s)
562 int len = strlen (s) + 1;
563 char *p = (char *)malloc (len);
571 LogBuffer* buf = (LogBuffer *)alloc_buffer (BUFFER_SIZE);
573 InterlockedIncrement (&buffer_allocations);
575 buf->size = BUFFER_SIZE;
576 buf->time_base = current_time ();
577 buf->last_time = buf->time_base;
578 buf->buf_end = (unsigned char*)buf + buf->size;
579 buf->cursor = buf->buf;
586 if (!TLS_GET (LogBuffer, tlsbuffer)) {
587 LogBuffer *logbuffer = create_buffer ();
588 TLS_SET (tlsbuffer, logbuffer);
589 logbuffer->thread_id = thread_id ();
591 if (!TLS_GET (GPtrArray, tlsmethodlist)) {
592 GPtrArray *methodlist = g_ptr_array_new ();
593 TLS_SET (tlsmethodlist, methodlist);
596 //printf ("thread %p at time %llu\n", (void*)logbuffer->thread_id, logbuffer->time_base);
600 ensure_logbuf_inner (LogBuffer *old, int bytes)
602 if (old && old->cursor + bytes + 100 < old->buf_end)
605 LogBuffer *new_ = (LogBuffer *)create_buffer ();
606 new_->thread_id = thread_id ();
610 new_->call_depth = old->call_depth;
616 ensure_logbuf (int bytes)
618 LogBuffer *old = TLS_GET (LogBuffer, tlsbuffer);
619 LogBuffer *new_ = ensure_logbuf_inner (old, bytes);
622 return old; // Still enough space.
624 TLS_SET (tlsbuffer, new_);
631 emit_byte (LogBuffer *logbuffer, int value)
633 logbuffer->cursor [0] = value;
635 assert (logbuffer->cursor <= logbuffer->buf_end);
639 emit_value (LogBuffer *logbuffer, int value)
641 encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
642 assert (logbuffer->cursor <= logbuffer->buf_end);
646 emit_time (LogBuffer *logbuffer, uint64_t value)
648 uint64_t tdiff = value - logbuffer->last_time;
649 //if (value < logbuffer->last_time)
650 // printf ("time went backwards\n");
651 //if (tdiff > 1000000)
652 // printf ("large time offset: %llu\n", tdiff);
653 encode_uleb128 (tdiff, logbuffer->cursor, &logbuffer->cursor);
654 /*if (tdiff != decode_uleb128 (p, &p))
655 printf ("incorrect encoding: %llu\n", tdiff);*/
656 logbuffer->last_time = value;
657 assert (logbuffer->cursor <= logbuffer->buf_end);
661 emit_svalue (LogBuffer *logbuffer, int64_t value)
663 encode_sleb128 (value, logbuffer->cursor, &logbuffer->cursor);
664 assert (logbuffer->cursor <= logbuffer->buf_end);
668 emit_uvalue (LogBuffer *logbuffer, uint64_t value)
670 encode_uleb128 (value, logbuffer->cursor, &logbuffer->cursor);
671 assert (logbuffer->cursor <= logbuffer->buf_end);
675 emit_ptr (LogBuffer *logbuffer, void *ptr)
677 if (!logbuffer->ptr_base)
678 logbuffer->ptr_base = (uintptr_t)ptr;
679 emit_svalue (logbuffer, (intptr_t)ptr - logbuffer->ptr_base);
680 assert (logbuffer->cursor <= logbuffer->buf_end);
684 emit_method_inner (LogBuffer *logbuffer, void *method)
686 if (!logbuffer->method_base) {
687 logbuffer->method_base = (intptr_t)method;
688 logbuffer->last_method = (intptr_t)method;
690 encode_sleb128 ((intptr_t)((char*)method - (char*)logbuffer->last_method), logbuffer->cursor, &logbuffer->cursor);
691 logbuffer->last_method = (intptr_t)method;
692 assert (logbuffer->cursor <= logbuffer->buf_end);
702 find_method (MonoDomain *domain, void *user_data)
704 MethodSearch *search = user_data;
709 MonoJitInfo *ji = mono_get_jit_info_from_method (domain, search->method);
711 // It could be AOT'd, so we need to get it from the AOT runtime's cache.
713 void *ip = mono_aot_get_method (domain, search->method);
715 // Avoid a slow path in mono_jit_info_table_find ().
717 ji = mono_jit_info_table_find (domain, ip);
726 register_method_local (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji)
728 if (!mono_conc_hashtable_lookup (prof->method_table, method)) {
730 * FIXME: In some cases, we crash while looking up JIT info for AOT'd methods.
731 * This usually happens for static constructors. This code is disabled for now
732 * as we don't need this info for anything critical.
734 * https://bugzilla.xamarin.com/show_bug.cgi?id=35171
738 MethodSearch search = { method, NULL };
740 mono_domain_foreach (find_method, &search);
747 * FIXME: We can't always find JIT info for a generic shared method, especially
748 * if we obtained the MonoMethod during an async stack walk. For now, we deal
749 * with this by giving the generic shared method name and dummy code start/size
750 * information (i.e. zeroes).
754 MethodInfo *info = (MethodInfo *)malloc (sizeof (MethodInfo));
756 info->method = method;
758 info->time = current_time ();
760 g_ptr_array_add (TLS_GET (GPtrArray, tlsmethodlist), info);
765 emit_method (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
767 register_method_local (prof, method, NULL);
768 emit_method_inner (logbuffer, method);
772 emit_method_as_ptr (MonoProfiler *prof, LogBuffer *logbuffer, MonoMethod *method)
774 register_method_local (prof, method, NULL);
775 emit_ptr (logbuffer, method);
779 emit_obj (LogBuffer *logbuffer, void *ptr)
781 if (!logbuffer->obj_base)
782 logbuffer->obj_base = (uintptr_t)ptr >> 3;
783 emit_svalue (logbuffer, ((uintptr_t)ptr >> 3) - logbuffer->obj_base);
784 assert (logbuffer->cursor <= logbuffer->buf_end);
788 emit_string (LogBuffer *logbuffer, const char *str, size_t size)
792 for (; i < size; i++) {
795 emit_byte (logbuffer, str [i]);
798 emit_byte (logbuffer, '\0');
802 emit_double (LogBuffer *logbuffer, double value)
805 unsigned char buffer[8];
806 memcpy (buffer, &value, 8);
807 #if G_BYTE_ORDER == G_BIG_ENDIAN
808 for (i = 7; i >= 0; i--)
810 for (i = 0; i < 8; i++)
812 emit_byte (logbuffer, buffer[i]);
816 write_int16 (char *buf, int32_t value)
819 for (i = 0; i < 2; ++i) {
827 write_int32 (char *buf, int32_t value)
830 for (i = 0; i < 4; ++i) {
838 write_int64 (char *buf, int64_t value)
841 for (i = 0; i < 8; ++i) {
849 dump_header (MonoProfiler *profiler)
853 p = write_int32 (p, LOG_HEADER_ID);
854 *p++ = LOG_VERSION_MAJOR;
855 *p++ = LOG_VERSION_MINOR;
856 *p++ = LOG_DATA_VERSION;
857 *p++ = sizeof (void*);
858 p = write_int64 (p, ((uint64_t)time (NULL)) * 1000); /* startup time */
859 p = write_int32 (p, get_timer_overhead ()); /* timer overhead */
860 p = write_int32 (p, 0); /* flags */
861 p = write_int32 (p, process_id ()); /* pid */
862 p = write_int16 (p, profiler->command_port); /* port */
863 p = write_int16 (p, 0); /* opsystem */
864 #if defined (HAVE_SYS_ZLIB)
865 if (profiler->gzfile) {
866 gzwrite (profiler->gzfile, hbuf, p - hbuf);
868 fwrite (hbuf, p - hbuf, 1, profiler->file);
871 fwrite (hbuf, p - hbuf, 1, profiler->file);
872 fflush (profiler->file);
877 send_buffer (MonoProfiler *prof, GPtrArray *methods, LogBuffer *buffer)
879 WriterQueueEntry *entry = (WriterQueueEntry *)calloc (1, sizeof (WriterQueueEntry));
880 mono_lock_free_queue_node_init (&entry->node, FALSE);
881 entry->methods = methods;
882 entry->buffer = buffer;
883 mono_lock_free_queue_enqueue (&prof->writer_queue, &entry->node);
884 mono_os_sem_post (&prof->writer_queue_sem);
888 dump_buffer (MonoProfiler *profiler, LogBuffer *buf)
893 dump_buffer (profiler, buf->next);
894 p = write_int32 (p, BUF_ID);
895 p = write_int32 (p, buf->cursor - buf->buf);
896 p = write_int64 (p, buf->time_base);
897 p = write_int64 (p, buf->ptr_base);
898 p = write_int64 (p, buf->obj_base);
899 p = write_int64 (p, buf->thread_id);
900 p = write_int64 (p, buf->method_base);
901 #if defined (HAVE_SYS_ZLIB)
902 if (profiler->gzfile) {
903 gzwrite (profiler->gzfile, hbuf, p - hbuf);
904 gzwrite (profiler->gzfile, buf->buf, buf->cursor - buf->buf);
907 fwrite (hbuf, p - hbuf, 1, profiler->file);
908 fwrite (buf->buf, buf->cursor - buf->buf, 1, profiler->file);
909 fflush (profiler->file);
910 #if defined (HAVE_SYS_ZLIB)
913 free_buffer (buf, buf->size);
917 process_requests (MonoProfiler *profiler)
919 if (heapshot_requested)
920 mono_gc_collect (mono_gc_max_generation ());
923 static void counters_init (MonoProfiler *profiler);
924 static void counters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless);
927 * Can be called only at safe callback locations.
930 safe_send (MonoProfiler *profiler, LogBuffer *logbuffer)
932 /* We need the runtime initialized so that we have threads and hazard
933 * pointers available. Otherwise, the lock free queue will not work and
934 * there won't be a thread to process the data.
936 * While the runtime isn't initialized, we just accumulate data in the
937 * thread local buffer list.
939 if (!InterlockedRead (&runtime_inited))
942 int cd = logbuffer->call_depth;
944 send_buffer (profiler, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
946 TLS_SET (tlsbuffer, NULL);
947 TLS_SET (tlsmethodlist, NULL);
951 TLS_GET (LogBuffer, tlsbuffer)->call_depth = cd;
955 safe_send_threadless (MonoProfiler *prof, LogBuffer *buf)
957 for (LogBuffer *iter = buf; iter; iter = iter->next)
960 safe_send (prof, buf);
964 gc_reference (MonoObject *obj, MonoClass *klass, uintptr_t size, uintptr_t num, MonoObject **refs, uintptr_t *offsets, void *data)
967 uintptr_t last_offset = 0;
968 //const char *name = mono_class_get_name (klass);
969 LogBuffer *logbuffer = ensure_logbuf (
970 EVENT_SIZE /* event */ +
971 LEB128_SIZE /* obj */ +
972 LEB128_SIZE /* klass */ +
973 LEB128_SIZE /* size */ +
974 LEB128_SIZE /* num */ +
976 LEB128_SIZE /* offset */ +
977 LEB128_SIZE /* ref */
980 emit_byte (logbuffer, TYPE_HEAP_OBJECT | TYPE_HEAP);
981 emit_obj (logbuffer, obj);
982 emit_ptr (logbuffer, klass);
983 /* account for object alignment in the heap */
986 emit_value (logbuffer, size);
987 emit_value (logbuffer, num);
988 for (i = 0; i < num; ++i) {
989 emit_value (logbuffer, offsets [i] - last_offset);
990 last_offset = offsets [i];
991 emit_obj (logbuffer, refs [i]);
994 // printf ("obj: %p, klass: %s, refs: %d, size: %d\n", obj, name, (int)num, (int)size);
998 static unsigned int hs_mode_ms = 0;
999 static unsigned int hs_mode_gc = 0;
1000 static unsigned int hs_mode_ondemand = 0;
1001 static unsigned int gc_count = 0;
1002 static uint64_t last_hs_time = 0;
1005 heap_walk (MonoProfiler *profiler)
1009 LogBuffer *logbuffer;
1012 logbuffer = ensure_logbuf (
1013 EVENT_SIZE /* event */ +
1014 LEB128_SIZE /* time */
1016 now = current_time ();
1017 if (hs_mode_ms && (now - last_hs_time)/1000000 >= hs_mode_ms)
1019 else if (hs_mode_gc && (gc_count % hs_mode_gc) == 0)
1021 else if (hs_mode_ondemand)
1022 do_walk = heapshot_requested;
1023 else if (!hs_mode_ms && !hs_mode_gc && profiler->last_gc_gen_started == mono_gc_max_generation ())
1028 heapshot_requested = 0;
1029 emit_byte (logbuffer, TYPE_HEAP_START | TYPE_HEAP);
1030 emit_time (logbuffer, now);
1031 mono_gc_walk_heap (0, gc_reference, NULL);
1032 logbuffer = ensure_logbuf (
1033 EVENT_SIZE /* event */ +
1034 LEB128_SIZE /* time */
1036 now = current_time ();
1037 emit_byte (logbuffer, TYPE_HEAP_END | TYPE_HEAP);
1038 emit_time (logbuffer, now);
1043 gc_event (MonoProfiler *profiler, MonoGCEvent ev, int generation) {
1045 LogBuffer *logbuffer = ensure_logbuf (
1046 EVENT_SIZE /* event */ +
1047 LEB128_SIZE /* time */ +
1048 LEB128_SIZE /* gc event */ +
1049 LEB128_SIZE /* generation */
1051 now = current_time ();
1052 ENTER_LOG (logbuffer, "gcevent");
1053 emit_byte (logbuffer, TYPE_GC_EVENT | TYPE_GC);
1054 emit_time (logbuffer, now);
1055 emit_value (logbuffer, ev);
1056 emit_value (logbuffer, generation);
1057 /* to deal with nested gen1 after gen0 started */
1058 if (ev == MONO_GC_EVENT_START) {
1059 profiler->last_gc_gen_started = generation;
1060 if (generation == mono_gc_max_generation ())
1063 if (ev == MONO_GC_EVENT_PRE_START_WORLD)
1064 heap_walk (profiler);
1065 EXIT_LOG (logbuffer);
1066 if (ev == MONO_GC_EVENT_POST_START_WORLD)
1067 safe_send (profiler, logbuffer);
1068 //printf ("gc event %d for generation %d\n", ev, generation);
1072 gc_resize (MonoProfiler *profiler, int64_t new_size) {
1074 LogBuffer *logbuffer = ensure_logbuf (
1075 EVENT_SIZE /* event */ +
1076 LEB128_SIZE /* time */ +
1077 LEB128_SIZE /* new size */
1079 now = current_time ();
1080 ENTER_LOG (logbuffer, "gcresize");
1081 emit_byte (logbuffer, TYPE_GC_RESIZE | TYPE_GC);
1082 emit_time (logbuffer, now);
1083 emit_value (logbuffer, new_size);
1084 //printf ("gc resized to %lld\n", new_size);
1085 EXIT_LOG (logbuffer);
1088 // If you alter MAX_FRAMES, you may need to alter SAMPLE_BLOCK_SIZE too.
1089 #define MAX_FRAMES 32
1093 MonoMethod* methods [MAX_FRAMES];
1094 int32_t il_offsets [MAX_FRAMES];
1095 int32_t native_offsets [MAX_FRAMES];
1098 static int num_frames = MAX_FRAMES;
1101 walk_stack (MonoMethod *method, int32_t native_offset, int32_t il_offset, mono_bool managed, void* data)
1103 FrameData *frame = (FrameData *)data;
1104 if (method && frame->count < num_frames) {
1105 frame->il_offsets [frame->count] = il_offset;
1106 frame->native_offsets [frame->count] = native_offset;
1107 frame->methods [frame->count++] = method;
1108 //printf ("In %d %s at %d (native: %d)\n", frame->count, mono_method_get_name (method), il_offset, native_offset);
1110 return frame->count == num_frames;
1114 * a note about stack walks: they can cause more profiler events to fire,
1115 * so we need to make sure they don't happen after we started emitting an
1116 * event, hence the collect_bt/emit_bt split.
1119 collect_bt (FrameData *data)
1122 mono_stack_walk_no_il (walk_stack, data);
1126 emit_bt (MonoProfiler *prof, LogBuffer *logbuffer, FrameData *data)
1128 /* FIXME: this is actually tons of data and we should
1129 * just output it the first time and use an id the next
1131 if (data->count > num_frames)
1132 printf ("bad num frames: %d\n", data->count);
1133 emit_value (logbuffer, 0); /* flags */
1134 emit_value (logbuffer, data->count);
1135 //if (*p != data.count) {
1136 // printf ("bad num frames enc at %d: %d -> %d\n", count, data.count, *p); printf ("frames end: %p->%p\n", p, logbuffer->cursor); exit(0);}
1137 while (data->count) {
1138 emit_method_as_ptr (prof, logbuffer, data->methods [--data->count]);
1143 gc_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
1147 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_ALLOC_BT: 0;
1149 LogBuffer *logbuffer;
1150 len = mono_object_get_size (obj);
1151 /* account for object alignment in the heap */
1156 logbuffer = ensure_logbuf (
1157 EVENT_SIZE /* event */ +
1158 LEB128_SIZE /* time */ +
1159 LEB128_SIZE /* klass */ +
1160 LEB128_SIZE /* obj */ +
1161 LEB128_SIZE /* size */ +
1163 LEB128_SIZE /* flags */ +
1164 LEB128_SIZE /* count */ +
1166 LEB128_SIZE /* method */
1170 now = current_time ();
1171 ENTER_LOG (logbuffer, "gcalloc");
1172 emit_byte (logbuffer, do_bt | TYPE_ALLOC);
1173 emit_time (logbuffer, now);
1174 emit_ptr (logbuffer, klass);
1175 emit_obj (logbuffer, obj);
1176 emit_value (logbuffer, len);
1178 emit_bt (prof, logbuffer, &data);
1179 EXIT_LOG (logbuffer);
1180 if (logbuffer->next)
1181 safe_send (prof, logbuffer);
1182 process_requests (prof);
1183 //printf ("gc alloc %s at %p\n", mono_class_get_name (klass), obj);
1187 gc_moves (MonoProfiler *prof, void **objects, int num)
1191 LogBuffer *logbuffer = ensure_logbuf (
1192 EVENT_SIZE /* event */ +
1193 LEB128_SIZE /* time */ +
1194 LEB128_SIZE /* num */ +
1196 LEB128_SIZE /* object */
1199 now = current_time ();
1200 ENTER_LOG (logbuffer, "gcmove");
1201 emit_byte (logbuffer, TYPE_GC_MOVE | TYPE_GC);
1202 emit_time (logbuffer, now);
1203 emit_value (logbuffer, num);
1204 for (i = 0; i < num; ++i)
1205 emit_obj (logbuffer, objects [i]);
1206 //printf ("gc moved %d objects\n", num/2);
1207 EXIT_LOG (logbuffer);
1211 gc_roots (MonoProfiler *prof, int num, void **objects, int *root_types, uintptr_t *extra_info)
1214 LogBuffer *logbuffer = ensure_logbuf (
1215 EVENT_SIZE /* event */ +
1216 LEB128_SIZE /* num */ +
1217 LEB128_SIZE /* collections */ +
1219 LEB128_SIZE /* object */ +
1220 LEB128_SIZE /* root type */ +
1221 LEB128_SIZE /* extra info */
1224 ENTER_LOG (logbuffer, "gcroots");
1225 emit_byte (logbuffer, TYPE_HEAP_ROOT | TYPE_HEAP);
1226 emit_value (logbuffer, num);
1227 emit_value (logbuffer, mono_gc_collection_count (mono_gc_max_generation ()));
1228 for (i = 0; i < num; ++i) {
1229 emit_obj (logbuffer, objects [i]);
1230 emit_value (logbuffer, root_types [i]);
1231 emit_value (logbuffer, extra_info [i]);
1233 EXIT_LOG (logbuffer);
1237 gc_handle (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj)
1239 int do_bt = nocalls && InterlockedRead (&runtime_inited) && !notraces;
1246 LogBuffer *logbuffer = ensure_logbuf (
1247 EVENT_SIZE /* event */ +
1248 LEB128_SIZE /* time */ +
1249 LEB128_SIZE /* type */ +
1250 LEB128_SIZE /* handle */ +
1251 (op == MONO_PROFILER_GC_HANDLE_CREATED ? (
1252 LEB128_SIZE /* obj */
1255 LEB128_SIZE /* flags */ +
1256 LEB128_SIZE /* count */ +
1258 LEB128_SIZE /* method */
1263 now = current_time ();
1264 ENTER_LOG (logbuffer, "gchandle");
1266 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1267 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_CREATED_BT : TYPE_GC_HANDLE_CREATED) | TYPE_GC);
1268 else if (op == MONO_PROFILER_GC_HANDLE_DESTROYED)
1269 emit_byte (logbuffer, (do_bt ? TYPE_GC_HANDLE_DESTROYED_BT : TYPE_GC_HANDLE_DESTROYED) | TYPE_GC);
1271 g_assert_not_reached ();
1273 emit_time (logbuffer, now);
1274 emit_value (logbuffer, type);
1275 emit_value (logbuffer, handle);
1277 if (op == MONO_PROFILER_GC_HANDLE_CREATED)
1278 emit_obj (logbuffer, obj);
1281 emit_bt (prof, logbuffer, &data);
1283 EXIT_LOG (logbuffer);
1284 process_requests (prof);
1288 push_nesting (char *p, MonoClass *klass)
1293 nesting = mono_class_get_nesting_type (klass);
1295 p = push_nesting (p, nesting);
1299 name = mono_class_get_name (klass);
1300 nspace = mono_class_get_namespace (klass);
1303 p += strlen (nspace);
1313 type_name (MonoClass *klass)
1317 push_nesting (buf, klass);
1318 p = (char *)malloc (strlen (buf) + 1);
1324 image_loaded (MonoProfiler *prof, MonoImage *image, int result)
1329 LogBuffer *logbuffer;
1330 if (result != MONO_PROFILE_OK)
1332 name = mono_image_get_filename (image);
1333 nlen = strlen (name) + 1;
1334 logbuffer = ensure_logbuf (
1335 EVENT_SIZE /* event */ +
1336 LEB128_SIZE /* time */ +
1337 EVENT_SIZE /* type */ +
1338 LEB128_SIZE /* image */ +
1339 LEB128_SIZE /* flags */ +
1342 now = current_time ();
1343 ENTER_LOG (logbuffer, "image");
1344 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1345 emit_time (logbuffer, now);
1346 emit_byte (logbuffer, TYPE_IMAGE);
1347 emit_ptr (logbuffer, image);
1348 emit_value (logbuffer, 0); /* flags */
1349 memcpy (logbuffer->cursor, name, nlen);
1350 logbuffer->cursor += nlen;
1351 //printf ("loaded image %p (%s)\n", image, name);
1352 EXIT_LOG (logbuffer);
1353 if (logbuffer->next)
1354 safe_send (prof, logbuffer);
1355 process_requests (prof);
1357 InterlockedIncrement (&image_loads);
1361 image_unloaded (MonoProfiler *prof, MonoImage *image)
1363 const char *name = mono_image_get_filename (image);
1364 int nlen = strlen (name) + 1;
1365 LogBuffer *logbuffer = ensure_logbuf (
1366 EVENT_SIZE /* event */ +
1367 LEB128_SIZE /* time */ +
1368 EVENT_SIZE /* type */ +
1369 LEB128_SIZE /* image */ +
1370 LEB128_SIZE /* flags */ +
1373 uint64_t now = current_time ();
1375 ENTER_LOG (logbuffer, "image-unload");
1376 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1377 emit_time (logbuffer, now);
1378 emit_byte (logbuffer, TYPE_IMAGE);
1379 emit_ptr (logbuffer, image);
1380 emit_value (logbuffer, 0); /* flags */
1381 memcpy (logbuffer->cursor, name, nlen);
1382 logbuffer->cursor += nlen;
1383 EXIT_LOG (logbuffer);
1385 if (logbuffer->next)
1386 safe_send (prof, logbuffer);
1388 process_requests (prof);
1390 InterlockedIncrement (&image_unloads);
1394 assembly_loaded (MonoProfiler *prof, MonoAssembly *assembly, int result)
1396 if (result != MONO_PROFILE_OK)
1399 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1400 int nlen = strlen (name) + 1;
1401 LogBuffer *logbuffer = ensure_logbuf (
1402 EVENT_SIZE /* event */ +
1403 LEB128_SIZE /* time */ +
1404 EVENT_SIZE /* type */ +
1405 LEB128_SIZE /* assembly */ +
1406 LEB128_SIZE /* flags */ +
1409 uint64_t now = current_time ();
1411 ENTER_LOG (logbuffer, "assembly-load");
1412 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1413 emit_time (logbuffer, now);
1414 emit_byte (logbuffer, TYPE_ASSEMBLY);
1415 emit_ptr (logbuffer, assembly);
1416 emit_value (logbuffer, 0); /* flags */
1417 memcpy (logbuffer->cursor, name, nlen);
1418 logbuffer->cursor += nlen;
1419 EXIT_LOG (logbuffer);
1423 if (logbuffer->next)
1424 safe_send (prof, logbuffer);
1426 process_requests (prof);
1428 InterlockedIncrement (&assembly_loads);
1432 assembly_unloaded (MonoProfiler *prof, MonoAssembly *assembly)
1434 char *name = mono_stringify_assembly_name (mono_assembly_get_name (assembly));
1435 int nlen = strlen (name) + 1;
1436 LogBuffer *logbuffer = ensure_logbuf (
1437 EVENT_SIZE /* event */ +
1438 LEB128_SIZE /* time */ +
1439 EVENT_SIZE /* type */ +
1440 LEB128_SIZE /* assembly */ +
1441 LEB128_SIZE /* flags */ +
1444 uint64_t now = current_time ();
1446 ENTER_LOG (logbuffer, "assembly-unload");
1447 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1448 emit_time (logbuffer, now);
1449 emit_byte (logbuffer, TYPE_ASSEMBLY);
1450 emit_ptr (logbuffer, assembly);
1451 emit_value (logbuffer, 0); /* flags */
1452 memcpy (logbuffer->cursor, name, nlen);
1453 logbuffer->cursor += nlen;
1454 EXIT_LOG (logbuffer);
1458 if (logbuffer->next)
1459 safe_send (prof, logbuffer);
1461 process_requests (prof);
1463 InterlockedIncrement (&assembly_unloads);
1467 class_loaded (MonoProfiler *prof, MonoClass *klass, int result)
1473 LogBuffer *logbuffer;
1474 if (result != MONO_PROFILE_OK)
1476 if (InterlockedRead (&runtime_inited))
1477 name = mono_type_get_name (mono_class_get_type (klass));
1479 name = type_name (klass);
1480 nlen = strlen (name) + 1;
1481 image = mono_class_get_image (klass);
1482 logbuffer = ensure_logbuf (
1483 EVENT_SIZE /* event */ +
1484 LEB128_SIZE /* time */ +
1485 EVENT_SIZE /* type */ +
1486 LEB128_SIZE /* klass */ +
1487 LEB128_SIZE /* image */ +
1488 LEB128_SIZE /* flags */ +
1491 now = current_time ();
1492 ENTER_LOG (logbuffer, "class");
1493 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1494 emit_time (logbuffer, now);
1495 emit_byte (logbuffer, TYPE_CLASS);
1496 emit_ptr (logbuffer, klass);
1497 emit_ptr (logbuffer, image);
1498 emit_value (logbuffer, 0); /* flags */
1499 memcpy (logbuffer->cursor, name, nlen);
1500 logbuffer->cursor += nlen;
1501 //printf ("loaded class %p (%s)\n", klass, name);
1506 EXIT_LOG (logbuffer);
1507 if (logbuffer->next)
1508 safe_send (prof, logbuffer);
1509 process_requests (prof);
1511 InterlockedIncrement (&class_loads);
1515 class_unloaded (MonoProfiler *prof, MonoClass *klass)
1519 if (InterlockedRead (&runtime_inited))
1520 name = mono_type_get_name (mono_class_get_type (klass));
1522 name = type_name (klass);
1524 int nlen = strlen (name) + 1;
1525 MonoImage *image = mono_class_get_image (klass);
1526 LogBuffer *logbuffer = ensure_logbuf (
1527 EVENT_SIZE /* event */ +
1528 LEB128_SIZE /* time */ +
1529 EVENT_SIZE /* type */ +
1530 LEB128_SIZE /* klass */ +
1531 LEB128_SIZE /* image */ +
1532 LEB128_SIZE /* flags */ +
1535 uint64_t now = current_time ();
1537 ENTER_LOG (logbuffer, "class-unload");
1538 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1539 emit_time (logbuffer, now);
1540 emit_byte (logbuffer, TYPE_CLASS);
1541 emit_ptr (logbuffer, klass);
1542 emit_ptr (logbuffer, image);
1543 emit_value (logbuffer, 0); /* flags */
1544 memcpy (logbuffer->cursor, name, nlen);
1545 logbuffer->cursor += nlen;
1546 EXIT_LOG (logbuffer);
1553 if (logbuffer->next)
1554 safe_send (prof, logbuffer);
1556 process_requests (prof);
1558 InterlockedIncrement (&class_unloads);
1561 #ifndef DISABLE_HELPER_THREAD
1562 static void process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method);
1563 #endif /* DISABLE_HELPER_THREAD */
1566 method_enter (MonoProfiler *prof, MonoMethod *method)
1568 uint64_t now = current_time ();
1570 #ifndef DISABLE_HELPER_THREAD
1571 process_method_enter_coverage (prof, method);
1572 #endif /* DISABLE_HELPER_THREAD */
1574 LogBuffer *logbuffer = ensure_logbuf (
1575 EVENT_SIZE /* event */ +
1576 LEB128_SIZE /* time */ +
1577 LEB128_SIZE /* method */
1579 if (logbuffer->call_depth++ > max_call_depth)
1581 ENTER_LOG (logbuffer, "enter");
1582 emit_byte (logbuffer, TYPE_ENTER | TYPE_METHOD);
1583 emit_time (logbuffer, now);
1584 emit_method (prof, logbuffer, method);
1585 EXIT_LOG (logbuffer);
1587 process_requests (prof);
1591 method_leave (MonoProfiler *prof, MonoMethod *method)
1594 LogBuffer *logbuffer = ensure_logbuf (
1595 EVENT_SIZE /* event */ +
1596 LEB128_SIZE /* time */ +
1597 LEB128_SIZE /* method */
1599 if (--logbuffer->call_depth > max_call_depth)
1601 now = current_time ();
1602 ENTER_LOG (logbuffer, "leave");
1603 emit_byte (logbuffer, TYPE_LEAVE | TYPE_METHOD);
1604 emit_time (logbuffer, now);
1605 emit_method (prof, logbuffer, method);
1606 EXIT_LOG (logbuffer);
1607 if (logbuffer->next)
1608 safe_send (prof, logbuffer);
1609 process_requests (prof);
1613 method_exc_leave (MonoProfiler *prof, MonoMethod *method)
1616 LogBuffer *logbuffer;
1619 logbuffer = ensure_logbuf (
1620 EVENT_SIZE /* event */ +
1621 LEB128_SIZE /* time */ +
1622 LEB128_SIZE /* method */
1624 if (--logbuffer->call_depth > max_call_depth)
1626 now = current_time ();
1627 ENTER_LOG (logbuffer, "eleave");
1628 emit_byte (logbuffer, TYPE_EXC_LEAVE | TYPE_METHOD);
1629 emit_time (logbuffer, now);
1630 emit_method (prof, logbuffer, method);
1631 EXIT_LOG (logbuffer);
1632 process_requests (prof);
1636 method_jitted (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *ji, int result)
1638 if (result != MONO_PROFILE_OK)
1641 register_method_local (prof, method, ji);
1643 process_requests (prof);
1647 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
1652 LogBuffer *logbuffer;
1653 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
1654 name = (char *)data;
1655 nlen = strlen (name) + 1;
1660 logbuffer = ensure_logbuf (
1661 EVENT_SIZE /* event */ +
1662 LEB128_SIZE /* time */ +
1663 LEB128_SIZE /* type */ +
1664 LEB128_SIZE /* buffer */ +
1665 LEB128_SIZE /* size */ +
1670 now = current_time ();
1671 ENTER_LOG (logbuffer, "code buffer");
1672 emit_byte (logbuffer, TYPE_JITHELPER | TYPE_RUNTIME);
1673 emit_time (logbuffer, now);
1674 emit_value (logbuffer, type);
1675 emit_ptr (logbuffer, buffer);
1676 emit_value (logbuffer, size);
1678 memcpy (logbuffer->cursor, name, nlen);
1679 logbuffer->cursor += nlen;
1681 EXIT_LOG (logbuffer);
1682 process_requests (prof);
1686 throw_exc (MonoProfiler *prof, MonoObject *object)
1688 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces)? TYPE_EXCEPTION_BT: 0;
1691 LogBuffer *logbuffer;
1694 logbuffer = ensure_logbuf (
1695 EVENT_SIZE /* event */ +
1696 LEB128_SIZE /* time */ +
1697 LEB128_SIZE /* object */ +
1699 LEB128_SIZE /* flags */ +
1700 LEB128_SIZE /* count */ +
1702 LEB128_SIZE /* method */
1706 now = current_time ();
1707 ENTER_LOG (logbuffer, "throw");
1708 emit_byte (logbuffer, do_bt | TYPE_EXCEPTION);
1709 emit_time (logbuffer, now);
1710 emit_obj (logbuffer, object);
1712 emit_bt (prof, logbuffer, &data);
1713 EXIT_LOG (logbuffer);
1714 process_requests (prof);
1718 clause_exc (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num)
1721 LogBuffer *logbuffer = ensure_logbuf (
1722 EVENT_SIZE /* event */ +
1723 LEB128_SIZE /* time */ +
1724 LEB128_SIZE /* clause type */ +
1725 LEB128_SIZE /* clause num */ +
1726 LEB128_SIZE /* method */
1728 now = current_time ();
1729 ENTER_LOG (logbuffer, "clause");
1730 emit_byte (logbuffer, TYPE_EXCEPTION | TYPE_CLAUSE);
1731 emit_time (logbuffer, now);
1732 emit_value (logbuffer, clause_type);
1733 emit_value (logbuffer, clause_num);
1734 emit_method (prof, logbuffer, method);
1735 EXIT_LOG (logbuffer);
1737 process_requests (prof);
1741 monitor_event (MonoProfiler *profiler, MonoObject *object, MonoProfilerMonitorEvent event)
1743 int do_bt = (nocalls && InterlockedRead (&runtime_inited) && !notraces && event == MONO_PROFILER_MONITOR_CONTENTION)? TYPE_MONITOR_BT: 0;
1746 LogBuffer *logbuffer;
1749 logbuffer = ensure_logbuf (
1750 EVENT_SIZE /* event */ +
1751 LEB128_SIZE /* time */ +
1752 LEB128_SIZE /* object */ +
1754 LEB128_SIZE /* flags */ +
1755 LEB128_SIZE /* count */ +
1757 LEB128_SIZE /* method */
1761 now = current_time ();
1762 ENTER_LOG (logbuffer, "monitor");
1763 emit_byte (logbuffer, (event << 4) | do_bt | TYPE_MONITOR);
1764 emit_time (logbuffer, now);
1765 emit_obj (logbuffer, object);
1767 emit_bt (profiler, logbuffer, &data);
1768 EXIT_LOG (logbuffer);
1769 process_requests (profiler);
1773 thread_start (MonoProfiler *prof, uintptr_t tid)
1775 //printf ("thread start %p\n", (void*)tid);
1778 LogBuffer *logbuffer = ensure_logbuf (
1779 EVENT_SIZE /* event */ +
1780 LEB128_SIZE /* time */ +
1781 EVENT_SIZE /* type */ +
1782 LEB128_SIZE /* tid */ +
1783 LEB128_SIZE /* flags */
1785 uint64_t now = current_time ();
1787 ENTER_LOG (logbuffer, "thread-start");
1788 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1789 emit_time (logbuffer, now);
1790 emit_byte (logbuffer, TYPE_THREAD);
1791 emit_ptr (logbuffer, (void*) tid);
1792 emit_value (logbuffer, 0); /* flags */
1793 EXIT_LOG (logbuffer);
1795 if (logbuffer->next)
1796 safe_send (prof, logbuffer);
1798 process_requests (prof);
1800 InterlockedIncrement (&thread_starts);
1804 thread_end (MonoProfiler *prof, uintptr_t tid)
1806 if (TLS_GET (LogBuffer, tlsbuffer)) {
1807 LogBuffer *logbuffer = ensure_logbuf (
1808 EVENT_SIZE /* event */ +
1809 LEB128_SIZE /* time */ +
1810 EVENT_SIZE /* type */ +
1811 LEB128_SIZE /* tid */ +
1812 LEB128_SIZE /* flags */
1814 uint64_t now = current_time ();
1816 ENTER_LOG (logbuffer, "thread-end");
1817 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1818 emit_time (logbuffer, now);
1819 emit_byte (logbuffer, TYPE_THREAD);
1820 emit_ptr (logbuffer, (void*) tid);
1821 emit_value (logbuffer, 0); /* flags */
1822 EXIT_LOG (logbuffer);
1824 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), logbuffer);
1826 /* Don't process requests as the thread is detached from the runtime. */
1829 TLS_SET (tlsbuffer, NULL);
1830 TLS_SET (tlsmethodlist, NULL);
1832 InterlockedIncrement (&thread_ends);
1836 domain_loaded (MonoProfiler *prof, MonoDomain *domain, int result)
1838 if (result != MONO_PROFILE_OK)
1841 LogBuffer *logbuffer = ensure_logbuf (
1842 EVENT_SIZE /* event */ +
1843 LEB128_SIZE /* time */ +
1844 EVENT_SIZE /* type */ +
1845 LEB128_SIZE /* domain id */ +
1846 LEB128_SIZE /* flags */
1848 uint64_t now = current_time ();
1850 ENTER_LOG (logbuffer, "domain-start");
1851 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1852 emit_time (logbuffer, now);
1853 emit_byte (logbuffer, TYPE_DOMAIN);
1854 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1855 emit_value (logbuffer, 0); /* flags */
1856 EXIT_LOG (logbuffer);
1858 if (logbuffer->next)
1859 safe_send (prof, logbuffer);
1861 process_requests (prof);
1863 InterlockedIncrement (&domain_loads);
1867 domain_unloaded (MonoProfiler *prof, MonoDomain *domain)
1869 LogBuffer *logbuffer = ensure_logbuf (
1870 EVENT_SIZE /* event */ +
1871 LEB128_SIZE /* time */ +
1872 EVENT_SIZE /* type */ +
1873 LEB128_SIZE /* domain id */ +
1874 LEB128_SIZE /* flags */
1876 uint64_t now = current_time ();
1878 ENTER_LOG (logbuffer, "domain-end");
1879 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1880 emit_time (logbuffer, now);
1881 emit_byte (logbuffer, TYPE_DOMAIN);
1882 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1883 emit_value (logbuffer, 0); /* flags */
1884 EXIT_LOG (logbuffer);
1886 if (logbuffer->next)
1887 safe_send (prof, logbuffer);
1889 process_requests (prof);
1891 InterlockedIncrement (&domain_unloads);
1895 domain_name (MonoProfiler *prof, MonoDomain *domain, const char *name)
1897 int nlen = strlen (name) + 1;
1898 LogBuffer *logbuffer = ensure_logbuf (
1899 EVENT_SIZE /* event */ +
1900 LEB128_SIZE /* time */ +
1901 EVENT_SIZE /* type */ +
1902 LEB128_SIZE /* domain id */ +
1903 LEB128_SIZE /* flags */ +
1906 uint64_t now = current_time ();
1908 ENTER_LOG (logbuffer, "domain-name");
1909 emit_byte (logbuffer, TYPE_METADATA);
1910 emit_time (logbuffer, now);
1911 emit_byte (logbuffer, TYPE_DOMAIN);
1912 emit_ptr (logbuffer, (void*)(uintptr_t) mono_domain_get_id (domain));
1913 emit_value (logbuffer, 0); /* flags */
1914 memcpy (logbuffer->cursor, name, nlen);
1915 logbuffer->cursor += nlen;
1916 EXIT_LOG (logbuffer);
1918 if (logbuffer->next)
1919 safe_send (prof, logbuffer);
1921 process_requests (prof);
1925 context_loaded (MonoProfiler *prof, MonoAppContext *context)
1927 LogBuffer *logbuffer = ensure_logbuf (
1928 EVENT_SIZE /* event */ +
1929 LEB128_SIZE /* time */ +
1930 EVENT_SIZE /* type */ +
1931 LEB128_SIZE /* context id */ +
1932 LEB128_SIZE /* flags */ +
1933 LEB128_SIZE /* domain id */
1935 uint64_t now = current_time ();
1937 ENTER_LOG (logbuffer, "context-start");
1938 emit_byte (logbuffer, TYPE_END_LOAD | TYPE_METADATA);
1939 emit_time (logbuffer, now);
1940 emit_byte (logbuffer, TYPE_CONTEXT);
1941 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
1942 emit_value (logbuffer, 0); /* flags */
1943 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
1944 EXIT_LOG (logbuffer);
1946 if (logbuffer->next)
1947 safe_send (prof, logbuffer);
1949 process_requests (prof);
1951 InterlockedIncrement (&context_loads);
1955 context_unloaded (MonoProfiler *prof, MonoAppContext *context)
1957 LogBuffer *logbuffer = ensure_logbuf (
1958 EVENT_SIZE /* event */ +
1959 LEB128_SIZE /* time */ +
1960 EVENT_SIZE /* type */ +
1961 LEB128_SIZE /* context id */ +
1962 LEB128_SIZE /* flags */ +
1963 LEB128_SIZE /* domain id */
1965 uint64_t now = current_time ();
1967 ENTER_LOG (logbuffer, "context-end");
1968 emit_byte (logbuffer, TYPE_END_UNLOAD | TYPE_METADATA);
1969 emit_time (logbuffer, now);
1970 emit_byte (logbuffer, TYPE_CONTEXT);
1971 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_id (context));
1972 emit_value (logbuffer, 0); /* flags */
1973 emit_ptr (logbuffer, (void*)(uintptr_t) mono_context_get_domain_id (context));
1974 EXIT_LOG (logbuffer);
1976 if (logbuffer->next)
1977 safe_send (prof, logbuffer);
1979 process_requests (prof);
1981 InterlockedIncrement (&context_unloads);
1985 thread_name (MonoProfiler *prof, uintptr_t tid, const char *name)
1987 int len = strlen (name) + 1;
1989 LogBuffer *logbuffer;
1990 logbuffer = ensure_logbuf (
1991 EVENT_SIZE /* event */ +
1992 LEB128_SIZE /* time */ +
1993 EVENT_SIZE /* type */ +
1994 LEB128_SIZE /* tid */ +
1995 LEB128_SIZE /* flags */ +
1998 now = current_time ();
1999 ENTER_LOG (logbuffer, "tname");
2000 emit_byte (logbuffer, TYPE_METADATA);
2001 emit_time (logbuffer, now);
2002 emit_byte (logbuffer, TYPE_THREAD);
2003 emit_ptr (logbuffer, (void*)tid);
2004 emit_value (logbuffer, 0); /* flags */
2005 memcpy (logbuffer->cursor, name, len);
2006 logbuffer->cursor += len;
2007 EXIT_LOG (logbuffer);
2009 if (logbuffer->next)
2010 safe_send (prof, logbuffer);
2012 process_requests (prof);
2023 MonoLockFreeQueueNode node;
2029 AsyncFrameInfo frames [MONO_ZERO_LEN_ARRAY];
2033 async_walk_stack (MonoMethod *method, MonoDomain *domain, void *base_address, int offset, void *data)
2035 SampleHit *sample = (SampleHit *) data;
2037 if (sample->count < num_frames) {
2038 int i = sample->count;
2040 sample->frames [i].method = method;
2041 sample->frames [i].domain = domain;
2042 sample->frames [i].base_address = base_address;
2043 sample->frames [i].offset = offset;
2048 return sample->count == num_frames;
2051 #define SAMPLE_SLOT_SIZE(FRAMES) (sizeof (SampleHit) + sizeof (AsyncFrameInfo) * (FRAMES - MONO_ZERO_LEN_ARRAY))
2052 #define SAMPLE_BLOCK_SIZE (mono_pagesize ())
2055 enqueue_sample_hit (gpointer p)
2057 SampleHit *sample = p;
2059 mono_lock_free_queue_node_unpoison (&sample->node);
2060 mono_lock_free_queue_enqueue (&sample->prof->dumper_queue, &sample->node);
2061 mono_os_sem_post (&sample->prof->dumper_queue_sem);
2063 InterlockedIncrement (&sample_flushes);
2067 mono_sample_hit (MonoProfiler *profiler, unsigned char *ip, void *context)
2070 * Please note: We rely on the runtime loading the profiler with
2071 * MONO_DL_EAGER (RTLD_NOW) so that references to runtime functions within
2072 * this function (and its siblings) are resolved when the profiler is
2073 * loaded. Otherwise, we would potentially invoke the dynamic linker when
2074 * invoking runtime functions, which is not async-signal-safe.
2080 InterlockedIncrement (&sample_hits);
2082 uint64_t now = current_time ();
2084 SampleHit *sample = (SampleHit *) mono_lock_free_queue_dequeue (&profiler->sample_reuse_queue);
2088 * If we're out of reusable sample events and we're not allowed to
2089 * allocate more, we have no choice but to drop the event.
2091 if (InterlockedRead (&sample_allocations) >= max_allocated_sample_hits)
2094 sample = mono_lock_free_alloc (&profiler->sample_allocator);
2095 sample->prof = profiler;
2096 mono_lock_free_queue_node_init (&sample->node, TRUE);
2098 InterlockedIncrement (&sample_allocations);
2102 mono_stack_walk_async_safe (&async_walk_stack, context, sample);
2104 uintptr_t elapsed = (now - profiler->startup_time) / 10000;
2106 sample->elapsed = elapsed;
2107 sample->tid = thread_id ();
2113 snprintf (buf, sizeof (buf), "hit at %p in thread %p after %llu ms\n", ip, (void *) thread_id (), (unsigned long long int) elapsed / 100);
2115 ign_res (write (2, buf, len));
2118 mono_thread_hazardous_try_free (sample, enqueue_sample_hit);
2121 static uintptr_t *code_pages = 0;
2122 static int num_code_pages = 0;
2123 static int size_code_pages = 0;
2124 #define CPAGE_SHIFT (9)
2125 #define CPAGE_SIZE (1 << CPAGE_SHIFT)
2126 #define CPAGE_MASK (~(CPAGE_SIZE - 1))
2127 #define CPAGE_ADDR(p) ((p) & CPAGE_MASK)
2130 add_code_page (uintptr_t *hash, uintptr_t hsize, uintptr_t page)
2133 uintptr_t start_pos;
2134 start_pos = (page >> CPAGE_SHIFT) % hsize;
2137 if (hash [i] && CPAGE_ADDR (hash [i]) == CPAGE_ADDR (page)) {
2139 } else if (!hash [i]) {
2146 } while (i != start_pos);
2147 /* should not happen */
2148 printf ("failed code page store\n");
2153 add_code_pointer (uintptr_t ip)
2156 if (num_code_pages * 2 >= size_code_pages) {
2158 uintptr_t old_size = size_code_pages;
2159 size_code_pages *= 2;
2160 if (size_code_pages == 0)
2161 size_code_pages = 16;
2162 n = (uintptr_t *)calloc (sizeof (uintptr_t) * size_code_pages, 1);
2163 for (i = 0; i < old_size; ++i) {
2165 add_code_page (n, size_code_pages, code_pages [i]);
2171 num_code_pages += add_code_page (code_pages, size_code_pages, ip & CPAGE_MASK);
2174 /* ELF code crashes on some systems. */
2175 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2178 dump_ubin (const char *filename, uintptr_t load_addr, uint64_t offset, uintptr_t size)
2181 LogBuffer *logbuffer;
2183 len = strlen (filename) + 1;
2184 now = current_time ();
2185 logbuffer = ensure_logbuf (
2186 EVENT_SIZE /* event */ +
2187 LEB128_SIZE /* time */ +
2188 LEB128_SIZE /* load address */ +
2189 LEB128_SIZE /* offset */ +
2190 LEB128_SIZE /* size */ +
2191 nlen /* file name */
2193 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_UBIN);
2194 emit_time (logbuffer, now);
2195 emit_svalue (logbuffer, load_addr);
2196 emit_uvalue (logbuffer, offset);
2197 emit_uvalue (logbuffer, size);
2198 memcpy (logbuffer->cursor, filename, len);
2199 logbuffer->cursor += len;
2204 dump_usym (const char *name, uintptr_t value, uintptr_t size)
2206 LogBuffer *logbuffer;
2208 len = strlen (name) + 1;
2209 logbuffer = ensure_logbuf (
2210 EVENT_SIZE /* event */ +
2211 LEB128_SIZE /* value */ +
2212 LEB128_SIZE /* size */ +
2215 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_USYM);
2216 emit_ptr (logbuffer, (void*)value);
2217 emit_value (logbuffer, size);
2218 memcpy (logbuffer->cursor, name, len);
2219 logbuffer->cursor += len;
2222 /* ELF code crashes on some systems. */
2223 //#if defined(ELFMAG0)
2226 #if SIZEOF_VOID_P == 4
2227 #define ELF_WSIZE 32
2229 #define ELF_WSIZE 64
2232 #define ElfW(type) _ElfW (Elf, ELF_WSIZE, type)
2233 #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
2234 #define _ElfW_1(e,w,t) e##w##t
2238 dump_elf_symbols (ElfW(Sym) *symbols, int num_symbols, const char *strtab, void *load_addr)
2241 for (i = 0; i < num_symbols; ++i) {
2243 sym = strtab + symbols [i].st_name;
2244 if (!symbols [i].st_name || !symbols [i].st_size || (symbols [i].st_info & 0xf) != STT_FUNC)
2246 //printf ("symbol %s at %d\n", sym, symbols [i].st_value);
2247 dump_usym (sym, (uintptr_t)load_addr + symbols [i].st_value, symbols [i].st_size);
2252 read_elf_symbols (MonoProfiler *prof, const char *filename, void *load_addr)
2259 ElfW(Shdr) *sheader;
2260 ElfW(Shdr) *shstrtabh;
2261 ElfW(Shdr) *symtabh = NULL;
2262 ElfW(Shdr) *strtabh = NULL;
2263 ElfW(Sym) *symbols = NULL;
2267 fd = open (filename, O_RDONLY);
2270 if (fstat (fd, &statb) != 0) {
2274 file_size = statb.st_size;
2275 data = mmap (NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
2277 if (data == MAP_FAILED)
2280 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2281 header->e_ident [EI_MAG1] != ELFMAG1 ||
2282 header->e_ident [EI_MAG2] != ELFMAG2 ||
2283 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2284 munmap (data, file_size);
2287 sheader = (void*)((char*)data + header->e_shoff);
2288 shstrtabh = (void*)((char*)sheader + (header->e_shentsize * header->e_shstrndx));
2289 strtab = (const char*)data + shstrtabh->sh_offset;
2290 for (i = 0; i < header->e_shnum; ++i) {
2291 //printf ("section header: %d\n", sheader->sh_type);
2292 if (sheader->sh_type == SHT_SYMTAB) {
2294 strtabh = (void*)((char*)data + header->e_shoff + sheader->sh_link * header->e_shentsize);
2295 /*printf ("symtab section header: %d, .strstr: %d\n", i, sheader->sh_link);*/
2298 sheader = (void*)((char*)sheader + header->e_shentsize);
2300 if (!symtabh || !strtabh) {
2301 munmap (data, file_size);
2304 strtab = (const char*)data + strtabh->sh_offset;
2305 num_symbols = symtabh->sh_size / symtabh->sh_entsize;
2306 symbols = (void*)((char*)data + symtabh->sh_offset);
2307 dump_elf_symbols (symbols, num_symbols, strtab, load_addr);
2308 munmap (data, file_size);
2313 /* ELF code crashes on some systems. */
2314 //#if defined(HAVE_DL_ITERATE_PHDR) && defined(ELFMAG0)
2317 elf_dl_callback (struct dl_phdr_info *info, size_t size, void *data)
2319 MonoProfiler *prof = data;
2321 const char *filename;
2323 char *a = (void*)info->dlpi_addr;
2325 ElfW(Dyn) *dyn = NULL;
2326 ElfW(Sym) *symtab = NULL;
2327 ElfW(Word) *hash_table = NULL;
2328 ElfW(Ehdr) *header = NULL;
2329 const char* strtab = NULL;
2330 for (obj = prof->binary_objects; obj; obj = obj->next) {
2334 filename = info->dlpi_name;
2337 if (!info->dlpi_addr && !filename [0]) {
2338 int l = readlink ("/proc/self/exe", buf, sizeof (buf) - 1);
2344 obj = calloc (sizeof (BinaryObject), 1);
2345 obj->addr = (void*)info->dlpi_addr;
2346 obj->name = pstrdup (filename);
2347 obj->next = prof->binary_objects;
2348 prof->binary_objects = obj;
2349 //printf ("loaded file: %s at %p, segments: %d\n", filename, (void*)info->dlpi_addr, info->dlpi_phnum);
2351 for (i = 0; i < info->dlpi_phnum; ++i) {
2352 //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);
2353 if (info->dlpi_phdr[i].p_type == PT_LOAD && !header) {
2354 header = (ElfW(Ehdr)*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2355 if (header->e_ident [EI_MAG0] != ELFMAG0 ||
2356 header->e_ident [EI_MAG1] != ELFMAG1 ||
2357 header->e_ident [EI_MAG2] != ELFMAG2 ||
2358 header->e_ident [EI_MAG3] != ELFMAG3 ) {
2361 dump_ubin (filename, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, info->dlpi_phdr[i].p_offset, info->dlpi_phdr[i].p_memsz);
2362 } else if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
2363 dyn = (ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2366 if (read_elf_symbols (prof, filename, (void*)info->dlpi_addr))
2368 if (!info->dlpi_name || !info->dlpi_name[0])
2372 for (i = 0; dyn [i].d_tag != DT_NULL; ++i) {
2373 if (dyn [i].d_tag == DT_SYMTAB) {
2374 if (symtab && do_debug)
2375 printf ("multiple symtabs: %d\n", i);
2376 symtab = (ElfW(Sym) *)(a + dyn [i].d_un.d_ptr);
2377 } else if (dyn [i].d_tag == DT_HASH) {
2378 hash_table = (ElfW(Word) *)(a + dyn [i].d_un.d_ptr);
2379 } else if (dyn [i].d_tag == DT_STRTAB) {
2380 strtab = (const char*)(a + dyn [i].d_un.d_ptr);
2385 num_sym = hash_table [1];
2386 dump_elf_symbols (symtab, num_sym, strtab, (void*)info->dlpi_addr);
2391 load_binaries (MonoProfiler *prof)
2393 dl_iterate_phdr (elf_dl_callback, prof);
2398 load_binaries (MonoProfiler *prof)
2405 symbol_for (uintptr_t code)
2408 void *ip = (void*)code;
2410 if (dladdr (ip, &di)) {
2412 return di.dli_sname;
2415 names = backtrace_symbols (&ip, 1);
2417 const char* p = names [0];
2428 dump_unmanaged_coderefs (MonoProfiler *prof)
2431 const char* last_symbol;
2432 uintptr_t addr, page_end;
2434 if (load_binaries (prof))
2436 for (i = 0; i < size_code_pages; ++i) {
2438 if (!code_pages [i] || code_pages [i] & 1)
2441 addr = CPAGE_ADDR (code_pages [i]);
2442 page_end = addr + CPAGE_SIZE;
2443 code_pages [i] |= 1;
2444 /* we dump the symbols for the whole page */
2445 for (; addr < page_end; addr += 16) {
2446 sym = symbol_for (addr);
2447 if (sym && sym == last_symbol)
2452 dump_usym (sym, addr, 0); /* let's not guess the size */
2453 //printf ("found symbol at %p: %s\n", (void*)addr, sym);
2459 mono_cpu_count (void)
2461 #ifdef PLATFORM_ANDROID
2462 /* Android tries really hard to save power by powering off CPUs on SMP phones which
2463 * means the normal way to query cpu count returns a wrong value with userspace API.
2464 * Instead we use /sys entries to query the actual hardware CPU count.
2467 char buffer[8] = {'\0'};
2468 int present = open ("/sys/devices/system/cpu/present", O_RDONLY);
2469 /* Format of the /sys entry is a cpulist of indexes which in the case
2470 * of present is always of the form "0-(n-1)" when there is more than
2471 * 1 core, n being the number of CPU cores in the system. Otherwise
2472 * the value is simply 0
2474 if (present != -1 && read (present, (char*)buffer, sizeof (buffer)) > 3)
2475 count = strtol (((char*)buffer) + 2, NULL, 10);
2482 #if defined(HOST_ARM) || defined (HOST_ARM64)
2484 /* ARM platforms tries really hard to save power by powering off CPUs on SMP phones which
2485 * means the normal way to query cpu count returns a wrong value with userspace API. */
2487 #ifdef _SC_NPROCESSORS_CONF
2489 int count = sysconf (_SC_NPROCESSORS_CONF);
2497 #ifdef HAVE_SCHED_GETAFFINITY
2500 if (sched_getaffinity (getpid (), sizeof (set), &set) == 0)
2501 return CPU_COUNT (&set);
2504 #ifdef _SC_NPROCESSORS_ONLN
2506 int count = sysconf (_SC_NPROCESSORS_ONLN);
2512 #endif /* defined(HOST_ARM) || defined (HOST_ARM64) */
2518 size_t len = sizeof (int);
2521 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0)
2528 GetSystemInfo (&info);
2529 return info.dwNumberOfProcessors;
2540 unsigned int prev_pos;
2542 struct perf_event_mmap_page *page_desc;
2545 static PerfData *perf_data = NULL;
2546 static int num_perf;
2547 #define PERF_PAGES_SHIFT 4
2548 static int num_pages = 1 << PERF_PAGES_SHIFT;
2549 static unsigned int mmap_mask;
2552 struct perf_event_header h;
2562 perf_event_syscall (struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)
2564 attr->size = PERF_ATTR_SIZE_VER0;
2565 //printf ("perf attr size: %d\n", attr->size);
2566 #if defined(__x86_64__)
2567 return syscall(/*__NR_perf_event_open*/ 298, attr, pid, cpu, group_fd, flags);
2568 #elif defined(__i386__)
2569 return syscall(/*__NR_perf_event_open*/ 336, attr, pid, cpu, group_fd, flags);
2570 #elif defined(__arm__) || defined (__aarch64__)
2571 return syscall(/*__NR_perf_event_open*/ 364, attr, pid, cpu, group_fd, flags);
2578 setup_perf_map (PerfData *perf)
2580 perf->mmap_base = mmap (NULL, (num_pages + 1) * getpagesize (), PROT_READ|PROT_WRITE, MAP_SHARED, perf->perf_fd, 0);
2581 if (perf->mmap_base == MAP_FAILED) {
2583 printf ("failed mmap\n");
2586 perf->page_desc = perf->mmap_base;
2588 printf ("mmap version: %d\n", perf->page_desc->version);
2593 dump_perf_hits (MonoProfiler *prof, void *buf, int size)
2595 LogBuffer *logbuffer;
2598 void *end = (char*)buf + size;
2600 int pid = getpid ();
2606 if (pid != s->pid) {
2608 printf ("event for different pid: %d\n", s->pid);
2609 buf = (char*)buf + s->h.size;
2612 /*ip = (void*)s->ip;
2613 printf ("sample: %d, size: %d, ip: %p (%s), timestamp: %llu, nframes: %llu\n",
2614 s->h.type, s->h.size, ip, symbol_for (ip), s->timestamp, s->nframes);*/
2615 logbuffer = ensure_logbuf (
2616 EVENT_SIZE /* event */ +
2617 LEB128_SIZE /* type */ +
2618 LEB128_SIZE /* time */ +
2619 LEB128_SIZE /* tid */ +
2620 LEB128_SIZE /* count */ +
2622 LEB128_SIZE /* ip */
2624 LEB128_SIZE /* managed count */ +
2626 LEB128_SIZE /* method */ +
2627 LEB128_SIZE /* il offset */ +
2628 LEB128_SIZE /* native offset */
2631 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
2632 emit_value (logbuffer, sample_type);
2633 emit_uvalue (logbuffer, s->timestamp - prof->startup_time);
2635 * No useful thread ID to write here, since throughout the
2636 * profiler we use pthread_self () but the ID we get from
2637 * perf is the kernel's thread ID.
2639 emit_ptr (logbuffer, 0);
2640 emit_value (logbuffer, count);
2641 emit_ptr (logbuffer, (void*)(uintptr_t)s->ip);
2642 add_code_pointer (s->ip);
2643 /* no support here yet for the managed backtrace */
2644 emit_uvalue (logbuffer, mbt_count);
2645 buf = (char*)buf + s->h.size;
2649 printf ("dumped %d samples\n", samples);
2650 dump_unmanaged_coderefs (prof);
2653 /* read events from the ring buffer */
2655 read_perf_mmap (MonoProfiler* prof, int cpu)
2657 PerfData *perf = perf_data + cpu;
2659 unsigned char *data = (unsigned char*)perf->mmap_base + getpagesize ();
2660 unsigned int head = perf->page_desc->data_head;
2664 mono_memory_read_barrier ();
2666 old = perf->prev_pos;
2670 printf ("lost mmap events: old: %d, head: %d\n", old, head);
2674 if ((old & mmap_mask) + size != (head & mmap_mask)) {
2675 buf = data + (old & mmap_mask);
2676 size = mmap_mask + 1 - (old & mmap_mask);
2678 /* size bytes at buf */
2680 printf ("found1 bytes of events: %d\n", size);
2681 dump_perf_hits (prof, buf, size);
2683 buf = data + (old & mmap_mask);
2685 /* size bytes at buf */
2687 printf ("found bytes of events: %d\n", size);
2688 dump_perf_hits (prof, buf, size);
2690 perf->prev_pos = old;
2691 perf->page_desc->data_tail = old;
2696 setup_perf_event_for_cpu (PerfData *perf, int cpu)
2698 struct perf_event_attr attr;
2699 memset (&attr, 0, sizeof (attr));
2700 attr.type = PERF_TYPE_HARDWARE;
2701 switch (sample_type) {
2702 case SAMPLE_CYCLES: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2703 case SAMPLE_INSTRUCTIONS: attr.config = PERF_COUNT_HW_INSTRUCTIONS; break;
2704 case SAMPLE_CACHE_MISSES: attr.config = PERF_COUNT_HW_CACHE_MISSES; break;
2705 case SAMPLE_CACHE_REFS: attr.config = PERF_COUNT_HW_CACHE_REFERENCES; break;
2706 case SAMPLE_BRANCHES: attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; break;
2707 case SAMPLE_BRANCH_MISSES: attr.config = PERF_COUNT_HW_BRANCH_MISSES; break;
2708 default: attr.config = PERF_COUNT_HW_CPU_CYCLES; break;
2710 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD | PERF_SAMPLE_TIME;
2711 // attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2712 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
2715 attr.sample_freq = sample_freq;
2717 perf->perf_fd = perf_event_syscall (&attr, getpid (), cpu, -1, 0);
2719 printf ("perf fd: %d, freq: %d, event: %llu\n", perf->perf_fd, sample_freq, attr.config);
2720 if (perf->perf_fd < 0) {
2721 if (perf->perf_fd == -EPERM) {
2722 fprintf (stderr, "Perf syscall denied, do \"echo 1 > /proc/sys/kernel/perf_event_paranoid\" as root to enable.\n");
2725 perror ("open perf event");
2729 if (!setup_perf_map (perf)) {
2730 close (perf->perf_fd);
2738 setup_perf_event (void)
2741 mmap_mask = num_pages * getpagesize () - 1;
2742 num_perf = mono_cpu_count ();
2743 perf_data = calloc (num_perf, sizeof (PerfData));
2744 for (i = 0; i < num_perf; ++i) {
2745 count += setup_perf_event_for_cpu (perf_data + i, i);
2754 #endif /* USE_PERF_EVENTS */
2756 #ifndef DISABLE_HELPER_THREAD
2758 typedef struct MonoCounterAgent {
2759 MonoCounter *counter;
2760 // MonoCounterAgent specific data :
2765 struct MonoCounterAgent *next;
2768 static MonoCounterAgent* counters;
2769 static gboolean counters_initialized = FALSE;
2770 static int counters_index = 1;
2771 static mono_mutex_t counters_mutex;
2774 counters_add_agent (MonoCounter *counter)
2776 MonoCounterAgent *agent, *item;
2778 if (!counters_initialized)
2781 mono_os_mutex_lock (&counters_mutex);
2783 for (agent = counters; agent; agent = agent->next) {
2784 if (agent->counter == counter) {
2785 agent->value_size = 0;
2787 free (agent->value);
2788 agent->value = NULL;
2790 mono_os_mutex_unlock (&counters_mutex);
2795 agent = (MonoCounterAgent *)malloc (sizeof (MonoCounterAgent));
2796 agent->counter = counter;
2797 agent->value = NULL;
2798 agent->value_size = 0;
2799 agent->index = counters_index++;
2812 mono_os_mutex_unlock (&counters_mutex);
2816 counters_init_foreach_callback (MonoCounter *counter, gpointer data)
2818 counters_add_agent (counter);
2823 counters_init (MonoProfiler *profiler)
2825 assert (!counters_initialized);
2827 mono_os_mutex_init (&counters_mutex);
2829 counters_initialized = TRUE;
2831 mono_counters_on_register (&counters_add_agent);
2832 mono_counters_foreach (counters_init_foreach_callback, NULL);
2836 counters_emit (MonoProfiler *profiler, gboolean threadless)
2838 MonoCounterAgent *agent;
2839 LogBuffer *logbuffer;
2842 EVENT_SIZE /* event */ +
2843 LEB128_SIZE /* len */
2846 if (!counters_initialized)
2849 mono_os_mutex_lock (&counters_mutex);
2851 for (agent = counters; agent; agent = agent->next) {
2856 LEB128_SIZE /* section */ +
2857 strlen (mono_counter_get_name (agent->counter)) + 1 /* name */ +
2858 LEB128_SIZE /* type */ +
2859 LEB128_SIZE /* unit */ +
2860 LEB128_SIZE /* variance */ +
2861 LEB128_SIZE /* index */
2868 mono_os_mutex_unlock (&counters_mutex);
2872 logbuffer = ensure_logbuf (size);
2874 ENTER_LOG (logbuffer, "counters");
2875 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
2876 emit_value (logbuffer, len);
2877 for (agent = counters; agent; agent = agent->next) {
2883 name = mono_counter_get_name (agent->counter);
2884 emit_value (logbuffer, mono_counter_get_section (agent->counter));
2885 emit_string (logbuffer, name, strlen (name) + 1);
2886 emit_value (logbuffer, mono_counter_get_type (agent->counter));
2887 emit_value (logbuffer, mono_counter_get_unit (agent->counter));
2888 emit_value (logbuffer, mono_counter_get_variance (agent->counter));
2889 emit_value (logbuffer, agent->index);
2893 EXIT_LOG (logbuffer);
2896 safe_send_threadless (profiler, logbuffer);
2898 safe_send (profiler, logbuffer);
2900 mono_os_mutex_unlock (&counters_mutex);
2904 counters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless)
2906 MonoCounterAgent *agent;
2907 MonoCounter *counter;
2908 LogBuffer *logbuffer;
2914 if (!counters_initialized)
2917 counters_emit (profiler, threadless);
2920 buffer = calloc (1, buffer_size);
2922 mono_os_mutex_lock (&counters_mutex);
2925 EVENT_SIZE /* event */ +
2926 LEB128_SIZE /* time */
2929 for (agent = counters; agent; agent = agent->next) {
2931 LEB128_SIZE /* index */ +
2932 LEB128_SIZE /* type */ +
2933 mono_counter_get_size (agent->counter) /* value */
2938 LEB128_SIZE /* stop marker */
2941 logbuffer = ensure_logbuf (size);
2943 ENTER_LOG (logbuffer, "counters");
2944 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
2945 emit_uvalue (logbuffer, timestamp);
2946 for (agent = counters; agent; agent = agent->next) {
2949 counter = agent->counter;
2951 size = mono_counter_get_size (counter);
2953 continue; // FIXME error
2954 } else if (size > buffer_size) {
2956 buffer = realloc (buffer, buffer_size);
2959 memset (buffer, 0, buffer_size);
2961 if (mono_counters_sample (counter, buffer, size) < 0)
2962 continue; // FIXME error
2964 type = mono_counter_get_type (counter);
2966 if (!agent->value) {
2967 agent->value = calloc (1, size);
2968 agent->value_size = size;
2970 if (type == MONO_COUNTER_STRING) {
2971 if (strcmp (agent->value, buffer) == 0)
2974 if (agent->value_size == size && memcmp (agent->value, buffer, size) == 0)
2979 emit_uvalue (logbuffer, agent->index);
2980 emit_uvalue (logbuffer, type);
2982 case MONO_COUNTER_INT:
2983 #if SIZEOF_VOID_P == 4
2984 case MONO_COUNTER_WORD:
2986 emit_svalue (logbuffer, *(int*)buffer - *(int*)agent->value);
2988 case MONO_COUNTER_UINT:
2989 emit_uvalue (logbuffer, *(guint*)buffer - *(guint*)agent->value);
2991 case MONO_COUNTER_TIME_INTERVAL:
2992 case MONO_COUNTER_LONG:
2993 #if SIZEOF_VOID_P == 8
2994 case MONO_COUNTER_WORD:
2996 emit_svalue (logbuffer, *(gint64*)buffer - *(gint64*)agent->value);
2998 case MONO_COUNTER_ULONG:
2999 emit_uvalue (logbuffer, *(guint64*)buffer - *(guint64*)agent->value);
3001 case MONO_COUNTER_DOUBLE:
3002 emit_double (logbuffer, *(double*)buffer);
3004 case MONO_COUNTER_STRING:
3006 emit_byte (logbuffer, 0);
3008 emit_byte (logbuffer, 1);
3009 emit_string (logbuffer, (char*)buffer, size);
3016 if (type == MONO_COUNTER_STRING && size > agent->value_size) {
3017 agent->value = realloc (agent->value, size);
3018 agent->value_size = size;
3022 memcpy (agent->value, buffer, size);
3026 emit_value (logbuffer, 0);
3027 EXIT_LOG (logbuffer);
3030 safe_send_threadless (profiler, logbuffer);
3032 safe_send (profiler, logbuffer);
3034 mono_os_mutex_unlock (&counters_mutex);
3037 typedef struct _PerfCounterAgent PerfCounterAgent;
3038 struct _PerfCounterAgent {
3039 PerfCounterAgent *next;
3041 char *category_name;
3050 static PerfCounterAgent *perfcounters = NULL;
3053 perfcounters_emit (MonoProfiler *profiler, gboolean threadless)
3055 PerfCounterAgent *pcagent;
3056 LogBuffer *logbuffer;
3059 EVENT_SIZE /* event */ +
3060 LEB128_SIZE /* len */
3063 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3064 if (pcagent->emitted)
3068 LEB128_SIZE /* section */ +
3069 strlen (pcagent->category_name) + 1 /* category name */ +
3070 strlen (pcagent->name) + 1 /* name */ +
3071 LEB128_SIZE /* type */ +
3072 LEB128_SIZE /* unit */ +
3073 LEB128_SIZE /* variance */ +
3074 LEB128_SIZE /* index */
3083 logbuffer = ensure_logbuf (size);
3085 ENTER_LOG (logbuffer, "perfcounters");
3086 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS_DESC | TYPE_SAMPLE);
3087 emit_value (logbuffer, len);
3088 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3089 if (pcagent->emitted)
3092 emit_value (logbuffer, MONO_COUNTER_PERFCOUNTERS);
3093 emit_string (logbuffer, pcagent->category_name, strlen (pcagent->category_name) + 1);
3094 emit_string (logbuffer, pcagent->name, strlen (pcagent->name) + 1);
3095 emit_value (logbuffer, MONO_COUNTER_LONG);
3096 emit_value (logbuffer, MONO_COUNTER_RAW);
3097 emit_value (logbuffer, MONO_COUNTER_VARIABLE);
3098 emit_value (logbuffer, pcagent->index);
3100 pcagent->emitted = 1;
3102 EXIT_LOG (logbuffer);
3105 safe_send_threadless (profiler, logbuffer);
3107 safe_send (profiler, logbuffer);
3111 perfcounters_foreach (char *category_name, char *name, unsigned char type, gint64 value, gpointer user_data)
3113 PerfCounterAgent *pcagent;
3115 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3116 if (strcmp (pcagent->category_name, category_name) != 0 || strcmp (pcagent->name, name) != 0)
3118 if (pcagent->value == value)
3121 pcagent->value = value;
3122 pcagent->updated = 1;
3123 pcagent->deleted = 0;
3127 pcagent = g_new0 (PerfCounterAgent, 1);
3128 pcagent->next = perfcounters;
3129 pcagent->index = counters_index++;
3130 pcagent->category_name = g_strdup (category_name);
3131 pcagent->name = g_strdup (name);
3132 pcagent->type = (int) type;
3133 pcagent->value = value;
3134 pcagent->emitted = 0;
3135 pcagent->updated = 1;
3136 pcagent->deleted = 0;
3138 perfcounters = pcagent;
3144 perfcounters_sample (MonoProfiler *profiler, uint64_t timestamp, gboolean threadless)
3146 PerfCounterAgent *pcagent;
3147 LogBuffer *logbuffer;
3150 if (!counters_initialized)
3153 mono_os_mutex_lock (&counters_mutex);
3155 /* mark all perfcounters as deleted, foreach will unmark them as necessary */
3156 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next)
3157 pcagent->deleted = 1;
3159 mono_perfcounter_foreach (perfcounters_foreach, perfcounters);
3161 perfcounters_emit (profiler, threadless);
3164 EVENT_SIZE /* event */ +
3165 LEB128_SIZE /* time */
3168 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3169 if (pcagent->deleted || !pcagent->updated)
3173 LEB128_SIZE /* index */ +
3174 LEB128_SIZE /* type */ +
3175 LEB128_SIZE /* value */
3180 LEB128_SIZE /* stop marker */
3183 logbuffer = ensure_logbuf (size);
3185 ENTER_LOG (logbuffer, "perfcounters");
3186 emit_byte (logbuffer, TYPE_SAMPLE_COUNTERS | TYPE_SAMPLE);
3187 emit_uvalue (logbuffer, timestamp);
3188 for (pcagent = perfcounters; pcagent; pcagent = pcagent->next) {
3189 if (pcagent->deleted || !pcagent->updated)
3191 emit_uvalue (logbuffer, pcagent->index);
3192 emit_uvalue (logbuffer, MONO_COUNTER_LONG);
3193 emit_svalue (logbuffer, pcagent->value);
3195 pcagent->updated = 0;
3198 emit_value (logbuffer, 0);
3199 EXIT_LOG (logbuffer);
3202 safe_send_threadless (profiler, logbuffer);
3204 safe_send (profiler, logbuffer);
3206 mono_os_mutex_unlock (&counters_mutex);
3210 counters_and_perfcounters_sample (MonoProfiler *prof, gboolean threadless)
3212 static uint64_t start = -1;
3216 start = current_time ();
3218 now = current_time ();
3219 counters_sample (prof, (now - start) / 1000/ 1000, threadless);
3220 perfcounters_sample (prof, (now - start) / 1000/ 1000, threadless);
3223 #define COVERAGE_DEBUG(x) if (debug_coverage) {x}
3224 static mono_mutex_t coverage_mutex;
3225 static MonoConcurrentHashTable *coverage_methods = NULL;
3226 static MonoConcurrentHashTable *coverage_assemblies = NULL;
3227 static MonoConcurrentHashTable *coverage_classes = NULL;
3229 static MonoConcurrentHashTable *filtered_classes = NULL;
3230 static MonoConcurrentHashTable *entered_methods = NULL;
3231 static MonoConcurrentHashTable *image_to_methods = NULL;
3232 static MonoConcurrentHashTable *suppressed_assemblies = NULL;
3233 static gboolean coverage_initialized = FALSE;
3235 static GPtrArray *coverage_data = NULL;
3236 static int previous_offset = 0;
3238 typedef struct _MethodNode MethodNode;
3239 struct _MethodNode {
3240 MonoLockFreeQueueNode node;
3244 typedef struct _CoverageEntry CoverageEntry;
3245 struct _CoverageEntry {
3254 free_coverage_entry (gpointer data, gpointer userdata)
3256 CoverageEntry *entry = (CoverageEntry *)data;
3257 g_free (entry->filename);
3262 obtain_coverage_for_method (MonoProfiler *prof, const MonoProfileCoverageEntry *entry)
3264 int offset = entry->iloffset - previous_offset;
3265 CoverageEntry *e = g_new (CoverageEntry, 1);
3267 previous_offset = entry->iloffset;
3270 e->counter = entry->counter;
3271 e->filename = g_strdup(entry->filename ? entry->filename : "");
3272 e->line = entry->line;
3273 e->column = entry->col;
3275 g_ptr_array_add (coverage_data, e);
3279 parse_generic_type_names(char *name)
3281 char *new_name, *ret;
3282 int within_generic_declaration = 0, generic_members = 1;
3284 if (name == NULL || *name == '\0')
3285 return g_strdup ("");
3287 if (!(ret = new_name = (char *)calloc (strlen (name) * 4 + 1, sizeof (char))))
3293 within_generic_declaration = 1;
3297 within_generic_declaration = 0;
3299 if (*(name - 1) != '<') {
3301 *new_name++ = '0' + generic_members;
3303 memcpy (new_name, "<>", 8);
3307 generic_members = 0;
3315 if (!within_generic_declaration)
3316 *new_name++ = *name;
3325 static int method_id;
3327 build_method_buffer (gpointer key, gpointer value, gpointer userdata)
3329 MonoMethod *method = (MonoMethod *)value;
3330 MonoProfiler *prof = (MonoProfiler *)userdata;
3334 const char *image_name, *method_name, *sig, *first_filename;
3335 LogBuffer *logbuffer;
3338 previous_offset = 0;
3339 coverage_data = g_ptr_array_new ();
3341 mono_profiler_coverage_get (prof, method, obtain_coverage_for_method);
3343 klass = mono_method_get_class (method);
3344 image = mono_class_get_image (klass);
3345 image_name = mono_image_get_name (image);
3347 sig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3348 class_name = parse_generic_type_names (mono_type_get_name (mono_class_get_type (klass)));
3349 method_name = mono_method_get_name (method);
3351 if (coverage_data->len != 0) {
3352 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[0];
3353 first_filename = entry->filename ? entry->filename : "";
3355 first_filename = "";
3357 image_name = image_name ? image_name : "";
3358 sig = sig ? sig : "";
3359 method_name = method_name ? method_name : "";
3361 logbuffer = ensure_logbuf (
3362 EVENT_SIZE /* event */ +
3363 strlen (image_name) + 1 /* image name */ +
3364 strlen (class_name) + 1 /* class name */ +
3365 strlen (method_name) + 1 /* method name */ +
3366 strlen (sig) + 1 /* signature */ +
3367 strlen (first_filename) + 1 /* first file name */ +
3368 LEB128_SIZE /* token */ +
3369 LEB128_SIZE /* method id */ +
3370 LEB128_SIZE /* entries */
3372 ENTER_LOG (logbuffer, "coverage-methods");
3374 emit_byte (logbuffer, TYPE_COVERAGE_METHOD | TYPE_COVERAGE);
3375 emit_string (logbuffer, image_name, strlen (image_name) + 1);
3376 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3377 emit_string (logbuffer, method_name, strlen (method_name) + 1);
3378 emit_string (logbuffer, sig, strlen (sig) + 1);
3379 emit_string (logbuffer, first_filename, strlen (first_filename) + 1);
3381 emit_uvalue (logbuffer, mono_method_get_token (method));
3382 emit_uvalue (logbuffer, method_id);
3383 emit_value (logbuffer, coverage_data->len);
3385 EXIT_LOG (logbuffer);
3386 safe_send (prof, logbuffer);
3388 for (i = 0; i < coverage_data->len; i++) {
3389 CoverageEntry *entry = (CoverageEntry *)coverage_data->pdata[i];
3391 logbuffer = ensure_logbuf (
3392 EVENT_SIZE /* event */ +
3393 LEB128_SIZE /* method id */ +
3394 LEB128_SIZE /* offset */ +
3395 LEB128_SIZE /* counter */ +
3396 LEB128_SIZE /* line */ +
3397 LEB128_SIZE /* column */
3399 ENTER_LOG (logbuffer, "coverage-statement");
3401 emit_byte (logbuffer, TYPE_COVERAGE_STATEMENT | TYPE_COVERAGE);
3402 emit_uvalue (logbuffer, method_id);
3403 emit_uvalue (logbuffer, entry->offset);
3404 emit_uvalue (logbuffer, entry->counter);
3405 emit_uvalue (logbuffer, entry->line);
3406 emit_uvalue (logbuffer, entry->column);
3408 EXIT_LOG (logbuffer);
3409 safe_send (prof, logbuffer);
3414 g_free (class_name);
3416 g_ptr_array_foreach (coverage_data, free_coverage_entry, NULL);
3417 g_ptr_array_free (coverage_data, TRUE);
3418 coverage_data = NULL;
3421 /* This empties the queue */
3423 count_queue (MonoLockFreeQueue *queue)
3425 MonoLockFreeQueueNode *node;
3428 while ((node = mono_lock_free_queue_dequeue (queue))) {
3430 mono_thread_hazardous_try_free (node, free);
3437 build_class_buffer (gpointer key, gpointer value, gpointer userdata)
3439 MonoClass *klass = (MonoClass *)key;
3440 MonoLockFreeQueue *class_methods = (MonoLockFreeQueue *)value;
3441 MonoProfiler *prof = (MonoProfiler *)userdata;
3444 const char *assembly_name;
3445 int number_of_methods, partially_covered;
3446 guint fully_covered;
3447 LogBuffer *logbuffer;
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 logbuffer = ensure_logbuf (
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 ENTER_LOG (logbuffer, "coverage-class");
3469 emit_byte (logbuffer, TYPE_COVERAGE_CLASS | TYPE_COVERAGE);
3470 emit_string (logbuffer, assembly_name, strlen (assembly_name) + 1);
3471 emit_string (logbuffer, class_name, strlen (class_name) + 1);
3472 emit_uvalue (logbuffer, number_of_methods);
3473 emit_uvalue (logbuffer, fully_covered);
3474 emit_uvalue (logbuffer, partially_covered);
3475 EXIT_LOG (logbuffer);
3477 safe_send (prof, logbuffer);
3479 g_free (class_name);
3483 get_coverage_for_image (MonoImage *image, int *number_of_methods, guint *fully_covered, int *partially_covered)
3485 MonoLockFreeQueue *image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
3487 *number_of_methods = mono_image_get_table_rows (image, MONO_TABLE_METHOD);
3489 *fully_covered = count_queue (image_methods);
3493 // FIXME: We don't handle partially covered yet.
3494 *partially_covered = 0;
3498 build_assembly_buffer (gpointer key, gpointer value, gpointer userdata)
3500 MonoAssembly *assembly = (MonoAssembly *)value;
3501 MonoProfiler *prof = (MonoProfiler *)userdata;
3502 MonoImage *image = mono_assembly_get_image (assembly);
3503 LogBuffer *logbuffer;
3504 const char *name, *guid, *filename;
3505 int number_of_methods = 0, partially_covered = 0;
3506 guint fully_covered = 0;
3508 name = mono_image_get_name (image);
3509 guid = mono_image_get_guid (image);
3510 filename = mono_image_get_filename (image);
3512 name = name ? name : "";
3513 guid = guid ? guid : "";
3514 filename = filename ? filename : "";
3516 get_coverage_for_image (image, &number_of_methods, &fully_covered, &partially_covered);
3518 logbuffer = ensure_logbuf (
3519 EVENT_SIZE /* event */ +
3520 strlen (name) + 1 /* name */ +
3521 strlen (guid) + 1 /* guid */ +
3522 strlen (filename) + 1 /* file name */ +
3523 LEB128_SIZE /* no. methods */ +
3524 LEB128_SIZE /* fully covered */ +
3525 LEB128_SIZE /* partially covered */
3528 ENTER_LOG (logbuffer, "coverage-assemblies");
3529 emit_byte (logbuffer, TYPE_COVERAGE_ASSEMBLY | TYPE_COVERAGE);
3530 emit_string (logbuffer, name, strlen (name) + 1);
3531 emit_string (logbuffer, guid, strlen (guid) + 1);
3532 emit_string (logbuffer, filename, strlen (filename) + 1);
3533 emit_uvalue (logbuffer, number_of_methods);
3534 emit_uvalue (logbuffer, fully_covered);
3535 emit_uvalue (logbuffer, partially_covered);
3536 EXIT_LOG (logbuffer);
3538 safe_send (prof, logbuffer);
3542 dump_coverage (MonoProfiler *prof)
3544 if (!coverage_initialized)
3547 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Started dump\n");)
3550 mono_os_mutex_lock (&coverage_mutex);
3551 mono_conc_hashtable_foreach (coverage_assemblies, build_assembly_buffer, prof);
3552 mono_conc_hashtable_foreach (coverage_classes, build_class_buffer, prof);
3553 mono_conc_hashtable_foreach (coverage_methods, build_method_buffer, prof);
3554 mono_os_mutex_unlock (&coverage_mutex);
3556 COVERAGE_DEBUG(fprintf (stderr, "Coverage: Finished dump\n");)
3560 process_method_enter_coverage (MonoProfiler *prof, MonoMethod *method)
3565 if (!coverage_initialized)
3568 klass = mono_method_get_class (method);
3569 image = mono_class_get_image (klass);
3571 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)))
3574 mono_os_mutex_lock (&coverage_mutex);
3575 mono_conc_hashtable_insert (entered_methods, method, method);
3576 mono_os_mutex_unlock (&coverage_mutex);
3579 static MonoLockFreeQueueNode *
3580 create_method_node (MonoMethod *method)
3582 MethodNode *node = (MethodNode *)g_malloc (sizeof (MethodNode));
3583 mono_lock_free_queue_node_init ((MonoLockFreeQueueNode *) node, FALSE);
3584 node->method = method;
3586 return (MonoLockFreeQueueNode *) node;
3590 coverage_filter (MonoProfiler *prof, MonoMethod *method)
3595 MonoAssembly *assembly;
3596 MonoMethodHeader *header;
3597 guint32 iflags, flags, code_size;
3598 char *fqn, *classname;
3599 gboolean has_positive, found;
3600 MonoLockFreeQueue *image_methods, *class_methods;
3601 MonoLockFreeQueueNode *node;
3603 if (!coverage_initialized)
3606 COVERAGE_DEBUG(fprintf (stderr, "Coverage filter for %s\n", mono_method_get_name (method));)
3608 flags = mono_method_get_flags (method, &iflags);
3609 if ((iflags & 0x1000 /*METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL*/) ||
3610 (flags & 0x2000 /*METHOD_ATTRIBUTE_PINVOKE_IMPL*/)) {
3611 COVERAGE_DEBUG(fprintf (stderr, " Internal call or pinvoke - ignoring\n");)
3615 // Don't need to do anything else if we're already tracking this method
3616 if (mono_conc_hashtable_lookup (coverage_methods, method)) {
3617 COVERAGE_DEBUG(fprintf (stderr, " Already tracking\n");)
3621 klass = mono_method_get_class (method);
3622 image = mono_class_get_image (klass);
3624 // Don't handle coverage for the core assemblies
3625 if (mono_conc_hashtable_lookup (suppressed_assemblies, (gpointer) mono_image_get_name (image)) != NULL)
3628 if (prof->coverage_filters) {
3629 /* Check already filtered classes first */
3630 if (mono_conc_hashtable_lookup (filtered_classes, klass)) {
3631 COVERAGE_DEBUG(fprintf (stderr, " Already filtered\n");)
3635 classname = mono_type_get_name (mono_class_get_type (klass));
3637 fqn = g_strdup_printf ("[%s]%s", mono_image_get_name (image), classname);
3639 COVERAGE_DEBUG(fprintf (stderr, " Looking for %s in filter\n", fqn);)
3640 // Check positive filters first
3641 has_positive = FALSE;
3643 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3644 char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
3646 if (filter [0] == '+') {
3647 filter = &filter [1];
3649 COVERAGE_DEBUG(fprintf (stderr, " Checking against +%s ...", filter);)
3651 if (strstr (fqn, filter) != NULL) {
3652 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3655 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3657 has_positive = TRUE;
3661 if (has_positive && !found) {
3662 COVERAGE_DEBUG(fprintf (stderr, " Positive match was not found\n");)
3664 mono_os_mutex_lock (&coverage_mutex);
3665 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3666 mono_os_mutex_unlock (&coverage_mutex);
3673 for (guint i = 0; i < prof->coverage_filters->len; ++i) {
3674 // FIXME: Is substring search sufficient?
3675 char *filter = (char *)g_ptr_array_index (prof->coverage_filters, i);
3676 if (filter [0] == '+')
3680 filter = &filter [1];
3681 COVERAGE_DEBUG(fprintf (stderr, " Checking against -%s ...", filter);)
3683 if (strstr (fqn, filter) != NULL) {
3684 COVERAGE_DEBUG(fprintf (stderr, "matched\n");)
3686 mono_os_mutex_lock (&coverage_mutex);
3687 mono_conc_hashtable_insert (filtered_classes, klass, klass);
3688 mono_os_mutex_unlock (&coverage_mutex);
3694 COVERAGE_DEBUG(fprintf (stderr, "no match\n");)
3702 COVERAGE_DEBUG(fprintf (stderr, " Handling coverage for %s\n", mono_method_get_name (method));)
3703 header = mono_method_get_header_checked (method, &error);
3704 mono_error_cleanup (&error);
3706 mono_method_header_get_code (header, &code_size, NULL);
3708 assembly = mono_image_get_assembly (image);
3710 // Need to keep the assemblies around for as long as they are kept in the hashtable
3711 // Nunit, for example, has a habit of unloading them before the coverage statistics are
3712 // generated causing a crash. See https://bugzilla.xamarin.com/show_bug.cgi?id=39325
3713 mono_assembly_addref (assembly);
3715 mono_os_mutex_lock (&coverage_mutex);
3716 mono_conc_hashtable_insert (coverage_methods, method, method);
3717 mono_conc_hashtable_insert (coverage_assemblies, assembly, assembly);
3718 mono_os_mutex_unlock (&coverage_mutex);
3720 image_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (image_to_methods, image);
3722 if (image_methods == NULL) {
3723 image_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
3724 mono_lock_free_queue_init (image_methods);
3725 mono_os_mutex_lock (&coverage_mutex);
3726 mono_conc_hashtable_insert (image_to_methods, image, image_methods);
3727 mono_os_mutex_unlock (&coverage_mutex);
3730 node = create_method_node (method);
3731 mono_lock_free_queue_enqueue (image_methods, node);
3733 class_methods = (MonoLockFreeQueue *)mono_conc_hashtable_lookup (coverage_classes, klass);
3735 if (class_methods == NULL) {
3736 class_methods = (MonoLockFreeQueue *)g_malloc (sizeof (MonoLockFreeQueue));
3737 mono_lock_free_queue_init (class_methods);
3738 mono_os_mutex_lock (&coverage_mutex);
3739 mono_conc_hashtable_insert (coverage_classes, klass, class_methods);
3740 mono_os_mutex_unlock (&coverage_mutex);
3743 node = create_method_node (method);
3744 mono_lock_free_queue_enqueue (class_methods, node);
3749 #define LINE_BUFFER_SIZE 4096
3750 /* Max file limit of 128KB */
3751 #define MAX_FILE_SIZE 128 * 1024
3753 get_file_content (FILE *stream)
3758 int res, offset = 0;
3760 res = fseek (stream, 0, SEEK_END);
3764 filesize = ftell (stream);
3768 res = fseek (stream, 0, SEEK_SET);
3772 if (filesize > MAX_FILE_SIZE)
3775 buffer = (char *)g_malloc ((filesize + 1) * sizeof (char));
3776 while ((bytes_read = fread (buffer + offset, 1, LINE_BUFFER_SIZE, stream)) > 0)
3777 offset += bytes_read;
3779 /* NULL terminate our buffer */
3780 buffer[filesize] = '\0';
3785 get_next_line (char *contents, char **next_start)
3789 if (p == NULL || *p == '\0') {
3794 while (*p != '\n' && *p != '\0')
3799 *next_start = p + 1;
3807 init_suppressed_assemblies (void)
3813 suppressed_assemblies = mono_conc_hashtable_new (g_str_hash, g_str_equal);
3814 sa_file = fopen (SUPPRESSION_DIR "/mono-profiler-log.suppression", "r");
3815 if (sa_file == NULL)
3818 /* Don't need to free @content as it is referred to by the lines stored in @suppressed_assemblies */
3819 content = get_file_content (sa_file);
3820 if (content == NULL) {
3821 g_error ("mono-profiler-log.suppression is greater than 128kb - aborting\n");
3824 while ((line = get_next_line (content, &content))) {
3825 line = g_strchomp (g_strchug (line));
3826 /* No locking needed as we're doing initialization */
3827 mono_conc_hashtable_insert (suppressed_assemblies, line, line);
3833 #endif /* DISABLE_HELPER_THREAD */
3836 coverage_init (MonoProfiler *prof)
3838 #ifndef DISABLE_HELPER_THREAD
3839 assert (!coverage_initialized);
3841 COVERAGE_DEBUG(fprintf (stderr, "Coverage initialized\n");)
3843 mono_os_mutex_init (&coverage_mutex);
3844 coverage_methods = mono_conc_hashtable_new (NULL, NULL);
3845 coverage_assemblies = mono_conc_hashtable_new (NULL, NULL);
3846 coverage_classes = mono_conc_hashtable_new (NULL, NULL);
3847 filtered_classes = mono_conc_hashtable_new (NULL, NULL);
3848 entered_methods = mono_conc_hashtable_new (NULL, NULL);
3849 image_to_methods = mono_conc_hashtable_new (NULL, NULL);
3850 init_suppressed_assemblies ();
3852 coverage_initialized = TRUE;
3853 #endif /* DISABLE_HELPER_THREAD */
3857 unref_coverage_assemblies (gpointer key, gpointer value, gpointer userdata)
3859 MonoAssembly *assembly = (MonoAssembly *)value;
3860 mono_assembly_close (assembly);
3864 free_sample_hit (gpointer p)
3866 mono_lock_free_free (p, SAMPLE_BLOCK_SIZE);
3870 cleanup_reusable_samples (MonoProfiler *prof)
3874 while ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->sample_reuse_queue)))
3875 mono_thread_hazardous_try_free (sample, free_sample_hit);
3879 log_shutdown (MonoProfiler *prof)
3884 #ifndef DISABLE_HELPER_THREAD
3885 counters_and_perfcounters_sample (prof, FALSE);
3887 dump_coverage (prof);
3889 if (prof->command_port) {
3891 ign_res (write (prof->pipes [1], &c, 1));
3892 pthread_join (prof->helper_thread, &res);
3898 for (i = 0; i < num_perf; ++i)
3899 read_perf_mmap (prof, i);
3903 if (TLS_GET (LogBuffer, tlsbuffer))
3904 send_buffer (prof, TLS_GET (GPtrArray, tlsmethodlist), TLS_GET (LogBuffer, tlsbuffer));
3906 TLS_SET (tlsbuffer, NULL);
3907 TLS_SET (tlsmethodlist, NULL);
3909 InterlockedWrite (&prof->run_dumper_thread, 0);
3910 mono_os_sem_post (&prof->dumper_queue_sem);
3911 pthread_join (prof->dumper_thread, &res);
3912 mono_os_sem_destroy (&prof->dumper_queue_sem);
3914 InterlockedWrite (&prof->run_writer_thread, 0);
3915 mono_os_sem_post (&prof->writer_queue_sem);
3916 pthread_join (prof->writer_thread, &res);
3917 mono_os_sem_destroy (&prof->writer_queue_sem);
3919 cleanup_reusable_samples (prof);
3921 #if defined (HAVE_SYS_ZLIB)
3923 gzclose (prof->gzfile);
3925 if (prof->pipe_output)
3926 pclose (prof->file);
3928 fclose (prof->file);
3930 mono_conc_hashtable_destroy (prof->method_table);
3931 mono_os_mutex_destroy (&prof->method_table_mutex);
3933 if (coverage_initialized) {
3934 mono_os_mutex_lock (&coverage_mutex);
3935 mono_conc_hashtable_foreach (coverage_assemblies, unref_coverage_assemblies, prof);
3936 mono_os_mutex_unlock (&coverage_mutex);
3938 mono_conc_hashtable_destroy (coverage_methods);
3939 mono_conc_hashtable_destroy (coverage_assemblies);
3940 mono_conc_hashtable_destroy (coverage_classes);
3941 mono_conc_hashtable_destroy (filtered_classes);
3943 mono_conc_hashtable_destroy (entered_methods);
3944 mono_conc_hashtable_destroy (image_to_methods);
3945 mono_conc_hashtable_destroy (suppressed_assemblies);
3946 mono_os_mutex_destroy (&coverage_mutex);
3953 new_filename (const char* filename)
3955 time_t t = time (NULL);
3956 int pid = process_id ();
3961 int count_dates = 0;
3965 for (p = filename; *p; p++) {
3976 if (!count_dates && !count_pids)
3977 return pstrdup (filename);
3978 snprintf (pid_buf, sizeof (pid_buf), "%d", pid);
3980 snprintf (time_buf, sizeof (time_buf), "%d%02d%02d%02d%02d%02d",
3981 1900 + ts->tm_year, 1 + ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
3982 s_date = strlen (time_buf);
3983 s_pid = strlen (pid_buf);
3984 d = res = (char *)malloc (strlen (filename) + s_date * count_dates + s_pid * count_pids);
3985 for (p = filename; *p; p++) {
3992 strcpy (d, time_buf);
3995 } else if (*p == 'p') {
3996 strcpy (d, pid_buf);
3999 } else if (*p == '%') {
4011 //this is exposed by the JIT, but it's not meant to be a supported API for now.
4012 extern void mono_threads_attach_tools_thread (void);
4014 #ifndef DISABLE_HELPER_THREAD
4017 helper_thread (void* arg)
4019 MonoProfiler* prof = (MonoProfiler *)arg;
4023 MonoThread *thread = NULL;
4025 mono_threads_attach_tools_thread ();
4026 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler helper");
4028 //fprintf (stderr, "Server listening\n");
4029 command_socket = -1;
4035 FD_SET (prof->server_socket, &rfds);
4036 max_fd = prof->server_socket;
4037 FD_SET (prof->pipes [0], &rfds);
4038 if (max_fd < prof->pipes [0])
4039 max_fd = prof->pipes [0];
4040 if (command_socket >= 0) {
4041 FD_SET (command_socket, &rfds);
4042 if (max_fd < command_socket)
4043 max_fd = command_socket;
4048 for ( i = 0; i < num_perf; ++i) {
4049 if (perf_data [i].perf_fd < 0)
4051 FD_SET (perf_data [i].perf_fd, &rfds);
4052 if (max_fd < perf_data [i].perf_fd)
4053 max_fd = perf_data [i].perf_fd;
4058 counters_and_perfcounters_sample (prof, TRUE);
4062 len = select (max_fd + 1, &rfds, NULL, NULL, &tv);
4068 g_warning ("Error in proflog server: %s", strerror (errno));
4072 if (FD_ISSET (prof->pipes [0], &rfds)) {
4074 read (prof->pipes [0], &c, 1);
4076 mono_thread_detach (thread);
4078 fprintf (stderr, "helper shutdown\n");
4082 for ( i = 0; i < num_perf; ++i) {
4083 if (perf_data [i].perf_fd < 0)
4085 if (FD_ISSET (perf_data [i].perf_fd, &rfds))
4086 read_perf_mmap (prof, i);
4090 safe_send_threadless (prof, ensure_logbuf (0));
4096 for ( i = 0; i < num_perf; ++i) {
4097 if (perf_data [i].perf_fd < 0)
4099 if (FD_ISSET (perf_data [i].perf_fd, &rfds)) {
4100 read_perf_mmap (prof, i);
4101 safe_send_threadless (prof, ensure_logbuf (0));
4106 if (command_socket >= 0 && FD_ISSET (command_socket, &rfds)) {
4107 len = read (command_socket, buf, sizeof (buf) - 1);
4111 close (command_socket);
4112 command_socket = -1;
4116 if (strcmp (buf, "heapshot\n") == 0) {
4117 heapshot_requested = 1;
4118 //fprintf (stderr, "perform heapshot\n");
4119 if (InterlockedRead (&runtime_inited) && !thread) {
4120 thread = mono_thread_attach (mono_get_root_domain ());
4121 /*fprintf (stderr, "attached\n");*/
4124 process_requests (prof);
4125 mono_thread_detach (thread);
4131 if (!FD_ISSET (prof->server_socket, &rfds)) {
4134 command_socket = accept (prof->server_socket, NULL, NULL);
4135 if (command_socket < 0)
4137 //fprintf (stderr, "Accepted connection\n");
4140 mono_thread_info_detach ();
4146 start_helper_thread (MonoProfiler* prof)
4148 struct sockaddr_in server_address;
4151 if (pipe (prof->pipes) < 0) {
4152 fprintf (stderr, "Cannot create pipe\n");
4155 prof->server_socket = socket (PF_INET, SOCK_STREAM, 0);
4156 if (prof->server_socket < 0) {
4157 fprintf (stderr, "Cannot create server socket\n");
4160 memset (&server_address, 0, sizeof (server_address));
4161 server_address.sin_family = AF_INET;
4162 server_address.sin_addr.s_addr = INADDR_ANY;
4163 server_address.sin_port = htons (prof->command_port);
4164 if (bind (prof->server_socket, (struct sockaddr *) &server_address, sizeof (server_address)) < 0) {
4165 fprintf (stderr, "Cannot bind server socket, port: %d: %s\n", prof->command_port, strerror (errno));
4166 close (prof->server_socket);
4169 if (listen (prof->server_socket, 1) < 0) {
4170 fprintf (stderr, "Cannot listen server socket\n");
4171 close (prof->server_socket);
4174 slen = sizeof (server_address);
4175 if (getsockname (prof->server_socket, (struct sockaddr *)&server_address, &slen) == 0) {
4176 prof->command_port = ntohs (server_address.sin_port);
4177 /*fprintf (stderr, "Assigned server port: %d\n", prof->command_port);*/
4180 r = pthread_create (&prof->helper_thread, NULL, helper_thread, prof);
4182 close (prof->server_socket);
4190 handle_writer_queue_entry (MonoProfiler *prof)
4192 WriterQueueEntry *entry;
4194 if ((entry = (WriterQueueEntry *) mono_lock_free_queue_dequeue (&prof->writer_queue))) {
4195 LogBuffer *method_buffer = NULL;
4196 gboolean new_methods = FALSE;
4198 if (entry->methods->len)
4199 method_buffer = create_buffer ();
4202 * Encode the method events in a temporary log buffer that we
4203 * flush to disk before the main buffer, ensuring that all
4204 * methods have metadata emitted before they're referenced.
4206 for (guint i = 0; i < entry->methods->len; i++) {
4207 MethodInfo *info = (MethodInfo *)g_ptr_array_index (entry->methods, i);
4209 if (mono_conc_hashtable_lookup (prof->method_table, info->method))
4215 * Other threads use this hash table to get a general
4216 * idea of whether a method has already been emitted to
4217 * the stream. Due to the way we add to this table, it
4218 * can easily happen that multiple threads queue up the
4219 * same methods, but that's OK since eventually all
4220 * methods will be in this table and the thread-local
4221 * method lists will just be empty for the rest of the
4224 mono_os_mutex_lock (&prof->method_table_mutex);
4225 mono_conc_hashtable_insert (prof->method_table, info->method, info->method);
4226 mono_os_mutex_unlock (&prof->method_table_mutex);
4228 char *name = mono_method_full_name (info->method, 1);
4229 int nlen = strlen (name) + 1;
4230 void *cstart = info->ji ? mono_jit_info_get_code_start (info->ji) : NULL;
4231 int csize = info->ji ? mono_jit_info_get_code_size (info->ji) : 0;
4233 method_buffer = ensure_logbuf_inner (method_buffer,
4234 EVENT_SIZE /* event */ +
4235 LEB128_SIZE /* time */ +
4236 LEB128_SIZE /* method */ +
4237 LEB128_SIZE /* start */ +
4238 LEB128_SIZE /* size */ +
4242 emit_byte (method_buffer, TYPE_JIT | TYPE_METHOD);
4243 emit_time (method_buffer, info->time);
4244 emit_method_inner (method_buffer, info->method);
4245 emit_ptr (method_buffer, cstart);
4246 emit_value (method_buffer, csize);
4248 memcpy (method_buffer->cursor, name, nlen);
4249 method_buffer->cursor += nlen;
4255 g_ptr_array_free (entry->methods, TRUE);
4258 for (LogBuffer *iter = method_buffer; iter; iter = iter->next)
4259 iter->thread_id = 0;
4261 dump_buffer (prof, method_buffer);
4262 } else if (method_buffer)
4263 free_buffer (method_buffer, method_buffer->size);
4265 dump_buffer (prof, entry->buffer);
4267 mono_thread_hazardous_try_free (entry, free);
4276 writer_thread (void *arg)
4278 MonoProfiler *prof = (MonoProfiler *)arg;
4280 mono_threads_attach_tools_thread ();
4281 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler writer");
4285 while (InterlockedRead (&prof->run_writer_thread)) {
4286 mono_os_sem_wait (&prof->writer_queue_sem, MONO_SEM_FLAGS_NONE);
4287 handle_writer_queue_entry (prof);
4290 /* Drain any remaining entries on shutdown. */
4291 while (handle_writer_queue_entry (prof));
4293 mono_thread_info_detach ();
4299 start_writer_thread (MonoProfiler* prof)
4301 InterlockedWrite (&prof->run_writer_thread, 1);
4303 return !pthread_create (&prof->writer_thread, NULL, writer_thread, prof);
4307 reuse_sample_hit (gpointer p)
4309 SampleHit *sample = p;
4311 mono_lock_free_queue_node_unpoison (&sample->node);
4312 mono_lock_free_queue_enqueue (&sample->prof->sample_reuse_queue, &sample->node);
4316 handle_dumper_queue_entry (MonoProfiler *prof)
4320 if ((sample = (SampleHit *) mono_lock_free_queue_dequeue (&prof->dumper_queue))) {
4321 for (int i = 0; i < sample->count; ++i) {
4322 MonoMethod *method = sample->frames [i].method;
4323 MonoDomain *domain = sample->frames [i].domain;
4324 void *address = sample->frames [i].base_address;
4330 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *) address);
4333 sample->frames [i].method = mono_jit_info_get_method (ji);
4337 LogBuffer *logbuffer = ensure_logbuf (
4338 EVENT_SIZE /* event */ +
4339 LEB128_SIZE /* type */ +
4340 LEB128_SIZE /* time */ +
4341 LEB128_SIZE /* tid */ +
4342 LEB128_SIZE /* count */ +
4344 LEB128_SIZE /* ip */
4346 LEB128_SIZE /* managed count */ +
4348 LEB128_SIZE /* method */ +
4349 LEB128_SIZE /* il offset */ +
4350 LEB128_SIZE /* native offset */
4354 emit_byte (logbuffer, TYPE_SAMPLE | TYPE_SAMPLE_HIT);
4355 emit_value (logbuffer, sample_type);
4356 emit_uvalue (logbuffer, prof->startup_time + sample->elapsed * 10000);
4357 emit_ptr (logbuffer, (void *) sample->tid);
4358 emit_value (logbuffer, 1);
4360 // TODO: Actual native unwinding.
4361 for (int i = 0; i < 1; ++i) {
4362 emit_ptr (logbuffer, sample->ip);
4363 add_code_pointer ((uintptr_t) sample->ip);
4366 /* new in data version 6 */
4367 emit_uvalue (logbuffer, sample->count);
4369 for (int i = 0; i < sample->count; ++i) {
4370 emit_method (prof, logbuffer, sample->frames [i].method);
4371 emit_svalue (logbuffer, 0); /* il offset will always be 0 from now on */
4372 emit_svalue (logbuffer, sample->frames [i].offset);
4375 mono_thread_hazardous_try_free (sample, reuse_sample_hit);
4377 dump_unmanaged_coderefs (prof);
4379 if (logbuffer->next)
4380 safe_send_threadless (prof, logbuffer);
4387 dumper_thread (void *arg)
4389 MonoProfiler *prof = (MonoProfiler *)arg;
4391 mono_threads_attach_tools_thread ();
4392 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler dumper");
4394 while (InterlockedRead (&prof->run_dumper_thread)) {
4395 mono_os_sem_wait (&prof->dumper_queue_sem, MONO_SEM_FLAGS_NONE);
4396 handle_dumper_queue_entry (prof);
4399 /* Drain any remaining entries on shutdown. */
4400 while (handle_dumper_queue_entry (prof));
4402 safe_send_threadless (prof, ensure_logbuf (0));
4404 mono_thread_info_detach ();
4410 start_dumper_thread (MonoProfiler* prof)
4412 InterlockedWrite (&prof->run_dumper_thread, 1);
4414 return !pthread_create (&prof->dumper_thread, NULL, dumper_thread, prof);
4418 runtime_initialized (MonoProfiler *profiler)
4420 #ifndef DISABLE_HELPER_THREAD
4421 if (hs_mode_ondemand || need_helper_thread) {
4422 if (!start_helper_thread (profiler))
4423 profiler->command_port = 0;
4427 start_writer_thread (profiler);
4428 start_dumper_thread (profiler);
4430 InterlockedWrite (&runtime_inited, 1);
4431 #ifndef DISABLE_HELPER_THREAD
4432 counters_init (profiler);
4433 counters_sample (profiler, 0, FALSE);
4435 /* ensure the main thread data and startup are available soon */
4436 safe_send (profiler, ensure_logbuf (0));
4439 static MonoProfiler*
4440 create_profiler (const char *filename, GPtrArray *filters)
4444 int force_delete = 0;
4445 prof = (MonoProfiler *)calloc (1, sizeof (MonoProfiler));
4447 prof->command_port = command_port;
4448 if (filename && *filename == '-') {
4454 filename = "|mprof-report -";
4456 filename = "output.mlpd";
4457 nf = (char*)filename;
4459 nf = new_filename (filename);
4461 int s = strlen (nf) + 32;
4462 char *p = (char *)malloc (s);
4463 snprintf (p, s, "|mprof-report '--out=%s' -", nf);
4469 prof->file = popen (nf + 1, "w");
4470 prof->pipe_output = 1;
4471 } else if (*nf == '#') {
4472 int fd = strtol (nf + 1, NULL, 10);
4473 prof->file = fdopen (fd, "a");
4477 prof->file = fopen (nf, "wb");
4480 fprintf (stderr, "Cannot create profiler output: %s\n", nf);
4483 #if defined (HAVE_SYS_ZLIB)
4485 prof->gzfile = gzdopen (fileno (prof->file), "wb");
4488 if (sample_type && !do_mono_sample)
4489 need_helper_thread = setup_perf_event ();
4491 /* FIXME: warn if different freq or sample type */
4495 if (do_mono_sample) {
4496 need_helper_thread = 1;
4498 if (do_counters && !need_helper_thread) {
4499 need_helper_thread = 1;
4503 * If you hit this assert while increasing MAX_FRAMES, you need to increase
4504 * SAMPLE_BLOCK_SIZE as well.
4506 g_assert (SAMPLE_SLOT_SIZE (MAX_FRAMES) * 2 < LOCK_FREE_ALLOC_SB_USABLE_SIZE (SAMPLE_BLOCK_SIZE));
4508 // FIXME: We should free this stuff too.
4509 mono_lock_free_allocator_init_size_class (&prof->sample_size_class, SAMPLE_SLOT_SIZE (num_frames), SAMPLE_BLOCK_SIZE);
4510 mono_lock_free_allocator_init_allocator (&prof->sample_allocator, &prof->sample_size_class);
4512 mono_lock_free_queue_init (&prof->sample_reuse_queue);
4514 #ifdef DISABLE_HELPER_THREAD
4515 if (hs_mode_ondemand)
4516 fprintf (stderr, "Ondemand heapshot unavailable on this arch.\n");
4519 fprintf (stderr, "Coverage unavailable on this arch.\n");
4523 mono_lock_free_queue_init (&prof->writer_queue);
4524 mono_os_sem_init (&prof->writer_queue_sem, 0);
4526 mono_lock_free_queue_init (&prof->dumper_queue);
4527 mono_os_sem_init (&prof->dumper_queue_sem, 0);
4529 mono_os_mutex_init (&prof->method_table_mutex);
4530 prof->method_table = mono_conc_hashtable_new (NULL, NULL);
4533 coverage_init (prof);
4534 prof->coverage_filters = filters;
4536 prof->startup_time = current_time ();
4543 printf ("Log profiler version %d.%d (format: %d)\n", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
4544 printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
4545 printf ("Options:\n");
4546 printf ("\thelp show this usage info\n");
4547 printf ("\t[no]alloc enable/disable recording allocation info\n");
4548 printf ("\t[no]calls enable/disable recording enter/leave method events\n");
4549 printf ("\theapshot[=MODE] record heap shot info (by default at each major collection)\n");
4550 printf ("\t MODE: every XXms milliseconds, every YYgc collections, ondemand\n");
4551 printf ("\tcounters sample counters every 1s\n");
4552 printf ("\tsample[=TYPE] use statistical sampling mode (by default cycles/100)\n");
4553 printf ("\t TYPE: cycles,instr,cacherefs,cachemiss,branches,branchmiss\n");
4554 printf ("\t TYPE can be followed by /FREQUENCY\n");
4555 printf ("\ttime=fast use a faster (but more inaccurate) timer\n");
4556 printf ("\tmaxframes=NUM collect up to NUM stack frames\n");
4557 printf ("\tcalldepth=NUM ignore method events for call chain depth bigger than NUM\n");
4558 printf ("\toutput=FILENAME write the data to file FILENAME (-FILENAME to overwrite)\n");
4559 printf ("\toutput=|PROGRAM write the data to the stdin of PROGRAM\n");
4560 printf ("\t %%t is subtituted with date and time, %%p with the pid\n");
4561 printf ("\treport create a report instead of writing the raw data to a file\n");
4562 printf ("\tzip compress the output data\n");
4563 printf ("\tport=PORTNUM use PORTNUM for the listening command server\n");
4564 printf ("\tcoverage enable collection of code coverage data\n");
4565 printf ("\tcovfilter=ASSEMBLY add an assembly to the code coverage filters\n");
4566 printf ("\t add a + to include the assembly or a - to exclude it\n");
4567 printf ("\t filter=-mscorlib\n");
4568 printf ("\tcovfilter-file=FILE use FILE to generate the list of assemblies to be filtered\n");
4574 match_option (const char* p, const char *opt, char **rval)
4576 int len = strlen (opt);
4577 if (strncmp (p, opt, len) == 0) {
4579 if (p [len] == '=' && p [len + 1]) {
4580 const char *opt = p + len + 1;
4581 const char *end = strchr (opt, ',');
4589 val = (char *)malloc (l + 1);
4590 memcpy (val, opt, l);
4595 if (p [len] == 0 || p [len] == ',') {
4597 return p + len + (p [len] == ',');
4615 static const SampleMode sample_modes [] = {
4616 {"cycles", SAMPLE_CYCLES},
4617 {"instr", SAMPLE_INSTRUCTIONS},
4618 {"cachemiss", SAMPLE_CACHE_MISSES},
4619 {"cacherefs", SAMPLE_CACHE_REFS},
4620 {"branches", SAMPLE_BRANCHES},
4621 {"branchmiss", SAMPLE_BRANCH_MISSES},
4626 set_sample_mode (char* val, int allow_empty)
4629 char *maybe_freq = NULL;
4631 const SampleMode *smode = sample_modes;
4632 #ifndef USE_PERF_EVENTS
4635 if (allow_empty && !val) {
4636 sample_type = SAMPLE_CYCLES;
4640 if (strcmp (val, "mono") == 0) {
4642 sample_type = SAMPLE_CYCLES;
4646 for (smode = sample_modes; smode->name; smode++) {
4647 int l = strlen (smode->name);
4648 if (strncmp (val, smode->name, l) == 0) {
4649 sample_type = smode->sample_mode;
4650 maybe_freq = val + l;
4656 if (*maybe_freq == '/') {
4657 count = strtoul (maybe_freq + 1, &end, 10);
4658 if (maybe_freq + 1 == end)
4660 sample_freq = count;
4661 } else if (*maybe_freq != 0) {
4670 set_hsmode (char* val, int allow_empty)
4674 if (allow_empty && !val)
4676 if (strcmp (val, "ondemand") == 0) {
4677 hs_mode_ondemand = 1;
4681 count = strtoul (val, &end, 10);
4684 if (strcmp (end, "ms") == 0)
4686 else if (strcmp (end, "gc") == 0)
4694 * declaration to silence the compiler: this is the entry point that
4695 * mono will load from the shared library and call.
4698 mono_profiler_startup (const char *desc);
4701 mono_profiler_startup_log (const char *desc);
4704 * this is the entry point that will be used when the profiler
4705 * is embedded inside the main executable.
4708 mono_profiler_startup_log (const char *desc)
4710 mono_profiler_startup (desc);
4714 mono_profiler_startup (const char *desc)
4717 GPtrArray *filters = NULL;
4718 char *filename = NULL;
4722 int calls_enabled = 0;
4723 int allocs_enabled = 0;
4724 int only_counters = 0;
4725 int only_coverage = 0;
4726 int events = MONO_PROFILE_GC|MONO_PROFILE_ALLOCATIONS|
4727 MONO_PROFILE_GC_MOVES|MONO_PROFILE_CLASS_EVENTS|MONO_PROFILE_THREADS|
4728 MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_EXCEPTIONS|
4729 MONO_PROFILE_MONITOR_EVENTS|MONO_PROFILE_MODULE_EVENTS|MONO_PROFILE_GC_ROOTS|
4730 MONO_PROFILE_INS_COVERAGE|MONO_PROFILE_APPDOMAIN_EVENTS|MONO_PROFILE_CONTEXT_EVENTS|
4731 MONO_PROFILE_ASSEMBLY_EVENTS;
4733 max_allocated_sample_hits = mono_cpu_count () * 1000;
4735 mono_counters_register ("Sample hits", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_hits);
4736 mono_counters_register ("Sample flushes", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_flushes);
4737 mono_counters_register ("Sample events allocated", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &sample_allocations);
4738 mono_counters_register ("Log buffers allocated", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &buffer_allocations);
4739 mono_counters_register ("Thread start events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &thread_starts);
4740 mono_counters_register ("Thread stop events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &thread_ends);
4741 mono_counters_register ("Domain load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &domain_loads);
4742 mono_counters_register ("Domain unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &domain_unloads);
4743 mono_counters_register ("Context load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &context_loads);
4744 mono_counters_register ("Context unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &context_unloads);
4745 mono_counters_register ("Assembly load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &assembly_loads);
4746 mono_counters_register ("Assembly unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &assembly_unloads);
4747 mono_counters_register ("Image load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &image_loads);
4748 mono_counters_register ("Image unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &image_unloads);
4749 mono_counters_register ("Class load events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &class_loads);
4750 mono_counters_register ("Class unload events", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &class_unloads);
4753 if (strncmp (p, "log", 3))
4758 for (; *p; p = opt) {
4764 if ((opt = match_option (p, "help", NULL)) != p) {
4768 if ((opt = match_option (p, "calls", NULL)) != p) {
4772 if ((opt = match_option (p, "nocalls", NULL)) != p) {
4773 events &= ~MONO_PROFILE_ENTER_LEAVE;
4777 if ((opt = match_option (p, "alloc", NULL)) != p) {
4781 if ((opt = match_option (p, "noalloc", NULL)) != p) {
4782 events &= ~MONO_PROFILE_ALLOCATIONS;
4785 if ((opt = match_option (p, "time", &val)) != p) {
4786 if (strcmp (val, "fast") == 0)
4788 else if (strcmp (val, "null") == 0)
4795 if ((opt = match_option (p, "report", NULL)) != p) {
4799 if ((opt = match_option (p, "debug", NULL)) != p) {
4803 if ((opt = match_option (p, "sampling-real", NULL)) != p) {
4804 sampling_mode = MONO_PROFILER_STAT_MODE_REAL;
4807 if ((opt = match_option (p, "sampling-process", NULL)) != p) {
4808 sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
4811 if ((opt = match_option (p, "heapshot", &val)) != p) {
4812 events &= ~MONO_PROFILE_ALLOCATIONS;
4813 events &= ~MONO_PROFILE_ENTER_LEAVE;
4816 set_hsmode (val, 1);
4819 if ((opt = match_option (p, "sample", &val)) != p) {
4820 events &= ~MONO_PROFILE_ALLOCATIONS;
4821 events &= ~MONO_PROFILE_ENTER_LEAVE;
4823 set_sample_mode (val, 1);
4826 if ((opt = match_option (p, "hsmode", &val)) != p) {
4827 fprintf (stderr, "The hsmode profiler option is obsolete, use heapshot=MODE.\n");
4828 set_hsmode (val, 0);
4831 if ((opt = match_option (p, "zip", NULL)) != p) {
4835 if ((opt = match_option (p, "output", &val)) != p) {
4839 if ((opt = match_option (p, "port", &val)) != p) {
4841 command_port = strtoul (val, &end, 10);
4845 if ((opt = match_option (p, "maxframes", &val)) != p) {
4847 num_frames = strtoul (val, &end, 10);
4848 if (num_frames > MAX_FRAMES)
4849 num_frames = MAX_FRAMES;
4851 notraces = num_frames == 0;
4854 if ((opt = match_option (p, "maxsamples", &val)) != p) {
4856 max_allocated_sample_hits = strtoul (val, &end, 10);
4857 if (!max_allocated_sample_hits)
4858 max_allocated_sample_hits = G_MAXINT32;
4862 if ((opt = match_option (p, "calldepth", &val)) != p) {
4864 max_call_depth = strtoul (val, &end, 10);
4868 if ((opt = match_option (p, "counters", NULL)) != p) {
4872 if ((opt = match_option (p, "countersonly", NULL)) != p) {
4876 if ((opt = match_option (p, "coverage", NULL)) != p) {
4878 events |= MONO_PROFILE_ENTER_LEAVE;
4879 debug_coverage = (g_getenv ("MONO_PROFILER_DEBUG_COVERAGE") != NULL);
4882 if ((opt = match_option (p, "onlycoverage", NULL)) != p) {
4886 if ((opt = match_option (p, "covfilter-file", &val)) != p) {
4888 char *line, *content;
4890 if (filters == NULL)
4891 filters = g_ptr_array_new ();
4893 filter_file = fopen (val, "r");
4894 if (filter_file == NULL) {
4895 fprintf (stderr, "Unable to open %s\n", val);
4899 /* Don't need to free content as it is referred to by the lines stored in @filters */
4900 content = get_file_content (filter_file);
4901 if (content == NULL)
4902 fprintf (stderr, "WARNING: %s is greater than 128kb - ignoring\n", val);
4904 while ((line = get_next_line (content, &content)))
4905 g_ptr_array_add (filters, g_strchug (g_strchomp (line)));
4907 fclose (filter_file);
4910 if ((opt = match_option (p, "covfilter", &val)) != p) {
4911 if (filters == NULL)
4912 filters = g_ptr_array_new ();
4914 g_ptr_array_add (filters, val);
4922 if (calls_enabled) {
4923 events |= MONO_PROFILE_ENTER_LEAVE;
4927 events |= MONO_PROFILE_ALLOCATIONS;
4931 events = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_INS_COVERAGE;
4933 utils_init (fast_time);
4935 prof = create_profiler (filename, filters);
4941 mono_profiler_install (prof, log_shutdown);
4942 mono_profiler_install_gc (gc_event, gc_resize);
4943 mono_profiler_install_allocation (gc_alloc);
4944 mono_profiler_install_gc_moves (gc_moves);
4945 mono_profiler_install_gc_roots (gc_handle, gc_roots);
4946 mono_profiler_install_appdomain (NULL, domain_loaded, domain_unloaded, NULL);
4947 mono_profiler_install_appdomain_name (domain_name);
4948 mono_profiler_install_context (context_loaded, context_unloaded);
4949 mono_profiler_install_class (NULL, class_loaded, class_unloaded, NULL);
4950 mono_profiler_install_module (NULL, image_loaded, image_unloaded, NULL);
4951 mono_profiler_install_assembly (NULL, assembly_loaded, assembly_unloaded, NULL);
4952 mono_profiler_install_thread (thread_start, thread_end);
4953 mono_profiler_install_thread_name (thread_name);
4954 mono_profiler_install_enter_leave (method_enter, method_leave);
4955 mono_profiler_install_jit_end (method_jitted);
4956 mono_profiler_install_code_buffer_new (code_buffer_new);
4957 mono_profiler_install_exception (throw_exc, method_exc_leave, clause_exc);
4958 mono_profiler_install_monitor (monitor_event);
4959 mono_profiler_install_runtime_initialized (runtime_initialized);
4961 mono_profiler_install_coverage_filter (coverage_filter);
4963 if (do_mono_sample && sample_type == SAMPLE_CYCLES && !only_counters) {
4964 events |= MONO_PROFILE_STATISTICAL;
4965 mono_profiler_set_statistical_mode (sampling_mode, sample_freq);
4966 mono_profiler_install_statistical (mono_sample_hit);
4969 mono_profiler_set_events ((MonoProfileFlags)events);
4971 TLS_INIT (tlsbuffer);
4972 TLS_INIT (tlsmethodlist);