From 786548f0c1b76eacb64dc37fa3db8a5f2d68fbc0 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Mon, 14 Sep 2020 10:47:47 -0400 Subject: [PATCH] [mono] Enable startup stats timer on wasm (#41994) * 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 --- src/mono/mono/mini/mini-posix.c | 2 +- src/mono/mono/utils/mono-counters.c | 21 ++++++++++++++++++--- src/mono/mono/utils/mono-time.c | 19 +++++++++++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/mono/mono/mini/mini-posix.c b/src/mono/mono/mini/mini-posix.c index f477464aa1d26..cfafeb3141027 100644 --- a/src/mono/mono/mini/mini-posix.c +++ b/src/mono/mono/mini/mini-posix.c @@ -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 @@ -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)) { diff --git a/src/mono/mono/utils/mono-counters.c b/src/mono/mono/utils/mono-counters.c index cc04809e40a39..67b5269dc7c70 100644 --- a/src/mono/mono/utils/mono-counters.c +++ b/src/mono/mono/utils/mono-counters.c @@ -8,9 +8,11 @@ #include #include #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 @@ -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; @@ -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; @@ -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) { @@ -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)); diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index 25e22aea2a5d7..05dd2b9a6fd0e 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -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 @@ -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