-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add nanosecond resolution support on unixes to jl_gettimeofday
and time()
#45023
base: master
Are you sure you want to change the base?
Changes from all commits
2aca432
62f1433
8ed53d7
35bda41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -176,13 +176,20 @@ struct TimeVal | |||||||
usec::Int64 | ||||||||
end | ||||||||
|
||||||||
function TimeVal() | ||||||||
tv = Ref{TimeVal}() | ||||||||
status = ccall(:jl_gettimeofday, Cint, (Ref{TimeVal},), tv) | ||||||||
struct TimeSpec | ||||||||
sec::Int64 | ||||||||
nsec::Int64 | ||||||||
end | ||||||||
|
||||||||
function TimeSpec() | ||||||||
ts = Ref{TimeSpec}() | ||||||||
status = ccall(:jl_gettimeofday, Cint, (Ref{TimeSpec},), ts) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could now go through libuv to avoid our need for a custom wrapper (and get 100-nanosecond precision)
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this have resolution guarantees? I can't seem to find that in the libuv docs 🤔 |
||||||||
status != 0 && error("unable to determine current time: ", status) | ||||||||
return tv[] | ||||||||
return ts[] | ||||||||
end | ||||||||
|
||||||||
Base.convert(::Type{TimeVal}, ts::TimeSpec) = TimeVal(ts.sec, div(ts.nsec, 1000)) | ||||||||
|
||||||||
""" | ||||||||
TmStruct([seconds]) | ||||||||
|
||||||||
|
@@ -280,7 +287,7 @@ time(tm::TmStruct) = Float64(ccall(:mktime, Int, (Ref{TmStruct},), tm)) | |||||||
""" | ||||||||
time() -> Float64 | ||||||||
|
||||||||
Get the system time in seconds since the epoch, with fairly high (typically, microsecond) resolution. | ||||||||
Get the system time in seconds since the epoch. Resolution is system dependent and limited to milliseconds on windows, with (up to) nanosecond resolution on other platforms. | ||||||||
""" | ||||||||
time() = ccall(:jl_clock_now, Float64, ()) | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
#if defined(_OS_WINDOWS_) | ||
#include <sys/timeb.h> | ||
#else | ||
#include <sys/time.h> | ||
#include <time.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not 100% sure about this, but was required to get it to build on my system. CI will tell if this was a mistake 🤷 |
||
#include <sys/select.h> | ||
#endif | ||
|
||
|
@@ -23,12 +23,13 @@ JL_DLLEXPORT int jl_gettimeofday(struct jl_timeval *jtv) | |
struct __timeb64 tb; | ||
errno_t code = _ftime64_s(&tb); | ||
jtv->sec = tb.time; | ||
jtv->usec = tb.millitm * 1000; | ||
jtv->nsec = tb.millitm * 1000000; | ||
#else | ||
struct timeval tv; | ||
int code = gettimeofday(&tv, NULL); | ||
jtv->sec = tv.tv_sec; | ||
jtv->usec = tv.tv_usec; | ||
struct timespec ts; | ||
int code = clock_gettime(CLOCK_REALTIME, &ts); | ||
// TODO: warn/error on EINVAL/EOVERFLOW? | ||
jtv->sec = ts.tv_sec; | ||
jtv->nsec = ts.tv_nsec; | ||
#endif | ||
return code; | ||
} | ||
|
@@ -37,7 +38,7 @@ JL_DLLEXPORT double jl_clock_now(void) | |
{ | ||
struct jl_timeval now; | ||
jl_gettimeofday(&now); | ||
return now.sec + now.usec * 1e-6; | ||
return now.sec + now.nsec * 1e-9; | ||
} | ||
|
||
void sleep_ms(int ms) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a Clong, which is sometimes Int32 and sometimes Int64, and sometimes just plain inconvenient to know what the platform will do. But if we use libuv, then it is explicitly Int32.