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

Implement issue 13433 - add option for using coarser realtime clock. #2584

Merged
merged 1 commit into from Jun 29, 2015

Conversation

jmdavis
Copy link
Member

@jmdavis jmdavis commented Oct 4, 2014

This adds an option for getting the time using the coarser, faster
realtime clock on systems that support it for those who need to get the
time frequently (and thus quickly) but don't need it to be particularly
precise.

https://issues.dlang.org/show_bug.cgi?id=13433

The OSX change is because it's the only one using gettimeofday. I figured that it was better to change that to go off of the OS version rather than whether clock_gettime existed or not.

@jmdavis
Copy link
Member Author

jmdavis commented Oct 4, 2014

dlang/druntime#981 should fix the build error on FreeBSD.

}

/++ Ditto +/
static SysTime currTime(bool coarse) @safe
Copy link
Member

Choose a reason for hiding this comment

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

Booleans like this are pretty bad interface design. It's impossible to see at a glance what the boolean is actually for, i.e. it can't be inferred from the function name, so it has to be memorized from the documentation.

Can we please use an enum instead?

Copy link
Member

Choose a reason for hiding this comment

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

Another option would be to split currTime and currStdTime into two functions, one that gives coarse time and one that doesn't. This reduces branching as the decision to use coarse time or not is almost always going to be known at compile-time.

Copy link
Member

Choose a reason for hiding this comment

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

Can we please use an enum instead?

Isn't it a job for std.typecons.Flag ?

Copy link
Member

Choose a reason for hiding this comment

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

+1. However, I remember some time ago that somebody objected to std.typecons.Flag for some reason I can't remember... nevertheless, I agree we should use it, it does make the code a lot clearer.

Copy link
Member

Choose a reason for hiding this comment

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

@quickfur That may have been here: #1965 (comment). The only problem with it that I can see is that the user has to import std.typecons to use the Yes and No structs.

@jmdavis
Copy link
Member Author

jmdavis commented Oct 14, 2014

To deal with the concerns raised about using a bool parameter, I've created this druntime PR for adding an enum for the type of clock to use: dlang/druntime#990

Once that has been accepted, I'll update this pull to use it.

@jmdavis
Copy link
Member Author

jmdavis commented Jun 6, 2015

Okay. This has been updated to use core.time.ClockType rather than a boolean for whether the clock should be coarse or not (so, in addition to the normal clock, there is now a coarse clock, a precise clock, and a clock with second precision), and the argument is a compile-time argument, since that works better (and is what core.time is doing with MonoTime, though in this case, it's just a couple of functions being templated rather than a whole type).

assert(norm1 <= norm2, format("%s %s", norm1, norm2));
assert(abs(norm1 - norm2) <= limit);

auto coarse1 = Clock.currStdTime!(ClockType.coarse);
Copy link
Member

Choose a reason for hiding this comment

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

These 3 quartets of lines are exactly the same - foreach over a TypeTuple!(ClockType.coarse, ClockType.precise, ClockType.second) ?

@jmdavis
Copy link
Member Author

jmdavis commented Jun 7, 2015

Okay. The tests were updated to use TypeTuple as suggested.

@d-random-contributor
Copy link

FWIW, C time function returns a value with an implementation-defined meaning, it's neither unix time, nor UTC, nor a number of seconds. It's just unix happens to define C time to be unix time. You should use gmtime or localtime to get anything meaningful from it.

throw new TimeException("Failed in clock_gettime().");

if(clock_gettime(clockArg, &ts) != 0)
throw new TimeException("Call to gettimeofday() failed");
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this say "Call to clock_gettime"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. It's gettimeofday for Mac OS X, and clock_gettime for Linux and FreeBSD, and apparently, I screwed up the message when I was doing some copy-pasting. Fixed now.

This adds an option for getting the time using an alternative clock
chosen via core.time.ClockType, including access to a coarser clock as
requested by issue# 13433.

For ClockType.normal, the normal clock which has always been used is
used.

For ClockType.coarse, a faster clock is used if available even if it's
coarser so long as it still has sub-second precision, and the normal
clock is used if no such clock is available.

For ClockType.precise, a more precise clock is used if available
(currently only on FreeBSD), otherwise the normal clock is used.

For ClockType.second, a faster clock is used which only has second
precision. If no such clock is available, then the normal clock is used,
but the result is truncated to second precision.
@jmdavis
Copy link
Member Author

jmdavis commented Jun 17, 2015

Any more comments on this or things that need to be changed before this can be merged?

@DmitryOlshansky
Copy link
Member

LGTM

@DmitryOlshansky
Copy link
Member

Auto-merge toggled on

DmitryOlshansky added a commit that referenced this pull request Jun 29, 2015
Implement issue 13433 - add option for using coarser realtime clock.
@DmitryOlshansky DmitryOlshansky merged commit 1433072 into dlang:master Jun 29, 2015
@jmdavis jmdavis deleted the 13433 branch May 5, 2017 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants