BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mono / metadata / sgen-pinning-stats.c
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  * 
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include "config.h"
27 #ifdef HAVE_SGEN_GC
28
29 #include "metadata/sgen-gc.h"
30 #include "metadata/sgen-pinning.h"
31
32
33 typedef struct _PinStatAddress PinStatAddress;
34 struct _PinStatAddress {
35         char *addr;
36         int pin_types;
37         PinStatAddress *left;
38         PinStatAddress *right;
39 };
40
41 typedef struct {
42         gulong num_pins [PIN_TYPE_MAX];
43 } PinnedClassEntry;
44
45 typedef struct {
46         gulong num_remsets;
47 } GlobalRemsetClassEntry;
48
49 static PinStatAddress *pin_stat_addresses = NULL;
50 static size_t pinned_byte_counts [PIN_TYPE_MAX];
51
52 static ObjectList *pinned_objects = NULL;
53
54 static SgenHashTable pinned_class_hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_STATISTICS, INTERNAL_MEM_STAT_PINNED_CLASS, sizeof (PinnedClassEntry), g_str_hash, g_str_equal);
55 static SgenHashTable global_remset_class_hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_STATISTICS, INTERNAL_MEM_STAT_REMSET_CLASS, sizeof (GlobalRemsetClassEntry), g_str_hash, g_str_equal);
56
57 static void
58 pin_stats_tree_free (PinStatAddress *node)
59 {
60         if (!node)
61                 return;
62         pin_stats_tree_free (node->left);
63         pin_stats_tree_free (node->right);
64         sgen_free_internal_dynamic (node, sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
65 }
66
67 void
68 sgen_pin_stats_reset (void)
69 {
70         int i;
71         pin_stats_tree_free (pin_stat_addresses);
72         pin_stat_addresses = NULL;
73         for (i = 0; i < PIN_TYPE_MAX; ++i)
74                 pinned_byte_counts [i] = 0;
75         while (pinned_objects) {
76                 ObjectList *next = pinned_objects->next;
77                 sgen_free_internal_dynamic (pinned_objects, sizeof (ObjectList), INTERNAL_MEM_STATISTICS);
78                 pinned_objects = next;
79         }
80 }
81
82 void
83 sgen_pin_stats_register_address (char *addr, int pin_type)
84 {
85         PinStatAddress **node_ptr = &pin_stat_addresses;
86         PinStatAddress *node;
87         int pin_type_bit = 1 << pin_type;
88
89         while (*node_ptr) {
90                 node = *node_ptr;
91                 if (addr == node->addr) {
92                         node->pin_types |= pin_type_bit;
93                         return;
94                 }
95                 if (addr < node->addr)
96                         node_ptr = &node->left;
97                 else
98                         node_ptr = &node->right;
99         }
100
101         node = sgen_alloc_internal_dynamic (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
102         node->addr = addr;
103         node->pin_types = pin_type_bit;
104         node->left = node->right = NULL;
105
106         *node_ptr = node;
107 }
108
109 static void
110 pin_stats_count_object_from_tree (char *obj, size_t size, PinStatAddress *node, int *pin_types)
111 {
112         if (!node)
113                 return;
114         if (node->addr >= obj && node->addr < obj + size) {
115                 int i;
116                 for (i = 0; i < PIN_TYPE_MAX; ++i) {
117                         int pin_bit = 1 << i;
118                         if (!(*pin_types & pin_bit) && (node->pin_types & pin_bit)) {
119                                 pinned_byte_counts [i] += size;
120                                 *pin_types |= pin_bit;
121                         }
122                 }
123         }
124         if (obj < node->addr)
125                 pin_stats_count_object_from_tree (obj, size, node->left, pin_types);
126         if (obj + size - 1 > node->addr)
127                 pin_stats_count_object_from_tree (obj, size, node->right, pin_types);
128 }
129
130 static gpointer
131 lookup_class_entry (SgenHashTable *hash_table, MonoClass *class, gpointer empty_entry)
132 {
133         char *name = g_strdup_printf ("%s.%s", class->name_space, class->name);
134         gpointer entry = sgen_hash_table_lookup (hash_table, name);
135
136         if (entry) {
137                 g_free (name);
138         } else {
139                 sgen_hash_table_replace (hash_table, name, empty_entry);
140                 entry = sgen_hash_table_lookup (hash_table, name);
141         }
142
143         return entry;
144 }
145
146 static void
147 register_class (MonoClass *class, int pin_types)
148 {
149         PinnedClassEntry empty_entry;
150         PinnedClassEntry *entry;
151         int i;
152
153         memset (&empty_entry, 0, sizeof (PinnedClassEntry));
154         entry = lookup_class_entry (&pinned_class_hash_table, class, &empty_entry);
155
156         for (i = 0; i < PIN_TYPE_MAX; ++i) {
157                 if (pin_types & (1 << i))
158                         ++entry->num_pins [i];
159         }
160 }
161
162 void
163 sgen_pin_stats_register_object (char *obj, size_t size)
164 {
165         int pin_types = 0;
166         ObjectList *list;
167
168         list = sgen_alloc_internal_dynamic (sizeof (ObjectList), INTERNAL_MEM_STATISTICS);
169         pin_stats_count_object_from_tree (obj, size, pin_stat_addresses, &pin_types);
170         list->obj = (MonoObject*)obj;
171         list->next = pinned_objects;
172         pinned_objects = list;
173
174         if (pin_types)
175                 register_class (((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass, pin_types);
176 }
177
178 void
179 sgen_pin_stats_register_global_remset (char *obj)
180 {
181         GlobalRemsetClassEntry empty_entry;
182         GlobalRemsetClassEntry *entry;
183
184         memset (&empty_entry, 0, sizeof (GlobalRemsetClassEntry));
185         entry = lookup_class_entry (&global_remset_class_hash_table, ((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass, &empty_entry);
186
187         ++entry->num_remsets;
188 }
189
190 void
191 sgen_pin_stats_print_class_stats (void)
192 {
193         char *name;
194         PinnedClassEntry *pinned_entry;
195         GlobalRemsetClassEntry *remset_entry;
196
197         g_print ("\n%-50s  %10s  %10s  %10s\n", "Class", "Stack", "Static", "Other");
198         SGEN_HASH_TABLE_FOREACH (&pinned_class_hash_table, name, pinned_entry) {
199                 int i;
200                 g_print ("%-50s", name);
201                 for (i = 0; i < PIN_TYPE_MAX; ++i)
202                         g_print ("  %10ld", pinned_entry->num_pins [i]);
203                 g_print ("\n");
204         } SGEN_HASH_TABLE_FOREACH_END;
205
206         g_print ("\n%-50s  %10s\n", "Class", "#Remsets");
207         SGEN_HASH_TABLE_FOREACH (&global_remset_class_hash_table, name, remset_entry) {
208                 g_print ("%-50s  %10ld\n", name, remset_entry->num_remsets);
209         } SGEN_HASH_TABLE_FOREACH_END;
210 }
211
212 size_t
213 sgen_pin_stats_get_pinned_byte_count (int pin_type)
214 {
215         return pinned_byte_counts [pin_type];
216 }
217
218 ObjectList*
219 sgen_pin_stats_get_object_list (void)
220 {
221         return pinned_objects;
222 }
223
224 #endif /* HAVE_SGEN_GC */