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)
9 * Note: this profiler is completely unsafe wrt handling managed objects,
10 * don't use and don't copy code from here.
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/utils/mono-io-portability.h>
17 #include <mono/metadata/metadata.h>
18 #include <mono/metadata/metadata-internals.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/class-internals.h>
21 #include <mono/metadata/object-internals.h>
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/mono-debug.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/threads.h>
26 #include <mono/metadata/profiler.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/utils/mono-os-mutex.h>
30 #define LOCATION_INDENT " "
31 #define BACKTRACE_SIZE 64
33 typedef struct _MonoStackBacktraceInfo
37 } MonoStackBacktraceInfo;
44 } MismatchedFilesStats;
46 typedef struct _SavedString
50 void *stack [BACKTRACE_SIZE];
52 struct _SavedString *next;
55 typedef struct _SavedStringFindInfo
59 } SavedStringFindInfo;
61 typedef struct _StringLocation
64 struct _StringLocation *next;
69 GHashTable *mismatched_files_hash;
70 GHashTable *saved_strings_hash;
71 GHashTable *string_locations_hash;
72 gboolean may_have_locations;
75 typedef struct _StackWalkData
83 static mono_mutex_t mismatched_files_section;
84 static gboolean runtime_initialized = FALSE;
86 static inline void append_report (GString **report, const gchar *format, ...);
87 static inline void print_report (const gchar *format, ...);
88 static inline guint32 do_calc_string_hash (guint32 hash, const gchar *str);
89 static inline guint32 calc_strings_hash (const gchar *str1, const gchar *str2, guint32 *str1hash);
90 static void print_mismatched_stats (MonoProfiler *prof);
91 static inline gchar *build_hint (SavedString *head);
92 static inline gchar *build_hint_from_stack (MonoDomain *domain, void **stack, gint stack_entries);
93 static inline void store_string_location (MonoProfiler *prof, const gchar *string, guint32 hash, size_t len);
94 static void mono_portability_remember_string (MonoProfiler *prof, MonoDomain *domain, MonoString *str);
95 void mono_profiler_startup (const char *desc);
97 static void mismatched_stats_foreach_func (gpointer key, gpointer value, gpointer user_data)
99 MismatchedFilesStats *stats = (MismatchedFilesStats*)value;
100 StringLocation *location;
101 MonoProfiler *prof = (MonoProfiler*)user_data;
103 gboolean bannerShown = FALSE;
105 hash = do_calc_string_hash (0, stats->requestedName);
110 stats->count, stats->requestedName, stats->actualName);
112 if (!prof->may_have_locations) {
113 fprintf (stdout, "\n");
117 location = (StringLocation *)g_hash_table_lookup (prof->string_locations_hash, &hash);
119 if (location->hint && strlen (location->hint) > 0) {
121 fprintf (stdout, "Locations:\n");
124 fprintf (stdout, "%s", location->hint);
126 location = location->next;
128 fprintf (stdout, LOCATION_INDENT "--\n");
131 fprintf (stdout, "\n");
134 static void print_mismatched_stats (MonoProfiler *prof)
136 if (!prof->mismatched_files_hash || g_hash_table_size (prof->mismatched_files_hash) == 0)
139 prof->may_have_locations = g_hash_table_size (prof->string_locations_hash) > 0;
141 fprintf (stdout, "\n-=-=-=-=-=-=-= MONO_IOMAP Stats -=-=-=-=-=-=-=\n");
142 g_hash_table_foreach (prof->mismatched_files_hash, mismatched_stats_foreach_func, (gpointer)prof);
146 static guint mismatched_files_guint32_hash (gconstpointer key)
151 return *((guint32*)key);
154 static gboolean mismatched_files_guint32_equal (gconstpointer key1, gconstpointer key2)
159 return (gboolean)(*((guint32*)key1) == *((guint32*)key2));
162 static inline guint32 do_calc_string_hash (guint32 hash, const gchar *str)
165 gchar *cc = (gchar*)str;
166 gchar *end = (gchar*)(str + strlen (str) - 1);
168 for (; cc < end; cc += 2) {
169 ret = (ret << 5) - ret + *cc;
170 ret = (ret << 5) - ret + cc [1];
174 ret = (ret << 5) - ret + *cc;
179 static inline guint32 calc_strings_hash (const gchar *str1, const gchar *str2, guint32 *str1hash)
181 guint32 hash = do_calc_string_hash (0, str1);
184 return do_calc_string_hash (hash, str2);
187 static inline void print_report (const gchar *format, ...)
196 fprintf (stdout, "-=-=-=-=-=-=- MONO_IOMAP REPORT -=-=-=-=-=-=-\n");
197 va_start (ap, format);
198 vfprintf (stdout, format, ap);
199 fprintf (stdout, "\n");
201 klass = mono_class_load_from_name (mono_get_corlib (), "System", "Environment");
202 mono_class_init (klass);
203 prop = mono_class_get_property_from_name (klass, "StackTrace");
204 str = (MonoString*)mono_property_get_value_checked (prop, NULL, NULL, &error);
205 mono_error_assert_ok (&error);
206 stack_trace = mono_string_to_utf8 (str);
208 fprintf (stdout, "-= Stack Trace =-\n%s\n\n", stack_trace);
209 g_free (stack_trace);
213 static inline void append_report (GString **report, const gchar *format, ...)
215 #if defined (_EGLIB_MAJOR) || GLIB_CHECK_VERSION(2,14,0)
218 *report = g_string_new ("");
220 va_start (ap, format);
221 g_string_append_vprintf (*report, format, ap);
224 g_assert_not_reached ();
228 static gboolean saved_strings_find_func (gpointer key, gpointer value, gpointer user_data)
230 SavedStringFindInfo *info = (SavedStringFindInfo*)user_data;
231 SavedString *saved = (SavedString*)value;
235 if (!info || !saved || mono_string_length (saved->string) != info->len)
238 utf_str = mono_string_to_utf8 (saved->string);
239 hash = do_calc_string_hash (0, utf_str);
242 if (hash != info->hash)
248 static inline void store_string_location (MonoProfiler *prof, const gchar *string, guint32 hash, size_t len)
250 StringLocation *location = (StringLocation *)g_hash_table_lookup (prof->string_locations_hash, &hash);
252 SavedStringFindInfo info;
261 /* Expensive but unavoidable... */
262 saved = (SavedString*)g_hash_table_find (prof->saved_strings_hash, saved_strings_find_func, &info);
263 hashptr = (guint32*)g_malloc (sizeof (guint32));
265 location = (StringLocation*)g_malloc0 (sizeof (location));
267 g_hash_table_insert (prof->string_locations_hash, hashptr, location);
271 g_hash_table_remove (prof->saved_strings_hash, saved->string);
272 location->hint = build_hint (saved);
275 static gboolean ignore_frame (MonoMethod *method)
277 MonoClass *klass = method->klass;
279 if (method->wrapper_type != MONO_WRAPPER_NONE)
282 /* Now ignore the assemblies we know shouldn't contain mixed-case names (only the most frequent cases) */
284 if (strcmp (klass->image->assembly_name, "mscorlib") == 0)
286 else if (strcmp (klass->image->assembly_name, "System") == 0)
288 else if (strncmp (klass->image->assembly_name, "Mono.", 5) == 0)
290 else if (strncmp (klass->image->assembly_name, "System.", 7) == 0)
292 else if (strcmp (klass->image->assembly_name, "PEAPI") == 0)
299 static inline gchar *build_hint_from_stack (MonoDomain *domain, void **stack, gint stack_entries)
302 MonoMethod *method, *selectedMethod;
303 MonoAssembly *assembly;
305 MonoDebugSourceLocation *location;
306 MonoStackBacktraceInfo *info;
307 gboolean use_full_trace;
309 gint i, native_offset, firstAvailable;
311 selectedMethod = NULL;
313 use_full_trace = FALSE;
315 for (i = 0; i < stack_entries; i++) {
316 info = (MonoStackBacktraceInfo*) stack [i];
317 method = info ? info->method : NULL;
319 if (!method || method->wrapper_type != MONO_WRAPPER_NONE)
322 if (firstAvailable == -1)
325 image = method->klass->image;
326 assembly = image->assembly;
328 if ((assembly && assembly->in_gac) || ignore_frame (method))
330 selectedMethod = method;
331 native_offset = info->native_offset;
335 if (!selectedMethod) {
336 /* All the frames were from assemblies installed in GAC. Find first frame that is
337 * not in the ignore list */
338 for (i = 0; i < stack_entries; i++) {
339 info = (MonoStackBacktraceInfo*) stack [i];
340 method = info ? info->method : NULL;
342 if (!method || ignore_frame (method))
344 selectedMethod = method;
345 native_offset = info->native_offset;
350 use_full_trace = TRUE;
354 if (use_full_trace) {
355 GString *trace = g_string_new ("Full trace:\n");
356 for (i = firstAvailable; i < stack_entries; i++) {
357 info = (MonoStackBacktraceInfo*) stack [i];
358 method = info ? info->method : NULL;
359 if (!method || method->wrapper_type != MONO_WRAPPER_NONE)
362 location = mono_debug_lookup_source_location (method, info->native_offset, domain);
363 methodName = mono_method_full_name (method, TRUE);
366 append_report (&trace, LOCATION_INDENT "%s in %s:%u\n", methodName, location->source_file, location->row);
367 mono_debug_free_source_location (location);
369 append_report (&trace, LOCATION_INDENT "%s\n", methodName);
375 hint = g_string_free (trace, FALSE);
377 g_string_free (trace, TRUE);
380 location = mono_debug_lookup_source_location (selectedMethod, native_offset, domain);
381 methodName = mono_method_full_name (selectedMethod, TRUE);
384 hint = g_strdup_printf (LOCATION_INDENT "%s in %s:%u\n", methodName, location->source_file, location->row);
385 mono_debug_free_source_location (location);
387 hint = g_strdup_printf (LOCATION_INDENT "%s\n", methodName);
394 static inline gchar *build_hint (SavedString *head)
396 SavedString *current;
398 GString *hint = NULL;
402 tmp = build_hint_from_stack (current->domain, current->stack, current->stack_entries);
403 current = current->next;
407 append_report (&hint, tmp);
412 return g_string_free (hint, FALSE);
414 g_string_free (hint, FALSE);
422 static gboolean stack_walk_func (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
424 StackWalkData *swdata = (StackWalkData*)data;
425 MonoStackBacktraceInfo *info;
427 if (swdata->frame_count >= swdata->stack_size)
430 info = (MonoStackBacktraceInfo*)g_malloc (sizeof (*info));
431 info->method = method;
432 info->native_offset = native_offset;
434 swdata->stack [swdata->frame_count++] = info;
438 static inline int mono_stack_backtrace (MonoProfiler *prof, MonoDomain *domain, void **stack, int size)
444 data.stack_size = size;
445 data.frame_count = 0;
447 mono_stack_walk_no_il (stack_walk_func, (gpointer)&data);
449 return data.frame_count;
452 static void mono_portability_remember_string (MonoProfiler *prof, MonoDomain *domain, MonoString *str)
454 SavedString *head, *entry;
456 if (!str || !domain || !runtime_initialized)
459 entry = (SavedString*)g_malloc0 (sizeof (SavedString));
461 entry->domain = domain;
462 entry->stack_entries = mono_stack_backtrace (prof, domain, entry->stack, BACKTRACE_SIZE);
463 if (entry->stack_entries == 0) {
468 mono_os_mutex_lock (&mismatched_files_section);
469 head = (SavedString*)g_hash_table_lookup (prof->saved_strings_hash, (gpointer)str);
475 g_hash_table_insert (prof->saved_strings_hash, (gpointer)str, (gpointer)entry);
476 mono_os_mutex_unlock (&mismatched_files_section);
479 static MonoClass *string_class = NULL;
481 static void mono_portability_remember_alloc (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
483 if (klass != string_class)
485 mono_portability_remember_string (prof, mono_object_get_domain (obj), (MonoString*)obj);
488 static void mono_portability_iomap_event (MonoProfiler *prof, const char *report, const char *pathname, const char *new_pathname)
490 guint32 hash, pathnameHash;
491 MismatchedFilesStats *stats;
493 if (!runtime_initialized)
496 mono_os_mutex_lock (&mismatched_files_section);
497 hash = calc_strings_hash (pathname, new_pathname, &pathnameHash);
498 stats = (MismatchedFilesStats*)g_hash_table_lookup (prof->mismatched_files_hash, &hash);
502 stats = (MismatchedFilesStats*) g_malloc (sizeof (MismatchedFilesStats));
504 stats->requestedName = g_strdup (pathname);
505 stats->actualName = g_strdup (new_pathname);
506 hashptr = (guint32*)g_malloc (sizeof (guint32));
509 g_hash_table_insert (prof->mismatched_files_hash, (gpointer)hashptr, stats);
511 g_error ("Out of memory allocating integer pointer for mismatched files hash table.");
513 store_string_location (prof, (const gchar*)stats->requestedName, pathnameHash, strlen (stats->requestedName));
514 mono_os_mutex_unlock (&mismatched_files_section);
516 print_report ("%s - Found file path: '%s'\n", report, new_pathname);
518 mono_os_mutex_unlock (&mismatched_files_section);
523 static void runtime_initialized_cb (MonoProfiler *prof)
525 runtime_initialized = TRUE;
526 string_class = mono_get_string_class ();
529 static void profiler_shutdown (MonoProfiler *prof)
531 print_mismatched_stats (prof);
532 mono_os_mutex_destroy (&mismatched_files_section);
535 void mono_profiler_startup (const char *desc)
537 MonoProfiler *prof = g_new0 (MonoProfiler, 1);
539 mono_os_mutex_init (&mismatched_files_section);
540 prof->mismatched_files_hash = g_hash_table_new (mismatched_files_guint32_hash, mismatched_files_guint32_equal);
541 prof->saved_strings_hash = g_hash_table_new (NULL, NULL);
542 prof->string_locations_hash = g_hash_table_new (mismatched_files_guint32_hash, mismatched_files_guint32_equal);
544 mono_profiler_install (prof, profiler_shutdown);
545 mono_profiler_install_runtime_initialized (runtime_initialized_cb);
546 mono_profiler_install_iomap (mono_portability_iomap_event);
547 mono_profiler_install_allocation (mono_portability_remember_alloc);
549 mono_profiler_set_events ((MonoProfileFlags)(MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_IOMAP_EVENTS));