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:
46 mono_field_get_value (this, field, &value);
47 len = mono_array_length ((MonoArray *)value);
48 for (i = 0; i < len; i++){
49 MonoObject *item = mono_array_get ((MonoArray *) value, gpointer, i);
51 total += memory_usage (item, visited);
63 case MONO_TYPE_BOOLEAN:
68 printf ("unhandled type: 0x%x\n", ftype->type);
72 total += mono_class_instance_size (class);
78 * Only returns data for instances, not for static fields, those might
79 * be larger, or hold larger structures
82 GetMemoryUsage (MonoObject *this)
84 GHashTable *visited = g_hash_table_new (NULL, NULL);
87 n = memory_usage (this, visited);
89 g_hash_table_destroy (visited);
94 static int installed = 0;
96 void install_icall (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result)
101 mono_add_internal_call ("Mono.ObjectServices.ObjectInspector::GetMemoryUsage", GetMemoryUsage);
106 mono_profiler_startup (const char *desc)
108 mono_profiler_install_jit_end (install_icall);
109 mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);