Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
apinger: use clock_monotonic() when available #2
Browse files Browse the repository at this point in the history
  • Loading branch information
fichtner committed Apr 29, 2016
1 parent 5fd8e0f commit 9a330e4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AC_FUNC_FORK
AC_FUNC_MALLOC
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(clock_gettime)
AC_CHECK_FUNCS([gettimeofday inet_ntoa memset socket \
strdup strerror strpbrk poll vsyslog time popen setvbuf access],
[],AC_MSG_ERROR(some needed function is missing))
Expand Down
58 changes: 47 additions & 11 deletions src/apinger.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@
# define assert(cond)
#endif

#ifndef HAVE_CLOCK_GETTIME
#undef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 0
static int
clock_gettime(int clock_id, struct timespec *tp)
{
struct timeval now;
int ret;

(void)clock_id;

ret = gettimeofday(&now, NULL);
if (ret) {
return ret;
}

tp->tv_nsec = now.tv_usec * 1000;
tp->tv_sec = now.tv_sec;

return (0);
}
#endif

void
apinger_gettime(struct timeval *tp)
{
struct timespec now;

memset(&now, 0, sizeof(now));

if (clock_gettime(CLOCK_MONOTONIC, &now)) {
debug("System time fetch failed");
}

tp->tv_usec = now.tv_nsec / 1000;
tp->tv_sec = now.tv_sec;
}

#ifndef timerisset
# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#endif
Expand Down Expand Up @@ -118,7 +156,7 @@ void alarm_on(struct target *t,struct alarm_cfg *a){
struct active_alarm_list *al;
struct timeval cur_time,tv;

gettimeofday(&cur_time,NULL);
apinger_gettime(&cur_time);
al=NEW(struct active_alarm_list,1);
al->next=t->active_alarms;
al->alarm=a;
Expand Down Expand Up @@ -392,7 +430,7 @@ struct delayed_report *dr,*tdr;
dr->t=t;
dr->a=a;
dr->on=on;
gettimeofday(&dr->timestamp,NULL);
apinger_gettime(&dr->timestamp);
dr->next=NULL;
if (tdr==NULL)
delayed_reports=dr;
Expand All @@ -410,7 +448,7 @@ struct timeval ct,tv;
int ret;

if (cur_time==NULL){
gettimeofday(&ct,NULL);
apinger_gettime(&ct);
cur_time=&ct;
}
if (!timerisset(next_event) || timercmp(next_event,cur_time,<)){
Expand Down Expand Up @@ -824,7 +862,7 @@ configure_targets(struct config *cfg)
return (1);
}

gettimeofday(&operation_started, NULL);
apinger_gettime(&operation_started);

if (cfg->rrd_interval) {
rrd_create();
Expand Down Expand Up @@ -975,7 +1013,7 @@ main_loop(void)
memset(&pfd, '\0', sizeof pfd);

if (config->status_interval) {
gettimeofday(&cur_time, NULL);
apinger_gettime(&cur_time);
tv.tv_sec=config->status_interval / 1000;
tv.tv_usec=(config->status_interval % 1000) * 1000;
timeradd(&cur_time, &tv, &next_status);
Expand All @@ -984,7 +1022,7 @@ main_loop(void)
while (!interrupted_by) {
npfd = 0;

gettimeofday(&cur_time, NULL);
apinger_gettime(&cur_time);
if (!timercmp(&next_probe, &cur_time, >)) {
timerclear(&next_probe);
}
Expand Down Expand Up @@ -1025,7 +1063,7 @@ main_loop(void)
}
}

gettimeofday(&event_time, NULL);
apinger_gettime(&event_time);
if (reload_request) {
reload_request = 0;
logit("SIGHUP received, reloading configuration.");
Expand Down Expand Up @@ -1099,17 +1137,15 @@ main_loop(void)
}
}

#if 0
{
char buf[100];

strftime(buf, sizeof(buf), "%b %d %H:%M:%S",
localtime(&next_probe.tv_sec));
debug("Next event scheduled for %s", buf);
}
#endif

gettimeofday(&cur_time, NULL);
apinger_gettime(&cur_time);
if (timercmp(&cur_time, &event_time, <)) {
timedelta = 0;
} else {
Expand All @@ -1126,7 +1162,7 @@ main_loop(void)
if (poll(pfd, npfd, timeout) < 0) {
continue;
}
gettimeofday(&cur_time, NULL);
apinger_gettime(&cur_time);

for (i = 0; i < npfd; i++) {
if (!(pfd[i].revents & POLLIN)) {
Expand Down
2 changes: 2 additions & 0 deletions src/apinger.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ extern uint16_t ident;

extern struct timeval next_probe;

void apinger_gettime(struct timeval *tp);

int make_icmp_socket(struct target *t);
void recv_icmp(struct target *t, struct timeval *, int);
void send_icmp_probe(struct target *t,int seq);
Expand Down
4 changes: 2 additions & 2 deletions src/icmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ int ret;
p->icmp_id=ident;

#ifdef HAVE_SCHED_YIELD
/* Give away our time now, or we may be stopped between gettimeofday() and sendto() */
/* Give away our time now, or we may be stopped between apinger_gettime() and sendto() */
sched_yield();
#endif
gettimeofday(&cur_time,NULL);
apinger_gettime(&cur_time);
ti.timestamp=cur_time;
ti.target_id=t;
ti.seq=seq;
Expand Down
4 changes: 2 additions & 2 deletions src/icmp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ int ret;
p->icmp6_id=ident;

#ifdef HAVE_SCHED_YIELD
/* Give away our time now, or we may be stopped between gettimeofday() and sendto() */
/* Give away our time now, or we may be stopped between apinger_gettime() and sendto() */
sched_yield();
#endif
gettimeofday(&cur_time,NULL);
apinger_gettime(&cur_time);
ti.timestamp=cur_time;
ti.target_id=t;
ti.seq=seq;
Expand Down

0 comments on commit 9a330e4

Please sign in to comment.