Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mono / metadata / sgen-pinning-stats.c
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 enum {
25         PIN_TYPE_STACK,
26         PIN_TYPE_STATIC_DATA,
27         PIN_TYPE_OTHER,
28         PIN_TYPE_MAX
29 };
30
31 typedef struct _PinStatAddress PinStatAddress;
32 struct _PinStatAddress {
33         char *addr;
34         int pin_types;
35         PinStatAddress *left;
36         PinStatAddress *right;
37 };
38
39 typedef struct _ObjectList ObjectList;
40 struct _ObjectList {
41         MonoObject *obj;
42         ObjectList *next;
43 };
44
45 static PinStatAddress *pin_stat_addresses = NULL;
46 static size_t pinned_byte_counts [PIN_TYPE_MAX];
47
48 static ObjectList *pinned_objects = NULL;
49
50 static void
51 pin_stats_tree_free (PinStatAddress *node)
52 {
53         if (!node)
54                 return;
55         pin_stats_tree_free (node->left);
56         pin_stats_tree_free (node->right);
57         mono_sgen_free_internal_dynamic (node, sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
58 }
59
60 static void
61 pin_stats_reset (void)
62 {
63         int i;
64         pin_stats_tree_free (pin_stat_addresses);
65         pin_stat_addresses = NULL;
66         for (i = 0; i < PIN_TYPE_MAX; ++i)
67                 pinned_byte_counts [i] = 0;
68         while (pinned_objects) {
69                 ObjectList *next = pinned_objects->next;
70                 mono_sgen_free_internal_dynamic (pinned_objects, sizeof (ObjectList), INTERNAL_MEM_STATISTICS);
71                 pinned_objects = next;
72         }
73 }
74
75 static void
76 pin_stats_register_address (char *addr, int pin_type)
77 {
78         PinStatAddress **node_ptr = &pin_stat_addresses;
79         PinStatAddress *node;
80         int pin_type_bit = 1 << pin_type;
81
82         while (*node_ptr) {
83                 node = *node_ptr;
84                 if (addr == node->addr) {
85                         node->pin_types |= pin_type_bit;
86                         return;
87                 }
88                 if (addr < node->addr)
89                         node_ptr = &node->left;
90                 else
91                         node_ptr = &node->right;
92         }
93
94         node = mono_sgen_alloc_internal_dynamic (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
95         node->addr = addr;
96         node->pin_types = pin_type_bit;
97         node->left = node->right = NULL;
98
99         *node_ptr = node;
100 }
101
102 static void
103 pin_stats_count_object_from_tree (char *obj, size_t size, PinStatAddress *node, int *pin_types)
104 {
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 (obj, size, node->left, pin_types);
119         if (obj + size - 1 > node->addr)
120                 pin_stats_count_object_from_tree (obj, size, node->right, pin_types);
121 }
122
123 static void
124 pin_stats_register_object (char *obj, size_t size)
125 {
126         int pin_types = 0;
127         ObjectList *list = mono_sgen_alloc_internal_dynamic (sizeof (ObjectList), INTERNAL_MEM_STATISTICS);
128         pin_stats_count_object_from_tree (obj, size, pin_stat_addresses, &pin_types);
129         list->obj = (MonoObject*)obj;
130         list->next = pinned_objects;
131         pinned_objects = list;
132 }