2 #include "mono/metadata/profiler-private.h"
3 #include "mono/metadata/debug-helpers.h"
4 #include "mono/metadata/mono-debug.h"
8 static MonoProfiler * current_profiler = NULL;
10 static MonoProfileAppDomainFunc domain_start_load;
11 static MonoProfileAppDomainResult domain_end_load;
12 static MonoProfileAppDomainFunc domain_start_unload;
13 static MonoProfileAppDomainFunc domain_end_unload;
15 static MonoProfileAssemblyFunc assembly_start_load;
16 static MonoProfileAssemblyResult assembly_end_load;
17 static MonoProfileAssemblyFunc assembly_start_unload;
18 static MonoProfileAssemblyFunc assembly_end_unload;
20 static MonoProfileModuleFunc module_start_load;
21 static MonoProfileModuleResult module_end_load;
22 static MonoProfileModuleFunc module_start_unload;
23 static MonoProfileModuleFunc module_end_unload;
25 static MonoProfileClassFunc class_start_load;
26 static MonoProfileClassResult class_end_load;
27 static MonoProfileClassFunc class_start_unload;
28 static MonoProfileClassFunc class_end_unload;
30 static MonoProfileMethodFunc jit_start;
31 static MonoProfileMethodResult jit_end;
32 static MonoProfileMethodResult man_unman_transition;
33 static MonoProfileAllocFunc allocation_cb;
34 static MonoProfileMethodFunc method_enter;
35 static MonoProfileMethodFunc method_leave;
37 static MonoProfileThreadFunc thread_start;
38 static MonoProfileThreadFunc thread_end;
40 static MonoProfileCoverageFilterFunc coverage_filter_cb;
42 static MonoProfileFunc shutdown_callback;
44 static MonoProfileCoverageLevel coverage_level = MONO_COVERAGE_BASIC_BLOCK;
46 /* this is directly accessible to other mono libs. */
47 MonoProfileFlags mono_profiler_events;
50 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
53 g_error ("profiler already setup");
54 current_profiler = prof;
55 shutdown_callback = callback;
59 mono_profiler_set_events (MonoProfileFlags events)
61 mono_profiler_events = events;
65 mono_profiler_get_events (void)
67 return mono_profiler_events;
71 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
74 method_leave = fleave;
78 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
85 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
92 mono_profiler_install_transition (MonoProfileMethodResult callback)
94 man_unman_transition = callback;
98 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
100 allocation_cb = callback;
104 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
106 coverage_filter_cb = callback;
110 mono_profiler_set_coverage_level (MonoProfileCoverageLevel level)
112 coverage_level = level;
115 MonoProfileCoverageLevel
116 mono_profiler_get_coverage_level ()
118 return coverage_level;
122 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
123 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
126 domain_start_load = start_load;
127 domain_end_load = end_load;
128 domain_start_unload = start_unload;
129 domain_end_unload = end_unload;
133 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
134 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
136 assembly_start_load = start_load;
137 assembly_end_load = end_load;
138 assembly_start_unload = start_unload;
139 assembly_end_unload = end_unload;
143 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
144 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
146 module_start_load = start_load;
147 module_end_load = end_load;
148 module_start_unload = start_unload;
149 module_end_unload = end_unload;
153 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
154 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
156 class_start_load = start_load;
157 class_end_load = end_load;
158 class_start_unload = start_unload;
159 class_end_unload = end_unload;
163 mono_profiler_method_enter (MonoMethod *method)
165 if ((mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) && method_enter)
166 method_enter (current_profiler, method);
170 mono_profiler_method_leave (MonoMethod *method)
172 if ((mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) && method_leave)
173 method_leave (current_profiler, method);
177 mono_profiler_method_jit (MonoMethod *method)
179 if ((mono_profiler_events & MONO_PROFILE_JIT_COMPILATION) && jit_start)
180 jit_start (current_profiler, method);
184 mono_profiler_method_end_jit (MonoMethod *method, int result)
186 if ((mono_profiler_events & MONO_PROFILE_JIT_COMPILATION) && jit_end)
187 jit_end (current_profiler, method, result);
191 mono_profiler_code_transition (MonoMethod *method, int result)
193 if ((mono_profiler_events & MONO_PROFILE_TRANSITIONS) && man_unman_transition)
194 man_unman_transition (current_profiler, method, result);
198 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
200 if ((mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && allocation_cb)
201 allocation_cb (current_profiler, obj, klass);
205 mono_profiler_thread_start (guint32 tid)
207 if ((mono_profiler_events & MONO_PROFILE_THREADS) && thread_start)
208 thread_start (current_profiler, tid);
212 mono_profiler_thread_end (guint32 tid)
214 if ((mono_profiler_events & MONO_PROFILE_THREADS) && thread_end)
215 thread_end (current_profiler, tid);
219 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
221 if (!(mono_profiler_events & MONO_PROFILE_ASSEMBLY_EVENTS))
225 case MONO_PROFILE_START_LOAD:
226 if (assembly_start_load)
227 assembly_start_load (current_profiler, assembly);
229 case MONO_PROFILE_START_UNLOAD:
230 if (assembly_start_unload)
231 assembly_start_unload (current_profiler, assembly);
233 case MONO_PROFILE_END_UNLOAD:
234 if (assembly_end_unload)
235 assembly_end_unload (current_profiler, assembly);
238 g_assert_not_reached ();
243 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
245 if ((mono_profiler_events & MONO_PROFILE_ASSEMBLY_EVENTS) && assembly_end_load)
246 assembly_end_load (current_profiler, assembly, result);
250 mono_profiler_module_event (MonoImage *module, int code)
252 if (!(mono_profiler_events & MONO_PROFILE_MODULE_EVENTS))
256 case MONO_PROFILE_START_LOAD:
257 if (module_start_load)
258 module_start_load (current_profiler, module);
260 case MONO_PROFILE_START_UNLOAD:
261 if (module_start_unload)
262 module_start_unload (current_profiler, module);
264 case MONO_PROFILE_END_UNLOAD:
265 if (module_end_unload)
266 module_end_unload (current_profiler, module);
269 g_assert_not_reached ();
274 mono_profiler_module_loaded (MonoImage *module, int result)
276 if ((mono_profiler_events & MONO_PROFILE_MODULE_EVENTS) && module_end_load)
277 module_end_load (current_profiler, module, result);
281 mono_profiler_class_event (MonoClass *klass, int code)
283 if (!(mono_profiler_events & MONO_PROFILE_CLASS_EVENTS))
287 case MONO_PROFILE_START_LOAD:
288 if (class_start_load)
289 class_start_load (current_profiler, klass);
291 case MONO_PROFILE_START_UNLOAD:
292 if (class_start_unload)
293 class_start_unload (current_profiler, klass);
295 case MONO_PROFILE_END_UNLOAD:
296 if (class_end_unload)
297 class_end_unload (current_profiler, klass);
300 g_assert_not_reached ();
305 mono_profiler_class_loaded (MonoClass *klass, int result)
307 if ((mono_profiler_events & MONO_PROFILE_CLASS_EVENTS) && class_end_load)
308 class_end_load (current_profiler, klass, result);
312 mono_profiler_appdomain_event (MonoDomain *domain, int code)
314 if (!(mono_profiler_events & MONO_PROFILE_APPDOMAIN_EVENTS))
318 case MONO_PROFILE_START_LOAD:
319 if (domain_start_load)
320 domain_start_load (current_profiler, domain);
322 case MONO_PROFILE_START_UNLOAD:
323 if (domain_start_unload)
324 domain_start_unload (current_profiler, domain);
326 case MONO_PROFILE_END_UNLOAD:
327 if (domain_end_unload)
328 domain_end_unload (current_profiler, domain);
331 g_assert_not_reached ();
336 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
338 if ((mono_profiler_events & MONO_PROFILE_APPDOMAIN_EVENTS) && domain_end_load)
339 domain_end_load (current_profiler, domain, result);
343 mono_profiler_shutdown (void)
345 if (current_profiler && shutdown_callback)
346 shutdown_callback (current_profiler);
349 static GHashTable *coverage_hash = NULL;
351 MonoProfileCoverageInfo*
352 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
354 MonoProfileCoverageInfo *res;
356 if (coverage_filter_cb)
357 if (! (*coverage_filter_cb) (current_profiler, method))
361 coverage_hash = g_hash_table_new (NULL, NULL);
363 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
365 res->entries = entries;
367 g_hash_table_insert (coverage_hash, method, res);
372 /* safe only when the method antive code has been unloaded */
374 mono_profiler_coverage_free (MonoMethod *method)
376 MonoProfileCoverageInfo* info;
379 info = g_hash_table_lookup (coverage_hash, method);
383 g_hash_table_remove (coverage_hash, method);
387 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
389 MonoProfileCoverageInfo* info;
392 unsigned char *start, *end, *cil_code;
393 MonoMethodHeader *header;
394 MonoProfileCoverageEntry entry;
396 info = g_hash_table_lookup (coverage_hash, method);
400 header = ((MonoMethodNormal *)method)->header;
401 start = (unsigned char*)header->code;
402 end = start + header->code_size;
403 for (i = 0; i < info->entries; ++i) {
404 cil_code = info->data [i].cil_code;
405 if (cil_code && cil_code >= start && cil_code < end) {
406 offset = cil_code - start;
407 entry.iloffset = offset;
408 entry.method = method;
409 entry.counter = info->data [i].count;
410 /* the debug interface doesn't support column info, sigh */
412 entry.filename = mono_debug_source_location_from_il_offset (method, offset, &line);
421 * Small profiler extracted from mint: we should move it in a loadable module
422 * and improve it to do graphs and more accurate timestamping with rdtsc.
426 #define USE_WIN32COUNTER 0
430 unsigned int lows, highs, lowe, highe;
433 #define rdtsc(low,high) \
434 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
439 rdtsc_elapsed (MonoRdtscTimer *t)
441 unsigned long long diff;
442 unsigned int highe = t->highe;
443 if (t->lowe < t->lows)
445 diff = (((unsigned long long) highe - t->highs) << 32) + (t->lowe - t->lows);
446 return ((double)diff / freq) / 1000000; /* have to return the result in seconds */
457 if (!(cpuinfo = fopen ("/proc/cpuinfo", "r")))
459 while (fgets (buf, sizeof(buf), cpuinfo)) {
460 if (sscanf (buf, "cpu MHz : %f", &val) == 1) {
461 /*printf ("got mh: %f\n", val);*/
464 if (strncmp (buf, "flags", 5) == 0) {
465 if (strstr (buf, "tsc")) {
467 /*printf ("have tsc\n");*/
472 return have_flag? have_freq: 0;
475 #define MONO_TIMER_STARTUP \
476 if (!(freq = have_rdtsc ())) g_error ("Compiled with TSC support, but none found");
477 #define MONO_TIMER_TYPE MonoRdtscTimer
478 #define MONO_TIMER_INIT(t)
479 #define MONO_TIMER_DESTROY(t)
480 #define MONO_TIMER_START(t) rdtsc ((t).lows, (t).highs);
481 #define MONO_TIMER_STOP(t) rdtsc ((t).lowe, (t).highe);
482 #define MONO_TIMER_ELAPSED(t) rdtsc_elapsed (&(t))
484 #elif USE_WIN32COUNTER
488 LARGE_INTEGER start, stop;
494 win32_elapsed (MonoWin32Timer *t)
496 LONGLONG diff = t->stop.QuadPart - t->start.QuadPart;
497 return ((double)diff / freq) / 1000000; /* have to return the result in seconds */
501 have_win32counter (void) {
504 if (!QueryPerformanceFrequency (&f))
509 #define MONO_TIMER_STARTUP \
510 if (!(freq = have_win32counter ())) g_error ("Compiled with Win32 counter support, but none found");
511 #define MONO_TIMER_TYPE MonoWin32Timer
512 #define MONO_TIMER_INIT(t)
513 #define MONO_TIMER_DESTROY(t)
514 #define MONO_TIMER_START(t) QueryPerformanceCounter (&(t).start)
515 #define MONO_TIMER_STOP(t) QueryPerformanceCounter (&(t).stop)
516 #define MONO_TIMER_ELAPSED(t) win32_elapsed (&(t))
521 GTimeVal start, stop;
525 timeval_elapsed (MonoGLibTimer *t)
527 if (t->start.tv_usec > t->stop.tv_usec) {
528 t->stop.tv_usec += G_USEC_PER_SEC;
531 return (t->stop.tv_sec - t->start.tv_sec)
532 + ((double)(t->stop.tv_usec - t->start.tv_usec))/ G_USEC_PER_SEC;
535 #define MONO_TIMER_STARTUP
536 #define MONO_TIMER_TYPE MonoGLibTimer
537 #define MONO_TIMER_INIT(t)
538 #define MONO_TIMER_DESTROY(t)
539 #define MONO_TIMER_START(t) g_get_current_time (&(t).start)
540 #define MONO_TIMER_STOP(t) g_get_current_time (&(t).stop)
541 #define MONO_TIMER_ELAPSED(t) timeval_elapsed (&(t))
544 typedef struct _AllocInfo AllocInfo;
545 typedef struct _CallerInfo CallerInfo;
546 typedef struct _LastCallerInfo LastCallerInfo;
548 struct _MonoProfiler {
550 MonoMemPool *mempool;
551 /* info about JIT time */
552 MONO_TIMER_TYPE jit_timer;
555 MonoMethod *max_jit_method;
558 /* tls_id is the id for the TLS slot where MonoProfiler is stored */
562 /* chain of callers for the current thread */
563 LastCallerInfo *callers;
564 /* LastCallerInfo nodes for faster allocation */
565 LastCallerInfo *cstorage;
572 AllocInfo *alloc_info;
573 CallerInfo *caller_info;
576 typedef struct _MethodCallProfile MethodCallProfile;
578 struct _MethodCallProfile {
579 MethodCallProfile *next;
580 MONO_TIMER_TYPE timer;
597 struct _LastCallerInfo {
598 LastCallerInfo *next;
600 MONO_TIMER_TYPE timer;
604 create_profiler (void)
606 MonoProfiler *prof = g_new0 (MonoProfiler, 1);
608 prof->methods = g_hash_table_new (g_direct_hash, g_direct_equal);
609 MONO_TIMER_INIT (prof->jit_timer);
610 prof->mempool = mono_mempool_new ();
614 #define GET_THREAD_PROF(prof) do {\
615 MonoProfiler *_tprofiler = TlsGetValue ((prof)->tls_id); \
617 _tprofiler = create_profiler (); \
618 prof->per_thread = g_slist_prepend (prof->per_thread, _tprofiler); \
619 TlsSetValue ((prof)->tls_id, _tprofiler); \
624 /* thread unsafe but faster variant */
625 #define GET_THREAD_PROF(prof)
629 compare_profile (MethodProfile *profa, MethodProfile *profb)
631 return (gint)((profb->total - profa->total)*1000);
635 build_profile (MonoMethod *m, MethodProfile *prof, GList **funcs)
638 *funcs = g_list_insert_sorted (*funcs, prof, (GCompareFunc)compare_profile);
642 method_get_name (MonoMethod* method)
646 sig = mono_signature_get_desc (method->signature, FALSE);
647 res = g_strdup_printf ("%s.%s::%s(%s)", method->klass->name_space, method->klass->name,
653 static void output_callers (MethodProfile *p);
656 output_profile (GList *funcs)
661 guint64 total_calls = 0;
664 g_print ("Time(ms) Count P/call(ms) Method name\n");
665 for (tmp = funcs; tmp; tmp = tmp->next) {
667 total_calls += p->count;
668 if (!(gint)(p->total*1000))
670 m = method_get_name (p->method);
671 printf ("########################\n% 8.3f %7llu % 8.3f %s\n",
672 (double)(p->total*1000), p->count, (double)(p->total*1000)/(double)p->count, m);
677 printf ("Total number of calls: %lld\n", total_calls);
686 compare_newobj_profile (NewobjProfile *profa, NewobjProfile *profb)
688 return (gint)profb->count - (gint)profa->count;
692 build_newobj_profile (MonoClass *class, MethodProfile *mprof, GList **funcs)
694 NewobjProfile *prof = g_new (NewobjProfile, 1);
699 /* we use the total amount of memory to sort */
700 for (tmp = mprof->alloc_info; tmp; tmp = tmp->next)
703 *funcs = g_list_insert_sorted (*funcs, prof, (GCompareFunc)compare_newobj_profile);
707 compare_caller (CallerInfo *a, CallerInfo *b)
709 return b->count - a->count;
713 compare_alloc (AllocInfo *a, AllocInfo *b)
715 return b->mem - a->mem;
719 sort_alloc_list (AllocInfo *ai)
723 for (tmp = ai; tmp; tmp = tmp->next) {
724 l = g_slist_insert_sorted (l, tmp, (GCompareFunc)compare_alloc);
730 sort_caller_list (CallerInfo *ai)
734 for (tmp = ai; tmp; tmp = tmp->next) {
735 l = g_slist_insert_sorted (l, tmp, (GCompareFunc)compare_caller);
741 output_callers (MethodProfile *p) {
742 guint total_callers, percent;
743 GSList *sorted, *tmps;
747 g_print (" Callers (with count) that contribute at least for 1%%:\n");
749 for (cinfo = p->caller_info; cinfo; cinfo = cinfo->next) {
750 total_callers += cinfo->count;
752 sorted = sort_caller_list (p->caller_info);
753 for (tmps = sorted; tmps; tmps = tmps->next) {
755 percent = (cinfo->count * 100)/total_callers;
758 m = method_get_name (cinfo->caller);
759 g_print (" %8d % 3d %% %s\n", cinfo->count, percent, m);
765 output_newobj_profile (GList *proflist)
776 GSList *sorted, *tmps;
778 g_print ("\nAllocation profiler\n");
781 g_print ("%-9s %s\n", "Total mem", "Method");
782 for (tmp = proflist; tmp; tmp = tmp->next) {
785 if (p->count < 50000)
788 m = method_get_name (mp->method);
789 g_print ("########################\n%8d KB %s\n", p->count / 1024, m);
791 sorted = sort_alloc_list (mp->alloc_info);
792 for (tmps = sorted; tmps; tmps = tmps->next) {
794 if (ainfo->mem < 50000)
796 klass = ainfo->klass;
799 klass = klass->element_class;
803 g_snprintf (buf, sizeof (buf), "%s.%s%s",
804 klass->name_space, klass->name, isarray);
805 g_print (" %8d KB %8d %-48s\n", ainfo->mem / 1024, ainfo->count, buf);
810 g_print ("Total memory allocated: %d KB\n", total / 1024);
814 merge_methods (MonoMethod *method, MethodProfile *profile, MonoProfiler *prof)
816 MethodProfile *mprof;
817 AllocInfo *talloc_info, *alloc_info;
818 CallerInfo *tcaller_info, *caller_info;
820 mprof = g_hash_table_lookup (prof->methods, method);
822 /* the master thread didn't see this method, just transfer the info as is */
823 g_hash_table_insert (prof->methods, method, profile);
826 /* merge the info from profile into mprof */
827 mprof->count += profile->count;
828 mprof->total += profile->total;
829 /* merge alloc info */
830 for (talloc_info = profile->alloc_info; talloc_info; talloc_info = talloc_info->next) {
831 for (alloc_info = mprof->alloc_info; alloc_info; alloc_info = alloc_info->next) {
832 if (alloc_info->klass == talloc_info->klass) {
833 /* mprof already has a record for the klass, merge */
834 alloc_info->count += talloc_info->count;
835 alloc_info->mem += talloc_info->mem;
840 /* mprof didn't have the info, just copy it over */
841 alloc_info = mono_mempool_alloc0 (prof->mempool, sizeof (AllocInfo));
842 *alloc_info = *talloc_info;
843 alloc_info->next = mprof->alloc_info;
844 mprof->alloc_info = alloc_info->next;
847 /* merge callers info */
848 for (tcaller_info = profile->caller_info; tcaller_info; tcaller_info = tcaller_info->next) {
849 for (caller_info = mprof->caller_info; caller_info; caller_info = caller_info->next) {
850 if (caller_info->caller == tcaller_info->caller) {
851 /* mprof already has a record for the caller method, merge */
852 caller_info->count += tcaller_info->count;
857 /* mprof didn't have the info, just copy it over */
858 caller_info = mono_mempool_alloc0 (prof->mempool, sizeof (CallerInfo));
859 *caller_info = *tcaller_info;
860 caller_info->next = mprof->caller_info;
861 mprof->caller_info = caller_info->next;
867 merge_thread_data (MonoProfiler *master, MonoProfiler *tprof)
869 master->jit_time += tprof->jit_time;
870 master->methods_jitted += tprof->methods_jitted;
871 if (master->max_jit_time < tprof->max_jit_time) {
872 master->max_jit_time = tprof->max_jit_time;
873 master->max_jit_method = tprof->max_jit_method;
876 g_hash_table_foreach (tprof->methods, (GHFunc)merge_methods, master);
880 simple_method_enter (MonoProfiler *prof, MonoMethod *method)
882 MethodProfile *profile_info;
883 LastCallerInfo *callinfo;
884 GET_THREAD_PROF (prof);
885 /*g_print ("enter %p %s::%s in %d (%p)\n", method, method->klass->name, method->name, GetCurrentThreadId (), prof);*/
886 if (!(profile_info = g_hash_table_lookup (prof->methods, method))) {
887 profile_info = mono_mempool_alloc0 (prof->mempool, sizeof (MethodProfile));
888 MONO_TIMER_INIT (profile_info->u.timer);
889 g_hash_table_insert (prof->methods, method, profile_info);
891 profile_info->count++;
894 MonoMethod *caller = prof->callers->method;
895 for (cinfo = profile_info->caller_info; cinfo; cinfo = cinfo->next) {
896 if (cinfo->caller == caller)
900 cinfo = mono_mempool_alloc0 (prof->mempool, sizeof (CallerInfo));
901 cinfo->caller = caller;
902 cinfo->next = profile_info->caller_info;
903 profile_info->caller_info = cinfo;
907 if (!(callinfo = prof->cstorage)) {
908 callinfo = mono_mempool_alloc (prof->mempool, sizeof (LastCallerInfo));
909 MONO_TIMER_INIT (callinfo->timer);
911 prof->cstorage = prof->cstorage->next;
913 callinfo->method = method;
914 callinfo->next = prof->callers;
915 prof->callers = callinfo;
916 MONO_TIMER_START (callinfo->timer);
920 simple_method_leave (MonoProfiler *prof, MonoMethod *method)
922 MethodProfile *profile_info;
923 LastCallerInfo *callinfo, *newcallinfo = NULL;
925 GET_THREAD_PROF (prof);
926 /*g_print ("leave %p %s::%s in %d (%p)\n", method, method->klass->name, method->name, GetCurrentThreadId (), prof);*/
927 callinfo = prof->callers;
928 /* should really not happen, but we don't catch exceptions events, yet ... */
930 MONO_TIMER_STOP (callinfo->timer);
931 profile_info = g_hash_table_lookup (prof->methods, callinfo->method);
933 profile_info->total += MONO_TIMER_ELAPSED (callinfo->timer);
934 newcallinfo = callinfo->next;
935 callinfo->next = prof->cstorage;
936 prof->cstorage = callinfo;
937 if (callinfo->method == method)
939 callinfo = newcallinfo;
941 prof->callers = newcallinfo;
945 simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
947 MethodProfile *profile_info;
950 GET_THREAD_PROF (prof);
952 if (!(profile_info = g_hash_table_lookup (prof->methods, prof->callers->method)))
953 g_assert_not_reached ();
955 return; /* fine for now */
958 for (tmp = profile_info->alloc_info; tmp; tmp = tmp->next) {
959 if (tmp->klass == klass)
963 tmp = mono_mempool_alloc0 (prof->mempool, sizeof (AllocInfo));
965 tmp->next = profile_info->alloc_info;
966 profile_info->alloc_info = tmp;
969 if (klass == mono_defaults.string_class) {
970 tmp->mem += sizeof (MonoString) + 2 * mono_string_length ((MonoString*)obj) + 2;
971 } else if (klass->parent == mono_defaults.array_class) {
972 tmp->mem += sizeof (MonoArray) + mono_array_element_size (klass) * mono_array_length ((MonoArray*)obj);
974 tmp->mem += mono_class_instance_size (klass);
979 simple_method_jit (MonoProfiler *prof, MonoMethod *method)
981 GET_THREAD_PROF (prof);
982 prof->methods_jitted++;
983 MONO_TIMER_START (prof->jit_timer);
987 simple_method_end_jit (MonoProfiler *prof, MonoMethod *method, int result)
990 GET_THREAD_PROF (prof);
991 MONO_TIMER_STOP (prof->jit_timer);
992 jtime = MONO_TIMER_ELAPSED (prof->jit_timer);
993 prof->jit_time += jtime;
994 if (jtime > prof->max_jit_time) {
995 prof->max_jit_time = jtime;
996 prof->max_jit_method = method;
1001 simple_shutdown (MonoProfiler *prof)
1003 GList *profile = NULL;
1004 MonoProfiler *tprof;
1008 for (tmp = prof->per_thread; tmp; tmp = tmp->next) {
1010 merge_thread_data (prof, tprof);
1013 printf("Total time spent compiling %d methods (sec): %.4g\n", prof->methods_jitted, prof->jit_time);
1014 if (prof->max_jit_method) {
1015 str = method_get_name (prof->max_jit_method);
1016 printf("Slowest method to compile (sec): %.4g: %s\n", prof->max_jit_time, str);
1019 g_hash_table_foreach (prof->methods, (GHFunc)build_profile, &profile);
1020 output_profile (profile);
1021 g_list_free (profile);
1024 g_hash_table_foreach (prof->methods, (GHFunc)build_newobj_profile, &profile);
1025 output_newobj_profile (profile);
1026 g_list_free (profile);
1030 mono_profiler_install_simple (const char *desc)
1036 prof = create_profiler ();
1037 prof->tls_id = TlsAlloc ();
1038 TlsSetValue (prof->tls_id, prof);
1040 mono_profiler_install (prof, simple_shutdown);
1041 /* later do also object creation */
1042 mono_profiler_install_enter_leave (simple_method_enter, simple_method_leave);
1043 mono_profiler_install_jit_compile (simple_method_jit, simple_method_end_jit);
1044 mono_profiler_install_allocation (simple_allocation);
1045 mono_profiler_set_events (MONO_PROFILE_ENTER_LEAVE|MONO_PROFILE_JIT_COMPILATION|MONO_PROFILE_ALLOCATIONS);
1048 typedef void (*ProfilerInitializer) (const char*);
1049 #define INITIALIZER_NAME "mono_profiler_startup"
1052 mono_profiler_load (const char *desc)
1054 if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
1055 mono_profiler_install_simple (desc);
1058 const char* col = strchr (desc, ':');
1063 mname = g_memdup (desc, col - desc);
1064 mname [col - desc] = 0;
1066 mname = g_strdup (desc);
1068 libname = g_strdup_printf ("mono-profiler-%s", mname);
1069 path = g_module_build_path (NULL, libname);
1070 pmodule = g_module_open (path, G_MODULE_BIND_LAZY);
1072 ProfilerInitializer func;
1073 if (!g_module_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func)) {
1074 g_warning ("Cannot find initializer function %s in profiler module: %s", INITIALIZER_NAME, libname);
1079 g_warning ("Error loading profiler module '%s': %s", libname, g_module_error ());