Refactor get_time_stamp_ns
authorfanyang-mono <yangfan@microsoft.com>
Sat, 4 Apr 2020 04:00:06 +0000 (00:00 -0400)
committerfanyang-mono <yangfan@microsoft.com>
Sat, 4 Apr 2020 04:00:06 +0000 (00:00 -0400)
src/mono/mono/mini/mini-posix.c
src/mono/mono/mini/mini-runtime.c
src/mono/mono/utils/mono-time.c
src/mono/mono/utils/mono-time.h

index 7f95374..562141a 100644 (file)
@@ -70,6 +70,7 @@
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/os-event.h>
 #include <mono/utils/mono-state.h>
+#include <mono/utils/mono-time.h>
 #include <mono/mini/debugger-state-machine.h>
 
 #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;
 
index d62d555..e051e70 100644 (file)
@@ -70,6 +70,7 @@
 #include <mono/utils/mono-compiler.h>
 #include <mono/utils/mono-proclib.h>
 #include <mono/utils/mono-state.h>
+#include <mono/utils/mono-time.h>
 #include <mono/metadata/w32handle.h>
 #include <mono/metadata/threadpool.h>
 
@@ -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);
 }
index 6874cc3..e03efae 100644 (file)
@@ -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
index 3f0634a..6b4a95e 100644 (file)
@@ -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;