3 * Profiler interface for Mono
6 * Paolo Molaro (lupus@ximian.com)
7 * Alex Rønne Petersen (alexrp@xamarin.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/metadata/profiler-private.h"
17 #include "mono/metadata/assembly.h"
18 #include "mono/metadata/debug-helpers.h"
19 #include "mono/metadata/mono-debug.h"
20 #include "mono/metadata/debug-internals.h"
21 #include "mono/metadata/metadata-internals.h"
22 #include "mono/metadata/class-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/gc-internals.h"
25 #include "mono/metadata/mono-config-dirs.h"
26 #include "mono/utils/mono-dl.h"
27 #include <mono/utils/mono-logger-internals.h>
32 #ifdef HAVE_SYS_TIME_H
35 #ifdef HAVE_BACKTRACE_SYMBOLS
39 typedef struct _ProfilerDesc ProfilerDesc;
40 struct _ProfilerDesc {
42 MonoProfiler *profiler;
43 MonoProfileFlags events;
45 MonoProfileAppDomainFunc domain_start_load;
46 MonoProfileAppDomainResult domain_end_load;
47 MonoProfileAppDomainFunc domain_start_unload;
48 MonoProfileAppDomainFunc domain_end_unload;
49 MonoProfileAppDomainFriendlyNameFunc domain_name;
51 MonoProfileContextFunc context_load;
52 MonoProfileContextFunc context_unload;
54 MonoProfileAssemblyFunc assembly_start_load;
55 MonoProfileAssemblyResult assembly_end_load;
56 MonoProfileAssemblyFunc assembly_start_unload;
57 MonoProfileAssemblyFunc assembly_end_unload;
59 MonoProfileModuleFunc module_start_load;
60 MonoProfileModuleResult module_end_load;
61 MonoProfileModuleFunc module_start_unload;
62 MonoProfileModuleFunc module_end_unload;
64 MonoProfileClassFunc class_start_load;
65 MonoProfileClassResult class_end_load;
66 MonoProfileClassFunc class_start_unload;
67 MonoProfileClassFunc class_end_unload;
69 MonoProfileMethodFunc jit_start;
70 MonoProfileMethodResult jit_end;
71 MonoProfileJitResult jit_end2;
72 MonoProfileMethodFunc method_free;
73 MonoProfileMethodFunc method_start_invoke;
74 MonoProfileMethodFunc method_end_invoke;
75 MonoProfileMethodResult man_unman_transition;
76 MonoProfileAllocFunc allocation_cb;
77 MonoProfileMonitorFunc monitor_event_cb;
78 MonoProfileStatFunc statistical_cb;
79 MonoProfileStatCallChainFunc statistical_call_chain_cb;
80 int statistical_call_chain_depth;
81 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
82 MonoProfileMethodFunc method_enter;
83 MonoProfileMethodFunc method_leave;
85 MonoProfileExceptionFunc exception_throw_cb;
86 MonoProfileMethodFunc exception_method_leave_cb;
87 MonoProfileExceptionClauseFunc exception_clause_cb;
89 MonoProfileIomapFunc iomap_cb;
91 MonoProfileThreadFunc thread_start;
92 MonoProfileThreadFunc thread_end;
93 MonoProfileThreadNameFunc thread_name;
95 MonoProfileCoverageFilterFunc coverage_filter_cb;
97 MonoProfileFunc shutdown_callback;
99 MonoProfileGCFunc gc_event;
100 MonoProfileGCResizeFunc gc_heap_resize;
101 MonoProfileGCMoveFunc gc_moves;
102 MonoProfileGCHandleFunc gc_handle;
103 MonoProfileGCRootFunc gc_roots;
105 MonoProfileGCFinalizeFunc gc_finalize_begin;
106 MonoProfileGCFinalizeObjectFunc gc_finalize_object_begin;
107 MonoProfileGCFinalizeObjectFunc gc_finalize_object_end;
108 MonoProfileGCFinalizeFunc gc_finalize_end;
110 MonoProfileFunc runtime_initialized_event;
112 MonoProfilerCodeChunkNew code_chunk_new;
113 MonoProfilerCodeChunkDestroy code_chunk_destroy;
114 MonoProfilerCodeBufferNew code_buffer_new;
117 static ProfilerDesc *prof_list = NULL;
119 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
120 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
121 static mono_mutex_t profiler_coverage_mutex;
123 /* this is directly accessible to other mono libs.
124 * It is the ORed value of all the profiler's events.
126 MonoProfileFlags mono_profiler_events;
129 * mono_profiler_install:
130 * \param prof a \c MonoProfiler structure pointer, or a pointer to a derived structure.
131 * \param callback the function to invoke at shutdown
132 * Use \c mono_profiler_install to activate profiling in the Mono runtime.
133 * Typically developers of new profilers will create a new structure whose
134 * first field is a \c MonoProfiler and put any extra information that they need
135 * to access from the various profiling callbacks there.
138 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
140 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
142 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
143 desc->profiler = prof;
144 desc->shutdown_callback = callback;
145 desc->next = prof_list;
150 * mono_profiler_set_events:
151 * \param events an ORed set of values made up of \c MONO_PROFILER_ flags
152 * The events described in the \p events argument is a set of flags
153 * that represent which profiling events must be triggered. For
154 * example if you have registered a set of methods for tracking
155 * JIT compilation start and end with \c mono_profiler_install_jit_compile,
156 * you will want to pass the \c MONO_PROFILE_JIT_COMPILATION flag to
159 * You can call \c mono_profile_set_events more than once and you can
160 * do this at runtime to modify which methods are invoked.
163 mono_profiler_set_events (MonoProfileFlags events)
166 MonoProfileFlags value = (MonoProfileFlags)0;
168 prof_list->events = events;
169 for (prof = prof_list; prof; prof = prof->next)
170 value = (MonoProfileFlags)(value | prof->events);
171 mono_profiler_events = value;
175 * mono_profiler_get_events:
177 * Returns a list of active events that will be intercepted.
180 mono_profiler_get_events (void)
182 return mono_profiler_events;
186 * mono_profiler_install_enter_leave:
187 * \param enter the routine to be called on each method entry
188 * \param fleave the routine to be called each time a method returns
190 * Use this routine to install routines that will be called everytime
191 * a method enters and leaves. The routines will receive as an argument
192 * the \c MonoMethod representing the method that is entering or leaving.
195 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
199 prof_list->method_enter = enter;
200 prof_list->method_leave = fleave;
204 * mono_profiler_install_jit_compile:
205 * \param start the routine to be called when the JIT process starts.
206 * \param end the routine to be called when the JIT process ends.
208 * Use this routine to install routines that will be called when JIT
209 * compilation of a method starts and completes.
212 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
216 prof_list->jit_start = start;
217 prof_list->jit_end = end;
221 mono_profiler_install_jit_end (MonoProfileJitResult end)
225 prof_list->jit_end2 = end;
229 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
233 prof_list->method_free = callback;
237 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
241 prof_list->method_start_invoke = start;
242 prof_list->method_end_invoke = end;
246 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
250 prof_list->thread_start = start;
251 prof_list->thread_end = end;
255 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
259 prof_list->thread_name = thread_name_cb;
263 mono_profiler_install_transition (MonoProfileMethodResult callback)
267 prof_list->man_unman_transition = callback;
271 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
275 prof_list->allocation_cb = callback;
279 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
283 prof_list->monitor_event_cb = callback;
286 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
287 static int64_t sampling_frequency = 100; // Hz
290 * mono_profiler_set_statistical_mode:
291 * \param mode the sampling mode used.
292 * \param sample_frequency_is_us the sampling frequency in microseconds.
294 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
295 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
297 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
299 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
302 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
304 sampling_mode = mode;
305 sampling_frequency = sampling_frequency_hz;
309 mono_profiler_install_statistical (MonoProfileStatFunc callback)
313 prof_list->statistical_cb = callback;
317 mono_profiler_get_sampling_rate (void)
319 return sampling_frequency;
322 MonoProfileSamplingMode
323 mono_profiler_get_sampling_mode (void)
325 return sampling_mode;
329 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
332 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
333 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
335 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
336 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
338 prof_list->statistical_call_chain_cb = callback;
339 prof_list->statistical_call_chain_depth = call_chain_depth;
340 prof_list->statistical_call_chain_strategy = call_chain_strategy;
344 mono_profiler_stat_get_call_chain_depth (void) {
345 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
346 return prof_list->statistical_call_chain_depth;
352 MonoProfilerCallChainStrategy
353 mono_profiler_stat_get_call_chain_strategy (void) {
354 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
355 return prof_list->statistical_call_chain_strategy;
357 return MONO_PROFILER_CALL_CHAIN_NONE;
361 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
365 prof_list->exception_throw_cb = throw_callback;
366 prof_list->exception_method_leave_cb = exc_method_leave;
367 prof_list->exception_clause_cb = clause_callback;
371 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
375 prof_list->coverage_filter_cb = callback;
379 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
380 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
385 prof_list->domain_start_load = start_load;
386 prof_list->domain_end_load = end_load;
387 prof_list->domain_start_unload = start_unload;
388 prof_list->domain_end_unload = end_unload;
392 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
397 prof_list->domain_name = domain_name_cb;
401 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
406 prof_list->context_load = load;
407 prof_list->context_unload = unload;
411 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
412 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
416 prof_list->assembly_start_load = start_load;
417 prof_list->assembly_end_load = end_load;
418 prof_list->assembly_start_unload = start_unload;
419 prof_list->assembly_end_unload = end_unload;
423 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
424 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
428 prof_list->module_start_load = start_load;
429 prof_list->module_end_load = end_load;
430 prof_list->module_start_unload = start_unload;
431 prof_list->module_end_unload = end_unload;
435 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
436 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
440 prof_list->class_start_load = start_load;
441 prof_list->class_end_load = end_load;
442 prof_list->class_start_unload = start_unload;
443 prof_list->class_end_unload = end_unload;
447 * mono_profiler_method_enter:
450 mono_profiler_method_enter (MonoMethod *method)
453 for (prof = prof_list; prof; prof = prof->next) {
454 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
455 prof->method_enter (prof->profiler, method);
460 * mono_profiler_method_leave:
463 mono_profiler_method_leave (MonoMethod *method)
466 for (prof = prof_list; prof; prof = prof->next) {
467 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
468 prof->method_leave (prof->profiler, method);
473 * mono_profiler_method_jit:
476 mono_profiler_method_jit (MonoMethod *method)
479 for (prof = prof_list; prof; prof = prof->next) {
480 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
481 prof->jit_start (prof->profiler, method);
486 * mono_profiler_method_end_jit:
489 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
492 for (prof = prof_list; prof; prof = prof->next) {
493 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
495 prof->jit_end (prof->profiler, method, result);
497 prof->jit_end2 (prof->profiler, method, jinfo, result);
503 mono_profiler_method_free (MonoMethod *method)
506 for (prof = prof_list; prof; prof = prof->next) {
507 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
508 prof->method_free (prof->profiler, method);
513 mono_profiler_method_start_invoke (MonoMethod *method)
516 for (prof = prof_list; prof; prof = prof->next) {
517 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
518 prof->method_start_invoke (prof->profiler, method);
523 mono_profiler_method_end_invoke (MonoMethod *method)
526 for (prof = prof_list; prof; prof = prof->next) {
527 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
528 prof->method_end_invoke (prof->profiler, method);
533 * mono_profiler_code_transition:
536 mono_profiler_code_transition (MonoMethod *method, int result)
539 for (prof = prof_list; prof; prof = prof->next) {
540 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
541 prof->man_unman_transition (prof->profiler, method, result);
546 * mono_profiler_allocation:
549 mono_profiler_allocation (MonoObject *obj)
552 for (prof = prof_list; prof; prof = prof->next) {
553 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
554 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
559 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
561 for (prof = prof_list; prof; prof = prof->next) {
562 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
563 prof->monitor_event_cb (prof->profiler, obj, event);
568 * mono_profiler_stat_hit:
571 mono_profiler_stat_hit (guchar *ip, void *context)
574 for (prof = prof_list; prof; prof = prof->next) {
575 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
576 prof->statistical_cb (prof->profiler, ip, context);
581 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
584 for (prof = prof_list; prof; prof = prof->next) {
585 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
586 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
591 mono_profiler_exception_thrown (MonoObject *exception)
594 for (prof = prof_list; prof; prof = prof->next) {
595 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
596 prof->exception_throw_cb (prof->profiler, exception);
601 mono_profiler_exception_method_leave (MonoMethod *method)
604 for (prof = prof_list; prof; prof = prof->next) {
605 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
606 prof->exception_method_leave_cb (prof->profiler, method);
611 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
614 for (prof = prof_list; prof; prof = prof->next) {
615 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
616 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
621 * mono_profiler_thread_start:
624 mono_profiler_thread_start (gsize tid)
627 for (prof = prof_list; prof; prof = prof->next) {
628 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
629 prof->thread_start (prof->profiler, tid);
634 * mono_profiler_thread_end:
637 mono_profiler_thread_end (gsize tid)
640 for (prof = prof_list; prof; prof = prof->next) {
641 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
642 prof->thread_end (prof->profiler, tid);
647 mono_profiler_thread_name (gsize tid, const char *name)
650 for (prof = prof_list; prof; prof = prof->next) {
651 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
652 prof->thread_name (prof->profiler, tid, name);
657 * mono_profiler_assembly_event:
660 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
663 for (prof = prof_list; prof; prof = prof->next) {
664 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
668 case MONO_PROFILE_START_LOAD:
669 if (prof->assembly_start_load)
670 prof->assembly_start_load (prof->profiler, assembly);
672 case MONO_PROFILE_START_UNLOAD:
673 if (prof->assembly_start_unload)
674 prof->assembly_start_unload (prof->profiler, assembly);
676 case MONO_PROFILE_END_UNLOAD:
677 if (prof->assembly_end_unload)
678 prof->assembly_end_unload (prof->profiler, assembly);
681 g_assert_not_reached ();
687 * mono_profiler_assembly_loaded:
690 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
693 for (prof = prof_list; prof; prof = prof->next) {
694 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
695 prof->assembly_end_load (prof->profiler, assembly, result);
699 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
702 for (prof = prof_list; prof; prof = prof->next) {
703 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
704 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
709 * mono_profiler_module_event:
712 mono_profiler_module_event (MonoImage *module, int code)
715 for (prof = prof_list; prof; prof = prof->next) {
716 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
720 case MONO_PROFILE_START_LOAD:
721 if (prof->module_start_load)
722 prof->module_start_load (prof->profiler, module);
724 case MONO_PROFILE_START_UNLOAD:
725 if (prof->module_start_unload)
726 prof->module_start_unload (prof->profiler, module);
728 case MONO_PROFILE_END_UNLOAD:
729 if (prof->module_end_unload)
730 prof->module_end_unload (prof->profiler, module);
733 g_assert_not_reached ();
739 * mono_profiler_module_loaded:
742 mono_profiler_module_loaded (MonoImage *module, int result)
745 for (prof = prof_list; prof; prof = prof->next) {
746 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
747 prof->module_end_load (prof->profiler, module, result);
752 * mono_profiler_class_event:
755 mono_profiler_class_event (MonoClass *klass, int code)
758 for (prof = prof_list; prof; prof = prof->next) {
759 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
763 case MONO_PROFILE_START_LOAD:
764 if (prof->class_start_load)
765 prof->class_start_load (prof->profiler, klass);
767 case MONO_PROFILE_START_UNLOAD:
768 if (prof->class_start_unload)
769 prof->class_start_unload (prof->profiler, klass);
771 case MONO_PROFILE_END_UNLOAD:
772 if (prof->class_end_unload)
773 prof->class_end_unload (prof->profiler, klass);
776 g_assert_not_reached ();
782 * mono_profiler_class_loaded:
785 mono_profiler_class_loaded (MonoClass *klass, int result)
788 for (prof = prof_list; prof; prof = prof->next) {
789 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
790 prof->class_end_load (prof->profiler, klass, result);
795 * mono_profiler_appdomain_event:
798 mono_profiler_appdomain_event (MonoDomain *domain, int code)
801 for (prof = prof_list; prof; prof = prof->next) {
802 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
806 case MONO_PROFILE_START_LOAD:
807 if (prof->domain_start_load)
808 prof->domain_start_load (prof->profiler, domain);
810 case MONO_PROFILE_START_UNLOAD:
811 if (prof->domain_start_unload)
812 prof->domain_start_unload (prof->profiler, domain);
814 case MONO_PROFILE_END_UNLOAD:
815 if (prof->domain_end_unload)
816 prof->domain_end_unload (prof->profiler, domain);
819 g_assert_not_reached ();
825 * mono_profiler_appdomain_loaded:
828 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
831 for (prof = prof_list; prof; prof = prof->next) {
832 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
833 prof->domain_end_load (prof->profiler, domain, result);
838 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
840 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
841 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
842 prof->domain_name (prof->profiler, domain, name);
846 mono_profiler_context_loaded (MonoAppContext *context)
848 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
849 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
850 prof->context_load (prof->profiler, context);
854 mono_profiler_context_unloaded (MonoAppContext *context)
856 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
857 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
858 prof->context_unload (prof->profiler, context);
862 * mono_profiler_shutdown:
865 mono_profiler_shutdown (void)
868 for (prof = prof_list; prof; prof = prof->next) {
869 if (prof->shutdown_callback)
870 prof->shutdown_callback (prof->profiler);
873 mono_profiler_set_events ((MonoProfileFlags)0);
877 * mono_profiler_gc_heap_resize:
880 mono_profiler_gc_heap_resize (gint64 new_size)
883 for (prof = prof_list; prof; prof = prof->next) {
884 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
885 prof->gc_heap_resize (prof->profiler, new_size);
890 * mono_profiler_gc_event:
893 mono_profiler_gc_event (MonoGCEvent event, int generation)
896 for (prof = prof_list; prof; prof = prof->next) {
897 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
898 prof->gc_event (prof->profiler, event, generation);
903 mono_profiler_gc_moves (void **objects, int num)
906 for (prof = prof_list; prof; prof = prof->next) {
907 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
908 prof->gc_moves (prof->profiler, objects, num);
913 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
916 for (prof = prof_list; prof; prof = prof->next) {
917 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
918 prof->gc_handle (prof->profiler, op, type, handle, obj);
923 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
926 for (prof = prof_list; prof; prof = prof->next) {
927 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
928 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
933 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
937 prof_list->gc_event = callback;
938 prof_list->gc_heap_resize = heap_resize_callback;
942 * mono_profiler_install_gc_moves:
943 * \param callback callback function
945 * Install the \p callback function that the GC will call when moving objects.
946 * The callback receives an array of pointers and the number of elements
947 * in the array. Every even element in the array is the original object location
948 * and the following odd element is the new location of the object in memory.
949 * So the number of elements argument will always be a multiple of 2.
950 * Since this callback happens during the GC, it is a restricted environment:
951 * no locks can be taken and the object pointers can be inspected only once
952 * the GC is finished (of course the original location pointers will not
953 * point to valid objects anymore).
956 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
960 prof_list->gc_moves = callback;
964 * mono_profiler_install_gc_roots:
965 * \param handle_callback callback function
966 * \param roots_callback callback function
968 * Install the \p handle_callback function that the GC will call when GC
969 * handles are created or destroyed.
970 * The callback receives an operation, which is either \c MONO_PROFILER_GC_HANDLE_CREATED
971 * or \c MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
972 * object pointer, if present.
973 * Install the \p roots_callback function that the GC will call when tracing
974 * the roots for a collection.
975 * The callback receives the number of elements and three arrays: an array
976 * of objects, an array of root types and flags and an array of extra info.
977 * The size of each array is given by the first argument.
980 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
984 prof_list->gc_handle = handle_callback;
985 prof_list->gc_roots = roots_callback;
989 mono_profiler_gc_finalize_begin (void)
991 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
992 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
993 prof->gc_finalize_begin (prof->profiler);
997 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
999 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1000 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
1001 prof->gc_finalize_object_begin (prof->profiler, obj);
1005 mono_profiler_gc_finalize_object_end (MonoObject *obj)
1007 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1008 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
1009 prof->gc_finalize_object_end (prof->profiler, obj);
1013 mono_profiler_gc_finalize_end (void)
1015 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1016 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
1017 prof->gc_finalize_end (prof->profiler);
1021 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
1026 prof_list->gc_finalize_begin = begin;
1027 prof_list->gc_finalize_object_begin = begin_obj;
1028 prof_list->gc_finalize_object_end = end_obj;
1029 prof_list->gc_finalize_end = end;
1033 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
1037 prof_list->runtime_initialized_event = runtime_initialized_callback;
1041 mono_profiler_runtime_initialized (void) {
1043 for (prof = prof_list; prof; prof = prof->next) {
1044 if (prof->runtime_initialized_event)
1045 prof->runtime_initialized_event (prof->profiler);
1050 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
1053 prof_list->code_chunk_new = callback;
1056 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1058 for (prof = prof_list; prof; prof = prof->next) {
1059 if (prof->code_chunk_new)
1060 prof->code_chunk_new (prof->profiler, chunk, size);
1065 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1068 prof_list->code_chunk_destroy = callback;
1071 mono_profiler_code_chunk_destroy (gpointer chunk) {
1073 for (prof = prof_list; prof; prof = prof->next) {
1074 if (prof->code_chunk_destroy)
1075 prof->code_chunk_destroy (prof->profiler, chunk);
1080 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1083 prof_list->code_buffer_new = callback;
1087 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1091 prof_list->iomap_cb = callback;
1095 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1097 for (prof = prof_list; prof; prof = prof->next) {
1098 if (prof->code_buffer_new)
1099 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1103 static GHashTable *coverage_hash = NULL;
1105 MonoProfileCoverageInfo*
1106 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1108 MonoProfileCoverageInfo *res;
1109 int instrument = FALSE;
1112 for (prof = prof_list; prof; prof = prof->next) {
1113 /* note that we call the filter on all the profilers even if just
1114 * a single one would be enough to instrument a method
1116 if (prof->coverage_filter_cb)
1117 if (prof->coverage_filter_cb (prof->profiler, method))
1123 mono_profiler_coverage_lock ();
1125 coverage_hash = g_hash_table_new (NULL, NULL);
1127 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1129 res->entries = entries;
1131 g_hash_table_insert (coverage_hash, method, res);
1132 mono_profiler_coverage_unlock ();
1137 /* safe only when the method antive code has been unloaded */
1139 mono_profiler_coverage_free (MonoMethod *method)
1141 MonoProfileCoverageInfo* info;
1143 mono_profiler_coverage_lock ();
1144 if (!coverage_hash) {
1145 mono_profiler_coverage_unlock ();
1149 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1152 g_hash_table_remove (coverage_hash, method);
1154 mono_profiler_coverage_unlock ();
1158 * mono_profiler_coverage_get:
1159 * \param prof The profiler handle, installed with mono_profiler_install
1160 * \param method the method to gather information from.
1161 * \param func A routine that will be called back with the results
1163 * If the \c MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1164 * it is possible to obtain coverage information about a give method.
1166 * The function \p func will be invoked repeatedly with instances of the
1167 * \c MonoProfileCoverageEntry structure.
1170 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1173 MonoProfileCoverageInfo* info = NULL;
1176 const unsigned char *start, *end, *cil_code;
1177 MonoMethodHeader *header;
1178 MonoProfileCoverageEntry entry;
1179 MonoDebugMethodInfo *debug_minfo;
1181 mono_profiler_coverage_lock ();
1183 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1184 mono_profiler_coverage_unlock ();
1189 header = mono_method_get_header_checked (method, &error);
1190 mono_error_assert_ok (&error);
1191 start = mono_method_header_get_code (header, &code_size, NULL);
1192 debug_minfo = mono_debug_lookup_method (method);
1194 end = start + code_size;
1195 for (i = 0; i < info->entries; ++i) {
1196 cil_code = info->data [i].cil_code;
1197 if (cil_code && cil_code >= start && cil_code < end) {
1199 offset = cil_code - start;
1200 entry.iloffset = offset;
1201 entry.method = method;
1202 entry.counter = info->data [i].count;
1203 entry.line = entry.col = 1;
1204 entry.filename = NULL;
1206 MonoDebugSourceLocation *location;
1208 location = mono_debug_method_lookup_location (debug_minfo, offset);
1210 entry.line = location->row;
1211 entry.col = location->column;
1212 entry.filename = fname = g_strdup (location->source_file);
1213 mono_debug_free_source_location (location);
1217 func (prof, &entry);
1221 mono_metadata_free_mh (header);
1224 typedef void (*ProfilerInitializer) (const char*);
1225 #define INITIALIZER_NAME "mono_profiler_startup"
1229 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1232 ProfilerInitializer func;
1237 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1247 load_embedded_profiler (const char *desc, const char *name)
1251 MonoDl *pmodule = NULL;
1255 * Some profilers (such as ours) may need to call back into the runtime
1256 * from their sampling callback (which is called in async-signal context).
1257 * They need to be able to know that all references back to the runtime
1258 * have been resolved; otherwise, calling runtime functions may result in
1259 * invoking the dynamic linker which is not async-signal-safe. Passing
1260 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1262 pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1264 g_warning ("Could not open main executable (%s)", err);
1269 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1270 result = load_profiler (pmodule, desc, symbol);
1276 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1278 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1280 MonoDl *pmodule = NULL;
1285 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1289 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1290 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1291 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1295 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1302 load_profiler_from_mono_installation (const char *libname, const char *desc)
1305 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1306 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1309 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1314 * mono_profiler_load:
1315 * \param desc arguments to configure the profiler
1317 * Invoke this method to initialize the profiler. This will drive the
1318 * loading of the internal ("default") or any external profilers.
1320 * This routine is invoked by Mono's driver, but must be called manually
1321 * if you embed Mono into your application.
1324 mono_profiler_load (const char *desc)
1327 mono_gc_base_init ();
1329 if (!desc || (strcmp ("default", desc) == 0)) {
1330 desc = "log:report";
1332 /* we keep command-line compat with the old version here */
1333 if (strncmp (desc, "default:", 8) == 0) {
1334 gchar **args, **ptr;
1335 GString *str = g_string_new ("log:report");
1336 args = g_strsplit (desc + 8, ",", -1);
1337 for (ptr = args; ptr && *ptr; ptr++) {
1338 const char *arg = *ptr;
1340 if (!strcmp (arg, "time"))
1341 g_string_append (str, ",calls");
1342 else if (!strcmp (arg, "alloc"))
1343 g_string_append (str, ",alloc");
1344 else if (!strcmp (arg, "stat"))
1345 g_string_append (str, ",sample");
1346 else if (!strcmp (arg, "jit"))
1347 continue; /* accept and do nothing */
1348 else if (strncmp (arg, "file=", 5) == 0) {
1349 g_string_append_printf (str, ",output=%s", arg + 5);
1351 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1355 desc = cdesc = g_string_free (str, FALSE);
1358 const char* col = strchr (desc, ':');
1361 gboolean res = FALSE;
1364 mname = (char *)g_memdup (desc, col - desc + 1);
1365 mname [col - desc] = 0;
1367 mname = g_strdup (desc);
1369 if (!load_embedded_profiler (desc, mname)) {
1370 libname = g_strdup_printf ("mono-profiler-%s", mname);
1371 res = load_profiler_from_mono_installation (libname, desc);
1372 if (!res && mono_config_get_assemblies_dir ())
1373 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1375 res = load_profiler_from_directory (NULL, libname, desc);
1377 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);