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