2 * vtune.c: VTune profiler
5 * Virgile Bello (virgile.bello@gmail.com)
7 * (C) 2011 Virgile Bello
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <mono/metadata/profiler.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/assembly.h>
33 #include <mono/metadata/mono-debug.h>
34 #include <mono/metadata/debug-internals.h>
35 #include <mono/metadata/class-internals.h>
36 #include <mono/utils/mono-publib.h>
42 #include <jitprofiling.h>
45 code_buffer_desc (MonoProfilerCodeBufferType type
)
48 case MONO_PROFILER_CODE_BUFFER_METHOD
:
49 return "code_buffer_method";
50 case MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE
:
51 return "code_buffer_method_trampoline";
52 case MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE
:
53 return "code_buffer_unbox_trampoline";
54 case MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE
:
55 return "code_buffer_imt_trampoline";
56 case MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
:
57 return "code_buffer_generics_trampoline";
58 case MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
:
59 return "code_buffer_specific_trampoline";
60 case MONO_PROFILER_CODE_BUFFER_HELPER
:
61 return "code_buffer_misc_helper";
62 case MONO_PROFILER_CODE_BUFFER_MONITOR
:
63 return "code_buffer_monitor";
64 case MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE
:
65 return "code_buffer_delegate_invoke";
66 case MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING
:
67 return "code_buffer_exception_handling";
73 /* called at the end of the program */
75 codeanalyst_shutdown (MonoProfiler
*prof
)
77 iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN
, NULL
);
81 method_jit_done (MonoProfiler
*prof
, MonoMethod
*method
, MonoJitInfo
* jinfo
)
84 MonoDebugSourceLocation
*sourceLoc
;
85 MonoDebugMethodJitInfo
*dmji
;
86 MonoClass
*klass
= mono_method_get_class (method
);
87 char *signature
= mono_signature_get_desc (mono_method_signature_internal (method
), TRUE
);
88 char *name
= g_strdup_printf ("%s(%s)", mono_method_get_name (method
), signature
);
89 char *classname
= g_strdup_printf ("%s%s%s", m_class_get_name_space (klass
), m_class_get_name_space (klass
)[0] != 0 ? "::" : "", m_class_get_name (klass
));
90 gpointer code_start
= mono_jit_info_get_code_start (jinfo
);
91 int code_size
= mono_jit_info_get_code_size (jinfo
);
93 iJIT_Method_Load vtuneMethod
;
94 memset(&vtuneMethod
, 0, sizeof(vtuneMethod
));
95 vtuneMethod
.method_id
= iJIT_GetNewMethodID();
96 vtuneMethod
.method_name
= name
;
97 vtuneMethod
.method_load_address
= code_start
;
98 vtuneMethod
.method_size
= code_size
;
99 vtuneMethod
.class_file_name
= classname
;
101 dmji
= mono_debug_find_method (method
, mono_domain_get());
105 vtuneMethod
.line_number_size
= dmji
->num_line_numbers
;
106 vtuneMethod
.line_number_table
= (vtuneMethod
.line_number_size
!= 0) ?
107 (LineNumberInfo
*)malloc(sizeof(LineNumberInfo
) * vtuneMethod
.line_number_size
) : NULL
;
109 for (i
= 0; i
< dmji
->num_line_numbers
; ++i
)
111 sourceLoc
= mono_debug_lookup_source_location (method
, dmji
->line_numbers
[i
].native_offset
, mono_domain_get());
112 if (sourceLoc
== NULL
)
114 g_free (vtuneMethod
.line_number_table
);
115 vtuneMethod
.line_number_table
= NULL
;
116 vtuneMethod
.line_number_size
= 0;
120 vtuneMethod
.source_file_name
= strdup(sourceLoc
->source_file
);
121 vtuneMethod
.line_number_table
[i
].Offset
= dmji
->line_numbers
[i
].native_offset
;
122 vtuneMethod
.line_number_table
[i
].LineNumber
= sourceLoc
->row
;
123 mono_debug_free_source_location (sourceLoc
);
125 mono_debug_free_method_jit_info (dmji
);
128 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED
, &vtuneMethod
);
130 if (vtuneMethod
.source_file_name
!= NULL
)
131 g_free (vtuneMethod
.source_file_name
);
132 if (vtuneMethod
.line_number_table
!= NULL
)
133 g_free (vtuneMethod
.line_number_table
);
141 code_buffer_new (MonoProfiler
*prof
, void *buffer
, int size
, MonoProfilerCodeBufferType type
, void *data
)
144 iJIT_Method_Load vtuneMethod
;
146 if (type
== MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
)
147 name
= g_strdup_printf ("code_buffer_specific_trampoline_%s", (char*) data
);
149 name
= (char*) code_buffer_desc (type
);
151 memset (&vtuneMethod
, 0, sizeof (vtuneMethod
));
152 vtuneMethod
.method_id
= iJIT_GetNewMethodID ();
153 vtuneMethod
.method_name
= name
;
154 vtuneMethod
.method_load_address
= buffer
;
155 vtuneMethod
.method_size
= size
;
157 iJIT_NotifyEvent (iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED
, &vtuneMethod
);
159 if (type
== MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
) {
165 mono_profiler_init_vtune (const char *desc
);
167 /* the entry point */
169 mono_profiler_init_vtune (const char *desc
)
171 iJIT_IsProfilingActiveFlags flags
= iJIT_IsProfilingActive();
172 if (flags
== iJIT_SAMPLING_ON
)
174 MonoProfilerHandle handle
= mono_profiler_create (NULL
);
175 mono_profiler_set_runtime_shutdown_end_callback (handle
, codeanalyst_shutdown
);
176 mono_profiler_set_jit_done_callback (handle
, method_jit_done
);
177 mono_profiler_set_jit_code_buffer_callback (handle
, code_buffer_new
);