Skip to content

Commit

Permalink
[mono] Enable startup stats timer on wasm (#41994)
Browse files Browse the repository at this point in the history
* Set clock in mono_clock_init if available and add wasm definitions

This matches the OSX behavior and cleans things up a bit. We also now call this cross-platform, so don't error when called on Windows

* Add new "real time" counter based on clock_gettime

The initial value is set early in mono_main as part of mono_counters_init. This is needed because /proc/stat does not exist with emscripten and we need a way to measure time on wasm
  • Loading branch information
CoffeeFlux authored Sep 14, 2020
1 parent cc68ca5 commit 786548f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/mono/mono/mini/mini-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ static clock_serv_t sampling_clock;
static void
clock_init_for_profiler (MonoProfilerSampleMode mode)
{
mono_clock_init (&sampling_clock);
}

static void
Expand Down Expand Up @@ -691,7 +692,6 @@ sampling_thread_func (gpointer unused)
goto init;
}

mono_clock_init (&sampling_clock);
clock_init_for_profiler (mode);

for (guint64 sleep = mono_clock_get_time_ns (sampling_clock); mono_atomic_load_i32 (&sampling_thread_running); clock_sleep_ns_abs (sleep)) {
Expand Down
21 changes: 18 additions & 3 deletions src/mono/mono/utils/mono-counters.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <stdlib.h>
#include <glib.h>
#include "config.h"
#include "mono-counters.h"
#include "mono-proclib.h"
#include "mono-os-mutex.h"

#include "mono/utils/mono-counters.h"
#include "mono/utils/mono-proclib.h"
#include "mono/utils/mono-os-mutex.h"
#include "mono/utils/mono-time.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
Expand All @@ -27,6 +29,9 @@ struct _MonoCounter {
static MonoCounter *counters = NULL;
static mono_mutex_t counters_mutex;

static mono_clock_id_t real_time_clock;
static guint64 real_time_start;

static volatile gboolean initialized = FALSE;

static int valid_mask = 0;
Expand Down Expand Up @@ -131,6 +136,9 @@ mono_counters_init (void)

mono_os_mutex_init (&counters_mutex);

mono_clock_init (&real_time_clock);
real_time_start = mono_clock_get_time_ns (real_time_clock);

initialize_system_counters ();

initialized = TRUE;
Expand Down Expand Up @@ -311,6 +319,12 @@ total_time (void)
return mono_process_get_data (GINT_TO_POINTER (mono_process_current_pid ()), MONO_PROCESS_TOTAL_TIME);
}

static guint64
real_time (void)
{
return mono_clock_get_time_ns (real_time_clock) - real_time_start;
}

static gint64
working_set (void)
{
Expand Down Expand Up @@ -411,6 +425,7 @@ initialize_system_counters (void)
register_internal ("User Time", SYSCOUNTER_TIME, (gpointer) &user_time, sizeof (gint64));
register_internal ("System Time", SYSCOUNTER_TIME, (gpointer) &system_time, sizeof (gint64));
register_internal ("Total Time", SYSCOUNTER_TIME, (gpointer) &total_time, sizeof (gint64));
register_internal ("Real Time", SYSCOUNTER_TIME, (gpointer) &real_time, sizeof (guint64));
register_internal ("Working Set", SYSCOUNTER_BYTES, (gpointer) &working_set, sizeof (gint64));
register_internal ("Private Bytes", SYSCOUNTER_BYTES, (gpointer) &private_bytes, sizeof (gint64));
register_internal ("Virtual Bytes", SYSCOUNTER_BYTES, (gpointer) &virtual_bytes, sizeof (gint64));
Expand Down
19 changes: 11 additions & 8 deletions src/mono/mono/utils/mono-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,16 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id)
return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec;
}

#elif defined(__linux__)
// TODO: Potentially make this the default?
// Can we assume clock_gettime exists on all modern POSIX systems? Maybe add a better check for it in configure.ac?
#elif defined(__linux__) || defined (TARGET_WASM)

void
mono_clock_init (mono_clock_id_t *clk_id)
{
{
#ifdef HAVE_CLOCK_MONOTONIC
*clk_id = CLOCK_MONOTONIC;
#endif
}

void
Expand All @@ -307,22 +312,20 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id)
void
mono_clock_init (mono_clock_id_t *clk_id)
{
// TODO: need to implement this function for PC
g_assert_not_reached ();
// TODO: need to implement this function for Windows
}

void
mono_clock_cleanup (mono_clock_id_t clk_id)
{
// TODO: need to implement this function for PC
g_assert_not_reached ();
// TODO: need to implement this function for Windows
}

guint64
mono_clock_get_time_ns (mono_clock_id_t clk_id)
{
// TODO: need to implement time stamp function for PC
g_assert_not_reached ();
// TODO: need to implement time stamp function for Windows
return 0;
}

#endif

0 comments on commit 786548f

Please sign in to comment.