Compare commits

...
38 Commits
Author SHA1 Message Date
Rodrigo Kumpera af56b95e7e Merge pull request #974 from ludovic-henry/kumpera-counters
Updated protocol for consistent string representation, added current protocol description, added file destination for sampling.
2014-03-31 16:47:24 -04:00
Ludovic Henry 96da7c5e99 Updated protocol for consistent string representation, added current protocol description, added file destination for sampling. 2014-03-31 16:12:38 -04:00
Rodrigo Kumpera eafa81cdfc Unbreak the mac. 2014-03-27 08:22:46 -04:00
Rodrigo Kumpera 72f0f99d34 Implement loadavg on linux. 2014-03-24 11:31:48 -04:00
Rodrigo Kumpera 3df4127b47 Export agent start so we can use it from managed. 2014-03-24 11:31:33 -04:00
Rodrigo Kumpera 540bb0e41f [counters] Add loadavg. 2014-03-22 17:52:53 -04:00
Rodrigo Kumpera a55b3c10b7 Fix custom counter lookup. 2014-03-21 18:48:30 -04:00
Rodrigo Kumpera 04fbfb5d57 Fix perf-counters walking. Fix custom ds. 2014-03-21 18:09:55 -04:00
Rodrigo Kumpera 282ce9964b [counters] Fix the protocol and add listing. Remove failed integration with perf-counters. 2014-03-21 16:09:59 -04:00
Rodrigo Kumpera 2a449373a8 [counters] Export user counters. 2014-03-21 01:25:19 -04:00
Rodrigo Kumpera 352a8b5903 [counters] Add/change API and cleanup the agent a bit. 2014-03-21 01:22:34 -04:00
Rodrigo Kumpera cfebb985ad [counters] Add factory func for synthetic counters. 2014-03-21 00:41:05 -04:00
Rodrigo Kumpera 0a75658aea [counters] Add callback to register foreign data sources. 2014-03-21 00:35:40 -04:00
Rodrigo Kumpera ef388997e0 [counters] add iterator func. 2014-03-20 21:56:53 -04:00
Rodrigo Kumpera e97081d272 [counters] Merge Ludovic code with new perf-agent commands. 2014-03-20 20:01:37 -04:00
Rodrigo Kumpera 0657f618bc [counters] First pass of fixes from Zoltan's code review. 2014-03-20 18:39:13 -04:00
Rodrigo Kumpera 9edc75121a [counters] First pass sanitizing counter usage in the GCs. 2014-03-19 20:17:23 -04:00
Rodrigo Kumpera 41a1b4b8c3 [counters] Fix sizeof usage. 2014-03-19 20:16:53 -04:00
Rodrigo Kumpera 0e7d1b5b68 [counters] Replace defines with static functions. Make stuff better typed. 2014-03-19 20:16:29 -04:00
Rodrigo Kumpera 1b24dd4b62 Increase the attach timeout from 1s to 10s. 2014-03-19 14:19:28 -04:00
Rodrigo Kumpera 4c66a9c278 Fix string length decoding. 2014-03-19 12:05:34 -04:00
Rodrigo Kumpera a4563b3c7e Fix some names. 2014-03-19 11:16:12 -04:00
Rodrigo Kumpera 071c7d6356 Add support for synthetic cpu counters. 2014-03-18 20:52:23 -04:00
Rodrigo Kumpera 9034c0e0a6 [counters] fix parsing of category. 2014-03-18 20:52:11 -04:00
Rodrigo Kumpera 486cf384f3 [counters] Add synthetic counters and counters with args. 2014-03-18 20:25:01 -04:00
Rodrigo Kumpera cce7652c6d Fix formating 2014-03-18 19:59:14 -04:00
Rodrigo Kumpera ac23d277ea Integrate the counters agent from Ludo. 2014-03-18 19:24:36 -04:00
Rodrigo Kumpera 8669dba202 [counters] Add support for starting the perf agent with attach 2014-03-18 19:24:36 -04:00
Rodrigo Kumpera 946fa2e3f8 Add a thread count counter. 2014-03-18 19:24:35 -04:00
Rodrigo Kumpera f71de54d95 [sgen] Write a few counters into the new infrastructure. 2014-03-18 19:24:35 -04:00
Rodrigo Kumpera d5df1e056f [counters] Add another helper. 2014-03-18 19:24:35 -04:00
Rodrigo Kumpera bfb79fb5fe [sgen] Clean the memory governor a bit. 2014-03-18 19:24:35 -04:00
Rodrigo Kumpera 2f00691256 Use new counters for gc counter. 2014-03-18 19:24:34 -04:00
Rodrigo Kumpera 4003c6b277 Fix callback, more helpers, fix units. 2014-03-18 19:24:34 -04:00
Rodrigo Kumpera 10870d8478 [counters] Add helpers. 2014-03-18 19:24:34 -04:00
Rodrigo Kumpera 7356f3da99 [counters] Add double counter type. 2014-03-18 19:24:34 -04:00
Rodrigo Kumpera 29ed618410 [counters] Add callback support back. 2014-03-18 19:24:33 -04:00
Rodrigo Kumpera 54f48abf3c [counters] New counters API.
The new API returns a pointer to the counter instead of taking one.
It exposes a richer set of information on counters.
2014-03-18 19:24:33 -04:00
18 changed files with 1784 additions and 204 deletions

No files matched your search

@@ -64,10 +64,10 @@ namespace Mono.Attach
return res;
}
/*
* Loads the specific agent assembly into this vm.
*/
public void Attach (string agent, string args) {
const int WAIT_LOOP_COUNT = 100;
void SendCommand (Action<NetworkStream> action) {
string user = UnixUserInfo.GetRealUser ().UserName;
// Check whenever the attach socket exists
@@ -86,13 +86,13 @@ namespace Mono.Attach
// Wait for the socket file to materialize
int i;
for (i = 0; i < 10; ++i) {
for (i = 0; i < WAIT_LOOP_COUNT; ++i) {
if (File.Exists (socket_file))
break;
Thread.Sleep (100);
}
if (i == 10)
if (i == WAIT_LOOP_COUNT)
throw new Exception (String.Format ("Runtime failed to create attach socket '{0}'.", socket_file));
} finally {
File.Delete (trigger_file);
@@ -108,24 +108,55 @@ namespace Mono.Attach
UnixClient client = new UnixClient (path);
NetworkStream stream = client.GetStream ();
using (NetworkStream stream = client.GetStream ()) {
// Write header
byte[] magic = new byte [] { (byte)'M', (byte)'O', (byte)'N', (byte)'O', 1, 0 };
stream.Write (magic, 0, magic.Length);
// Compose payload
MemoryStream ms = new MemoryStream ();
BinaryWriter writer = new BinaryWriter (ms);
write_string (writer, "attach");
write_string (writer, agent);
write_string (writer, args);
//Send command
action (stream);
stream.Flush ();
}
// Write header
byte[] magic = new byte [] { (byte)'M', (byte)'O', (byte)'N', (byte)'O', 1, 0 };
stream.Write (magic, 0, magic.Length);
}
// Write payload length
new BinaryWriter (stream).Write ((int)ms.Length);
/*
* Loads the specific agent assembly into this vm.
*/
public void Attach (string agent, string args) {
SendCommand (stream => {
// Compose payload
MemoryStream ms = new MemoryStream ();
BinaryWriter writer = new BinaryWriter (ms);
write_string (writer, "attach");
write_string (writer, agent);
write_string (writer, args);
// Write payload
stream.Write (ms.GetBuffer (), 0, (int)ms.Length);
// Write payload length
new BinaryWriter (stream).Write ((int)ms.Length);
// Write payload
stream.Write (ms.GetBuffer (), 0, (int)ms.Length);
});
}
/*
* Start the perf agent into this vm
*/
public void StartPerfAgent (string args) {
SendCommand (stream => {
// Compose payload
MemoryStream ms = new MemoryStream ();
BinaryWriter writer = new BinaryWriter (ms);
write_string (writer, "perf-agent");
write_string (writer, args);
// Write payload length
new BinaryWriter (stream).Write ((int)ms.Length);
// Write payload
stream.Write (ms.GetBuffer (), 0, (int)ms.Length);
});
}
enum PrimitiveType : byte {
+23 -17
View File
@@ -38,6 +38,7 @@
#include <mono/metadata/threads-types.h>
#include <mono/metadata/gc-internal.h>
#include <mono/utils/mono-threads.h>
#include <mono/utils/mono-counters-agent.h>
#include "attach.h"
/*
@@ -141,6 +142,7 @@ static char*
decode_string_value (guint8 *buf, guint8 **endbuf, guint8 *limit)
{
int type;
int shift = 0;
gint32 length;
guint8 *p = buf;
char *s;
@@ -155,10 +157,9 @@ decode_string_value (guint8 *buf, guint8 **endbuf, guint8 *limit)
length = 0;
while (TRUE) {
guint8 b = decode_byte (p, &p, limit);
length <<= 8;
length += b;
if (b <= 0x7f)
length += (b & 0x7f) << shift;
shift += 7;
if ((b & 0x80) == 0)
break;
}
@@ -290,7 +291,6 @@ mono_attach_load_agent (MonoDomain *domain, char *agent, char *args, MonoObject
agent_assembly = mono_assembly_open (agent, &open_status);
if (!agent_assembly) {
fprintf (stderr, "Cannot open agent assembly '%s': %s.\n", agent, mono_image_strerror (open_status));
g_free (agent);
return 2;
}
@@ -302,14 +302,12 @@ mono_attach_load_agent (MonoDomain *domain, char *agent, char *args, MonoObject
entry = mono_image_get_entry_point (image);
if (!entry) {
g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
g_free (agent);
return 1;
}
method = mono_get_method (image, entry, NULL);
if (method == NULL){
g_print ("The entry point method of assembly '%s' could not be loaded\n", agent);
g_free (agent);
return 1;
}
@@ -320,8 +318,6 @@ mono_attach_load_agent (MonoDomain *domain, char *agent, char *args, MonoObject
main_args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 0);
}
g_free (agent);
pa [0] = main_args;
mono_runtime_invoke (method, NULL, pa, exc);
@@ -507,7 +503,7 @@ receiver_thread (void *arg)
mono_thread_internal_current ()->state |= ThreadState_Background;
while (TRUE) {
char *cmd, *agent_name, *agent_args;
char *cmd;
guint8 *body;
/* Read Header */
@@ -547,17 +543,27 @@ receiver_thread (void *arg)
cmd = decode_string_value (p, &p, p_end);
if (cmd == NULL)
break;
g_assert (!strcmp (cmd, "attach"));
if (!strcmp (cmd, "attach")) {
char *agent_name = decode_string_value (p, &p, p_end);
char *agent_args = decode_string_value (p, &p, p_end);
agent_name = decode_string_value (p, &p, p_end);
agent_args = decode_string_value (p, &p, p_end);
printf ("attach: Loading agent '%s'.\n", agent_name);
mono_attach_load_agent (mono_domain_get (), agent_name, agent_args, &exc);
printf ("attach: Loading agent '%s'.\n", agent_name);
mono_attach_load_agent (mono_domain_get (), agent_name, agent_args, &exc);
g_free (agent_name);
g_free (agent_args);
} else if (!strcmp (cmd, "perf-agent")) {
char *agent_args = decode_string_value (p, &p, p_end);
mono_counters_agent_start (agent_args);
g_free (agent_args);
} else {
g_warning ("Invalid attach command: %s", cmd);
}
g_free (cmd);
g_free (body);
// FIXME: Send back a result
/* The attach protocol currently is unidirectional */
break;
}
close (conn_fd);
+8 -1
View File
@@ -30,6 +30,7 @@
#include <mono/utils/dtrace.h>
#include <mono/utils/gc_wrapper.h>
#include <mono/utils/mono-mutex.h>
#include <mono/utils/mono-counters-internals.h>
#if HAVE_BOEHM_GC
@@ -52,6 +53,9 @@ void *pthread_get_stackaddr_np(pthread_t);
static gboolean gc_initialized = FALSE;
static mono_mutex_t mono_gc_lock;
/* Counters */
static IntCounter major_gc_count;
static void*
boehm_thread_register (MonoThreadInfo* info, void *baseptr);
static void
@@ -205,6 +209,9 @@ mono_gc_base_init (void)
mono_thread_info_attach (&dummy);
mono_gc_enable_events ();
major_gc_count = mono_counters_new_int (MONO_COUNTER_CAT_GC, "Major GC collections", MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_MONOTONIC);
gc_initialized = TRUE;
}
@@ -428,7 +435,7 @@ on_gc_notification (GCEventType event)
if (mono_perfcounters)
mono_perfcounters->gc_collections0++;
#endif
gc_stats.major_gc_count ++;
mono_counters_int_inc (major_gc_count);
gc_start_time = mono_100ns_ticks ();
break;
-2
View File
@@ -16,8 +16,6 @@
#include <mono/utils/gc_wrapper.h>
typedef struct {
int minor_gc_count;
int major_gc_count;
long long minor_gc_time_usecs;
long long major_gc_time_usecs;
} GCStats;
-2
View File
@@ -1135,8 +1135,6 @@ mono_gc_init (void)
MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_PINNED].entries);
mono_counters_register ("Created object count", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &mono_stats.new_object_count);
mono_counters_register ("Minor GC collections", MONO_COUNTER_GC | MONO_COUNTER_INT, &gc_stats.minor_gc_count);
mono_counters_register ("Major GC collections", MONO_COUNTER_GC | MONO_COUNTER_INT, &gc_stats.major_gc_count);
mono_counters_register ("Minor GC time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &gc_stats.minor_gc_time_usecs);
mono_counters_register ("Major GC time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &gc_stats.major_gc_time_usecs);
+163 -2
View File
@@ -33,6 +33,7 @@
#include "metadata/object-internals.h"
/* for mono_stats */
#include "metadata/class-internals.h"
#include "utils/mono-counters-internals.h"
#include "utils/mono-time.h"
#include "utils/mono-mmap.h"
#include "utils/mono-proclib.h"
@@ -339,6 +340,10 @@ typedef struct {
/* maps a pid to a ExternalSArea pointer */
static GHashTable *pid_to_shared_area = NULL;
/* forward definitions */
static MonoCounter* mc_get_custom_counter (const char *cat_name, const char *counter_name);
static gboolean mc_enumerate_custom_counters (CountersEnumCallback cb);
static MonoSharedArea *
load_sarea_for_pid (int pid)
{
@@ -464,6 +469,8 @@ mono_perfcounters_init (void)
shared_area->data_start = d_offset;
shared_area->size = 4096;
mono_perfcounters = &shared_area->counters;
mono_counters_add_data_source (mc_get_custom_counter, mc_enumerate_custom_counters);
}
static int
@@ -1538,8 +1545,8 @@ mono_perfcounter_counter_names (MonoString *category, MonoString *machine)
int i;
res = mono_array_new (domain, mono_get_string_class (), scat->num_counters);
for (i = 0; i < scat->num_counters; ++i) {
mono_array_setref (res, i, mono_string_new (domain, p + 1));
p += 1 + strlen (p + 1) + 1; /* skip counter type and name */
mono_array_setref (res, i, mono_string_new (domain, p + 2));
p += 2 + strlen (p + 1) + 1; /* skip counter type and name */
p += strlen (p) + 1; /* skip counter help */
}
perfctr_unlock ();
@@ -1691,6 +1698,160 @@ mono_perfcounter_instance_names (MonoString *category, MonoString *machine)
return mono_array_new (mono_domain_get (), mono_get_string_class (), 0);
}
}
typedef struct {
CountersEnumCallback cb;
gboolean ok;
} EnumCountersData;
static gboolean
enumerate_category (SharedHeader *header, void *data)
{
int i;
SharedCategory *cat;
char *p;
EnumCountersData *ecd = data;
if (header->ftype != FTYPE_CATEGORY)
return TRUE;
cat = (SharedCategory*)header;
p = custom_category_counters (cat);
for (i = 0; i < cat->num_counters; ++i) {
SharedCounter *counter = (SharedCounter*)p;
if (!ecd->cb (cat->name, counter->name)) {
ecd->ok = FALSE;
return FALSE;
}
p += 2 + strlen (p + 1) + 1; /* skip counter type and name */
p += strlen (p) + 1; /* skip counter help */
}
return TRUE;
}
static gboolean
mc_enumerate_custom_counters (CountersEnumCallback cb)
{
EnumCountersData ecd;
ecd.cb = cb;
ecd.ok = TRUE;
foreach_shared_item (enumerate_category, (void*)&ecd);
return ecd.ok;
}
typedef struct {
const char *name;
SharedCategory *cat;
} CStrCatSearch;
static gboolean
cstr_category_search (SharedHeader *header, void *data)
{
CStrCatSearch *search = data;
if (header->ftype == FTYPE_CATEGORY) {
SharedCategory *cat = (SharedCategory*)header;
if (strcmp (search->name, cat->name) == 0) {
search->cat = cat;
return FALSE;
}
}
return TRUE;
}
static SharedCategory*
cstr_find_custom_category (const char *name)
{
CStrCatSearch search;
search.name = name;
search.cat = NULL;
foreach_shared_item (cstr_category_search, &search);
return search.cat;
}
static SharedCounter*
cstr_find_custom_counter (SharedCategory* cat, const char *name)
{
int i;
char *p = custom_category_counters (cat);
for (i = 0; i < cat->num_counters; ++i) {
SharedCounter *counter = (SharedCounter*)p;
if (strcmp (name, counter->name) == 0)
return counter;
p += 2 + strlen (p + 1) + 1; /* skip counter type and name */
p += strlen (p) + 1; /* skip counter help */
}
return NULL;
}
typedef struct {
unsigned int cat_offset;
SharedCategory* cat;
const char *instance;
SharedInstance* result;
} CstrInstanceSearch;
static gboolean
cstr_instance_search (SharedHeader *header, void *data)
{
CstrInstanceSearch *search = data;
if (header->ftype == FTYPE_INSTANCE) {
SharedInstance *ins = (SharedInstance*)header;
if (search->cat_offset == ins->category_offset) {
if (strcmp (search->instance, ins->instance_name) == 0) {
search->result = ins;
return FALSE;
}
}
}
return TRUE;
}
static SharedInstance*
cstr_find_custom_instance (SharedCategory* cat)
{
CstrInstanceSearch search;
search.cat_offset = (char*)cat - (char*)shared_area;
search.cat = cat;
search.instance = ".";
search.result = NULL;
foreach_shared_item (cstr_instance_search, &search);
return search.result;
}
static MonoCounter*
mc_get_custom_counter (const char *cat_name, const char *counter_name)
{
SharedCounter *scounter;
SharedInstance* inst;
int size;
void *addr;
SharedCategory *scat = cstr_find_custom_category (cat_name);
if (!scat)
return NULL;
scounter = cstr_find_custom_counter (scat, counter_name);
if (!scounter)
return NULL;
inst = cstr_find_custom_instance (scat);
if (!inst)
return NULL;
size = sizeof (SharedInstance) + strlen (inst->instance_name);
size += 7;
size &= ~7;
addr = (char*)inst + size + scounter->seq_num * sizeof (guint64);
return mono_counters_new_synt (
MONO_COUNTER_CAT_CUSTOM,
g_strdup_printf("%s:%s", cat_name, counter_name),
MONO_COUNTER_TYPE_LONG,
MONO_COUNTER_UNIT_NONE, //XXX can we do better?
MONO_COUNTER_VARIABLE, //XXX can we do better?
addr);
}
#else
void*
mono_perfcounter_get_impl (MonoString* category, MonoString* counter, MonoString* instance, MonoString* machine, int *type, MonoBoolean *custom)
+3 -2
View File
@@ -45,6 +45,7 @@
#include "metadata/profiler-private.h"
#include "metadata/marshal.h"
#include "metadata/method-builder.h"
#include "metadata/mono-gc.h"
#include "utils/mono-memory-model.h"
#include "utils/mono-counters.h"
@@ -117,13 +118,13 @@ alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
void *p;
if (!for_mature) {
if (last_major_gc_warned < stat_major_gcs) {
if (last_major_gc_warned < mono_gc_collection_count (1)) {
++num_degraded;
if (num_degraded == 1 || num_degraded == 3)
fprintf (stderr, "Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.\n");
else if (num_degraded == 10)
fprintf (stderr, "Warning: Repeated degraded allocation. Consider increasing nursery-size.\n");
last_major_gc_warned = stat_major_gcs;
last_major_gc_warned = mono_gc_collection_count (1);
}
InterlockedExchangeAdd (&degraded_mode, size);
sgen_ensure_free_space (size);
+26 -20
View File
@@ -218,7 +218,7 @@
#include "utils/mono-mmap.h"
#include "utils/mono-time.h"
#include "utils/mono-semaphore.h"
#include "utils/mono-counters.h"
#include "utils/mono-counters-internals.h"
#include "utils/mono-proclib.h"
#include "utils/mono-memory-model.h"
#include "utils/mono-logger-internal.h"
@@ -323,9 +323,6 @@ static int stat_wbarrier_value_copy = 0;
static int stat_wbarrier_object_copy = 0;
#endif
int stat_minor_gcs = 0;
int stat_major_gcs = 0;
static long long stat_pinned_objects = 0;
static long long time_minor_pre_collection_fragment_clear = 0;
@@ -354,6 +351,11 @@ static long long time_major_fragment_creation = 0;
int gc_debug_level = 0;
FILE* gc_debug_file;
/* New style counters */
static IntCounter major_gc_count;
static IntCounter minor_gc_count;
/*
void
mono_gc_flush_info (void)
@@ -2211,7 +2213,6 @@ init_stats (void)
if (inited)
return;
mono_counters_register ("Minor fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pre_collection_fragment_clear);
mono_counters_register ("Minor pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pinning);
mono_counters_register ("Minor scan remembered set", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_remsets);
@@ -2270,6 +2271,10 @@ init_stats (void)
sgen_alloc_init_heavy_stats ();
#endif
/* New style counters */
minor_gc_count = mono_counters_new_int (MONO_COUNTER_CAT_GC, "Minor GC collections", MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_MONOTONIC);
major_gc_count = mono_counters_new_int (MONO_COUNTER_CAT_GC, "Major GC collections", MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_MONOTONIC);
inited = TRUE;
}
@@ -2527,7 +2532,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
return TRUE;
MONO_GC_BEGIN (GENERATION_NURSERY);
binary_protocol_collection_begin (stat_minor_gcs, GENERATION_NURSERY);
binary_protocol_collection_begin (mono_counters_int_get (minor_gc_count), GENERATION_NURSERY);
verify_nursery ();
@@ -2553,7 +2558,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
/* FIXME: optimize later to use the higher address where an object can be present */
nursery_next = MAX (nursery_next, sgen_get_nursery_end ());
SGEN_LOG (1, "Start nursery collection %d %p-%p, size: %d", stat_minor_gcs, sgen_get_nursery_start (), nursery_next, (int)(nursery_next - sgen_get_nursery_start ()));
SGEN_LOG (1, "Start nursery collection %d %p-%p, size: %d", mono_counters_int_get (minor_gc_count), sgen_get_nursery_start (), nursery_next, (int)(nursery_next - sgen_get_nursery_start ()));
max_garbage_amount = nursery_next - sgen_get_nursery_start ();
g_assert (nursery_section->size >= max_garbage_amount);
@@ -2577,8 +2582,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
init_gray_queue ();
stat_minor_gcs++;
gc_stats.minor_gc_count ++;
mono_counters_int_inc (minor_gc_count);
MONO_GC_CHECKPOINT_1 (GENERATION_NURSERY);
@@ -2745,7 +2749,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
gc_stats.minor_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
if (heap_dump_file)
dump_heap ("minor", stat_minor_gcs - 1, NULL);
dump_heap ("minor", mono_counters_int_get (minor_gc_count) - 1, NULL);
/* prepare the pin queue for the next collection */
sgen_finish_pinning ();
@@ -2773,7 +2777,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
objects_pinned = 0;
MONO_GC_END (GENERATION_NURSERY);
binary_protocol_collection_end (stat_minor_gcs - 1, GENERATION_NURSERY);
binary_protocol_collection_end (mono_counters_int_get (minor_gc_count) - 1, GENERATION_NURSERY);
if (check_nursery_objects_pinned && !sgen_minor_collector.is_split)
sgen_check_nursery_objects_pinned (unpin_queue != NULL);
@@ -3071,9 +3075,10 @@ static void
major_start_collection (gboolean concurrent, int *old_next_pin_slot)
{
MONO_GC_BEGIN (GENERATION_OLD);
binary_protocol_collection_begin (stat_major_gcs, GENERATION_OLD);
binary_protocol_collection_begin (mono_counters_int_get (major_gc_count), GENERATION_OLD);
current_collection_generation = GENERATION_OLD;
#ifndef DISABLE_PERFCOUNTERS
mono_perfcounters->gc_collections1++;
#endif
@@ -3101,9 +3106,8 @@ major_start_collection (gboolean concurrent, int *old_next_pin_slot)
check_scan_starts ();
degraded_mode = 0;
SGEN_LOG (1, "Start major collection %d", stat_major_gcs);
stat_major_gcs++;
gc_stats.major_gc_count ++;
SGEN_LOG (1, "Start major collection %d", mono_counters_int_get (major_gc_count));
mono_counters_int_inc (major_gc_count);
if (major_collector.start_major_collection)
major_collector.start_major_collection ();
@@ -3261,7 +3265,7 @@ major_finish_collection (const char *reason, int old_next_pin_slot, gboolean sca
time_major_fragment_creation += TV_ELAPSED (btv, atv);
if (heap_dump_file)
dump_heap ("major", stat_major_gcs - 1, reason);
dump_heap ("major", mono_counters_int_get (major_gc_count) - 1, reason);
if (fin_ready_list || critical_fin_list) {
SGEN_LOG (4, "Finalizer-thread wakeup: ready %d", num_ready_finalizers);
@@ -3287,7 +3291,7 @@ major_finish_collection (const char *reason, int old_next_pin_slot, gboolean sca
//consistency_check ();
MONO_GC_END (GENERATION_OLD);
binary_protocol_collection_end (stat_major_gcs - 1, GENERATION_OLD);
binary_protocol_collection_end (mono_counters_int_get (major_gc_count) - 1, GENERATION_OLD);
}
static gboolean
@@ -4620,8 +4624,8 @@ int
mono_gc_collection_count (int generation)
{
if (generation == 0)
return stat_minor_gcs;
return stat_major_gcs;
return mono_counters_int_get (minor_gc_count);
return mono_counters_int_get (major_gc_count);
}
int64_t
@@ -5177,6 +5181,8 @@ mono_gc_base_init (void)
if (minor_collector_opt)
g_free (minor_collector_opt);
sgen_memgov_init (max_heap, soft_limit, allowance_ratio, save_target);
alloc_nursery ();
sgen_cement_init (cement_enabled);
@@ -5328,7 +5334,7 @@ mono_gc_base_init (void)
if (major_collector.post_param_init)
major_collector.post_param_init (&major_collector);
sgen_memgov_init (max_heap, soft_limit, debug_print_allowance, allowance_ratio, save_target);
sgen_memgov_init_debug (debug_print_allowance);
memset (&remset, 0, sizeof (remset));
-1
View File
@@ -974,7 +974,6 @@ extern __thread char *stack_end;
/* Other globals */
extern GCMemSection *nursery_section;
extern int stat_major_gcs;
extern guint32 collect_before_allocs;
extern guint32 verify_before_allocs;
extern gboolean has_per_allocation_action;
+58 -28
View File
@@ -31,7 +31,7 @@
#include "metadata/sgen-memory-governor.h"
#include "metadata/mono-gc.h"
#include "utils/mono-counters.h"
#include "utils/mono-counters-internals.h"
#include "utils/mono-mmap.h"
#include "utils/mono-logger-internal.h"
#include "utils/dtrace.h"
@@ -45,9 +45,15 @@ static mword soft_heap_limit = ((mword)0) - ((mword)1);
static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
/**/
static mword allocated_heap;
static mword total_alloc = 0;
/* Heap memory is in use*/
static WordCounter active_heap_memory;
/* Heap memory is currently allocated */
static WordCounter allocated_heap_memory;
/* An estimate of the total consumption by the GC */
static WordCounter total_gc_memory;
/* GC triggers. */
@@ -56,7 +62,7 @@ static gboolean debug_print_allowance = FALSE;
/* use this to tune when to do a major/minor collection */
static mword memory_pressure = 0;
static mword minor_collection_allowance;
static WordCounter minor_collection_allowance;
static int minor_collection_sections_alloced = 0;
static int last_major_num_sections = 0;
@@ -71,6 +77,11 @@ static mword last_collection_los_memory_alloced;
static mword sgen_memgov_available_free_space (void);
static void
atomic_counter_add (WordCounter counter, size_t val)
{
SGEN_ATOMIC_ADD_P (*(size_t*)counter.pointer, val);
}
static mword
double_to_mword_with_saturation (double value)
@@ -96,7 +107,7 @@ sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
if (!*major_collector.have_swept) {
if (overwrite)
minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
mono_counters_word_set (minor_collection_allowance, MIN_MINOR_COLLECTION_ALLOWANCE);
return;
}
@@ -127,13 +138,13 @@ sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
*/
allowance_target = double_to_mword_with_saturation ((double)save_target * (double)(minor_collection_sections_alloced * major_collector.section_size + last_collection_los_memory_alloced) / (double)(num_major_sections_saved * major_collector.section_size + los_memory_saved));
minor_collection_allowance = MAX (MIN (allowance_target, num_major_sections * major_collector.section_size + los_memory_usage), MIN_MINOR_COLLECTION_ALLOWANCE);
mono_counters_word_set (minor_collection_allowance, MAX (MIN (allowance_target, num_major_sections * major_collector.section_size + los_memory_usage), MIN_MINOR_COLLECTION_ALLOWANCE));
if (new_heap_size + minor_collection_allowance > soft_heap_limit) {
if (new_heap_size + mono_counters_word_get (minor_collection_allowance) > soft_heap_limit) {
if (new_heap_size > soft_heap_limit)
minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
mono_counters_word_set (minor_collection_allowance, MIN_MINOR_COLLECTION_ALLOWANCE);
else
minor_collection_allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
mono_counters_word_set (minor_collection_allowance, MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE));
}
if (debug_print_allowance) {
@@ -143,7 +154,7 @@ sgen_memgov_try_calculate_minor_collection_allowance (gboolean overwrite)
(long)(old_major + last_collection_old_los_memory_usage), (long)old_major, (long)last_collection_old_los_memory_usage);
SGEN_LOG (1, "After collection: %ld bytes (%ld major, %ld LOS)",
(long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
SGEN_LOG (1, "Allowance: %ld bytes", (long)minor_collection_allowance);
SGEN_LOG (1, "Allowance: %ld bytes", (long)mono_counters_word_get (minor_collection_allowance));
}
if (major_collector.have_computed_minor_collection_allowance)
@@ -161,7 +172,7 @@ sgen_need_major_collection (mword space_needed)
return FALSE;
los_alloced = los_memory_usage - MIN (last_collection_los_memory_usage, los_memory_usage);
return (space_needed > sgen_memgov_available_free_space ()) ||
minor_collection_sections_alloced * major_collector.section_size + los_alloced > minor_collection_allowance;
minor_collection_sections_alloced * major_collector.section_size + los_alloced > mono_counters_word_get (minor_collection_allowance);
}
void
@@ -256,7 +267,7 @@ sgen_register_major_sections_alloced (int num_sections)
mword
sgen_get_minor_collection_allowance (void)
{
return minor_collection_allowance;
return mono_counters_word_get (minor_collection_allowance);
}
/* Memory pressure API */
@@ -308,9 +319,11 @@ sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_desc
ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
sgen_assert_memory_alloc (ptr, size, assert_description);
if (ptr) {
SGEN_ATOMIC_ADD_P (total_alloc, size);
if (flags & SGEN_ALLOC_HEAP)
atomic_counter_add (total_gc_memory, size);
if (flags & SGEN_ALLOC_HEAP) {
atomic_counter_add (allocated_heap_memory, size);
MONO_GC_HEAP_ALLOC ((mword)ptr, size);
}
}
return ptr;
}
@@ -326,9 +339,11 @@ sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags
ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE));
sgen_assert_memory_alloc (ptr, size, assert_description);
if (ptr) {
SGEN_ATOMIC_ADD_P (total_alloc, size);
if (flags & SGEN_ALLOC_HEAP)
atomic_counter_add (total_gc_memory, size);
if (flags & SGEN_ALLOC_HEAP) {
atomic_counter_add (allocated_heap_memory, size);
MONO_GC_HEAP_ALLOC ((mword)ptr, size);
}
}
return ptr;
}
@@ -342,15 +357,17 @@ sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags)
g_assert (!(flags & ~SGEN_ALLOC_HEAP));
mono_vfree (addr, size);
SGEN_ATOMIC_ADD_P (total_alloc, -size);
if (flags & SGEN_ALLOC_HEAP)
atomic_counter_add (total_gc_memory, -size);
if (flags & SGEN_ALLOC_HEAP) {
atomic_counter_add (allocated_heap_memory, -size);
MONO_GC_HEAP_FREE ((mword)addr, size);
}
}
int64_t
mono_gc_get_heap_size (void)
{
return total_alloc;
return mono_counters_word_get (total_gc_memory);
}
@@ -363,34 +380,42 @@ for other parts of the GC.
static mword
sgen_memgov_available_free_space (void)
{
return max_heap_size - MIN (allocated_heap, max_heap_size);
return max_heap_size - MIN (mono_counters_word_get (active_heap_memory), max_heap_size);
}
void
sgen_memgov_release_space (mword size, int space)
{
SGEN_ATOMIC_ADD_P (allocated_heap, -size);
atomic_counter_add (active_heap_memory, -size);
}
/*
Try to add active memory to the pool
*/
gboolean
sgen_memgov_try_alloc_space (mword size, int space)
{
if (sgen_memgov_available_free_space () < size)
return FALSE;
SGEN_ATOMIC_ADD_P (allocated_heap, size);
mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap);
atomic_counter_add (active_heap_memory, size);
mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, mono_counters_word_get (active_heap_memory));
return TRUE;
}
void
sgen_memgov_init (glong max_heap, glong soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
sgen_memgov_init (glong max_heap, glong soft_limit, double allowance_ratio, double save_target)
{
active_heap_memory = mono_counters_new_word (MONO_COUNTER_CAT_GC, "Active Heap Size", MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE);
allocated_heap_memory = mono_counters_new_word (MONO_COUNTER_CAT_GC, "Current Heap Size", MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE);
minor_collection_allowance = mono_counters_new_word (MONO_COUNTER_CAT_GC, "Max Heap Size", MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE);
total_gc_memory = mono_counters_new_word (MONO_COUNTER_CAT_GC, "GC Memory Usage", MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE);
if (soft_limit)
soft_heap_limit = soft_limit;
debug_print_allowance = debug_allowance;
minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
mono_counters_word_set (minor_collection_allowance, MIN_MINOR_COLLECTION_ALLOWANCE);
if (max_heap == 0)
return;
@@ -411,7 +436,12 @@ sgen_memgov_init (glong max_heap, glong soft_limit, gboolean debug_allowance, do
if (save_target)
save_target_ratio = save_target;
minor_collection_allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
mono_counters_word_set (minor_collection_allowance, MIN_MINOR_COLLECTION_ALLOWANCE);
}
void
sgen_memgov_init_debug (gboolean debug_allowance)
{
debug_print_allowance = debug_allowance;
}
#endif
+2 -1
View File
@@ -25,7 +25,8 @@
#define __MONO_SGEN_MEMORY_GOVERNOR_H__
/* Heap limits */
void sgen_memgov_init (glong max_heap, glong soft_limit, gboolean debug_allowance, double min_allowance_ratio, double save_target) MONO_INTERNAL;
void sgen_memgov_init (glong max_heap, glong soft_limit, double min_allowance_ratio, double save_target) MONO_INTERNAL;
void sgen_memgov_init_debug (gboolean debug_allowance) MONO_INTERNAL;
void sgen_memgov_release_space (mword size, int space) MONO_INTERNAL;
gboolean sgen_memgov_try_alloc_space (mword size, int space) MONO_INTERNAL;
+9 -1
View File
@@ -44,6 +44,7 @@
#include <mono/utils/mono-tls.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-memory-model.h>
#include <mono/utils/mono-counters-internals.h>
#include <mono/metadata/gc-internal.h>
@@ -162,6 +163,10 @@ static MonoNativeTlsKey current_object_key;
static GHashTable *joinable_threads;
static int joinable_thread_count;
/* Counters */
static IntCounter thread_count;
#ifdef MONO_HAVE_FAST_TLS
/* we need to use both the Tls* functions and __thread because
* the gc needs to see all the threads
@@ -273,7 +278,7 @@ static gboolean handle_store(MonoThread *thread, gboolean force_attach)
g_assert (thread->internal_thread);
mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->internal_thread->tid),
thread->internal_thread);
mono_counters_int_inc (thread_count);
mono_threads_unlock ();
return TRUE;
@@ -305,6 +310,7 @@ static gboolean handle_remove(MonoInternalThread *thread)
else
ret = FALSE;
mono_counters_int_dec (thread_count);
mono_threads_unlock ();
/* Don't close the handle here, wait for the object finalizer
@@ -2522,6 +2528,8 @@ void mono_thread_init (MonoThreadStartCB start_cb,
* anything up.
*/
GetCurrentProcess ();
thread_count = mono_counters_new_int (MONO_COUNTER_CAT_THREAD, "Thread Count", MONO_COUNTER_UNIT_NONE, MONO_COUNTER_VARIABLE);
}
void mono_thread_cleanup (void)
+2 -1
View File
@@ -7761,7 +7761,8 @@ mini_cleanup (MonoDomain *domain)
mono_trace_cleanup ();
mono_counters_dump (-1, stdout);
if (mono_jit_stats.enabled)
mono_counters_dump (-1, stdout);
if (mono_inject_async_exc_method)
mono_method_desc_free (mono_inject_async_exc_method);
+3
View File
@@ -20,6 +20,9 @@ monoutils_sources = \
dlmalloc.h \
dlmalloc.c \
mono-counters.c \
mono-counters-internals.h \
mono-counters-agent.c \
mono-counters-agent.h \
mono-compiler.h \
mono-dl.c \
mono-dl.h \
+765
View File
@@ -0,0 +1,765 @@
/*
* Copyright 2014 Xamarin Inc
*
* Note : see the end of this file for a full description of the protocol.
*/
#include <config.h>
#ifndef HOST_WIN32
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <glib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include "mono-counters.h"
#include "mono-counters-internals.h"
#include "mono-counters-agent.h"
#include "mono-time.h"
#include "mono-threads.h"
#define MONO_COUNTERS_AGENT_PROTOCOL_VERSION 0x0001
enum {
AGENT_STATUS_OK,
AGENT_STATUS_NOK,
AGENT_STATUS_NOTFOUND,
AGENT_STATUS_EXISTING,
};
enum {
SRV_HELLO,
SRV_LIST_COUNTERS,
SRV_ADD_COUNTERS,
SRV_REMOVE_COUNTER,
SVR_SAMPLES,
};
typedef struct MonoCounterAgent {
MonoCounter *counter;
// MonoCounterAgent specific data :
void *value;
short index;
} MonoCounterAgent;
static const char *inspector_dest;
static const char *inspector_type;
static int inspector_port;
static gint64 interval;
static GSList* counters;
static gboolean agent_running;
static short current_index = 0;
static void
free_agent_memory (void)
{
GSList *counter = counters;
while (counter) {
MonoCounterAgent *ct = counter->data;
mono_counters_free_counter (ct->counter);
g_free (ct->value);
g_free (ct);
counter = counter->next;
}
g_slist_free (counters);
g_free ((char*)inspector_type);
g_free ((char*)inspector_dest);
counters = NULL;
inspector_type = NULL;
inspector_dest = NULL;
current_index = 0;
}
static MonoCounterAgent*
add_counter (MonoCounter *counter)
{
MonoCounterAgent *agent_counter;
agent_counter = g_new0 (MonoCounterAgent, 1);
agent_counter->counter = counter;
agent_counter->index = current_index++;
counters = g_slist_append (counters, agent_counter);
return agent_counter;
}
static void
parse_counters_names (const char *counters_names)
{
gchar **names, **ptr;
if (!counters_names)
return;
names = g_strsplit (counters_names, ";", -1);
for (ptr = names; *ptr; ++ptr) {
MonoCounter *counter;
gchar **split = g_strsplit (*ptr, "/", 2);
if (!split [0] || !split [1]) {
g_warning ("Bad counter format '%s' use Category/Name", *ptr);
goto end;
}
counter = mono_counters_get (split [0], split [1]);
if (!counter) {
g_warning ("Counter %s not found in category %s", split [1], split [0]);
goto end;
}
add_counter (counter);
end:
g_strfreev(split);
}
g_strfreev (names);
}
#define DEFAULT_TYPE "tcp"
#define DEFAULT_DEST "127.0.0.1"
#define DEFAULT_PORT 8888
static void
parse_tcp_address (const char *address)
{
inspector_type = "tcp";
gchar **split = g_strsplit (address, ":", 2);
if (split [0])
inspector_dest = g_strdup (split [0]);
if (split [1])
inspector_port = strtol (split [1], NULL, 10);
g_strfreev (split);
}
static void
parse_file_address (const char *address)
{
inspector_type = "file";
inspector_dest = g_strdup (address);
}
static void
parse_address (const char *address)
{
inspector_type = NULL;
inspector_dest = NULL;
inspector_port = DEFAULT_PORT;
if (address) {
gchar **split = g_strsplit (address, "://", 2);
if (split [1] == NULL) {
parse_tcp_address (split[0]);
} else {
if (strcmp (split [0], "file") == 0) {
parse_file_address (split [1]);
} else if (strcmp (split [0], "tcp") == 0) {
parse_tcp_address (split [1]);
} else {
g_warning ("Unknown address type '%s'", split [0]);
}
}
g_strfreev (split);
}
if (!inspector_type)
inspector_type = g_strdup (DEFAULT_TYPE);
if (!inspector_dest)
inspector_dest = g_strdup (DEFAULT_DEST);
}
static gboolean
write_buffer (int fd, const char* buffer, size_t size)
{
size_t sent = 0;
while (sent < size) {
ssize_t res = write (fd, buffer + sent, size - sent);
if (res <= 0)
return FALSE;
sent += res;
}
return TRUE;
}
static gboolean
read_buffer (int fd, char* buffer, size_t size)
{
size_t recvd = 0;
while (recvd < size) {
ssize_t res = read (fd, buffer + recvd, size - recvd);
if (res <= 0)
return FALSE;
recvd += res;
}
return TRUE;
}
static gboolean
write_string (int fd, const char *str)
{
int size = -1;
if (!str)
return write_buffer (fd, (char*)&size, 4);
size = strlen (str);
if (!write_buffer (fd, (char*)&size, 4))
return FALSE;
if (!write_buffer (fd, str, size))
return FALSE;
return TRUE;
}
static gboolean
read_string (int fd, char **str)
{
int len;
if (!read_buffer (fd, (char*)&len, 4))
return FALSE;
if (len < 0) {
*str = NULL;
return TRUE;
}
*str = g_malloc(len + 1);
if (!read_buffer (fd, *str, len))
return FALSE;
(*str) [len] = '\0';
return TRUE;
}
static gboolean
write_counter_agent_header (int fd, MonoCounterAgent *counter_agent)
{
if (!write_buffer(fd, (char*)&counter_agent->counter->category, 4) ||
!write_string (fd, counter_agent->counter->name) ||
!write_buffer (fd, (char*)&counter_agent->counter->type, 4) ||
!write_buffer (fd, (char*)&counter_agent->counter->unit, 4) ||
!write_buffer (fd, (char*)&counter_agent->counter->variance, 4) ||
!write_buffer (fd, (char*)&counter_agent->index, 2))
return FALSE;
return TRUE;
}
static gboolean
write_counter_agent_headers (int fd)
{
short count = g_slist_length (counters);
GSList *item;
if (!write_buffer (fd, (char*)&count, 2))
return FALSE;
for (item = counters; item; item = item->next) {
if (!write_counter_agent_header (fd, item->data))
return FALSE;
}
return TRUE;
}
static gboolean
do_hello (int fd)
{
char cmd = SRV_HELLO;
short version = MONO_COUNTERS_AGENT_PROTOCOL_VERSION;
if (!write_buffer (fd, (char*)&cmd, 1))
return FALSE;
if (!write_buffer (fd, (char*)&version, 2))
return FALSE;
if (!write_counter_agent_headers (fd))
return FALSE;
return TRUE;
}
static int _sock;
static gboolean
write_counter (const char *category, const char *name)
{
if (!write_string (_sock, category))
return FALSE;
if (!write_string (_sock, name))
return FALSE;
return TRUE;
}
static gboolean
do_list_counters (int fd)
{
char cmd = SRV_LIST_COUNTERS;
if (!write_buffer (fd, (char*)&cmd, 1))
return FALSE;
_sock = fd;
mono_counters_foreach (write_counter);
write_string (fd, NULL);
return TRUE;
}
static gboolean
do_add_counter (int fd)
{
char cmd = SRV_ADD_COUNTERS;
char *category = NULL;
char *name = NULL;
char *search_name = NULL;
char status;
MonoCounter *counter;
GSList *list;
MonoCounterCategory mcat;
MonoCounterAgent *added_counter;
if (!read_string (fd, &category) || !category)
goto fail;
if (!read_string (fd, &name) || !name)
goto fail;
mcat = mono_counters_category_name_to_id (category);
search_name = name;
if (mcat >= MONO_COUNTER_CAT_MAX) {
mcat = MONO_COUNTER_CAT_CUSTOM;
search_name = g_strdup_printf ("%s:%s", category, name);
}
for (list = counters; list; list = list->next) {
MonoCounterAgent *ac = list->data;
MonoCounter *c = ac->counter;
if (c->category == mcat && !strcmp (c->name, search_name)) {
status = AGENT_STATUS_EXISTING;
goto fail;
}
}
counter = mono_counters_get (category, name);
if (!counter) {
status = AGENT_STATUS_NOTFOUND;
goto done;
}
added_counter = add_counter (counter);
status = AGENT_STATUS_OK;
done:
g_free (category);
g_free (name);
if (!write_buffer (fd, (char*)&cmd, 1))
return FALSE;
if (!write_buffer (fd, &status, 1))
return FALSE;
if (status == AGENT_STATUS_OK) {
if (!write_counter_agent_header (fd, added_counter))
return FALSE;
}
return TRUE;
fail:
g_free(category);
g_free(name);
if (search_name != name)
g_free (search_name);
return FALSE;
}
static gboolean
do_remove_counter (int fd)
{
char cmd = SRV_REMOVE_COUNTER;
short index;
char status;
GSList *item;
MonoCounterAgent *counter_agent = NULL, *counter_agent_tmp;
if (!read_buffer (fd, (char*)&index, 2))
return FALSE;
if (index < 0) {
g_slist_free (counters);
status = AGENT_STATUS_OK;
} else {
for (item = counters; item; item = item->next) {
counter_agent_tmp = item->data;
if (counter_agent_tmp->index == index) {
counter_agent = counter_agent_tmp;
break;
}
}
if (counter_agent) {
g_slist_remove (counters, counter_agent);
g_free (counter_agent);
status = AGENT_STATUS_OK;
} else {
status = AGENT_STATUS_NOTFOUND;
}
}
if (!write_buffer (fd, (char*)&cmd, 1))
return FALSE;
return write_buffer (fd, &status, 1);
}
static gboolean
do_sampling (int fd)
{
short end;
char cmd = SVR_SAMPLES;
char buffer [8];
GSList *item;
MonoCounterAgent *counter_agent;
if (!write_buffer (fd, (char*)&cmd, 1))
return FALSE;
gint64 timestamp = mono_100ns_datetime ();
if (!write_buffer (fd, (char*)&timestamp, 8))
return FALSE;
for (item = counters; item; item = item->next) {
counter_agent = item->data;
if (!counter_agent)
continue;
int size = mono_counters_size (counter_agent->counter);
if (size < 0)
return FALSE;
if (mono_counters_sample (counter_agent->counter, buffer, size) != size) {
g_warning ("Sampling failed, bad size");
return FALSE;
}
if (!counter_agent->value)
counter_agent->value = g_malloc (size);
else if (memcmp (counter_agent->value, buffer, size) == 0)
continue;
memcpy (counter_agent->value, buffer, size);
if (!write_buffer (fd, (char*)&counter_agent->index, 2) ||
!write_buffer (fd, (char*)&size, 2) ||
!write_buffer (fd, buffer, size))
return FALSE;
}
end = -1;
if (!write_buffer (fd, (char*)&end, 2))
return FALSE;
return TRUE;
}
static void*
mono_counters_agent_tcp_sampling_thread (void* ptr)
{
int ret, socketfd = -1;
struct sockaddr_in inspector_addr;
struct timeval timeout;
fd_set socketfd_set;
gint64 last_sampling = 0, now;
char cmd;
if ((socketfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
g_warning ("Error with socket : %s", strerror (errno));
return NULL;
}
memset (&inspector_addr, 0, sizeof (inspector_addr));
inspector_addr.sin_family = AF_INET;
inspector_addr.sin_port = htons (inspector_port);
if (!inet_pton (AF_INET, inspector_dest, &inspector_addr.sin_addr)) {
g_warning ("Error with inet_pton : %s", strerror (errno));
goto cleanup;
}
if (connect(socketfd, (struct sockaddr*)&inspector_addr, sizeof(inspector_addr)) < 0) {
g_warning ("Error with connect : %s", strerror(errno));
goto cleanup;
}
if (!do_hello (socketfd))
goto cleanup;
for (;;) {
now = mono_100ns_datetime ();
if (last_sampling == 0 || now > last_sampling + interval * 10000) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
} else {
timeout.tv_sec = ((last_sampling + interval * 10000 - now) / 10) / 1000000;
timeout.tv_usec = ((last_sampling + interval * 10000 - now) / 10) % 1000000;
}
FD_ZERO (&socketfd_set);
FD_SET (socketfd, &socketfd_set);
if ((ret = select (socketfd + 1, &socketfd_set, NULL, NULL, &timeout)) < 0) {
if (errno == EINTR)
continue;
g_warning ("Error with select : %s", strerror(errno));
goto cleanup;
}
if (ret == 0) {
if (!do_sampling (socketfd))
goto cleanup;
last_sampling = mono_100ns_datetime ();
} else {
if (!read_buffer (socketfd, (char*)&cmd, 1))
goto cleanup;
switch (cmd) {
case 0:
if (!do_list_counters (socketfd))
goto cleanup;
break;
case 1:
if (!do_add_counter (socketfd))
goto cleanup;
break;
case 2:
if (!do_remove_counter (socketfd))
goto cleanup;
break;
case 127:
g_warning ("Closing connection");
goto cleanup;
default:
g_warning ("Unknown perf-agent command %d", cmd);
}
}
}
cleanup:
if (socketfd != -1)
close (socketfd);
free_agent_memory ();
agent_running = FALSE;
return NULL;
}
static void*
mono_counters_agent_file_sampling_thread (void* ptr)
{
int filefd = -1;
if ((filefd = open (inspector_dest, O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_EXLOCK, 0644)) < 0) {
g_warning ("Impossible to open output file '%s'", inspector_dest);
goto cleanup;
}
if (!do_hello (filefd))
goto cleanup;
for (;;) {
if (!do_sampling (filefd))
goto cleanup;
usleep (interval * 1000);
}
cleanup:
if (filefd != -1)
close (filefd);
free_agent_memory ();
agent_running = FALSE;
return NULL;
};
static void
parse_configuration (const gchar* configuration)
{
const char *counters_names = NULL;
const char *address = NULL;
if (configuration == NULL)
return;
gchar **opts = g_strsplit (configuration, ",", -1), **ptr;
for (ptr = opts; *ptr; ++ptr) {
const char *opt = *ptr;
if (g_str_has_prefix (opt, "address=")) {
opt = strchr (opt, '=') + 1;
address = g_strdup(opt);
} else if (g_str_has_prefix (opt, "counters=")) {
opt = strchr (opt, '=') + 1;
counters_names = g_strdup(opt);
} else if (g_str_has_prefix (opt, "interval=")) {
opt = strchr (opt, '=') + 1;
interval = strtoll (opt, NULL, 10);
}
}
parse_counters_names(counters_names);
parse_address(address);
g_free((void*)counters_names);
g_free((void*)address);
g_strfreev(opts);
}
void
mono_counters_agent_start (const char *args)
{
if (agent_running) {
g_warning ("Agent already running");
return;
}
agent_running = TRUE;
parse_configuration(args);
if (strcmp (inspector_type, "tcp") == 0)
mono_threads_create_thread ((LPTHREAD_START_ROUTINE)&mono_counters_agent_tcp_sampling_thread, NULL, 0, 0, NULL);
else if (strcmp (inspector_type, "file") == 0)
mono_threads_create_thread ((LPTHREAD_START_ROUTINE)&mono_counters_agent_file_sampling_thread, NULL, 0, 0, NULL);
}
#else
void
mono_counters_agent_start (const char *args)
{
g_warning ("Perf agent not supported on windows");
}
#endif
/*
* Full Protocol Description :
*
* DEFINITIONS:
*
* Agent : running in the Mono VM, tcp client or write to a file, in charge of : sending the samples to the inspector, execute add/remove/list counters commands.
*
* Inspector : tcp server or read a file, in charge of : displaying samples, send add/remove/lists counters commands. Can be written in any language that support socket or file streaming.
*
* TYPES:
*
* remark: there is two ways to send a list of values :
* - first, you can send the count first, and then the values
* - second, you send the values and finish the list by an end value ( marked as ‘type…endtype(value = end)’ )
*
* Char:
* [value: 1 byte]
*
* Short:
* [value: 2 bytes]
*
* Int:
* [value: 4 bytes]
*
* Long:
* [value: 8 bytes]
*
* Double:
* [value: 8 bytes]
*
* String: if value == null, then length = -1 and there is no value;
* [length: int]
* [value: length * char]
*
* Header: describe a counter that is going to be sent during sampling
* [category: int]
* [name: string]
* [type: int]
* [unit: int]
* [variance: int]
* [index: short]
*
* Headers: list of header
* [count: short] number of header
* [value: count * header]
*
* Counter:
* [name: string]
* [category: string]
*
* Value:
* [index: short]
* [size: short]
* [value: size * char]
*
* ResponseStatus:
* [id: char] possible values : { 0x00: OK, 0x01: NOK, 0x02: NOTFOUND, 0x03: EXISTING }
*
* COMMANDS:
*
* Hello: first command of the protocol, sent by the agent to the inspector on connection. One way from agent to inspector.
* [cmd: char] 0
* [version: short] version of the protocol, currently 0.1 => 1
* [headers: headers] the agent counters headers.
*
* List counters: list all the mono counters that are available for sampling. Request-Response from inspector to agent.
* Request:
* [cmd: char(value = 1)]
* Response:
* [cmd: char(value = 1)]
* [mono counter: counter…string(value = null)]
*
* Add Counter: add a counter to the sample set. Request-Response from inspector to agent.
* Request:
* [cmd: char(value = 2)]
* [name: string]
* [category: string]
* Response:
* [cmd: char(value = 2)]
* [status: response_status]
* [header: header]
*
* Remove Counter: remove counter from the sample set. Request-Response from inspector to agent.
* Request:
* [cmd: char(value = 3)]
* [index: short]
* Response:
* [cmd: char(value = 3)]
* [status: response_status]
*
* Sampling: actual sampling of the data. One way from agent to inspector.
* [cmd: 4]
* [timestamp: long]
* [values: value…short(value = -1)]
*
* Good Bye: close the connection. One way from inspector to agent.
* [cmd: 127]
*/
+8
View File
@@ -0,0 +1,8 @@
#ifndef __MONO_COUNTERS_AGENTS_H__
#define __MONO_COUNTERS_AGENTS_H__
#include "mono-compiler.h"
void mono_counters_agent_start (const char *args);
#endif /* __MONO_COUNTERS_AGENTS_H__ */
+129
View File
@@ -0,0 +1,129 @@
#ifndef __MONO_COUNTERS_INTERNALS_H__
#define __MONO_COUNTERS_INTERNALS_H__
#include "mono-counters.h"
#include "mono-compiler.h"
typedef enum {
MONO_COUNTER_CAT_JIT,
MONO_COUNTER_CAT_GC,
MONO_COUNTER_CAT_METADATA,
MONO_COUNTER_CAT_GENERICS,
MONO_COUNTER_CAT_SECURITY,
MONO_COUNTER_CAT_THREAD,
MONO_COUNTER_CAT_THREADPOOL,
MONO_COUNTER_CAT_SYS,
MONO_COUNTER_CAT_CUSTOM,
MONO_COUNTER_CAT_MAX
} MonoCounterCategory;
typedef enum {
MONO_COUNTER_TYPE_INT, /* 4 bytes */
MONO_COUNTER_TYPE_LONG, /* 8 bytes */
MONO_COUNTER_TYPE_WORD, /* machine word */
MONO_COUNTER_TYPE_DOUBLE,
MONO_COUNTER_TYPE_MAX
} MonoCounterType;
typedef enum {
MONO_COUNTER_UNIT_NONE, /* It's a raw value that needs special handling from the consumer */
MONO_COUNTER_UNIT_BYTES, /* Quantity of bytes the counter represent */
MONO_COUNTER_UNIT_TIME, /* This is a timestap in 100n units */
MONO_COUNTER_UNIT_EVENTS, /* Number of times the given event happens */
MONO_COUNTER_UNIT_CONFIG, /* Configuration knob of the runtime */
MONO_COUNTER_UNIT_PERCENTAGE, /* Percentage of something */
MONO_COUNTER_UNIT_MAX
} MonoCounterUnit;
typedef enum {
MONO_COUNTER_CONSTANT = 1, /* This counter doesn't change. Agent will only send it once */
MONO_COUNTER_MONOTONIC, /* This counter value always increase/decreases over time */
MONO_COUNTER_VARIABLE, /* This counter value can be anything on each sampling */
MONO_COUNTER_VARIANCE_MAX
} MonoCounterVariance;
typedef struct _MonoCounter MonoCounter;
struct _MonoCounter {
MonoCounter *next;
const char *name;
void *addr;
MonoCounterType type;
MonoCounterCategory category;
MonoCounterUnit unit;
MonoCounterVariance variance;
int callback_style;
gboolean is_synthetic;
void *user_arg;
};
typedef gboolean (*CountersEnumCallback) (const char *category, const char *name);
typedef gboolean (*CountersDataSourceForeach) (CountersEnumCallback);
typedef MonoCounter* (*CountersDataSourceGet) (const char *, const char *name);
/*
Limitations:
The old-style string counter type won't work as they cannot be safely sampled during execution.
TODO:
Size-bounded String counter.
Dynamic category registration.
MonoCounter size diet once we're done with the above.
FIXME
The naming schema for the counters sucks, cleanup and unify with mono-counters.h
*/
void* mono_counters_new (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance) MONO_INTERNAL;
MonoCounter* mono_counters_register_full (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *addr) MONO_INTERNAL;
void* mono_counters_new_synt (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *addr) MONO_INTERNAL;
void* mono_counters_new_synt_func (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *fun_addr, void *user_arg) MONO_INTERNAL;
MonoCounter* mono_counters_get (const char *category, const char* name) MONO_INTERNAL;
int mono_counters_sample (MonoCounter* counter, char* buffer, int size) MONO_INTERNAL;
int mono_counters_size (MonoCounter* counter) MONO_INTERNAL;
void mono_counters_foreach (CountersEnumCallback cb) MONO_INTERNAL;
void mono_counters_add_data_source (CountersDataSourceGet get_cb, CountersDataSourceForeach foreach_cb) MONO_INTERNAL;
MonoCounterCategory mono_counters_category_name_to_id (const char* name) MONO_INTERNAL;
const char* mono_counters_category_id_to_name (MonoCounterCategory id) MONO_INTERNAL;
void mono_counters_free_counter (MonoCounter* counter) MONO_INTERNAL;
/* Helpers */
/*
These helpers provide a typed struct and a bunch of helper functions:
Structs: IntCounter LongCounter, WordCounter, DoubleCounter
factory functions: mono_counters_new_int, mono_counters_new_const_int
update functions: mono_counters_int_set, mono_counters_int_inc, mono_counters_int_dec
read functions: mono_counters_int_get
*/
#define MK_COUNTER_HELPERS(TYPE, NAME, CAPITALIZED_NAME,COUNTER_TYPE) \
typedef struct { TYPE *pointer; } CAPITALIZED_NAME ##Counter; \
static void mono_counters_ ## NAME ## _set (CAPITALIZED_NAME ##Counter counter, TYPE value) { (*counter.pointer) += value; } \
static TYPE mono_counters_ ## NAME ## _get (CAPITALIZED_NAME ##Counter counter) { return *counter.pointer; } \
static void mono_counters_ ## NAME ## _inc (CAPITALIZED_NAME ##Counter counter) { (*counter.pointer) += 1; } \
static void mono_counters_ ## NAME ## _dec (CAPITALIZED_NAME ##Counter counter) { (*counter.pointer) -= 1; } \
static CAPITALIZED_NAME ##Counter mono_counters_new_ ## NAME (MonoCounterCategory category, const char *name, MonoCounterUnit unit, MonoCounterVariance variance) { \
CAPITALIZED_NAME ##Counter c = { mono_counters_new(category, name, MONO_COUNTER_TYPE_ ## COUNTER_TYPE, unit, variance) }; \
return c; \
} \
static void mono_counters_new_const_ ## NAME (MonoCounterCategory category, const char *name, MonoCounterUnit unit, TYPE value) { \
CAPITALIZED_NAME ##Counter c = { mono_counters_new(category, name, MONO_COUNTER_TYPE_ ## COUNTER_TYPE, unit, MONO_COUNTER_CONSTANT) }; \
mono_counters_ ## NAME ## _set (c, value); \
} \
MK_COUNTER_HELPERS(int,int,Int,INT)
MK_COUNTER_HELPERS(gint64,long,Long,LONG)
MK_COUNTER_HELPERS(ssize_t,word,Word,WORD)
MK_COUNTER_HELPERS(double,double,Double,DOUBLE)
#endif
+534 -106
View File
@@ -2,23 +2,34 @@
* Copyright 2006-2010 Novell
* Copyright 2011 Xamarin Inc
*/
#include "config.h"
#include <stdlib.h>
#include <glib.h>
#include "mono-counters.h"
#include "mono-counters-internals.h"
#include "mono-proclib.h"
typedef struct _MonoCounter MonoCounter;
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
struct _MonoCounter {
MonoCounter *next;
const char *name;
void *addr;
int type;
static MonoCounter *counters;
static int valid_mask = 0;
typedef struct _DataSource DataSource;
struct _DataSource {
DataSource *next;
CountersDataSourceGet get;
CountersDataSourceForeach foreach;
};
static DataSource *data_sources;
enum {
NO_CB,
CB_NO_ARG,
CB_WITH_ARG,
};
static MonoCounter *counters = NULL;
static int valid_mask = 0;
static int set_mask = 0;
/**
* mono_counters_enable:
@@ -32,6 +43,100 @@ mono_counters_enable (int section_mask)
valid_mask = section_mask & MONO_COUNTER_SECTION_MASK;
}
static MonoCounter*
mono_counters_new_full (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *addr)
{
MonoCounter *counter;
counter = g_new0 (MonoCounter, 1);
if (!counter)
return NULL;
counter->name = name;
counter->addr = addr;
counter->type = type;
counter->category = category;
counter->unit = unit;
counter->variance = variance;
return counter;
}
MonoCounter*
mono_counters_register_full (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *addr)
{
MonoCounter *counter = mono_counters_new_full (category, name, type, unit, variance, addr);
if (!counter)
return NULL;
/* Append */
if (counters) {
MonoCounter *item = counters;
while (item->next)
item = item->next;
item->next = counter;
} else {
counters = counter;
}
return counter;
}
/*
This function is a placeholder, it should eventually be replaced by code that allocs in the shm perfcounter arena.
*/
static void*
mono_counters_alloc_space (int size)
{
//FIXME actually alloc memory from perf-counters
return g_malloc0 (size);
}
/**
* mono_counters_new:
* @category: The category of this counter
* @name: The name of this counter
* @type: The type size of the counter
* @variance: The expected semantics of the values sampled.
*
* Register a new counter within the runtime.
*
* Avoid this function if possible and use the specialized helpers.
*
* Returns: The address of the counter.
*/
void*
mono_counters_new (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance)
{
const int sizes[] = { 4, 8, SIZEOF_VOID_P, 8 };
void *addr;
g_assert (type >= MONO_COUNTER_TYPE_INT && type < MONO_COUNTER_TYPE_MAX);
addr = mono_counters_alloc_space (sizes [type]);
if (!addr)
return NULL;
if (!mono_counters_register_full (category, name, type, unit, variance, addr))
return NULL; //FIXME release the counter memory?
return addr;
}
static int
section_to_category (int type)
{
switch (type & MONO_COUNTER_SECTION_MASK) {
case MONO_COUNTER_JIT:
return MONO_COUNTER_CAT_JIT;
case MONO_COUNTER_GC:
return MONO_COUNTER_CAT_GC;
case MONO_COUNTER_METADATA:
return MONO_COUNTER_CAT_METADATA;
case MONO_COUNTER_GENERICS:
return MONO_COUNTER_CAT_GENERICS;
case MONO_COUNTER_SECURITY:
return MONO_COUNTER_CAT_SECURITY;
default:
g_error ("Invalid section %x", type & MONO_COUNTER_SECTION_MASK);
}
}
/**
* mono_counters_register:
* @name: The name for this counters.
@@ -45,112 +150,85 @@ mono_counters_enable (int section_mask)
* It may be a function pointer if MONO_COUNTER_CALLBACK is specified:
* the function should return the value and take no arguments.
*/
void
void
mono_counters_register (const char* name, int type, void *addr)
{
MonoCounterCategory cat = section_to_category (type);
MonoCounterType counter_type;
MonoCounterUnit unit = MONO_COUNTER_UNIT_NONE;
MonoCounter *counter;
if (!(type & valid_mask))
return;
counter = malloc (sizeof (MonoCounter));
if (!counter)
return;
counter->name = name;
counter->type = type;
counter->addr = addr;
counter->next = NULL;
set_mask |= type;
/* Append */
if (counters) {
MonoCounter *item = counters;
while (item->next)
item = item->next;
item->next = counter;
} else {
counters = counter;
switch (type & MONO_COUNTER_TYPE_MASK) {
case MONO_COUNTER_INT:
case MONO_COUNTER_UINT:
counter_type = MONO_COUNTER_TYPE_INT;
break;
case MONO_COUNTER_LONG:
case MONO_COUNTER_ULONG:
counter_type = MONO_COUNTER_TYPE_LONG;
break;
case MONO_COUNTER_DOUBLE:
counter_type = MONO_COUNTER_TYPE_DOUBLE;
break;
case MONO_COUNTER_WORD:
counter_type = MONO_COUNTER_TYPE_WORD;
break;
case MONO_COUNTER_STRING:
g_error ("String counters no longer work");
case MONO_COUNTER_TIME_INTERVAL:
counter_type = MONO_COUNTER_TYPE_LONG;
unit = MONO_COUNTER_UNIT_TIME;
break;
default:
g_error ("Invalid type %x", type & MONO_COUNTER_TYPE_MASK);
}
counter = mono_counters_register_full (cat, name, counter_type, unit, MONO_COUNTER_VARIABLE, addr);
if (counter && (type & MONO_COUNTER_CALLBACK))
counter->callback_style = CB_NO_ARG;
}
typedef int (*IntFunc) (void);
typedef guint (*UIntFunc) (void);
typedef gint64 (*LongFunc) (void);
typedef guint64 (*ULongFunc) (void);
typedef gssize (*PtrFunc) (void);
typedef double (*DoubleFunc) (void);
typedef char* (*StrFunc) (void);
typedef int (*IntFunc2) (void*);
typedef gint64 (*LongFunc2) (void*);
typedef double (*DoubleFunc2) (void*);
#define ENTRY_FMT "%-36s: "
static void
dump_counter (MonoCounter *counter, FILE *outfile) {
int intval;
guint uintval;
gint64 int64val;
guint64 uint64val;
gssize wordval;
double dval;
const char *str;
switch (counter->type & MONO_COUNTER_TYPE_MASK) {
case MONO_COUNTER_INT:
if (counter->type & MONO_COUNTER_CALLBACK)
intval = ((IntFunc)counter->addr) ();
else
intval = *(int*)counter->addr;
fprintf (outfile, ENTRY_FMT "%d\n", counter->name, intval);
break;
case MONO_COUNTER_UINT:
if (counter->type & MONO_COUNTER_CALLBACK)
uintval = ((UIntFunc)counter->addr) ();
else
uintval = *(guint*)counter->addr;
fprintf (outfile, ENTRY_FMT "%u\n", counter->name, uintval);
break;
case MONO_COUNTER_LONG:
if (counter->type & MONO_COUNTER_CALLBACK)
int64val = ((LongFunc)counter->addr) ();
else
int64val = *(gint64*)counter->addr;
fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, (long long)int64val);
break;
case MONO_COUNTER_ULONG:
if (counter->type & MONO_COUNTER_CALLBACK)
uint64val = ((ULongFunc)counter->addr) ();
else
uint64val = *(guint64*)counter->addr;
fprintf (outfile, ENTRY_FMT "%llu\n", counter->name, (unsigned long long)uint64val);
break;
case MONO_COUNTER_WORD:
if (counter->type & MONO_COUNTER_CALLBACK)
wordval = ((PtrFunc)counter->addr) ();
else
wordval = *(gssize*)counter->addr;
#if SIZEOF_VOID_P == 8
fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, (gint64)wordval);
#else
fprintf (outfile, ENTRY_FMT "%d\n", counter->name, (gint)wordval);
switch (counter->type) {
#if SIZEOF_VOID_P == 4
case MONO_COUNTER_TYPE_WORD:
#endif
break;
case MONO_COUNTER_DOUBLE:
if (counter->type & MONO_COUNTER_CALLBACK)
dval = ((DoubleFunc)counter->addr) ();
else
dval = *(double*)counter->addr;
fprintf (outfile, ENTRY_FMT "%.4f\n", counter->name, dval);
break;
case MONO_COUNTER_STRING:
if (counter->type & MONO_COUNTER_CALLBACK)
str = ((StrFunc)counter->addr) ();
else
str = *(char**)counter->addr;
fprintf (outfile, ENTRY_FMT "%s\n", counter->name, str);
break;
case MONO_COUNTER_TIME_INTERVAL:
if (counter->type & MONO_COUNTER_CALLBACK)
int64val = ((LongFunc)counter->addr) ();
else
int64val = *(gint64*)counter->addr;
fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)int64val / 1000.0);
break;
case MONO_COUNTER_TYPE_INT: {
int value;
mono_counters_sample (counter, (char*)&value, 4);
fprintf (outfile, ENTRY_FMT "%d\n", counter->name, value);
break;
}
#if SIZEOF_VOID_P == 8
case MONO_COUNTER_TYPE_WORD:
#endif
case MONO_COUNTER_TYPE_LONG: {
gint64 value;
mono_counters_sample (counter, (char*)&value, 8);
if (counter->unit == MONO_COUNTER_UNIT_TIME)
fprintf (outfile, ENTRY_FMT "%.2f ms\n", counter->name, (double)value / 10000.0);
else
fprintf (outfile, ENTRY_FMT "%lld\n", counter->name, value);
break;
}
case MONO_COUNTER_TYPE_DOUBLE: {
double value;
mono_counters_sample (counter, (char*)&value, 8);
fprintf (outfile, ENTRY_FMT "%.4f ms\n", counter->name, value);
break;
}
}
}
@@ -164,11 +242,11 @@ section_names [][10] = {
};
static void
mono_counters_dump_section (int section, FILE *outfile)
mono_counters_dump_category (MonoCounterCategory category, FILE *outfile)
{
MonoCounter *counter = counters;
while (counter) {
if (counter->type & section)
if (counter->category == category)
dump_counter (counter, outfile);
counter = counter->next;
}
@@ -189,9 +267,9 @@ mono_counters_dump (int section_mask, FILE *outfile)
if (!counters)
return;
for (j = 0, i = MONO_COUNTER_JIT; i < MONO_COUNTER_LAST_SECTION; j++, i <<= 1) {
if ((section_mask & i) && (set_mask & i)) {
if ((section_mask & i)) {
fprintf (outfile, "\n%s statistics\n", section_names [j]);
mono_counters_dump_section (i, outfile);
mono_counters_dump_category (section_to_category (i), outfile);
}
}
@@ -266,6 +344,161 @@ mono_runtime_resource_limit (int resource_type, uintptr_t soft_limit, uintptr_t
return 1;
}
enum {
CPU_LOAD_AVG_1 = MONO_PROCESS_END + 1,
CPU_LOAD_AVG_5 = MONO_PROCESS_END + 2,
CPU_LOAD_AVG_15 = MONO_PROCESS_END + 3,
};
static double
sample_cpu_doule (void *arg)
{
int kind = GPOINTER_TO_INT (arg);
double load [3];
#ifdef TARGET_MACH
if (getloadavg (load, 3) > 0)
return load [kind - CPU_LOAD_AVG_1];
#else
FILE *f;
char buffer[512];
int len;
char *first, *second, *third;
f = fopen ("/proc/loadavg", "r");
if (!f)
return 0;
len = fread (buffer, 1, sizeof (buffer), f);
fclose (f);
if (len <= 0)
return 0;
first = buffer;
second = strchr (first, ' ') + 1;
third = strchr (second, ' ') + 1;
second [-1] = 0;
third [-1] = 0;
*strchr (third, ' ') = 0;
printf ("vals %s %s %s\n", first, second, third);
load [0] = strtod (first, NULL);
load [1] = strtod (second, NULL);
load [2] = strtod (third, NULL);
return load [kind - CPU_LOAD_AVG_1];
#endif
return 0;
}
static gint64
sample_cpu (void *arg)
{
int kind = GPOINTER_TO_INT (arg);
//FIXME replace getpid with something portable.
return mono_process_get_data (GINT_TO_POINTER (getpid ()), kind);
}
void*
mono_counters_new_synt (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *addr)
{
MonoCounter *counter = NULL;
counter = mono_counters_new_full (category, name, MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_TIME, MONO_COUNTER_VARIABLE, addr);
if (!counter)
return NULL;
counter->is_synthetic = TRUE;
return counter;
}
void*
mono_counters_new_synt_func (MonoCounterCategory category, const char *name, MonoCounterType type, MonoCounterUnit unit, MonoCounterVariance variance, void *fun_addr, void *user_arg)
{
MonoCounter *counter = NULL;
counter = mono_counters_new_full (category, name, MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_TIME, MONO_COUNTER_VARIABLE, fun_addr);
if (!counter)
return NULL;
counter->is_synthetic = TRUE;
counter->callback_style = CB_WITH_ARG;
counter->user_arg = user_arg;
return counter;
}
static const char* sys_counter_names[] = {
"User Time",
"System Time",
"Total Time",
"Working Set",
"Private Bytes",
"Virtual Bytes",
"Page Faults",
"CPU Load Average - 1min",
"CPU Load Average - 5min",
"CPU Load Average - 15min",
NULL
};
static MonoCounter*
get_sys_counter (const char *name)
{
MonoCounter *counter = NULL;
int type = 0;
if (!strcmp (name, "User Time")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "User Time", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_TIME, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_USER_TIME;
} else if (!strcmp (name, "System Time")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "System Time", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_TIME, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_SYSTEM_TIME;
} else if (!strcmp (name, "Total Time")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "Total Time", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_TIME, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_TOTAL_TIME;
} else if (!strcmp (name, "Working Set")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "Working Set", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_WORKING_SET;
} else if (!strcmp (name, "Private Bytes")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "Private Bytes", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_PRIVATE_BYTES;
} else if (!strcmp (name, "Virtual Bytes")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "Virtual Bytes", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_BYTES, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_VIRTUAL_BYTES;
} else if (!strcmp (name, "Page Faults")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "Page Faults", MONO_COUNTER_TYPE_LONG, MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_VARIABLE, NULL);
type = MONO_PROCESS_FAULTS;
} else if (!strcmp (name, "CPU Load Average - 1min")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "CPU Load Average - 1min", MONO_COUNTER_TYPE_DOUBLE, MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_VARIABLE, NULL);
type = CPU_LOAD_AVG_1;
} else if (!strcmp (name, "CPU Load Average - 5min")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "CPU Load Average - 5min", MONO_COUNTER_TYPE_DOUBLE, MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_VARIABLE, NULL);
type = CPU_LOAD_AVG_5;
} else if (!strcmp (name, "CPU Load Average - 15min")) {
counter = mono_counters_new_full (MONO_COUNTER_CAT_SYS, "CPU Load Average - 15min", MONO_COUNTER_TYPE_DOUBLE, MONO_COUNTER_UNIT_EVENTS, MONO_COUNTER_VARIABLE, NULL);
type = CPU_LOAD_AVG_15;
}
if (!counter)
return NULL;
if (type > MONO_PROCESS_END)
counter->addr = &sample_cpu_doule;
else
counter->addr = &sample_cpu;
counter->is_synthetic = TRUE;
counter->callback_style = CB_WITH_ARG;
counter->user_arg = GINT_TO_POINTER (type);
return counter;
}
static gboolean
enum_sys_counter (CountersEnumCallback cb)
{
int i;
const char *cat = mono_counters_category_id_to_name (MONO_COUNTER_CAT_SYS);
for (i = 0; sys_counter_names [i]; ++i) {
if (!cb (cat, sys_counter_names [i]))
return FALSE;
}
return TRUE;
}
/**
* mono_runtime_resource_set_callback:
* @callback: a function pointer
@@ -280,4 +513,199 @@ mono_runtime_resource_set_callback (MonoResourceCallback callback)
limit_reached = callback;
}
MonoCounter*
mono_counters_get (const char *cat_name, const char* name)
{
MonoCounter *counter = counters;
DataSource *ds;
MonoCounterCategory category = mono_counters_category_name_to_id (cat_name);
if (category < MONO_COUNTER_CAT_MAX) {
while (counter) {
if (counter->category == category && !strcmp (counter->name, name))
return counter;
counter = counter->next;
}
if (category == MONO_COUNTER_CAT_SYS) {
counter = get_sys_counter (name);
if (counter)
return counter;
}
} else {
for (ds = data_sources; ds; ds = ds->next) {
counter = ds->get (cat_name, name);
if (counter)
return counter;
}
}
return NULL;
}
void
mono_counters_foreach (CountersEnumCallback cb)
{
DataSource *ds;
MonoCounter *counter;
for (counter = counters; counter; counter = counter->next) {
if (!cb (mono_counters_category_id_to_name (counter->category), counter->name))
return;
}
if (!enum_sys_counter (cb))
return;
for (ds = data_sources; ds; ds = ds->next) {
if (!ds->foreach (cb))
return;
}
}
void
mono_counters_add_data_source (CountersDataSourceGet get_cb, CountersDataSourceForeach foreach_cb)
{
DataSource *ds = g_new0 (DataSource, 1);
ds->get = get_cb;
ds->foreach = foreach_cb;
ds->next = data_sources;
data_sources = ds;
}
int
mono_counters_sample (MonoCounter* counter, char* buffer, int size)
{
switch (counter->type) {
#if SIZEOF_VOID_P == 4
case MONO_COUNTER_TYPE_WORD:
#endif
case MONO_COUNTER_TYPE_INT: {
int value;
if (size < 4)
return -1;
switch (counter->callback_style) {
case NO_CB:
value = *(int*)counter->addr;
break;
case CB_NO_ARG:
value = ((IntFunc)counter->addr) ();
break;
case CB_WITH_ARG:
value = ((IntFunc2)counter->addr) (counter->user_arg);
break;
}
memcpy (buffer, &value, 4);
return 4;
}
#if SIZEOF_VOID_P == 8
case MONO_COUNTER_TYPE_WORD:
#endif
case MONO_COUNTER_TYPE_LONG: {
gint64 value;
if (size < 8)
return -1;
switch (counter->callback_style) {
case NO_CB:
value = *(gint64*)counter->addr;
break;
case CB_NO_ARG:
value = ((LongFunc)counter->addr) ();
break;
case CB_WITH_ARG:
value = ((LongFunc2)counter->addr) (counter->user_arg);
break;
}
memcpy (buffer, &value, 8);
return 8;
}
case MONO_COUNTER_TYPE_DOUBLE: {
double value;
if (size < 8)
return -1;
switch (counter->callback_style) {
case NO_CB:
value = *(double*)counter->addr;
break;
case CB_NO_ARG:
value = ((DoubleFunc)counter->addr) ();
break;
case CB_WITH_ARG:
value = ((DoubleFunc2)counter->addr) (counter->user_arg);
break;
}
memcpy (buffer, &value, 8);
return 8;
}
}
return -1;
}
int
mono_counters_size (MonoCounter* counter)
{
switch (counter->type) {
case MONO_COUNTER_TYPE_INT:
#if SIZEOF_VOID_P == 4
case MONO_COUNTER_TYPE_WORD:
#endif
return 4;
case MONO_COUNTER_TYPE_LONG:
#if SIZEOF_VOID_P == 8
case MONO_COUNTER_TYPE_WORD:
#endif
case MONO_COUNTER_TYPE_DOUBLE:
return 8;
}
return -1;
}
/* Keep this in sync with the MonoCounterCategory enum */
static const char* category_names[] = {
"Mono JIT",
"Mono GC",
"Mono Metadata",
"Mono Generics",
"Mono Security",
"Mono Thread",
"Mono ThreadPool",
"Mono System",
"Custom",
};
MonoCounterCategory
mono_counters_category_name_to_id (const char* name)
{
int i;
for (i = 0; i < MONO_COUNTER_CAT_MAX; ++i) {
if (!strcmp (category_names [i], name))
return i;
}
return -1;
}
const char*
mono_counters_category_id_to_name (MonoCounterCategory id)
{
if (id < 0 || id >= MONO_COUNTER_CAT_MAX)
return NULL;
return category_names [id];
}
void
mono_counters_free_counter (MonoCounter *counter)
{
if (counter->is_synthetic) {
if (counter->category == MONO_COUNTER_CAT_CUSTOM)
g_free ((char*)counter->name);
g_free (counter);
}
}