aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/lscript/lscript_execute_mono/size.cpp
blob: 6941b45aabbbc785212514d39c28b2c0c3f98dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/** 
 * NOTE: none of this actually applies as this file was taken from mono/samples
 * @file size.c
 * @brief file from mono samples 
 *
 * $LicenseInfo:firstyear=2007&license=internal$
 * 
 * Copyright (c) 2007-2009, Linden Research, Inc.
 * 
 * The following source code is PROPRIETARY AND CONFIDENTIAL. Use of
 * this source code is governed by the Linden Lab Source Code Disclosure
 * Agreement ("Agreement") previously entered between you and Linden
 * Lab. By accessing, using, copying, modifying or distributing this
 * software, you acknowledge that you have been informed of your
 * obligations under the Agreement and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include <glib.h>
#include <mono/jit/jit.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/profiler.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/llassembly.h>
#include <string.h>

#include "stdtypes.h"
#include "linden_common.h"

#define FIELD_ATTRIBUTE_STATIC 0x10
#define FIELD_ATTRIBUTE_HAS_FIELD_RVA 0x100

static int memory_usage (MonoObject *obj, GHashTable *visited, 
			 int (*functor)(MonoObject*, MonoType*, int));

static int
memory_usage_array (MonoArray *array, GHashTable *visited,
		    int (*functor)(MonoObject*, MonoType*, int))
{
  int total = 0;
  MonoClass *array_class = mono_object_get_class ((MonoObject *) array);
  MonoClass *element_class = mono_class_get_element_class (array_class);
  MonoType *element_type = mono_class_get_type (element_class);

  if (MONO_TYPE_IS_REFERENCE (element_type)) {
    int i;

    for (i = 0; i < mono_array_length (array); i++) {
      MonoObject *element = (MonoObject*)mono_array_get (array, gpointer, i);

      if (element != NULL)
	total += memory_usage (element, visited, functor);
    }
  }

  return total;
}

static int
memory_usage (MonoObject *obj, GHashTable *visited, 
	      int (*functor)(MonoObject*, MonoType*, int))
{
  int total = 0;
  MonoClass *klass;
  MonoType *type;
  gpointer iter = NULL;
  MonoClassField *field;

  if (g_hash_table_lookup (visited, obj))
    return 0;

  g_hash_table_insert (visited, obj, obj);

  klass = mono_object_get_class (obj);
  type = mono_class_get_type (klass);

  /* This is an array, so drill down into it */
  if (type->type == MONO_TYPE_SZARRAY)
    total += memory_usage_array ((MonoArray *) obj, visited, functor);

  while ((field = mono_class_get_fields (klass, &iter)) != NULL) {
    MonoType *ftype = mono_field_get_type (field);
    gpointer value;

    if ((ftype->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)) != 0)
      continue;

    /* FIXME: There are probably other types we need to drill down into */
    switch (ftype->type) {

    case MONO_TYPE_CLASS:
    case MONO_TYPE_OBJECT:
      mono_field_get_value (obj, field, &value);

      if (value != NULL)
	total += memory_usage ((MonoObject *) value, visited, functor);

      break;

    case MONO_TYPE_STRING:
      mono_field_get_value (obj, field, &value);
      if (value != NULL) 
	total += mono_object_get_size ((MonoObject *) value);
      break;

    case MONO_TYPE_SZARRAY:
      mono_field_get_value (obj, field, &value);

      if (value != NULL) {
	total += memory_usage_array ((MonoArray *) value, visited, functor);
	total += mono_object_get_size ((MonoObject *) value);
      }

      break;

    default:
      /* printf ("Got type 0x%x\n", ftype->type); */
      /* ignore, this will be included in mono_object_get_size () */
      break;
    }
  }

  total = functor(obj, type, total);

  return total;
}


int addObjectSize(MonoObject* obj, MonoType* type, int total) 
{
  return total + mono_object_get_size (obj);
}

/*
 * Only returns data for instances, not for static fields, those might
 * be larger, or hold larger structures
 */
int
GetMemoryUsage (MonoObject *obj)
{
  GHashTable *visited = g_hash_table_new (NULL, NULL);
  int n;
  
  n = memory_usage (obj, visited, addObjectSize);

  g_hash_table_destroy (visited);
  
  return n;
}



int printObjectSize(MonoObject* obj, MonoType* type, int total) 
{
  total += mono_object_get_size (obj);
  llinfos << "Object type: " << mono_type_full_name(type) << " size: "
	  << total << llendl;
    
  return total;
}

/*
 * Only returns data for instances, not for static fields, those might
 * be larger, or hold larger structures
 */
int
PrintMemoryUsage (MonoObject *obj)
{
  GHashTable *visited = g_hash_table_new (NULL, NULL);
  int n;
  
  n = memory_usage (obj, visited, printObjectSize);

  g_hash_table_destroy (visited);
  
  return n;
}