2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
4 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
29 #include "metadata/sgen-gc.h"
30 #include "metadata/sgen-pinning.h"
33 typedef struct _PinStatAddress PinStatAddress;
34 struct _PinStatAddress {
38 PinStatAddress *right;
42 gulong num_pins [PIN_TYPE_MAX];
47 } GlobalRemsetClassEntry;
49 static PinStatAddress *pin_stat_addresses = NULL;
50 static size_t pinned_byte_counts [PIN_TYPE_MAX];
52 static ObjectList *pinned_objects = NULL;
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);
58 pin_stats_tree_free (PinStatAddress *node)
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);
68 sgen_pin_stats_reset (void)
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;
83 sgen_pin_stats_register_address (char *addr, int pin_type)
85 PinStatAddress **node_ptr = &pin_stat_addresses;
87 int pin_type_bit = 1 << pin_type;
91 if (addr == node->addr) {
92 node->pin_types |= pin_type_bit;
95 if (addr < node->addr)
96 node_ptr = &node->left;
98 node_ptr = &node->right;
101 node = sgen_alloc_internal_dynamic (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
103 node->pin_types = pin_type_bit;
104 node->left = node->right = NULL;
110 pin_stats_count_object_from_tree (char *obj, size_t size, PinStatAddress *node, int *pin_types)
114 if (node->addr >= obj && node->addr < obj + size) {
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;
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);
131 lookup_class_entry (SgenHashTable *hash_table, MonoClass *class, gpointer empty_entry)
133 char *name = g_strdup_printf ("%s.%s", class->name_space, class->name);
134 gpointer entry = sgen_hash_table_lookup (hash_table, name);
139 sgen_hash_table_replace (hash_table, name, empty_entry);
140 entry = sgen_hash_table_lookup (hash_table, name);
147 register_class (MonoClass *class, int pin_types)
149 PinnedClassEntry empty_entry;
150 PinnedClassEntry *entry;
153 memset (&empty_entry, 0, sizeof (PinnedClassEntry));
154 entry = lookup_class_entry (&pinned_class_hash_table, class, &empty_entry);
156 for (i = 0; i < PIN_TYPE_MAX; ++i) {
157 if (pin_types & (1 << i))
158 ++entry->num_pins [i];
163 sgen_pin_stats_register_object (char *obj, size_t size)
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;
175 register_class (((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass, pin_types);
179 sgen_pin_stats_register_global_remset (char *obj)
181 GlobalRemsetClassEntry empty_entry;
182 GlobalRemsetClassEntry *entry;
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);
187 ++entry->num_remsets;
191 sgen_pin_stats_print_class_stats (void)
194 PinnedClassEntry *pinned_entry;
195 GlobalRemsetClassEntry *remset_entry;
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) {
200 g_print ("%-50s", name);
201 for (i = 0; i < PIN_TYPE_MAX; ++i)
202 g_print (" %10ld", pinned_entry->num_pins [i]);
204 } SGEN_HASH_TABLE_FOREACH_END;
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;
213 sgen_pin_stats_get_pinned_byte_count (int pin_type)
215 return pinned_byte_counts [pin_type];
219 sgen_pin_stats_get_object_list (void)
221 return pinned_objects;
224 #endif /* HAVE_SGEN_GC */