You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
time: use monotonic clock to measure elapsed time #12914
Go's standard library doesn't provide any API to access a monotonic clock source, which means one can't reliably time how long an operation takes without copy-pasting or importing some low-level platform-dependent code such as the clock package at https://github.com/davecheney/junk.
This API doesn't necessarily need to return a time.Time, just returning a number of nanoseconds as a uint64 would be enough, kinda like Java's System.nanoTime().
Reacted by Aurélien Bombo, Ahmet Alp Balkan, manoj-nd, Javier Honduvilla Coto, Oleg Kovalov, Simon Howard, Florian Schlachter, leonklingele, Ali Altun, Jaseem Abid and 43 more
Go's standard library doesn't provide any API to access a monotonic clock
source, which means one can't reliably time how long an operation takes
without copy-pasting or importing some low-level platform-dependent code
such as the clock package at https://github.com/davecheney/junk.
Why should this be in the standard library: because it's a pretty fundamental thing, especially in a language geared towards systems programming. I see a lot of people timing how long operations take using time.Now() and subtracting two time.Time objects, and I always end up saying "this is not a correct way of measuring how much time elapsed" but don't have a clear alternative to propose.
Come on, even Java has it.
Reacted by Matt Johnson-Pint, Yao Yue, Chris Hills, Bishwa Subedi, Mathias Leppich, Harm Verhagen, RaduBerinde, Matt Joiner, robert rodgers, hbc and 6 more
I see a lot of people timing how long operations take using time.Now() and subtracting two time.Time objects, and I always end up saying "this is not a correct way of measuring how much time elapsed"
This API doesn't necessarily need to return a time.Time, just returning a number of nanoseconds as a uint64 would be enough, kinda like Java's System.nanoTime().
time.Duration happens to represent a 64-bit quantity nanoseconds, and, unless there's some large problem with it, should be favored as the return type for any monotonic duration handling functions.
On what platforms is time.Now() not monotonic? For linux/amd64, time.Now is implemented by runtime·nanotime, which is calling clock_gettime (via VDSO) with CLOCK_MONOTONIC. I see similar calls being used on linux/arm, openbsd, freebsd, etc.
Instead of a new interface, can we document time.Now as monotonic?
@crawshaw time.Now is not monotonic on any platform that supports settimeofday. On amd64 GNU/Linux time.Now does not actually use CLOCK_MONOTONIC, it uses CLOCK_REALTIME. It's runtime.nanotime that uses CLOCK_MONOTONIC.
On Mon, Oct 12, 2015 at 9:07 PM, Andrew Gerrand wrote:
I suppose what I'm getting at is this: If there's a broad need for such a package, why isn't there one that people are already using?
Because people incorrectly assume that time only moves forward? It's an extremely common mistake that even at Google we were trying to hammer in people's head. When you see the large impact that things such as leap seconds have (including at Google), it's easy to see this is a well known problem but also a recurring problem.
How is it that a language geared towards systems programming that comes with such a rich standard library offers no standard way of measuring time correctly?
Look at gRPC, everysingleuseoftime.Now() there is buggy because it implicitly assumes that time only moves forward and at a constant pace, neither of which is true for CLOCK_REALTIME. Pretty much any distributed systems code that uses time.Now() other than for printing the current time is subtly buggy. And there is a lot of it. And it's there chiefly because there is no alternative in the standard library to get monotonic time so people don't even think about it.
Reacted by Gyuho Lee, Shaun Crampton, Chris K, Orivej Desh, Gabriel Handford, Dan Luu, Willow Chargin, Leigh, Amir Abushareb, Pradeep Chhetri and 54 more
How is it that a language geared towards systems programming that comes with such a rich standard library offers no standard way of measuring time correctly?
I expect that if you care that much about time you will run your system clocks correctly. On a well-run system, time does only move forward and at a constant (enough) pace.
When you see the large impact that things such as leap seconds have (including at Google), it's easy to see this is a well known problem but also a recurring problem.
I don't understand this comment. Leap seconds don'texist at Google.
It is true that if you use time.Now to measure time during a leap smear then your measurements may be off by approximately 0.001%. But time will still be monotonic.
Reacted by Jesterreborn, Orivej Desh and as
Reacted by Kenyon Ralph, Tamir Duberstein, Ryan King, Alper Cugun, Brian Lomeland, Vivian Mathews, Jaseem Abid, Igor Dubinskiy, Fulvio, Diana and 19 more
OK, this is checked in for Go 1.9. I moved the internal epoch to 1885 (max year 2157) to avoid any possible problem with NTP-epoch-derived times. I also adjusted the design doc to reflect this change and some minor encoding changes.
Thanks for the constructive conversation everyone.
Reacted by Kaur Kuut, Sean Chittenden, UnHa Kim, Ryan Ju and Sen Han
Reacted by jimmyfrasche, Dmitri Shuralyov, Adrien Delorme, Tibor Vass, Eric Lagergren, Stevie Beth Mhaol, Mauro Toffanin, Sean Chittenden, Zach Klippenstein, UnHa Kim and 5 more
The merged implementation changes the meaning of time.Now().Sub(old) when now and old are separated by system suspend and resume: it used to calculate real time difference and now that difference is reduced by the duration the system was suspended. Shouldn't time use monotonic real time source when available, and if so, can runtime.nanotime be redefined to read from that source, or should there be a new clock specifically for time.Now() to read from?
Is there an explanation for + 1 in nanotime() - startNano + 1 in the design doc? If it is needed, why startNano is not reduced by 1 instead?
Whether time.Since(start) includes "system suspended" time depends on the underlying OS's monotonic clock, but in general it's tricky to add that time into the clock since you can't take it back if you later find out you are wrong. My preference here would be to do the same thing on all operating systems, whatever that is. My guess is that all operating systems can provide the current semantics but that not all can provide the "include suspended time" semantics.
It seems to me that if you want to include time while the computer was off, you're really talking about wall times, so the fix would be to flip into wall mode by recording 'start := time.Now().AddDate(0, 0, 0)', putting up with the fact that time.Since(start) might then return negative elapsed times.
The +1 is just to make sure that tests never see a 0 monotonic time, since I used GetMono(&t) == 0 to say that there's no monotonic time. It's only for testing, and even in that context it's probably unnecessary, but also harmless.
I'm looking at some code which assumes that times truncated to the same time are equal. Simplified:
funcmain() {
t1:=time.Now().Truncate(1*time.Second)
t2:=time.Now().Truncate(1*time.Second)
h1, m1, s1:=t1.Clock()
h2, m2, s2:=t2.Clock()
ifh1==h2&&m1==m2&&s1==s2 {
if!t1.Equal(t2) {
fmt.Printf("BUG: times truncated to the same second, but are not equal:\n%v\n%v\n", t1, t2)
}
}
}
The actual code is mapping times to windows; it assumes that all times within a given window will truncate to the same time.
This doesn't seem unreasonable. Should Truncate discard the monotonic time component?
Activity
adg commentedon Oct 13, 2015
Do we have a monotonic clock source we can export?
If not, why should this be in the standard library and not just an
importable package?
On 13 October 2015 at 12:42, Benoit Sigoure notifications@github.com
wrote:
tsuna commentedon Oct 13, 2015
Why should this be in the standard library: because it's a pretty fundamental thing, especially in a language geared towards systems programming. I see a lot of people timing how long operations take using
time.Now()and subtracting twotime.Timeobjects, and I always end up saying "this is not a correct way of measuring how much time elapsed" but don't have a clear alternative to propose.Come on, even Java has it.
adg commentedon Oct 13, 2015
Got any concrete examples?
tsuna commentedon Oct 13, 2015
Any concrete examples of ..? People using
time.Now()or it being wrong because it doesn't take into account changes made to the system time?adg commentedon Oct 13, 2015
I suppose what I'm getting at is this: If there's a broad need for such a package, why isn't there one that people are already using?
extemporalgenome commentedon Oct 13, 2015
time.Durationhappens to represent a 64-bit quantity nanoseconds, and, unless there's some large problem with it, should be favored as the return type for any monotonic duration handling functions.taruti commentedon Oct 21, 2015
I think there are two things:
A) A proper monotonic clock package (there are multiple 3rd party ones)
B) Measuring elapsed time on systems where system time may leap to either direction
B is simpler. Either ensure and document that
time.Sinceworkseven with clock shifts or define new API like:
crawshaw commentedon Oct 23, 2015
On what platforms is time.Now() not monotonic? For linux/amd64, time.Now is implemented by runtime·nanotime, which is calling clock_gettime (via VDSO) with CLOCK_MONOTONIC. I see similar calls being used on linux/arm, openbsd, freebsd, etc.
Instead of a new interface, can we document time.Now as monotonic?
ianlancetaylor commentedon Oct 23, 2015
@crawshaw time.Now is not monotonic on any platform that supports settimeofday. On amd64 GNU/Linux time.Now does not actually use CLOCK_MONOTONIC, it uses CLOCK_REALTIME. It's runtime.nanotime that uses CLOCK_MONOTONIC.
ianlancetaylor commentedon Oct 23, 2015
Sorry, I somehow missed that you mentioned nanotime yourself. That's not what implements time.Now, though, that is implemented by time·now.
crawshaw commentedon Oct 23, 2015
Ah right, thanks. I got confused by
//go:linkname time_runtimeNano time.runtimeNano, which I misread as implementing time.Nowalexbrainman commentedon Oct 23, 2015
time.Now() is monotonic on windows. We use undocumented way to fetch time, but it is monotonic as far as I know. @dvyukov to confirm.
Alex
tsuna commentedon Oct 23, 2015
On Mon, Oct 12, 2015 at 9:07 PM, Andrew Gerrand wrote:
Because people incorrectly assume that time only moves forward? It's an extremely common mistake that even at Google we were trying to hammer in people's head. When you see the large impact that things such as leap seconds have (including at Google), it's easy to see this is a well known problem but also a recurring problem.
How is it that a language geared towards systems programming that comes with such a rich standard library offers no standard way of measuring time correctly?
Look at gRPC, every single use of
time.Now()there is buggy because it implicitly assumes that time only moves forward and at a constant pace, neither of which is true forCLOCK_REALTIME. Pretty much any distributed systems code that usestime.Now()other than for printing the current time is subtly buggy. And there is a lot of it. And it's there chiefly because there is no alternative in the standard library to get monotonic time so people don't even think about it.rsc commentedon Oct 24, 2015
I expect that if you care that much about time you will run your system clocks correctly. On a well-run system, time does only move forward and at a constant (enough) pace.
I don't understand this comment. Leap seconds don't exist at Google.
It is true that if you use time.Now to measure time during a leap smear then your measurements may be off by approximately 0.001%. But time will still be monotonic.
153 remaining items
cespare commentedon Feb 3, 2017
Looks like the current code on master uses 1885-2157 as the range of wall times when monotonic time is present.
rsc commentedon Feb 3, 2017
OK, this is checked in for Go 1.9. I moved the internal epoch to 1885 (max year 2157) to avoid any possible problem with NTP-epoch-derived times. I also adjusted the design doc to reflect this change and some minor encoding changes.
Thanks for the constructive conversation everyone.
orivej commentedon Feb 3, 2017
The merged implementation changes the meaning of
time.Now().Sub(old)when now and old are separated by system suspend and resume: it used to calculate real time difference and now that difference is reduced by the duration the system was suspended. Shouldn'ttimeuse monotonic real time source when available, and if so, canruntime.nanotimebe redefined to read from that source, or should there be a new clock specifically fortime.Now()to read from?Is there an explanation for
+ 1innanotime() - startNano + 1in the design doc? If it is needed, whystartNanois not reduced by 1 instead?rsc commentedon Feb 3, 2017
@orivej,
Whether time.Since(start) includes "system suspended" time depends on the underlying OS's monotonic clock, but in general it's tricky to add that time into the clock since you can't take it back if you later find out you are wrong. My preference here would be to do the same thing on all operating systems, whatever that is. My guess is that all operating systems can provide the current semantics but that not all can provide the "include suspended time" semantics.
It seems to me that if you want to include time while the computer was off, you're really talking about wall times, so the fix would be to flip into wall mode by recording 'start := time.Now().AddDate(0, 0, 0)', putting up with the fact that time.Since(start) might then return negative elapsed times.
The +1 is just to make sure that tests never see a 0 monotonic time, since I used GetMono(&t) == 0 to say that there's no monotonic time. It's only for testing, and even in that context it's probably unnecessary, but also harmless.
gopherbot commentedon Feb 5, 2017
CL https://golang.org/cl/36334 mentions this issue.
time: Fix typo in Time.String() description.
neild commentedon Mar 1, 2017
I'm looking at some code which assumes that times truncated to the same time are equal. Simplified:
The actual code is mapping times to windows; it assumes that all times within a given window will truncate to the same time.
This doesn't seem unreasonable. Should Truncate discard the monotonic time component?
dmitshur commentedon Mar 1, 2017
@neild A relevant open issue is #18991.
Modify time first, round after (and strip additional precision).
pkg, bpf: add api to return monotonic clock
pkg, bpf: add api to return monotonic clock