2 #include <mono/jit/jit.h>
3 #include <mono/metadata/environment.h>
4 #include <mono/metadata/profiler.h>
5 #include <mono/metadata/tokentype.h>
6 #include <mono/metadata/debug-helpers.h>
7 #include <mono/metadata/assembly.h>
10 #define FIELD_ATTRIBUTE_STATIC 0x10
13 memory_usage (MonoObject *this, GHashTable *visited)
17 gpointer iter = (gpointer) 0;
18 MonoClassField *field;
20 if (g_hash_table_lookup (visited, this))
23 class = mono_object_get_class (this);
25 g_hash_table_insert (visited, this, this);
27 while ((field = mono_class_get_fields (class, &iter)) != NULL){
28 MonoType *ftype = mono_field_get_type (field);
31 if ((ftype->attrs & FIELD_ATTRIBUTE_STATIC) != 0)
36 case MONO_TYPE_OBJECT:
37 mono_field_get_value (this, field, &value);
40 total += memory_usage ((MonoObject *) value, visited);
43 case MONO_TYPE_SZARRAY:
44 printf ("implement me\n");
54 case MONO_TYPE_BOOLEAN:
59 printf ("unhandled type: 0x%x\n", ftype->type);
63 total += mono_class_instance_size (class);
69 * Only returns data for instances, not for static fields, those might
70 * be larger, or hold larger structures
73 GetMemoryUsage (MonoObject *this)
75 GHashTable *visited = g_hash_table_new (NULL, NULL);
78 n = memory_usage (this, visited);
80 g_hash_table_destroy (visited);
85 static int installed = 0;
87 void install_icall (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result)
92 mono_add_internal_call ("Mono.ObjectServices.ObjectInspector::GetMemoryUsage", GetMemoryUsage);
97 mono_profiler_startup (const char *desc)
99 mono_profiler_install_jit_end (install_icall);
100 mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);