threading: use std::chrono for timestamps #9566

Open
wants to merge 2 commits into
from

Conversation

Projects

In progress in Boost → C++11 migration

7 participants
Member

theuni commented Jan 16, 2017

Obviously not for 0.14.

Though boost's chrono hides the racy gmtime() issue nicely, it's really just using gmtime_r internally. This switches to the same thing, but without the boost indirection.

PRing this separately from other boost removals because it's not a 1:1 replacement.

Contributor

fanquake commented Jan 17, 2017

Failed on both Windows builds with:

utiltime.cpp: In function ‘const tm gmtime_int(time_t)’:
utiltime.cpp:31:25: error: cannot convert ‘time_t* {aka long int*}’ to ‘tm*’ for argument ‘1’ to ‘errno_t gmtime_s(tm*, const time_t*)’
     gmtime_s(&time, &out);

theuni added some commits Jan 16, 2017

@theuni theuni time: Use std::chrono for time rather than boost
Unfortunately, there's still no standard way of printing the current time in a
threadsafe way. Digging down into boost's approach, they simply use gmtime_r
when possible, as guessed by availability macros.

We now use the same approach, but use autotools to detect whether gmtime_r or
gmtime_s can be used, or as a fallback, the racy gmtime.

Note that MilliSleep was not replaced because it is an interruption point. That
can be done once boost threads are all gone.
0512b02
@theuni theuni time: add runtime sanity check
std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
to use the Unix epoch timestamp, but in practice they almost certainly will.
Any differing behavior will be assumed to be an error, unless certain
platforms prove to consistently deviate, at which point we'll cope with it
by adding offsets.

Do a quick runtime check to verify that
time_t(0) == std::chrono::system_clock's epoch time == unix epoch.
9bd32ce
Member

theuni commented Jan 17, 2017

Of course windows has reversed arguments. Why wouldn't it?

Pushed the reversal.

+ return out;
+}
+
+bool ChronoSanityCheck()
@laanwj

laanwj Jan 17, 2017 edited

Owner

Needing something like this makes me wonder if we're not better off just using the C time functions, which guarantee being based on the UNIX epoch.

Especially as we're already probing for gmtime_int anyway. This seems double.

Do we need something that is only offered by std::chrono? (or needs much less code with std::chrono)

@theuni

theuni Jan 17, 2017

Member

@laanwj Best I can tell from google and reading the c spec, it's not guaranteed to be based on the UNIX epoch either :(. Of course, I've never seen an implementation that isn't, as that would likely break years worth of assumptions. And based on that, I would assume that c++ implementations will be using the underlying c implementation.

So my logic was: if we can run a quick sanity check here to be sure that c/c++/unix epoch are all aligned, then we can use c apis and std::chrono interchangeably throughout the codebase without worry.

As for why chrono here, it was to avoid gettimeofday portability issues. If you'd prefer to work around those instead, no problem.

You're right though that DateTimeStrFormat is kinda clunky. I only used c++ there to avoid c string size guessing. I'm also happy to switch that if you'd prefer.

@laanwj

laanwj Jan 17, 2017 edited

Owner

Best I can tell from google and reading the c spec, it's not guaranteed to be based on the UNIX epoch either :(

Which ones? The man page of "gmtime" tells me the following:

       The  ctime(),  gmtime()  and localtime() functions all take an argument of data type time_t, which represents calendar time.  When interpreted as an absolute time value, it represents the
       number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

Same for gettimeofday:

       and gives the number of seconds and microseconds since the Epoch (see time(2)).  The tz argument is a struct timezone:

If using std::chrono makes anything less hassle I'm fine with using it. It just seems like a lot of code necessary to work around issues (and that's just to get started using it!), but you're right that gettimeofday and strftime have their own issues.

@theuni

theuni Jan 17, 2017

Member

@laanwj Posix uses a value since the Unix epoch, but time_t is implementation-defined according to the c spec. But it really doesn't matter, realistically it'll be the Unix epoch everywhere we run. Sorry for the distraction.

I only brought it up because the c++ system_clock's epoch is technically implementation-defined as well, I was just making the case that it's probably just as safe to assume as with c.

There's a few places in Bitcoin Core where non monotonic time stamps can cause the node to act strangely, for example if NTP tweaks the time backwards between a p2p PING and PONG, the int64 for node latency can overflow. Without much review, does this change any of the behavior in that respect? chrono::steady_clock assures monotone time where required.

Member

theuni commented Jan 17, 2017

@zstardust There is (should be) no behavioral change here. steady_clock would indeed make sense for some of those cases.

Owner

laanwj commented Jan 17, 2017

Agreed, using monotonic clock makes sense where only differences in time are important, when there is no need to print the time (as it will have an arbitrary starting offset). That's orthogonal to the changes here, though.

fanquake added this to the 0.15.0 milestone Jan 18, 2017

Contributor

TheBlueMatt commented Jan 23, 2017

I think we need to think more about how we use time everywhere, see eg the issues turned up in #9606 - do we want our own types for time to enforce that we dont compare mock and non-mock time? Do we want multiple types of mock times? etc.

Member

theuni commented Jan 23, 2017

@TheBlueMatt This is intended to just be a replacement of what we have now. The behavioral changes can come next.

I have an impl of a clock/time_point that I've made that is explicitly non-convertable from one type to another. Happy to PR that for discussion as well. I expect that it would replace our current timestamp usage, but I figured it'd be best to get rid of boost first, since the boost condvars use boost::chrono.

Member

jtimon commented Feb 1, 2017

Concept ACK. With C++ std or with C time functions. Seems like removing boost's dependency here is better irrespective of next steps.
Needs rebase.

+
+ // Check that the above zero time is actually equal to the known unix timestamp.
+ tm epoch = gmtime_int(nTime);
+ if ((epoch.tm_sec != 0) || \
@laanwj

laanwj Feb 1, 2017

Owner

Why the \ at the end of the line?

+ // to a time_t and verify that it's the same as before.
+ const time_t zeroTime{};
+ auto clock = std::chrono::system_clock::from_time_t(zeroTime);
+ if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0)
@laanwj

laanwj Feb 1, 2017

Owner

nit: bracing style: we decided to always use braces unless the if and the statement can be on one line: https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md

Owner

laanwj commented Mar 9, 2017

Needs rebase/nit fix.

Owner

laanwj commented Apr 19, 2017

Is this still in your plans @theuni?

theuni was assigned by laanwj Jun 26, 2017

Member

theuni commented Jun 27, 2017

Thanks for the reminder, I'll circle back to this.

Owner

sipa commented Jul 13, 2017

What is the status here? Going to make it for 0.15?

Member

theuni commented Jul 13, 2017

I'll go ahead and rebase/clean this up now, but it's not a priority for 0.15.

@laanwj laanwj modified the milestone: 0.16.0, 0.15.0 Jul 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment