First set of licensing changes
[mono.git] / mono / sgen / 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  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9 #include "config.h"
10 #ifdef HAVE_SGEN_GC
11
12 #include <string.h>
13
14 #include "mono/sgen/sgen-gc.h"
15 #include "mono/sgen/sgen-pinning.h"
16 #include "mono/sgen/sgen-hash-table.h"
17 #include "mono/sgen/sgen-client.h"
18
19 typedef struct _PinStatAddress PinStatAddress;
20 struct _PinStatAddress {
21         char *addr;
22         int pin_types;
23         PinStatAddress *left;
24         PinStatAddress *right;
25 };
26
27 typedef struct {
28         size_t num_pins [PIN_TYPE_MAX];
29 } PinnedClassEntry;
30
31 typedef struct {
32         gulong num_remsets;
33 } GlobalRemsetClassEntry;
34
35 static gboolean do_pin_stats = FALSE;
36
37 static PinStatAddress *pin_stat_addresses = NULL;
38 static size_t pinned_byte_counts [PIN_TYPE_MAX];
39
40 static SgenPointerQueue pinned_objects = SGEN_POINTER_QUEUE_INIT (INTERNAL_MEM_STATISTICS);
41
42 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);
43 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);
44
45 void
46 sgen_pin_stats_enable (void)
47 {
48         do_pin_stats = TRUE;
49 }
50
51 static void
52 pin_stats_tree_free (PinStatAddress *node)
53 {
54         if (!node)
55                 return;
56         pin_stats_tree_free (node->left);
57         pin_stats_tree_free (node->right);
58         sgen_free_internal_dynamic (node, sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
59 }
60
61 void
62 sgen_pin_stats_reset (void)
63 {
64         int i;
65         pin_stats_tree_free (pin_stat_addresses);
66         pin_stat_addresses = NULL;
67         for (i = 0; i < PIN_TYPE_MAX; ++i)
68                 pinned_byte_counts [i] = 0;
69         sgen_pointer_queue_clear (&pinned_objects);
70         sgen_hash_table_clean (&pinned_class_hash_table);
71         sgen_hash_table_clean (&global_remset_class_hash_table);
72 }
73
74 void
75 sgen_pin_stats_register_address (char *addr, int pin_type)
76 {
77         PinStatAddress **node_ptr = &pin_stat_addresses;
78         PinStatAddress *node;
79         int pin_type_bit = 1 << pin_type;
80
81         while (*node_ptr) {
82                 node = *node_ptr;
83                 if (addr == node->addr) {
84                         node->pin_types |= pin_type_bit;
85                         return;
86                 }
87                 if (addr < node->addr)
88                         node_ptr = &node->left;
89                 else
90                         node_ptr = &node->right;
91         }
92
93         node = (PinStatAddress *)sgen_alloc_internal_dynamic (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS, TRUE);
94         node->addr = addr;
95         node->pin_types = pin_type_bit;
96         node->left = node->right = NULL;
97
98         *node_ptr = node;
99 }
100
101 static void
102 pin_stats_count_object_from_tree (GCObject *object, size_t size, PinStatAddress *node, int *pin_types)
103 {
104         char *obj = (char*)object;
105         if (!node)
106                 return;
107         if (node->addr >= obj && node->addr < obj + size) {
108                 int i;
109                 for (i = 0; i < PIN_TYPE_MAX; ++i) {
110                         int pin_bit = 1 << i;
111                         if (!(*pin_types & pin_bit) && (node->pin_types & pin_bit)) {
112                                 pinned_byte_counts [i] += size;
113                                 *pin_types |= pin_bit;
114                         }
115                 }
116         }
117         if (obj < node->addr)
118                 pin_stats_count_object_from_tree (object, size, node->left, pin_types);
119         if (obj + size - 1 > node->addr)
120                 pin_stats_count_object_from_tree (object, size, node->right, pin_types);
121 }
122
123 static gpointer
124 lookup_vtable_entry (SgenHashTable *hash_table, GCVTable vtable, gpointer empty_entry)
125 {
126         char *name = g_strdup_printf ("%s.%s", sgen_client_vtable_get_namespace (vtable), sgen_client_vtable_get_name (vtable));
127         gpointer entry = sgen_hash_table_lookup (hash_table, name);
128
129         if (entry) {
130                 g_free (name);
131         } else {
132                 sgen_hash_table_replace (hash_table, name, empty_entry, NULL);
133                 entry = sgen_hash_table_lookup (hash_table, name);
134         }
135
136         return entry;
137 }
138
139 static void
140 register_vtable (GCVTable vtable, int pin_types)
141 {
142         PinnedClassEntry empty_entry;
143         PinnedClassEntry *entry;
144         int i;
145
146         memset (&empty_entry, 0, sizeof (PinnedClassEntry));
147         entry = (PinnedClassEntry *)lookup_vtable_entry (&pinned_class_hash_table, vtable, &empty_entry);
148
149         for (i = 0; i < PIN_TYPE_MAX; ++i) {
150                 if (pin_types & (1 << i))
151                         ++entry->num_pins [i];
152         }
153 }
154
155 void
156 sgen_pin_stats_register_object (GCObject *obj, size_t size)
157 {
158         int pin_types = 0;
159
160         if (!do_pin_stats)
161                 return;
162
163         pin_stats_count_object_from_tree (obj, size, pin_stat_addresses, &pin_types);
164         sgen_pointer_queue_add (&pinned_objects, obj);
165
166         if (pin_types)
167                 register_vtable (SGEN_LOAD_VTABLE (obj), pin_types);
168 }
169
170 void
171 sgen_pin_stats_register_global_remset (GCObject *obj)
172 {
173         GlobalRemsetClassEntry empty_entry;
174         GlobalRemsetClassEntry *entry;
175
176         if (!do_pin_stats)
177                 return;
178
179         memset (&empty_entry, 0, sizeof (GlobalRemsetClassEntry));
180         entry = (GlobalRemsetClassEntry *)lookup_vtable_entry (&global_remset_class_hash_table, SGEN_LOAD_VTABLE (obj), &empty_entry);
181
182         ++entry->num_remsets;
183 }
184
185 void
186 sgen_pin_stats_print_class_stats (void)
187 {
188         char *name;
189         PinnedClassEntry *pinned_entry;
190         GlobalRemsetClassEntry *remset_entry;
191
192         if (!do_pin_stats)
193                 return;
194
195         mono_gc_printf (gc_debug_file, "\n%-50s  %10s  %10s  %10s\n", "Class", "Stack", "Static", "Other");
196         SGEN_HASH_TABLE_FOREACH (&pinned_class_hash_table, char *, name, PinnedClassEntry *, pinned_entry) {
197                 int i;
198                 mono_gc_printf (gc_debug_file, "%-50s", name);
199                 for (i = 0; i < PIN_TYPE_MAX; ++i)
200                         mono_gc_printf (gc_debug_file, "  %10ld", pinned_entry->num_pins [i]);
201                 mono_gc_printf (gc_debug_file, "\n");
202         } SGEN_HASH_TABLE_FOREACH_END;
203
204         mono_gc_printf (gc_debug_file, "\n%-50s  %10s\n", "Class", "#Remsets");
205         SGEN_HASH_TABLE_FOREACH (&global_remset_class_hash_table, char *, name, GlobalRemsetClassEntry *, remset_entry) {
206                 mono_gc_printf (gc_debug_file, "%-50s  %10ld\n", name, remset_entry->num_remsets);
207         } SGEN_HASH_TABLE_FOREACH_END;
208
209         mono_gc_printf (gc_debug_file, "\nTotal bytes pinned from stack: %ld  static: %ld  other: %ld\n",
210                         pinned_byte_counts [PIN_TYPE_STACK],
211                         pinned_byte_counts [PIN_TYPE_STATIC_DATA],
212                         pinned_byte_counts [PIN_TYPE_OTHER]);
213 }
214
215 size_t
216 sgen_pin_stats_get_pinned_byte_count (int pin_type)
217 {
218         return pinned_byte_counts [pin_type];
219 }
220
221 SgenPointerQueue*
222 sgen_pin_stats_get_object_list (void)
223 {
224         return &pinned_objects;
225 }
226
227 #endif /* HAVE_SGEN_GC */