Skip to content

Commit

Permalink
added time-change guard for fallback non-monotonic gettimeofday()
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasHabets committed Sep 4, 2010
1 parent 548e983 commit f36dc37
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/ei_errqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ handleRecvErrSEE(struct sock_extended_err *see,
}
if (lastPingTime) {
printf(" time=%.2f ms",
1000*(monotonic_get_dbl()-lastPingTime));
1000*(clock_get_dbl()-lastPingTime));
}
printf(": ");
}
Expand Down
43 changes: 31 additions & 12 deletions src/gtping.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ sendEcho(int fd, int seq)
argv0, curSeq, (int)packetlen);
}

sendTimes[seq % TRACKPINGS_SIZE] = monotonic_get_dbl();
sendTimes[seq % TRACKPINGS_SIZE] = clock_get_dbl();
gotIt[seq % TRACKPINGS_SIZE] = 0;

if (packetlen != send(fd, packet, packetlen, 0)) {
Expand Down Expand Up @@ -880,7 +880,7 @@ recvEchoReply(int fd)
fprintf(stderr, "%s: recvEchoReply()\n", argv0);
}

now = monotonic_get_dbl();
now = clock_get_dbl();

memset(packet, 0, sizeof(packet));
if (0 > (packetlen = doRecv(fd,
Expand Down Expand Up @@ -1027,7 +1027,7 @@ tracerouteMainloop(int fd)
fds.revents = 0;

/* time to send yet? */
curPingTime = monotonic_get_dbl();
curPingTime = clock_get_dbl();
if ((lastRecvTime >= lastPingTime)
|| (curPingTime > lastPingTime + options.interval)) {
if (printStar) {
Expand Down Expand Up @@ -1063,7 +1063,7 @@ tracerouteMainloop(int fd)
}

/* max waittime: until it's time to send the next one */
timewait = lastPingTime+options.interval - monotonic_get_dbl();
timewait = lastPingTime+options.interval - clock_get_dbl();
if (timewait < 0) {
timewait = 0;
}
Expand All @@ -1076,7 +1076,7 @@ tracerouteMainloop(int fd)
int e;
e = handleRecvErr(fd, NULL, lastPingTime);
if (e) {
lastRecvTime = monotonic_get_dbl();
lastRecvTime = clock_get_dbl();
}
if (e > 1) {
endOfTraceroute = 1;
Expand All @@ -1086,7 +1086,7 @@ tracerouteMainloop(int fd)
n = recvEchoReply(fd);
endOfTraceroute = 1;
if (!n) {
lastRecvTime = monotonic_get_dbl();
lastRecvTime = clock_get_dbl();
} else if (n > 0) {
/* still ok, but no reply */
printStar = 1;
Expand Down Expand Up @@ -1137,7 +1137,7 @@ pingMainloop(int fd)
fprintf(stderr, "%s: mainloop(%d)\n", argv0, fd);
}

startTime = monotonic_get_dbl();
startTime = clock_get_dbl();

printf("GTPING %s (%s) packet version %d\n",
options.target,
Expand All @@ -1162,7 +1162,14 @@ pingMainloop(int fd)
}

/* time to send yet? */
curPingTime = monotonic_get_dbl();
curPingTime = clock_get_dbl();

/* if clock is not monotonic and time set backwards
* since last ping, start a new ping cycle */
if (curPingTime < lastpingTime) {
lastpingTime = curPingTime - options.interval - 0.001;
}

if (curPingTime > lastpingTime + options.interval) {
if (options.count && (curSeq == options.count)) {
if (lastRecvTime+options.wait < curPingTime) {
Expand All @@ -1183,11 +1190,23 @@ pingMainloop(int fd)
fds.revents = 0;

/* max waittime: until it's time to send the next one */
timewait = lastpingTime+options.interval - monotonic_get_dbl();
timewait = lastpingTime+options.interval - clock_get_dbl();

/* never wait more than an interval. this can happen if
* clock is not monotonic */
if (timewait > options.interval) {
timewait = options.interval;
}

/* this should never happen, should have been taken care of
* above. */
if (timewait < 0) {
timewait = 0;
}
timewait *= 0.5; /* leave room for overhead */

/* leave room for overhead */
timewait *= 0.5;

switch ((n = poll(&fds, 1, (int)(timewait * 1000)))) {
case 1: /* read ready */
if (fds.revents & POLLERR) {
Expand All @@ -1199,7 +1218,7 @@ pingMainloop(int fd)
n = recvEchoReply(fd);
if (!n) {
recvd++;
lastRecvTime = monotonic_get_dbl();
lastRecvTime = clock_get_dbl();
} else if (n > 0) {
/* still ok, but no reply */
} else { /* n < 0 */
Expand Down Expand Up @@ -1239,7 +1258,7 @@ pingMainloop(int fd)
options.target,
sent, recvd,
(int)((100.0*(sent-recvd))/sent),
(int)(1000*(monotonic_get_dbl()-startTime)),
(int)(1000*(clock_get_dbl()-startTime)),
reorder, dups,
connectionRefused);
errInspectionPrintSummary();
Expand Down
2 changes: 1 addition & 1 deletion src/gtping.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int handleRecvErr(int fd, const char *reason, double lastPingTime);
const char *tos2String(int tos, char *buf, size_t buflen);
struct addrinfo* getIfAddrs(const struct addrinfo *dest);
int sockaddrlen(int af);
double monotonic_get_dbl();
double clock_get_dbl();

/* ---- Emacs Variables ----
* Local Variables:
Expand Down
2 changes: 1 addition & 1 deletion src/monotonic_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/
double
monotonic_get_dbl()
clock_get_dbl()
{
struct timespec ts;
struct timeval tv;
Expand Down
2 changes: 1 addition & 1 deletion src/monotonic_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/
double
monotonic_get_dbl()
clock_get_dbl()
{
struct timeval tv;
if (gettimeofday(&tv, NULL)) {
Expand Down

0 comments on commit f36dc37

Please sign in to comment.