DiffLoader didn't handle encoded name and caused mismatch between .NET.
[mono-project/dkf.git] / mono / profiler / mono-profiler-aot.c
blob080ccfa1b2b738af93f7e89cc25aac825021b7a1
1 /*
2 * mono-profiler-aot.c: Ahead of Time Compiler Profiler for Mono.
5 * Copyright 2008-2009 Novell, Inc (http://www.novell.com)
7 * This profiler collects profiling information usable by the Mono AOT compiler
8 * to generate better code. It saves the information into files under ~/.mono.
9 * The AOT compiler can load these files during compilation.
10 * Currently, only the order in which methods were compiled is saved,
11 * allowing more efficient function ordering in the AOT files.
14 #include <config.h>
15 #include <mono/metadata/profiler.h>
16 #include <mono/metadata/tokentype.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/assembly.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <sys/stat.h>
26 struct _MonoProfiler {
27 GHashTable *images;
30 typedef struct {
31 GList *methods;
32 } PerImageData;
34 typedef struct ForeachData {
35 MonoProfiler *prof;
36 FILE *outfile;
37 MonoImage *image;
38 MonoMethod *method;
39 } ForeachData;
41 static void
42 foreach_method (gpointer data, gpointer user_data)
44 ForeachData *udata = (ForeachData*)user_data;
45 MonoMethod *method = (MonoMethod*)data;
46 char *name;
48 if (!mono_method_get_token (method) || mono_class_get_image (mono_method_get_class (method)) != udata->image)
49 return;
51 name = mono_method_full_name (method, TRUE);
52 fprintf (udata->outfile, "%s\n", name);
53 g_free (name);
56 static void
57 output_image (gpointer key, gpointer value, gpointer user_data)
59 MonoImage *image = (MonoImage*)key;
60 PerImageData *image_data = (PerImageData*)value;
61 MonoProfiler *prof = (MonoProfiler*)user_data;
62 char *tmp, *outfile_name;
63 FILE *outfile;
64 int i, err;
65 ForeachData data;
67 tmp = g_strdup_printf ("%s/.mono/aot-profile-data", g_get_home_dir ());
69 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
70 #ifdef HOST_WIN32
71 err = mkdir (tmp);
72 #else
73 err = mkdir (tmp, 0777);
74 #endif
75 if (err) {
76 fprintf (stderr, "mono-profiler-aot: Unable to create output directory '%s': %s\n", tmp, g_strerror (errno));
77 exit (1);
81 i = 0;
82 while (TRUE) {
83 outfile_name = g_strdup_printf ("%s/%s-%d", tmp, mono_image_get_name (image), i);
85 if (!g_file_test (outfile_name, G_FILE_TEST_IS_REGULAR))
86 break;
88 i ++;
91 printf ("Creating output file: %s\n", outfile_name);
93 outfile = fopen (outfile_name, "w+");
94 g_assert (outfile);
96 fprintf (outfile, "#VER:%d\n", 2);
98 data.prof = prof;
99 data.outfile = outfile;
100 data.image = image;
102 g_list_foreach (image_data->methods, foreach_method, &data);
105 /* called at the end of the program */
106 static void
107 prof_shutdown (MonoProfiler *prof)
109 g_hash_table_foreach (prof->images, output_image, prof);
112 static void
113 prof_jit_enter (MonoProfiler *prof, MonoMethod *method)
117 static void
118 prof_jit_leave (MonoProfiler *prof, MonoMethod *method, int result)
120 MonoImage *image = mono_class_get_image (mono_method_get_class (method));
121 PerImageData *data;
123 data = g_hash_table_lookup (prof->images, image);
124 if (!data) {
125 data = g_new0 (PerImageData, 1);
126 g_hash_table_insert (prof->images, image, data);
129 data->methods = g_list_append (data->methods, method);
132 void
133 mono_profiler_startup (const char *desc);
135 /* the entry point */
136 void
137 mono_profiler_startup (const char *desc)
139 MonoProfiler *prof;
141 prof = g_new0 (MonoProfiler, 1);
142 prof->images = g_hash_table_new (NULL, NULL);
144 mono_profiler_install (prof, prof_shutdown);
146 mono_profiler_install_jit_compile (prof_jit_enter, prof_jit_leave);
148 mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);