2 * profiler.c: Profiler interface for Mono
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
14 #include "mono/metadata/profiler-private.h"
15 #include "mono/metadata/assembly.h"
16 #include "mono/metadata/debug-helpers.h"
17 #include "mono/metadata/mono-debug.h"
18 #include "mono/metadata/debug-mono-symfile.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include "mono/metadata/class-internals.h"
21 #include "mono/metadata/domain-internals.h"
22 #include "mono/metadata/gc-internal.h"
23 #include "mono/metadata/mono-config-dirs.h"
24 #include "mono/io-layer/io-layer.h"
25 #include "mono/utils/mono-dl.h"
30 #ifdef HAVE_SYS_TIME_H
33 #ifdef HAVE_BACKTRACE_SYMBOLS
37 typedef struct _ProfilerDesc ProfilerDesc;
38 struct _ProfilerDesc {
40 MonoProfiler *profiler;
41 MonoProfileFlags events;
43 MonoProfileAppDomainFunc domain_start_load;
44 MonoProfileAppDomainResult domain_end_load;
45 MonoProfileAppDomainFunc domain_start_unload;
46 MonoProfileAppDomainFunc domain_end_unload;
47 MonoProfileAppDomainFriendlyNameFunc domain_name;
49 MonoProfileContextFunc context_load;
50 MonoProfileContextFunc context_unload;
52 MonoProfileAssemblyFunc assembly_start_load;
53 MonoProfileAssemblyResult assembly_end_load;
54 MonoProfileAssemblyFunc assembly_start_unload;
55 MonoProfileAssemblyFunc assembly_end_unload;
57 MonoProfileModuleFunc module_start_load;
58 MonoProfileModuleResult module_end_load;
59 MonoProfileModuleFunc module_start_unload;
60 MonoProfileModuleFunc module_end_unload;
62 MonoProfileClassFunc class_start_load;
63 MonoProfileClassResult class_end_load;
64 MonoProfileClassFunc class_start_unload;
65 MonoProfileClassFunc class_end_unload;
67 MonoProfileMethodFunc jit_start;
68 MonoProfileMethodResult jit_end;
69 MonoProfileJitResult jit_end2;
70 MonoProfileMethodFunc method_free;
71 MonoProfileMethodFunc method_start_invoke;
72 MonoProfileMethodFunc method_end_invoke;
73 MonoProfileMethodResult man_unman_transition;
74 MonoProfileAllocFunc allocation_cb;
75 MonoProfileMonitorFunc monitor_event_cb;
76 MonoProfileStatFunc statistical_cb;
77 MonoProfileStatCallChainFunc statistical_call_chain_cb;
78 int statistical_call_chain_depth;
79 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
80 MonoProfileMethodFunc method_enter;
81 MonoProfileMethodFunc method_leave;
83 MonoProfileExceptionFunc exception_throw_cb;
84 MonoProfileMethodFunc exception_method_leave_cb;
85 MonoProfileExceptionClauseFunc exception_clause_cb;
87 MonoProfileIomapFunc iomap_cb;
89 MonoProfileThreadFunc thread_start;
90 MonoProfileThreadFunc thread_end;
91 MonoProfileThreadNameFunc thread_name;
93 MonoProfileCoverageFilterFunc coverage_filter_cb;
95 MonoProfileFunc shutdown_callback;
97 MonoProfileGCFunc gc_event;
98 MonoProfileGCResizeFunc gc_heap_resize;
99 MonoProfileGCMoveFunc gc_moves;
100 MonoProfileGCHandleFunc gc_handle;
101 MonoProfileGCRootFunc gc_roots;
103 MonoProfileFunc runtime_initialized_event;
105 MonoProfilerCodeChunkNew code_chunk_new;
106 MonoProfilerCodeChunkDestroy code_chunk_destroy;
107 MonoProfilerCodeBufferNew code_buffer_new;
110 static ProfilerDesc *prof_list = NULL;
112 #define mono_profiler_coverage_lock() mono_mutex_lock (&profiler_coverage_mutex)
113 #define mono_profiler_coverage_unlock() mono_mutex_unlock (&profiler_coverage_mutex)
114 static mono_mutex_t profiler_coverage_mutex;
116 /* this is directly accessible to other mono libs.
117 * It is the ORed value of all the profiler's events.
119 MonoProfileFlags mono_profiler_events;
122 * mono_profiler_install:
123 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
124 * @callback: the function to invoke at shutdown
126 * Use mono_profiler_install to activate profiling in the Mono runtime.
127 * Typically developers of new profilers will create a new structure whose
128 * first field is a MonoProfiler and put any extra information that they need
129 * to access from the various profiling callbacks there.
133 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
135 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
137 mono_mutex_init_recursive (&profiler_coverage_mutex);
138 desc->profiler = prof;
139 desc->shutdown_callback = callback;
140 desc->next = prof_list;
145 * mono_profiler_set_events:
146 * @events: an ORed set of values made up of MONO_PROFILER_ flags
148 * The events descriped in the @events argument is a set of flags
149 * that represent which profiling events must be triggered. For
150 * example if you have registered a set of methods for tracking
151 * JIT compilation start and end with mono_profiler_install_jit_compile,
152 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
155 * You can call mono_profile_set_events more than once and you can
156 * do this at runtime to modify which methods are invoked.
159 mono_profiler_set_events (MonoProfileFlags events)
162 MonoProfileFlags value = 0;
164 prof_list->events = events;
165 for (prof = prof_list; prof; prof = prof->next)
166 value |= prof->events;
167 mono_profiler_events = value;
171 * mono_profiler_get_events:
173 * Returns a list of active events that will be intercepted.
176 mono_profiler_get_events (void)
178 return mono_profiler_events;
182 * mono_profiler_install_enter_leave:
183 * @enter: the routine to be called on each method entry
184 * @fleave: the routine to be called each time a method returns
186 * Use this routine to install routines that will be called everytime
187 * a method enters and leaves. The routines will receive as an argument
188 * the MonoMethod representing the method that is entering or leaving.
191 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
195 prof_list->method_enter = enter;
196 prof_list->method_leave = fleave;
200 * mono_profiler_install_jit_compile:
201 * @start: the routine to be called when the JIT process starts.
202 * @end: the routine to be called when the JIT process ends.
204 * Use this routine to install routines that will be called when JIT
205 * compilation of a method starts and completes.
208 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
212 prof_list->jit_start = start;
213 prof_list->jit_end = end;
217 mono_profiler_install_jit_end (MonoProfileJitResult end)
221 prof_list->jit_end2 = end;
225 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
229 prof_list->method_free = callback;
233 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
237 prof_list->method_start_invoke = start;
238 prof_list->method_end_invoke = end;
242 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
246 prof_list->thread_start = start;
247 prof_list->thread_end = end;
251 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
255 prof_list->thread_name = thread_name_cb;
259 mono_profiler_install_transition (MonoProfileMethodResult callback)
263 prof_list->man_unman_transition = callback;
267 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
269 mono_gc_enable_alloc_events ();
272 prof_list->allocation_cb = callback;
276 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
280 prof_list->monitor_event_cb = callback;
283 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
284 static int64_t sampling_frequency = 1000; //1ms
287 * mono_profiler_set_statistical_mode:
288 * @mode the sampling mode used.
289 * @sample_frequency_is_us the sampling frequency in microseconds.
291 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
292 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
294 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
296 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
299 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us)
301 sampling_mode = mode;
302 sampling_frequency = sampling_frequency_is_us;
306 mono_profiler_install_statistical (MonoProfileStatFunc callback)
310 prof_list->statistical_cb = callback;
314 mono_profiler_get_sampling_rate (void)
316 return sampling_frequency;
319 MonoProfileSamplingMode
320 mono_profiler_get_sampling_mode (void)
322 return sampling_mode;
326 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
329 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
330 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
332 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
333 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
335 prof_list->statistical_call_chain_cb = callback;
336 prof_list->statistical_call_chain_depth = call_chain_depth;
337 prof_list->statistical_call_chain_strategy = call_chain_strategy;
341 mono_profiler_stat_get_call_chain_depth (void) {
342 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
343 return prof_list->statistical_call_chain_depth;
349 MonoProfilerCallChainStrategy
350 mono_profiler_stat_get_call_chain_strategy (void) {
351 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
352 return prof_list->statistical_call_chain_strategy;
354 return MONO_PROFILER_CALL_CHAIN_NONE;
358 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
362 prof_list->exception_throw_cb = throw_callback;
363 prof_list->exception_method_leave_cb = exc_method_leave;
364 prof_list->exception_clause_cb = clause_callback;
368 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
372 prof_list->coverage_filter_cb = callback;
376 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
377 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
382 prof_list->domain_start_load = start_load;
383 prof_list->domain_end_load = end_load;
384 prof_list->domain_start_unload = start_unload;
385 prof_list->domain_end_unload = end_unload;
389 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
394 prof_list->domain_name = domain_name_cb;
398 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
403 prof_list->context_load = load;
404 prof_list->context_unload = unload;
408 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
409 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
413 prof_list->assembly_start_load = start_load;
414 prof_list->assembly_end_load = end_load;
415 prof_list->assembly_start_unload = start_unload;
416 prof_list->assembly_end_unload = end_unload;
420 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
421 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
425 prof_list->module_start_load = start_load;
426 prof_list->module_end_load = end_load;
427 prof_list->module_start_unload = start_unload;
428 prof_list->module_end_unload = end_unload;
432 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
433 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
437 prof_list->class_start_load = start_load;
438 prof_list->class_end_load = end_load;
439 prof_list->class_start_unload = start_unload;
440 prof_list->class_end_unload = end_unload;
444 mono_profiler_method_enter (MonoMethod *method)
447 for (prof = prof_list; prof; prof = prof->next) {
448 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
449 prof->method_enter (prof->profiler, method);
454 mono_profiler_method_leave (MonoMethod *method)
457 for (prof = prof_list; prof; prof = prof->next) {
458 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
459 prof->method_leave (prof->profiler, method);
464 mono_profiler_method_jit (MonoMethod *method)
467 for (prof = prof_list; prof; prof = prof->next) {
468 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
469 prof->jit_start (prof->profiler, method);
474 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
477 for (prof = prof_list; prof; prof = prof->next) {
478 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
480 prof->jit_end (prof->profiler, method, result);
482 prof->jit_end2 (prof->profiler, method, jinfo, result);
488 mono_profiler_method_free (MonoMethod *method)
491 for (prof = prof_list; prof; prof = prof->next) {
492 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
493 prof->method_free (prof->profiler, method);
498 mono_profiler_method_start_invoke (MonoMethod *method)
501 for (prof = prof_list; prof; prof = prof->next) {
502 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
503 prof->method_start_invoke (prof->profiler, method);
508 mono_profiler_method_end_invoke (MonoMethod *method)
511 for (prof = prof_list; prof; prof = prof->next) {
512 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
513 prof->method_end_invoke (prof->profiler, method);
518 mono_profiler_code_transition (MonoMethod *method, int result)
521 for (prof = prof_list; prof; prof = prof->next) {
522 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
523 prof->man_unman_transition (prof->profiler, method, result);
528 mono_profiler_allocation (MonoObject *obj)
531 for (prof = prof_list; prof; prof = prof->next) {
532 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
533 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
538 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
540 for (prof = prof_list; prof; prof = prof->next) {
541 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
542 prof->monitor_event_cb (prof->profiler, obj, event);
547 mono_profiler_stat_hit (guchar *ip, void *context)
550 for (prof = prof_list; prof; prof = prof->next) {
551 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
552 prof->statistical_cb (prof->profiler, ip, context);
557 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
560 for (prof = prof_list; prof; prof = prof->next) {
561 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
562 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
567 mono_profiler_exception_thrown (MonoObject *exception)
570 for (prof = prof_list; prof; prof = prof->next) {
571 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
572 prof->exception_throw_cb (prof->profiler, exception);
577 mono_profiler_exception_method_leave (MonoMethod *method)
580 for (prof = prof_list; prof; prof = prof->next) {
581 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
582 prof->exception_method_leave_cb (prof->profiler, method);
587 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
590 for (prof = prof_list; prof; prof = prof->next) {
591 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
592 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
597 mono_profiler_thread_start (gsize tid)
600 for (prof = prof_list; prof; prof = prof->next) {
601 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
602 prof->thread_start (prof->profiler, tid);
607 mono_profiler_thread_end (gsize tid)
610 for (prof = prof_list; prof; prof = prof->next) {
611 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
612 prof->thread_end (prof->profiler, tid);
617 mono_profiler_thread_name (gsize tid, const char *name)
620 for (prof = prof_list; prof; prof = prof->next) {
621 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
622 prof->thread_name (prof->profiler, tid, name);
627 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
630 for (prof = prof_list; prof; prof = prof->next) {
631 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
635 case MONO_PROFILE_START_LOAD:
636 if (prof->assembly_start_load)
637 prof->assembly_start_load (prof->profiler, assembly);
639 case MONO_PROFILE_START_UNLOAD:
640 if (prof->assembly_start_unload)
641 prof->assembly_start_unload (prof->profiler, assembly);
643 case MONO_PROFILE_END_UNLOAD:
644 if (prof->assembly_end_unload)
645 prof->assembly_end_unload (prof->profiler, assembly);
648 g_assert_not_reached ();
654 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
657 for (prof = prof_list; prof; prof = prof->next) {
658 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
659 prof->assembly_end_load (prof->profiler, assembly, result);
663 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
666 for (prof = prof_list; prof; prof = prof->next) {
667 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
668 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
673 mono_profiler_module_event (MonoImage *module, int code)
676 for (prof = prof_list; prof; prof = prof->next) {
677 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
681 case MONO_PROFILE_START_LOAD:
682 if (prof->module_start_load)
683 prof->module_start_load (prof->profiler, module);
685 case MONO_PROFILE_START_UNLOAD:
686 if (prof->module_start_unload)
687 prof->module_start_unload (prof->profiler, module);
689 case MONO_PROFILE_END_UNLOAD:
690 if (prof->module_end_unload)
691 prof->module_end_unload (prof->profiler, module);
694 g_assert_not_reached ();
700 mono_profiler_module_loaded (MonoImage *module, int result)
703 for (prof = prof_list; prof; prof = prof->next) {
704 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
705 prof->module_end_load (prof->profiler, module, result);
710 mono_profiler_class_event (MonoClass *klass, int code)
713 for (prof = prof_list; prof; prof = prof->next) {
714 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
718 case MONO_PROFILE_START_LOAD:
719 if (prof->class_start_load)
720 prof->class_start_load (prof->profiler, klass);
722 case MONO_PROFILE_START_UNLOAD:
723 if (prof->class_start_unload)
724 prof->class_start_unload (prof->profiler, klass);
726 case MONO_PROFILE_END_UNLOAD:
727 if (prof->class_end_unload)
728 prof->class_end_unload (prof->profiler, klass);
731 g_assert_not_reached ();
737 mono_profiler_class_loaded (MonoClass *klass, int result)
740 for (prof = prof_list; prof; prof = prof->next) {
741 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
742 prof->class_end_load (prof->profiler, klass, result);
747 mono_profiler_appdomain_event (MonoDomain *domain, int code)
750 for (prof = prof_list; prof; prof = prof->next) {
751 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
755 case MONO_PROFILE_START_LOAD:
756 if (prof->domain_start_load)
757 prof->domain_start_load (prof->profiler, domain);
759 case MONO_PROFILE_START_UNLOAD:
760 if (prof->domain_start_unload)
761 prof->domain_start_unload (prof->profiler, domain);
763 case MONO_PROFILE_END_UNLOAD:
764 if (prof->domain_end_unload)
765 prof->domain_end_unload (prof->profiler, domain);
768 g_assert_not_reached ();
774 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
777 for (prof = prof_list; prof; prof = prof->next) {
778 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
779 prof->domain_end_load (prof->profiler, domain, result);
784 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
786 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
787 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
788 prof->domain_name (prof->profiler, domain, name);
792 mono_profiler_context_loaded (MonoAppContext *context)
794 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
795 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
796 prof->context_load (prof->profiler, context);
800 mono_profiler_context_unloaded (MonoAppContext *context)
802 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
803 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
804 prof->context_unload (prof->profiler, context);
808 mono_profiler_shutdown (void)
811 for (prof = prof_list; prof; prof = prof->next) {
812 if (prof->shutdown_callback)
813 prof->shutdown_callback (prof->profiler);
816 mono_profiler_set_events (0);
820 mono_profiler_gc_heap_resize (gint64 new_size)
823 for (prof = prof_list; prof; prof = prof->next) {
824 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
825 prof->gc_heap_resize (prof->profiler, new_size);
830 mono_profiler_gc_event (MonoGCEvent event, int generation)
833 for (prof = prof_list; prof; prof = prof->next) {
834 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
835 prof->gc_event (prof->profiler, event, generation);
840 mono_profiler_gc_moves (void **objects, int num)
843 for (prof = prof_list; prof; prof = prof->next) {
844 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
845 prof->gc_moves (prof->profiler, objects, num);
850 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
853 for (prof = prof_list; prof; prof = prof->next) {
854 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
855 prof->gc_handle (prof->profiler, op, type, handle, obj);
860 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
863 for (prof = prof_list; prof; prof = prof->next) {
864 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
865 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
870 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
872 mono_gc_enable_events ();
875 prof_list->gc_event = callback;
876 prof_list->gc_heap_resize = heap_resize_callback;
880 * mono_profiler_install_gc_moves:
881 * @callback: callback function
883 * Install the @callback function that the GC will call when moving objects.
884 * The callback receives an array of pointers and the number of elements
885 * in the array. Every even element in the array is the original object location
886 * and the following odd element is the new location of the object in memory.
887 * So the number of elements argument will always be a multiple of 2.
888 * Since this callback happens during the GC, it is a restricted environment:
889 * no locks can be taken and the object pointers can be inspected only once
890 * the GC is finished (of course the original location pointers will not
891 * point to valid objects anymore).
894 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
898 prof_list->gc_moves = callback;
902 * mono_profiler_install_gc_roots:
903 * @handle_callback: callback function
904 * @roots_callback: callback function
906 * Install the @handle_callback function that the GC will call when GC
907 * handles are created or destroyed.
908 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
909 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
910 * object pointer, if present.
911 * Install the @roots_callback function that the GC will call when tracing
912 * the roots for a collection.
913 * The callback receives the number of elements and three arrays: an array
914 * of objects, an array of root types and flags and an array of extra info.
915 * The size of each array is given by the first argument.
918 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
922 prof_list->gc_handle = handle_callback;
923 prof_list->gc_roots = roots_callback;
927 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
931 prof_list->runtime_initialized_event = runtime_initialized_callback;
935 mono_profiler_runtime_initialized (void) {
937 for (prof = prof_list; prof; prof = prof->next) {
938 if (prof->runtime_initialized_event)
939 prof->runtime_initialized_event (prof->profiler);
944 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
947 prof_list->code_chunk_new = callback;
950 mono_profiler_code_chunk_new (gpointer chunk, int size) {
952 for (prof = prof_list; prof; prof = prof->next) {
953 if (prof->code_chunk_new)
954 prof->code_chunk_new (prof->profiler, chunk, size);
959 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
962 prof_list->code_chunk_destroy = callback;
965 mono_profiler_code_chunk_destroy (gpointer chunk) {
967 for (prof = prof_list; prof; prof = prof->next) {
968 if (prof->code_chunk_destroy)
969 prof->code_chunk_destroy (prof->profiler, chunk);
974 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
977 prof_list->code_buffer_new = callback;
981 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
985 prof_list->iomap_cb = callback;
989 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
991 for (prof = prof_list; prof; prof = prof->next) {
992 if (prof->code_buffer_new)
993 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
997 static GHashTable *coverage_hash = NULL;
999 MonoProfileCoverageInfo*
1000 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1002 MonoProfileCoverageInfo *res;
1003 int instrument = FALSE;
1006 for (prof = prof_list; prof; prof = prof->next) {
1007 /* note that we call the filter on all the profilers even if just
1008 * a single one would be enough to instrument a method
1010 if (prof->coverage_filter_cb)
1011 if (prof->coverage_filter_cb (prof->profiler, method))
1017 mono_profiler_coverage_lock ();
1019 coverage_hash = g_hash_table_new (NULL, NULL);
1021 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1023 res->entries = entries;
1025 g_hash_table_insert (coverage_hash, method, res);
1026 mono_profiler_coverage_unlock ();
1031 /* safe only when the method antive code has been unloaded */
1033 mono_profiler_coverage_free (MonoMethod *method)
1035 MonoProfileCoverageInfo* info;
1037 mono_profiler_coverage_lock ();
1038 if (!coverage_hash) {
1039 mono_profiler_coverage_unlock ();
1043 info = g_hash_table_lookup (coverage_hash, method);
1046 g_hash_table_remove (coverage_hash, method);
1048 mono_profiler_coverage_unlock ();
1052 * mono_profiler_coverage_get:
1053 * @prof: The profiler handle, installed with mono_profiler_install
1054 * @method: the method to gather information from.
1055 * @func: A routine that will be called back with the results
1057 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1058 * it is posisble to obtain coverage information about a give method.
1060 * The function @func will be invoked repeatedly with instances of the
1061 * MonoProfileCoverageEntry structure.
1064 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1066 MonoProfileCoverageInfo* info = NULL;
1069 const unsigned char *start, *end, *cil_code;
1070 MonoMethodHeader *header;
1071 MonoProfileCoverageEntry entry;
1072 MonoDebugMethodInfo *debug_minfo;
1074 mono_profiler_coverage_lock ();
1076 info = g_hash_table_lookup (coverage_hash, method);
1077 mono_profiler_coverage_unlock ();
1082 header = mono_method_get_header (method);
1083 start = mono_method_header_get_code (header, &code_size, NULL);
1084 debug_minfo = mono_debug_lookup_method (method);
1086 end = start + code_size;
1087 for (i = 0; i < info->entries; ++i) {
1088 cil_code = info->data [i].cil_code;
1089 if (cil_code && cil_code >= start && cil_code < end) {
1091 offset = cil_code - start;
1092 entry.iloffset = offset;
1093 entry.method = method;
1094 entry.counter = info->data [i].count;
1095 entry.line = entry.col = 1;
1096 entry.filename = NULL;
1098 MonoDebugSourceLocation *location;
1100 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1102 entry.line = location->row;
1103 entry.col = location->column;
1104 entry.filename = fname = g_strdup (location->source_file);
1105 mono_debug_free_source_location (location);
1109 func (prof, &entry);
1113 mono_metadata_free_mh (header);
1116 typedef void (*ProfilerInitializer) (const char*);
1117 #define INITIALIZER_NAME "mono_profiler_startup"
1121 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1124 ProfilerInitializer func;
1129 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1139 load_embedded_profiler (const char *desc, const char *name)
1143 MonoDl *pmodule = NULL;
1146 pmodule = mono_dl_open (NULL, MONO_DL_LAZY, &err);
1148 g_warning ("Could not open main executable (%s)", err);
1153 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1154 result = load_profiler (pmodule, desc, symbol);
1161 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1163 MonoDl *pmodule = NULL;
1170 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1171 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1175 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1182 load_profiler_from_mono_instalation (const char *libname, const char *desc)
1185 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_LAZY, &err);
1188 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1193 * mono_profiler_load:
1194 * @desc: arguments to configure the profiler
1196 * Invoke this method to initialize the profiler. This will drive the
1197 * loading of the internal ("default") or any external profilers.
1199 * This routine is invoked by Mono's driver, but must be called manually
1200 * if you embed Mono into your application.
1203 mono_profiler_load (const char *desc)
1206 mono_gc_base_init ();
1208 if (!desc || (strcmp ("default", desc) == 0)) {
1209 desc = "log:report";
1211 /* we keep command-line compat with the old version here */
1212 if (strncmp (desc, "default:", 8) == 0) {
1213 gchar **args, **ptr;
1214 GString *str = g_string_new ("log:report");
1215 args = g_strsplit (desc + 8, ",", -1);
1216 for (ptr = args; ptr && *ptr; ptr++) {
1217 const char *arg = *ptr;
1219 if (!strcmp (arg, "time"))
1220 g_string_append (str, ",calls");
1221 else if (!strcmp (arg, "alloc"))
1222 g_string_append (str, ",alloc");
1223 else if (!strcmp (arg, "stat"))
1224 g_string_append (str, ",sample");
1225 else if (!strcmp (arg, "jit"))
1226 continue; /* accept and do nothing */
1227 else if (strncmp (arg, "file=", 5) == 0) {
1228 g_string_append_printf (str, ",output=%s", arg + 5);
1230 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1234 desc = cdesc = g_string_free (str, FALSE);
1237 const char* col = strchr (desc, ':');
1240 gboolean res = FALSE;
1243 mname = g_memdup (desc, col - desc + 1);
1244 mname [col - desc] = 0;
1246 mname = g_strdup (desc);
1248 if (!load_embedded_profiler (desc, mname)) {
1249 libname = g_strdup_printf ("mono-profiler-%s", mname);
1250 if (mono_config_get_assemblies_dir ())
1251 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1253 res = load_profiler_from_directory (NULL, libname, desc);
1255 res = load_profiler_from_mono_instalation (libname, desc);
1257 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);