4 * Performance counters support.
6 * Author: Paolo Molaro (lupus@ximian.com)
8 * Copyright 2008-2009 Novell, Inc (http://www.novell.com)
19 #if defined (__OpenBSD__)
20 #include <sys/param.h>
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_TIME_H
28 #if defined (__NetBSD__) || defined (__APPLE__)
29 #include <sys/sysctl.h>
31 #include "metadata/mono-perfcounters.h"
32 #include "metadata/appdomain.h"
33 #include "metadata/object-internals.h"
35 #include "metadata/class-internals.h"
36 #include "utils/mono-time.h"
37 #include "utils/mono-mmap.h"
38 #include "utils/mono-proclib.h"
39 #include "utils/mono-networkinterfaces.h"
40 #include "utils/mono-error-internals.h"
41 #include "utils/atomic.h"
42 #include <mono/io-layer/io-layer.h>
44 /* map of CounterSample.cs */
45 struct _MonoCounterSample {
48 gint64 counterFrequency;
49 gint64 systemFrequency;
51 gint64 timeStamp100nSec;
52 gint64 counterTimeStamp;
56 #ifndef DISABLE_PERFCOUNTERS
57 /* map of PerformanceCounterType.cs */
59 NumberOfItemsHEX32=0x00000000,
60 NumberOfItemsHEX64=0x00000100,
61 NumberOfItems32=0x00010000,
62 NumberOfItems64=0x00010100,
63 CounterDelta32=0x00400400,
64 CounterDelta64=0x00400500,
65 SampleCounter=0x00410400,
66 CountPerTimeInterval32=0x00450400,
67 CountPerTimeInterval64=0x00450500,
68 RateOfCountsPerSecond32=0x10410400,
69 RateOfCountsPerSecond64=0x10410500,
70 RawFraction=0x20020400,
71 CounterTimer=0x20410500,
72 Timer100Ns=0x20510500,
73 SampleFraction=0x20C20400,
74 CounterTimerInverse=0x21410500,
75 Timer100NsInverse=0x21510500,
76 CounterMultiTimer=0x22410500,
77 CounterMultiTimer100Ns=0x22510500,
78 CounterMultiTimerInverse=0x23410500,
79 CounterMultiTimer100NsInverse=0x23510500,
80 AverageTimer32=0x30020400,
81 ElapsedTime=0x30240500,
82 AverageCount64=0x40020500,
83 SampleBase=0x40030401,
84 AverageBase=0x40030402,
86 CounterMultiBase=0x42030500
89 /* maps a small integer type to the counter types above */
91 simple_type_to_type [] = {
92 NumberOfItemsHEX32, NumberOfItemsHEX64,
93 NumberOfItems32, NumberOfItems64,
94 CounterDelta32, CounterDelta64,
95 SampleCounter, CountPerTimeInterval32,
96 CountPerTimeInterval64, RateOfCountsPerSecond32,
97 RateOfCountsPerSecond64, RawFraction,
98 CounterTimer, Timer100Ns,
99 SampleFraction, CounterTimerInverse,
100 Timer100NsInverse, CounterMultiTimer,
101 CounterMultiTimer100Ns, CounterMultiTimerInverse,
102 CounterMultiTimer100NsInverse, AverageTimer32,
103 ElapsedTime, AverageCount64,
104 SampleBase, AverageBase,
105 RawBase, CounterMultiBase
119 NetworkInterfaceInstance,
123 #define PERFCTR_CAT(id,name,help,type,inst,first_counter) CATEGORY_ ## id,
124 #define PERFCTR_COUNTER(id,name,help,type,field)
126 #include "mono-perfcounters-def.h"
131 #undef PERFCTR_COUNTER
132 #define PERFCTR_CAT(id,name,help,type,inst,first_counter) CATEGORY_START_ ## id = -1,
133 #define PERFCTR_COUNTER(id,name,help,type,field) COUNTER_ ## id,
134 /* each counter is assigned an id starting from 0 inside the category */
136 #include "mono-perfcounters-def.h"
141 #undef PERFCTR_COUNTER
142 #define PERFCTR_CAT(id,name,help,type,inst,first_counter)
143 #define PERFCTR_COUNTER(id,name,help,type,field) CCOUNTER_ ## id,
144 /* this is used just to count the number of counters */
146 #include "mono-perfcounters-def.h"
150 static mono_mutex_t perfctr_mutex;
151 #define perfctr_lock() mono_mutex_lock (&perfctr_mutex)
152 #define perfctr_unlock() mono_mutex_unlock (&perfctr_mutex)
157 unsigned short counters_start;
158 unsigned short counters_size;
159 unsigned short data_start;
160 MonoPerfCounters counters;
165 binary format of custom counters in shared memory, starting from MonoSharedArea* + data_start;
167 struct stanza_header {
168 byte stanza_type; // FTYPE_*
170 ushort stanza_length; // includeas header
175 // perfcat and perfinstance are 4-bytes aligned
179 ushort length; // includes the counters
181 ushort counters_data_size;
183 char name[]; // null terminated
184 char help[]; // null terminated
185 // perfcounters follow
188 char name[]; // null terminated
189 char help[]; // null terminated
194 struct perfinstance {
196 byte data_offset; // offset of counters from beginning of struct
198 uint category_offset; // offset of category in the shared area
199 char name[]; // null terminated
200 // data follows: this is always 8-byte aligned
206 FTYPE_CATEGORY = 'C',
208 FTYPE_PREDEF_INSTANCE = 'P', // an instance of a predef counter
209 FTYPE_INSTANCE = 'I',
222 unsigned short num_counters;
223 unsigned short counters_data_size;
225 /* variable length data follows */
231 unsigned int category_offset;
232 /* variable length data follows */
233 char instance_name [1];
239 /* variable length data follows */
248 unsigned int instance_type : 6;
256 unsigned short offset; // offset inside MonoPerfCounters
261 #undef PERFCTR_COUNTER
262 #define PERFCTR_CAT(id,name,help,type,inst,first_counter) {name, help, CATEGORY_ ## id, type, inst ## Instance, CCOUNTER_ ## first_counter},
263 #define PERFCTR_COUNTER(id,name,help,type,field)
264 static const CategoryDesc
265 predef_categories [] = {
266 #include "mono-perfcounters-def.h"
267 {NULL, NULL, NUM_CATEGORIES, -1, 0, NUM_COUNTERS}
271 #undef PERFCTR_COUNTER
272 #define PERFCTR_CAT(id,name,help,type,inst,first_counter)
273 #define PERFCTR_COUNTER(id,name,help,type,field) {name, help, COUNTER_ ## id, G_STRUCT_OFFSET (MonoPerfCounters, field), type},
274 static const CounterDesc
275 predef_counters [] = {
276 #include "mono-perfcounters-def.h"
277 {NULL, NULL, -1, 0, 0}
281 * We have several different classes of counters:
283 * *) runtime counters
285 * *) user-defined counters
286 * *) windows counters (the implementation on windows will use this)
288 * To easily handle the differences we create a vtable for each class that contains the
289 * function pointers with the actual implementation to access the counters.
291 typedef struct _ImplVtable ImplVtable;
293 typedef MonoBoolean (*SampleFunc) (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample* sample);
294 typedef gint64 (*UpdateFunc) (ImplVtable *vtable, MonoBoolean do_incr, gint64 value);
295 typedef void (*CleanupFunc) (ImplVtable *vtable);
311 MonoPerfCounters *counters;
317 SharedInstance *instance_desc;
318 SharedCounter *counter_desc;
322 create_vtable (void *arg, SampleFunc sample, UpdateFunc update)
324 ImplVtable *vtable = g_new0 (ImplVtable, 1);
326 vtable->sample = sample;
327 vtable->update = update;
331 MonoPerfCounters *mono_perfcounters = NULL;
332 static MonoSharedArea *shared_area = NULL;
339 /* maps a pid to a ExternalSArea pointer */
340 static GHashTable *pid_to_shared_area = NULL;
342 static MonoSharedArea *
343 load_sarea_for_pid (int pid)
346 MonoSharedArea *area = NULL;
349 if (pid_to_shared_area == NULL)
350 pid_to_shared_area = g_hash_table_new (NULL, NULL);
351 data = g_hash_table_lookup (pid_to_shared_area, GINT_TO_POINTER (pid));
353 area = mono_shared_area_for_pid (GINT_TO_POINTER (pid));
355 data = g_new (ExternalSArea, 1);
358 g_hash_table_insert (pid_to_shared_area, GINT_TO_POINTER (pid), data);
369 unref_pid_unlocked (int pid)
372 data = g_hash_table_lookup (pid_to_shared_area, GINT_TO_POINTER (pid));
375 if (!data->refcount) {
376 g_hash_table_remove (pid_to_shared_area, GINT_TO_POINTER (pid));
377 mono_shared_area_unload (data->sarea);
384 predef_cleanup (ImplVtable *vtable)
386 PredefVtable *vt = (PredefVtable*)vtable;
387 /* ExternalSArea *data; */
390 if (!pid_to_shared_area) {
394 unref_pid_unlocked (vt->pid);
399 mono_determine_physical_ram_size (void)
401 #if defined (TARGET_WIN32)
402 MEMORYSTATUSEX memstat;
404 memstat.dwLength = sizeof (memstat);
405 GlobalMemoryStatusEx (&memstat);
406 return (guint64)memstat.ullTotalPhys;
407 #elif defined (__NetBSD__) || defined (__APPLE__)
421 size_t size_sys = sizeof (value);
423 sysctl (mib, 2, &value, &size_sys, NULL, 0);
427 return (guint64)value;
428 #elif defined (HAVE_SYSCONF)
429 guint64 page_size = 0, num_pages = 0;
431 /* sysconf works on most *NIX operating systems, if your system doesn't have it or if it
432 * reports invalid values, please add your OS specific code below. */
434 page_size = (guint64)sysconf (_SC_PAGESIZE);
437 #ifdef _SC_PHYS_PAGES
438 num_pages = (guint64)sysconf (_SC_PHYS_PAGES);
441 if (!page_size || !num_pages) {
442 g_warning ("Your operating system's sysconf (3) function doesn't correctly report physical memory size!");
446 return page_size * num_pages;
453 mono_perfcounters_init (void)
455 int d_offset = G_STRUCT_OFFSET (MonoSharedArea, data);
459 mono_mutex_init_recursive (&perfctr_mutex);
461 shared_area = mono_shared_area ();
462 shared_area->counters_start = G_STRUCT_OFFSET (MonoSharedArea, counters);
463 shared_area->counters_size = sizeof (MonoPerfCounters);
464 shared_area->data_start = d_offset;
465 shared_area->size = 4096;
466 mono_perfcounters = &shared_area->counters;
470 perfctr_type_compress (int type)
473 for (i = 0; i < G_N_ELEMENTS (simple_type_to_type); ++i) {
474 if (simple_type_to_type [i] == type)
477 /* NumberOfItems32 */
481 static unsigned char*
482 shared_data_find_room (int size)
484 unsigned char *p = (unsigned char *)shared_area + shared_area->data_start;
485 unsigned char *end = (unsigned char *)shared_area + shared_area->size;
490 unsigned short *next;
491 if (*p == FTYPE_END) {
492 if (size < (end - p))
498 next = (unsigned short*)(p + 2);
499 if (*p == FTYPE_DELETED) {
500 /* we reuse only if it's the same size */
510 typedef gboolean (*SharedFunc) (SharedHeader *header, void *data);
513 foreach_shared_item_in_area (unsigned char *p, unsigned char *end, SharedFunc func, void *data)
516 unsigned short *next;
519 next = (unsigned short*)(p + 2);
520 if (!func ((SharedHeader*)p, data))
529 foreach_shared_item (SharedFunc func, void *data)
531 unsigned char *p = (unsigned char *)shared_area + shared_area->data_start;
532 unsigned char *end = (unsigned char *)shared_area + shared_area->size;
534 foreach_shared_item_in_area (p, end, func, data);
538 mono_string_compare_ascii (MonoString *str, const char *ascii_str)
540 /* FIXME: make this case insensitive */
541 guint16 *strc = mono_string_chars (str);
542 while (*strc == *ascii_str++) {
547 return *strc - *(const unsigned char *)(ascii_str - 1);
556 category_search (SharedHeader *header, void *data)
558 CatSearch *search = data;
559 if (header->ftype == FTYPE_CATEGORY) {
560 SharedCategory *cat = (SharedCategory*)header;
561 if (mono_string_compare_ascii (search->name, cat->name) == 0) {
569 static SharedCategory*
570 find_custom_category (MonoString *name)
575 foreach_shared_item (category_search, &search);
580 category_collect (SharedHeader *header, void *data)
582 GSList **list = data;
583 if (header->ftype == FTYPE_CATEGORY) {
584 *list = g_slist_prepend (*list, header);
590 get_custom_categories (void) {
592 foreach_shared_item (category_collect, &list);
597 custom_category_counters (SharedCategory* cat)
599 char *p = cat->name + strlen (cat->name) + 1;
600 p += strlen (p) + 1; /* skip category help */
604 static SharedCounter*
605 find_custom_counter (SharedCategory* cat, MonoString *name)
608 char *p = custom_category_counters (cat);
609 for (i = 0; i < cat->num_counters; ++i) {
610 SharedCounter *counter = (SharedCounter*)p;
611 if (mono_string_compare_ascii (name, counter->name) == 0)
613 p += 1 + strlen (p + 1) + 1; /* skip counter type and name */
614 p += strlen (p) + 1; /* skip counter help */
620 unsigned int cat_offset;
622 MonoString *instance;
623 SharedInstance* result;
628 instance_search (SharedHeader *header, void *data)
630 InstanceSearch *search = data;
631 if (header->ftype == FTYPE_INSTANCE) {
632 SharedInstance *ins = (SharedInstance*)header;
633 if (search->cat_offset == ins->category_offset) {
634 if (search->instance) {
635 if (mono_string_compare_ascii (search->instance, ins->instance_name) == 0) {
636 search->result = ins;
640 search->list = g_slist_prepend (search->list, ins);
647 static SharedInstance*
648 find_custom_instance (SharedCategory* cat, MonoString *instance)
650 InstanceSearch search;
651 search.cat_offset = (char*)cat - (char*)shared_area;
653 search.instance = instance;
655 search.result = NULL;
656 foreach_shared_item (instance_search, &search);
657 return search.result;
661 get_custom_instances_list (SharedCategory* cat)
663 InstanceSearch search;
664 search.cat_offset = (char*)cat - (char*)shared_area;
666 search.instance = NULL;
668 search.result = NULL;
669 foreach_shared_item (instance_search, &search);
674 custom_category_help (SharedCategory* cat)
676 return cat->name + strlen (cat->name) + 1;
679 static const CounterDesc*
680 get_counter_in_category (const CategoryDesc *desc, MonoString *counter)
682 const CounterDesc *cdesc = &predef_counters [desc->first_counter];
683 const CounterDesc *end = &predef_counters [desc [1].first_counter];
684 for (; cdesc < end; ++cdesc) {
685 if (mono_string_compare_ascii (counter, cdesc->name) == 0)
691 /* fill the info in sample (except the raw value) */
693 fill_sample (MonoCounterSample *sample)
695 sample->timeStamp = mono_100ns_ticks ();
696 sample->timeStamp100nSec = sample->timeStamp;
697 sample->counterTimeStamp = sample->timeStamp;
698 sample->counterFrequency = 10000000;
699 sample->systemFrequency = 10000000;
700 // the real basevalue needs to be get from a different counter...
701 sample->baseValue = 0;
705 id_from_string (MonoString *instance, gboolean is_process)
708 if (mono_string_length (instance)) {
709 char *id_str = mono_string_to_utf8 (instance);
711 id = strtol (id_str, &end, 0);
712 if (end == id_str && !is_process)
720 get_cpu_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
722 MonoProcessError error;
723 int id = GPOINTER_TO_INT (vtable->arg);
727 fill_sample (sample);
728 sample->baseValue = 1;
730 sample->counterType = predef_counters [predef_categories [CATEGORY_CPU].first_counter + id].type;
732 case COUNTER_CPU_USER_TIME:
733 sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_USER_TIME, &error);
735 case COUNTER_CPU_PRIV_TIME:
736 sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_PRIV_TIME, &error);
738 case COUNTER_CPU_INTR_TIME:
739 sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_INTR_TIME, &error);
741 case COUNTER_CPU_DCP_TIME:
742 sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_DCP_TIME, &error);
744 case COUNTER_CPU_PROC_TIME:
745 sample->rawValue = mono_cpu_get_data (pid, MONO_CPU_IDLE_TIME, &error);
752 cpu_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
754 int id = id_from_string (instance, FALSE) << 5;
755 const CounterDesc *cdesc;
757 /* increase the shift above and the mask also in the implementation functions */
758 //g_assert (32 > desc [1].first_counter - desc->first_counter);
759 if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_CPU], counter))) {
761 return create_vtable (GINT_TO_POINTER (id | cdesc->id), get_cpu_counter, NULL);
767 get_network_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
769 MonoNetworkError error = MONO_NETWORK_ERROR_OTHER;
770 NetworkVtableArg *narg = (NetworkVtableArg*) vtable->arg;
772 fill_sample (sample);
775 sample->counterType = predef_counters [predef_categories [CATEGORY_NETWORK].first_counter + narg->id].type;
777 case COUNTER_NETWORK_BYTESRECSEC:
778 sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESREC, &error);
780 case COUNTER_NETWORK_BYTESSENTSEC:
781 sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESSENT, &error);
783 case COUNTER_NETWORK_BYTESTOTALSEC:
784 sample->rawValue = mono_network_get_data (narg->name, MONO_NETWORK_BYTESTOTAL, &error);
788 if (error == MONO_NETWORK_ERROR_NONE)
795 network_cleanup (ImplVtable *vtable)
797 NetworkVtableArg *narg;
813 network_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
815 const CounterDesc *cdesc;
816 NetworkVtableArg *narg;
821 if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_NETWORK], counter))) {
822 instance_name = mono_string_to_utf8 (instance);
823 narg = g_new0 (NetworkVtableArg, 1);
824 narg->id = cdesc->id;
825 narg->name = instance_name;
827 vtable = create_vtable (narg, get_network_counter, NULL);
828 vtable->cleanup = network_cleanup;
835 get_process_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
837 int id = GPOINTER_TO_INT (vtable->arg);
843 fill_sample (sample);
844 sample->baseValue = 1;
846 sample->counterType = predef_counters [predef_categories [CATEGORY_PROC].first_counter + id].type;
848 case COUNTER_PROC_USER_TIME:
849 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_USER_TIME);
851 case COUNTER_PROC_PRIV_TIME:
852 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_SYSTEM_TIME);
854 case COUNTER_PROC_PROC_TIME:
855 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_TOTAL_TIME);
857 case COUNTER_PROC_THREADS:
858 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_NUM_THREADS);
860 case COUNTER_PROC_VBYTES:
861 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_VIRTUAL_BYTES);
863 case COUNTER_PROC_WSET:
864 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_WORKING_SET);
866 case COUNTER_PROC_PBYTES:
867 sample->rawValue = mono_process_get_data (GINT_TO_POINTER (pid), MONO_PROCESS_PRIVATE_BYTES);
874 process_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
876 int id = id_from_string (instance, TRUE) << 5;
877 const CounterDesc *cdesc;
879 /* increase the shift above and the mask also in the implementation functions */
880 //g_assert (32 > desc [1].first_counter - desc->first_counter);
881 if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_PROC], counter))) {
883 return create_vtable (GINT_TO_POINTER (id | cdesc->id), get_process_counter, NULL);
889 mono_mem_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
891 int id = GPOINTER_TO_INT (vtable->arg);
893 fill_sample (sample);
894 sample->baseValue = 1;
896 sample->counterType = predef_counters [predef_categories [CATEGORY_MONO_MEM].first_counter + id].type;
898 case COUNTER_MEM_NUM_OBJECTS:
899 sample->rawValue = mono_stats.new_object_count;
901 case COUNTER_MEM_PHYS_TOTAL:
902 sample->rawValue = mono_determine_physical_ram_size ();;
909 mono_mem_get_impl (MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
911 const CounterDesc *cdesc;
913 if ((cdesc = get_counter_in_category (&predef_categories [CATEGORY_MONO_MEM], counter))) {
915 return create_vtable (GINT_TO_POINTER ((gint) cdesc->id), mono_mem_counter, NULL);
921 predef_readonly_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
923 PredefVtable *vt = (PredefVtable *)vtable;
924 const CounterDesc *desc;
925 int cat_id = GPOINTER_TO_INT (vtable->arg);
926 int id = cat_id >> 16;
929 fill_sample (sample);
930 sample->baseValue = 1;
932 desc = &predef_counters [predef_categories [cat_id].first_counter + id];
933 sample->counterType = desc->type;
934 /* FIXME: check that the offset fits inside imported counters */
935 /*g_print ("loading %s at %d\n", desc->name, desc->offset);*/
936 sample->rawValue = *(guint32*)((char*)vt->counters + desc->offset);
941 predef_vtable (void *arg, MonoString *instance)
943 MonoSharedArea *area;
944 PredefVtable *vtable;
945 char *pids = mono_string_to_utf8 (instance);
950 area = load_sarea_for_pid (pid);
954 vtable = g_new (PredefVtable, 1);
955 vtable->vtable.arg = arg;
956 vtable->vtable.sample = predef_readonly_counter;
957 vtable->vtable.cleanup = predef_cleanup;
958 vtable->counters = (MonoPerfCounters*)((char*)area + area->counters_start);
961 return (ImplVtable*)vtable;
964 /* consider storing the pointer directly in vtable->arg, so the runtime overhead is lower:
965 * this needs some way to set sample->counterType as well, though.
968 predef_writable_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
970 int cat_id = GPOINTER_TO_INT (vtable->arg);
971 int id = cat_id >> 16;
974 fill_sample (sample);
975 sample->baseValue = 1;
977 sample->counterType = predef_counters [predef_categories [cat_id].first_counter + id].type;
981 case COUNTER_EXC_THROWN:
982 sample->rawValue = mono_perfcounters->exceptions_thrown;
986 case CATEGORY_ASPNET:
988 case COUNTER_ASPNET_REQ_Q:
989 sample->rawValue = mono_perfcounters->aspnet_requests_queued;
991 case COUNTER_ASPNET_REQ_TOTAL:
992 sample->rawValue = mono_perfcounters->aspnet_requests;
996 case CATEGORY_THREADPOOL:
998 case COUNTER_THREADPOOL_WORKITEMS:
999 sample->rawValue = mono_perfcounters->threadpool_workitems;
1001 case COUNTER_THREADPOOL_IOWORKITEMS:
1002 sample->rawValue = mono_perfcounters->threadpool_ioworkitems;
1004 case COUNTER_THREADPOOL_THREADS:
1005 sample->rawValue = mono_perfcounters->threadpool_threads;
1007 case COUNTER_THREADPOOL_IOTHREADS:
1008 sample->rawValue = mono_perfcounters->threadpool_iothreads;
1014 case COUNTER_JIT_BYTES:
1015 sample->rawValue = mono_perfcounters->jit_bytes;
1017 case COUNTER_JIT_METHODS:
1018 sample->rawValue = mono_perfcounters->jit_methods;
1020 case COUNTER_JIT_TIME:
1021 sample->rawValue = mono_perfcounters->jit_time;
1023 case COUNTER_JIT_BYTES_PSEC:
1024 sample->rawValue = mono_perfcounters->jit_bytes;
1026 case COUNTER_JIT_FAILURES:
1027 sample->rawValue = mono_perfcounters->jit_failures;
1036 predef_writable_update (ImplVtable *vtable, MonoBoolean do_incr, gint64 value)
1038 guint32 *volatile ptr = NULL;
1039 gint64 *volatile ptr64 = NULL;
1040 int cat_id = GPOINTER_TO_INT (vtable->arg);
1041 int id = cat_id >> 16;
1044 case CATEGORY_ASPNET:
1046 case COUNTER_ASPNET_REQ_Q: ptr = &mono_perfcounters->aspnet_requests_queued; break;
1047 case COUNTER_ASPNET_REQ_TOTAL: ptr = &mono_perfcounters->aspnet_requests; break;
1050 case CATEGORY_THREADPOOL:
1052 case COUNTER_THREADPOOL_WORKITEMS: ptr64 = (gint64 *) &mono_perfcounters->threadpool_workitems; break;
1053 case COUNTER_THREADPOOL_IOWORKITEMS: ptr64 = (gint64 *) &mono_perfcounters->threadpool_ioworkitems; break;
1054 case COUNTER_THREADPOOL_THREADS: ptr = &mono_perfcounters->threadpool_threads; break;
1055 case COUNTER_THREADPOOL_IOTHREADS: ptr = &mono_perfcounters->threadpool_iothreads; break;
1062 return InterlockedIncrement ((gint32 *) ptr); /* FIXME: sign */
1064 return InterlockedDecrement ((gint32 *) ptr); /* FIXME: sign */
1069 /* this can be non-atomic */
1074 /* FIXME: we need to do this atomically */
1075 /* No InterlockedIncrement64() yet */
1078 return InterlockedIncrement64 (ptr);
1080 return InterlockedDecrement64 (ptr);
1086 /* this can be non-atomic */
1094 predef_writable_get_impl (int cat, MonoString* counter, MonoString* instance, int *type, MonoBoolean *custom)
1096 const CounterDesc *cdesc;
1098 if ((cdesc = get_counter_in_category (&predef_categories [cat], counter))) {
1099 *type = cdesc->type;
1100 if (instance == NULL || mono_string_compare_ascii (instance, "") == 0)
1101 return create_vtable (GINT_TO_POINTER ((cdesc->id << 16) | cat), predef_writable_counter, predef_writable_update);
1103 return predef_vtable (GINT_TO_POINTER ((cdesc->id << 16) | cat), instance);
1109 custom_writable_counter (ImplVtable *vtable, MonoBoolean only_value, MonoCounterSample *sample)
1111 CustomVTable *counter_data = (CustomVTable *)vtable;
1113 fill_sample (sample);
1114 sample->baseValue = 1;
1116 sample->counterType = simple_type_to_type [counter_data->counter_desc->type];
1118 sample->rawValue = 0;
1120 sample->rawValue = *(guint64*)vtable->arg;
1125 custom_writable_update (ImplVtable *vtable, MonoBoolean do_incr, gint64 value)
1127 /* FIXME: check writability */
1128 guint64 *ptr = vtable->arg;
1131 /* FIXME: we need to do this atomically */
1135 /* this can be non-atomic */
1142 static SharedInstance*
1143 custom_get_instance (SharedCategory *cat, SharedCounter *scounter, MonoString* instance)
1145 SharedInstance* inst;
1148 int size, data_offset;
1150 inst = find_custom_instance (cat, instance);
1153 name = mono_string_to_utf8 (instance);
1154 size = sizeof (SharedInstance) + strlen (name);
1158 size += (sizeof (guint64) * cat->num_counters);
1160 ptr = shared_data_find_room (size);
1166 inst = (SharedInstance*)ptr;
1167 inst->header.extra = 0; /* data_offset could overflow here, so we leave this field unused */
1168 inst->header.size = size;
1169 inst->category_offset = (char*)cat - (char*)shared_area;
1170 cat->num_instances++;
1171 /* now copy the variable data */
1172 p = inst->instance_name;
1174 p += strlen (name) + 1;
1175 inst->header.ftype = FTYPE_INSTANCE;
1183 custom_vtable (SharedCounter *scounter, SharedInstance* inst, char *data)
1185 CustomVTable* vtable;
1186 vtable = g_new0 (CustomVTable, 1);
1187 vtable->vtable.arg = data;
1188 vtable->vtable.sample = custom_writable_counter;
1189 vtable->vtable.update = custom_writable_update;
1190 vtable->instance_desc = inst;
1191 vtable->counter_desc = scounter;
1193 return (ImplVtable*)vtable;
1197 custom_get_impl (SharedCategory *cat, MonoString* counter, MonoString* instance, int *type)
1199 SharedCounter *scounter;
1200 SharedInstance* inst;
1203 scounter = find_custom_counter (cat, counter);
1206 *type = simple_type_to_type [scounter->type];
1207 inst = custom_get_instance (cat, scounter, instance);
1210 size = sizeof (SharedInstance) + strlen (inst->instance_name);
1213 return custom_vtable (scounter, inst, (char*)inst + size + scounter->seq_num * sizeof (guint64));
1216 static const CategoryDesc*
1217 find_category (MonoString *category)
1220 for (i = 0; i < NUM_CATEGORIES; ++i) {
1221 if (mono_string_compare_ascii (category, predef_categories [i].name) == 0)
1222 return &predef_categories [i];
1228 mono_perfcounter_get_impl (MonoString* category, MonoString* counter, MonoString* instance,
1229 MonoString* machine, int *type, MonoBoolean *custom)
1231 const CategoryDesc *cdesc;
1232 /* no support for counters on other machines */
1233 if (mono_string_compare_ascii (machine, "."))
1235 cdesc = find_category (category);
1237 SharedCategory *scat = find_custom_category (category);
1241 return custom_get_impl (scat, counter, instance, type);
1243 switch (cdesc->id) {
1245 return cpu_get_impl (counter, instance, type, custom);
1247 return process_get_impl (counter, instance, type, custom);
1248 case CATEGORY_MONO_MEM:
1249 return mono_mem_get_impl (counter, instance, type, custom);
1250 case CATEGORY_NETWORK:
1251 return network_get_impl (counter, instance, type, custom);
1255 case CATEGORY_REMOTING:
1256 case CATEGORY_LOADING:
1257 case CATEGORY_THREAD:
1258 case CATEGORY_INTEROP:
1259 case CATEGORY_SECURITY:
1260 case CATEGORY_ASPNET:
1261 case CATEGORY_THREADPOOL:
1262 return predef_writable_get_impl (cdesc->id, counter, instance, type, custom);
1268 mono_perfcounter_get_sample (void *impl, MonoBoolean only_value, MonoCounterSample *sample)
1270 ImplVtable *vtable = impl;
1271 if (vtable && vtable->sample)
1272 return vtable->sample (vtable, only_value, sample);
1277 mono_perfcounter_update_value (void *impl, MonoBoolean do_incr, gint64 value)
1279 ImplVtable *vtable = impl;
1280 if (vtable && vtable->update)
1281 return vtable->update (vtable, do_incr, value);
1286 mono_perfcounter_free_data (void *impl)
1288 ImplVtable *vtable = impl;
1289 if (vtable && vtable->cleanup)
1290 vtable->cleanup (vtable);
1294 /* Category icalls */
1296 mono_perfcounter_category_del (MonoString *name)
1298 const CategoryDesc *cdesc;
1299 SharedCategory *cat;
1300 cdesc = find_category (name);
1301 /* can't delete a predefined category */
1305 cat = find_custom_category (name);
1306 /* FIXME: check the semantics, if deleting a category means also deleting the instances */
1307 if (!cat || cat->num_instances) {
1311 cat->header.ftype = FTYPE_DELETED;
1317 mono_perfcounter_category_help (MonoString *category, MonoString *machine)
1319 const CategoryDesc *cdesc;
1320 /* no support for counters on other machines */
1321 if (mono_string_compare_ascii (machine, "."))
1323 cdesc = find_category (category);
1325 SharedCategory *scat = find_custom_category (category);
1328 return mono_string_new (mono_domain_get (), custom_category_help (scat));
1330 return mono_string_new (mono_domain_get (), cdesc->help);
1334 * Check if the category named @category exists on @machine. If @counter is not NULL, return
1335 * TRUE only if a counter with that name exists in the category.
1338 mono_perfcounter_category_exists (MonoString *counter, MonoString *category, MonoString *machine)
1340 const CategoryDesc *cdesc;
1341 /* no support for counters on other machines */
1342 if (mono_string_compare_ascii (machine, "."))
1344 cdesc = find_category (category);
1346 SharedCategory *scat = find_custom_category (category);
1349 /* counter is allowed to be null */
1352 /* search through the custom category */
1353 return find_custom_counter (scat, counter) != NULL;
1355 /* counter is allowed to be null */
1358 if (get_counter_in_category (cdesc, counter))
1363 /* C map of the type with the same name */
1369 } CounterCreationData;
1372 * Since we'll keep a copy of the category per-process, we should also make sure
1373 * categories with the same name are compatible.
1376 mono_perfcounter_create (MonoString *category, MonoString *help, int type, MonoArray *items)
1381 int num_counters = mono_array_length (items);
1382 int counters_data_size;
1385 char **counter_info = NULL;
1388 SharedCategory *cat;
1390 /* FIXME: ensure there isn't a category created already */
1391 mono_error_init (&error);
1392 name = mono_string_to_utf8_checked (category, &error);
1393 if (!mono_error_ok (&error))
1395 chelp = mono_string_to_utf8_checked (help, &error);
1396 if (!mono_error_ok (&error))
1398 counter_info = g_new0 (char*, num_counters * 2);
1399 /* calculate the size we need structure size + name/help + 2 0 string terminators */
1400 size = G_STRUCT_OFFSET (SharedCategory, name) + strlen (name) + strlen (chelp) + 2;
1401 for (i = 0; i < num_counters; ++i) {
1402 CounterCreationData *data = mono_array_get (items, CounterCreationData*, i);
1403 counter_info [i * 2] = mono_string_to_utf8_checked (data->name, &error);
1404 if (!mono_error_ok (&error))
1406 counter_info [i * 2 + 1] = mono_string_to_utf8_checked (data->help, &error);
1407 if (!mono_error_ok (&error))
1409 size += sizeof (SharedCounter) + 1; /* 1 is for the help 0 terminator */
1411 for (i = 0; i < num_counters * 2; ++i) {
1412 if (!counter_info [i])
1414 size += strlen (counter_info [i]) + 1;
1418 counters_data_size = num_counters * 8; /* optimize for size later */
1422 ptr = shared_data_find_room (size);
1427 cat = (SharedCategory*)ptr;
1428 cat->header.extra = type;
1429 cat->header.size = size;
1430 cat->num_counters = num_counters;
1431 cat->counters_data_size = counters_data_size;
1432 /* now copy the vaiable data */
1435 p += strlen (name) + 1;
1437 p += strlen (chelp) + 1;
1438 for (i = 0; i < num_counters; ++i) {
1439 CounterCreationData *data = mono_array_get (items, CounterCreationData*, i);
1440 /* emit the SharedCounter structures */
1441 *p++ = perfctr_type_compress (data->type);
1443 strcpy (p, counter_info [i * 2]);
1444 p += strlen (counter_info [i * 2]) + 1;
1445 strcpy (p, counter_info [i * 2 + 1]);
1446 p += strlen (counter_info [i * 2 + 1]) + 1;
1448 cat->header.ftype = FTYPE_CATEGORY;
1454 for (i = 0; i < num_counters * 2; ++i) {
1455 g_free (counter_info [i]);
1457 g_free (counter_info);
1461 mono_error_cleanup (&error);
1466 mono_perfcounter_instance_exists (MonoString *instance, MonoString *category, MonoString *machine)
1468 const CategoryDesc *cdesc;
1469 /* no support for counters on other machines */
1470 /*FIXME: machine appears to be wrong
1471 if (mono_string_compare_ascii (machine, "."))
1473 cdesc = find_category (category);
1475 SharedCategory *scat;
1476 scat = find_custom_category (category);
1479 if (find_custom_instance (scat, instance))
1482 /* FIXME: search instance */
1488 mono_perfcounter_category_names (MonoString *machine)
1492 MonoDomain *domain = mono_domain_get ();
1493 GSList *custom_categories, *tmp;
1494 /* no support for counters on other machines */
1495 if (mono_string_compare_ascii (machine, "."))
1496 return mono_array_new (domain, mono_get_string_class (), 0);
1498 custom_categories = get_custom_categories ();
1499 res = mono_array_new (domain, mono_get_string_class (), NUM_CATEGORIES + g_slist_length (custom_categories));
1500 for (i = 0; i < NUM_CATEGORIES; ++i) {
1501 const CategoryDesc *cdesc = &predef_categories [i];
1502 mono_array_setref (res, i, mono_string_new (domain, cdesc->name));
1504 for (tmp = custom_categories; tmp; tmp = tmp->next) {
1505 SharedCategory *scat = tmp->data;
1506 mono_array_setref (res, i, mono_string_new (domain, scat->name));
1510 g_slist_free (custom_categories);
1515 mono_perfcounter_counter_names (MonoString *category, MonoString *machine)
1518 SharedCategory *scat;
1519 const CategoryDesc *cdesc;
1521 MonoDomain *domain = mono_domain_get ();
1522 /* no support for counters on other machines */
1523 if (mono_string_compare_ascii (machine, "."))
1524 return mono_array_new (domain, mono_get_string_class (), 0);
1525 cdesc = find_category (category);
1527 res = mono_array_new (domain, mono_get_string_class (), cdesc [1].first_counter - cdesc->first_counter);
1528 for (i = cdesc->first_counter; i < cdesc [1].first_counter; ++i) {
1529 const CounterDesc *desc = &predef_counters [i];
1530 mono_array_setref (res, i - cdesc->first_counter, mono_string_new (domain, desc->name));
1535 scat = find_custom_category (category);
1537 char *p = custom_category_counters (scat);
1539 res = mono_array_new (domain, mono_get_string_class (), scat->num_counters);
1540 for (i = 0; i < scat->num_counters; ++i) {
1541 mono_array_setref (res, i, mono_string_new (domain, p + 1));
1542 p += 1 + strlen (p + 1) + 1; /* skip counter type and name */
1543 p += strlen (p) + 1; /* skip counter help */
1549 return mono_array_new (domain, mono_get_string_class (), 0);
1553 get_string_array (void **array, int count, gboolean is_process)
1556 MonoDomain *domain = mono_domain_get ();
1557 MonoArray * res = mono_array_new (mono_domain_get (), mono_get_string_class (), count);
1558 for (i = 0; i < count; ++i) {
1562 char *pname = mono_process_get_name (array [i], buf, sizeof (buf));
1563 p = g_strdup_printf ("%d/%s", GPOINTER_TO_INT (array [i]), pname);
1565 sprintf (buf, "%d", GPOINTER_TO_INT (array [i]));
1568 mono_array_setref (res, i, mono_string_new (domain, p));
1576 get_string_array_of_strings (void **array, int count)
1579 MonoDomain *domain = mono_domain_get ();
1580 MonoArray * res = mono_array_new (mono_domain_get (), mono_get_string_class (), count);
1581 for (i = 0; i < count; ++i) {
1583 mono_array_setref (res, i, mono_string_new (domain, p));
1590 get_mono_instances (void)
1599 buf = g_new (void*, count);
1600 res = mono_shared_area_instances (buf, count);
1601 } while (res == count);
1602 array = get_string_array (buf, res, TRUE);
1608 get_cpu_instances (void)
1614 count = mono_cpu_count () + 1; /* +1 for "_Total" */
1615 buf = g_new (void*, count);
1616 for (i = 0; i < count; ++i)
1617 buf [i] = GINT_TO_POINTER (i - 1); /* -1 => _Total */
1618 array = get_string_array (buf, count, FALSE);
1620 mono_array_setref (array, 0, mono_string_new (mono_domain_get (), "_Total"));
1625 get_processes_instances (void)
1629 void **buf = mono_process_list (&count);
1631 return get_string_array (NULL, 0, FALSE);
1632 array = get_string_array (buf, count, TRUE);
1638 get_networkinterface_instances (void)
1642 void **buf = mono_networkinterface_list (&count);
1644 return get_string_array_of_strings (NULL, 0);
1645 array = get_string_array_of_strings (buf, count);
1646 g_strfreev ((char **) buf);
1651 get_custom_instances (MonoString *category)
1653 SharedCategory *scat;
1654 scat = find_custom_category (category);
1656 GSList *list = get_custom_instances_list (scat);
1659 MonoArray *array = mono_array_new (mono_domain_get (), mono_get_string_class (), g_slist_length (list));
1660 for (tmp = list; tmp; tmp = tmp->next) {
1661 SharedInstance *inst = tmp->data;
1662 mono_array_setref (array, i, mono_string_new (mono_domain_get (), inst->instance_name));
1665 g_slist_free (list);
1668 return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
1672 mono_perfcounter_instance_names (MonoString *category, MonoString *machine)
1674 const CategoryDesc* cat;
1675 if (mono_string_compare_ascii (machine, "."))
1676 return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
1677 cat = find_category (category);
1679 return get_custom_instances (category);
1680 switch (cat->instance_type) {
1682 return get_mono_instances ();
1684 return get_cpu_instances ();
1685 case ProcessInstance:
1686 return get_processes_instances ();
1687 case NetworkInterfaceInstance:
1688 return get_networkinterface_instances ();
1689 case ThreadInstance:
1691 return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
1696 mono_perfcounter_get_impl (MonoString* category, MonoString* counter, MonoString* instance, MonoString* machine, int *type, MonoBoolean *custom)
1698 g_assert_not_reached ();
1702 mono_perfcounter_get_sample (void *impl, MonoBoolean only_value, MonoCounterSample *sample)
1704 g_assert_not_reached ();
1708 mono_perfcounter_update_value (void *impl, MonoBoolean do_incr, gint64 value)
1710 g_assert_not_reached ();
1714 mono_perfcounter_free_data (void *impl)
1716 g_assert_not_reached ();
1719 /* Category icalls */
1721 mono_perfcounter_category_del (MonoString *name)
1723 g_assert_not_reached ();
1727 mono_perfcounter_category_help (MonoString *category, MonoString *machine)
1729 g_assert_not_reached ();
1733 mono_perfcounter_category_exists (MonoString *counter, MonoString *category, MonoString *machine)
1735 g_assert_not_reached ();
1739 mono_perfcounter_create (MonoString *category, MonoString *help, int type, MonoArray *items)
1741 g_assert_not_reached ();
1745 mono_perfcounter_instance_exists (MonoString *instance, MonoString *category, MonoString *machine)
1747 g_assert_not_reached ();
1751 mono_perfcounter_category_names (MonoString *machine)
1753 g_assert_not_reached ();
1757 mono_perfcounter_counter_names (MonoString *category, MonoString *machine)
1759 g_assert_not_reached ();
1763 mono_perfcounter_instance_names (MonoString *category, MonoString *machine)
1765 g_assert_not_reached ();