diff options
Diffstat (limited to 'linden/indra/lscript/lscript_execute_mono')
-rw-r--r-- | linden/indra/lscript/lscript_execute_mono/assembly.cpp | 74 | ||||
-rw-r--r-- | linden/indra/lscript/lscript_execute_mono/size.cpp | 182 |
2 files changed, 256 insertions, 0 deletions
diff --git a/linden/indra/lscript/lscript_execute_mono/assembly.cpp b/linden/indra/lscript/lscript_execute_mono/assembly.cpp new file mode 100644 index 0000000..dd8b284 --- /dev/null +++ b/linden/indra/lscript/lscript_execute_mono/assembly.cpp | |||
@@ -0,0 +1,74 @@ | |||
1 | /** | ||
2 | * @file llassembly.cpp | ||
3 | * @brief keeps track of user scripts, ensures that only one copy exists for each | ||
4 | * | ||
5 | * $LicenseInfo:firstyear=2007&license=internal$ | ||
6 | * | ||
7 | * Copyright (c) 2007-2008, Linden Research, Inc. | ||
8 | * | ||
9 | * The following source code is PROPRIETARY AND CONFIDENTIAL. Use of | ||
10 | * this source code is governed by the Linden Lab Source Code Disclosure | ||
11 | * Agreement ("Agreement") previously entered between you and Linden | ||
12 | * Lab. By accessing, using, copying, modifying or distributing this | ||
13 | * software, you acknowledge that you have been informed of your | ||
14 | * obligations under the Agreement and agree to abide by those obligations. | ||
15 | * | ||
16 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
17 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
18 | * COMPLETENESS OR PERFORMANCE. | ||
19 | * $/LicenseInfo$ | ||
20 | */ | ||
21 | |||
22 | |||
23 | #include "llassembly.h" | ||
24 | #include <map> | ||
25 | |||
26 | |||
27 | static std::map<std::string, LLAssembly*> sAssemblyMap; | ||
28 | |||
29 | boost::intrusive_ptr<LLAssembly> LLAssembly::create(const std::string& name, | ||
30 | const U8* buffer, U32 size) | ||
31 | { | ||
32 | std::map<std::string, LLAssembly*>::iterator it = sAssemblyMap.find(name); | ||
33 | if (it != sAssemblyMap.end()) | ||
34 | { | ||
35 | return it->second; | ||
36 | } | ||
37 | else | ||
38 | { | ||
39 | LLAssembly* a = new LLAssembly(name, buffer, size); | ||
40 | sAssemblyMap[name] = a; | ||
41 | return a; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | |||
46 | LLAssembly::LLAssembly(const std::string &name, const U8* buffer, U32 size) : | ||
47 | mName(name), mRefCount(0), mBuffer(buffer, buffer+size) | ||
48 | { | ||
49 | } | ||
50 | |||
51 | LLAssembly::~LLAssembly() | ||
52 | { | ||
53 | std::map<std::string, LLAssembly*>::iterator it = sAssemblyMap.find(mName); | ||
54 | if (it != sAssemblyMap.end()) | ||
55 | { | ||
56 | sAssemblyMap.erase(it); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | |||
61 | void intrusive_ptr_add_ref(LLAssembly* p) | ||
62 | { | ||
63 | ++(p->mRefCount); | ||
64 | } | ||
65 | |||
66 | void intrusive_ptr_release(LLAssembly* p) | ||
67 | { | ||
68 | if (0 == --(p->mRefCount)) | ||
69 | { | ||
70 | delete p; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | |||
diff --git a/linden/indra/lscript/lscript_execute_mono/size.cpp b/linden/indra/lscript/lscript_execute_mono/size.cpp new file mode 100644 index 0000000..f9ca7a3 --- /dev/null +++ b/linden/indra/lscript/lscript_execute_mono/size.cpp | |||
@@ -0,0 +1,182 @@ | |||
1 | /** | ||
2 | * NOTE: none of this actually applies as this file was taken from mono/samples | ||
3 | * @file size.c | ||
4 | * @brief file from mono samples | ||
5 | * | ||
6 | * $LicenseInfo:firstyear=2007&license=internal$ | ||
7 | * | ||
8 | * Copyright (c) 2007-2008, Linden Research, Inc. | ||
9 | * | ||
10 | * The following source code is PROPRIETARY AND CONFIDENTIAL. Use of | ||
11 | * this source code is governed by the Linden Lab Source Code Disclosure | ||
12 | * Agreement ("Agreement") previously entered between you and Linden | ||
13 | * Lab. By accessing, using, copying, modifying or distributing this | ||
14 | * software, you acknowledge that you have been informed of your | ||
15 | * obligations under the Agreement and agree to abide by those obligations. | ||
16 | * | ||
17 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
18 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
19 | * COMPLETENESS OR PERFORMANCE. | ||
20 | * $/LicenseInfo$ | ||
21 | */ | ||
22 | |||
23 | #include <glib.h> | ||
24 | #include <mono/jit/jit.h> | ||
25 | #include <mono/metadata/environment.h> | ||
26 | #include <mono/metadata/profiler.h> | ||
27 | #include <mono/metadata/tokentype.h> | ||
28 | #include <mono/metadata/debug-helpers.h> | ||
29 | #include <mono/metadata/llassembly.h> | ||
30 | #include <string.h> | ||
31 | |||
32 | #include "stdtypes.h" | ||
33 | #include "linden_common.h" | ||
34 | |||
35 | #define FIELD_ATTRIBUTE_STATIC 0x10 | ||
36 | #define FIELD_ATTRIBUTE_HAS_FIELD_RVA 0x100 | ||
37 | |||
38 | static int memory_usage (MonoObject *obj, GHashTable *visited, | ||
39 | int (*functor)(MonoObject*, MonoType*, int)); | ||
40 | |||
41 | static int | ||
42 | memory_usage_array (MonoArray *array, GHashTable *visited, | ||
43 | int (*functor)(MonoObject*, MonoType*, int)) | ||
44 | { | ||
45 | int total = 0; | ||
46 | MonoClass *array_class = mono_object_get_class ((MonoObject *) array); | ||
47 | MonoClass *element_class = mono_class_get_element_class (array_class); | ||
48 | MonoType *element_type = mono_class_get_type (element_class); | ||
49 | |||
50 | if (MONO_TYPE_IS_REFERENCE (element_type)) { | ||
51 | int i; | ||
52 | |||
53 | for (i = 0; i < mono_array_length (array); i++) { | ||
54 | MonoObject *element = (MonoObject*)mono_array_get (array, gpointer, i); | ||
55 | |||
56 | if (element != NULL) | ||
57 | total += memory_usage (element, visited, functor); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | return total; | ||
62 | } | ||
63 | |||
64 | static int | ||
65 | memory_usage (MonoObject *obj, GHashTable *visited, | ||
66 | int (*functor)(MonoObject*, MonoType*, int)) | ||
67 | { | ||
68 | int total = 0; | ||
69 | MonoClass *klass; | ||
70 | MonoType *type; | ||
71 | gpointer iter = NULL; | ||
72 | MonoClassField *field; | ||
73 | |||
74 | if (g_hash_table_lookup (visited, obj)) | ||
75 | return 0; | ||
76 | |||
77 | g_hash_table_insert (visited, obj, obj); | ||
78 | |||
79 | klass = mono_object_get_class (obj); | ||
80 | type = mono_class_get_type (klass); | ||
81 | |||
82 | /* This is an array, so drill down into it */ | ||
83 | if (type->type == MONO_TYPE_SZARRAY) | ||
84 | total += memory_usage_array ((MonoArray *) obj, visited, functor); | ||
85 | |||
86 | while ((field = mono_class_get_fields (klass, &iter)) != NULL) { | ||
87 | MonoType *ftype = mono_field_get_type (field); | ||
88 | gpointer value; | ||
89 | |||
90 | if ((ftype->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)) != 0) | ||
91 | continue; | ||
92 | |||
93 | /* FIXME: There are probably other types we need to drill down into */ | ||
94 | switch (ftype->type) { | ||
95 | |||
96 | case MONO_TYPE_CLASS: | ||
97 | case MONO_TYPE_OBJECT: | ||
98 | mono_field_get_value (obj, field, &value); | ||
99 | |||
100 | if (value != NULL) | ||
101 | total += memory_usage ((MonoObject *) value, visited, functor); | ||
102 | |||
103 | break; | ||
104 | |||
105 | case MONO_TYPE_STRING: | ||
106 | mono_field_get_value (obj, field, &value); | ||
107 | if (value != NULL) | ||
108 | total += mono_object_get_size ((MonoObject *) value); | ||
109 | break; | ||
110 | |||
111 | case MONO_TYPE_SZARRAY: | ||
112 | mono_field_get_value (obj, field, &value); | ||
113 | |||
114 | if (value != NULL) { | ||
115 | total += memory_usage_array ((MonoArray *) value, visited, functor); | ||
116 | total += mono_object_get_size ((MonoObject *) value); | ||
117 | } | ||
118 | |||
119 | break; | ||
120 | |||
121 | default: | ||
122 | /* printf ("Got type 0x%x\n", ftype->type); */ | ||
123 | /* ignore, this will be included in mono_object_get_size () */ | ||
124 | break; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | total = functor(obj, type, total); | ||
129 | |||
130 | return total; | ||
131 | } | ||
132 | |||
133 | |||
134 | int addObjectSize(MonoObject* obj, MonoType* type, int total) | ||
135 | { | ||
136 | return total + mono_object_get_size (obj); | ||
137 | } | ||
138 | |||
139 | /* | ||
140 | * Only returns data for instances, not for static fields, those might | ||
141 | * be larger, or hold larger structures | ||
142 | */ | ||
143 | int | ||
144 | GetMemoryUsage (MonoObject *obj) | ||
145 | { | ||
146 | GHashTable *visited = g_hash_table_new (NULL, NULL); | ||
147 | int n; | ||
148 | |||
149 | n = memory_usage (obj, visited, addObjectSize); | ||
150 | |||
151 | g_hash_table_destroy (visited); | ||
152 | |||
153 | return n; | ||
154 | } | ||
155 | |||
156 | |||
157 | |||
158 | int printObjectSize(MonoObject* obj, MonoType* type, int total) | ||
159 | { | ||
160 | total += mono_object_get_size (obj); | ||
161 | llinfos << "Object type: " << mono_type_full_name(type) << " size: " | ||
162 | << total << llendl; | ||
163 | |||
164 | return total; | ||
165 | } | ||
166 | |||
167 | /* | ||
168 | * Only returns data for instances, not for static fields, those might | ||
169 | * be larger, or hold larger structures | ||
170 | */ | ||
171 | int | ||
172 | PrintMemoryUsage (MonoObject *obj) | ||
173 | { | ||
174 | GHashTable *visited = g_hash_table_new (NULL, NULL); | ||
175 | int n; | ||
176 | |||
177 | n = memory_usage (obj, visited, printObjectSize); | ||
178 | |||
179 | g_hash_table_destroy (visited); | ||
180 | |||
181 | return n; | ||
182 | } | ||