2010-04-01 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mono / profiler / mono-profiler-aot.c
1 /*
2  * mono-profiler-aot.c: Ahead of Time Compiler Profiler for Mono.
3  *
4  *
5  * Copyright 2008-2009 Novell, Inc (http://www.novell.com)
6  *
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.
12  */
13
14 #include <mono/metadata/profiler.h>
15 #include <mono/metadata/tokentype.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/assembly.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <sys/stat.h>
24
25 struct _MonoProfiler {
26         GHashTable *images;
27 };
28
29 typedef struct {
30         GList *methods;
31 } PerImageData;
32
33 typedef struct ForeachData {
34         MonoProfiler *prof;
35         FILE *outfile;
36         MonoImage *image;
37         MonoMethod *method;
38 } ForeachData;
39
40 static void
41 foreach_method (gpointer data, gpointer user_data)
42 {
43         ForeachData *udata = (ForeachData*)user_data;
44         MonoMethod *method = (MonoMethod*)data;
45         char *name;
46
47         if (!mono_method_get_token (method) || mono_class_get_image (mono_method_get_class (method)) != udata->image)
48                 return;
49
50         name = mono_method_full_name (method, TRUE);
51         fprintf (udata->outfile, "%s\n", name);
52         g_free (name);
53 }
54
55 static void
56 output_image (gpointer key, gpointer value, gpointer user_data)
57 {
58         MonoImage *image = (MonoImage*)key;
59         PerImageData *image_data = (PerImageData*)value;
60         MonoProfiler *prof = (MonoProfiler*)user_data;
61         char *tmp, *outfile_name;
62         FILE *outfile;
63         int i, err;
64         ForeachData data;
65
66         tmp = g_strdup_printf ("%s/.mono/aot-profile-data", g_get_home_dir ());
67
68         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
69 #ifdef HOST_WIN32
70                 err = mkdir (tmp);
71 #else
72                 err = mkdir (tmp, 0777);
73 #endif
74                 if (err) {
75                         fprintf (stderr, "mono-profiler-aot: Unable to create output directory '%s': %s\n", tmp, g_strerror (errno));
76                         exit (1);
77                 }
78         }
79
80         i = 0;
81         while (TRUE) {
82                 outfile_name = g_strdup_printf ("%s/%s-%d", tmp, mono_image_get_name (image), i);
83
84                 if (!g_file_test (outfile_name, G_FILE_TEST_IS_REGULAR))
85                         break;
86
87                 i ++;
88         }
89
90         printf ("Creating output file: %s\n", outfile_name);
91
92         outfile = fopen (outfile_name, "w+");
93         g_assert (outfile);
94
95         fprintf (outfile, "#VER:%d\n", 2);
96
97         data.prof = prof;
98         data.outfile = outfile;
99         data.image = image;
100
101         g_list_foreach (image_data->methods, foreach_method, &data);
102 }
103
104 /* called at the end of the program */
105 static void
106 prof_shutdown (MonoProfiler *prof)
107 {
108         g_hash_table_foreach (prof->images, output_image, prof);
109 }
110
111 static void
112 prof_jit_enter (MonoProfiler *prof, MonoMethod *method)
113 {
114 }
115
116 static void
117 prof_jit_leave (MonoProfiler *prof, MonoMethod *method, int result)
118 {
119         MonoImage *image = mono_class_get_image (mono_method_get_class (method));
120         PerImageData *data;
121
122         data = g_hash_table_lookup (prof->images, image);
123         if (!data) {
124                 data = g_new0 (PerImageData, 1);
125                 g_hash_table_insert (prof->images, image, data);
126         }
127
128         data->methods = g_list_append (data->methods, method);
129 }
130
131 void
132 mono_profiler_startup (const char *desc);
133
134 /* the entry point */
135 void
136 mono_profiler_startup (const char *desc)
137 {
138         MonoProfiler *prof;
139
140         prof = g_new0 (MonoProfiler, 1);
141         prof->images = g_hash_table_new (NULL, NULL);
142
143         mono_profiler_install (prof, prof_shutdown);
144         
145         mono_profiler_install_jit_compile (prof_jit_enter, prof_jit_leave);
146
147         mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);
148 }
149
150