3 * Copyright 2006-2010 Novell
4 * Copyright 2011 Xamarin Inc
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #include "mono-counters.h"
12 #include "mono-proclib.h"
13 #include "mono-os-mutex.h"
27 static MonoCounter *counters = NULL;
28 static mono_mutex_t counters_mutex;
30 static volatile gboolean initialized = FALSE;
32 static int valid_mask = 0;
33 static int set_mask = 0;
35 static GSList *register_callbacks = NULL;
37 static void initialize_system_counters (void);
40 * mono_counter_get_variance:
41 * \param counter counter to get the variance
43 * Variance specifies how the counter value is expected to behave between any two samplings.
45 * \returns the monotonicity of the counter.
48 mono_counter_get_variance (MonoCounter *counter)
50 return counter->type & MONO_COUNTER_VARIANCE_MASK;
54 * mono_counter_get_unit:
55 * \param counter counter to get the unit
57 * The unit gives a high level view of the unit that the counter is measuring.
59 * \returns the unit of the counter.
62 mono_counter_get_unit (MonoCounter *counter)
64 return counter->type & MONO_COUNTER_UNIT_MASK;
68 * mono_counter_get_section:
69 * \param counter counter to get the section
70 * 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 * \param counter counter to get the type
83 * \returns the type used to store the value of the counter.
86 mono_counter_get_type (MonoCounter *counter)
88 return counter->type & MONO_COUNTER_TYPE_MASK;
92 * mono_counter_get_name:
93 * \param counter counter to get the name
94 * \returns the counter name. The string should not be freed.
98 mono_counter_get_name (MonoCounter *counter)
100 return counter->name;
104 * mono_counter_get_size:
105 * \param counter counter to get the max size of the counter
106 * Use the returned size to create the buffer used with \c mono_counters_sample
107 * \returns the max size of the counter data.
110 mono_counter_get_size (MonoCounter *counter)
112 return counter->size;
116 * mono_counters_enable:
117 * \param sectionmask a mask listing the sections that will be displayed
118 * This is used to track which counters will be displayed.
121 mono_counters_enable (int section_mask)
123 valid_mask = section_mask & MONO_COUNTER_SECTION_MASK;
127 mono_counters_init (void)
132 mono_os_mutex_init (&counters_mutex);
134 initialize_system_counters ();
140 register_internal (const char *name, int type, void *addr, int size)
142 MonoCounter *counter;
143 GSList *register_callback;
145 g_assert (size >= 0);
146 if ((type & MONO_COUNTER_VARIANCE_MASK) == 0)
147 type |= MONO_COUNTER_MONOTONIC;
149 mono_os_mutex_lock (&counters_mutex);
151 for (counter = counters; counter; counter = counter->next) {
152 if (counter->addr == addr) {
153 g_warning ("you are registering twice the same counter address");
154 mono_os_mutex_unlock (&counters_mutex);
159 counter = (MonoCounter *) g_malloc (sizeof (MonoCounter));
161 mono_os_mutex_unlock (&counters_mutex);
164 counter->name = g_strdup (name);
165 counter->type = type;
166 counter->addr = addr;
167 counter->next = NULL;
168 counter->size = size;
174 MonoCounter *item = counters;
177 item->next = counter;
182 for (register_callback = register_callbacks; register_callback; register_callback = register_callback->next)
183 ((MonoCounterRegisterCallback)register_callback->data) (counter);
185 mono_os_mutex_unlock (&counters_mutex);
189 * mono_counters_register:
190 * \param name The name for this counters.
191 * \param type One of the possible \c MONO_COUNTER types, or \c MONO_COUNTER_CALLBACK for a function pointer.
192 * \param addr The address to register.
194 * Register \p addr as the address of a counter of type type.
195 * Note that \p name must be a valid string at all times until
196 * \c mono_counters_dump() is called.
198 * This function should not be used with counter types that require an explicit size such as string
199 * as the counter size will be set to zero making them effectively useless.
201 * It may be a function pointer if \c MONO_COUNTER_CALLBACK is specified:
202 * the function should return the value and take no arguments.
205 mono_counters_register (const char* name, int type, void *addr)
208 switch (type & MONO_COUNTER_TYPE_MASK) {
209 case MONO_COUNTER_INT:
212 case MONO_COUNTER_UINT:
213 size = sizeof (guint);
215 case MONO_COUNTER_LONG:
216 case MONO_COUNTER_TIME_INTERVAL:
217 size = sizeof (gint64);
219 case MONO_COUNTER_ULONG:
220 size = sizeof (guint64);
222 case MONO_COUNTER_WORD:
223 size = sizeof (gssize);
225 case MONO_COUNTER_DOUBLE:
226 size = sizeof (double);
228 case MONO_COUNTER_STRING:
232 g_assert_not_reached ();
236 g_debug ("counters not enabled");
238 register_internal (name, type, addr, size);
242 * mono_counters_register_with_size:
243 * \param name The name for this counters.
244 * \param type One of the possible MONO_COUNTER types, or MONO_COUNTER_CALLBACK for a function pointer.
245 * \param addr The address to register.
246 * \param size Max size of the counter data.
248 * Register \p addr as the address of a counter of type \p type.
249 * Note that \p name must be a valid string at all times until
250 * \c mono_counters_dump() is called.
252 * It may be a function pointer if \c MONO_COUNTER_CALLBACK is specified:
253 * the function should return the value and take no arguments.
255 * The value of \p size is ignored for types with fixed size such as int and long.
257 * Use \p size for types that can have dynamic size such as string.
259 * If \p size is negative, it's silently converted to zero.
262 mono_counters_register_with_size (const char *name, int type, void *addr, int size)
265 g_debug ("counters not enabled");
267 register_internal (name, type, addr, size);
271 * mono_counters_on_register
272 * \param callback function to callback when a counter is registered
273 * Add a callback that is going to be called when a counter is registered
276 mono_counters_on_register (MonoCounterRegisterCallback callback)
279 g_debug ("counters not enabled");
283 mono_os_mutex_lock (&counters_mutex);
284 register_callbacks = g_slist_append (register_callbacks, (gpointer) callback);
285 mono_os_mutex_unlock (&counters_mutex);
288 typedef int (*IntFunc) (void);
289 typedef guint (*UIntFunc) (void);
290 typedef gint64 (*LongFunc) (void);
291 typedef guint64 (*ULongFunc) (void);
292 typedef gssize (*PtrFunc) (void);
293 typedef double (*DoubleFunc) (void);
294 typedef char* (*StrFunc) (void);
299 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_USER_TIME);
305 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_SYSTEM_TIME);
311 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_TOTAL_TIME);
317 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_WORKING_SET);
323 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_PRIVATE_BYTES);
329 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_VIRTUAL_BYTES);
335 return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_FAULTS);
339 // If cpu_load gets inlined on Windows then cpu_load_1min, cpu_load_5min and cpu_load_15min can be folded into a single function and that will
340 // cause a failure when registering counters since the same function address will be used by all three functions. Preventing this method from being inlined
341 // will make sure the registered callback functions remains unique.
348 #if defined(TARGET_WIN32)
349 #elif defined(TARGET_MACH)
351 if (getloadavg (load, 3) > 0)
354 char buffer[512], *b;
356 FILE *f = fopen ("/proc/loadavg", "r");
358 len = fread (buffer, 1, sizeof (buffer) - 1, f);
361 buffer [len < 511 ? len : 511] = 0;
363 for (i = 0; i < 3; i++) {
365 return strtod (b, NULL);
392 cpu_load_15min (void)
397 #define SYSCOUNTER_TIME (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_TIME | MONO_COUNTER_MONOTONIC | MONO_COUNTER_CALLBACK)
398 #define SYSCOUNTER_BYTES (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE | MONO_COUNTER_CALLBACK)
399 #define SYSCOUNTER_COUNT (MONO_COUNTER_SYSTEM | MONO_COUNTER_LONG | MONO_COUNTER_COUNT | MONO_COUNTER_MONOTONIC | MONO_COUNTER_CALLBACK)
400 #define SYSCOUNTER_LOAD (MONO_COUNTER_SYSTEM | MONO_COUNTER_DOUBLE | MONO_COUNTER_PERCENTAGE | MONO_COUNTER_VARIABLE | MONO_COUNTER_CALLBACK)
403 initialize_system_counters (void)
405 register_internal ("User Time", SYSCOUNTER_TIME, (gpointer) &user_time, sizeof (gint64));
406 register_internal ("System Time", SYSCOUNTER_TIME, (gpointer) &system_time, sizeof (gint64));
407 register_internal ("Total Time", SYSCOUNTER_TIME, (gpointer) &total_time, sizeof (gint64));
408 register_internal ("Working Set", SYSCOUNTER_BYTES, (gpointer) &working_set, sizeof (gint64));
409 register_internal ("Private Bytes", SYSCOUNTER_BYTES, (gpointer) &private_bytes, sizeof (gint64));
410 register_internal ("Virtual Bytes", SYSCOUNTER_BYTES, (gpointer) &virtual_bytes, sizeof (gint64));
411 register_internal ("Page Faults", SYSCOUNTER_COUNT, (gpointer) &page_faults, sizeof (gint64));
412 register_internal ("CPU Load Average - 1min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_1min, sizeof (double));
413 register_internal ("CPU Load Average - 5min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_5min, sizeof (double));
414 register_internal ("CPU Load Average - 15min", SYSCOUNTER_LOAD, (gpointer) &cpu_load_15min, sizeof (double));
418 * mono_counters_foreach:
419 * \param cb The callback that will be called for each counter.
420 * \param user_data Value passed as second argument of the callback.
421 * Iterate over all counters and call \p cb for each one of them. Stop iterating if
422 * the callback returns FALSE.
425 mono_counters_foreach (CountersEnumCallback cb, gpointer user_data)
427 MonoCounter *counter;
430 g_debug ("counters not enabled");
434 mono_os_mutex_lock (&counters_mutex);
436 for (counter = counters; counter; counter = counter->next) {
437 if (!cb (counter, user_data)) {
438 mono_os_mutex_unlock (&counters_mutex);
443 mono_os_mutex_unlock (&counters_mutex);
446 #define COPY_COUNTER(type,functype) do { \
447 size = sizeof (type); \
448 if (buffer_size < size) \
451 *(type*)buffer = cb ? ((functype)counter->addr) () : *(type*)counter->addr; \
456 sample_internal (MonoCounter *counter, void *buffer, int buffer_size)
458 int cb = counter->type & MONO_COUNTER_CALLBACK;
463 switch (mono_counter_get_type (counter)) {
464 case MONO_COUNTER_INT:
465 COPY_COUNTER (int, IntFunc);
467 case MONO_COUNTER_UINT:
468 COPY_COUNTER (guint, UIntFunc);
470 case MONO_COUNTER_LONG:
471 case MONO_COUNTER_TIME_INTERVAL:
472 COPY_COUNTER (gint64, LongFunc);
474 case MONO_COUNTER_ULONG:
475 COPY_COUNTER (guint64, ULongFunc);
477 case MONO_COUNTER_WORD:
478 COPY_COUNTER (gssize, PtrFunc);
480 case MONO_COUNTER_DOUBLE:
481 COPY_COUNTER (double, DoubleFunc);
483 case MONO_COUNTER_STRING:
484 if (buffer_size < counter->size) {
486 } else if (counter->size == 0) {
489 strval = cb ? ((StrFunc)counter->addr) () : (char*)counter->addr;
493 size = counter->size;
494 memcpy ((char *) buffer, strval, size - 1);
495 ((char*)buffer)[size - 1] = '\0';
504 mono_counters_sample (MonoCounter *counter, void *buffer, int buffer_size)
507 g_debug ("counters not enabled");
511 return sample_internal (counter, buffer, buffer_size);
514 #define ENTRY_FMT "%-36s: "
516 dump_counter (MonoCounter *counter, FILE *outfile) {
517 void *buffer = g_malloc0 (counter->size);
518 int size = sample_internal (counter, buffer, counter->size);
520 switch (counter->type & MONO_COUNTER_TYPE_MASK) {
521 case MONO_COUNTER_INT:
522 fprintf (outfile, ENTRY_FMT "%d\n", counter->name, *(int*)buffer);
524 case MONO_COUNTER_UINT:
525 fprintf (outfile, ENTRY_FMT "%u\n", counter->name, *(guint*)buffer);
527 case MONO_COUNTER_LONG:
528 if ((counter->type & MONO_COUNTER_UNIT_MASK) == MONO_COUNTER_TIME)
529 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(gint64*)buffer) / 10000.0);
531 fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, *(long long *)buffer);
533 case MONO_COUNTER_ULONG:
534 if ((counter->type & MONO_COUNTER_UNIT_MASK) == MONO_COUNTER_TIME)
535 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(guint64*)buffer) / 10000.0);
537 fprintf (outfile, ENTRY_FMT "%llu\n", counter->name, *(unsigned long long *)buffer);
539 case MONO_COUNTER_WORD:
540 fprintf (outfile, ENTRY_FMT "%zd\n", counter->name, *(gssize*)buffer);
542 case MONO_COUNTER_DOUBLE:
543 fprintf (outfile, ENTRY_FMT "%.4f\n", counter->name, *(double*)buffer);
545 case MONO_COUNTER_STRING:
546 fprintf (outfile, ENTRY_FMT "%s\n", counter->name, (size == 0) ? "(null)" : (char*)buffer);
548 case MONO_COUNTER_TIME_INTERVAL:
549 fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)(*(gint64*)buffer) / 1000.0);
557 section_names [][12] = {
565 "", // MONO_COUNTER_PERFCOUNTERS - not used.
570 mono_counters_dump_section (int section, int variance, FILE *outfile)
572 MonoCounter *counter = counters;
574 if ((counter->type & section) && (mono_counter_get_variance (counter) & variance))
575 dump_counter (counter, outfile);
576 counter = counter->next;
581 * mono_counters_dump:
582 * \param section_mask The sections to dump counters for
583 * \param outfile a FILE to dump the results to
584 * Displays the counts of all the enabled counters registered.
585 * To filter by variance, you can OR one or more variance with the specific section you want.
586 * Use \c MONO_COUNTER_SECTION_MASK to dump all categories of a specific variance.
589 mono_counters_dump (int section_mask, FILE *outfile)
593 section_mask &= valid_mask;
598 mono_os_mutex_lock (&counters_mutex);
601 mono_os_mutex_unlock (&counters_mutex);
605 variance = section_mask & MONO_COUNTER_VARIANCE_MASK;
607 /* If no variance mask is supplied, we default to all kinds. */
609 variance = MONO_COUNTER_VARIANCE_MASK;
610 section_mask &= ~MONO_COUNTER_VARIANCE_MASK;
612 for (j = 0, i = MONO_COUNTER_JIT; i < MONO_COUNTER_LAST_SECTION; j++, i <<= 1) {
613 if ((section_mask & i) && (set_mask & i)) {
614 fprintf (outfile, "\n%s statistics\n", section_names [j]);
615 mono_counters_dump_section (i, variance, outfile);
620 mono_os_mutex_unlock (&counters_mutex);
624 * mono_counters_cleanup:
626 * Perform any needed cleanup at process exit.
629 mono_counters_cleanup (void)
631 MonoCounter *counter;
636 mono_os_mutex_lock (&counters_mutex);
641 MonoCounter *tmp = counter;
642 counter = counter->next;
643 g_free ((void*)tmp->name);
647 mono_os_mutex_unlock (&counters_mutex);
650 static MonoResourceCallback limit_reached = NULL;
651 static uintptr_t resource_limits [MONO_RESOURCE_COUNT * 2];
654 * mono_runtime_resource_check_limit:
655 * \param resource_type one of the \c MonoResourceType enum values
656 * \param value the current value of the resource usage
657 * Check if a runtime resource limit has been reached. This function
658 * is intended to be used by the runtime only.
661 mono_runtime_resource_check_limit (int resource_type, uintptr_t value)
665 /* check the hard limit first */
666 if (value > resource_limits [resource_type * 2 + 1]) {
667 limit_reached (resource_type, value, 0);
670 if (value > resource_limits [resource_type * 2])
671 limit_reached (resource_type, value, 1);
675 * mono_runtime_resource_limit:
676 * \param resource_type one of the \c MonoResourceType enum values
677 * \param soft_limit the soft limit value
678 * \param hard_limit the hard limit value
679 * This function sets the soft and hard limit for runtime resources. When the limit
680 * is reached, a user-specified callback is called. The callback runs in a restricted
681 * environment, in which the world coult be stopped, so it can't take locks, perform
682 * allocations etc. The callback may be called multiple times once a limit has been reached
683 * if action is not taken to decrease the resource use.
684 * \returns 0 on error or a positive integer otherwise.
687 mono_runtime_resource_limit (int resource_type, uintptr_t soft_limit, uintptr_t hard_limit)
689 if (resource_type >= MONO_RESOURCE_COUNT || resource_type < 0)
691 if (soft_limit > hard_limit)
693 resource_limits [resource_type * 2] = soft_limit;
694 resource_limits [resource_type * 2 + 1] = hard_limit;
699 * mono_runtime_resource_set_callback:
700 * \param callback a function pointer
701 * Set the callback to be invoked when a resource limit is reached.
702 * The callback will receive the resource type, the resource amount in resource-specific
703 * units and a flag indicating whether the soft or hard limit was reached.
706 mono_runtime_resource_set_callback (MonoResourceCallback callback)
708 limit_reached = callback;