0

I have tasks that need to run at reasonably precise intervals in the range of one second.

I use gettimeofday() to establish start_usec. After task execution is done, it calls a timedSleep() function. timedSleep() invokes gettimeofday() to calculate sleep time in microseconds, increments the calling function's start_usec variable, then calls usleep().

It works perfectly, except that about once a minute it will 'skip' - the timedSleep function returns after one second (as close as I can tell), but gettimeofday returns a value that's three or four seconds ahead.

Here's a code snippet followed by some output. On the last line, time has moved forward by about four seconds, but the actual elapsed time was just a second.

The BeagleBone is running a Debian distro that uses timesyncd. Could that be causing this behavior?

struct timeval now;
gettimeofday(&now,NULL);
printf("Start: %02d %06d Now: %02d %06d... ",(int)((start_usec / 1000000) %100), (int)(start_usec % 1000000),  now.tv_sec %100, + now.tv_usec);

// Sleep for desired interval
shm.timedSleep(rule_id, &start_usec);

gettimeofday(&now,NULL);
printf("%02d %06d ",now.tv_sec %100, + now.tv_usec);
time(&curtime);
loctime = localtime(&curtime);
strftime(msgbuff, sizeof(msgbuff), "%Y-%m-%dT%H:%M:%S",loctime);
printf("%s\n",msgbuff);


Start: 78 117538 Now: 04 355933... 05 354979 2017-11-15T13:58:25
Start: 79 117538 Now: 05 355818... 06 354980 2017-11-15T13:58:26
Start: 80 117538 Now: 06 355816... 07 354981 2017-11-15T13:58:27
Start: 81 117538 Now: 07 355819... 08 354981 2017-11-15T13:58:28
Start: 82 117538 Now: 08 355819... 09 354979 2017-11-15T13:58:29
Start: 83 117538 Now: 09 355232... 10 354838 2017-11-15T13:58:30
Start: 84 117538 Now: 10 355776... 14 041276 2017-11-15T13:58:34

This BeagleBone has an external RTC which seems to be fine:

root@vesta:/usr/local/vesta/bbb/src# timedatectl
      Local time: Wed 2017-11-15 14:24:06 EST
  Universal time: Wed 2017-11-15 19:24:06 UTC
        RTC time: Wed 2017-11-15 19:24:06
       Time zone: America/New_York (EST, -0500)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no
pbft
  • 41
  • 5

1 Answers1

0

OK - it's timesyncd. The BeagleBone system clock is painfully slow, and timesyncd bumps it a few seconds every minute or so. Stopping timesyncd eliminates the problem. Now investigating ntp as an alternative.

pbft
  • 41
  • 5