New test.
[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 static PinStatAddress *pin_stat_addresses = NULL;
40 static size_t pinned_byte_counts [PIN_TYPE_MAX];
41
42 static void
43 pin_stats_tree_free (PinStatAddress *node)
44 {
45         if (!node)
46                 return;
47         pin_stats_tree_free (node->left);
48         pin_stats_tree_free (node->right);
49         free_internal_mem (node, INTERNAL_MEM_STATISTICS);
50 }
51
52 static void
53 pin_stats_reset (void)
54 {
55         int i;
56         pin_stats_tree_free (pin_stat_addresses);
57         pin_stat_addresses = NULL;
58         for (i = 0; i < PIN_TYPE_MAX; ++i)
59                 pinned_byte_counts [i] = 0;
60 }
61
62 static void
63 pin_stats_register_address (char *addr, int pin_type)
64 {
65         PinStatAddress **node_ptr = &pin_stat_addresses;
66         PinStatAddress *node;
67         int pin_type_bit = 1 << pin_type;
68
69         while (*node_ptr) {
70                 node = *node_ptr;
71                 if (addr == node->addr) {
72                         node->pin_types |= pin_type_bit;
73                         return;
74                 }
75                 if (addr < node->addr)
76                         node_ptr = &node->left;
77                 else
78                         node_ptr = &node->right;
79         }
80
81         node = get_internal_mem (sizeof (PinStatAddress), INTERNAL_MEM_STATISTICS);
82         node->addr = addr;
83         node->pin_types = pin_type_bit;
84         node->left = node->right = NULL;
85
86         *node_ptr = node;
87 }
88
89 static void
90 pin_stats_count_object_from_tree (char *obj, size_t size, PinStatAddress *node, int *pin_types)
91 {
92         if (!node)
93                 return;
94         if (node->addr >= obj && node->addr < obj + size) {
95                 int i;
96                 for (i = 0; i < PIN_TYPE_MAX; ++i) {
97                         int pin_bit = 1 << i;
98                         if (!(*pin_types & pin_bit) && (node->pin_types & pin_bit)) {
99                                 pinned_byte_counts [i] += size;
100                                 *pin_types |= pin_bit;
101                         }
102                 }
103         }
104         if (obj < node->addr)
105                 pin_stats_count_object_from_tree (obj, size, node->left, pin_types);
106         if (obj + size - 1 > node->addr)
107                 pin_stats_count_object_from_tree (obj, size, node->right, pin_types);
108 }
109
110 static void
111 pin_stats_register_object (char *obj, size_t size)
112 {
113         int pin_types = 0;
114         pin_stats_count_object_from_tree (obj, size, pin_stat_addresses, &pin_types);
115 }