2 * mono-codeanalyst.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-mono-symfile.h>
40 #include <jitprofiling.h>
43 code_buffer_desc (MonoProfilerCodeBufferType type)
46 case MONO_PROFILER_CODE_BUFFER_METHOD:
47 return "code_buffer_method";
48 case MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE:
49 return "code_buffer_method_trampoline";
50 case MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE:
51 return "code_buffer_unbox_trampoline";
52 case MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE:
53 return "code_buffer_imt_trampoline";
54 case MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE:
55 return "code_buffer_generics_trampoline";
56 case MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE:
57 return "code_buffer_specific_trampoline";
58 case MONO_PROFILER_CODE_BUFFER_HELPER:
59 return "code_buffer_misc_helper";
60 case MONO_PROFILER_CODE_BUFFER_MONITOR:
61 return "code_buffer_monitor";
62 case MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE:
63 return "code_buffer_delegate_invoke";
69 /* called at the end of the program */
71 codeanalyst_shutdown (MonoProfiler *prof)
73 iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, NULL);
77 method_jit_result (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result) {
78 if (result == MONO_PROFILE_OK) {
80 MonoDebugSourceLocation *sourceLoc;
81 MonoDebugMethodJitInfo *dmji;
82 MonoClass *klass = mono_method_get_class (method);
83 char *signature = mono_signature_get_desc (mono_method_signature (method), TRUE);
84 char *name = g_strdup_printf ("%s(%s)", mono_method_get_name (method), signature);
85 char *classname = g_strdup_printf ("%s%s%s", mono_class_get_namespace (klass), mono_class_get_namespace (klass)[0] != 0 ? "::" : "", mono_class_get_name (klass));
86 gpointer code_start = mono_jit_info_get_code_start (jinfo);
87 int code_size = mono_jit_info_get_code_size (jinfo);
89 iJIT_Method_Load vtuneMethod;
90 memset(&vtuneMethod, 0, sizeof(vtuneMethod));
91 vtuneMethod.method_id = iJIT_GetNewMethodID();
92 vtuneMethod.method_name = name;
93 vtuneMethod.method_load_address = code_start;
94 vtuneMethod.method_size = code_size;
95 vtuneMethod.class_file_name = classname;
97 dmji = mono_debug_find_method (method, mono_domain_get());
101 vtuneMethod.line_number_size = dmji->num_line_numbers;
102 vtuneMethod.line_number_table = (vtuneMethod.line_number_size != 0) ?
103 (LineNumberInfo*)malloc(sizeof(LineNumberInfo) * vtuneMethod.line_number_size) : NULL;
105 for (i = 0; i < dmji->num_line_numbers; ++i)
107 sourceLoc = mono_debug_lookup_source_location (method, dmji->line_numbers[i].native_offset, mono_domain_get());
108 if (sourceLoc == NULL)
110 free(vtuneMethod.line_number_table);
111 vtuneMethod.line_number_table = NULL;
112 vtuneMethod.line_number_size = 0;
116 vtuneMethod.source_file_name = strdup(sourceLoc->source_file);
117 vtuneMethod.line_number_table[i].Offset = dmji->line_numbers[i].native_offset;
118 vtuneMethod.line_number_table[i].LineNumber = sourceLoc->row;
119 mono_debug_free_source_location (sourceLoc);
121 mono_debug_free_method_jit_info (dmji);
124 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &vtuneMethod);
126 if (vtuneMethod.source_file_name != NULL)
127 free(vtuneMethod.source_file_name);
128 if (vtuneMethod.line_number_table != NULL)
129 free(vtuneMethod.line_number_table);
138 code_buffer_new (MonoProfiler *prof, void *buffer, int size, MonoProfilerCodeBufferType type, void *data)
141 iJIT_Method_Load vtuneMethod;
143 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE)
144 name = g_strdup_printf ("code_buffer_specific_trampoline_%s", (char*) data);
146 name = (char*) code_buffer_desc (type);
148 memset (&vtuneMethod, 0, sizeof (vtuneMethod));
149 vtuneMethod.method_id = iJIT_GetNewMethodID ();
150 vtuneMethod.method_name = name;
151 vtuneMethod.method_load_address = buffer;
152 vtuneMethod.method_size = size;
154 iJIT_NotifyEvent (iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &vtuneMethod);
156 if (type == MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE) {
161 /* the entry point */
163 mono_profiler_startup (const char *desc)
165 iJIT_IsProfilingActiveFlags flags = iJIT_IsProfilingActive();
166 if (flags == iJIT_SAMPLING_ON)
168 mono_profiler_install (NULL, codeanalyst_shutdown);
169 mono_profiler_install_jit_end (method_jit_result);
170 mono_profiler_install_code_buffer_new (code_buffer_new);
171 mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);