2 * mono-profiler-iomap.c: IOMAP string profiler for Mono.
5 * Marek Habersack <mhabersack@novell.com>
7 * Copyright (c) 2009 Novell, Inc (http://novell.com)
12 #include <mono/utils/mono-io-portability.h>
13 #include <mono/metadata/metadata.h>
14 #include <mono/metadata/metadata-internals.h>
15 #include <mono/metadata/class.h>
16 #include <mono/metadata/class-internals.h>
17 #include <mono/metadata/image.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/profiler.h>
22 #include <mono/metadata/loader.h>
23 #include <mono/io-layer/mono-mutex.h>
25 #define LOCATION_INDENT " "
26 #define BACKTRACE_SIZE 64
28 typedef struct _MonoStackBacktraceInfo
32 } MonoStackBacktraceInfo;
39 } MismatchedFilesStats;
41 typedef struct _SavedString
45 void *stack [BACKTRACE_SIZE];
47 struct _SavedString *next;
50 typedef struct _SavedStringFindInfo
54 } SavedStringFindInfo;
56 typedef struct _StringLocation
59 struct _StringLocation *next;
64 GHashTable *mismatched_files_hash;
65 GHashTable *saved_strings_hash;
66 GHashTable *string_locations_hash;
67 gboolean may_have_locations;
70 typedef struct _StackWalkData
78 static mono_mutex_t mismatched_files_section;
79 static gboolean runtime_initialized = FALSE;
81 static inline void append_report (GString **report, const gchar *format, ...);
82 static inline void print_report (const gchar *format, ...);
83 static inline guint32 do_calc_string_hash (guint32 hash, const gchar *str);
84 static inline guint32 calc_strings_hash (const gchar *str1, const gchar *str2, guint32 *str1hash);
85 static void print_mismatched_stats (MonoProfiler *prof);
86 static inline gchar *build_hint (SavedString *head);
87 static inline gchar *build_hint_from_stack (MonoDomain *domain, void **stack, gint stack_entries);
88 static inline void store_string_location (MonoProfiler *prof, const gchar *string, guint32 hash, size_t len);
89 static void mono_portability_remember_string (MonoProfiler *prof, MonoDomain *domain, MonoString *str);
90 void mono_profiler_startup (const char *desc);
92 static void mismatched_stats_foreach_func (gpointer key, gpointer value, gpointer user_data)
94 MismatchedFilesStats *stats = (MismatchedFilesStats*)value;
95 StringLocation *location;
96 MonoProfiler *prof = (MonoProfiler*)user_data;
98 gboolean bannerShown = FALSE;
100 hash = do_calc_string_hash (0, stats->requestedName);
105 stats->count, stats->requestedName, stats->actualName);
107 if (!prof->may_have_locations) {
108 fprintf (stdout, "\n");
112 location = g_hash_table_lookup (prof->string_locations_hash, &hash);
114 if (location->hint && strlen (location->hint) > 0) {
116 fprintf (stdout, "Locations:\n");
119 fprintf (stdout, "%s", location->hint);
121 location = location->next;
123 fprintf (stdout, LOCATION_INDENT "--\n");
126 fprintf (stdout, "\n");
129 static void print_mismatched_stats (MonoProfiler *prof)
131 if (!prof->mismatched_files_hash || g_hash_table_size (prof->mismatched_files_hash) == 0)
134 prof->may_have_locations = g_hash_table_size (prof->string_locations_hash) > 0;
136 fprintf (stdout, "\n-=-=-=-=-=-=-= MONO_IOMAP Stats -=-=-=-=-=-=-=\n");
137 g_hash_table_foreach (prof->mismatched_files_hash, mismatched_stats_foreach_func, (gpointer)prof);
141 static guint mismatched_files_guint32_hash (gconstpointer key)
146 return *((guint32*)key);
149 static gboolean mismatched_files_guint32_equal (gconstpointer key1, gconstpointer key2)
154 return (gboolean)(*((guint32*)key1) == *((guint32*)key2));
157 static inline guint32 do_calc_string_hash (guint32 hash, const gchar *str)
160 gchar *cc = (gchar*)str;
161 gchar *end = (gchar*)(str + strlen (str) - 1);
163 for (; cc < end; cc += 2) {
164 ret = (ret << 5) - ret + *cc;
165 ret = (ret << 5) - ret + cc [1];
169 ret = (ret << 5) - ret + *cc;
174 static inline guint32 calc_strings_hash (const gchar *str1, const gchar *str2, guint32 *str1hash)
176 guint32 hash = do_calc_string_hash (0, str1);
179 return do_calc_string_hash (hash, str2);
182 static inline void print_report (const gchar *format, ...)
190 fprintf (stdout, "-=-=-=-=-=-=- MONO_IOMAP REPORT -=-=-=-=-=-=-\n");
191 va_start (ap, format);
192 vfprintf (stdout, format, ap);
193 fprintf (stdout, "\n");
195 klass = mono_class_from_name (mono_get_corlib (), "System", "Environment");
196 mono_class_init (klass);
197 prop = mono_class_get_property_from_name (klass, "StackTrace");
198 str = (MonoString*)mono_property_get_value (prop, NULL, NULL, NULL);
199 stack_trace = mono_string_to_utf8 (str);
201 fprintf (stdout, "-= Stack Trace =-\n%s\n\n", stack_trace);
202 g_free (stack_trace);
206 static inline void append_report (GString **report, const gchar *format, ...)
208 #if GLIB_CHECK_VERSION(2,14,0)
211 *report = g_string_new ("");
213 va_start (ap, format);
214 g_string_append_vprintf (*report, format, ap);
217 g_assert_not_reached ();
221 static gboolean saved_strings_find_func (gpointer key, gpointer value, gpointer user_data)
223 SavedStringFindInfo *info = (SavedStringFindInfo*)user_data;
224 SavedString *saved = (SavedString*)value;
228 if (!info || !saved || saved->string->length != info->len)
231 utf_str = mono_string_to_utf8 (saved->string);
232 hash = do_calc_string_hash (0, utf_str);
235 if (hash != info->hash)
241 static inline void store_string_location (MonoProfiler *prof, const gchar *string, guint32 hash, size_t len)
243 StringLocation *location = g_hash_table_lookup (prof->string_locations_hash, &hash);
245 SavedStringFindInfo info;
254 /* Expensive but unavoidable... */
255 saved = (SavedString*)g_hash_table_find (prof->saved_strings_hash, saved_strings_find_func, &info);
256 hashptr = (guint32*)g_malloc (sizeof (guint32));
258 location = (StringLocation*)g_malloc0 (sizeof (location));
260 g_hash_table_insert (prof->string_locations_hash, hashptr, location);
264 g_hash_table_remove (prof->saved_strings_hash, saved->string);
265 location->hint = build_hint (saved);
268 static gboolean ignore_frame (MonoMethod *method)
270 MonoClass *klass = method->klass;
272 if (method->wrapper_type != MONO_WRAPPER_NONE)
275 /* Now ignore the assemblies we know shouldn't contain mixed-case names (only the most frequent cases) */
277 if (strcmp (klass->image->assembly_name, "mscorlib") == 0)
279 else if (strcmp (klass->image->assembly_name, "System") == 0)
281 else if (strncmp (klass->image->assembly_name, "Mono.", 5) == 0)
283 else if (strncmp (klass->image->assembly_name, "System.", 7) == 0)
285 else if (strcmp (klass->image->assembly_name, "PEAPI") == 0)
292 static inline gchar *build_hint_from_stack (MonoDomain *domain, void **stack, gint stack_entries)
295 MonoMethod *method, *selectedMethod;
296 MonoAssembly *assembly;
298 MonoDebugSourceLocation *location;
299 MonoStackBacktraceInfo *info;
300 gboolean use_full_trace;
302 gint i, native_offset, firstAvailable;
304 selectedMethod = NULL;
306 use_full_trace = FALSE;
308 for (i = 0; i < stack_entries; i++) {
309 info = (MonoStackBacktraceInfo*) stack [i];
310 method = info ? info->method : NULL;
312 if (!method || method->wrapper_type != MONO_WRAPPER_NONE)
315 if (firstAvailable == -1)
318 image = method->klass->image;
319 assembly = image->assembly;
321 if ((assembly && assembly->in_gac) || ignore_frame (method))
323 selectedMethod = method;
324 native_offset = info->native_offset;
328 if (!selectedMethod) {
329 /* All the frames were from assemblies installed in GAC. Find first frame that is
330 * not in the ignore list */
331 for (i = 0; i < stack_entries; i++) {
332 info = (MonoStackBacktraceInfo*) stack [i];
333 method = info ? info->method : NULL;
335 if (!method || ignore_frame (method))
337 selectedMethod = method;
338 native_offset = info->native_offset;
343 use_full_trace = TRUE;
347 if (use_full_trace) {
348 GString *trace = g_string_new ("Full trace:\n");
349 for (i = firstAvailable; i < stack_entries; i++) {
350 info = (MonoStackBacktraceInfo*) stack [i];
351 method = info ? info->method : NULL;
352 if (!method || method->wrapper_type != MONO_WRAPPER_NONE)
355 location = mono_debug_lookup_source_location (method, info->native_offset, domain);
356 methodName = mono_method_full_name (method, TRUE);
359 append_report (&trace, LOCATION_INDENT "%s in %s:%u\n", methodName, location->source_file, location->row);
360 mono_debug_free_source_location (location);
362 append_report (&trace, LOCATION_INDENT "%s\n", methodName);
368 hint = g_string_free (trace, FALSE);
370 g_string_free (trace, TRUE);
373 location = mono_debug_lookup_source_location (selectedMethod, native_offset, domain);
374 methodName = mono_method_full_name (selectedMethod, TRUE);
377 hint = g_strdup_printf (LOCATION_INDENT "%s in %s:%u\n", methodName, location->source_file, location->row);
378 mono_debug_free_source_location (location);
380 hint = g_strdup_printf (LOCATION_INDENT "%s\n", methodName);
387 static inline gchar *build_hint (SavedString *head)
389 SavedString *current;
391 GString *hint = NULL;
395 tmp = build_hint_from_stack (current->domain, current->stack, current->stack_entries);
396 current = current->next;
400 append_report (&hint, tmp);
405 return g_string_free (hint, FALSE);
407 g_string_free (hint, FALSE);
415 static gboolean stack_walk_func (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
417 StackWalkData *swdata = (StackWalkData*)data;
418 MonoStackBacktraceInfo *info;
420 if (swdata->frame_count >= swdata->stack_size)
423 info = (MonoStackBacktraceInfo*)g_malloc (sizeof (*info));
424 info->method = method;
425 info->native_offset = native_offset;
427 swdata->stack [swdata->frame_count++] = info;
431 static inline int mono_stack_backtrace (MonoProfiler *prof, MonoDomain *domain, void **stack, int size)
437 data.stack_size = size;
438 data.frame_count = 0;
440 mono_stack_walk_no_il (stack_walk_func, (gpointer)&data);
442 return data.frame_count;
445 static void mono_portability_remember_string (MonoProfiler *prof, MonoDomain *domain, MonoString *str)
447 SavedString *head, *entry;
449 if (!str || !domain || !runtime_initialized)
452 entry = (SavedString*)g_malloc0 (sizeof (SavedString));
454 entry->domain = domain;
455 entry->stack_entries = mono_stack_backtrace (prof, domain, entry->stack, BACKTRACE_SIZE);
456 if (entry->stack_entries == 0) {
461 mono_mutex_lock (&mismatched_files_section);
462 head = (SavedString*)g_hash_table_lookup (prof->saved_strings_hash, (gpointer)str);
468 g_hash_table_insert (prof->saved_strings_hash, (gpointer)str, (gpointer)entry);
469 mono_mutex_unlock (&mismatched_files_section);
472 static MonoClass *string_class = NULL;
474 static void mono_portability_remember_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
476 if (klass != string_class)
478 mono_portability_remember_string (prof, mono_object_domain (obj), (MonoString*)obj);
481 static void mono_portability_iomap_event (MonoProfiler *prof, const char *report, const char *pathname, const char *new_pathname)
483 guint32 hash, pathnameHash;
484 MismatchedFilesStats *stats;
486 if (!runtime_initialized)
489 mono_mutex_lock (&mismatched_files_section);
490 hash = calc_strings_hash (pathname, new_pathname, &pathnameHash);
491 stats = (MismatchedFilesStats*)g_hash_table_lookup (prof->mismatched_files_hash, &hash);
495 stats = (MismatchedFilesStats*) g_malloc (sizeof (MismatchedFilesStats));
497 stats->requestedName = g_strdup (pathname);
498 stats->actualName = g_strdup (new_pathname);
499 hashptr = (guint32*)g_malloc (sizeof (guint32));
502 g_hash_table_insert (prof->mismatched_files_hash, (gpointer)hashptr, stats);
504 g_error ("Out of memory allocating integer pointer for mismatched files hash table.");
506 store_string_location (prof, (const gchar*)stats->requestedName, pathnameHash, strlen (stats->requestedName));
507 mono_mutex_unlock (&mismatched_files_section);
509 print_report ("%s - Found file path: '%s'\n", report, new_pathname);
511 mono_mutex_unlock (&mismatched_files_section);
516 static void runtime_initialized_cb (MonoProfiler *prof)
518 runtime_initialized = TRUE;
519 string_class = mono_get_string_class ();
522 static void profiler_shutdown (MonoProfiler *prof)
524 print_mismatched_stats (prof);
525 mono_mutex_destroy (&mismatched_files_section);
528 void mono_profiler_startup (const char *desc)
530 MonoProfiler *prof = g_new0 (MonoProfiler, 1);
532 mono_mutex_init (&mismatched_files_section, NULL);
533 prof->mismatched_files_hash = g_hash_table_new (mismatched_files_guint32_hash, mismatched_files_guint32_equal);
534 prof->saved_strings_hash = g_hash_table_new (NULL, NULL);
535 prof->string_locations_hash = g_hash_table_new (mismatched_files_guint32_hash, mismatched_files_guint32_equal);
537 mono_profiler_install (prof, profiler_shutdown);
538 mono_profiler_install_runtime_initialized (runtime_initialized_cb);
539 mono_profiler_install_iomap (mono_portability_iomap_event);
540 mono_profiler_install_allocation (mono_portability_remember_alloc);
542 mono_profiler_set_events (MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_IOMAP_EVENTS);