2 * Copyright 2006-2010 Novell
3 * Copyright 2011 Xamarin Inc
8 #include "mono-counters.h"
17 static MonoCounter *counters = NULL;
18 static int valid_mask = 0;
19 static int set_mask = 0;
22 mono_counter_get_variance (MonoCounter *counter)
24 return counter->type & MONO_COUNTER_VARIANCE_MASK;
28 mono_counter_get_unit (MonoCounter *counter)
30 return counter->type & MONO_COUNTER_UNIT_MASK;
34 * mono_counters_enable:
35 * @section_mask: a mask listing the sections that will be displayed
37 * This is used to track which counters will be displayed.
40 mono_counters_enable (int section_mask)
42 valid_mask = section_mask & MONO_COUNTER_SECTION_MASK;
46 * mono_counters_register:
47 * @name: The name for this counters.
48 * @type: One of the possible MONO_COUNTER types, or MONO_COUNTER_CALLBACK for a function pointer.
49 * @addr: The address to register.
51 * Register addr as the address of a counter of type type.
52 * Note that @name must be a valid string at all times until
53 * mono_counters_dump () is called.
55 * It may be a function pointer if MONO_COUNTER_CALLBACK is specified:
56 * the function should return the value and take no arguments.
59 mono_counters_register (const char* name, int type, void *addr)
62 if (!(type & valid_mask))
64 counter = malloc (sizeof (MonoCounter));
76 MonoCounter *item = counters;
85 typedef int (*IntFunc) (void);
86 typedef guint (*UIntFunc) (void);
87 typedef gint64 (*LongFunc) (void);
88 typedef guint64 (*ULongFunc) (void);
89 typedef gssize (*PtrFunc) (void);
90 typedef double (*DoubleFunc) (void);
91 typedef char* (*StrFunc) (void);
93 #define ENTRY_FMT "%-36s: "
95 dump_counter (MonoCounter *counter, FILE *outfile) {
103 switch (counter->type & MONO_COUNTER_TYPE_MASK) {
104 case MONO_COUNTER_INT:
105 if (counter->type & MONO_COUNTER_CALLBACK)
106 intval = ((IntFunc)counter->addr) ();
108 intval = *(int*)counter->addr;
109 fprintf (outfile, ENTRY_FMT "%d\n", counter->name, intval);
111 case MONO_COUNTER_UINT:
112 if (counter->type & MONO_COUNTER_CALLBACK)
113 uintval = ((UIntFunc)counter->addr) ();
115 uintval = *(guint*)counter->addr;
116 fprintf (outfile, ENTRY_FMT "%u\n", counter->name, uintval);
118 case MONO_COUNTER_LONG:
119 if (counter->type & MONO_COUNTER_CALLBACK)
120 int64val = ((LongFunc)counter->addr) ();
122 int64val = *(gint64*)counter->addr;
123 if (mono_counter_get_unit (counter) == MONO_COUNTER_TIME)
124 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)int64val / 10000.0);
126 fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, (long long)int64val);
128 case MONO_COUNTER_ULONG:
129 if (counter->type & MONO_COUNTER_CALLBACK)
130 uint64val = ((ULongFunc)counter->addr) ();
132 uint64val = *(guint64*)counter->addr;
133 fprintf (outfile, ENTRY_FMT "%llu\n", counter->name, (unsigned long long)uint64val);
135 case MONO_COUNTER_WORD:
136 if (counter->type & MONO_COUNTER_CALLBACK)
137 wordval = ((PtrFunc)counter->addr) ();
139 wordval = *(gssize*)counter->addr;
140 fprintf (outfile, ENTRY_FMT "%zd\n", counter->name, (gint64)wordval);
142 case MONO_COUNTER_DOUBLE:
143 if (counter->type & MONO_COUNTER_CALLBACK)
144 dval = ((DoubleFunc)counter->addr) ();
146 dval = *(double*)counter->addr;
147 fprintf (outfile, ENTRY_FMT "%.4f\n", counter->name, dval);
149 case MONO_COUNTER_STRING:
150 if (counter->type & MONO_COUNTER_CALLBACK)
151 str = ((StrFunc)counter->addr) ();
153 str = *(char**)counter->addr;
154 fprintf (outfile, ENTRY_FMT "%s\n", counter->name, str);
160 mono_counters_foreach (CountersEnumCallback cb, gpointer user_data)
162 MonoCounter *counter;
164 for (counter = counters; counter; counter = counter->next) {
165 if (!cb (counter, user_data))
171 section_names [][10] = {
182 mono_counters_dump_section (int section, FILE *outfile)
184 MonoCounter *counter = counters;
186 if (counter->type & section && mono_counter_get_variance (counter) == MONO_COUNTER_MONOTONIC)
187 dump_counter (counter, outfile);
188 counter = counter->next;
193 * mono_counters_dump:
194 * @section_mask: The sections to dump counters for
195 * @outfile: a FILE to dump the results to
197 * Displays the counts of all the enabled counters registered.
200 mono_counters_dump (int section_mask, FILE *outfile)
203 section_mask &= valid_mask;
206 for (j = 0, i = MONO_COUNTER_JIT; i < MONO_COUNTER_LAST_SECTION; j++, i <<= 1) {
207 if ((section_mask & i) && (set_mask & i)) {
208 fprintf (outfile, "\n%s statistics\n", section_names [j]);
209 mono_counters_dump_section (i, outfile);
217 * mono_counters_cleanup:
219 * Perform any needed cleanup at process exit.
222 mono_counters_cleanup (void)
224 MonoCounter *counter = counters;
227 MonoCounter *tmp = counter;
228 counter = counter->next;
233 static MonoResourceCallback limit_reached = NULL;
234 static uintptr_t resource_limits [MONO_RESOURCE_COUNT * 2];
237 * mono_runtime_resource_check_limit:
238 * @resource_type: one of the #MonoResourceType enum values
239 * @value: the current value of the resource usage
241 * Check if a runtime resource limit has been reached. This function
242 * is intended to be used by the runtime only.
245 mono_runtime_resource_check_limit (int resource_type, uintptr_t value)
249 /* check the hard limit first */
250 if (value > resource_limits [resource_type * 2 + 1]) {
251 limit_reached (resource_type, value, 0);
254 if (value > resource_limits [resource_type * 2])
255 limit_reached (resource_type, value, 1);
259 * mono_runtime_resource_limit:
260 * @resource_type: one of the #MonoResourceType enum values
261 * @soft_limit: the soft limit value
262 * @hard_limit: the hard limit value
264 * This function sets the soft and hard limit for runtime resources. When the limit
265 * is reached, a user-specified callback is called. The callback runs in a restricted
266 * environment, in which the world coult be stopped, so it can't take locks, perform
267 * allocations etc. The callback may be called multiple times once a limit has been reached
268 * if action is not taken to decrease the resource use.
270 * Returns: 0 on error or a positive integer otherwise.
273 mono_runtime_resource_limit (int resource_type, uintptr_t soft_limit, uintptr_t hard_limit)
275 if (resource_type >= MONO_RESOURCE_COUNT || resource_type < 0)
277 if (soft_limit > hard_limit)
279 resource_limits [resource_type * 2] = soft_limit;
280 resource_limits [resource_type * 2 + 1] = hard_limit;
285 * mono_runtime_resource_set_callback:
286 * @callback: a function pointer
288 * Set the callback to be invoked when a resource limit is reached.
289 * The callback will receive the resource type, the resource amount in resource-specific
290 * units and a flag indicating whether the soft or hard limit was reached.
293 mono_runtime_resource_set_callback (MonoResourceCallback callback)
295 limit_reached = callback;