Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get_time_stamp_ns #34536

Merged
merged 14 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions src/mono/mono/mini/mini-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -745,7 +719,7 @@ sampling_thread_func (gpointer unused)

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;

Expand Down
16 changes: 4 additions & 12 deletions src/mono/mono/mini/mini-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions src/mono/mono/utils/mono-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,39 @@ mono_100ns_datetime_from_timeval (struct timeval tv)

#endif

#ifdef HOST_DARWIN

static clock_serv_t sampling_clock_service;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to be initialized in clock_init() under mono-posix.c, but it's now always 0.


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;
Copy link
Contributor

@lpereira lpereira Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same: the POSIX version of clock_init() would initialize this, but it's now always zero. The POSIX version, also, will initialize this variable considering a MonoProfilerSampleMode, so this patch will break profiling.


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
2 changes: 2 additions & 0 deletions src/mono/mono/utils/mono-time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down