Read local time immediately after select()

This removes a small inaccuracy caused by delay between select() and
file handler calls.
This commit is contained in:
Miroslav Lichvar 2009-11-30 13:19:55 +01:00
parent 159a9519e8
commit e9ae3d0a0b
5 changed files with 20 additions and 6 deletions

View file

@ -436,7 +436,6 @@ read_from_socket(void *anything)
IPAddr remote_ip;
int i, ok;
struct timeval now;
double local_time_err;
SourceRecord *src;
flags = 0;
@ -444,7 +443,7 @@ read_from_socket(void *anything)
his_addr_len = sizeof(his_addr);
/* Get timestamp */
LCL_ReadCookedTime(&now, &local_time_err);
SCH_GetFileReadyTime(&now);
sock_fd = (long)anything;
status = recvfrom (sock_fd, (char *)&msg, message_length, flags,

View file

@ -275,7 +275,6 @@ read_from_socket(void *anything)
unsigned int flags = 0;
struct timeval now;
NTP_Remote_Address remote_addr;
double local_clock_err;
char cmsgbuf[256];
struct msghdr msg;
struct iovec iov;
@ -283,7 +282,7 @@ read_from_socket(void *anything)
assert(initialised);
LCL_ReadCookedTime(&now, &local_clock_err);
SCH_GetFileReadyTime(&now);
iov.iov_base = message.arbitrary;
iov.iov_len = sizeof(message);

View file

@ -872,7 +872,6 @@ read_from_device(void *any)
struct rtc_time rtc_raw;
struct tm rtc_tm;
time_t rtc_t;
double read_err;
int error = 0;
status = read(fd, &data, sizeof(data));
@ -905,7 +904,7 @@ read_from_device(void *any)
/* Read RTC time, sandwiched between two polls of the system clock
so we can bound any error. */
LCL_ReadCookedTime(&sys_time, &read_err);
SCH_GetFileReadyTime(&sys_time);
status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
if (status < 0) {

13
sched.c
View file

@ -71,6 +71,9 @@ typedef struct {
static FileHandlerEntry file_handlers[FD_SET_SIZE];
/* Last timestamp when a file descriptor became readable */
static struct timeval last_fdready;
/* ================================================== */
/* Variables to handler the timer queue */
@ -225,6 +228,14 @@ SCH_RemoveInputFileHandler(int fd)
/* ================================================== */
void
SCH_GetFileReadyTime(struct timeval *tv)
{
*tv = last_fdready;
}
/* ================================================== */
#define TQE_ALLOC_QUANTUM 32
static TimerQueueEntry *
@ -514,6 +525,7 @@ SCH_MainLoop(void)
int status;
struct timeval tv, *ptv;
struct timeval now;
double err;
if (!initialised) {
CROAK("Should be initialised");
@ -551,6 +563,7 @@ SCH_MainLoop(void)
}
status = select(one_highest_fd, &rd, NULL, NULL, ptv);
LCL_ReadCookedTime(&last_fdready, &err);
if (status < 0) {
if (!need_to_exit)

View file

@ -60,6 +60,10 @@ extern void SCH_AddInputFileHandler
);
extern void SCH_RemoveInputFileHandler(int fd);
/* Get the time (cooked) when file descriptor became ready, intended for use
in file handlers */
extern void SCH_GetFileReadyTime(struct timeval *tv);
/* This queues a timeout to elapse at a given (raw) local time */
extern SCH_TimeoutID SCH_AddTimeout(struct timeval *tv, SCH_TimeoutHandler, SCH_ArbitraryArgument);