2 * Copyright 2006-2010 Novell
3 * Copyright 2011 Xamarin Inc
9 #include "mono-counters.h"
10 #include "mono-proclib.h"
11 #include "mono-os-mutex.h"
25 static MonoCounter *counters = NULL;
26 static mono_mutex_t counters_mutex;
28 static volatile gboolean initialized = FALSE;
30 static int valid_mask = 0;
31 static int set_mask = 0;
33 static GSList *register_callbacks = NULL;
35 static void initialize_system_counters (void);
38 * mono_counter_get_variance:
39 * @counter: counter to get the variance
41 * Variance specifies how the counter value is expected to behave between any two samplings.
43 * Returns: the monotonicity of the counter.
46 mono_counter_get_variance (MonoCounter *counter)
48 return counter->type & MONO_COUNTER_VARIANCE_MASK;
52 * mono_counter_get_unit:
53 * @counter: counter to get the unit
55 * The unit gives a high level view of the unit that the counter is measuring.
57 * Returns: the unit of the counter.
60 mono_counter_get_unit (MonoCounter *counter)
62 return counter->type & MONO_COUNTER_UNIT_MASK;
66 * mono_counter_get_section:
67 * @counter: counter to get the section
69 * Sections are the unit of organization between all counters.
71 * Returns: the section of the counter.
75 mono_counter_get_section (MonoCounter *counter)
77 return counter->type & MONO_COUNTER_SECTION_MASK;
81 * mono_counter_get_type:
82 * @counter: counter to get the type
84 * Returns the type used to strong the value of the counter.
86 * Returns:the type of the counter.
89 mono_counter_get_type (MonoCounter *counter)
91 return counter->type & MONO_COUNTER_TYPE_MASK;
95 * mono_counter_get_name:
96 * @counter: counter to get the name
98 * Returns the counter name. The string should not be freed.
100 * Returns the name of the counter.
104 mono_counter_get_name (MonoCounter *counter)
106 return counter->name;
110 * mono_counter_get_size:
111 * @counter: counter to get the max size of the counter
113 * Use the returned size to create the buffer used with mono_counters_sample
115 * Returns: the max size of the counter data.
118 mono_counter_get_size (MonoCounter *counter)
120 return counter->size;
124 * mono_counters_enable:
125 * @section_mask: a mask listing the sections that will be displayed
127 * This is used to track which counters will be displayed.
130 mono_counters_enable (int section_mask)
132 valid_mask = section_mask & MONO_COUNTER_SECTION_MASK;
136 mono_counters_init (void)
141 mono_os_mutex_init (&counters_mutex);
143 initialize_system_counters ();
149 register_internal (const char *name, int type, void *addr, int size)
151 MonoCounter *counter;
152 GSList *register_callback;
154 g_assert (size >= 0);
155 if ((type & MONO_COUNTER_VARIANCE_MASK) == 0)
156 type |= MONO_COUNTER_MONOTONIC;
158 mono_os_mutex_lock (&counters_mutex);
160 for (counter = counters; counter; counter = counter->next) {
161 if (counter->addr == addr) {
162 g_warning ("you are registering twice the same counter address");
163 mono_os_mutex_unlock (&counters_mutex);
168 counter = (MonoCounter *) malloc (sizeof (MonoCounter));
170 mono_os_mutex_unlock (&counters_mutex);
173 counter->name = g_strdup (name);
174 counter->type = type;
175 counter->addr = addr;
176 counter->next = NULL;
177 counter->size = size;
183 MonoCounter *item = counters;
186 item->next = counter;
191 for (register_callback = register_callbacks; register_callback; register_callback = register_callback->next)
192 ((MonoCounterRegisterCallback)register_callback->data) (counter);
194 mono_os_mutex_unlock (&counters_mutex);
198 * mono_counters_register:
199 * @name: The name for this counters.
200 * @type: One of the possible MONO_COUNTER types, or MONO_COUNTER_CALLBACK for a function pointer.
201 * @addr: The address to register.
203 * Register addr as the address of a counter of type type.
204 * Note that @name must be a valid string at all times until
205 * mono_counters_dump () is called.
207 * This function should not be used with counter types that require an explicit size such as string
208 * as the counter size will be set to zero making them effectively useless.
211 * It may be a function pointer if MONO_COUNTER_CALLBACK is specified:
212 * the function should return the value and take no arguments.
215 mono_counters_register (const char* name, int type, void *addr)
218 switch (type & MONO_COUNTER_TYPE_MASK) {
219 case MONO_COUNTER_INT:
222 case MONO_COUNTER_UINT:
223 size = sizeof (guint);
225 case MONO_COUNTER_LONG:
226 case MONO_COUNTER_TIME_INTERVAL:
227 size = sizeof (gint64);
229 case MONO_COUNTER_ULONG:
230 size = sizeof (guint64);
232 case MONO_COUNTER_WORD:
233 size = sizeof (gssize);
235 case MONO_COUNTER_DOUBLE:
236 size = sizeof (double);
238 case MONO_COUNTER_STRING:
242 g_assert_not_reached ();
246 g_debug ("counters not enabled");
248 register_internal (name, type, addr, size);
252 * mono_counters_register_with_size:
253 * @name: The name for this counters.
254 * @type: One of the possible MONO_COUNTER types, or MONO_COUNTER_CALLBACK for a function pointer.
255 * @addr: The address to register.
256 * @size: Max size of the counter data.
258 * Register addr as the address of a counter of type @type.
259 * Note that @name must be a valid string at all times until
260 * mono_counters_dump () is called.
262 * It may be a function pointer if MONO_COUNTER_CALLBACK is specified:
263 * the function should return the value and take no arguments.
265 * The value of @size is ignored for types with fixed size such as int and long.
267 * Use @size for types that can have dynamic size such as string.
269 * If @size is negative, it's silently converted to zero.
272 mono_counters_register_with_size (const char *name, int type, void *addr, int size)
275 g_debug ("counters not enabled");
277 register_internal (name, type, addr, size);
281 * mono_counters_on_register
282 * @callback : function to callback when a counter is registered
284 * Add a callback that is going to be called when a counter is registered
287 mono_counters_on_register (MonoCounterRegisterCallback callback)
290 g_debug ("counters not enabled");
294 mono_os_mutex_lock (&counters_mutex);
295 register_callbacks = g_slist_append (register_callbacks, (gpointer) callback);
296 mono_os_mutex_unlock (&counters_mutex);
299 typedef int (*IntFunc) (void);
300 typedef guint (*UIntFunc) (void);
301 typedef gint64 (*LongFunc) (void);
302 typedef guint64 (*ULongFunc) (void);
303 typedef gssize (*PtrFunc) (void);
304 typedef double (*DoubleFunc) (void);
305 typedef char* (*StrFunc) (void);
310 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_USER_TIME);
316 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_SYSTEM_TIME);
322 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_TOTAL_TIME);
328 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_WORKING_SET);
334 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_PRIVATE_BYTES);
340 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_VIRTUAL_BYTES);
346 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_FAULTS);
352 #if defined(TARGET_WIN32)
353 #elif defined(TARGET_MACH)
355 if (getloadavg (load, 3) > 0)
358 char buffer[512], *b;
360 FILE *f = fopen ("/proc/loadavg", "r");
362 len = fread (buffer, 1, sizeof (buffer) - 1, f);
365 buffer [len < 511 ? len : 511] = 0;
367 for (i = 0; i < 3; i++) {
369 return strtod (b, NULL);
396 cpu_load_15min (void)
401 #define SYSCOUNTER_TIME (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_TIME | MONO_COUNTER_MONOTONIC | MONO_COUNTER_CALLBACK)
402 #define SYSCOUNTER_BYTES (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE | MONO_COUNTER_CALLBACK)
403 #define SYSCOUNTER_COUNT (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_COUNT | MONO_COUNTER_MONOTONIC | MONO_COUNTER_CALLBACK)
404 #define SYSCOUNTER_LOAD (MONO_COUNTER_SYSTEM | MONO_COUNTER_DOUBLE | MONO_COUNTER_PERCENTAGE | MONO_COUNTER_VARIABLE | MONO_COUNTER_CALLBACK)
407 initialize_system_counters (void)
409 register_internal ("User Time", SYSCOUNTER_TIME, (gpointer) &user_time, sizeof (gint64));
410 register_internal ("System Time", SYSCOUNTER_TIME, (gpointer) &system_time, sizeof (gint64));
411 register_internal ("Total Time", SYSCOUNTER_TIME, (gpointer) &total_time, sizeof (gint64));
412 register_internal ("Working Set", SYSCOUNTER_BYTES, (gpointer) &working_set, sizeof (gint64));
413 register_internal ("Private Bytes", SYSCOUNTER_BYTES, (gpointer) &private_bytes, sizeof (gint64));
414 register_internal ("Virtual Bytes", SYSCOUNTER_BYTES, (gpointer) &virtual_bytes, sizeof (gint64));
415 register_internal ("Page Faults", SYSCOUNTER_COUNT, (gpointer) &page_faults, sizeof (gint64));
416 register_internal ("CPU Load Average - 1min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_1min, sizeof (double));
417 register_internal ("CPU Load Average - 5min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_5min, sizeof (double));
418 register_internal ("CPU Load Average - 15min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_15min, sizeof (double));
422 * mono_counters_foreach:
423 * @cb: The callback that will be called for each counter.
424 * @user_data: Value passed as second argument of the callback.
426 * Iterate over all counters and call @cb for each one of them. Stop iterating if
427 * the callback returns FALSE.
431 mono_counters_foreach (CountersEnumCallback cb, gpointer user_data)
433 MonoCounter *counter;
436 g_debug ("counters not enabled");
440 mono_os_mutex_lock (&counters_mutex);
442 for (counter = counters; counter; counter = counter->next) {
443 if (!cb (counter, user_data)) {
444 mono_os_mutex_unlock (&counters_mutex);
449 mono_os_mutex_unlock (&counters_mutex);
452 #define COPY_COUNTER(type,functype) do { \
453 size = sizeof (type); \
454 if (buffer_size < size) \
457 *(type*)buffer = cb ? ((functype)counter->addr) () : *(type*)counter->addr; \
462 sample_internal (MonoCounter *counter, void *buffer, int buffer_size)
464 int cb = counter->type & MONO_COUNTER_CALLBACK;
469 switch (mono_counter_get_type (counter)) {
470 case MONO_COUNTER_INT:
471 COPY_COUNTER (int, IntFunc);
473 case MONO_COUNTER_UINT:
474 COPY_COUNTER (guint, UIntFunc);
476 case MONO_COUNTER_LONG:
477 case MONO_COUNTER_TIME_INTERVAL:
478 COPY_COUNTER (gint64, LongFunc);
480 case MONO_COUNTER_ULONG:
481 COPY_COUNTER (guint64, ULongFunc);
483 case MONO_COUNTER_WORD:
484 COPY_COUNTER (gssize, PtrFunc);
486 case MONO_COUNTER_DOUBLE:
487 COPY_COUNTER (double, DoubleFunc);
489 case MONO_COUNTER_STRING:
490 if (buffer_size < counter->size) {
492 } else if (counter->size == 0) {
495 strval = cb ? ((StrFunc)counter->addr) () : (char*)counter->addr;
499 size = counter->size;
500 strncpy ((char *) buffer, strval, size - 1);
501 ((char*)buffer)[size - 1] = '\0';
510 mono_counters_sample (MonoCounter *counter, void *buffer, int buffer_size)
513 g_debug ("counters not enabled");
517 return sample_internal (counter, buffer, buffer_size);
520 #define ENTRY_FMT "%-36s: "
522 dump_counter (MonoCounter *counter, FILE *outfile) {
523 void *buffer = g_malloc0 (counter->size);
524 int size = sample_internal (counter, buffer, counter->size);
526 switch (counter->type & MONO_COUNTER_TYPE_MASK) {
527 case MONO_COUNTER_INT:
528 fprintf (outfile, ENTRY_FMT "%d\n", counter->name, *(int*)buffer);
530 case MONO_COUNTER_UINT:
531 fprintf (outfile, ENTRY_FMT "%u\n", counter->name, *(guint*)buffer);
533 case MONO_COUNTER_LONG:
534 if ((counter->type & MONO_COUNTER_UNIT_MASK) == MONO_COUNTER_TIME)
535 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(gint64*)buffer) / 10000.0);
537 fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, *(long long *)buffer);
539 case MONO_COUNTER_ULONG:
540 if ((counter->type & MONO_COUNTER_UNIT_MASK) == MONO_COUNTER_TIME)
541 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(guint64*)buffer) / 10000.0);
543 fprintf (outfile, ENTRY_FMT "%llu\n", counter->name, *(unsigned long long *)buffer);
545 case MONO_COUNTER_WORD:
546 fprintf (outfile, ENTRY_FMT "%zd\n", counter->name, *(gssize*)buffer);
548 case MONO_COUNTER_DOUBLE:
549 fprintf (outfile, ENTRY_FMT "%.4f\n", counter->name, *(double*)buffer);
551 case MONO_COUNTER_STRING:
552 fprintf (outfile, ENTRY_FMT "%s\n", counter->name, (size == 0) ? "(null)" : (char*)buffer);
554 case MONO_COUNTER_TIME_INTERVAL:
555 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(gint64*)buffer) / 1000.0);
563 section_names [][10] = {
574 mono_counters_dump_section (int section, int variance, FILE *outfile)
576 MonoCounter *counter = counters;
578 if ((counter->type & section) && (mono_counter_get_variance (counter) & variance))
579 dump_counter (counter, outfile);
580 counter = counter->next;
585 * mono_counters_dump:
586 * @section_mask: The sections to dump counters for
587 * @outfile: a FILE to dump the results to
589 * Displays the counts of all the enabled counters registered.
590 * To filter by variance, you can OR one or more variance with the specific section you want.
591 * Use MONO_COUNTER_SECTION_MASK to dump all categories of a specific variance.
594 mono_counters_dump (int section_mask, FILE *outfile)
598 section_mask &= valid_mask;
603 mono_os_mutex_lock (&counters_mutex);
606 mono_os_mutex_unlock (&counters_mutex);
610 variance = section_mask & MONO_COUNTER_VARIANCE_MASK;
612 /* If no variance mask is supplied, we default to all kinds. */
614 variance = MONO_COUNTER_VARIANCE_MASK;
615 section_mask &= ~MONO_COUNTER_VARIANCE_MASK;
617 for (j = 0, i = MONO_COUNTER_JIT; i < MONO_COUNTER_LAST_SECTION; j++, i <<= 1) {
618 if ((section_mask & i) && (set_mask & i)) {
619 fprintf (outfile, "\n%s statistics\n", section_names [j]);
620 mono_counters_dump_section (i, variance, outfile);
625 mono_os_mutex_unlock (&counters_mutex);
629 * mono_counters_cleanup:
631 * Perform any needed cleanup at process exit.
634 mono_counters_cleanup (void)
636 MonoCounter *counter;
641 mono_os_mutex_lock (&counters_mutex);
646 MonoCounter *tmp = counter;
647 counter = counter->next;
648 free ((void*)tmp->name);
652 mono_os_mutex_unlock (&counters_mutex);
655 static MonoResourceCallback limit_reached = NULL;
656 static uintptr_t resource_limits [MONO_RESOURCE_COUNT * 2];
659 * mono_runtime_resource_check_limit:
660 * @resource_type: one of the #MonoResourceType enum values
661 * @value: the current value of the resource usage
663 * Check if a runtime resource limit has been reached. This function
664 * is intended to be used by the runtime only.
667 mono_runtime_resource_check_limit (int resource_type, uintptr_t value)
671 /* check the hard limit first */
672 if (value > resource_limits [resource_type * 2 + 1]) {
673 limit_reached (resource_type, value, 0);
676 if (value > resource_limits [resource_type * 2])
677 limit_reached (resource_type, value, 1);
681 * mono_runtime_resource_limit:
682 * @resource_type: one of the #MonoResourceType enum values
683 * @soft_limit: the soft limit value
684 * @hard_limit: the hard limit value
686 * This function sets the soft and hard limit for runtime resources. When the limit
687 * is reached, a user-specified callback is called. The callback runs in a restricted
688 * environment, in which the world coult be stopped, so it can't take locks, perform
689 * allocations etc. The callback may be called multiple times once a limit has been reached
690 * if action is not taken to decrease the resource use.
692 * Returns: 0 on error or a positive integer otherwise.
695 mono_runtime_resource_limit (int resource_type, uintptr_t soft_limit, uintptr_t hard_limit)
697 if (resource_type >= MONO_RESOURCE_COUNT || resource_type < 0)
699 if (soft_limit > hard_limit)
701 resource_limits [resource_type * 2] = soft_limit;
702 resource_limits [resource_type * 2 + 1] = hard_limit;
707 * mono_runtime_resource_set_callback:
708 * @callback: a function pointer
710 * Set the callback to be invoked when a resource limit is reached.
711 * The callback will receive the resource type, the resource amount in resource-specific
712 * units and a flag indicating whether the soft or hard limit was reached.
715 mono_runtime_resource_set_callback (MonoResourceCallback callback)
717 limit_reached = callback;