From 5cf0c743815169d06629a3abf662301fd3d43474 Mon Sep 17 00:00:00 2001 From: fanyang-mono Date: Sat, 4 Apr 2020 00:00:06 -0400 Subject: [PATCH] Refactor get_time_stamp_ns --- src/mono/mono/mini/mini-posix.c | 32 +++----------------------------- src/mono/mono/mini/mini-runtime.c | 16 ++++------------ src/mono/mono/utils/mono-time.c | 36 ++++++++++++++++++++++++++++++++++++ src/mono/mono/utils/mono-time.h | 2 ++ 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/mono/mono/mini/mini-posix.c b/src/mono/mono/mini/mini-posix.c index 7f95374..562141a 100644 --- a/src/mono/mono/mini/mini-posix.c +++ b/src/mono/mono/mini/mini-posix.c @@ -70,6 +70,7 @@ #include #include #include +#include #include #include "mini.h" @@ -545,22 +546,6 @@ clock_cleanup (void) g_error ("%s: mach_port_deallocate () returned %d", __func__, ret); } -static guint64 -clock_get_time_ns (void) -{ - kern_return_t ret; - mach_timespec_t mach_ts; - - do { - ret = clock_get_time (sampling_clock_service, &mach_ts); - } while (ret == KERN_ABORTED); - - if (ret != KERN_SUCCESS) - g_error ("%s: clock_get_time () returned %d", __func__, ret); - - return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec; -} - static void clock_sleep_ns_abs (guint64 ns_abs) { @@ -618,17 +603,6 @@ clock_cleanup (void) { } -static guint64 -clock_get_time_ns (void) -{ - struct timespec ts; - - if (clock_gettime (sampling_posix_clock, &ts) == -1) - g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno); - - return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec; -} - static void clock_sleep_ns_abs (guint64 ns_abs) { @@ -675,7 +649,7 @@ clock_sleep_ns_abs (guint64 ns_abs) * nanoseconds). */ do { - diff = (gint64) ns_abs - (gint64) clock_get_time_ns (); + diff = (gint64) ns_abs - (gint64) mono_clock_get_time_ns (); if (diff <= 0) break; @@ -745,7 +719,7 @@ init: clock_init (mode); - for (guint64 sleep = clock_get_time_ns (); mono_atomic_load_i32 (&sampling_thread_running); clock_sleep_ns_abs (sleep)) { + for (guint64 sleep = mono_clock_get_time_ns (); mono_atomic_load_i32 (&sampling_thread_running); clock_sleep_ns_abs (sleep)) { uint32_t freq; MonoProfilerSampleMode new_mode; diff --git a/src/mono/mono/mini/mini-runtime.c b/src/mono/mono/mini/mini-runtime.c index d62d555..e051e70 100644 --- a/src/mono/mono/mini/mini-runtime.c +++ b/src/mono/mono/mini/mini-runtime.c @@ -70,6 +70,7 @@ #include #include #include +#include #include #include @@ -1980,7 +1981,6 @@ typedef struct } JitCodeLoadRecord; static void add_file_header_info (FileHeader *header); -static guint64 get_time_stamp_ns (void); static void add_basic_JitCodeLoadRecord_info (JitCodeLoadRecord *record); void @@ -2021,18 +2021,10 @@ add_file_header_info (FileHeader *header) header->elf_mach = ELF_MACHINE; header->pad1 = 0; header->pid = perf_dump_pid; - header->timestamp = get_time_stamp_ns (); + header->timestamp = mono_clock_get_time_ns (); header->flags = 0; } -static guint64 -get_time_stamp_ns (void) -{ - struct timespec ts; - int result = clock_gettime (CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000000ULL + ts.tv_nsec; -} - void mono_emit_jit_dump (MonoJitInfo *jinfo, gpointer code) { @@ -2055,7 +2047,7 @@ mono_emit_jit_dump (MonoJitInfo *jinfo, gpointer code) // TODO: write debugInfo and unwindInfo immediately before the JitCodeLoadRecord (while lock is held). - record.header.timestamp = get_time_stamp_ns (); + record.header.timestamp = mono_clock_get_time_ns (); fwrite (&record, sizeof (record), 1, perf_dump_file); fwrite (jinfo->d.method->name, nameLen + 1, 1, perf_dump_file); @@ -2069,7 +2061,7 @@ static void add_basic_JitCodeLoadRecord_info (JitCodeLoadRecord *record) { record->header.id = JIT_CODE_LOAD; - record->header.timestamp = get_time_stamp_ns (); + record->header.timestamp = mono_clock_get_time_ns (); record->pid = perf_dump_pid; record->tid = syscall (SYS_gettid); } diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index 6874cc3..e03efae 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -229,3 +229,39 @@ mono_100ns_datetime_from_timeval (struct timeval tv) #endif +#ifdef HOST_DARWIN + +static clock_serv_t sampling_clock_service; + +guint64 +mono_clock_get_time_ns (void) +{ + kern_return_t ret; + mach_timespec_t mach_ts; + + do { + ret = clock_get_time (sampling_clock_service, &mach_ts); + } while (ret == KERN_ABORTED); + + if (ret != KERN_SUCCESS) + g_error ("%s: clock_get_time () returned %d", __func__, ret); + + return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec; +} + +#else + +static clockid_t sampling_posix_clock; + +guint64 +mono_clock_get_time_ns (void) +{ + struct timespec ts; + + if (clock_gettime (sampling_posix_clock, &ts) == -1) + g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno); + + return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec; +} + +#endif diff --git a/src/mono/mono/utils/mono-time.h b/src/mono/mono/utils/mono-time.h index 3f0634a..6b4a95e 100644 --- a/src/mono/mono/utils/mono-time.h +++ b/src/mono/mono/utils/mono-time.h @@ -29,6 +29,8 @@ gint64 mono_100ns_datetime (void); gint64 mono_100ns_datetime_from_timeval (struct timeval tv); #endif +guint64 mono_clock_get_time_ns (void); + /* Stopwatch class for internal runtime use */ typedef struct { gint64 start, stop; -- 2.7.4