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"
31 #include "metadata/sgen-hash-table.h"
34 typedef struct _PinStatAddress PinStatAddress;
35 struct _PinStatAddress {
39 PinStatAddress *right;
43 size_t num_pins [PIN_TYPE_MAX];
48 } GlobalRemsetClassEntry;
50 static PinStatAddress *pin_stat_addresses = NULL;
51 static size_t pinned_byte_counts [PIN_TYPE_MAX];
53 static ObjectList *pinned_objects = NULL;
55 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);
56 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);
59 pin_stats_tree_free (PinStatAddress *node)
63 pin_stats_tree_free (node->left);
64 pin_stats_tree_free (node->right);
65 sgen_free_internal_dynamic (node, sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
69 sgen_pin_stats_reset (void)
72 pin_stats_tree_free (pin_stat_addresses);
73 pin_stat_addresses = NULL;
74 for (i = 0; i < PIN_TYPE_MAX; ++i)
75 pinned_byte_counts [i] = 0;
76 while (pinned_objects) {
77 ObjectList *next = pinned_objects->next;
78 sgen_free_internal_dynamic (pinned_objects, sizeof (ObjectList), INTERNAL_MEM_STATISTICS);
79 pinned_objects = next;
84 sgen_pin_stats_register_address (char *addr, int pin_type)
86 PinStatAddress **node_ptr = &pin_stat_addresses;
88 int pin_type_bit = 1 << pin_type;
92 if (addr == node->addr) {
93 node->pin_types |= pin_type_bit;
96 if (addr < node->addr)
97 node_ptr = &node->left;
99 node_ptr = &node->right;
102 node = sgen_alloc_internal_dynamic (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS, TRUE);
104 node->pin_types = pin_type_bit;
105 node->left = node->right = NULL;
111 pin_stats_count_object_from_tree (char *obj, size_t size, PinStatAddress *node, int *pin_types)
115 if (node->addr >= obj && node->addr < obj + size) {
117 for (i = 0; i < PIN_TYPE_MAX; ++i) {
118 int pin_bit = 1 << i;
119 if (!(*pin_types & pin_bit) && (node->pin_types & pin_bit)) {
120 pinned_byte_counts [i] += size;
121 *pin_types |= pin_bit;
125 if (obj < node->addr)
126 pin_stats_count_object_from_tree (obj, size, node->left, pin_types);
127 if (obj + size - 1 > node->addr)
128 pin_stats_count_object_from_tree (obj, size, node->right, pin_types);
132 lookup_class_entry (SgenHashTable *hash_table, MonoClass *class, gpointer empty_entry)
134 char *name = g_strdup_printf ("%s.%s", class->name_space, class->name);
135 gpointer entry = sgen_hash_table_lookup (hash_table, name);
140 sgen_hash_table_replace (hash_table, name, empty_entry, NULL);
141 entry = sgen_hash_table_lookup (hash_table, name);
148 register_class (MonoClass *class, int pin_types)
150 PinnedClassEntry empty_entry;
151 PinnedClassEntry *entry;
154 memset (&empty_entry, 0, sizeof (PinnedClassEntry));
155 entry = lookup_class_entry (&pinned_class_hash_table, class, &empty_entry);
157 for (i = 0; i < PIN_TYPE_MAX; ++i) {
158 if (pin_types & (1 << i))
159 ++entry->num_pins [i];
164 sgen_pin_stats_register_object (char *obj, size_t size)
169 list = sgen_alloc_internal_dynamic (sizeof (ObjectList), INTERNAL_MEM_STATISTICS, TRUE);
170 pin_stats_count_object_from_tree (obj, size, pin_stat_addresses, &pin_types);
171 list->obj = (MonoObject*)obj;
172 list->next = pinned_objects;
173 pinned_objects = list;
176 register_class (((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass, pin_types);
180 sgen_pin_stats_register_global_remset (char *obj)
182 GlobalRemsetClassEntry empty_entry;
183 GlobalRemsetClassEntry *entry;
185 memset (&empty_entry, 0, sizeof (GlobalRemsetClassEntry));
186 entry = lookup_class_entry (&global_remset_class_hash_table, ((MonoVTable*)SGEN_LOAD_VTABLE (obj))->klass, &empty_entry);
188 ++entry->num_remsets;
192 sgen_pin_stats_print_class_stats (void)
195 PinnedClassEntry *pinned_entry;
196 GlobalRemsetClassEntry *remset_entry;
198 g_print ("\n%-50s %10s %10s %10s\n", "Class", "Stack", "Static", "Other");
199 SGEN_HASH_TABLE_FOREACH (&pinned_class_hash_table, name, pinned_entry) {
201 g_print ("%-50s", name);
202 for (i = 0; i < PIN_TYPE_MAX; ++i)
203 g_print (" %10ld", pinned_entry->num_pins [i]);
205 } SGEN_HASH_TABLE_FOREACH_END;
207 g_print ("\n%-50s %10s\n", "Class", "#Remsets");
208 SGEN_HASH_TABLE_FOREACH (&global_remset_class_hash_table, name, remset_entry) {
209 g_print ("%-50s %10ld\n", name, remset_entry->num_remsets);
210 } SGEN_HASH_TABLE_FOREACH_END;
214 sgen_pin_stats_get_pinned_byte_count (int pin_type)
216 return pinned_byte_counts [pin_type];
220 sgen_pin_stats_get_object_list (void)
222 return pinned_objects;
225 #endif /* HAVE_SGEN_GC */