-
Notifications
You must be signed in to change notification settings - Fork 18k
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
time: inaccurate timer #14410
Comments
I'm not on the go team, but in general, timers are best-effort kinds of things in every general computing environment. Unless you're running on a real-time embedded operating system you can't rely on timer ticks to be accurate. You have two choices -- either spend cycles polling the real-time clock, and fire off your timer ticks yourself, or use the RTC to calculate the actual time spent (as you've done here) and adjust the result based on actual time elapsed. In game development, you generally build your physics engine to take a deltaT parameter and pass that in on every tick, so that variable-time ticks average out. If you rely on timers to occur on a fixed interval, generally you'll lag behind as ticks go long or get delayed. (Notice, by the way, that inside your timer loop you're doing a format and a print. That alone could be causing your problem.) If it were up to me, I would close this as not a bug. |
If this is a bug, then it's a bug in the linux kernel.
Try write the equivalent C program using usleep and gettimeofday and see
what happens.
|
I already knew the timers are best-effort kinds of things. |
I wrote this C code and test it many times, here is the result:
The C code: #include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#define TO_US(x) x.tv_sec*1e6 + x.tv_usec
int main(){
int i;
long us, us2;
struct timeval tv;
gettimeofday(&tv, NULL);
us=TO_US(tv);
for(i=0;i<20;i++){
usleep(10e3);
gettimeofday(&tv, NULL);
us2=TO_US(tv);
printf("%ld\n", us2-us);
us=us2;
}
} It may be a terrible problem of VM's provider, I'm sorry for opening this issue. |
This is old and closed, but I just saw it while looking for something else and wanted to respond. The timer is ticking at a fixed rate whether you read from the channel or not, and even if the program is for whatever reason slow to read from the channel. The channel carries the original send time, which shows more accurately what is going on:
prints
You can see the timer is targeting ticking every 10ms. Sometimes it is a little late to send, but it doesn't let that skew later results. Instead it keeps up at a steady one-per-10ms, provided the receiver keeps up receiving. |
go version go1.6 linux/arm64
ubuntu 14.04 kernel 4.2 aarch64
run "timer_testing.go"
print "10ms" 10 times
some inaccurate duration
Test code "timer_testing.go"
Run many times, I got some errors like this:
I have some programs that depend on accurate timing, those time error will result of some serious logic errors.
The text was updated successfully, but these errors were encountered: