Merge pull request #201 from QuickJack/master
[mono.git] / mono / utils / mono-counters.c
1 /*
2  * Copyright 2006-2010 Novell
3  * Copyright 2011 Xamarin Inc
4  */
5
6 #include <stdlib.h>
7 #include <glib.h>
8 #include "mono-counters.h"
9
10 typedef struct _MonoCounter MonoCounter;
11
12 struct _MonoCounter {
13         MonoCounter *next;
14         const char *name;
15         void *addr;
16         int type;
17 };
18
19 static MonoCounter *counters = NULL;
20 static int valid_mask = 0;
21 static int set_mask = 0;
22
23 /**
24  * mono_counters_enable:
25  * @section_mask: a mask listing the sections that will be displayed
26  *
27  * This is used to track which counters will be displayed.
28  */
29 void
30 mono_counters_enable (int section_mask)
31 {
32         valid_mask = section_mask & MONO_COUNTER_SECTION_MASK;
33 }
34
35 /**
36  * mono_counters_register:
37  * @name: The name for this counters.
38  * @type: One of the possible MONO_COUNTER types, or MONO_COUNTER_CALLBACK for a function pointer.
39  * @addr: The address to register.
40  *
41  * Register addr as the address of a counter of type type.
42  * Note that @name must be a valid string at all times until
43  * mono_counters_dump () is called.
44  *
45  * It may be a function pointer if MONO_COUNTER_CALLBACK is specified:
46  * the function should return the value and take no arguments.
47  */
48 void 
49 mono_counters_register (const char* name, int type, void *addr)
50 {
51         MonoCounter *counter;
52         if (!(type & valid_mask))
53                 return;
54         counter = malloc (sizeof (MonoCounter));
55         if (!counter)
56                 return;
57         counter->name = name;
58         counter->type = type;
59         counter->addr = addr;
60         counter->next = NULL;
61
62         set_mask |= type;
63
64         /* Append */
65         if (counters) {
66                 MonoCounter *item = counters;
67                 while (item->next)
68                         item = item->next;
69                 item->next = counter;
70         } else {
71                 counters = counter;
72         }
73 }
74
75 typedef int (*IntFunc) (void);
76 typedef guint (*UIntFunc) (void);
77 typedef gint64 (*LongFunc) (void);
78 typedef guint64 (*ULongFunc) (void);
79 typedef gssize (*PtrFunc) (void);
80 typedef double (*DoubleFunc) (void);
81 typedef char* (*StrFunc) (void);
82
83 #define ENTRY_FMT "%-36s: "
84 static void
85 dump_counter (MonoCounter *counter, FILE *outfile) {
86         int intval;
87         guint uintval;
88         gint64 int64val;
89         guint64 uint64val;
90         gssize wordval;
91         double dval;
92         const char *str;
93         switch (counter->type & MONO_COUNTER_TYPE_MASK) {
94         case MONO_COUNTER_INT:
95               if (counter->type & MONO_COUNTER_CALLBACK)
96                       intval = ((IntFunc)counter->addr) ();
97               else
98                       intval = *(int*)counter->addr;
99               fprintf (outfile, ENTRY_FMT "%d\n", counter->name, intval);
100               break;
101         case MONO_COUNTER_UINT:
102               if (counter->type & MONO_COUNTER_CALLBACK)
103                       uintval = ((UIntFunc)counter->addr) ();
104               else
105                       uintval = *(guint*)counter->addr;
106               fprintf (outfile, ENTRY_FMT "%u\n", counter->name, uintval);
107               break;
108         case MONO_COUNTER_LONG:
109               if (counter->type & MONO_COUNTER_CALLBACK)
110                       int64val = ((LongFunc)counter->addr) ();
111               else
112                       int64val = *(gint64*)counter->addr;
113               fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, (long long)int64val);
114               break;
115         case MONO_COUNTER_ULONG:
116               if (counter->type & MONO_COUNTER_CALLBACK)
117                       uint64val = ((ULongFunc)counter->addr) ();
118               else
119                       uint64val = *(guint64*)counter->addr;
120               fprintf (outfile, ENTRY_FMT "%llu\n", counter->name, (unsigned long long)uint64val);
121               break;
122         case MONO_COUNTER_WORD:
123               if (counter->type & MONO_COUNTER_CALLBACK)
124                       wordval = ((PtrFunc)counter->addr) ();
125               else
126                       wordval = *(gssize*)counter->addr;
127 #if SIZEOF_VOID_P == 8
128               fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, (gint64)wordval);
129 #else
130               fprintf (outfile, ENTRY_FMT "%d\n", counter->name, (gint)wordval);
131 #endif
132               break;
133         case MONO_COUNTER_DOUBLE:
134               if (counter->type & MONO_COUNTER_CALLBACK)
135                       dval = ((DoubleFunc)counter->addr) ();
136               else
137                       dval = *(double*)counter->addr;
138               fprintf (outfile, ENTRY_FMT "%.4f\n", counter->name, dval);
139               break;
140         case MONO_COUNTER_STRING:
141               if (counter->type & MONO_COUNTER_CALLBACK)
142                       str = ((StrFunc)counter->addr) ();
143               else
144                       str = *(char**)counter->addr;
145               fprintf (outfile, ENTRY_FMT "%s\n", counter->name, str);
146               break;
147         }
148 }
149
150 static const char
151 section_names [][10] = {
152         "JIT",
153         "GC",
154         "Metadata",
155         "Generics",
156         "Security"
157 };
158
159 static void
160 mono_counters_dump_section (int section, FILE *outfile)
161 {
162         MonoCounter *counter = counters;
163         while (counter) {
164                 if (counter->type & section)
165                         dump_counter (counter, outfile);
166                 counter = counter->next;
167         }
168 }
169
170 /**
171  * mono_counters_dump:
172  * @section_mask: The sections to dump counters for
173  * @outfile: a FILE to dump the results to
174  *
175  * Displays the counts of all the enabled counters registered. 
176  */
177 void
178 mono_counters_dump (int section_mask, FILE *outfile)
179 {
180         int i, j;
181         section_mask &= valid_mask;
182         if (!counters)
183                 return;
184         for (j = 0, i = MONO_COUNTER_JIT; i < MONO_COUNTER_LAST_SECTION; j++, i <<= 1) {
185                 if ((section_mask & i) && (set_mask & i)) {
186                         fprintf (outfile, "\n%s statistics\n", section_names [j]);
187                         mono_counters_dump_section (i, outfile);
188                 }
189         }
190
191         fflush (outfile);
192 }
193
194 /**
195  * mono_counters_cleanup:
196  *
197  * Perform any needed cleanup at process exit.
198  */
199 void
200 mono_counters_cleanup (void)
201 {
202         MonoCounter *counter = counters;
203         while (counter) {
204                 MonoCounter *tmp = counters;
205                 counter = counter->next;
206                 free (tmp);
207         }
208         counters = NULL;
209 }
210
211 static MonoResourceCallback limit_reached = NULL;
212 static uintptr_t resource_limits [MONO_RESOURCE_COUNT * 2];
213
214 /**
215  * mono_runtime_resource_check_limit:
216  * @resource_type: one of the #MonoResourceType enum values
217  * @value: the current value of the resource usage
218  *
219  * Check if a runtime resource limit has been reached. This function
220  * is intended to be used by the runtime only.
221  */
222 void
223 mono_runtime_resource_check_limit (int resource_type, uintptr_t value)
224 {
225         if (!limit_reached)
226                 return;
227         /* check the hard limit first */
228         if (value > resource_limits [resource_type * 2 + 1]) {
229                 limit_reached (resource_type, value, 0);
230                 return;
231         }
232         if (value > resource_limits [resource_type * 2])
233                 limit_reached (resource_type, value, 1);
234 }
235
236 /**
237  * mono_runtime_resource_limit:
238  * @resource_type: one of the #MonoResourceType enum values
239  * @soft_limit: the soft limit value
240  * @hard_limit: the hard limit value
241  *
242  * This function sets the soft and hard limit for runtime resources. When the limit
243  * is reached, a user-specified callback is called. The callback runs in a restricted
244  * environment, in which the world coult be stopped, so it can't take locks, perform
245  * allocations etc. The callback may be called multiple times once a limit has been reached
246  * if action is not taken to decrease the resource use.
247  *
248  * Returns: 0 on error or a positive integer otherwise.
249  */
250 int
251 mono_runtime_resource_limit (int resource_type, uintptr_t soft_limit, uintptr_t hard_limit)
252 {
253         if (resource_type >= MONO_RESOURCE_COUNT || resource_type < 0)
254                 return 0;
255         if (soft_limit > hard_limit)
256                 return 0;
257         resource_limits [resource_type * 2] = soft_limit;
258         resource_limits [resource_type * 2 + 1] = hard_limit;
259         return 1;
260 }
261
262 /**
263  * mono_runtime_resource_set_callback:
264  * @callback: a function pointer
265  * 
266  * Set the callback to be invoked when a resource limit is reached.
267  * The callback will receive the resource type, the resource amount in resource-specific
268  * units and a flag indicating whether the soft or hard limit was reached.
269  */
270 void
271 mono_runtime_resource_set_callback (MonoResourceCallback callback)
272 {
273         limit_reached = callback;
274 }
275
276